Released GUPC 4.9.0.1 based on version 203902.
[official-gcc.git] / gcc / ipa.c
blob80a97ba7df62bd596cc60375258b18fd34e63db9
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "cgraph.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 "ipa-utils.h"
34 #include "ipa-inline.h"
35 #include "tree-inline.h"
36 #include "profile.h"
37 #include "params.h"
39 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
41 static bool
42 cgraph_non_local_node_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
44 /* FIXME: Aliases can be local, but i386 gets thunks wrong then. */
45 return !(cgraph_only_called_directly_or_aliased_p (node)
46 && !ipa_ref_has_aliases_p (&node->symbol.ref_list)
47 && node->symbol.definition
48 && !DECL_EXTERNAL (node->symbol.decl)
49 && !node->symbol.externally_visible
50 && !node->symbol.used_from_other_partition
51 && !node->symbol.in_other_partition);
54 /* Return true when function can be marked local. */
56 static bool
57 cgraph_local_node_p (struct cgraph_node *node)
59 struct cgraph_node *n = cgraph_function_or_thunk_node (node, NULL);
61 /* FIXME: thunks can be considered local, but we need prevent i386
62 from attempting to change calling convention of them. */
63 if (n->thunk.thunk_p)
64 return false;
65 return !cgraph_for_node_and_aliases (n,
66 cgraph_non_local_node_p_1, NULL, true);
70 /* Return true when NODE has ADDR reference. */
72 static bool
73 has_addr_references_p (struct cgraph_node *node,
74 void *data ATTRIBUTE_UNUSED)
76 int i;
77 struct ipa_ref *ref;
79 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list,
80 i, ref); i++)
81 if (ref->use == IPA_REF_ADDR)
82 return true;
83 return false;
86 /* Look for all functions inlined to NODE and update their inlined_to pointers
87 to INLINED_TO. */
89 static void
90 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
92 struct cgraph_edge *e;
93 for (e = node->callees; e; e = e->next_callee)
94 if (e->callee->global.inlined_to)
96 e->callee->global.inlined_to = inlined_to;
97 update_inlined_to_pointer (e->callee, inlined_to);
101 /* Add symtab NODE to queue starting at FIRST.
103 The queue is linked via AUX pointers and terminated by pointer to 1.
104 We enqueue nodes at two occasions: when we find them reachable or when we find
105 their bodies needed for further clonning. In the second case we mark them
106 by pointer to 2 after processing so they are re-queue when they become
107 reachable. */
109 static void
110 enqueue_node (symtab_node node, symtab_node *first,
111 struct pointer_set_t *reachable)
113 /* Node is still in queue; do nothing. */
114 if (node->symbol.aux && node->symbol.aux != (void *) 2)
115 return;
116 /* Node was already processed as unreachable, re-enqueue
117 only if it became reachable now. */
118 if (node->symbol.aux == (void *)2 && !pointer_set_contains (reachable, node))
119 return;
120 node->symbol.aux = *first;
121 *first = node;
124 /* Process references. */
126 static void
127 process_references (struct ipa_ref_list *list,
128 symtab_node *first,
129 bool before_inlining_p,
130 struct pointer_set_t *reachable)
132 int i;
133 struct ipa_ref *ref;
134 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
136 symtab_node node = ref->referred;
138 if (node->symbol.definition && !node->symbol.in_other_partition
139 && ((!DECL_EXTERNAL (node->symbol.decl) || node->symbol.alias)
140 || (before_inlining_p
141 /* We use variable constructors during late complation for
142 constant folding. Keep references alive so partitioning
143 knows about potential references. */
144 || (TREE_CODE (node->symbol.decl) == VAR_DECL
145 && flag_wpa
146 && ctor_for_folding (node->symbol.decl)
147 != error_mark_node))))
148 pointer_set_insert (reachable, node);
149 enqueue_node ((symtab_node) node, first, reachable);
153 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
154 all its potential targets as reachable to permit later inlining if
155 devirtualization happens. After inlining still keep their declarations
156 around, so we can devirtualize to a direct call.
158 Also try to make trivial devirutalization when no or only one target is
159 possible. */
161 static void
162 walk_polymorphic_call_targets (pointer_set_t *reachable_call_targets,
163 struct cgraph_edge *edge,
164 symtab_node *first,
165 pointer_set_t *reachable, bool before_inlining_p)
167 unsigned int i;
168 void *cache_token;
169 bool final;
170 vec <cgraph_node *>targets
171 = possible_polymorphic_call_targets
172 (edge, &final, &cache_token);
174 if (!pointer_set_insert (reachable_call_targets,
175 cache_token))
177 for (i = 0; i < targets.length (); i++)
179 struct cgraph_node *n = targets[i];
181 /* Do not bother to mark virtual methods in anonymous namespace;
182 either we will find use of virtual table defining it, or it is
183 unused. */
184 if (TREE_CODE (TREE_TYPE (n->symbol.decl)) == METHOD_TYPE
185 && type_in_anonymous_namespace_p
186 (method_class_type (TREE_TYPE (n->symbol.decl))))
187 continue;
189 /* Prior inlining, keep alive bodies of possible targets for
190 devirtualization. */
191 if (n->symbol.definition
192 && before_inlining_p)
193 pointer_set_insert (reachable, n);
195 /* Even after inlining we want to keep the possible targets in the
196 boundary, so late passes can still produce direct call even if
197 the chance for inlining is lost. */
198 enqueue_node ((symtab_node) n, first, reachable);
202 /* Very trivial devirtualization; when the type is
203 final or anonymous (so we know all its derivation)
204 and there is only one possible virtual call target,
205 make the edge direct. */
206 if (final)
208 if (targets.length () <= 1)
210 cgraph_node *target, *node = edge->caller;
211 if (targets.length () == 1)
212 target = targets[0];
213 else
214 target = cgraph_get_create_node
215 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
217 if (dump_file)
218 fprintf (dump_file,
219 "Devirtualizing call in %s/%i to %s/%i\n",
220 cgraph_node_name (edge->caller),
221 edge->caller->symbol.order,
222 cgraph_node_name (target), target->symbol.order);
223 edge = cgraph_make_edge_direct (edge, target);
224 if (!inline_summary_vec && edge->call_stmt)
225 cgraph_redirect_edge_call_stmt_to_callee (edge);
226 else
227 inline_update_overall_summary (node);
232 /* Perform reachability analysis and reclaim all unreachable nodes.
234 The algorithm is basically mark&sweep but with some extra refinements:
236 - reachable extern inline functions needs special handling; the bodies needs
237 to stay in memory until inlining in hope that they will be inlined.
238 After inlining we release their bodies and turn them into unanalyzed
239 nodes even when they are reachable.
241 BEFORE_INLINING_P specify whether we are before or after inlining.
243 - virtual functions are kept in callgraph even if they seem unreachable in
244 hope calls to them will be devirtualized.
246 Again we remove them after inlining. In late optimization some
247 devirtualization may happen, but it is not importnat since we won't inline
248 the call. In theory early opts and IPA should work out all important cases.
250 - virtual clones needs bodies of their origins for later materialization;
251 this means that we want to keep the body even if the origin is unreachable
252 otherwise. To avoid origin from sitting in the callgraph and being
253 walked by IPA passes, we turn them into unanalyzed nodes with body
254 defined.
256 We maintain set of function declaration where body needs to stay in
257 body_needed_for_clonning
259 Inline clones represent special case: their declaration match the
260 declaration of origin and cgraph_remove_node already knows how to
261 reshape callgraph and preserve body when offline copy of function or
262 inline clone is being removed.
264 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
265 variables with DECL_INITIAL set. We finalize these and keep reachable
266 ones around for constant folding purposes. After inlining we however
267 stop walking their references to let everything static referneced by them
268 to be removed when it is otherwise unreachable.
270 We maintain queue of both reachable symbols (i.e. defined symbols that needs
271 to stay) and symbols that are in boundary (i.e. external symbols referenced
272 by reachable symbols or origins of clones). The queue is represented
273 as linked list by AUX pointer terminated by 1.
275 A the end we keep all reachable symbols. For symbols in boundary we always
276 turn definition into a declaration, but we may keep function body around
277 based on body_needed_for_clonning
279 All symbols that enter the queue have AUX pointer non-zero and are in the
280 boundary. Pointer set REACHABLE is used to track reachable symbols.
282 Every symbol can be visited twice - once as part of boundary and once
283 as real reachable symbol. enqueue_node needs to decide whether the
284 node needs to be re-queued for second processing. For this purpose
285 we set AUX pointer of processed symbols in the boundary to constant 2. */
287 bool
288 symtab_remove_unreachable_nodes (bool before_inlining_p, FILE *file)
290 symtab_node first = (symtab_node) (void *) 1;
291 struct cgraph_node *node, *next;
292 struct varpool_node *vnode, *vnext;
293 bool changed = false;
294 struct pointer_set_t *reachable = pointer_set_create ();
295 struct pointer_set_t *body_needed_for_clonning = pointer_set_create ();
296 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
298 timevar_push (TV_IPA_UNREACHABLE);
299 #ifdef ENABLE_CHECKING
300 verify_symtab ();
301 #endif
302 if (optimize && flag_devirtualize)
303 build_type_inheritance_graph ();
304 if (file)
305 fprintf (file, "\nReclaiming functions:");
306 #ifdef ENABLE_CHECKING
307 FOR_EACH_FUNCTION (node)
308 gcc_assert (!node->symbol.aux);
309 FOR_EACH_VARIABLE (vnode)
310 gcc_assert (!vnode->symbol.aux);
311 #endif
312 /* Mark functions whose bodies are obviously needed.
313 This is mostly when they can be referenced externally. Inline clones
314 are special since their declarations are shared with master clone and thus
315 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
316 FOR_EACH_FUNCTION (node)
318 node->used_as_abstract_origin = false;
319 if (node->symbol.definition
320 && !node->global.inlined_to
321 && !node->symbol.in_other_partition
322 && !cgraph_can_remove_if_no_direct_calls_and_refs_p (node))
324 gcc_assert (!node->global.inlined_to);
325 pointer_set_insert (reachable, node);
326 enqueue_node ((symtab_node)node, &first, reachable);
328 else
329 gcc_assert (!node->symbol.aux);
332 /* Mark variables that are obviously needed. */
333 FOR_EACH_DEFINED_VARIABLE (vnode)
334 if (!varpool_can_remove_if_no_refs (vnode)
335 && !vnode->symbol.in_other_partition)
337 pointer_set_insert (reachable, vnode);
338 enqueue_node ((symtab_node)vnode, &first, reachable);
341 /* Perform reachability analysis. */
342 while (first != (symtab_node) (void *) 1)
344 bool in_boundary_p = !pointer_set_contains (reachable, first);
345 symtab_node node = first;
347 first = (symtab_node)first->symbol.aux;
349 /* If we are processing symbol in boundary, mark its AUX pointer for
350 possible later re-processing in enqueue_node. */
351 if (in_boundary_p)
352 node->symbol.aux = (void *)2;
353 else
355 if (DECL_ABSTRACT_ORIGIN (node->symbol.decl))
357 struct cgraph_node *origin_node
358 = cgraph_get_create_real_symbol_node (DECL_ABSTRACT_ORIGIN (node->symbol.decl));
359 origin_node->used_as_abstract_origin = true;
360 enqueue_node ((symtab_node) origin_node, &first, reachable);
362 /* If any symbol in a comdat group is reachable, force
363 all other in the same comdat group to be also reachable. */
364 if (node->symbol.same_comdat_group)
366 symtab_node next;
367 for (next = node->symbol.same_comdat_group;
368 next != node;
369 next = next->symbol.same_comdat_group)
370 if (!pointer_set_insert (reachable, next))
371 enqueue_node ((symtab_node) next, &first, reachable);
373 /* Mark references as reachable. */
374 process_references (&node->symbol.ref_list, &first,
375 before_inlining_p, reachable);
378 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
380 /* Mark the callees reachable unless they are direct calls to extern
381 inline functions we decided to not inline. */
382 if (!in_boundary_p)
384 struct cgraph_edge *e;
385 /* Keep alive possible targets for devirtualization. */
386 if (optimize && flag_devirtualize)
388 struct cgraph_edge *next;
389 for (e = cnode->indirect_calls; e; e = next)
391 next = e->next_callee;
392 if (e->indirect_info->polymorphic)
393 walk_polymorphic_call_targets (reachable_call_targets,
394 e, &first, reachable,
395 before_inlining_p);
398 for (e = cnode->callees; e; e = e->next_callee)
400 if (e->callee->symbol.definition
401 && !e->callee->symbol.in_other_partition
402 && (!e->inline_failed
403 || !DECL_EXTERNAL (e->callee->symbol.decl)
404 || e->callee->symbol.alias
405 || before_inlining_p))
406 pointer_set_insert (reachable, e->callee);
407 enqueue_node ((symtab_node) e->callee, &first, reachable);
410 /* When inline clone exists, mark body to be preserved so when removing
411 offline copy of the function we don't kill it. */
412 if (cnode->global.inlined_to)
413 pointer_set_insert (body_needed_for_clonning, cnode->symbol.decl);
415 /* For non-inline clones, force their origins to the boundary and ensure
416 that body is not removed. */
417 while (cnode->clone_of)
419 bool noninline = cnode->clone_of->symbol.decl != cnode->symbol.decl;
420 cnode = cnode->clone_of;
421 if (noninline)
423 pointer_set_insert (body_needed_for_clonning, cnode->symbol.decl);
424 enqueue_node ((symtab_node)cnode, &first, reachable);
429 /* When we see constructor of external variable, keep referred nodes in the
430 boundary. This will also hold initializers of the external vars NODE
431 refers to. */
432 varpool_node *vnode = dyn_cast <varpool_node> (node);
433 if (vnode
434 && DECL_EXTERNAL (node->symbol.decl)
435 && !vnode->symbol.alias
436 && in_boundary_p)
438 struct ipa_ref *ref;
439 for (int i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
440 enqueue_node (ref->referred, &first, reachable);
444 /* Remove unreachable functions. */
445 for (node = cgraph_first_function (); node; node = next)
447 next = cgraph_next_function (node);
449 /* If node is not needed at all, remove it. */
450 if (!node->symbol.aux)
452 if (file)
453 fprintf (file, " %s", cgraph_node_name (node));
454 cgraph_remove_node (node);
455 changed = true;
457 /* If node is unreachable, remove its body. */
458 else if (!pointer_set_contains (reachable, node))
460 if (!pointer_set_contains (body_needed_for_clonning, node->symbol.decl))
461 cgraph_release_function_body (node);
462 else if (!node->clone_of)
463 gcc_assert (in_lto_p || DECL_RESULT (node->symbol.decl));
464 if (node->symbol.definition)
466 if (file)
467 fprintf (file, " %s", cgraph_node_name (node));
468 node->symbol.analyzed = false;
469 node->symbol.definition = false;
470 node->symbol.cpp_implicit_alias = false;
471 node->symbol.alias = false;
472 node->symbol.weakref = false;
473 if (!node->symbol.in_other_partition)
474 node->local.local = false;
475 cgraph_node_remove_callees (node);
476 ipa_remove_all_references (&node->symbol.ref_list);
477 changed = true;
480 else
481 gcc_assert (node->clone_of || !cgraph_function_with_gimple_body_p (node)
482 || in_lto_p || DECL_RESULT (node->symbol.decl));
485 /* Inline clones might be kept around so their materializing allows further
486 cloning. If the function the clone is inlined into is removed, we need
487 to turn it into normal cone. */
488 FOR_EACH_FUNCTION (node)
490 if (node->global.inlined_to
491 && !node->callers)
493 gcc_assert (node->clones);
494 node->global.inlined_to = NULL;
495 update_inlined_to_pointer (node, node);
497 node->symbol.aux = NULL;
500 /* Remove unreachable variables. */
501 if (file)
502 fprintf (file, "\nReclaiming variables:");
503 for (vnode = varpool_first_variable (); vnode; vnode = vnext)
505 vnext = varpool_next_variable (vnode);
506 if (!vnode->symbol.aux
507 /* For can_refer_decl_in_current_unit_p we want to track for
508 all external variables if they are defined in other partition
509 or not. */
510 && (!flag_ltrans || !DECL_EXTERNAL (vnode->symbol.decl)))
512 if (file)
513 fprintf (file, " %s", varpool_node_name (vnode));
514 varpool_remove_node (vnode);
515 changed = true;
517 else if (!pointer_set_contains (reachable, vnode))
519 tree init;
520 if (vnode->symbol.definition)
522 if (file)
523 fprintf (file, " %s", varpool_node_name (vnode));
524 changed = true;
526 vnode->symbol.definition = false;
527 vnode->symbol.analyzed = false;
528 vnode->symbol.aux = NULL;
530 /* Keep body if it may be useful for constant folding. */
531 if ((init = ctor_for_folding (vnode->symbol.decl)) == error_mark_node)
532 varpool_remove_initializer (vnode);
533 else
534 DECL_INITIAL (vnode->symbol.decl) = init;
535 ipa_remove_all_references (&vnode->symbol.ref_list);
537 else
538 vnode->symbol.aux = NULL;
541 pointer_set_destroy (reachable);
542 pointer_set_destroy (body_needed_for_clonning);
543 pointer_set_destroy (reachable_call_targets);
545 /* Now update address_taken flags and try to promote functions to be local. */
546 if (file)
547 fprintf (file, "\nClearing address taken flags:");
548 FOR_EACH_DEFINED_FUNCTION (node)
549 if (node->symbol.address_taken
550 && !node->symbol.used_from_other_partition)
552 if (!cgraph_for_node_and_aliases (node, has_addr_references_p, NULL, true))
554 if (file)
555 fprintf (file, " %s", cgraph_node_name (node));
556 node->symbol.address_taken = false;
557 changed = true;
558 if (cgraph_local_node_p (node))
560 node->local.local = true;
561 if (file)
562 fprintf (file, " (local)");
566 if (file)
567 fprintf (file, "\n");
569 #ifdef ENABLE_CHECKING
570 verify_symtab ();
571 #endif
573 /* If we removed something, perhaps profile could be improved. */
574 if (changed && optimize && inline_edge_summary_vec.exists ())
575 FOR_EACH_DEFINED_FUNCTION (node)
576 ipa_propagate_frequency (node);
578 timevar_pop (TV_IPA_UNREACHABLE);
579 return changed;
582 /* Discover variables that have no longer address taken or that are read only
583 and update their flags.
585 FIXME: This can not be done in between gimplify and omp_expand since
586 readonly flag plays role on what is shared and what is not. Currently we do
587 this transformation as part of whole program visibility and re-do at
588 ipa-reference pass (to take into account clonning), but it would
589 make sense to do it before early optimizations. */
591 void
592 ipa_discover_readonly_nonaddressable_vars (void)
594 struct varpool_node *vnode;
595 if (dump_file)
596 fprintf (dump_file, "Clearing variable flags:");
597 FOR_EACH_VARIABLE (vnode)
598 if (vnode->symbol.definition && varpool_all_refs_explicit_p (vnode)
599 && (TREE_ADDRESSABLE (vnode->symbol.decl)
600 || !TREE_READONLY (vnode->symbol.decl)))
602 bool written = false;
603 bool address_taken = false;
604 int i;
605 struct ipa_ref *ref;
606 for (i = 0; ipa_ref_list_referring_iterate (&vnode->symbol.ref_list,
607 i, ref)
608 && (!written || !address_taken); i++)
609 switch (ref->use)
611 case IPA_REF_ADDR:
612 address_taken = true;
613 break;
614 case IPA_REF_LOAD:
615 break;
616 case IPA_REF_STORE:
617 written = true;
618 break;
620 if (TREE_ADDRESSABLE (vnode->symbol.decl) && !address_taken)
622 if (dump_file)
623 fprintf (dump_file, " %s (addressable)", varpool_node_name (vnode));
624 TREE_ADDRESSABLE (vnode->symbol.decl) = 0;
626 if (!TREE_READONLY (vnode->symbol.decl) && !address_taken && !written
627 /* Making variable in explicit section readonly can cause section
628 type conflict.
629 See e.g. gcc.c-torture/compile/pr23237.c */
630 && DECL_SECTION_NAME (vnode->symbol.decl) == NULL)
632 if (dump_file)
633 fprintf (dump_file, " %s (read-only)", varpool_node_name (vnode));
634 TREE_READONLY (vnode->symbol.decl) = 1;
637 if (dump_file)
638 fprintf (dump_file, "\n");
641 /* Return true when there is a reference to node and it is not vtable. */
642 static bool
643 address_taken_from_non_vtable_p (symtab_node node)
645 int i;
646 struct ipa_ref *ref;
647 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list,
648 i, ref); i++)
649 if (ref->use == IPA_REF_ADDR)
651 struct varpool_node *node;
652 if (is_a <cgraph_node> (ref->referring))
653 return true;
654 node = ipa_ref_referring_varpool_node (ref);
655 if (!DECL_VIRTUAL_P (node->symbol.decl))
656 return true;
658 return false;
661 /* A helper for comdat_can_be_unshared_p. */
663 static bool
664 comdat_can_be_unshared_p_1 (symtab_node node)
666 /* When address is taken, we don't know if equality comparison won't
667 break eventually. Exception are virutal functions, C++
668 constructors/destructors and vtables, where this is not possible by
669 language standard. */
670 if (!DECL_VIRTUAL_P (node->symbol.decl)
671 && (TREE_CODE (node->symbol.decl) != FUNCTION_DECL
672 || (!DECL_CXX_CONSTRUCTOR_P (node->symbol.decl)
673 && !DECL_CXX_DESTRUCTOR_P (node->symbol.decl)))
674 && address_taken_from_non_vtable_p (node))
675 return false;
677 /* If the symbol is used in some weird way, better to not touch it. */
678 if (node->symbol.force_output)
679 return false;
681 /* Explicit instantiations needs to be output when possibly
682 used externally. */
683 if (node->symbol.forced_by_abi
684 && TREE_PUBLIC (node->symbol.decl)
685 && (node->symbol.resolution != LDPR_PREVAILING_DEF_IRONLY
686 && !flag_whole_program))
687 return false;
689 /* Non-readonly and volatile variables can not be duplicated. */
690 if (is_a <varpool_node> (node)
691 && (!TREE_READONLY (node->symbol.decl)
692 || TREE_THIS_VOLATILE (node->symbol.decl)))
693 return false;
694 return true;
697 /* COMDAT functions must be shared only if they have address taken,
698 otherwise we can produce our own private implementation with
699 -fwhole-program.
700 Return true when turning COMDAT functoin static can not lead to wrong
701 code when the resulting object links with a library defining same COMDAT.
703 Virtual functions do have their addresses taken from the vtables,
704 but in C++ there is no way to compare their addresses for equality. */
706 static bool
707 comdat_can_be_unshared_p (symtab_node node)
709 if (!comdat_can_be_unshared_p_1 (node))
710 return false;
711 if (node->symbol.same_comdat_group)
713 symtab_node next;
715 /* If more than one function is in the same COMDAT group, it must
716 be shared even if just one function in the comdat group has
717 address taken. */
718 for (next = node->symbol.same_comdat_group;
719 next != node; next = next->symbol.same_comdat_group)
720 if (!comdat_can_be_unshared_p_1 (next))
721 return false;
723 return true;
726 /* Return true when function NODE should be considered externally visible. */
728 static bool
729 cgraph_externally_visible_p (struct cgraph_node *node,
730 bool whole_program)
732 if (!node->symbol.definition)
733 return false;
734 if (!TREE_PUBLIC (node->symbol.decl)
735 || DECL_EXTERNAL (node->symbol.decl))
736 return false;
738 /* Do not try to localize built-in functions yet. One of problems is that we
739 end up mangling their asm for WHOPR that makes it impossible to call them
740 using the implicit built-in declarations anymore. Similarly this enables
741 us to remove them as unreachable before actual calls may appear during
742 expansion or folding. */
743 if (DECL_BUILT_IN (node->symbol.decl))
744 return true;
746 /* If linker counts on us, we must preserve the function. */
747 if (symtab_used_from_object_file_p ((symtab_node) node))
748 return true;
749 if (DECL_PRESERVE_P (node->symbol.decl))
750 return true;
751 if (lookup_attribute ("externally_visible",
752 DECL_ATTRIBUTES (node->symbol.decl)))
753 return true;
754 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
755 && lookup_attribute ("dllexport",
756 DECL_ATTRIBUTES (node->symbol.decl)))
757 return true;
758 if (node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY)
759 return false;
760 /* When doing LTO or whole program, we can bring COMDAT functoins static.
761 This improves code quality and we know we will duplicate them at most twice
762 (in the case that we are not using plugin and link with object file
763 implementing same COMDAT) */
764 if ((in_lto_p || whole_program)
765 && DECL_COMDAT (node->symbol.decl)
766 && comdat_can_be_unshared_p ((symtab_node) node))
767 return false;
769 /* When doing link time optimizations, hidden symbols become local. */
770 if (in_lto_p
771 && (DECL_VISIBILITY (node->symbol.decl) == VISIBILITY_HIDDEN
772 || DECL_VISIBILITY (node->symbol.decl) == VISIBILITY_INTERNAL)
773 /* Be sure that node is defined in IR file, not in other object
774 file. In that case we don't set used_from_other_object_file. */
775 && node->symbol.definition)
777 else if (!whole_program)
778 return true;
780 if (MAIN_NAME_P (DECL_NAME (node->symbol.decl)))
781 return true;
783 return false;
786 /* Return true when variable VNODE should be considered externally visible. */
788 bool
789 varpool_externally_visible_p (struct varpool_node *vnode)
791 if (DECL_EXTERNAL (vnode->symbol.decl))
792 return true;
794 if (!TREE_PUBLIC (vnode->symbol.decl))
795 return false;
797 /* If linker counts on us, we must preserve the function. */
798 if (symtab_used_from_object_file_p ((symtab_node) vnode))
799 return true;
801 if (DECL_HARD_REGISTER (vnode->symbol.decl))
802 return true;
803 if (DECL_PRESERVE_P (vnode->symbol.decl))
804 return true;
805 if (lookup_attribute ("externally_visible",
806 DECL_ATTRIBUTES (vnode->symbol.decl)))
807 return true;
808 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
809 && lookup_attribute ("dllexport",
810 DECL_ATTRIBUTES (vnode->symbol.decl)))
811 return true;
813 /* See if we have linker information about symbol not being used or
814 if we need to make guess based on the declaration.
816 Even if the linker clams the symbol is unused, never bring internal
817 symbols that are declared by user as used or externally visible.
818 This is needed for i.e. references from asm statements. */
819 if (symtab_used_from_object_file_p ((symtab_node) vnode))
820 return true;
821 if (vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY)
822 return false;
824 /* As a special case, the COMDAT virtual tables can be unshared.
825 In LTO mode turn vtables into static variables. The variable is readonly,
826 so this does not enable more optimization, but referring static var
827 is faster for dynamic linking. Also this match logic hidding vtables
828 from LTO symbol tables. */
829 if ((in_lto_p || flag_whole_program)
830 && DECL_COMDAT (vnode->symbol.decl)
831 && comdat_can_be_unshared_p ((symtab_node) vnode))
832 return false;
834 /* When doing link time optimizations, hidden symbols become local. */
835 if (in_lto_p
836 && (DECL_VISIBILITY (vnode->symbol.decl) == VISIBILITY_HIDDEN
837 || DECL_VISIBILITY (vnode->symbol.decl) == VISIBILITY_INTERNAL)
838 /* Be sure that node is defined in IR file, not in other object
839 file. In that case we don't set used_from_other_object_file. */
840 && vnode->symbol.definition)
842 else if (!flag_whole_program)
843 return true;
845 /* Do not attempt to privatize COMDATS by default.
846 This would break linking with C++ libraries sharing
847 inline definitions.
849 FIXME: We can do so for readonly vars with no address taken and
850 possibly also for vtables since no direct pointer comparsion is done.
851 It might be interesting to do so to reduce linking overhead. */
852 if (DECL_COMDAT (vnode->symbol.decl) || DECL_WEAK (vnode->symbol.decl))
853 return true;
854 return false;
857 /* Return true if reference to NODE can be replaced by a local alias.
858 Local aliases save dynamic linking overhead and enable more optimizations.
861 bool
862 can_replace_by_local_alias (symtab_node node)
864 return (symtab_node_availability (node) > AVAIL_OVERWRITABLE
865 && !symtab_can_be_discarded (node));
868 /* Mark visibility of all functions.
870 A local function is one whose calls can occur only in the current
871 compilation unit and all its calls are explicit, so we can change
872 its calling convention. We simply mark all static functions whose
873 address is not taken as local.
875 We also change the TREE_PUBLIC flag of all declarations that are public
876 in language point of view but we want to overwrite this default
877 via visibilities for the backend point of view. */
879 static unsigned int
880 function_and_variable_visibility (bool whole_program)
882 struct cgraph_node *node;
883 struct varpool_node *vnode;
885 /* All aliases should be procssed at this point. */
886 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
888 FOR_EACH_FUNCTION (node)
890 int flags = flags_from_decl_or_type (node->symbol.decl);
892 /* Optimize away PURE and CONST constructors and destructors. */
893 if (optimize
894 && (flags & (ECF_CONST | ECF_PURE))
895 && !(flags & ECF_LOOPING_CONST_OR_PURE))
897 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
898 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
901 /* Frontends and alias code marks nodes as needed before parsing is finished.
902 We may end up marking as node external nodes where this flag is meaningless
903 strip it. */
904 if (DECL_EXTERNAL (node->symbol.decl) || !node->symbol.definition)
906 node->symbol.force_output = 0;
907 node->symbol.forced_by_abi = 0;
910 /* C++ FE on lack of COMDAT support create local COMDAT functions
911 (that ought to be shared but can not due to object format
912 limitations). It is necessary to keep the flag to make rest of C++ FE
913 happy. Clear the flag here to avoid confusion in middle-end. */
914 if (DECL_COMDAT (node->symbol.decl) && !TREE_PUBLIC (node->symbol.decl))
915 DECL_COMDAT (node->symbol.decl) = 0;
917 /* For external decls stop tracking same_comdat_group. It doesn't matter
918 what comdat group they are in when they won't be emitted in this TU. */
919 if (node->symbol.same_comdat_group && DECL_EXTERNAL (node->symbol.decl))
921 #ifdef ENABLE_CHECKING
922 symtab_node n;
924 for (n = node->symbol.same_comdat_group;
925 n != (symtab_node)node;
926 n = n->symbol.same_comdat_group)
927 /* If at least one of same comdat group functions is external,
928 all of them have to be, otherwise it is a front-end bug. */
929 gcc_assert (DECL_EXTERNAL (n->symbol.decl));
930 #endif
931 symtab_dissolve_same_comdat_group_list ((symtab_node) node);
933 gcc_assert ((!DECL_WEAK (node->symbol.decl)
934 && !DECL_COMDAT (node->symbol.decl))
935 || TREE_PUBLIC (node->symbol.decl)
936 || node->symbol.weakref
937 || DECL_EXTERNAL (node->symbol.decl));
938 if (cgraph_externally_visible_p (node, whole_program))
940 gcc_assert (!node->global.inlined_to);
941 node->symbol.externally_visible = true;
943 else
945 node->symbol.externally_visible = false;
946 node->symbol.forced_by_abi = false;
948 if (!node->symbol.externally_visible
949 && node->symbol.definition && !node->symbol.weakref
950 && !DECL_EXTERNAL (node->symbol.decl))
952 gcc_assert (whole_program || in_lto_p
953 || !TREE_PUBLIC (node->symbol.decl));
954 node->symbol.unique_name = ((node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
955 || node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
956 && TREE_PUBLIC (node->symbol.decl));
957 symtab_make_decl_local (node->symbol.decl);
958 node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
959 if (node->symbol.same_comdat_group)
960 /* cgraph_externally_visible_p has already checked all other nodes
961 in the group and they will all be made local. We need to
962 dissolve the group at once so that the predicate does not
963 segfault though. */
964 symtab_dissolve_same_comdat_group_list ((symtab_node) node);
967 if (node->thunk.thunk_p
968 && TREE_PUBLIC (node->symbol.decl))
970 struct cgraph_node *decl_node = node;
972 decl_node = cgraph_function_node (decl_node->callees->callee, NULL);
974 /* Thunks have the same visibility as function they are attached to.
975 Make sure the C++ front end set this up properly. */
976 if (DECL_ONE_ONLY (decl_node->symbol.decl))
978 gcc_checking_assert (DECL_COMDAT (node->symbol.decl)
979 == DECL_COMDAT (decl_node->symbol.decl));
980 gcc_checking_assert (DECL_COMDAT_GROUP (node->symbol.decl)
981 == DECL_COMDAT_GROUP (decl_node->symbol.decl));
982 gcc_checking_assert (node->symbol.same_comdat_group);
984 if (DECL_EXTERNAL (decl_node->symbol.decl))
985 DECL_EXTERNAL (node->symbol.decl) = 1;
988 FOR_EACH_DEFINED_FUNCTION (node)
990 node->local.local |= cgraph_local_node_p (node);
992 /* If we know that function can not be overwritten by a different semantics
993 and moreover its section can not be discarded, replace all direct calls
994 by calls to an nonoverwritable alias. This make dynamic linking
995 cheaper and enable more optimization.
997 TODO: We can also update virtual tables. */
998 if (node->callers && can_replace_by_local_alias ((symtab_node)node))
1000 struct cgraph_node *alias = cgraph (symtab_nonoverwritable_alias ((symtab_node) node));
1002 if (alias && alias != node)
1004 while (node->callers)
1006 struct cgraph_edge *e = node->callers;
1008 cgraph_redirect_edge_callee (e, alias);
1009 if (gimple_has_body_p (e->caller->symbol.decl))
1011 push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl));
1012 cgraph_redirect_edge_call_stmt_to_callee (e);
1013 pop_cfun ();
1019 FOR_EACH_VARIABLE (vnode)
1021 /* weak flag makes no sense on local variables. */
1022 gcc_assert (!DECL_WEAK (vnode->symbol.decl)
1023 || vnode->symbol.weakref
1024 || TREE_PUBLIC (vnode->symbol.decl)
1025 || DECL_EXTERNAL (vnode->symbol.decl));
1026 /* In several cases declarations can not be common:
1028 - when declaration has initializer
1029 - when it is in weak
1030 - when it has specific section
1031 - when it resides in non-generic address space.
1032 - if declaration is local, it will get into .local common section
1033 so common flag is not needed. Frontends still produce these in
1034 certain cases, such as for:
1036 static int a __attribute__ ((common))
1038 Canonicalize things here and clear the redundant flag. */
1039 if (DECL_COMMON (vnode->symbol.decl)
1040 && (!(TREE_PUBLIC (vnode->symbol.decl)
1041 || DECL_EXTERNAL (vnode->symbol.decl))
1042 || (DECL_INITIAL (vnode->symbol.decl)
1043 && DECL_INITIAL (vnode->symbol.decl) != error_mark_node)
1044 || DECL_WEAK (vnode->symbol.decl)
1045 || DECL_SECTION_NAME (vnode->symbol.decl) != NULL
1046 || ! (ADDR_SPACE_GENERIC_P
1047 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->symbol.decl))))))
1048 DECL_COMMON (vnode->symbol.decl) = 0;
1050 FOR_EACH_DEFINED_VARIABLE (vnode)
1052 if (!vnode->symbol.definition)
1053 continue;
1054 if (varpool_externally_visible_p (vnode))
1055 vnode->symbol.externally_visible = true;
1056 else
1058 vnode->symbol.externally_visible = false;
1059 vnode->symbol.forced_by_abi = false;
1061 if (!vnode->symbol.externally_visible
1062 && !vnode->symbol.weakref)
1064 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->symbol.decl));
1065 vnode->symbol.unique_name = ((vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
1066 || vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
1067 && TREE_PUBLIC (vnode->symbol.decl));
1068 symtab_make_decl_local (vnode->symbol.decl);
1069 if (vnode->symbol.same_comdat_group)
1070 symtab_dissolve_same_comdat_group_list ((symtab_node) vnode);
1071 vnode->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
1075 if (dump_file)
1077 fprintf (dump_file, "\nMarking local functions:");
1078 FOR_EACH_DEFINED_FUNCTION (node)
1079 if (node->local.local)
1080 fprintf (dump_file, " %s", cgraph_node_name (node));
1081 fprintf (dump_file, "\n\n");
1082 fprintf (dump_file, "\nMarking externally visible functions:");
1083 FOR_EACH_DEFINED_FUNCTION (node)
1084 if (node->symbol.externally_visible)
1085 fprintf (dump_file, " %s", cgraph_node_name (node));
1086 fprintf (dump_file, "\n\n");
1087 fprintf (dump_file, "\nMarking externally visible variables:");
1088 FOR_EACH_DEFINED_VARIABLE (vnode)
1089 if (vnode->symbol.externally_visible)
1090 fprintf (dump_file, " %s", varpool_node_name (vnode));
1091 fprintf (dump_file, "\n\n");
1093 cgraph_function_flags_ready = true;
1094 return 0;
1097 /* Local function pass handling visibilities. This happens before LTO streaming
1098 so in particular -fwhole-program should be ignored at this level. */
1100 static unsigned int
1101 local_function_and_variable_visibility (void)
1103 return function_and_variable_visibility (flag_whole_program && !flag_lto);
1106 namespace {
1108 const pass_data pass_data_ipa_function_and_variable_visibility =
1110 SIMPLE_IPA_PASS, /* type */
1111 "visibility", /* name */
1112 OPTGROUP_NONE, /* optinfo_flags */
1113 false, /* has_gate */
1114 true, /* has_execute */
1115 TV_CGRAPHOPT, /* tv_id */
1116 0, /* properties_required */
1117 0, /* properties_provided */
1118 0, /* properties_destroyed */
1119 0, /* todo_flags_start */
1120 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1123 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
1125 public:
1126 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1127 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
1128 ctxt)
1131 /* opt_pass methods: */
1132 unsigned int execute () {
1133 return local_function_and_variable_visibility ();
1136 }; // class pass_ipa_function_and_variable_visibility
1138 } // anon namespace
1140 simple_ipa_opt_pass *
1141 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1143 return new pass_ipa_function_and_variable_visibility (ctxt);
1146 /* Free inline summary. */
1148 static unsigned
1149 free_inline_summary (void)
1151 inline_free_summary ();
1152 return 0;
1155 namespace {
1157 const pass_data pass_data_ipa_free_inline_summary =
1159 SIMPLE_IPA_PASS, /* type */
1160 "*free_inline_summary", /* name */
1161 OPTGROUP_NONE, /* optinfo_flags */
1162 false, /* has_gate */
1163 true, /* has_execute */
1164 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
1165 0, /* properties_required */
1166 0, /* properties_provided */
1167 0, /* properties_destroyed */
1168 0, /* todo_flags_start */
1169 0, /* todo_flags_finish */
1172 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
1174 public:
1175 pass_ipa_free_inline_summary (gcc::context *ctxt)
1176 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
1179 /* opt_pass methods: */
1180 unsigned int execute () { return free_inline_summary (); }
1182 }; // class pass_ipa_free_inline_summary
1184 } // anon namespace
1186 simple_ipa_opt_pass *
1187 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
1189 return new pass_ipa_free_inline_summary (ctxt);
1192 /* Do not re-run on ltrans stage. */
1194 static bool
1195 gate_whole_program_function_and_variable_visibility (void)
1197 return !flag_ltrans;
1200 /* Bring functionss local at LTO time with -fwhole-program. */
1202 static unsigned int
1203 whole_program_function_and_variable_visibility (void)
1205 function_and_variable_visibility (flag_whole_program);
1206 if (optimize)
1207 ipa_discover_readonly_nonaddressable_vars ();
1208 return 0;
1211 namespace {
1213 const pass_data pass_data_ipa_whole_program_visibility =
1215 IPA_PASS, /* type */
1216 "whole-program", /* name */
1217 OPTGROUP_NONE, /* optinfo_flags */
1218 true, /* has_gate */
1219 true, /* has_execute */
1220 TV_CGRAPHOPT, /* tv_id */
1221 0, /* properties_required */
1222 0, /* properties_provided */
1223 0, /* properties_destroyed */
1224 0, /* todo_flags_start */
1225 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1228 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
1230 public:
1231 pass_ipa_whole_program_visibility (gcc::context *ctxt)
1232 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
1233 NULL, /* generate_summary */
1234 NULL, /* write_summary */
1235 NULL, /* read_summary */
1236 NULL, /* write_optimization_summary */
1237 NULL, /* read_optimization_summary */
1238 NULL, /* stmt_fixup */
1239 0, /* function_transform_todo_flags_start */
1240 NULL, /* function_transform */
1241 NULL) /* variable_transform */
1244 /* opt_pass methods: */
1245 bool gate () {
1246 return gate_whole_program_function_and_variable_visibility ();
1248 unsigned int execute () {
1249 return whole_program_function_and_variable_visibility ();
1252 }; // class pass_ipa_whole_program_visibility
1254 } // anon namespace
1256 ipa_opt_pass_d *
1257 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
1259 return new pass_ipa_whole_program_visibility (ctxt);
1262 /* Generate and emit a static constructor or destructor. WHICH must
1263 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1264 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1265 initialization priority for this constructor or destructor.
1267 FINAL specify whether the externally visible name for collect2 should
1268 be produced. */
1270 static void
1271 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
1273 static int counter = 0;
1274 char which_buf[16];
1275 tree decl, name, resdecl;
1277 /* The priority is encoded in the constructor or destructor name.
1278 collect2 will sort the names and arrange that they are called at
1279 program startup. */
1280 if (final)
1281 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1282 else
1283 /* Proudce sane name but one not recognizable by collect2, just for the
1284 case we fail to inline the function. */
1285 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
1286 name = get_file_function_name (which_buf);
1288 decl = build_decl (input_location, FUNCTION_DECL, name,
1289 build_function_type_list (void_type_node, NULL_TREE));
1290 current_function_decl = decl;
1292 resdecl = build_decl (input_location,
1293 RESULT_DECL, NULL_TREE, void_type_node);
1294 DECL_ARTIFICIAL (resdecl) = 1;
1295 DECL_RESULT (decl) = resdecl;
1296 DECL_CONTEXT (resdecl) = decl;
1298 allocate_struct_function (decl, false);
1300 TREE_STATIC (decl) = 1;
1301 TREE_USED (decl) = 1;
1302 DECL_ARTIFICIAL (decl) = 1;
1303 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1304 DECL_SAVED_TREE (decl) = body;
1305 if (!targetm.have_ctors_dtors && final)
1307 TREE_PUBLIC (decl) = 1;
1308 DECL_PRESERVE_P (decl) = 1;
1310 DECL_UNINLINABLE (decl) = 1;
1312 DECL_INITIAL (decl) = make_node (BLOCK);
1313 TREE_USED (DECL_INITIAL (decl)) = 1;
1315 DECL_SOURCE_LOCATION (decl) = input_location;
1316 cfun->function_end_locus = input_location;
1318 switch (which)
1320 case 'I':
1321 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1322 decl_init_priority_insert (decl, priority);
1323 break;
1324 case 'D':
1325 DECL_STATIC_DESTRUCTOR (decl) = 1;
1326 decl_fini_priority_insert (decl, priority);
1327 break;
1328 default:
1329 gcc_unreachable ();
1332 gimplify_function_tree (decl);
1334 cgraph_add_new_function (decl, false);
1336 set_cfun (NULL);
1337 current_function_decl = NULL;
1340 /* Generate and emit a static constructor or destructor. WHICH must
1341 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1342 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1343 initialization priority for this constructor or destructor. */
1345 void
1346 cgraph_build_static_cdtor (char which, tree body, int priority)
1348 cgraph_build_static_cdtor_1 (which, body, priority, false);
1351 /* A vector of FUNCTION_DECLs declared as static constructors. */
1352 static vec<tree> static_ctors;
1353 /* A vector of FUNCTION_DECLs declared as static destructors. */
1354 static vec<tree> static_dtors;
1356 /* When target does not have ctors and dtors, we call all constructor
1357 and destructor by special initialization/destruction function
1358 recognized by collect2.
1360 When we are going to build this function, collect all constructors and
1361 destructors and turn them into normal functions. */
1363 static void
1364 record_cdtor_fn (struct cgraph_node *node)
1366 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1367 static_ctors.safe_push (node->symbol.decl);
1368 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1369 static_dtors.safe_push (node->symbol.decl);
1370 node = cgraph_get_node (node->symbol.decl);
1371 DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl) = 1;
1374 /* Define global constructors/destructor functions for the CDTORS, of
1375 which they are LEN. The CDTORS are sorted by initialization
1376 priority. If CTOR_P is true, these are constructors; otherwise,
1377 they are destructors. */
1379 static void
1380 build_cdtor (bool ctor_p, vec<tree> cdtors)
1382 size_t i,j;
1383 size_t len = cdtors.length ();
1385 i = 0;
1386 while (i < len)
1388 tree body;
1389 tree fn;
1390 priority_type priority;
1392 priority = 0;
1393 body = NULL_TREE;
1394 j = i;
1397 priority_type p;
1398 fn = cdtors[j];
1399 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1400 if (j == i)
1401 priority = p;
1402 else if (p != priority)
1403 break;
1404 j++;
1406 while (j < len);
1408 /* When there is only one cdtor and target supports them, do nothing. */
1409 if (j == i + 1
1410 && targetm.have_ctors_dtors)
1412 i++;
1413 continue;
1415 /* Find the next batch of constructors/destructors with the same
1416 initialization priority. */
1417 for (;i < j; i++)
1419 tree call;
1420 fn = cdtors[i];
1421 call = build_call_expr (fn, 0);
1422 if (ctor_p)
1423 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1424 else
1425 DECL_STATIC_DESTRUCTOR (fn) = 0;
1426 /* We do not want to optimize away pure/const calls here.
1427 When optimizing, these should be already removed, when not
1428 optimizing, we want user to be able to breakpoint in them. */
1429 TREE_SIDE_EFFECTS (call) = 1;
1430 append_to_statement_list (call, &body);
1432 gcc_assert (body != NULL_TREE);
1433 /* Generate a function to call all the function of like
1434 priority. */
1435 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1439 /* Comparison function for qsort. P1 and P2 are actually of type
1440 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1441 used to determine the sort order. */
1443 static int
1444 compare_ctor (const void *p1, const void *p2)
1446 tree f1;
1447 tree f2;
1448 int priority1;
1449 int priority2;
1451 f1 = *(const tree *)p1;
1452 f2 = *(const tree *)p2;
1453 priority1 = DECL_INIT_PRIORITY (f1);
1454 priority2 = DECL_INIT_PRIORITY (f2);
1456 if (priority1 < priority2)
1457 return -1;
1458 else if (priority1 > priority2)
1459 return 1;
1460 else
1461 /* Ensure a stable sort. Constructors are executed in backwarding
1462 order to make LTO initialize braries first. */
1463 return DECL_UID (f2) - DECL_UID (f1);
1466 /* Comparison function for qsort. P1 and P2 are actually of type
1467 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1468 used to determine the sort order. */
1470 static int
1471 compare_dtor (const void *p1, const void *p2)
1473 tree f1;
1474 tree f2;
1475 int priority1;
1476 int priority2;
1478 f1 = *(const tree *)p1;
1479 f2 = *(const tree *)p2;
1480 priority1 = DECL_FINI_PRIORITY (f1);
1481 priority2 = DECL_FINI_PRIORITY (f2);
1483 if (priority1 < priority2)
1484 return -1;
1485 else if (priority1 > priority2)
1486 return 1;
1487 else
1488 /* Ensure a stable sort. */
1489 return DECL_UID (f1) - DECL_UID (f2);
1492 /* Generate functions to call static constructors and destructors
1493 for targets that do not support .ctors/.dtors sections. These
1494 functions have magic names which are detected by collect2. */
1496 static void
1497 build_cdtor_fns (void)
1499 if (!static_ctors.is_empty ())
1501 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1502 static_ctors.qsort (compare_ctor);
1503 build_cdtor (/*ctor_p=*/true, static_ctors);
1506 if (!static_dtors.is_empty ())
1508 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1509 static_dtors.qsort (compare_dtor);
1510 build_cdtor (/*ctor_p=*/false, static_dtors);
1514 /* Look for constructors and destructors and produce function calling them.
1515 This is needed for targets not supporting ctors or dtors, but we perform the
1516 transformation also at linktime to merge possibly numerous
1517 constructors/destructors into single function to improve code locality and
1518 reduce size. */
1520 static unsigned int
1521 ipa_cdtor_merge (void)
1523 struct cgraph_node *node;
1524 FOR_EACH_DEFINED_FUNCTION (node)
1525 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
1526 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1527 record_cdtor_fn (node);
1528 build_cdtor_fns ();
1529 static_ctors.release ();
1530 static_dtors.release ();
1531 return 0;
1534 /* Perform the pass when we have no ctors/dtors support
1535 or at LTO time to merge multiple constructors into single
1536 function. */
1538 static bool
1539 gate_ipa_cdtor_merge (void)
1541 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1544 namespace {
1546 const pass_data pass_data_ipa_cdtor_merge =
1548 IPA_PASS, /* type */
1549 "cdtor", /* name */
1550 OPTGROUP_NONE, /* optinfo_flags */
1551 true, /* has_gate */
1552 true, /* has_execute */
1553 TV_CGRAPHOPT, /* tv_id */
1554 0, /* properties_required */
1555 0, /* properties_provided */
1556 0, /* properties_destroyed */
1557 0, /* todo_flags_start */
1558 0, /* todo_flags_finish */
1561 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1563 public:
1564 pass_ipa_cdtor_merge (gcc::context *ctxt)
1565 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1566 NULL, /* generate_summary */
1567 NULL, /* write_summary */
1568 NULL, /* read_summary */
1569 NULL, /* write_optimization_summary */
1570 NULL, /* read_optimization_summary */
1571 NULL, /* stmt_fixup */
1572 0, /* function_transform_todo_flags_start */
1573 NULL, /* function_transform */
1574 NULL) /* variable_transform */
1577 /* opt_pass methods: */
1578 bool gate () { return gate_ipa_cdtor_merge (); }
1579 unsigned int execute () { return ipa_cdtor_merge (); }
1581 }; // class pass_ipa_cdtor_merge
1583 } // anon namespace
1585 ipa_opt_pass_d *
1586 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1588 return new pass_ipa_cdtor_merge (ctxt);