* config/msp430/msp430.md (split): Don't allow subregs when
[official-gcc.git] / gcc / ipa.c
blob9c88fe4dbbadaaae5ae2e9dfc00e0d68f215ba7d
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "calls.h"
26 #include "stringpool.h"
27 #include "cgraph.h"
28 #include "tree-pass.h"
29 #include "pointer-set.h"
30 #include "gimple-expr.h"
31 #include "gimplify.h"
32 #include "flags.h"
33 #include "target.h"
34 #include "tree-iterator.h"
35 #include "ipa-utils.h"
36 #include "ipa-inline.h"
37 #include "tree-inline.h"
38 #include "profile.h"
39 #include "params.h"
41 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
43 static bool
44 cgraph_non_local_node_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
46 /* FIXME: Aliases can be local, but i386 gets thunks wrong then. */
47 return !(cgraph_only_called_directly_or_aliased_p (node)
48 && !ipa_ref_has_aliases_p (&node->ref_list)
49 && node->definition
50 && !DECL_EXTERNAL (node->decl)
51 && !node->externally_visible
52 && !node->used_from_other_partition
53 && !node->in_other_partition);
56 /* Return true when function can be marked local. */
58 static bool
59 cgraph_local_node_p (struct cgraph_node *node)
61 struct cgraph_node *n = cgraph_function_or_thunk_node (node, NULL);
63 /* FIXME: thunks can be considered local, but we need prevent i386
64 from attempting to change calling convention of them. */
65 if (n->thunk.thunk_p)
66 return false;
67 return !cgraph_for_node_and_aliases (n,
68 cgraph_non_local_node_p_1, NULL, true);
72 /* Return true when NODE has ADDR reference. */
74 static bool
75 has_addr_references_p (struct cgraph_node *node,
76 void *data ATTRIBUTE_UNUSED)
78 int i;
79 struct ipa_ref *ref;
81 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
82 i, ref); i++)
83 if (ref->use == IPA_REF_ADDR)
84 return true;
85 return false;
88 /* Look for all functions inlined to NODE and update their inlined_to pointers
89 to INLINED_TO. */
91 static void
92 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
94 struct cgraph_edge *e;
95 for (e = node->callees; e; e = e->next_callee)
96 if (e->callee->global.inlined_to)
98 e->callee->global.inlined_to = inlined_to;
99 update_inlined_to_pointer (e->callee, inlined_to);
103 /* Add symtab NODE to queue starting at FIRST.
105 The queue is linked via AUX pointers and terminated by pointer to 1.
106 We enqueue nodes at two occasions: when we find them reachable or when we find
107 their bodies needed for further clonning. In the second case we mark them
108 by pointer to 2 after processing so they are re-queue when they become
109 reachable. */
111 static void
112 enqueue_node (symtab_node *node, symtab_node **first,
113 struct pointer_set_t *reachable)
115 /* Node is still in queue; do nothing. */
116 if (node->aux && node->aux != (void *) 2)
117 return;
118 /* Node was already processed as unreachable, re-enqueue
119 only if it became reachable now. */
120 if (node->aux == (void *)2 && !pointer_set_contains (reachable, node))
121 return;
122 node->aux = *first;
123 *first = node;
126 /* Process references. */
128 static void
129 process_references (struct ipa_ref_list *list,
130 symtab_node **first,
131 bool before_inlining_p,
132 struct pointer_set_t *reachable)
134 int i;
135 struct ipa_ref *ref;
136 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
138 symtab_node *node = ref->referred;
140 if (node->definition && !node->in_other_partition
141 && ((!DECL_EXTERNAL (node->decl) || node->alias)
142 || (((before_inlining_p
143 && (cgraph_state < CGRAPH_STATE_IPA_SSA
144 || !lookup_attribute ("always_inline",
145 DECL_ATTRIBUTES (node->decl)))))
146 /* We use variable constructors during late complation for
147 constant folding. Keep references alive so partitioning
148 knows about potential references. */
149 || (TREE_CODE (node->decl) == VAR_DECL
150 && flag_wpa
151 && ctor_for_folding (node->decl)
152 != error_mark_node))))
153 pointer_set_insert (reachable, node);
154 enqueue_node (node, first, reachable);
158 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
159 all its potential targets as reachable to permit later inlining if
160 devirtualization happens. After inlining still keep their declarations
161 around, so we can devirtualize to a direct call.
163 Also try to make trivial devirutalization when no or only one target is
164 possible. */
166 static void
167 walk_polymorphic_call_targets (pointer_set_t *reachable_call_targets,
168 struct cgraph_edge *edge,
169 symtab_node **first,
170 pointer_set_t *reachable, bool before_inlining_p)
172 unsigned int i;
173 void *cache_token;
174 bool final;
175 vec <cgraph_node *>targets
176 = possible_polymorphic_call_targets
177 (edge, &final, &cache_token);
179 if (!pointer_set_insert (reachable_call_targets,
180 cache_token))
182 for (i = 0; i < targets.length (); i++)
184 struct cgraph_node *n = targets[i];
186 /* Do not bother to mark virtual methods in anonymous namespace;
187 either we will find use of virtual table defining it, or it is
188 unused. */
189 if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
190 && type_in_anonymous_namespace_p
191 (method_class_type (TREE_TYPE (n->decl))))
192 continue;
194 /* Prior inlining, keep alive bodies of possible targets for
195 devirtualization. */
196 if (n->definition
197 && (before_inlining_p
198 && (cgraph_state < CGRAPH_STATE_IPA_SSA
199 || !lookup_attribute ("always_inline",
200 DECL_ATTRIBUTES (n->decl)))))
201 pointer_set_insert (reachable, n);
203 /* Even after inlining we want to keep the possible targets in the
204 boundary, so late passes can still produce direct call even if
205 the chance for inlining is lost. */
206 enqueue_node (n, first, reachable);
210 /* Very trivial devirtualization; when the type is
211 final or anonymous (so we know all its derivation)
212 and there is only one possible virtual call target,
213 make the edge direct. */
214 if (final)
216 if (targets.length () <= 1)
218 cgraph_node *target, *node = edge->caller;
219 if (targets.length () == 1)
220 target = targets[0];
221 else
222 target = cgraph_get_create_node
223 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
225 if (dump_file)
226 fprintf (dump_file,
227 "Devirtualizing call in %s/%i to %s/%i\n",
228 edge->caller->name (),
229 edge->caller->order,
230 target->name (), target->order);
231 edge = cgraph_make_edge_direct (edge, target);
232 if (inline_summary_vec)
233 inline_update_overall_summary (node);
234 else if (edge->call_stmt)
235 cgraph_redirect_edge_call_stmt_to_callee (edge);
240 /* Perform reachability analysis and reclaim all unreachable nodes.
242 The algorithm is basically mark&sweep but with some extra refinements:
244 - reachable extern inline functions needs special handling; the bodies needs
245 to stay in memory until inlining in hope that they will be inlined.
246 After inlining we release their bodies and turn them into unanalyzed
247 nodes even when they are reachable.
249 BEFORE_INLINING_P specify whether we are before or after inlining.
251 - virtual functions are kept in callgraph even if they seem unreachable in
252 hope calls to them will be devirtualized.
254 Again we remove them after inlining. In late optimization some
255 devirtualization may happen, but it is not important since we won't inline
256 the call. In theory early opts and IPA should work out all important cases.
258 - virtual clones needs bodies of their origins for later materialization;
259 this means that we want to keep the body even if the origin is unreachable
260 otherwise. To avoid origin from sitting in the callgraph and being
261 walked by IPA passes, we turn them into unanalyzed nodes with body
262 defined.
264 We maintain set of function declaration where body needs to stay in
265 body_needed_for_clonning
267 Inline clones represent special case: their declaration match the
268 declaration of origin and cgraph_remove_node already knows how to
269 reshape callgraph and preserve body when offline copy of function or
270 inline clone is being removed.
272 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
273 variables with DECL_INITIAL set. We finalize these and keep reachable
274 ones around for constant folding purposes. After inlining we however
275 stop walking their references to let everything static referneced by them
276 to be removed when it is otherwise unreachable.
278 We maintain queue of both reachable symbols (i.e. defined symbols that needs
279 to stay) and symbols that are in boundary (i.e. external symbols referenced
280 by reachable symbols or origins of clones). The queue is represented
281 as linked list by AUX pointer terminated by 1.
283 At the end we keep all reachable symbols. For symbols in boundary we always
284 turn definition into a declaration, but we may keep function body around
285 based on body_needed_for_clonning
287 All symbols that enter the queue have AUX pointer non-zero and are in the
288 boundary. Pointer set REACHABLE is used to track reachable symbols.
290 Every symbol can be visited twice - once as part of boundary and once
291 as real reachable symbol. enqueue_node needs to decide whether the
292 node needs to be re-queued for second processing. For this purpose
293 we set AUX pointer of processed symbols in the boundary to constant 2. */
295 bool
296 symtab_remove_unreachable_nodes (bool before_inlining_p, FILE *file)
298 symtab_node *first = (symtab_node *) (void *) 1;
299 struct cgraph_node *node, *next;
300 varpool_node *vnode, *vnext;
301 bool changed = false;
302 struct pointer_set_t *reachable = pointer_set_create ();
303 struct pointer_set_t *body_needed_for_clonning = pointer_set_create ();
304 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
306 timevar_push (TV_IPA_UNREACHABLE);
307 #ifdef ENABLE_CHECKING
308 verify_symtab ();
309 #endif
310 if (optimize && flag_devirtualize)
311 build_type_inheritance_graph ();
312 if (file)
313 fprintf (file, "\nReclaiming functions:");
314 #ifdef ENABLE_CHECKING
315 FOR_EACH_FUNCTION (node)
316 gcc_assert (!node->aux);
317 FOR_EACH_VARIABLE (vnode)
318 gcc_assert (!vnode->aux);
319 #endif
320 /* Mark functions whose bodies are obviously needed.
321 This is mostly when they can be referenced externally. Inline clones
322 are special since their declarations are shared with master clone and thus
323 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
324 FOR_EACH_FUNCTION (node)
326 node->used_as_abstract_origin = false;
327 if (node->definition
328 && !node->global.inlined_to
329 && !node->in_other_partition
330 && !cgraph_can_remove_if_no_direct_calls_and_refs_p (node))
332 gcc_assert (!node->global.inlined_to);
333 pointer_set_insert (reachable, node);
334 enqueue_node (node, &first, reachable);
336 else
337 gcc_assert (!node->aux);
340 /* Mark variables that are obviously needed. */
341 FOR_EACH_DEFINED_VARIABLE (vnode)
342 if (!varpool_can_remove_if_no_refs (vnode)
343 && !vnode->in_other_partition)
345 pointer_set_insert (reachable, vnode);
346 enqueue_node (vnode, &first, reachable);
349 /* Perform reachability analysis. */
350 while (first != (symtab_node *) (void *) 1)
352 bool in_boundary_p = !pointer_set_contains (reachable, first);
353 symtab_node *node = first;
355 first = (symtab_node *)first->aux;
357 /* If we are processing symbol in boundary, mark its AUX pointer for
358 possible later re-processing in enqueue_node. */
359 if (in_boundary_p)
360 node->aux = (void *)2;
361 else
363 if (TREE_CODE (node->decl) == FUNCTION_DECL
364 && DECL_ABSTRACT_ORIGIN (node->decl))
366 struct cgraph_node *origin_node
367 = cgraph_get_create_node (DECL_ABSTRACT_ORIGIN (node->decl));
368 origin_node->used_as_abstract_origin = true;
369 enqueue_node (origin_node, &first, reachable);
371 /* If any symbol in a comdat group is reachable, force
372 all externally visible symbols in the same comdat
373 group to be reachable as well. Comdat-local symbols
374 can be discarded if all uses were inlined. */
375 if (node->same_comdat_group)
377 symtab_node *next;
378 for (next = node->same_comdat_group;
379 next != node;
380 next = next->same_comdat_group)
381 if (!symtab_comdat_local_p (next)
382 && !pointer_set_insert (reachable, next))
383 enqueue_node (next, &first, reachable);
385 /* Mark references as reachable. */
386 process_references (&node->ref_list, &first,
387 before_inlining_p, reachable);
390 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
392 /* Mark the callees reachable unless they are direct calls to extern
393 inline functions we decided to not inline. */
394 if (!in_boundary_p)
396 struct cgraph_edge *e;
397 /* Keep alive possible targets for devirtualization. */
398 if (optimize && flag_devirtualize)
400 struct cgraph_edge *next;
401 for (e = cnode->indirect_calls; e; e = next)
403 next = e->next_callee;
404 if (e->indirect_info->polymorphic)
405 walk_polymorphic_call_targets (reachable_call_targets,
406 e, &first, reachable,
407 before_inlining_p);
410 for (e = cnode->callees; e; e = e->next_callee)
412 if (e->callee->definition
413 && !e->callee->in_other_partition
414 && (!e->inline_failed
415 || !DECL_EXTERNAL (e->callee->decl)
416 || e->callee->alias
417 || before_inlining_p))
419 /* Be sure that we will not optimize out alias target
420 body. */
421 if (DECL_EXTERNAL (e->callee->decl)
422 && e->callee->alias
423 && before_inlining_p)
425 pointer_set_insert (reachable,
426 cgraph_function_node (e->callee));
428 pointer_set_insert (reachable, e->callee);
430 enqueue_node (e->callee, &first, reachable);
433 /* When inline clone exists, mark body to be preserved so when removing
434 offline copy of the function we don't kill it. */
435 if (cnode->global.inlined_to)
436 pointer_set_insert (body_needed_for_clonning, cnode->decl);
438 /* For non-inline clones, force their origins to the boundary and ensure
439 that body is not removed. */
440 while (cnode->clone_of)
442 bool noninline = cnode->clone_of->decl != cnode->decl;
443 cnode = cnode->clone_of;
444 if (noninline)
446 pointer_set_insert (body_needed_for_clonning, cnode->decl);
447 enqueue_node (cnode, &first, reachable);
452 /* If any reachable function has simd clones, mark them as
453 reachable as well. */
454 if (cnode->simd_clones)
456 cgraph_node *next;
457 for (next = cnode->simd_clones;
458 next;
459 next = next->simdclone->next_clone)
460 if (in_boundary_p
461 || !pointer_set_insert (reachable, next))
462 enqueue_node (next, &first, reachable);
465 /* When we see constructor of external variable, keep referred nodes in the
466 boundary. This will also hold initializers of the external vars NODE
467 refers to. */
468 varpool_node *vnode = dyn_cast <varpool_node *> (node);
469 if (vnode
470 && DECL_EXTERNAL (node->decl)
471 && !vnode->alias
472 && in_boundary_p)
474 struct ipa_ref *ref;
475 for (int i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
476 enqueue_node (ref->referred, &first, reachable);
480 /* Remove unreachable functions. */
481 for (node = cgraph_first_function (); node; node = next)
483 next = cgraph_next_function (node);
485 /* If node is not needed at all, remove it. */
486 if (!node->aux)
488 if (file)
489 fprintf (file, " %s/%i", node->name (), node->order);
490 cgraph_remove_node (node);
491 changed = true;
493 /* If node is unreachable, remove its body. */
494 else if (!pointer_set_contains (reachable, node))
496 if (!pointer_set_contains (body_needed_for_clonning, node->decl))
497 cgraph_release_function_body (node);
498 else if (!node->clone_of)
499 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
500 if (node->definition)
502 if (file)
503 fprintf (file, " %s/%i", node->name (), node->order);
504 node->body_removed = true;
505 node->analyzed = false;
506 node->definition = false;
507 node->cpp_implicit_alias = false;
508 node->alias = false;
509 node->thunk.thunk_p = false;
510 node->weakref = false;
511 /* After early inlining we drop always_inline attributes on
512 bodies of functions that are still referenced (have their
513 address taken). */
514 DECL_ATTRIBUTES (node->decl)
515 = remove_attribute ("always_inline",
516 DECL_ATTRIBUTES (node->decl));
517 if (!node->in_other_partition)
518 node->local.local = false;
519 cgraph_node_remove_callees (node);
520 symtab_remove_from_same_comdat_group (node);
521 ipa_remove_all_references (&node->ref_list);
522 changed = true;
525 else
526 gcc_assert (node->clone_of || !cgraph_function_with_gimple_body_p (node)
527 || in_lto_p || DECL_RESULT (node->decl));
530 /* Inline clones might be kept around so their materializing allows further
531 cloning. If the function the clone is inlined into is removed, we need
532 to turn it into normal cone. */
533 FOR_EACH_FUNCTION (node)
535 if (node->global.inlined_to
536 && !node->callers)
538 gcc_assert (node->clones);
539 node->global.inlined_to = NULL;
540 update_inlined_to_pointer (node, node);
542 node->aux = NULL;
545 /* Remove unreachable variables. */
546 if (file)
547 fprintf (file, "\nReclaiming variables:");
548 for (vnode = varpool_first_variable (); vnode; vnode = vnext)
550 vnext = varpool_next_variable (vnode);
551 if (!vnode->aux
552 /* For can_refer_decl_in_current_unit_p we want to track for
553 all external variables if they are defined in other partition
554 or not. */
555 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
557 if (file)
558 fprintf (file, " %s/%i", vnode->name (), vnode->order);
559 varpool_remove_node (vnode);
560 changed = true;
562 else if (!pointer_set_contains (reachable, vnode))
564 tree init;
565 if (vnode->definition)
567 if (file)
568 fprintf (file, " %s", vnode->name ());
569 changed = true;
571 vnode->body_removed = true;
572 vnode->definition = false;
573 vnode->analyzed = false;
574 vnode->aux = NULL;
576 symtab_remove_from_same_comdat_group (vnode);
578 /* Keep body if it may be useful for constant folding. */
579 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node)
580 varpool_remove_initializer (vnode);
581 else
582 DECL_INITIAL (vnode->decl) = init;
583 ipa_remove_all_references (&vnode->ref_list);
585 else
586 vnode->aux = NULL;
589 pointer_set_destroy (reachable);
590 pointer_set_destroy (body_needed_for_clonning);
591 pointer_set_destroy (reachable_call_targets);
593 /* Now update address_taken flags and try to promote functions to be local. */
594 if (file)
595 fprintf (file, "\nClearing address taken flags:");
596 FOR_EACH_DEFINED_FUNCTION (node)
597 if (node->address_taken
598 && !node->used_from_other_partition)
600 if (!cgraph_for_node_and_aliases (node, has_addr_references_p, NULL, true))
602 if (file)
603 fprintf (file, " %s", node->name ());
604 node->address_taken = false;
605 changed = true;
606 if (cgraph_local_node_p (node))
608 node->local.local = true;
609 if (file)
610 fprintf (file, " (local)");
614 if (file)
615 fprintf (file, "\n");
617 #ifdef ENABLE_CHECKING
618 verify_symtab ();
619 #endif
621 /* If we removed something, perhaps profile could be improved. */
622 if (changed && optimize && inline_edge_summary_vec.exists ())
623 FOR_EACH_DEFINED_FUNCTION (node)
624 ipa_propagate_frequency (node);
626 timevar_pop (TV_IPA_UNREACHABLE);
627 return changed;
630 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
631 as needed, also clear EXPLICIT_REFS if the references to given variable
632 do not need to be explicit. */
634 void
635 process_references (varpool_node *vnode,
636 bool *written, bool *address_taken,
637 bool *read, bool *explicit_refs)
639 int i;
640 struct ipa_ref *ref;
642 if (!varpool_all_refs_explicit_p (vnode)
643 || TREE_THIS_VOLATILE (vnode->decl))
644 *explicit_refs = false;
646 for (i = 0; ipa_ref_list_referring_iterate (&vnode->ref_list,
647 i, ref)
648 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
649 switch (ref->use)
651 case IPA_REF_ADDR:
652 *address_taken = true;
653 break;
654 case IPA_REF_LOAD:
655 *read = true;
656 break;
657 case IPA_REF_STORE:
658 *written = true;
659 break;
660 case IPA_REF_ALIAS:
661 process_references (varpool (ref->referring), written, address_taken,
662 read, explicit_refs);
663 break;
667 /* Set TREE_READONLY bit. */
669 bool
670 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
672 TREE_READONLY (vnode->decl) = true;
673 return false;
676 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
678 bool
679 set_writeonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
681 vnode->writeonly = true;
682 if (optimize)
684 DECL_INITIAL (vnode->decl) = NULL;
685 if (!vnode->alias)
686 ipa_remove_all_references (&vnode->ref_list);
688 return false;
691 /* Clear addressale bit of VNODE. */
693 bool
694 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
696 vnode->address_taken = false;
697 TREE_ADDRESSABLE (vnode->decl) = 0;
698 return false;
701 /* Discover variables that have no longer address taken or that are read only
702 and update their flags.
704 FIXME: This can not be done in between gimplify and omp_expand since
705 readonly flag plays role on what is shared and what is not. Currently we do
706 this transformation as part of whole program visibility and re-do at
707 ipa-reference pass (to take into account clonning), but it would
708 make sense to do it before early optimizations. */
710 void
711 ipa_discover_readonly_nonaddressable_vars (void)
713 varpool_node *vnode;
714 if (dump_file)
715 fprintf (dump_file, "Clearing variable flags:");
716 FOR_EACH_VARIABLE (vnode)
717 if (!vnode->alias
718 && (TREE_ADDRESSABLE (vnode->decl)
719 || !vnode->writeonly
720 || !TREE_READONLY (vnode->decl)))
722 bool written = false;
723 bool address_taken = false;
724 bool read = false;
725 bool explicit_refs = true;
727 process_references (vnode, &written, &address_taken, &read, &explicit_refs);
728 if (!explicit_refs)
729 continue;
730 if (!address_taken)
732 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
733 fprintf (dump_file, " %s (addressable)", vnode->name ());
734 varpool_for_node_and_aliases (vnode, clear_addressable_bit, NULL, true);
736 if (!address_taken && !written
737 /* Making variable in explicit section readonly can cause section
738 type conflict.
739 See e.g. gcc.c-torture/compile/pr23237.c */
740 && DECL_SECTION_NAME (vnode->decl) == NULL)
742 if (!TREE_READONLY (vnode->decl) && dump_file)
743 fprintf (dump_file, " %s (read-only)", vnode->name ());
744 varpool_for_node_and_aliases (vnode, set_readonly_bit, NULL, true);
746 if (!vnode->writeonly && !read && !address_taken)
748 if (dump_file)
749 fprintf (dump_file, " %s (write-only)", vnode->name ());
750 varpool_for_node_and_aliases (vnode, set_writeonly_bit, NULL, true);
753 if (dump_file)
754 fprintf (dump_file, "\n");
757 /* Return true when there is a reference to node and it is not vtable. */
758 static bool
759 address_taken_from_non_vtable_p (symtab_node *node)
761 int i;
762 struct ipa_ref *ref;
763 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
764 i, ref); i++)
765 if (ref->use == IPA_REF_ADDR)
767 varpool_node *node;
768 if (is_a <cgraph_node *> (ref->referring))
769 return true;
770 node = ipa_ref_referring_varpool_node (ref);
771 if (!DECL_VIRTUAL_P (node->decl))
772 return true;
774 return false;
777 /* A helper for comdat_can_be_unshared_p. */
779 static bool
780 comdat_can_be_unshared_p_1 (symtab_node *node)
782 if (!node->externally_visible)
783 return true;
784 /* When address is taken, we don't know if equality comparison won't
785 break eventually. Exception are virutal functions, C++
786 constructors/destructors and vtables, where this is not possible by
787 language standard. */
788 if (!DECL_VIRTUAL_P (node->decl)
789 && (TREE_CODE (node->decl) != FUNCTION_DECL
790 || (!DECL_CXX_CONSTRUCTOR_P (node->decl)
791 && !DECL_CXX_DESTRUCTOR_P (node->decl)))
792 && address_taken_from_non_vtable_p (node))
793 return false;
795 /* If the symbol is used in some weird way, better to not touch it. */
796 if (node->force_output)
797 return false;
799 /* Explicit instantiations needs to be output when possibly
800 used externally. */
801 if (node->forced_by_abi
802 && TREE_PUBLIC (node->decl)
803 && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
804 && !flag_whole_program))
805 return false;
807 /* Non-readonly and volatile variables can not be duplicated. */
808 if (is_a <varpool_node *> (node)
809 && (!TREE_READONLY (node->decl)
810 || TREE_THIS_VOLATILE (node->decl)))
811 return false;
812 return true;
815 /* COMDAT functions must be shared only if they have address taken,
816 otherwise we can produce our own private implementation with
817 -fwhole-program.
818 Return true when turning COMDAT functoin static can not lead to wrong
819 code when the resulting object links with a library defining same COMDAT.
821 Virtual functions do have their addresses taken from the vtables,
822 but in C++ there is no way to compare their addresses for equality. */
824 static bool
825 comdat_can_be_unshared_p (symtab_node *node)
827 if (!comdat_can_be_unshared_p_1 (node))
828 return false;
829 if (node->same_comdat_group)
831 symtab_node *next;
833 /* If more than one function is in the same COMDAT group, it must
834 be shared even if just one function in the comdat group has
835 address taken. */
836 for (next = node->same_comdat_group;
837 next != node; next = next->same_comdat_group)
838 if (!comdat_can_be_unshared_p_1 (next))
839 return false;
841 return true;
844 /* Return true when function NODE should be considered externally visible. */
846 static bool
847 cgraph_externally_visible_p (struct cgraph_node *node,
848 bool whole_program)
850 if (!node->definition)
851 return false;
852 if (!TREE_PUBLIC (node->decl)
853 || DECL_EXTERNAL (node->decl))
854 return false;
856 /* Do not try to localize built-in functions yet. One of problems is that we
857 end up mangling their asm for WHOPR that makes it impossible to call them
858 using the implicit built-in declarations anymore. Similarly this enables
859 us to remove them as unreachable before actual calls may appear during
860 expansion or folding. */
861 if (DECL_BUILT_IN (node->decl))
862 return true;
864 /* If linker counts on us, we must preserve the function. */
865 if (symtab_used_from_object_file_p (node))
866 return true;
867 if (DECL_PRESERVE_P (node->decl))
868 return true;
869 if (lookup_attribute ("externally_visible",
870 DECL_ATTRIBUTES (node->decl)))
871 return true;
872 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
873 && lookup_attribute ("dllexport",
874 DECL_ATTRIBUTES (node->decl)))
875 return true;
876 if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
877 return false;
878 /* When doing LTO or whole program, we can bring COMDAT functoins static.
879 This improves code quality and we know we will duplicate them at most twice
880 (in the case that we are not using plugin and link with object file
881 implementing same COMDAT) */
882 if ((in_lto_p || whole_program)
883 && DECL_COMDAT (node->decl)
884 && comdat_can_be_unshared_p (node))
885 return false;
887 /* When doing link time optimizations, hidden symbols become local. */
888 if (in_lto_p
889 && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
890 || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
891 /* Be sure that node is defined in IR file, not in other object
892 file. In that case we don't set used_from_other_object_file. */
893 && node->definition)
895 else if (!whole_program)
896 return true;
898 if (MAIN_NAME_P (DECL_NAME (node->decl)))
899 return true;
901 return false;
904 /* Return true when variable VNODE should be considered externally visible. */
906 bool
907 varpool_externally_visible_p (varpool_node *vnode)
909 if (DECL_EXTERNAL (vnode->decl))
910 return true;
912 if (!TREE_PUBLIC (vnode->decl))
913 return false;
915 /* If linker counts on us, we must preserve the function. */
916 if (symtab_used_from_object_file_p (vnode))
917 return true;
919 if (DECL_HARD_REGISTER (vnode->decl))
920 return true;
921 if (DECL_PRESERVE_P (vnode->decl))
922 return true;
923 if (lookup_attribute ("externally_visible",
924 DECL_ATTRIBUTES (vnode->decl)))
925 return true;
926 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
927 && lookup_attribute ("dllexport",
928 DECL_ATTRIBUTES (vnode->decl)))
929 return true;
931 /* See if we have linker information about symbol not being used or
932 if we need to make guess based on the declaration.
934 Even if the linker clams the symbol is unused, never bring internal
935 symbols that are declared by user as used or externally visible.
936 This is needed for i.e. references from asm statements. */
937 if (symtab_used_from_object_file_p (vnode))
938 return true;
939 if (vnode->resolution == LDPR_PREVAILING_DEF_IRONLY)
940 return false;
942 /* As a special case, the COMDAT virtual tables can be unshared.
943 In LTO mode turn vtables into static variables. The variable is readonly,
944 so this does not enable more optimization, but referring static var
945 is faster for dynamic linking. Also this match logic hidding vtables
946 from LTO symbol tables. */
947 if ((in_lto_p || flag_whole_program)
948 && DECL_COMDAT (vnode->decl)
949 && comdat_can_be_unshared_p (vnode))
950 return false;
952 /* When doing link time optimizations, hidden symbols become local. */
953 if (in_lto_p
954 && (DECL_VISIBILITY (vnode->decl) == VISIBILITY_HIDDEN
955 || DECL_VISIBILITY (vnode->decl) == VISIBILITY_INTERNAL)
956 /* Be sure that node is defined in IR file, not in other object
957 file. In that case we don't set used_from_other_object_file. */
958 && vnode->definition)
960 else if (!flag_whole_program)
961 return true;
963 /* Do not attempt to privatize COMDATS by default.
964 This would break linking with C++ libraries sharing
965 inline definitions.
967 FIXME: We can do so for readonly vars with no address taken and
968 possibly also for vtables since no direct pointer comparsion is done.
969 It might be interesting to do so to reduce linking overhead. */
970 if (DECL_COMDAT (vnode->decl) || DECL_WEAK (vnode->decl))
971 return true;
972 return false;
975 /* Return true if reference to NODE can be replaced by a local alias.
976 Local aliases save dynamic linking overhead and enable more optimizations.
979 bool
980 can_replace_by_local_alias (symtab_node *node)
982 return (symtab_node_availability (node) > AVAIL_OVERWRITABLE
983 && !symtab_can_be_discarded (node));
986 /* In LTO we can remove COMDAT groups and weak symbols.
987 Either turn them into normal symbols or external symbol depending on
988 resolution info. */
990 static void
991 update_visibility_by_resolution_info (symtab_node * node)
993 bool define;
995 if (!node->externally_visible
996 || (!DECL_WEAK (node->decl) && !DECL_ONE_ONLY (node->decl))
997 || node->resolution == LDPR_UNKNOWN)
998 return;
1000 define = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
1001 || node->resolution == LDPR_PREVAILING_DEF
1002 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
1004 /* The linker decisions ought to agree in the whole group. */
1005 if (node->same_comdat_group)
1006 for (symtab_node *next = node->same_comdat_group;
1007 next != node; next = next->same_comdat_group)
1008 gcc_assert (!node->externally_visible
1009 || define == (next->resolution == LDPR_PREVAILING_DEF_IRONLY
1010 || next->resolution == LDPR_PREVAILING_DEF
1011 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP));
1013 if (node->same_comdat_group)
1014 for (symtab_node *next = node->same_comdat_group;
1015 next != node; next = next->same_comdat_group)
1017 DECL_COMDAT_GROUP (next->decl) = NULL;
1018 DECL_WEAK (next->decl) = false;
1019 if (next->externally_visible
1020 && !define)
1021 DECL_EXTERNAL (next->decl) = true;
1023 DECL_COMDAT_GROUP (node->decl) = NULL;
1024 DECL_WEAK (node->decl) = false;
1025 if (!define)
1026 DECL_EXTERNAL (node->decl) = true;
1027 symtab_dissolve_same_comdat_group_list (node);
1030 /* Mark visibility of all functions.
1032 A local function is one whose calls can occur only in the current
1033 compilation unit and all its calls are explicit, so we can change
1034 its calling convention. We simply mark all static functions whose
1035 address is not taken as local.
1037 We also change the TREE_PUBLIC flag of all declarations that are public
1038 in language point of view but we want to overwrite this default
1039 via visibilities for the backend point of view. */
1041 static unsigned int
1042 function_and_variable_visibility (bool whole_program)
1044 struct cgraph_node *node;
1045 varpool_node *vnode;
1047 /* All aliases should be procssed at this point. */
1048 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
1050 FOR_EACH_FUNCTION (node)
1052 int flags = flags_from_decl_or_type (node->decl);
1054 /* Optimize away PURE and CONST constructors and destructors. */
1055 if (optimize
1056 && (flags & (ECF_CONST | ECF_PURE))
1057 && !(flags & ECF_LOOPING_CONST_OR_PURE))
1059 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
1060 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
1063 /* Frontends and alias code marks nodes as needed before parsing is finished.
1064 We may end up marking as node external nodes where this flag is meaningless
1065 strip it. */
1066 if (DECL_EXTERNAL (node->decl) || !node->definition)
1068 node->force_output = 0;
1069 node->forced_by_abi = 0;
1072 /* C++ FE on lack of COMDAT support create local COMDAT functions
1073 (that ought to be shared but can not due to object format
1074 limitations). It is necessary to keep the flag to make rest of C++ FE
1075 happy. Clear the flag here to avoid confusion in middle-end. */
1076 if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
1077 DECL_COMDAT (node->decl) = 0;
1079 /* For external decls stop tracking same_comdat_group. It doesn't matter
1080 what comdat group they are in when they won't be emitted in this TU. */
1081 if (node->same_comdat_group && DECL_EXTERNAL (node->decl))
1083 #ifdef ENABLE_CHECKING
1084 symtab_node *n;
1086 for (n = node->same_comdat_group;
1087 n != node;
1088 n = n->same_comdat_group)
1089 /* If at least one of same comdat group functions is external,
1090 all of them have to be, otherwise it is a front-end bug. */
1091 gcc_assert (DECL_EXTERNAL (n->decl));
1092 #endif
1093 symtab_dissolve_same_comdat_group_list (node);
1095 gcc_assert ((!DECL_WEAK (node->decl)
1096 && !DECL_COMDAT (node->decl))
1097 || TREE_PUBLIC (node->decl)
1098 || node->weakref
1099 || DECL_EXTERNAL (node->decl));
1100 if (cgraph_externally_visible_p (node, whole_program))
1102 gcc_assert (!node->global.inlined_to);
1103 node->externally_visible = true;
1105 else
1107 node->externally_visible = false;
1108 node->forced_by_abi = false;
1110 if (!node->externally_visible
1111 && node->definition && !node->weakref
1112 && !DECL_EXTERNAL (node->decl))
1114 gcc_assert (whole_program || in_lto_p
1115 || !TREE_PUBLIC (node->decl));
1116 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
1117 || node->unique_name
1118 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
1119 && TREE_PUBLIC (node->decl));
1120 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
1121 if (node->same_comdat_group && TREE_PUBLIC (node->decl))
1123 symtab_node *next = node;
1125 /* Set all members of comdat group local. */
1126 if (node->same_comdat_group)
1127 for (next = node->same_comdat_group;
1128 next != node;
1129 next = next->same_comdat_group)
1131 symtab_make_decl_local (next->decl);
1132 next->unique_name = ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
1133 || next->unique_name
1134 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
1135 && TREE_PUBLIC (next->decl));
1137 /* cgraph_externally_visible_p has already checked all other nodes
1138 in the group and they will all be made local. We need to
1139 dissolve the group at once so that the predicate does not
1140 segfault though. */
1141 symtab_dissolve_same_comdat_group_list (node);
1143 symtab_make_decl_local (node->decl);
1146 if (node->thunk.thunk_p
1147 && TREE_PUBLIC (node->decl))
1149 struct cgraph_node *decl_node = node;
1151 decl_node = cgraph_function_node (decl_node->callees->callee, NULL);
1153 /* Thunks have the same visibility as function they are attached to.
1154 Make sure the C++ front end set this up properly. */
1155 if (DECL_ONE_ONLY (decl_node->decl))
1157 gcc_checking_assert (DECL_COMDAT (node->decl)
1158 == DECL_COMDAT (decl_node->decl));
1159 gcc_checking_assert (DECL_COMDAT_GROUP (node->decl)
1160 == DECL_COMDAT_GROUP (decl_node->decl));
1161 gcc_checking_assert (node->same_comdat_group);
1163 node->forced_by_abi = decl_node->forced_by_abi;
1164 if (DECL_EXTERNAL (decl_node->decl))
1165 DECL_EXTERNAL (node->decl) = 1;
1168 update_visibility_by_resolution_info (node);
1170 FOR_EACH_DEFINED_FUNCTION (node)
1172 node->local.local |= cgraph_local_node_p (node);
1174 /* If we know that function can not be overwritten by a different semantics
1175 and moreover its section can not be discarded, replace all direct calls
1176 by calls to an nonoverwritable alias. This make dynamic linking
1177 cheaper and enable more optimization.
1179 TODO: We can also update virtual tables. */
1180 if (node->callers && can_replace_by_local_alias (node))
1182 struct cgraph_node *alias = cgraph (symtab_nonoverwritable_alias (node));
1184 if (alias && alias != node)
1186 while (node->callers)
1188 struct cgraph_edge *e = node->callers;
1190 cgraph_redirect_edge_callee (e, alias);
1191 if (gimple_has_body_p (e->caller->decl))
1193 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1194 cgraph_redirect_edge_call_stmt_to_callee (e);
1195 pop_cfun ();
1201 FOR_EACH_VARIABLE (vnode)
1203 /* weak flag makes no sense on local variables. */
1204 gcc_assert (!DECL_WEAK (vnode->decl)
1205 || vnode->weakref
1206 || TREE_PUBLIC (vnode->decl)
1207 || DECL_EXTERNAL (vnode->decl));
1208 /* In several cases declarations can not be common:
1210 - when declaration has initializer
1211 - when it is in weak
1212 - when it has specific section
1213 - when it resides in non-generic address space.
1214 - if declaration is local, it will get into .local common section
1215 so common flag is not needed. Frontends still produce these in
1216 certain cases, such as for:
1218 static int a __attribute__ ((common))
1220 Canonicalize things here and clear the redundant flag. */
1221 if (DECL_COMMON (vnode->decl)
1222 && (!(TREE_PUBLIC (vnode->decl)
1223 || DECL_EXTERNAL (vnode->decl))
1224 || (DECL_INITIAL (vnode->decl)
1225 && DECL_INITIAL (vnode->decl) != error_mark_node)
1226 || DECL_WEAK (vnode->decl)
1227 || DECL_SECTION_NAME (vnode->decl) != NULL
1228 || ! (ADDR_SPACE_GENERIC_P
1229 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
1230 DECL_COMMON (vnode->decl) = 0;
1232 FOR_EACH_DEFINED_VARIABLE (vnode)
1234 if (!vnode->definition)
1235 continue;
1236 if (varpool_externally_visible_p (vnode))
1237 vnode->externally_visible = true;
1238 else
1240 vnode->externally_visible = false;
1241 vnode->forced_by_abi = false;
1243 if (!vnode->externally_visible
1244 && !vnode->weakref)
1246 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->decl));
1247 vnode->unique_name = ((vnode->resolution == LDPR_PREVAILING_DEF_IRONLY
1248 || vnode->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
1249 && TREE_PUBLIC (vnode->decl));
1250 symtab_make_decl_local (vnode->decl);
1251 if (vnode->same_comdat_group)
1252 symtab_dissolve_same_comdat_group_list (vnode);
1253 vnode->resolution = LDPR_PREVAILING_DEF_IRONLY;
1255 update_visibility_by_resolution_info (vnode);
1258 if (dump_file)
1260 fprintf (dump_file, "\nMarking local functions:");
1261 FOR_EACH_DEFINED_FUNCTION (node)
1262 if (node->local.local)
1263 fprintf (dump_file, " %s", node->name ());
1264 fprintf (dump_file, "\n\n");
1265 fprintf (dump_file, "\nMarking externally visible functions:");
1266 FOR_EACH_DEFINED_FUNCTION (node)
1267 if (node->externally_visible)
1268 fprintf (dump_file, " %s", node->name ());
1269 fprintf (dump_file, "\n\n");
1270 fprintf (dump_file, "\nMarking externally visible variables:");
1271 FOR_EACH_DEFINED_VARIABLE (vnode)
1272 if (vnode->externally_visible)
1273 fprintf (dump_file, " %s", vnode->name ());
1274 fprintf (dump_file, "\n\n");
1276 cgraph_function_flags_ready = true;
1277 return 0;
1280 /* Local function pass handling visibilities. This happens before LTO streaming
1281 so in particular -fwhole-program should be ignored at this level. */
1283 namespace {
1285 const pass_data pass_data_ipa_function_and_variable_visibility =
1287 SIMPLE_IPA_PASS, /* type */
1288 "visibility", /* name */
1289 OPTGROUP_NONE, /* optinfo_flags */
1290 true, /* has_execute */
1291 TV_CGRAPHOPT, /* tv_id */
1292 0, /* properties_required */
1293 0, /* properties_provided */
1294 0, /* properties_destroyed */
1295 0, /* todo_flags_start */
1296 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1299 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
1301 public:
1302 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1303 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
1304 ctxt)
1307 /* opt_pass methods: */
1308 virtual unsigned int execute (function *)
1310 return function_and_variable_visibility (flag_whole_program && !flag_lto);
1313 }; // class pass_ipa_function_and_variable_visibility
1315 } // anon namespace
1317 simple_ipa_opt_pass *
1318 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1320 return new pass_ipa_function_and_variable_visibility (ctxt);
1323 /* Free inline summary. */
1325 namespace {
1327 const pass_data pass_data_ipa_free_inline_summary =
1329 SIMPLE_IPA_PASS, /* type */
1330 "*free_inline_summary", /* name */
1331 OPTGROUP_NONE, /* optinfo_flags */
1332 true, /* has_execute */
1333 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
1334 0, /* properties_required */
1335 0, /* properties_provided */
1336 0, /* properties_destroyed */
1337 0, /* todo_flags_start */
1338 0, /* todo_flags_finish */
1341 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
1343 public:
1344 pass_ipa_free_inline_summary (gcc::context *ctxt)
1345 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
1348 /* opt_pass methods: */
1349 virtual unsigned int execute (function *)
1351 inline_free_summary ();
1352 return 0;
1355 }; // class pass_ipa_free_inline_summary
1357 } // anon namespace
1359 simple_ipa_opt_pass *
1360 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
1362 return new pass_ipa_free_inline_summary (ctxt);
1365 /* Bring functionss local at LTO time with -fwhole-program. */
1367 static unsigned int
1368 whole_program_function_and_variable_visibility (void)
1370 function_and_variable_visibility (flag_whole_program);
1371 if (optimize)
1372 ipa_discover_readonly_nonaddressable_vars ();
1373 return 0;
1376 namespace {
1378 const pass_data pass_data_ipa_whole_program_visibility =
1380 IPA_PASS, /* type */
1381 "whole-program", /* name */
1382 OPTGROUP_NONE, /* optinfo_flags */
1383 true, /* has_execute */
1384 TV_CGRAPHOPT, /* tv_id */
1385 0, /* properties_required */
1386 0, /* properties_provided */
1387 0, /* properties_destroyed */
1388 0, /* todo_flags_start */
1389 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1392 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
1394 public:
1395 pass_ipa_whole_program_visibility (gcc::context *ctxt)
1396 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
1397 NULL, /* generate_summary */
1398 NULL, /* write_summary */
1399 NULL, /* read_summary */
1400 NULL, /* write_optimization_summary */
1401 NULL, /* read_optimization_summary */
1402 NULL, /* stmt_fixup */
1403 0, /* function_transform_todo_flags_start */
1404 NULL, /* function_transform */
1405 NULL) /* variable_transform */
1408 /* opt_pass methods: */
1410 virtual bool gate (function *)
1412 /* Do not re-run on ltrans stage. */
1413 return !flag_ltrans;
1415 virtual unsigned int execute (function *)
1417 return whole_program_function_and_variable_visibility ();
1420 }; // class pass_ipa_whole_program_visibility
1422 } // anon namespace
1424 ipa_opt_pass_d *
1425 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
1427 return new pass_ipa_whole_program_visibility (ctxt);
1430 /* Generate and emit a static constructor or destructor. WHICH must
1431 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1432 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1433 initialization priority for this constructor or destructor.
1435 FINAL specify whether the externally visible name for collect2 should
1436 be produced. */
1438 static void
1439 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
1441 static int counter = 0;
1442 char which_buf[16];
1443 tree decl, name, resdecl;
1445 /* The priority is encoded in the constructor or destructor name.
1446 collect2 will sort the names and arrange that they are called at
1447 program startup. */
1448 if (final)
1449 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1450 else
1451 /* Proudce sane name but one not recognizable by collect2, just for the
1452 case we fail to inline the function. */
1453 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
1454 name = get_file_function_name (which_buf);
1456 decl = build_decl (input_location, FUNCTION_DECL, name,
1457 build_function_type_list (void_type_node, NULL_TREE));
1458 current_function_decl = decl;
1460 resdecl = build_decl (input_location,
1461 RESULT_DECL, NULL_TREE, void_type_node);
1462 DECL_ARTIFICIAL (resdecl) = 1;
1463 DECL_RESULT (decl) = resdecl;
1464 DECL_CONTEXT (resdecl) = decl;
1466 allocate_struct_function (decl, false);
1468 TREE_STATIC (decl) = 1;
1469 TREE_USED (decl) = 1;
1470 DECL_ARTIFICIAL (decl) = 1;
1471 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1472 DECL_SAVED_TREE (decl) = body;
1473 if (!targetm.have_ctors_dtors && final)
1475 TREE_PUBLIC (decl) = 1;
1476 DECL_PRESERVE_P (decl) = 1;
1478 DECL_UNINLINABLE (decl) = 1;
1480 DECL_INITIAL (decl) = make_node (BLOCK);
1481 TREE_USED (DECL_INITIAL (decl)) = 1;
1483 DECL_SOURCE_LOCATION (decl) = input_location;
1484 cfun->function_end_locus = input_location;
1486 switch (which)
1488 case 'I':
1489 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1490 decl_init_priority_insert (decl, priority);
1491 break;
1492 case 'D':
1493 DECL_STATIC_DESTRUCTOR (decl) = 1;
1494 decl_fini_priority_insert (decl, priority);
1495 break;
1496 default:
1497 gcc_unreachable ();
1500 gimplify_function_tree (decl);
1502 cgraph_add_new_function (decl, false);
1504 set_cfun (NULL);
1505 current_function_decl = NULL;
1508 /* Generate and emit a static constructor or destructor. WHICH must
1509 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1510 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1511 initialization priority for this constructor or destructor. */
1513 void
1514 cgraph_build_static_cdtor (char which, tree body, int priority)
1516 cgraph_build_static_cdtor_1 (which, body, priority, false);
1519 /* A vector of FUNCTION_DECLs declared as static constructors. */
1520 static vec<tree> static_ctors;
1521 /* A vector of FUNCTION_DECLs declared as static destructors. */
1522 static vec<tree> static_dtors;
1524 /* When target does not have ctors and dtors, we call all constructor
1525 and destructor by special initialization/destruction function
1526 recognized by collect2.
1528 When we are going to build this function, collect all constructors and
1529 destructors and turn them into normal functions. */
1531 static void
1532 record_cdtor_fn (struct cgraph_node *node)
1534 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1535 static_ctors.safe_push (node->decl);
1536 if (DECL_STATIC_DESTRUCTOR (node->decl))
1537 static_dtors.safe_push (node->decl);
1538 node = cgraph_get_node (node->decl);
1539 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
1542 /* Define global constructors/destructor functions for the CDTORS, of
1543 which they are LEN. The CDTORS are sorted by initialization
1544 priority. If CTOR_P is true, these are constructors; otherwise,
1545 they are destructors. */
1547 static void
1548 build_cdtor (bool ctor_p, vec<tree> cdtors)
1550 size_t i,j;
1551 size_t len = cdtors.length ();
1553 i = 0;
1554 while (i < len)
1556 tree body;
1557 tree fn;
1558 priority_type priority;
1560 priority = 0;
1561 body = NULL_TREE;
1562 j = i;
1565 priority_type p;
1566 fn = cdtors[j];
1567 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1568 if (j == i)
1569 priority = p;
1570 else if (p != priority)
1571 break;
1572 j++;
1574 while (j < len);
1576 /* When there is only one cdtor and target supports them, do nothing. */
1577 if (j == i + 1
1578 && targetm.have_ctors_dtors)
1580 i++;
1581 continue;
1583 /* Find the next batch of constructors/destructors with the same
1584 initialization priority. */
1585 for (;i < j; i++)
1587 tree call;
1588 fn = cdtors[i];
1589 call = build_call_expr (fn, 0);
1590 if (ctor_p)
1591 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1592 else
1593 DECL_STATIC_DESTRUCTOR (fn) = 0;
1594 /* We do not want to optimize away pure/const calls here.
1595 When optimizing, these should be already removed, when not
1596 optimizing, we want user to be able to breakpoint in them. */
1597 TREE_SIDE_EFFECTS (call) = 1;
1598 append_to_statement_list (call, &body);
1600 gcc_assert (body != NULL_TREE);
1601 /* Generate a function to call all the function of like
1602 priority. */
1603 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1607 /* Comparison function for qsort. P1 and P2 are actually of type
1608 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1609 used to determine the sort order. */
1611 static int
1612 compare_ctor (const void *p1, const void *p2)
1614 tree f1;
1615 tree f2;
1616 int priority1;
1617 int priority2;
1619 f1 = *(const tree *)p1;
1620 f2 = *(const tree *)p2;
1621 priority1 = DECL_INIT_PRIORITY (f1);
1622 priority2 = DECL_INIT_PRIORITY (f2);
1624 if (priority1 < priority2)
1625 return -1;
1626 else if (priority1 > priority2)
1627 return 1;
1628 else
1629 /* Ensure a stable sort. Constructors are executed in backwarding
1630 order to make LTO initialize braries first. */
1631 return DECL_UID (f2) - DECL_UID (f1);
1634 /* Comparison function for qsort. P1 and P2 are actually of type
1635 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1636 used to determine the sort order. */
1638 static int
1639 compare_dtor (const void *p1, const void *p2)
1641 tree f1;
1642 tree f2;
1643 int priority1;
1644 int priority2;
1646 f1 = *(const tree *)p1;
1647 f2 = *(const tree *)p2;
1648 priority1 = DECL_FINI_PRIORITY (f1);
1649 priority2 = DECL_FINI_PRIORITY (f2);
1651 if (priority1 < priority2)
1652 return -1;
1653 else if (priority1 > priority2)
1654 return 1;
1655 else
1656 /* Ensure a stable sort. */
1657 return DECL_UID (f1) - DECL_UID (f2);
1660 /* Generate functions to call static constructors and destructors
1661 for targets that do not support .ctors/.dtors sections. These
1662 functions have magic names which are detected by collect2. */
1664 static void
1665 build_cdtor_fns (void)
1667 if (!static_ctors.is_empty ())
1669 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1670 static_ctors.qsort (compare_ctor);
1671 build_cdtor (/*ctor_p=*/true, static_ctors);
1674 if (!static_dtors.is_empty ())
1676 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1677 static_dtors.qsort (compare_dtor);
1678 build_cdtor (/*ctor_p=*/false, static_dtors);
1682 /* Look for constructors and destructors and produce function calling them.
1683 This is needed for targets not supporting ctors or dtors, but we perform the
1684 transformation also at linktime to merge possibly numerous
1685 constructors/destructors into single function to improve code locality and
1686 reduce size. */
1688 static unsigned int
1689 ipa_cdtor_merge (void)
1691 struct cgraph_node *node;
1692 FOR_EACH_DEFINED_FUNCTION (node)
1693 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1694 || DECL_STATIC_DESTRUCTOR (node->decl))
1695 record_cdtor_fn (node);
1696 build_cdtor_fns ();
1697 static_ctors.release ();
1698 static_dtors.release ();
1699 return 0;
1702 namespace {
1704 const pass_data pass_data_ipa_cdtor_merge =
1706 IPA_PASS, /* type */
1707 "cdtor", /* name */
1708 OPTGROUP_NONE, /* optinfo_flags */
1709 true, /* has_execute */
1710 TV_CGRAPHOPT, /* tv_id */
1711 0, /* properties_required */
1712 0, /* properties_provided */
1713 0, /* properties_destroyed */
1714 0, /* todo_flags_start */
1715 0, /* todo_flags_finish */
1718 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1720 public:
1721 pass_ipa_cdtor_merge (gcc::context *ctxt)
1722 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1723 NULL, /* generate_summary */
1724 NULL, /* write_summary */
1725 NULL, /* read_summary */
1726 NULL, /* write_optimization_summary */
1727 NULL, /* read_optimization_summary */
1728 NULL, /* stmt_fixup */
1729 0, /* function_transform_todo_flags_start */
1730 NULL, /* function_transform */
1731 NULL) /* variable_transform */
1734 /* opt_pass methods: */
1735 virtual bool gate (function *);
1736 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1738 }; // class pass_ipa_cdtor_merge
1740 bool
1741 pass_ipa_cdtor_merge::gate (function *)
1743 /* Perform the pass when we have no ctors/dtors support
1744 or at LTO time to merge multiple constructors into single
1745 function. */
1746 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1749 } // anon namespace
1751 ipa_opt_pass_d *
1752 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1754 return new pass_ipa_cdtor_merge (ctxt);