2013-11-20 Jan-Benedict Glaw <jbglaw@lug-owl.de>
[official-gcc.git] / gcc / ipa.c
blob3950d4eb8568ec2bc61d296e4e4aa3b3d3e88599
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "calls.h"
26 #include "stringpool.h"
27 #include "cgraph.h"
28 #include "tree-pass.h"
29 #include "gimple.h"
30 #include "gimplify.h"
31 #include "ggc.h"
32 #include "flags.h"
33 #include "pointer-set.h"
34 #include "target.h"
35 #include "tree-iterator.h"
36 #include "ipa-utils.h"
37 #include "ipa-inline.h"
38 #include "tree-inline.h"
39 #include "profile.h"
40 #include "params.h"
42 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
44 static bool
45 cgraph_non_local_node_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
47 /* FIXME: Aliases can be local, but i386 gets thunks wrong then. */
48 return !(cgraph_only_called_directly_or_aliased_p (node)
49 && !ipa_ref_has_aliases_p (&node->ref_list)
50 && node->definition
51 && !DECL_EXTERNAL (node->decl)
52 && !node->externally_visible
53 && !node->used_from_other_partition
54 && !node->in_other_partition);
57 /* Return true when function can be marked local. */
59 static bool
60 cgraph_local_node_p (struct cgraph_node *node)
62 struct cgraph_node *n = cgraph_function_or_thunk_node (node, NULL);
64 /* FIXME: thunks can be considered local, but we need prevent i386
65 from attempting to change calling convention of them. */
66 if (n->thunk.thunk_p)
67 return false;
68 return !cgraph_for_node_and_aliases (n,
69 cgraph_non_local_node_p_1, NULL, true);
73 /* Return true when NODE has ADDR reference. */
75 static bool
76 has_addr_references_p (struct cgraph_node *node,
77 void *data ATTRIBUTE_UNUSED)
79 int i;
80 struct ipa_ref *ref;
82 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
83 i, ref); i++)
84 if (ref->use == IPA_REF_ADDR)
85 return true;
86 return false;
89 /* Look for all functions inlined to NODE and update their inlined_to pointers
90 to INLINED_TO. */
92 static void
93 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
95 struct cgraph_edge *e;
96 for (e = node->callees; e; e = e->next_callee)
97 if (e->callee->global.inlined_to)
99 e->callee->global.inlined_to = inlined_to;
100 update_inlined_to_pointer (e->callee, inlined_to);
104 /* Add symtab NODE to queue starting at FIRST.
106 The queue is linked via AUX pointers and terminated by pointer to 1.
107 We enqueue nodes at two occasions: when we find them reachable or when we find
108 their bodies needed for further clonning. In the second case we mark them
109 by pointer to 2 after processing so they are re-queue when they become
110 reachable. */
112 static void
113 enqueue_node (symtab_node *node, symtab_node **first,
114 struct pointer_set_t *reachable)
116 /* Node is still in queue; do nothing. */
117 if (node->aux && node->aux != (void *) 2)
118 return;
119 /* Node was already processed as unreachable, re-enqueue
120 only if it became reachable now. */
121 if (node->aux == (void *)2 && !pointer_set_contains (reachable, node))
122 return;
123 node->aux = *first;
124 *first = node;
127 /* Process references. */
129 static void
130 process_references (struct ipa_ref_list *list,
131 symtab_node **first,
132 bool before_inlining_p,
133 struct pointer_set_t *reachable)
135 int i;
136 struct ipa_ref *ref;
137 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
139 symtab_node *node = ref->referred;
141 if (node->definition && !node->in_other_partition
142 && ((!DECL_EXTERNAL (node->decl) || node->alias)
143 || (before_inlining_p
144 /* We use variable constructors during late complation for
145 constant folding. Keep references alive so partitioning
146 knows about potential references. */
147 || (TREE_CODE (node->decl) == VAR_DECL
148 && flag_wpa
149 && ctor_for_folding (node->decl)
150 != error_mark_node))))
151 pointer_set_insert (reachable, node);
152 enqueue_node (node, first, reachable);
156 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
157 all its potential targets as reachable to permit later inlining if
158 devirtualization happens. After inlining still keep their declarations
159 around, so we can devirtualize to a direct call.
161 Also try to make trivial devirutalization when no or only one target is
162 possible. */
164 static void
165 walk_polymorphic_call_targets (pointer_set_t *reachable_call_targets,
166 struct cgraph_edge *edge,
167 symtab_node **first,
168 pointer_set_t *reachable, bool before_inlining_p)
170 unsigned int i;
171 void *cache_token;
172 bool final;
173 vec <cgraph_node *>targets
174 = possible_polymorphic_call_targets
175 (edge, &final, &cache_token);
177 if (!pointer_set_insert (reachable_call_targets,
178 cache_token))
180 for (i = 0; i < targets.length (); i++)
182 struct cgraph_node *n = targets[i];
184 /* Do not bother to mark virtual methods in anonymous namespace;
185 either we will find use of virtual table defining it, or it is
186 unused. */
187 if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
188 && type_in_anonymous_namespace_p
189 (method_class_type (TREE_TYPE (n->decl))))
190 continue;
192 /* Prior inlining, keep alive bodies of possible targets for
193 devirtualization. */
194 if (n->definition
195 && before_inlining_p)
196 pointer_set_insert (reachable, n);
198 /* Even after inlining we want to keep the possible targets in the
199 boundary, so late passes can still produce direct call even if
200 the chance for inlining is lost. */
201 enqueue_node (n, first, reachable);
205 /* Very trivial devirtualization; when the type is
206 final or anonymous (so we know all its derivation)
207 and there is only one possible virtual call target,
208 make the edge direct. */
209 if (final)
211 if (targets.length () <= 1)
213 cgraph_node *target, *node = edge->caller;
214 if (targets.length () == 1)
215 target = targets[0];
216 else
217 target = cgraph_get_create_node
218 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
220 if (dump_file)
221 fprintf (dump_file,
222 "Devirtualizing call in %s/%i to %s/%i\n",
223 edge->caller->name (),
224 edge->caller->order,
225 target->name (), target->order);
226 edge = cgraph_make_edge_direct (edge, target);
227 if (!inline_summary_vec && edge->call_stmt)
228 cgraph_redirect_edge_call_stmt_to_callee (edge);
229 else
230 inline_update_overall_summary (node);
235 /* Perform reachability analysis and reclaim all unreachable nodes.
237 The algorithm is basically mark&sweep but with some extra refinements:
239 - reachable extern inline functions needs special handling; the bodies needs
240 to stay in memory until inlining in hope that they will be inlined.
241 After inlining we release their bodies and turn them into unanalyzed
242 nodes even when they are reachable.
244 BEFORE_INLINING_P specify whether we are before or after inlining.
246 - virtual functions are kept in callgraph even if they seem unreachable in
247 hope calls to them will be devirtualized.
249 Again we remove them after inlining. In late optimization some
250 devirtualization may happen, but it is not importnat since we won't inline
251 the call. In theory early opts and IPA should work out all important cases.
253 - virtual clones needs bodies of their origins for later materialization;
254 this means that we want to keep the body even if the origin is unreachable
255 otherwise. To avoid origin from sitting in the callgraph and being
256 walked by IPA passes, we turn them into unanalyzed nodes with body
257 defined.
259 We maintain set of function declaration where body needs to stay in
260 body_needed_for_clonning
262 Inline clones represent special case: their declaration match the
263 declaration of origin and cgraph_remove_node already knows how to
264 reshape callgraph and preserve body when offline copy of function or
265 inline clone is being removed.
267 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
268 variables with DECL_INITIAL set. We finalize these and keep reachable
269 ones around for constant folding purposes. After inlining we however
270 stop walking their references to let everything static referneced by them
271 to be removed when it is otherwise unreachable.
273 We maintain queue of both reachable symbols (i.e. defined symbols that needs
274 to stay) and symbols that are in boundary (i.e. external symbols referenced
275 by reachable symbols or origins of clones). The queue is represented
276 as linked list by AUX pointer terminated by 1.
278 A the end we keep all reachable symbols. For symbols in boundary we always
279 turn definition into a declaration, but we may keep function body around
280 based on body_needed_for_clonning
282 All symbols that enter the queue have AUX pointer non-zero and are in the
283 boundary. Pointer set REACHABLE is used to track reachable symbols.
285 Every symbol can be visited twice - once as part of boundary and once
286 as real reachable symbol. enqueue_node needs to decide whether the
287 node needs to be re-queued for second processing. For this purpose
288 we set AUX pointer of processed symbols in the boundary to constant 2. */
290 bool
291 symtab_remove_unreachable_nodes (bool before_inlining_p, FILE *file)
293 symtab_node *first = (symtab_node *) (void *) 1;
294 struct cgraph_node *node, *next;
295 struct varpool_node *vnode, *vnext;
296 bool changed = false;
297 struct pointer_set_t *reachable = pointer_set_create ();
298 struct pointer_set_t *body_needed_for_clonning = pointer_set_create ();
299 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
301 timevar_push (TV_IPA_UNREACHABLE);
302 #ifdef ENABLE_CHECKING
303 verify_symtab ();
304 #endif
305 if (optimize && flag_devirtualize)
306 build_type_inheritance_graph ();
307 if (file)
308 fprintf (file, "\nReclaiming functions:");
309 #ifdef ENABLE_CHECKING
310 FOR_EACH_FUNCTION (node)
311 gcc_assert (!node->aux);
312 FOR_EACH_VARIABLE (vnode)
313 gcc_assert (!vnode->aux);
314 #endif
315 /* Mark functions whose bodies are obviously needed.
316 This is mostly when they can be referenced externally. Inline clones
317 are special since their declarations are shared with master clone and thus
318 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
319 FOR_EACH_FUNCTION (node)
321 node->used_as_abstract_origin = false;
322 if (node->definition
323 && !node->global.inlined_to
324 && !node->in_other_partition
325 && !cgraph_can_remove_if_no_direct_calls_and_refs_p (node))
327 gcc_assert (!node->global.inlined_to);
328 pointer_set_insert (reachable, node);
329 enqueue_node (node, &first, reachable);
331 else
332 gcc_assert (!node->aux);
335 /* Mark variables that are obviously needed. */
336 FOR_EACH_DEFINED_VARIABLE (vnode)
337 if (!varpool_can_remove_if_no_refs (vnode)
338 && !vnode->in_other_partition)
340 pointer_set_insert (reachable, vnode);
341 enqueue_node (vnode, &first, reachable);
344 /* Perform reachability analysis. */
345 while (first != (symtab_node *) (void *) 1)
347 bool in_boundary_p = !pointer_set_contains (reachable, first);
348 symtab_node *node = first;
350 first = (symtab_node *)first->aux;
352 /* If we are processing symbol in boundary, mark its AUX pointer for
353 possible later re-processing in enqueue_node. */
354 if (in_boundary_p)
355 node->aux = (void *)2;
356 else
358 if (DECL_ABSTRACT_ORIGIN (node->decl))
360 struct cgraph_node *origin_node
361 = cgraph_get_create_node (DECL_ABSTRACT_ORIGIN (node->decl));
362 origin_node->used_as_abstract_origin = true;
363 enqueue_node (origin_node, &first, reachable);
365 /* If any symbol in a comdat group is reachable, force
366 all other in the same comdat group to be also reachable. */
367 if (node->same_comdat_group)
369 symtab_node *next;
370 for (next = node->same_comdat_group;
371 next != node;
372 next = next->same_comdat_group)
373 if (!pointer_set_insert (reachable, next))
374 enqueue_node (next, &first, reachable);
376 /* Mark references as reachable. */
377 process_references (&node->ref_list, &first,
378 before_inlining_p, reachable);
381 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
383 /* Mark the callees reachable unless they are direct calls to extern
384 inline functions we decided to not inline. */
385 if (!in_boundary_p)
387 struct cgraph_edge *e;
388 /* Keep alive possible targets for devirtualization. */
389 if (optimize && flag_devirtualize)
391 struct cgraph_edge *next;
392 for (e = cnode->indirect_calls; e; e = next)
394 next = e->next_callee;
395 if (e->indirect_info->polymorphic)
396 walk_polymorphic_call_targets (reachable_call_targets,
397 e, &first, reachable,
398 before_inlining_p);
401 for (e = cnode->callees; e; e = e->next_callee)
403 if (e->callee->definition
404 && !e->callee->in_other_partition
405 && (!e->inline_failed
406 || !DECL_EXTERNAL (e->callee->decl)
407 || e->callee->alias
408 || before_inlining_p))
409 pointer_set_insert (reachable, e->callee);
410 enqueue_node (e->callee, &first, reachable);
413 /* When inline clone exists, mark body to be preserved so when removing
414 offline copy of the function we don't kill it. */
415 if (cnode->global.inlined_to)
416 pointer_set_insert (body_needed_for_clonning, cnode->decl);
418 /* For non-inline clones, force their origins to the boundary and ensure
419 that body is not removed. */
420 while (cnode->clone_of)
422 bool noninline = cnode->clone_of->decl != cnode->decl;
423 cnode = cnode->clone_of;
424 if (noninline)
426 pointer_set_insert (body_needed_for_clonning, cnode->decl);
427 enqueue_node (cnode, &first, reachable);
432 /* When we see constructor of external variable, keep referred nodes in the
433 boundary. This will also hold initializers of the external vars NODE
434 refers to. */
435 varpool_node *vnode = dyn_cast <varpool_node> (node);
436 if (vnode
437 && DECL_EXTERNAL (node->decl)
438 && !vnode->alias
439 && in_boundary_p)
441 struct ipa_ref *ref;
442 for (int i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
443 enqueue_node (ref->referred, &first, reachable);
447 /* Remove unreachable functions. */
448 for (node = cgraph_first_function (); node; node = next)
450 next = cgraph_next_function (node);
452 /* If node is not needed at all, remove it. */
453 if (!node->aux)
455 if (file)
456 fprintf (file, " %s", node->name ());
457 cgraph_remove_node (node);
458 changed = true;
460 /* If node is unreachable, remove its body. */
461 else if (!pointer_set_contains (reachable, node))
463 if (!pointer_set_contains (body_needed_for_clonning, node->decl))
464 cgraph_release_function_body (node);
465 else if (!node->clone_of)
466 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
467 if (node->definition)
469 if (file)
470 fprintf (file, " %s", node->name ());
471 node->analyzed = false;
472 node->definition = false;
473 node->cpp_implicit_alias = false;
474 node->alias = false;
475 node->weakref = false;
476 if (!node->in_other_partition)
477 node->local.local = false;
478 cgraph_node_remove_callees (node);
479 ipa_remove_all_references (&node->ref_list);
480 changed = true;
483 else
484 gcc_assert (node->clone_of || !cgraph_function_with_gimple_body_p (node)
485 || in_lto_p || DECL_RESULT (node->decl));
488 /* Inline clones might be kept around so their materializing allows further
489 cloning. If the function the clone is inlined into is removed, we need
490 to turn it into normal cone. */
491 FOR_EACH_FUNCTION (node)
493 if (node->global.inlined_to
494 && !node->callers)
496 gcc_assert (node->clones);
497 node->global.inlined_to = NULL;
498 update_inlined_to_pointer (node, node);
500 node->aux = NULL;
503 /* Remove unreachable variables. */
504 if (file)
505 fprintf (file, "\nReclaiming variables:");
506 for (vnode = varpool_first_variable (); vnode; vnode = vnext)
508 vnext = varpool_next_variable (vnode);
509 if (!vnode->aux
510 /* For can_refer_decl_in_current_unit_p we want to track for
511 all external variables if they are defined in other partition
512 or not. */
513 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
515 if (file)
516 fprintf (file, " %s", vnode->name ());
517 varpool_remove_node (vnode);
518 changed = true;
520 else if (!pointer_set_contains (reachable, vnode))
522 tree init;
523 if (vnode->definition)
525 if (file)
526 fprintf (file, " %s", vnode->name ());
527 changed = true;
529 vnode->definition = false;
530 vnode->analyzed = false;
531 vnode->aux = NULL;
533 /* Keep body if it may be useful for constant folding. */
534 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node)
535 varpool_remove_initializer (vnode);
536 else
537 DECL_INITIAL (vnode->decl) = init;
538 ipa_remove_all_references (&vnode->ref_list);
540 else
541 vnode->aux = NULL;
544 pointer_set_destroy (reachable);
545 pointer_set_destroy (body_needed_for_clonning);
546 pointer_set_destroy (reachable_call_targets);
548 /* Now update address_taken flags and try to promote functions to be local. */
549 if (file)
550 fprintf (file, "\nClearing address taken flags:");
551 FOR_EACH_DEFINED_FUNCTION (node)
552 if (node->address_taken
553 && !node->used_from_other_partition)
555 if (!cgraph_for_node_and_aliases (node, has_addr_references_p, NULL, true))
557 if (file)
558 fprintf (file, " %s", node->name ());
559 node->address_taken = false;
560 changed = true;
561 if (cgraph_local_node_p (node))
563 node->local.local = true;
564 if (file)
565 fprintf (file, " (local)");
569 if (file)
570 fprintf (file, "\n");
572 #ifdef ENABLE_CHECKING
573 verify_symtab ();
574 #endif
576 /* If we removed something, perhaps profile could be improved. */
577 if (changed && optimize && inline_edge_summary_vec.exists ())
578 FOR_EACH_DEFINED_FUNCTION (node)
579 ipa_propagate_frequency (node);
581 timevar_pop (TV_IPA_UNREACHABLE);
582 return changed;
585 /* Discover variables that have no longer address taken or that are read only
586 and update their flags.
588 FIXME: This can not be done in between gimplify and omp_expand since
589 readonly flag plays role on what is shared and what is not. Currently we do
590 this transformation as part of whole program visibility and re-do at
591 ipa-reference pass (to take into account clonning), but it would
592 make sense to do it before early optimizations. */
594 void
595 ipa_discover_readonly_nonaddressable_vars (void)
597 struct varpool_node *vnode;
598 if (dump_file)
599 fprintf (dump_file, "Clearing variable flags:");
600 FOR_EACH_VARIABLE (vnode)
601 if (vnode->definition && varpool_all_refs_explicit_p (vnode)
602 && (TREE_ADDRESSABLE (vnode->decl)
603 || !TREE_READONLY (vnode->decl)))
605 bool written = false;
606 bool address_taken = false;
607 int i;
608 struct ipa_ref *ref;
609 for (i = 0; ipa_ref_list_referring_iterate (&vnode->ref_list,
610 i, ref)
611 && (!written || !address_taken); i++)
612 switch (ref->use)
614 case IPA_REF_ADDR:
615 address_taken = true;
616 break;
617 case IPA_REF_LOAD:
618 break;
619 case IPA_REF_STORE:
620 written = true;
621 break;
623 if (TREE_ADDRESSABLE (vnode->decl) && !address_taken)
625 if (dump_file)
626 fprintf (dump_file, " %s (addressable)", vnode->name ());
627 TREE_ADDRESSABLE (vnode->decl) = 0;
629 if (!TREE_READONLY (vnode->decl) && !address_taken && !written
630 /* Making variable in explicit section readonly can cause section
631 type conflict.
632 See e.g. gcc.c-torture/compile/pr23237.c */
633 && DECL_SECTION_NAME (vnode->decl) == NULL)
635 if (dump_file)
636 fprintf (dump_file, " %s (read-only)", vnode->name ());
637 TREE_READONLY (vnode->decl) = 1;
640 if (dump_file)
641 fprintf (dump_file, "\n");
644 /* Return true when there is a reference to node and it is not vtable. */
645 static bool
646 address_taken_from_non_vtable_p (symtab_node *node)
648 int i;
649 struct ipa_ref *ref;
650 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
651 i, ref); i++)
652 if (ref->use == IPA_REF_ADDR)
654 struct varpool_node *node;
655 if (is_a <cgraph_node> (ref->referring))
656 return true;
657 node = ipa_ref_referring_varpool_node (ref);
658 if (!DECL_VIRTUAL_P (node->decl))
659 return true;
661 return false;
664 /* A helper for comdat_can_be_unshared_p. */
666 static bool
667 comdat_can_be_unshared_p_1 (symtab_node *node)
669 /* When address is taken, we don't know if equality comparison won't
670 break eventually. Exception are virutal functions, C++
671 constructors/destructors and vtables, where this is not possible by
672 language standard. */
673 if (!DECL_VIRTUAL_P (node->decl)
674 && (TREE_CODE (node->decl) != FUNCTION_DECL
675 || (!DECL_CXX_CONSTRUCTOR_P (node->decl)
676 && !DECL_CXX_DESTRUCTOR_P (node->decl)))
677 && address_taken_from_non_vtable_p (node))
678 return false;
680 /* If the symbol is used in some weird way, better to not touch it. */
681 if (node->force_output)
682 return false;
684 /* Explicit instantiations needs to be output when possibly
685 used externally. */
686 if (node->forced_by_abi
687 && TREE_PUBLIC (node->decl)
688 && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
689 && !flag_whole_program))
690 return false;
692 /* Non-readonly and volatile variables can not be duplicated. */
693 if (is_a <varpool_node> (node)
694 && (!TREE_READONLY (node->decl)
695 || TREE_THIS_VOLATILE (node->decl)))
696 return false;
697 return true;
700 /* COMDAT functions must be shared only if they have address taken,
701 otherwise we can produce our own private implementation with
702 -fwhole-program.
703 Return true when turning COMDAT functoin static can not lead to wrong
704 code when the resulting object links with a library defining same COMDAT.
706 Virtual functions do have their addresses taken from the vtables,
707 but in C++ there is no way to compare their addresses for equality. */
709 static bool
710 comdat_can_be_unshared_p (symtab_node *node)
712 if (!comdat_can_be_unshared_p_1 (node))
713 return false;
714 if (node->same_comdat_group)
716 symtab_node *next;
718 /* If more than one function is in the same COMDAT group, it must
719 be shared even if just one function in the comdat group has
720 address taken. */
721 for (next = node->same_comdat_group;
722 next != node; next = next->same_comdat_group)
723 if (!comdat_can_be_unshared_p_1 (next))
724 return false;
726 return true;
729 /* Return true when function NODE should be considered externally visible. */
731 static bool
732 cgraph_externally_visible_p (struct cgraph_node *node,
733 bool whole_program)
735 if (!node->definition)
736 return false;
737 if (!TREE_PUBLIC (node->decl)
738 || DECL_EXTERNAL (node->decl))
739 return false;
741 /* Do not try to localize built-in functions yet. One of problems is that we
742 end up mangling their asm for WHOPR that makes it impossible to call them
743 using the implicit built-in declarations anymore. Similarly this enables
744 us to remove them as unreachable before actual calls may appear during
745 expansion or folding. */
746 if (DECL_BUILT_IN (node->decl))
747 return true;
749 /* If linker counts on us, we must preserve the function. */
750 if (symtab_used_from_object_file_p (node))
751 return true;
752 if (DECL_PRESERVE_P (node->decl))
753 return true;
754 if (lookup_attribute ("externally_visible",
755 DECL_ATTRIBUTES (node->decl)))
756 return true;
757 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
758 && lookup_attribute ("dllexport",
759 DECL_ATTRIBUTES (node->decl)))
760 return true;
761 if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
762 return false;
763 /* When doing LTO or whole program, we can bring COMDAT functoins static.
764 This improves code quality and we know we will duplicate them at most twice
765 (in the case that we are not using plugin and link with object file
766 implementing same COMDAT) */
767 if ((in_lto_p || whole_program)
768 && DECL_COMDAT (node->decl)
769 && comdat_can_be_unshared_p (node))
770 return false;
772 /* When doing link time optimizations, hidden symbols become local. */
773 if (in_lto_p
774 && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
775 || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
776 /* Be sure that node is defined in IR file, not in other object
777 file. In that case we don't set used_from_other_object_file. */
778 && node->definition)
780 else if (!whole_program)
781 return true;
783 if (MAIN_NAME_P (DECL_NAME (node->decl)))
784 return true;
786 return false;
789 /* Return true when variable VNODE should be considered externally visible. */
791 bool
792 varpool_externally_visible_p (struct varpool_node *vnode)
794 if (DECL_EXTERNAL (vnode->decl))
795 return true;
797 if (!TREE_PUBLIC (vnode->decl))
798 return false;
800 /* If linker counts on us, we must preserve the function. */
801 if (symtab_used_from_object_file_p (vnode))
802 return true;
804 if (DECL_HARD_REGISTER (vnode->decl))
805 return true;
806 if (DECL_PRESERVE_P (vnode->decl))
807 return true;
808 if (lookup_attribute ("externally_visible",
809 DECL_ATTRIBUTES (vnode->decl)))
810 return true;
811 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
812 && lookup_attribute ("dllexport",
813 DECL_ATTRIBUTES (vnode->decl)))
814 return true;
816 /* See if we have linker information about symbol not being used or
817 if we need to make guess based on the declaration.
819 Even if the linker clams the symbol is unused, never bring internal
820 symbols that are declared by user as used or externally visible.
821 This is needed for i.e. references from asm statements. */
822 if (symtab_used_from_object_file_p (vnode))
823 return true;
824 if (vnode->resolution == LDPR_PREVAILING_DEF_IRONLY)
825 return false;
827 /* As a special case, the COMDAT virtual tables can be unshared.
828 In LTO mode turn vtables into static variables. The variable is readonly,
829 so this does not enable more optimization, but referring static var
830 is faster for dynamic linking. Also this match logic hidding vtables
831 from LTO symbol tables. */
832 if ((in_lto_p || flag_whole_program)
833 && DECL_COMDAT (vnode->decl)
834 && comdat_can_be_unshared_p (vnode))
835 return false;
837 /* When doing link time optimizations, hidden symbols become local. */
838 if (in_lto_p
839 && (DECL_VISIBILITY (vnode->decl) == VISIBILITY_HIDDEN
840 || DECL_VISIBILITY (vnode->decl) == VISIBILITY_INTERNAL)
841 /* Be sure that node is defined in IR file, not in other object
842 file. In that case we don't set used_from_other_object_file. */
843 && vnode->definition)
845 else if (!flag_whole_program)
846 return true;
848 /* Do not attempt to privatize COMDATS by default.
849 This would break linking with C++ libraries sharing
850 inline definitions.
852 FIXME: We can do so for readonly vars with no address taken and
853 possibly also for vtables since no direct pointer comparsion is done.
854 It might be interesting to do so to reduce linking overhead. */
855 if (DECL_COMDAT (vnode->decl) || DECL_WEAK (vnode->decl))
856 return true;
857 return false;
860 /* Return true if reference to NODE can be replaced by a local alias.
861 Local aliases save dynamic linking overhead and enable more optimizations.
864 bool
865 can_replace_by_local_alias (symtab_node *node)
867 return (symtab_node_availability (node) > AVAIL_OVERWRITABLE
868 && !symtab_can_be_discarded (node));
871 /* Mark visibility of all functions.
873 A local function is one whose calls can occur only in the current
874 compilation unit and all its calls are explicit, so we can change
875 its calling convention. We simply mark all static functions whose
876 address is not taken as local.
878 We also change the TREE_PUBLIC flag of all declarations that are public
879 in language point of view but we want to overwrite this default
880 via visibilities for the backend point of view. */
882 static unsigned int
883 function_and_variable_visibility (bool whole_program)
885 struct cgraph_node *node;
886 struct varpool_node *vnode;
888 /* All aliases should be procssed at this point. */
889 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
891 FOR_EACH_FUNCTION (node)
893 int flags = flags_from_decl_or_type (node->decl);
895 /* Optimize away PURE and CONST constructors and destructors. */
896 if (optimize
897 && (flags & (ECF_CONST | ECF_PURE))
898 && !(flags & ECF_LOOPING_CONST_OR_PURE))
900 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
901 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
904 /* Frontends and alias code marks nodes as needed before parsing is finished.
905 We may end up marking as node external nodes where this flag is meaningless
906 strip it. */
907 if (DECL_EXTERNAL (node->decl) || !node->definition)
909 node->force_output = 0;
910 node->forced_by_abi = 0;
913 /* C++ FE on lack of COMDAT support create local COMDAT functions
914 (that ought to be shared but can not due to object format
915 limitations). It is necessary to keep the flag to make rest of C++ FE
916 happy. Clear the flag here to avoid confusion in middle-end. */
917 if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
918 DECL_COMDAT (node->decl) = 0;
920 /* For external decls stop tracking same_comdat_group. It doesn't matter
921 what comdat group they are in when they won't be emitted in this TU. */
922 if (node->same_comdat_group && DECL_EXTERNAL (node->decl))
924 #ifdef ENABLE_CHECKING
925 symtab_node *n;
927 for (n = node->same_comdat_group;
928 n != node;
929 n = n->same_comdat_group)
930 /* If at least one of same comdat group functions is external,
931 all of them have to be, otherwise it is a front-end bug. */
932 gcc_assert (DECL_EXTERNAL (n->decl));
933 #endif
934 symtab_dissolve_same_comdat_group_list (node);
936 gcc_assert ((!DECL_WEAK (node->decl)
937 && !DECL_COMDAT (node->decl))
938 || TREE_PUBLIC (node->decl)
939 || node->weakref
940 || DECL_EXTERNAL (node->decl));
941 if (cgraph_externally_visible_p (node, whole_program))
943 gcc_assert (!node->global.inlined_to);
944 node->externally_visible = true;
946 else
948 node->externally_visible = false;
949 node->forced_by_abi = false;
951 if (!node->externally_visible
952 && node->definition && !node->weakref
953 && !DECL_EXTERNAL (node->decl))
955 gcc_assert (whole_program || in_lto_p
956 || !TREE_PUBLIC (node->decl));
957 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
958 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
959 && TREE_PUBLIC (node->decl));
960 symtab_make_decl_local (node->decl);
961 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
962 if (node->same_comdat_group)
963 /* cgraph_externally_visible_p has already checked all other nodes
964 in the group and they will all be made local. We need to
965 dissolve the group at once so that the predicate does not
966 segfault though. */
967 symtab_dissolve_same_comdat_group_list (node);
970 if (node->thunk.thunk_p
971 && TREE_PUBLIC (node->decl))
973 struct cgraph_node *decl_node = node;
975 decl_node = cgraph_function_node (decl_node->callees->callee, NULL);
977 /* Thunks have the same visibility as function they are attached to.
978 Make sure the C++ front end set this up properly. */
979 if (DECL_ONE_ONLY (decl_node->decl))
981 gcc_checking_assert (DECL_COMDAT (node->decl)
982 == DECL_COMDAT (decl_node->decl));
983 gcc_checking_assert (DECL_COMDAT_GROUP (node->decl)
984 == DECL_COMDAT_GROUP (decl_node->decl));
985 gcc_checking_assert (node->same_comdat_group);
987 if (DECL_EXTERNAL (decl_node->decl))
988 DECL_EXTERNAL (node->decl) = 1;
991 FOR_EACH_DEFINED_FUNCTION (node)
993 node->local.local |= cgraph_local_node_p (node);
995 /* If we know that function can not be overwritten by a different semantics
996 and moreover its section can not be discarded, replace all direct calls
997 by calls to an nonoverwritable alias. This make dynamic linking
998 cheaper and enable more optimization.
1000 TODO: We can also update virtual tables. */
1001 if (node->callers && can_replace_by_local_alias (node))
1003 struct cgraph_node *alias = cgraph (symtab_nonoverwritable_alias (node));
1005 if (alias && alias != node)
1007 while (node->callers)
1009 struct cgraph_edge *e = node->callers;
1011 cgraph_redirect_edge_callee (e, alias);
1012 if (gimple_has_body_p (e->caller->decl))
1014 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1015 cgraph_redirect_edge_call_stmt_to_callee (e);
1016 pop_cfun ();
1022 FOR_EACH_VARIABLE (vnode)
1024 /* weak flag makes no sense on local variables. */
1025 gcc_assert (!DECL_WEAK (vnode->decl)
1026 || vnode->weakref
1027 || TREE_PUBLIC (vnode->decl)
1028 || DECL_EXTERNAL (vnode->decl));
1029 /* In several cases declarations can not be common:
1031 - when declaration has initializer
1032 - when it is in weak
1033 - when it has specific section
1034 - when it resides in non-generic address space.
1035 - if declaration is local, it will get into .local common section
1036 so common flag is not needed. Frontends still produce these in
1037 certain cases, such as for:
1039 static int a __attribute__ ((common))
1041 Canonicalize things here and clear the redundant flag. */
1042 if (DECL_COMMON (vnode->decl)
1043 && (!(TREE_PUBLIC (vnode->decl)
1044 || DECL_EXTERNAL (vnode->decl))
1045 || (DECL_INITIAL (vnode->decl)
1046 && DECL_INITIAL (vnode->decl) != error_mark_node)
1047 || DECL_WEAK (vnode->decl)
1048 || DECL_SECTION_NAME (vnode->decl) != NULL
1049 || ! (ADDR_SPACE_GENERIC_P
1050 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
1051 DECL_COMMON (vnode->decl) = 0;
1053 FOR_EACH_DEFINED_VARIABLE (vnode)
1055 if (!vnode->definition)
1056 continue;
1057 if (varpool_externally_visible_p (vnode))
1058 vnode->externally_visible = true;
1059 else
1061 vnode->externally_visible = false;
1062 vnode->forced_by_abi = false;
1064 if (!vnode->externally_visible
1065 && !vnode->weakref)
1067 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->decl));
1068 vnode->unique_name = ((vnode->resolution == LDPR_PREVAILING_DEF_IRONLY
1069 || vnode->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
1070 && TREE_PUBLIC (vnode->decl));
1071 symtab_make_decl_local (vnode->decl);
1072 if (vnode->same_comdat_group)
1073 symtab_dissolve_same_comdat_group_list (vnode);
1074 vnode->resolution = LDPR_PREVAILING_DEF_IRONLY;
1078 if (dump_file)
1080 fprintf (dump_file, "\nMarking local functions:");
1081 FOR_EACH_DEFINED_FUNCTION (node)
1082 if (node->local.local)
1083 fprintf (dump_file, " %s", node->name ());
1084 fprintf (dump_file, "\n\n");
1085 fprintf (dump_file, "\nMarking externally visible functions:");
1086 FOR_EACH_DEFINED_FUNCTION (node)
1087 if (node->externally_visible)
1088 fprintf (dump_file, " %s", node->name ());
1089 fprintf (dump_file, "\n\n");
1090 fprintf (dump_file, "\nMarking externally visible variables:");
1091 FOR_EACH_DEFINED_VARIABLE (vnode)
1092 if (vnode->externally_visible)
1093 fprintf (dump_file, " %s", vnode->name ());
1094 fprintf (dump_file, "\n\n");
1096 cgraph_function_flags_ready = true;
1097 return 0;
1100 /* Local function pass handling visibilities. This happens before LTO streaming
1101 so in particular -fwhole-program should be ignored at this level. */
1103 static unsigned int
1104 local_function_and_variable_visibility (void)
1106 return function_and_variable_visibility (flag_whole_program && !flag_lto);
1109 namespace {
1111 const pass_data pass_data_ipa_function_and_variable_visibility =
1113 SIMPLE_IPA_PASS, /* type */
1114 "visibility", /* name */
1115 OPTGROUP_NONE, /* optinfo_flags */
1116 false, /* has_gate */
1117 true, /* has_execute */
1118 TV_CGRAPHOPT, /* tv_id */
1119 0, /* properties_required */
1120 0, /* properties_provided */
1121 0, /* properties_destroyed */
1122 0, /* todo_flags_start */
1123 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1126 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
1128 public:
1129 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1130 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
1131 ctxt)
1134 /* opt_pass methods: */
1135 unsigned int execute () {
1136 return local_function_and_variable_visibility ();
1139 }; // class pass_ipa_function_and_variable_visibility
1141 } // anon namespace
1143 simple_ipa_opt_pass *
1144 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1146 return new pass_ipa_function_and_variable_visibility (ctxt);
1149 /* Free inline summary. */
1151 static unsigned
1152 free_inline_summary (void)
1154 inline_free_summary ();
1155 return 0;
1158 namespace {
1160 const pass_data pass_data_ipa_free_inline_summary =
1162 SIMPLE_IPA_PASS, /* type */
1163 "*free_inline_summary", /* name */
1164 OPTGROUP_NONE, /* optinfo_flags */
1165 false, /* has_gate */
1166 true, /* has_execute */
1167 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
1168 0, /* properties_required */
1169 0, /* properties_provided */
1170 0, /* properties_destroyed */
1171 0, /* todo_flags_start */
1172 0, /* todo_flags_finish */
1175 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
1177 public:
1178 pass_ipa_free_inline_summary (gcc::context *ctxt)
1179 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
1182 /* opt_pass methods: */
1183 unsigned int execute () { return free_inline_summary (); }
1185 }; // class pass_ipa_free_inline_summary
1187 } // anon namespace
1189 simple_ipa_opt_pass *
1190 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
1192 return new pass_ipa_free_inline_summary (ctxt);
1195 /* Do not re-run on ltrans stage. */
1197 static bool
1198 gate_whole_program_function_and_variable_visibility (void)
1200 return !flag_ltrans;
1203 /* Bring functionss local at LTO time with -fwhole-program. */
1205 static unsigned int
1206 whole_program_function_and_variable_visibility (void)
1208 function_and_variable_visibility (flag_whole_program);
1209 if (optimize)
1210 ipa_discover_readonly_nonaddressable_vars ();
1211 return 0;
1214 namespace {
1216 const pass_data pass_data_ipa_whole_program_visibility =
1218 IPA_PASS, /* type */
1219 "whole-program", /* name */
1220 OPTGROUP_NONE, /* optinfo_flags */
1221 true, /* has_gate */
1222 true, /* has_execute */
1223 TV_CGRAPHOPT, /* tv_id */
1224 0, /* properties_required */
1225 0, /* properties_provided */
1226 0, /* properties_destroyed */
1227 0, /* todo_flags_start */
1228 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1231 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
1233 public:
1234 pass_ipa_whole_program_visibility (gcc::context *ctxt)
1235 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
1236 NULL, /* generate_summary */
1237 NULL, /* write_summary */
1238 NULL, /* read_summary */
1239 NULL, /* write_optimization_summary */
1240 NULL, /* read_optimization_summary */
1241 NULL, /* stmt_fixup */
1242 0, /* function_transform_todo_flags_start */
1243 NULL, /* function_transform */
1244 NULL) /* variable_transform */
1247 /* opt_pass methods: */
1248 bool gate () {
1249 return gate_whole_program_function_and_variable_visibility ();
1251 unsigned int execute () {
1252 return whole_program_function_and_variable_visibility ();
1255 }; // class pass_ipa_whole_program_visibility
1257 } // anon namespace
1259 ipa_opt_pass_d *
1260 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
1262 return new pass_ipa_whole_program_visibility (ctxt);
1265 /* Generate and emit a static constructor or destructor. WHICH must
1266 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
1267 (for chp static vars constructor) or 'B' (for chkp static bounds
1268 constructor). BODY is a STATEMENT_LIST containing GENERIC
1269 statements. PRIORITY is the initialization priority for this
1270 constructor or destructor.
1272 FINAL specify whether the externally visible name for collect2 should
1273 be produced. */
1275 static void
1276 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
1278 static int counter = 0;
1279 char which_buf[16];
1280 tree decl, name, resdecl;
1282 /* The priority is encoded in the constructor or destructor name.
1283 collect2 will sort the names and arrange that they are called at
1284 program startup. */
1285 if (final)
1286 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1287 else
1288 /* Proudce sane name but one not recognizable by collect2, just for the
1289 case we fail to inline the function. */
1290 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
1291 name = get_file_function_name (which_buf);
1293 decl = build_decl (input_location, FUNCTION_DECL, name,
1294 build_function_type_list (void_type_node, NULL_TREE));
1295 current_function_decl = decl;
1297 resdecl = build_decl (input_location,
1298 RESULT_DECL, NULL_TREE, void_type_node);
1299 DECL_ARTIFICIAL (resdecl) = 1;
1300 DECL_RESULT (decl) = resdecl;
1301 DECL_CONTEXT (resdecl) = decl;
1303 allocate_struct_function (decl, false);
1305 TREE_STATIC (decl) = 1;
1306 TREE_USED (decl) = 1;
1307 DECL_ARTIFICIAL (decl) = 1;
1308 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1309 DECL_SAVED_TREE (decl) = body;
1310 if (!targetm.have_ctors_dtors && final)
1312 TREE_PUBLIC (decl) = 1;
1313 DECL_PRESERVE_P (decl) = 1;
1315 DECL_UNINLINABLE (decl) = 1;
1317 DECL_INITIAL (decl) = make_node (BLOCK);
1318 TREE_USED (DECL_INITIAL (decl)) = 1;
1320 DECL_SOURCE_LOCATION (decl) = input_location;
1321 cfun->function_end_locus = input_location;
1323 switch (which)
1325 case 'I':
1326 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1327 decl_init_priority_insert (decl, priority);
1328 break;
1329 case 'P':
1330 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1331 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("chkp ctor"),
1332 NULL,
1333 NULL_TREE);
1334 decl_init_priority_insert (decl, priority);
1335 break;
1336 case 'B':
1337 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1338 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("bnd_legacy"),
1339 NULL,
1340 NULL_TREE);
1341 decl_init_priority_insert (decl, priority);
1342 break;
1343 case 'D':
1344 DECL_STATIC_DESTRUCTOR (decl) = 1;
1345 decl_fini_priority_insert (decl, priority);
1346 break;
1347 default:
1348 gcc_unreachable ();
1351 gimplify_function_tree (decl);
1353 cgraph_add_new_function (decl, false);
1355 set_cfun (NULL);
1356 current_function_decl = NULL;
1359 /* Generate and emit a static constructor or destructor. WHICH must
1360 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
1361 (for chkp static vars constructor) or 'B' (for chkp static bounds
1362 constructor). BODY is a STATEMENT_LIST containing GENERIC
1363 statements. PRIORITY is the initialization priority for this
1364 constructor or destructor. */
1366 void
1367 cgraph_build_static_cdtor (char which, tree body, int priority)
1369 cgraph_build_static_cdtor_1 (which, body, priority, false);
1372 /* A vector of FUNCTION_DECLs declared as static constructors. */
1373 static vec<tree> static_ctors;
1374 /* A vector of FUNCTION_DECLs declared as static destructors. */
1375 static vec<tree> static_dtors;
1377 /* When target does not have ctors and dtors, we call all constructor
1378 and destructor by special initialization/destruction function
1379 recognized by collect2.
1381 When we are going to build this function, collect all constructors and
1382 destructors and turn them into normal functions. */
1384 static void
1385 record_cdtor_fn (struct cgraph_node *node)
1387 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1388 static_ctors.safe_push (node->decl);
1389 if (DECL_STATIC_DESTRUCTOR (node->decl))
1390 static_dtors.safe_push (node->decl);
1391 node = cgraph_get_node (node->decl);
1392 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
1395 /* Define global constructors/destructor functions for the CDTORS, of
1396 which they are LEN. The CDTORS are sorted by initialization
1397 priority. If CTOR_P is true, these are constructors; otherwise,
1398 they are destructors. */
1400 static void
1401 build_cdtor (bool ctor_p, vec<tree> cdtors)
1403 size_t i,j;
1404 size_t len = cdtors.length ();
1406 i = 0;
1407 while (i < len)
1409 tree body;
1410 tree fn;
1411 priority_type priority;
1413 priority = 0;
1414 body = NULL_TREE;
1415 j = i;
1418 priority_type p;
1419 fn = cdtors[j];
1420 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1421 if (j == i)
1422 priority = p;
1423 else if (p != priority)
1424 break;
1425 j++;
1427 while (j < len);
1429 /* When there is only one cdtor and target supports them, do nothing. */
1430 if (j == i + 1
1431 && targetm.have_ctors_dtors)
1433 i++;
1434 continue;
1436 /* Find the next batch of constructors/destructors with the same
1437 initialization priority. */
1438 for (;i < j; i++)
1440 tree call;
1441 fn = cdtors[i];
1442 call = build_call_expr (fn, 0);
1443 if (ctor_p)
1444 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1445 else
1446 DECL_STATIC_DESTRUCTOR (fn) = 0;
1447 /* We do not want to optimize away pure/const calls here.
1448 When optimizing, these should be already removed, when not
1449 optimizing, we want user to be able to breakpoint in them. */
1450 TREE_SIDE_EFFECTS (call) = 1;
1451 append_to_statement_list (call, &body);
1453 gcc_assert (body != NULL_TREE);
1454 /* Generate a function to call all the function of like
1455 priority. */
1456 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1460 /* Comparison function for qsort. P1 and P2 are actually of type
1461 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1462 used to determine the sort order. */
1464 static int
1465 compare_ctor (const void *p1, const void *p2)
1467 tree f1;
1468 tree f2;
1469 int priority1;
1470 int priority2;
1472 f1 = *(const tree *)p1;
1473 f2 = *(const tree *)p2;
1474 priority1 = DECL_INIT_PRIORITY (f1);
1475 priority2 = DECL_INIT_PRIORITY (f2);
1477 if (priority1 < priority2)
1478 return -1;
1479 else if (priority1 > priority2)
1480 return 1;
1481 else
1482 /* Ensure a stable sort. Constructors are executed in backwarding
1483 order to make LTO initialize braries first. */
1484 return DECL_UID (f2) - DECL_UID (f1);
1487 /* Comparison function for qsort. P1 and P2 are actually of type
1488 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1489 used to determine the sort order. */
1491 static int
1492 compare_dtor (const void *p1, const void *p2)
1494 tree f1;
1495 tree f2;
1496 int priority1;
1497 int priority2;
1499 f1 = *(const tree *)p1;
1500 f2 = *(const tree *)p2;
1501 priority1 = DECL_FINI_PRIORITY (f1);
1502 priority2 = DECL_FINI_PRIORITY (f2);
1504 if (priority1 < priority2)
1505 return -1;
1506 else if (priority1 > priority2)
1507 return 1;
1508 else
1509 /* Ensure a stable sort. */
1510 return DECL_UID (f1) - DECL_UID (f2);
1513 /* Generate functions to call static constructors and destructors
1514 for targets that do not support .ctors/.dtors sections. These
1515 functions have magic names which are detected by collect2. */
1517 static void
1518 build_cdtor_fns (void)
1520 if (!static_ctors.is_empty ())
1522 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1523 static_ctors.qsort (compare_ctor);
1524 build_cdtor (/*ctor_p=*/true, static_ctors);
1527 if (!static_dtors.is_empty ())
1529 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1530 static_dtors.qsort (compare_dtor);
1531 build_cdtor (/*ctor_p=*/false, static_dtors);
1535 /* Look for constructors and destructors and produce function calling them.
1536 This is needed for targets not supporting ctors or dtors, but we perform the
1537 transformation also at linktime to merge possibly numerous
1538 constructors/destructors into single function to improve code locality and
1539 reduce size. */
1541 static unsigned int
1542 ipa_cdtor_merge (void)
1544 struct cgraph_node *node;
1545 FOR_EACH_DEFINED_FUNCTION (node)
1546 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1547 || DECL_STATIC_DESTRUCTOR (node->decl))
1548 record_cdtor_fn (node);
1549 build_cdtor_fns ();
1550 static_ctors.release ();
1551 static_dtors.release ();
1552 return 0;
1555 /* Perform the pass when we have no ctors/dtors support
1556 or at LTO time to merge multiple constructors into single
1557 function. */
1559 static bool
1560 gate_ipa_cdtor_merge (void)
1562 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1565 namespace {
1567 const pass_data pass_data_ipa_cdtor_merge =
1569 IPA_PASS, /* type */
1570 "cdtor", /* name */
1571 OPTGROUP_NONE, /* optinfo_flags */
1572 true, /* has_gate */
1573 true, /* has_execute */
1574 TV_CGRAPHOPT, /* tv_id */
1575 0, /* properties_required */
1576 0, /* properties_provided */
1577 0, /* properties_destroyed */
1578 0, /* todo_flags_start */
1579 0, /* todo_flags_finish */
1582 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1584 public:
1585 pass_ipa_cdtor_merge (gcc::context *ctxt)
1586 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1587 NULL, /* generate_summary */
1588 NULL, /* write_summary */
1589 NULL, /* read_summary */
1590 NULL, /* write_optimization_summary */
1591 NULL, /* read_optimization_summary */
1592 NULL, /* stmt_fixup */
1593 0, /* function_transform_todo_flags_start */
1594 NULL, /* function_transform */
1595 NULL) /* variable_transform */
1598 /* opt_pass methods: */
1599 bool gate () { return gate_ipa_cdtor_merge (); }
1600 unsigned int execute () { return ipa_cdtor_merge (); }
1602 }; // class pass_ipa_cdtor_merge
1604 } // anon namespace
1606 ipa_opt_pass_d *
1607 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1609 return new pass_ipa_cdtor_merge (ctxt);