Merged revisions 196716,196830,198094,198116,198502,198877,199007,199262,199319,19946...
[official-gcc.git] / main / gcc / ipa.c
blob4cb4b96ddf4de9b10b91edc92e2d04e3bf18a29b
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "cgraph.h"
25 #include "toplev.h"
26 #include "tree-pass.h"
27 #include "gimple.h"
28 #include "ggc.h"
29 #include "flags.h"
30 #include "pointer-set.h"
31 #include "target.h"
32 #include "tree-iterator.h"
33 #include "l-ipo.h"
34 #include "ipa-utils.h"
35 #include "ipa-inline.h"
36 #include "tree-inline.h"
37 #include "profile.h"
38 #include "params.h"
40 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
42 static bool
43 cgraph_non_local_node_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
45 /* FIXME: Aliases can be local, but i386 gets thunks wrong then. */
46 return !(cgraph_only_called_directly_or_aliased_p (node)
47 && !ipa_ref_has_aliases_p (&node->symbol.ref_list)
48 && node->symbol.definition
49 && !DECL_EXTERNAL (node->symbol.decl)
50 && !node->symbol.externally_visible
51 && !node->symbol.used_from_other_partition
52 && !node->symbol.in_other_partition);
55 /* Return true when function can be marked local. */
57 static bool
58 cgraph_local_node_p (struct cgraph_node *node)
60 struct cgraph_node *n = cgraph_function_or_thunk_node (node, NULL);
62 /* FIXME: thunks can be considered local, but we need prevent i386
63 from attempting to change calling convention of them. */
64 if (n->thunk.thunk_p)
65 return false;
66 return !cgraph_for_node_and_aliases (n,
67 cgraph_non_local_node_p_1, NULL, true);
71 /* Return true when NODE has ADDR reference. */
73 static bool
74 has_addr_references_p (struct cgraph_node *node,
75 void *data ATTRIBUTE_UNUSED)
77 int i;
78 struct ipa_ref *ref;
80 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list,
81 i, ref); i++)
82 if (ref->use == IPA_REF_ADDR)
83 return true;
84 return false;
87 /* Look for all functions inlined to NODE and update their inlined_to pointers
88 to INLINED_TO. */
90 static void
91 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
93 struct cgraph_edge *e;
94 for (e = node->callees; e; e = e->next_callee)
95 if (e->callee->global.inlined_to)
97 e->callee->global.inlined_to = inlined_to;
98 update_inlined_to_pointer (e->callee, inlined_to);
102 /* Add symtab NODE to queue starting at FIRST.
104 The queue is linked via AUX pointers and terminated by pointer to 1.
105 We enqueue nodes at two occasions: when we find them reachable or when we find
106 their bodies needed for further clonning. In the second case we mark them
107 by pointer to 2 after processing so they are re-queue when they become
108 reachable. */
110 static void
111 enqueue_node (symtab_node node, symtab_node *first,
112 struct pointer_set_t *reachable)
114 /* Node is still in queue; do nothing. */
115 if (node->symbol.aux && node->symbol.aux != (void *) 2)
116 return;
117 /* Node was already processed as unreachable, re-enqueue
118 only if it became reachable now. */
119 if (node->symbol.aux == (void *)2 && !pointer_set_contains (reachable, node))
120 return;
121 node->symbol.aux = *first;
122 *first = node;
125 /* Process references. */
127 static void
128 process_references (struct ipa_ref_list *list,
129 symtab_node *first,
130 bool before_inlining_p,
131 struct pointer_set_t *reachable)
133 int i;
134 struct ipa_ref *ref;
135 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
137 symtab_node node = ref->referred;
139 if (node->symbol.definition && !node->symbol.in_other_partition
140 && ((!(DECL_EXTERNAL (node->symbol.decl)
141 || (is_a <cgraph_node> (node)
142 && cgraph_is_aux_decl_external (dyn_cast<cgraph_node> (node))))
143 || node->symbol.alias)
144 || (before_inlining_p
145 /* We use variable constructors during late complation for
146 constant folding. Keep references alive so partitioning
147 knows about potential references. */
148 || (TREE_CODE (node->symbol.decl) == VAR_DECL
149 && flag_wpa
150 && ctor_for_folding (node->symbol.decl)
151 != error_mark_node))))
152 pointer_set_insert (reachable, node);
153 enqueue_node ((symtab_node) node, first, reachable);
157 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
158 all its potential targets as reachable to permit later inlining if
159 devirtualization happens. After inlining still keep their declarations
160 around, so we can devirtualize to a direct call.
162 Also try to make trivial devirutalization when no or only one target is
163 possible. */
165 static void
166 walk_polymorphic_call_targets (pointer_set_t *reachable_call_targets,
167 struct cgraph_edge *edge,
168 symtab_node *first,
169 pointer_set_t *reachable, bool before_inlining_p)
171 unsigned int i;
172 void *cache_token;
173 bool final;
174 vec <cgraph_node *>targets
175 = possible_polymorphic_call_targets
176 (edge, &final, &cache_token);
178 if (!pointer_set_insert (reachable_call_targets,
179 cache_token))
181 for (i = 0; i < targets.length (); i++)
183 struct cgraph_node *n = targets[i];
185 /* Do not bother to mark virtual methods in anonymous namespace;
186 either we will find use of virtual table defining it, or it is
187 unused. */
188 if (TREE_CODE (TREE_TYPE (n->symbol.decl)) == METHOD_TYPE
189 && type_in_anonymous_namespace_p
190 (method_class_type (TREE_TYPE (n->symbol.decl))))
191 continue;
193 /* Prior inlining, keep alive bodies of possible targets for
194 devirtualization. */
195 if (n->symbol.definition
196 && before_inlining_p)
197 pointer_set_insert (reachable, n);
199 /* Even after inlining we want to keep the possible targets in the
200 boundary, so late passes can still produce direct call even if
201 the chance for inlining is lost. */
202 enqueue_node ((symtab_node) n, first, reachable);
206 /* Very trivial devirtualization; when the type is
207 final or anonymous (so we know all its derivation)
208 and there is only one possible virtual call target,
209 make the edge direct. */
210 if (final)
212 if (targets.length () <= 1)
214 cgraph_node *target, *node = edge->caller;
215 if (targets.length () == 1)
216 target = targets[0];
217 else
218 target = cgraph_get_create_node
219 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
221 if (dump_file)
222 fprintf (dump_file,
223 "Devirtualizing call in %s/%i to %s/%i\n",
224 cgraph_node_name (edge->caller),
225 edge->caller->symbol.order,
226 cgraph_node_name (target), target->symbol.order);
227 edge = cgraph_make_edge_direct (edge, target);
228 if (!inline_summary_vec && edge->call_stmt)
229 cgraph_redirect_edge_call_stmt_to_callee (edge);
230 else
231 inline_update_overall_summary (node);
236 /* Perform reachability analysis and reclaim all unreachable nodes.
238 The algorithm is basically mark&sweep but with some extra refinements:
240 - reachable extern inline functions needs special handling; the bodies needs
241 to stay in memory until inlining in hope that they will be inlined.
242 After inlining we release their bodies and turn them into unanalyzed
243 nodes even when they are reachable.
245 BEFORE_INLINING_P specify whether we are before or after inlining.
247 - virtual functions are kept in callgraph even if they seem unreachable in
248 hope calls to them will be devirtualized.
250 Again we remove them after inlining. In late optimization some
251 devirtualization may happen, but it is not importnat since we won't inline
252 the call. In theory early opts and IPA should work out all important cases.
254 - virtual clones needs bodies of their origins for later materialization;
255 this means that we want to keep the body even if the origin is unreachable
256 otherwise. To avoid origin from sitting in the callgraph and being
257 walked by IPA passes, we turn them into unanalyzed nodes with body
258 defined.
260 We maintain set of function declaration where body needs to stay in
261 body_needed_for_clonning
263 Inline clones represent special case: their declaration match the
264 declaration of origin and cgraph_remove_node already knows how to
265 reshape callgraph and preserve body when offline copy of function or
266 inline clone is being removed.
268 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
269 variables with DECL_INITIAL set. We finalize these and keep reachable
270 ones around for constant folding purposes. After inlining we however
271 stop walking their references to let everything static referneced by them
272 to be removed when it is otherwise unreachable.
274 We maintain queue of both reachable symbols (i.e. defined symbols that needs
275 to stay) and symbols that are in boundary (i.e. external symbols referenced
276 by reachable symbols or origins of clones). The queue is represented
277 as linked list by AUX pointer terminated by 1.
279 A the end we keep all reachable symbols. For symbols in boundary we always
280 turn definition into a declaration, but we may keep function body around
281 based on body_needed_for_clonning
283 All symbols that enter the queue have AUX pointer non-zero and are in the
284 boundary. Pointer set REACHABLE is used to track reachable symbols.
286 Every symbol can be visited twice - once as part of boundary and once
287 as real reachable symbol. enqueue_node needs to decide whether the
288 node needs to be re-queued for second processing. For this purpose
289 we set AUX pointer of processed symbols in the boundary to constant 2. */
291 bool
292 symtab_remove_unreachable_nodes (bool before_inlining_p, FILE *file)
294 symtab_node first = (symtab_node) (void *) 1;
295 struct cgraph_node *node, *next;
296 struct varpool_node *vnode, *vnext;
297 bool changed = false;
298 struct pointer_set_t *reachable = pointer_set_create ();
299 struct pointer_set_t *body_needed_for_clonning = pointer_set_create ();
300 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
302 /* In LIPO mode, do not remove functions until after global linking
303 is performed. Otherwise functions needed for cross module inlining
304 may get eliminated. Global linking will be done just before tree
305 profiling. */
306 if (L_IPO_COMP_MODE
307 && !cgraph_pre_profiling_inlining_done)
308 return false;
310 timevar_push (TV_IPA_UNREACHABLE);
311 #ifdef ENABLE_CHECKING
312 verify_symtab ();
313 #endif
314 if (optimize && flag_devirtualize)
315 build_type_inheritance_graph ();
316 if (file)
317 fprintf (file, "\nReclaiming functions:");
318 #ifdef ENABLE_CHECKING
319 FOR_EACH_FUNCTION (node)
320 gcc_assert (!node->symbol.aux);
321 FOR_EACH_VARIABLE (vnode)
322 gcc_assert (!vnode->symbol.aux);
323 #endif
324 /* Mark functions whose bodies are obviously needed.
325 This is mostly when they can be referenced externally. Inline clones
326 are special since their declarations are shared with master clone and thus
327 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
328 FOR_EACH_FUNCTION (node)
330 node->used_as_abstract_origin = false;
331 if (node->symbol.definition
332 && !node->global.inlined_to
333 && !node->symbol.in_other_partition
334 && !cgraph_can_remove_if_no_direct_calls_and_refs_p (node))
336 gcc_assert (!node->global.inlined_to);
337 pointer_set_insert (reachable, node);
338 enqueue_node ((symtab_node)node, &first, reachable);
340 else
341 gcc_assert (!node->symbol.aux);
344 /* Mark variables that are obviously needed. */
345 FOR_EACH_DEFINED_VARIABLE (vnode)
346 if (!varpool_can_remove_if_no_refs (vnode)
347 && !vnode->symbol.in_other_partition)
349 pointer_set_insert (reachable, vnode);
350 enqueue_node ((symtab_node)vnode, &first, reachable);
353 /* Perform reachability analysis. */
354 while (first != (symtab_node) (void *) 1)
356 bool in_boundary_p = !pointer_set_contains (reachable, first);
357 symtab_node node = first;
359 first = (symtab_node)first->symbol.aux;
361 /* If we are processing symbol in boundary, mark its AUX pointer for
362 possible later re-processing in enqueue_node. */
363 if (in_boundary_p)
364 node->symbol.aux = (void *)2;
365 else
367 if (DECL_ABSTRACT_ORIGIN (node->symbol.decl))
369 struct cgraph_node *origin_node
370 = cgraph_get_create_real_symbol_node (DECL_ABSTRACT_ORIGIN (node->symbol.decl));
371 origin_node->used_as_abstract_origin = true;
372 enqueue_node ((symtab_node) origin_node, &first, reachable);
374 /* If any symbol in a comdat group is reachable, force
375 all other in the same comdat group to be also reachable. */
376 if (node->symbol.same_comdat_group)
378 symtab_node next;
379 for (next = node->symbol.same_comdat_group;
380 next != node;
381 next = next->symbol.same_comdat_group)
382 if (!pointer_set_insert (reachable, next))
383 enqueue_node ((symtab_node) next, &first, reachable);
385 /* Mark references as reachable. */
386 process_references (&node->symbol.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->symbol.definition
413 && !e->callee->symbol.in_other_partition
414 && (!e->inline_failed
415 || !(DECL_EXTERNAL (e->callee->symbol.decl)
416 || cgraph_is_aux_decl_external (e->callee))
417 || e->callee->symbol.alias
418 || before_inlining_p))
419 pointer_set_insert (reachable, e->callee);
420 enqueue_node ((symtab_node) e->callee, &first, reachable);
423 /* When inline clone exists, mark body to be preserved so when removing
424 offline copy of the function we don't kill it. */
425 if (cnode->global.inlined_to)
426 pointer_set_insert (body_needed_for_clonning, cnode->symbol.decl);
428 /* For non-inline clones, force their origins to the boundary and ensure
429 that body is not removed. */
430 while (cnode->clone_of)
432 bool noninline = cnode->clone_of->symbol.decl != cnode->symbol.decl;
433 cnode = cnode->clone_of;
434 if (noninline)
436 pointer_set_insert (body_needed_for_clonning, cnode->symbol.decl);
437 enqueue_node ((symtab_node)cnode, &first, reachable);
442 /* When we see constructor of external variable, keep referred nodes in the
443 boundary. This will also hold initializers of the external vars NODE
444 refers to. */
445 varpool_node *vnode = dyn_cast <varpool_node> (node);
446 if (vnode
447 && DECL_EXTERNAL (node->symbol.decl)
448 && !vnode->symbol.alias
449 && in_boundary_p)
451 struct ipa_ref *ref;
452 for (int i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
453 enqueue_node (ref->referred, &first, reachable);
457 /* Remove unreachable functions. */
458 for (node = cgraph_first_function (); node; node = next)
460 next = cgraph_next_function (node);
462 /* If node is not needed at all, remove it. */
463 if (!node->symbol.aux)
465 if (file)
466 fprintf (file, " %s", cgraph_node_name (node));
467 cgraph_remove_node (node);
468 changed = true;
470 /* If node is unreachable, remove its body. */
471 else if (!pointer_set_contains (reachable, node))
473 if (!pointer_set_contains (body_needed_for_clonning, node->symbol.decl))
474 cgraph_release_function_body (node);
475 else if (!node->clone_of)
476 gcc_assert (in_lto_p || DECL_RESULT (node->symbol.decl));
477 if (node->symbol.definition)
479 if (file)
480 fprintf (file, " %s", cgraph_node_name (node));
481 node->symbol.analyzed = false;
482 node->symbol.definition = false;
483 node->symbol.cpp_implicit_alias = false;
484 node->symbol.alias = false;
485 node->symbol.weakref = false;
486 if (!node->symbol.in_other_partition)
487 node->local.local = false;
488 #ifdef FIXME_LIPO
489 error " Check the following code "
490 #endif
491 if (!cgraph_is_aux_decl_external (node)) {
492 cgraph_node_remove_callees (node);
493 ipa_remove_all_references (&node->symbol.ref_list);
495 changed = true;
498 else
499 gcc_assert (node->clone_of || !cgraph_function_with_gimple_body_p (node)
500 || in_lto_p || DECL_RESULT (node->symbol.decl));
503 /* Inline clones might be kept around so their materializing allows further
504 cloning. If the function the clone is inlined into is removed, we need
505 to turn it into normal cone. */
506 FOR_EACH_FUNCTION (node)
508 if (node->global.inlined_to
509 && !node->callers)
511 /* Clean up dangling references from callees as well.
512 TODO -- should be done recursively. */
513 if (L_IPO_COMP_MODE)
515 struct cgraph_edge *e;
516 for (e = node->callees; e; e = e->next_callee)
518 struct cgraph_node *callee_node;
520 callee_node = e->callee;
521 if (callee_node->global.inlined_to)
522 callee_node->global.inlined_to = node;
525 gcc_assert (node->clones);
526 node->global.inlined_to = NULL;
527 update_inlined_to_pointer (node, node);
529 node->symbol.aux = NULL;
532 /* Remove unreachable variables. */
533 if (file)
534 fprintf (file, "\n");
536 if (file)
537 fprintf (file, "Reclaiming variables:");
538 for (vnode = varpool_first_variable (); vnode; vnode = vnext)
540 vnext = varpool_next_variable (vnode);
541 if (!vnode->symbol.aux
542 /* For can_refer_decl_in_current_unit_p we want to track for
543 all external variables if they are defined in other partition
544 or not. */
545 && (!flag_ltrans || !DECL_EXTERNAL (vnode->symbol.decl)))
547 if (file)
548 fprintf (file, " %s", varpool_node_name (vnode));
549 varpool_remove_node (vnode);
550 changed = true;
552 else if (!pointer_set_contains (reachable, vnode))
554 tree init;
555 if (vnode->symbol.definition)
557 if (file)
558 fprintf (file, " %s", varpool_node_name (vnode));
559 changed = true;
561 vnode->symbol.definition = false;
562 vnode->symbol.analyzed = false;
563 vnode->symbol.aux = NULL;
565 /* Keep body if it may be useful for constant folding. */
566 if ((init = ctor_for_folding (vnode->symbol.decl)) == error_mark_node)
567 varpool_remove_initializer (vnode);
568 else
569 DECL_INITIAL (vnode->symbol.decl) = init;
570 ipa_remove_all_references (&vnode->symbol.ref_list);
572 else
573 vnode->symbol.aux = NULL;
576 pointer_set_destroy (reachable);
577 pointer_set_destroy (body_needed_for_clonning);
578 pointer_set_destroy (reachable_call_targets);
580 /* Now update address_taken flags and try to promote functions to be local. */
581 if (file)
582 fprintf (file, "\nClearing address taken flags:");
583 FOR_EACH_DEFINED_FUNCTION (node)
584 if (node->symbol.address_taken
585 && !node->symbol.used_from_other_partition)
587 if (!cgraph_for_node_and_aliases (node, has_addr_references_p, NULL, true))
589 if (file)
590 fprintf (file, " %s", cgraph_node_name (node));
591 node->symbol.address_taken = false;
592 changed = true;
593 if (cgraph_local_node_p (node))
595 node->local.local = true;
596 if (file)
597 fprintf (file, " (local)");
601 if (file)
602 fprintf (file, "\n");
604 #ifdef ENABLE_CHECKING
605 verify_symtab ();
606 #endif
608 /* If we removed something, perhaps profile could be improved. */
609 if (changed && optimize && inline_edge_summary_vec.exists ())
610 FOR_EACH_DEFINED_FUNCTION (node)
611 ipa_propagate_frequency (node);
613 timevar_pop (TV_IPA_UNREACHABLE);
614 return changed;
617 /* Discover variables that have no longer address taken or that are read only
618 and update their flags.
620 FIXME: This can not be done in between gimplify and omp_expand since
621 readonly flag plays role on what is shared and what is not. Currently we do
622 this transformation as part of whole program visibility and re-do at
623 ipa-reference pass (to take into account clonning), but it would
624 make sense to do it before early optimizations. */
626 void
627 ipa_discover_readonly_nonaddressable_vars (void)
629 struct varpool_node *vnode;
630 if (dump_file)
631 fprintf (dump_file, "Clearing variable flags:");
632 FOR_EACH_VARIABLE (vnode)
633 if (vnode->symbol.definition && varpool_all_refs_explicit_p (vnode)
634 && (TREE_ADDRESSABLE (vnode->symbol.decl)
635 || !TREE_READONLY (vnode->symbol.decl)))
637 bool written = false;
638 bool address_taken = false;
639 int i;
640 struct ipa_ref *ref;
641 for (i = 0; ipa_ref_list_referring_iterate (&vnode->symbol.ref_list,
642 i, ref)
643 && (!written || !address_taken); i++)
644 switch (ref->use)
646 case IPA_REF_ADDR:
647 address_taken = true;
648 break;
649 case IPA_REF_LOAD:
650 break;
651 case IPA_REF_STORE:
652 written = true;
653 break;
655 if (TREE_ADDRESSABLE (vnode->symbol.decl) && !address_taken)
657 if (dump_file)
658 fprintf (dump_file, " %s (addressable)", varpool_node_name (vnode));
659 TREE_ADDRESSABLE (vnode->symbol.decl) = 0;
661 if (!TREE_READONLY (vnode->symbol.decl) && !address_taken && !written
662 /* Making variable in explicit section readonly can cause section
663 type conflict.
664 See e.g. gcc.c-torture/compile/pr23237.c */
665 && DECL_SECTION_NAME (vnode->symbol.decl) == NULL)
667 if (dump_file)
668 fprintf (dump_file, " %s (read-only)", varpool_node_name (vnode));
669 TREE_READONLY (vnode->symbol.decl) = 1;
672 if (dump_file)
673 fprintf (dump_file, "\n");
676 /* Return true when there is a reference to node and it is not vtable. */
677 static bool
678 address_taken_from_non_vtable_p (symtab_node node)
680 int i;
681 struct ipa_ref *ref;
682 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list,
683 i, ref); i++)
684 if (ref->use == IPA_REF_ADDR)
686 struct varpool_node *node;
687 if (is_a <cgraph_node> (ref->referring))
688 return true;
689 node = ipa_ref_referring_varpool_node (ref);
690 if (!DECL_VIRTUAL_P (node->symbol.decl))
691 return true;
693 return false;
696 /* A helper for comdat_can_be_unshared_p. */
698 static bool
699 comdat_can_be_unshared_p_1 (symtab_node node)
701 /* When address is taken, we don't know if equality comparison won't
702 break eventually. Exception are virutal functions, C++
703 constructors/destructors and vtables, where this is not possible by
704 language standard. */
705 if (!DECL_VIRTUAL_P (node->symbol.decl)
706 && (TREE_CODE (node->symbol.decl) != FUNCTION_DECL
707 || (!DECL_CXX_CONSTRUCTOR_P (node->symbol.decl)
708 && !DECL_CXX_DESTRUCTOR_P (node->symbol.decl)))
709 && address_taken_from_non_vtable_p (node))
710 return false;
712 /* If the symbol is used in some weird way, better to not touch it. */
713 if (node->symbol.force_output)
714 return false;
716 /* Explicit instantiations needs to be output when possibly
717 used externally. */
718 if (node->symbol.forced_by_abi
719 && TREE_PUBLIC (node->symbol.decl)
720 && (node->symbol.resolution != LDPR_PREVAILING_DEF_IRONLY
721 && !flag_whole_program))
722 return false;
724 /* Non-readonly and volatile variables can not be duplicated. */
725 if (is_a <varpool_node> (node)
726 && (!TREE_READONLY (node->symbol.decl)
727 || TREE_THIS_VOLATILE (node->symbol.decl)))
728 return false;
729 return true;
732 /* COMDAT functions must be shared only if they have address taken,
733 otherwise we can produce our own private implementation with
734 -fwhole-program.
735 Return true when turning COMDAT functoin static can not lead to wrong
736 code when the resulting object links with a library defining same COMDAT.
738 Virtual functions do have their addresses taken from the vtables,
739 but in C++ there is no way to compare their addresses for equality. */
741 static bool
742 comdat_can_be_unshared_p (symtab_node node)
744 if (!comdat_can_be_unshared_p_1 (node))
745 return false;
746 if (node->symbol.same_comdat_group)
748 symtab_node next;
750 /* If more than one function is in the same COMDAT group, it must
751 be shared even if just one function in the comdat group has
752 address taken. */
753 for (next = node->symbol.same_comdat_group;
754 next != node; next = next->symbol.same_comdat_group)
755 if (!comdat_can_be_unshared_p_1 (next))
756 return false;
758 return true;
761 /* Return true when function NODE should be considered externally visible. */
763 static bool
764 cgraph_externally_visible_p (struct cgraph_node *node,
765 bool whole_program)
767 if (!node->symbol.definition)
768 return false;
769 if (!TREE_PUBLIC (node->symbol.decl)
770 || DECL_EXTERNAL (node->symbol.decl))
771 return false;
773 /* Do not try to localize built-in functions yet. One of problems is that we
774 end up mangling their asm for WHOPR that makes it impossible to call them
775 using the implicit built-in declarations anymore. Similarly this enables
776 us to remove them as unreachable before actual calls may appear during
777 expansion or folding. */
778 if (DECL_BUILT_IN (node->symbol.decl))
779 return true;
781 /* If linker counts on us, we must preserve the function. */
782 if (symtab_used_from_object_file_p ((symtab_node) node))
783 return true;
784 if (DECL_PRESERVE_P (node->symbol.decl))
785 return true;
786 if (lookup_attribute ("externally_visible",
787 DECL_ATTRIBUTES (node->symbol.decl)))
788 return true;
789 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
790 && lookup_attribute ("dllexport",
791 DECL_ATTRIBUTES (node->symbol.decl)))
792 return true;
793 if (node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY)
794 return false;
795 /* When doing LTO or whole program, we can bring COMDAT functoins static.
796 This improves code quality and we know we will duplicate them at most twice
797 (in the case that we are not using plugin and link with object file
798 implementing same COMDAT) */
799 if ((in_lto_p || whole_program)
800 && DECL_COMDAT (node->symbol.decl)
801 && comdat_can_be_unshared_p ((symtab_node) node))
802 return false;
804 /* When doing link time optimizations, hidden symbols become local. */
805 if (in_lto_p
806 && (DECL_VISIBILITY (node->symbol.decl) == VISIBILITY_HIDDEN
807 || DECL_VISIBILITY (node->symbol.decl) == VISIBILITY_INTERNAL)
808 /* Be sure that node is defined in IR file, not in other object
809 file. In that case we don't set used_from_other_object_file. */
810 && node->symbol.definition)
812 else if (!whole_program)
813 return true;
815 if (MAIN_NAME_P (DECL_NAME (node->symbol.decl)))
816 return true;
818 return false;
821 /* Return true when variable VNODE should be considered externally visible. */
823 bool
824 varpool_externally_visible_p (struct varpool_node *vnode)
826 if (DECL_EXTERNAL (vnode->symbol.decl))
827 return true;
829 if (!TREE_PUBLIC (vnode->symbol.decl))
830 return false;
832 /* If linker counts on us, we must preserve the function. */
833 if (symtab_used_from_object_file_p ((symtab_node) vnode))
834 return true;
836 if (DECL_HARD_REGISTER (vnode->symbol.decl))
837 return true;
838 if (DECL_PRESERVE_P (vnode->symbol.decl))
839 return true;
840 if (lookup_attribute ("externally_visible",
841 DECL_ATTRIBUTES (vnode->symbol.decl)))
842 return true;
843 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
844 && lookup_attribute ("dllexport",
845 DECL_ATTRIBUTES (vnode->symbol.decl)))
846 return true;
848 /* See if we have linker information about symbol not being used or
849 if we need to make guess based on the declaration.
851 Even if the linker clams the symbol is unused, never bring internal
852 symbols that are declared by user as used or externally visible.
853 This is needed for i.e. references from asm statements. */
854 if (symtab_used_from_object_file_p ((symtab_node) vnode))
855 return true;
856 if (vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY)
857 return false;
859 /* As a special case, the COMDAT virtual tables can be unshared.
860 In LTO mode turn vtables into static variables. The variable is readonly,
861 so this does not enable more optimization, but referring static var
862 is faster for dynamic linking. Also this match logic hidding vtables
863 from LTO symbol tables. */
864 if ((in_lto_p || flag_whole_program)
865 && DECL_COMDAT (vnode->symbol.decl)
866 && comdat_can_be_unshared_p ((symtab_node) vnode))
867 return false;
869 /* When doing link time optimizations, hidden symbols become local. */
870 if (in_lto_p
871 && (DECL_VISIBILITY (vnode->symbol.decl) == VISIBILITY_HIDDEN
872 || DECL_VISIBILITY (vnode->symbol.decl) == VISIBILITY_INTERNAL)
873 /* Be sure that node is defined in IR file, not in other object
874 file. In that case we don't set used_from_other_object_file. */
875 && vnode->symbol.definition)
877 else if (!flag_whole_program)
878 return true;
880 /* Do not attempt to privatize COMDATS by default.
881 This would break linking with C++ libraries sharing
882 inline definitions.
884 FIXME: We can do so for readonly vars with no address taken and
885 possibly also for vtables since no direct pointer comparsion is done.
886 It might be interesting to do so to reduce linking overhead. */
887 if (DECL_COMDAT (vnode->symbol.decl) || DECL_WEAK (vnode->symbol.decl))
888 return true;
889 return false;
892 /* Return true if reference to NODE can be replaced by a local alias.
893 Local aliases save dynamic linking overhead and enable more optimizations.
896 bool
897 can_replace_by_local_alias (symtab_node node)
899 return (symtab_node_availability (node) > AVAIL_OVERWRITABLE
900 && !symtab_can_be_discarded (node));
903 /* Mark visibility of all functions.
905 A local function is one whose calls can occur only in the current
906 compilation unit and all its calls are explicit, so we can change
907 its calling convention. We simply mark all static functions whose
908 address is not taken as local.
910 We also change the TREE_PUBLIC flag of all declarations that are public
911 in language point of view but we want to overwrite this default
912 via visibilities for the backend point of view. */
914 static unsigned int
915 function_and_variable_visibility (bool whole_program)
917 struct cgraph_node *node;
918 struct varpool_node *vnode;
920 /* All aliases should be procssed at this point. */
921 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
923 FOR_EACH_FUNCTION (node)
925 int flags = flags_from_decl_or_type (node->symbol.decl);
927 /* Optimize away PURE and CONST constructors and destructors. */
928 if (optimize
929 && (flags & (ECF_CONST | ECF_PURE))
930 && !(flags & ECF_LOOPING_CONST_OR_PURE))
932 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
933 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
936 /* Frontends and alias code marks nodes as needed before parsing is finished.
937 We may end up marking as node external nodes where this flag is meaningless
938 strip it. */
939 if (DECL_EXTERNAL (node->symbol.decl) || !node->symbol.definition)
941 node->symbol.force_output = 0;
942 node->symbol.forced_by_abi = 0;
945 /* C++ FE on lack of COMDAT support create local COMDAT functions
946 (that ought to be shared but can not due to object format
947 limitations). It is necessary to keep the flag to make rest of C++ FE
948 happy. Clear the flag here to avoid confusion in middle-end. */
949 if (DECL_COMDAT (node->symbol.decl) && !TREE_PUBLIC (node->symbol.decl))
950 DECL_COMDAT (node->symbol.decl) = 0;
952 /* For external decls stop tracking same_comdat_group. It doesn't matter
953 what comdat group they are in when they won't be emitted in this TU. */
954 if (node->symbol.same_comdat_group && DECL_EXTERNAL (node->symbol.decl))
956 #ifdef ENABLE_CHECKING
957 symtab_node n;
959 for (n = node->symbol.same_comdat_group;
960 n != (symtab_node)node;
961 n = n->symbol.same_comdat_group)
962 /* If at least one of same comdat group functions is external,
963 all of them have to be, otherwise it is a front-end bug. */
964 gcc_assert (DECL_EXTERNAL (n->symbol.decl));
965 #endif
966 symtab_dissolve_same_comdat_group_list ((symtab_node) node);
968 gcc_assert ((!DECL_WEAK (node->symbol.decl)
969 && !DECL_COMDAT (node->symbol.decl))
970 || TREE_PUBLIC (node->symbol.decl)
971 || node->symbol.weakref
972 || DECL_EXTERNAL (node->symbol.decl));
973 if (cgraph_externally_visible_p (node, whole_program))
975 gcc_assert (!node->global.inlined_to);
976 node->symbol.externally_visible = true;
978 else
980 node->symbol.externally_visible = false;
981 node->symbol.forced_by_abi = false;
983 if (!node->symbol.externally_visible
984 && node->symbol.definition && !node->symbol.weakref
985 && !DECL_EXTERNAL (node->symbol.decl))
987 gcc_assert (whole_program || in_lto_p
988 || !TREE_PUBLIC (node->symbol.decl));
989 node->symbol.unique_name = ((node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
990 || node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
991 && TREE_PUBLIC (node->symbol.decl));
992 symtab_make_decl_local (node->symbol.decl);
993 node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
994 if (node->symbol.same_comdat_group)
995 /* cgraph_externally_visible_p has already checked all other nodes
996 in the group and they will all be made local. We need to
997 dissolve the group at once so that the predicate does not
998 segfault though. */
999 symtab_dissolve_same_comdat_group_list ((symtab_node) node);
1002 if (node->thunk.thunk_p
1003 && TREE_PUBLIC (node->symbol.decl))
1005 struct cgraph_node *decl_node = node;
1007 decl_node = cgraph_function_node (decl_node->callees->callee, NULL);
1009 /* Thunks have the same visibility as function they are attached to.
1010 Make sure the C++ front end set this up properly. */
1011 if (DECL_ONE_ONLY (decl_node->symbol.decl))
1013 gcc_checking_assert (DECL_COMDAT (node->symbol.decl)
1014 == DECL_COMDAT (decl_node->symbol.decl));
1015 gcc_checking_assert (DECL_COMDAT_GROUP (node->symbol.decl)
1016 == DECL_COMDAT_GROUP (decl_node->symbol.decl));
1017 gcc_checking_assert (node->symbol.same_comdat_group);
1019 if (DECL_EXTERNAL (decl_node->symbol.decl))
1020 DECL_EXTERNAL (node->symbol.decl) = 1;
1023 FOR_EACH_DEFINED_FUNCTION (node)
1025 node->local.local |= cgraph_local_node_p (node);
1027 /* If we know that function can not be overwritten by a different semantics
1028 and moreover its section can not be discarded, replace all direct calls
1029 by calls to an nonoverwritable alias. This make dynamic linking
1030 cheaper and enable more optimization.
1032 TODO: We can also update virtual tables. */
1033 if (node->callers && can_replace_by_local_alias ((symtab_node)node))
1035 struct cgraph_node *alias = cgraph (symtab_nonoverwritable_alias ((symtab_node) node));
1037 if (alias && alias != node)
1039 while (node->callers)
1041 struct cgraph_edge *e = node->callers;
1043 cgraph_redirect_edge_callee (e, alias);
1044 if (gimple_has_body_p (e->caller->symbol.decl))
1046 push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl));
1047 cgraph_redirect_edge_call_stmt_to_callee (e);
1048 pop_cfun ();
1054 FOR_EACH_VARIABLE (vnode)
1056 /* weak flag makes no sense on local variables. */
1057 gcc_assert (!DECL_WEAK (vnode->symbol.decl)
1058 || vnode->symbol.weakref
1059 || TREE_PUBLIC (vnode->symbol.decl)
1060 || DECL_EXTERNAL (vnode->symbol.decl));
1061 /* In several cases declarations can not be common:
1063 - when declaration has initializer
1064 - when it is in weak
1065 - when it has specific section
1066 - when it resides in non-generic address space.
1067 - if declaration is local, it will get into .local common section
1068 so common flag is not needed. Frontends still produce these in
1069 certain cases, such as for:
1071 static int a __attribute__ ((common))
1073 Canonicalize things here and clear the redundant flag. */
1074 if (DECL_COMMON (vnode->symbol.decl)
1075 && (!(TREE_PUBLIC (vnode->symbol.decl)
1076 || DECL_EXTERNAL (vnode->symbol.decl))
1077 || (DECL_INITIAL (vnode->symbol.decl)
1078 && DECL_INITIAL (vnode->symbol.decl) != error_mark_node)
1079 || DECL_WEAK (vnode->symbol.decl)
1080 || DECL_SECTION_NAME (vnode->symbol.decl) != NULL
1081 || ! (ADDR_SPACE_GENERIC_P
1082 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->symbol.decl))))))
1083 DECL_COMMON (vnode->symbol.decl) = 0;
1085 FOR_EACH_DEFINED_VARIABLE (vnode)
1087 if (!vnode->symbol.definition)
1088 continue;
1089 if (varpool_externally_visible_p (vnode))
1090 vnode->symbol.externally_visible = true;
1091 else
1093 vnode->symbol.externally_visible = false;
1094 vnode->symbol.forced_by_abi = false;
1096 if (!vnode->symbol.externally_visible
1097 && !vnode->symbol.weakref)
1099 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->symbol.decl));
1100 vnode->symbol.unique_name = ((vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
1101 || vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
1102 && TREE_PUBLIC (vnode->symbol.decl));
1103 symtab_make_decl_local (vnode->symbol.decl);
1104 if (vnode->symbol.same_comdat_group)
1105 symtab_dissolve_same_comdat_group_list ((symtab_node) vnode);
1106 vnode->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
1108 /* Static variables defined in auxiliary modules are externalized to
1109 allow cross module inlining. */
1110 gcc_assert (TREE_STATIC (vnode->symbol.decl)
1111 || varpool_is_auxiliary (vnode));
1114 if (dump_file)
1116 fprintf (dump_file, "\nMarking local functions:");
1117 FOR_EACH_DEFINED_FUNCTION (node)
1118 if (node->local.local)
1119 fprintf (dump_file, " %s", cgraph_node_name (node));
1120 fprintf (dump_file, "\n\n");
1121 fprintf (dump_file, "\nMarking externally visible functions:");
1122 FOR_EACH_DEFINED_FUNCTION (node)
1123 if (node->symbol.externally_visible)
1124 fprintf (dump_file, " %s", cgraph_node_name (node));
1125 fprintf (dump_file, "\n\n");
1126 fprintf (dump_file, "\nMarking externally visible variables:");
1127 FOR_EACH_DEFINED_VARIABLE (vnode)
1128 if (vnode->symbol.externally_visible)
1129 fprintf (dump_file, " %s", varpool_node_name (vnode));
1130 fprintf (dump_file, "\n\n");
1132 cgraph_function_flags_ready = true;
1133 return 0;
1136 /* Local function pass handling visibilities. This happens before LTO streaming
1137 so in particular -fwhole-program should be ignored at this level. */
1139 static unsigned int
1140 local_function_and_variable_visibility (void)
1142 return function_and_variable_visibility (flag_whole_program && !flag_lto);
1145 namespace {
1147 const pass_data pass_data_ipa_function_and_variable_visibility =
1149 SIMPLE_IPA_PASS, /* type */
1150 "visibility", /* name */
1151 OPTGROUP_NONE, /* optinfo_flags */
1152 false, /* has_gate */
1153 true, /* has_execute */
1154 TV_CGRAPHOPT, /* tv_id */
1155 0, /* properties_required */
1156 0, /* properties_provided */
1157 0, /* properties_destroyed */
1158 0, /* todo_flags_start */
1159 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1162 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
1164 public:
1165 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1166 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
1167 ctxt)
1170 /* opt_pass methods: */
1171 unsigned int execute () {
1172 return local_function_and_variable_visibility ();
1175 }; // class pass_ipa_function_and_variable_visibility
1177 } // anon namespace
1179 simple_ipa_opt_pass *
1180 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1182 return new pass_ipa_function_and_variable_visibility (ctxt);
1185 /* Free inline summary. */
1187 static unsigned
1188 free_inline_summary (void)
1190 inline_free_summary ();
1191 return 0;
1194 namespace {
1196 const pass_data pass_data_ipa_free_inline_summary =
1198 SIMPLE_IPA_PASS, /* type */
1199 "*free_inline_summary", /* name */
1200 OPTGROUP_NONE, /* optinfo_flags */
1201 false, /* has_gate */
1202 true, /* has_execute */
1203 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
1204 0, /* properties_required */
1205 0, /* properties_provided */
1206 0, /* properties_destroyed */
1207 0, /* todo_flags_start */
1208 0, /* todo_flags_finish */
1211 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
1213 public:
1214 pass_ipa_free_inline_summary (gcc::context *ctxt)
1215 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
1218 /* opt_pass methods: */
1219 unsigned int execute () { return free_inline_summary (); }
1221 }; // class pass_ipa_free_inline_summary
1223 } // anon namespace
1225 simple_ipa_opt_pass *
1226 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
1228 return new pass_ipa_free_inline_summary (ctxt);
1231 /* Do not re-run on ltrans stage. */
1233 static bool
1234 gate_whole_program_function_and_variable_visibility (void)
1236 return !flag_ltrans;
1239 /* Bring functionss local at LTO time with -fwhole-program. */
1241 static unsigned int
1242 whole_program_function_and_variable_visibility (void)
1244 function_and_variable_visibility (flag_whole_program);
1245 if (optimize)
1246 ipa_discover_readonly_nonaddressable_vars ();
1247 return 0;
1250 namespace {
1252 const pass_data pass_data_ipa_whole_program_visibility =
1254 IPA_PASS, /* type */
1255 "whole-program", /* name */
1256 OPTGROUP_NONE, /* optinfo_flags */
1257 true, /* has_gate */
1258 true, /* has_execute */
1259 TV_CGRAPHOPT, /* tv_id */
1260 0, /* properties_required */
1261 0, /* properties_provided */
1262 0, /* properties_destroyed */
1263 0, /* todo_flags_start */
1264 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1267 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
1269 public:
1270 pass_ipa_whole_program_visibility (gcc::context *ctxt)
1271 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
1272 NULL, /* generate_summary */
1273 NULL, /* write_summary */
1274 NULL, /* read_summary */
1275 NULL, /* write_optimization_summary */
1276 NULL, /* read_optimization_summary */
1277 NULL, /* stmt_fixup */
1278 0, /* function_transform_todo_flags_start */
1279 NULL, /* function_transform */
1280 NULL) /* variable_transform */
1283 /* opt_pass methods: */
1284 bool gate () {
1285 return gate_whole_program_function_and_variable_visibility ();
1287 unsigned int execute () {
1288 return whole_program_function_and_variable_visibility ();
1291 }; // class pass_ipa_whole_program_visibility
1293 } // anon namespace
1295 ipa_opt_pass_d *
1296 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
1298 return new pass_ipa_whole_program_visibility (ctxt);
1301 /* Generate and emit a static constructor or destructor. WHICH must
1302 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1303 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1304 initialization priority for this constructor or destructor.
1306 FINAL specify whether the externally visible name for collect2 should
1307 be produced. */
1309 static void
1310 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
1312 static int counter = 0;
1313 char which_buf[16];
1314 tree decl, name, resdecl;
1316 /* The priority is encoded in the constructor or destructor name.
1317 collect2 will sort the names and arrange that they are called at
1318 program startup. */
1319 if (final)
1320 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1321 else
1322 /* Proudce sane name but one not recognizable by collect2, just for the
1323 case we fail to inline the function. */
1324 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
1325 name = get_file_function_name (which_buf);
1327 decl = build_decl (input_location, FUNCTION_DECL, name,
1328 build_function_type_list (void_type_node, NULL_TREE));
1329 current_function_decl = decl;
1331 resdecl = build_decl (input_location,
1332 RESULT_DECL, NULL_TREE, void_type_node);
1333 DECL_ARTIFICIAL (resdecl) = 1;
1334 DECL_RESULT (decl) = resdecl;
1335 DECL_CONTEXT (resdecl) = decl;
1337 allocate_struct_function (decl, false);
1339 TREE_STATIC (decl) = 1;
1340 TREE_USED (decl) = 1;
1341 DECL_ARTIFICIAL (decl) = 1;
1342 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1343 DECL_SAVED_TREE (decl) = body;
1344 if (!targetm.have_ctors_dtors && final)
1346 TREE_PUBLIC (decl) = 1;
1347 DECL_PRESERVE_P (decl) = 1;
1349 DECL_UNINLINABLE (decl) = 1;
1351 DECL_INITIAL (decl) = make_node (BLOCK);
1352 TREE_USED (DECL_INITIAL (decl)) = 1;
1354 DECL_SOURCE_LOCATION (decl) = input_location;
1355 cfun->function_end_locus = input_location;
1357 switch (which)
1359 case 'I':
1360 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1361 decl_init_priority_insert (decl, priority);
1362 break;
1363 case 'D':
1364 DECL_STATIC_DESTRUCTOR (decl) = 1;
1365 decl_fini_priority_insert (decl, priority);
1366 break;
1367 default:
1368 gcc_unreachable ();
1371 gimplify_function_tree (decl);
1373 cgraph_add_new_function (decl, false);
1375 set_cfun (NULL);
1376 current_function_decl = NULL;
1379 /* Generate and emit a static constructor or destructor. WHICH must
1380 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1381 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1382 initialization priority for this constructor or destructor. */
1384 void
1385 cgraph_build_static_cdtor (char which, tree body, int priority)
1387 cgraph_build_static_cdtor_1 (which, body, priority, false);
1390 /* A vector of FUNCTION_DECLs declared as static constructors. */
1391 static vec<tree> static_ctors;
1392 /* A vector of FUNCTION_DECLs declared as static destructors. */
1393 static vec<tree> static_dtors;
1395 /* When target does not have ctors and dtors, we call all constructor
1396 and destructor by special initialization/destruction function
1397 recognized by collect2.
1399 When we are going to build this function, collect all constructors and
1400 destructors and turn them into normal functions. */
1402 static void
1403 record_cdtor_fn (struct cgraph_node *node)
1405 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1406 static_ctors.safe_push (node->symbol.decl);
1407 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1408 static_dtors.safe_push (node->symbol.decl);
1409 node = cgraph_get_node (node->symbol.decl);
1410 DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl) = 1;
1413 /* Define global constructors/destructor functions for the CDTORS, of
1414 which they are LEN. The CDTORS are sorted by initialization
1415 priority. If CTOR_P is true, these are constructors; otherwise,
1416 they are destructors. */
1418 static void
1419 build_cdtor (bool ctor_p, vec<tree> cdtors)
1421 size_t i,j;
1422 size_t len = cdtors.length ();
1424 i = 0;
1425 while (i < len)
1427 tree body;
1428 tree fn;
1429 priority_type priority;
1431 priority = 0;
1432 body = NULL_TREE;
1433 j = i;
1436 priority_type p;
1437 fn = cdtors[j];
1438 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1439 if (j == i)
1440 priority = p;
1441 else if (p != priority)
1442 break;
1443 j++;
1445 while (j < len);
1447 /* When there is only one cdtor and target supports them, do nothing. */
1448 if (j == i + 1
1449 && targetm.have_ctors_dtors)
1451 i++;
1452 continue;
1454 /* Find the next batch of constructors/destructors with the same
1455 initialization priority. */
1456 for (;i < j; i++)
1458 tree call;
1459 fn = cdtors[i];
1460 call = build_call_expr (fn, 0);
1461 if (ctor_p)
1462 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1463 else
1464 DECL_STATIC_DESTRUCTOR (fn) = 0;
1465 /* We do not want to optimize away pure/const calls here.
1466 When optimizing, these should be already removed, when not
1467 optimizing, we want user to be able to breakpoint in them. */
1468 TREE_SIDE_EFFECTS (call) = 1;
1469 append_to_statement_list (call, &body);
1471 gcc_assert (body != NULL_TREE);
1472 /* Generate a function to call all the function of like
1473 priority. */
1474 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1478 /* Comparison function for qsort. P1 and P2 are actually of type
1479 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1480 used to determine the sort order. */
1482 static int
1483 compare_ctor (const void *p1, const void *p2)
1485 tree f1;
1486 tree f2;
1487 int priority1;
1488 int priority2;
1490 f1 = *(const tree *)p1;
1491 f2 = *(const tree *)p2;
1492 priority1 = DECL_INIT_PRIORITY (f1);
1493 priority2 = DECL_INIT_PRIORITY (f2);
1495 if (priority1 < priority2)
1496 return -1;
1497 else if (priority1 > priority2)
1498 return 1;
1499 else
1500 /* Ensure a stable sort. Constructors are executed in backwarding
1501 order to make LTO initialize braries first. */
1502 return DECL_UID (f2) - DECL_UID (f1);
1505 /* Comparison function for qsort. P1 and P2 are actually of type
1506 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1507 used to determine the sort order. */
1509 static int
1510 compare_dtor (const void *p1, const void *p2)
1512 tree f1;
1513 tree f2;
1514 int priority1;
1515 int priority2;
1517 f1 = *(const tree *)p1;
1518 f2 = *(const tree *)p2;
1519 priority1 = DECL_FINI_PRIORITY (f1);
1520 priority2 = DECL_FINI_PRIORITY (f2);
1522 if (priority1 < priority2)
1523 return -1;
1524 else if (priority1 > priority2)
1525 return 1;
1526 else
1527 /* Ensure a stable sort. */
1528 return DECL_UID (f1) - DECL_UID (f2);
1531 /* Generate functions to call static constructors and destructors
1532 for targets that do not support .ctors/.dtors sections. These
1533 functions have magic names which are detected by collect2. */
1535 static void
1536 build_cdtor_fns (void)
1538 if (!static_ctors.is_empty ())
1540 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1541 static_ctors.qsort (compare_ctor);
1542 build_cdtor (/*ctor_p=*/true, static_ctors);
1545 if (!static_dtors.is_empty ())
1547 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1548 static_dtors.qsort (compare_dtor);
1549 build_cdtor (/*ctor_p=*/false, static_dtors);
1553 /* Look for constructors and destructors and produce function calling them.
1554 This is needed for targets not supporting ctors or dtors, but we perform the
1555 transformation also at linktime to merge possibly numerous
1556 constructors/destructors into single function to improve code locality and
1557 reduce size. */
1559 static unsigned int
1560 ipa_cdtor_merge (void)
1562 struct cgraph_node *node;
1563 FOR_EACH_DEFINED_FUNCTION (node)
1564 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
1565 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1566 record_cdtor_fn (node);
1567 build_cdtor_fns ();
1568 static_ctors.release ();
1569 static_dtors.release ();
1570 return 0;
1573 /* Perform the pass when we have no ctors/dtors support
1574 or at LTO time to merge multiple constructors into single
1575 function. */
1577 static bool
1578 gate_ipa_cdtor_merge (void)
1580 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1583 namespace {
1585 const pass_data pass_data_ipa_cdtor_merge =
1587 IPA_PASS, /* type */
1588 "cdtor", /* name */
1589 OPTGROUP_NONE, /* optinfo_flags */
1590 true, /* has_gate */
1591 true, /* has_execute */
1592 TV_CGRAPHOPT, /* tv_id */
1593 0, /* properties_required */
1594 0, /* properties_provided */
1595 0, /* properties_destroyed */
1596 0, /* todo_flags_start */
1597 0, /* todo_flags_finish */
1600 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1602 public:
1603 pass_ipa_cdtor_merge (gcc::context *ctxt)
1604 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1605 NULL, /* generate_summary */
1606 NULL, /* write_summary */
1607 NULL, /* read_summary */
1608 NULL, /* write_optimization_summary */
1609 NULL, /* read_optimization_summary */
1610 NULL, /* stmt_fixup */
1611 0, /* function_transform_todo_flags_start */
1612 NULL, /* function_transform */
1613 NULL) /* variable_transform */
1616 /* opt_pass methods: */
1617 bool gate () { return gate_ipa_cdtor_merge (); }
1618 unsigned int execute () { return ipa_cdtor_merge (); }
1620 }; // class pass_ipa_cdtor_merge
1622 } // anon namespace
1624 ipa_opt_pass_d *
1625 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1627 return new pass_ipa_cdtor_merge (ctxt);