add dbgcnt support for devirt
[official-gcc.git] / gcc / ipa.c
bloba4e9b2273301e3b655cfd6cbbf9ed21885d5a458
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"
40 #include "internal-fn.h"
41 #include "tree-ssa-alias.h"
42 #include "gimple.h"
43 #include "dbgcnt.h"
45 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
47 static bool
48 cgraph_non_local_node_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
50 /* FIXME: Aliases can be local, but i386 gets thunks wrong then. */
51 return !(cgraph_only_called_directly_or_aliased_p (node)
52 && !ipa_ref_has_aliases_p (&node->ref_list)
53 && node->definition
54 && !DECL_EXTERNAL (node->decl)
55 && !node->externally_visible
56 && !node->used_from_other_partition
57 && !node->in_other_partition);
60 /* Return true when function can be marked local. */
62 static bool
63 cgraph_local_node_p (struct cgraph_node *node)
65 struct cgraph_node *n = cgraph_function_or_thunk_node (node, NULL);
67 /* FIXME: thunks can be considered local, but we need prevent i386
68 from attempting to change calling convention of them. */
69 if (n->thunk.thunk_p)
70 return false;
71 return !cgraph_for_node_and_aliases (n,
72 cgraph_non_local_node_p_1, NULL, true);
76 /* Return true when NODE has ADDR reference. */
78 static bool
79 has_addr_references_p (struct cgraph_node *node,
80 void *data ATTRIBUTE_UNUSED)
82 int i;
83 struct ipa_ref *ref;
85 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
86 i, ref); i++)
87 if (ref->use == IPA_REF_ADDR)
88 return true;
89 return false;
92 /* Look for all functions inlined to NODE and update their inlined_to pointers
93 to INLINED_TO. */
95 static void
96 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
98 struct cgraph_edge *e;
99 for (e = node->callees; e; e = e->next_callee)
100 if (e->callee->global.inlined_to)
102 e->callee->global.inlined_to = inlined_to;
103 update_inlined_to_pointer (e->callee, inlined_to);
107 /* Add symtab NODE to queue starting at FIRST.
109 The queue is linked via AUX pointers and terminated by pointer to 1.
110 We enqueue nodes at two occasions: when we find them reachable or when we find
111 their bodies needed for further clonning. In the second case we mark them
112 by pointer to 2 after processing so they are re-queue when they become
113 reachable. */
115 static void
116 enqueue_node (symtab_node *node, symtab_node **first,
117 struct pointer_set_t *reachable)
119 /* Node is still in queue; do nothing. */
120 if (node->aux && node->aux != (void *) 2)
121 return;
122 /* Node was already processed as unreachable, re-enqueue
123 only if it became reachable now. */
124 if (node->aux == (void *)2 && !pointer_set_contains (reachable, node))
125 return;
126 node->aux = *first;
127 *first = node;
130 /* Process references. */
132 static void
133 process_references (struct ipa_ref_list *list,
134 symtab_node **first,
135 bool before_inlining_p,
136 struct pointer_set_t *reachable)
138 int i;
139 struct ipa_ref *ref;
140 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
142 symtab_node *node = ref->referred;
144 if (node->definition && !node->in_other_partition
145 && ((!DECL_EXTERNAL (node->decl) || node->alias)
146 || (((before_inlining_p
147 && (cgraph_state < CGRAPH_STATE_IPA_SSA
148 || !lookup_attribute ("always_inline",
149 DECL_ATTRIBUTES (node->decl)))))
150 /* We use variable constructors during late complation for
151 constant folding. Keep references alive so partitioning
152 knows about potential references. */
153 || (TREE_CODE (node->decl) == VAR_DECL
154 && flag_wpa
155 && ctor_for_folding (node->decl)
156 != error_mark_node))))
157 pointer_set_insert (reachable, node);
158 enqueue_node (node, first, reachable);
162 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
163 all its potential targets as reachable to permit later inlining if
164 devirtualization happens. After inlining still keep their declarations
165 around, so we can devirtualize to a direct call.
167 Also try to make trivial devirutalization when no or only one target is
168 possible. */
170 static void
171 walk_polymorphic_call_targets (pointer_set_t *reachable_call_targets,
172 struct cgraph_edge *edge,
173 symtab_node **first,
174 pointer_set_t *reachable, bool before_inlining_p)
176 unsigned int i;
177 void *cache_token;
178 bool final;
179 vec <cgraph_node *>targets
180 = possible_polymorphic_call_targets
181 (edge, &final, &cache_token);
183 if (!pointer_set_insert (reachable_call_targets,
184 cache_token))
186 for (i = 0; i < targets.length (); i++)
188 struct cgraph_node *n = targets[i];
190 /* Do not bother to mark virtual methods in anonymous namespace;
191 either we will find use of virtual table defining it, or it is
192 unused. */
193 if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
194 && type_in_anonymous_namespace_p
195 (method_class_type (TREE_TYPE (n->decl))))
196 continue;
198 /* Prior inlining, keep alive bodies of possible targets for
199 devirtualization. */
200 if (n->definition
201 && (before_inlining_p
202 && (cgraph_state < CGRAPH_STATE_IPA_SSA
203 || !lookup_attribute ("always_inline",
204 DECL_ATTRIBUTES (n->decl)))))
205 pointer_set_insert (reachable, n);
207 /* Even after inlining we want to keep the possible targets in the
208 boundary, so late passes can still produce direct call even if
209 the chance for inlining is lost. */
210 enqueue_node (n, first, reachable);
214 /* Very trivial devirtualization; when the type is
215 final or anonymous (so we know all its derivation)
216 and there is only one possible virtual call target,
217 make the edge direct. */
218 if (final)
220 if (targets.length () <= 1 && dbg_cnt (devirt))
222 cgraph_node *target, *node = edge->caller;
223 if (targets.length () == 1)
224 target = targets[0];
225 else
226 target = cgraph_get_create_node
227 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
229 if (dump_enabled_p ())
231 location_t locus = gimple_location (edge->call_stmt);
232 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
233 "devirtualizing call in %s/%i to %s/%i\n",
234 edge->caller->name (), edge->caller->order,
235 target->name (),
236 target->order);
238 edge = cgraph_make_edge_direct (edge, target);
239 if (inline_summary_vec)
240 inline_update_overall_summary (node);
241 else if (edge->call_stmt)
242 cgraph_redirect_edge_call_stmt_to_callee (edge);
247 /* Perform reachability analysis and reclaim all unreachable nodes.
249 The algorithm is basically mark&sweep but with some extra refinements:
251 - reachable extern inline functions needs special handling; the bodies needs
252 to stay in memory until inlining in hope that they will be inlined.
253 After inlining we release their bodies and turn them into unanalyzed
254 nodes even when they are reachable.
256 BEFORE_INLINING_P specify whether we are before or after inlining.
258 - virtual functions are kept in callgraph even if they seem unreachable in
259 hope calls to them will be devirtualized.
261 Again we remove them after inlining. In late optimization some
262 devirtualization may happen, but it is not important since we won't inline
263 the call. In theory early opts and IPA should work out all important cases.
265 - virtual clones needs bodies of their origins for later materialization;
266 this means that we want to keep the body even if the origin is unreachable
267 otherwise. To avoid origin from sitting in the callgraph and being
268 walked by IPA passes, we turn them into unanalyzed nodes with body
269 defined.
271 We maintain set of function declaration where body needs to stay in
272 body_needed_for_clonning
274 Inline clones represent special case: their declaration match the
275 declaration of origin and cgraph_remove_node already knows how to
276 reshape callgraph and preserve body when offline copy of function or
277 inline clone is being removed.
279 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
280 variables with DECL_INITIAL set. We finalize these and keep reachable
281 ones around for constant folding purposes. After inlining we however
282 stop walking their references to let everything static referneced by them
283 to be removed when it is otherwise unreachable.
285 We maintain queue of both reachable symbols (i.e. defined symbols that needs
286 to stay) and symbols that are in boundary (i.e. external symbols referenced
287 by reachable symbols or origins of clones). The queue is represented
288 as linked list by AUX pointer terminated by 1.
290 At the end we keep all reachable symbols. For symbols in boundary we always
291 turn definition into a declaration, but we may keep function body around
292 based on body_needed_for_clonning
294 All symbols that enter the queue have AUX pointer non-zero and are in the
295 boundary. Pointer set REACHABLE is used to track reachable symbols.
297 Every symbol can be visited twice - once as part of boundary and once
298 as real reachable symbol. enqueue_node needs to decide whether the
299 node needs to be re-queued for second processing. For this purpose
300 we set AUX pointer of processed symbols in the boundary to constant 2. */
302 bool
303 symtab_remove_unreachable_nodes (bool before_inlining_p, FILE *file)
305 symtab_node *first = (symtab_node *) (void *) 1;
306 struct cgraph_node *node, *next;
307 varpool_node *vnode, *vnext;
308 bool changed = false;
309 struct pointer_set_t *reachable = pointer_set_create ();
310 struct pointer_set_t *body_needed_for_clonning = pointer_set_create ();
311 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
313 timevar_push (TV_IPA_UNREACHABLE);
314 #ifdef ENABLE_CHECKING
315 verify_symtab ();
316 #endif
317 if (optimize && flag_devirtualize)
318 build_type_inheritance_graph ();
319 if (file)
320 fprintf (file, "\nReclaiming functions:");
321 #ifdef ENABLE_CHECKING
322 FOR_EACH_FUNCTION (node)
323 gcc_assert (!node->aux);
324 FOR_EACH_VARIABLE (vnode)
325 gcc_assert (!vnode->aux);
326 #endif
327 /* Mark functions whose bodies are obviously needed.
328 This is mostly when they can be referenced externally. Inline clones
329 are special since their declarations are shared with master clone and thus
330 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
331 FOR_EACH_FUNCTION (node)
333 node->used_as_abstract_origin = false;
334 if (node->definition
335 && !node->global.inlined_to
336 && !node->in_other_partition
337 && !cgraph_can_remove_if_no_direct_calls_and_refs_p (node))
339 gcc_assert (!node->global.inlined_to);
340 pointer_set_insert (reachable, node);
341 enqueue_node (node, &first, reachable);
343 else
344 gcc_assert (!node->aux);
347 /* Mark variables that are obviously needed. */
348 FOR_EACH_DEFINED_VARIABLE (vnode)
349 if (!varpool_can_remove_if_no_refs (vnode)
350 && !vnode->in_other_partition)
352 pointer_set_insert (reachable, vnode);
353 enqueue_node (vnode, &first, reachable);
356 /* Perform reachability analysis. */
357 while (first != (symtab_node *) (void *) 1)
359 bool in_boundary_p = !pointer_set_contains (reachable, first);
360 symtab_node *node = first;
362 first = (symtab_node *)first->aux;
364 /* If we are processing symbol in boundary, mark its AUX pointer for
365 possible later re-processing in enqueue_node. */
366 if (in_boundary_p)
367 node->aux = (void *)2;
368 else
370 if (TREE_CODE (node->decl) == FUNCTION_DECL
371 && DECL_ABSTRACT_ORIGIN (node->decl))
373 struct cgraph_node *origin_node
374 = cgraph_get_create_node (DECL_ABSTRACT_ORIGIN (node->decl));
375 origin_node->used_as_abstract_origin = true;
376 enqueue_node (origin_node, &first, reachable);
378 /* If any symbol in a comdat group is reachable, force
379 all externally visible symbols in the same comdat
380 group to be reachable as well. Comdat-local symbols
381 can be discarded if all uses were inlined. */
382 if (node->same_comdat_group)
384 symtab_node *next;
385 for (next = node->same_comdat_group;
386 next != node;
387 next = next->same_comdat_group)
388 if (!symtab_comdat_local_p (next)
389 && !pointer_set_insert (reachable, next))
390 enqueue_node (next, &first, reachable);
392 /* Mark references as reachable. */
393 process_references (&node->ref_list, &first,
394 before_inlining_p, reachable);
397 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
399 /* Mark the callees reachable unless they are direct calls to extern
400 inline functions we decided to not inline. */
401 if (!in_boundary_p)
403 struct cgraph_edge *e;
404 /* Keep alive possible targets for devirtualization. */
405 if (optimize && flag_devirtualize)
407 struct cgraph_edge *next;
408 for (e = cnode->indirect_calls; e; e = next)
410 next = e->next_callee;
411 if (e->indirect_info->polymorphic)
412 walk_polymorphic_call_targets (reachable_call_targets,
413 e, &first, reachable,
414 before_inlining_p);
417 for (e = cnode->callees; e; e = e->next_callee)
419 if (e->callee->definition
420 && !e->callee->in_other_partition
421 && (!e->inline_failed
422 || !DECL_EXTERNAL (e->callee->decl)
423 || e->callee->alias
424 || before_inlining_p))
426 /* Be sure that we will not optimize out alias target
427 body. */
428 if (DECL_EXTERNAL (e->callee->decl)
429 && e->callee->alias
430 && before_inlining_p)
432 pointer_set_insert (reachable,
433 cgraph_function_node (e->callee));
435 pointer_set_insert (reachable, e->callee);
437 enqueue_node (e->callee, &first, reachable);
440 /* When inline clone exists, mark body to be preserved so when removing
441 offline copy of the function we don't kill it. */
442 if (cnode->global.inlined_to)
443 pointer_set_insert (body_needed_for_clonning, cnode->decl);
445 /* For non-inline clones, force their origins to the boundary and ensure
446 that body is not removed. */
447 while (cnode->clone_of)
449 bool noninline = cnode->clone_of->decl != cnode->decl;
450 cnode = cnode->clone_of;
451 if (noninline)
453 pointer_set_insert (body_needed_for_clonning, cnode->decl);
454 enqueue_node (cnode, &first, reachable);
459 /* If any reachable function has simd clones, mark them as
460 reachable as well. */
461 if (cnode->simd_clones)
463 cgraph_node *next;
464 for (next = cnode->simd_clones;
465 next;
466 next = next->simdclone->next_clone)
467 if (in_boundary_p
468 || !pointer_set_insert (reachable, next))
469 enqueue_node (next, &first, reachable);
472 /* When we see constructor of external variable, keep referred nodes in the
473 boundary. This will also hold initializers of the external vars NODE
474 refers to. */
475 varpool_node *vnode = dyn_cast <varpool_node *> (node);
476 if (vnode
477 && DECL_EXTERNAL (node->decl)
478 && !vnode->alias
479 && in_boundary_p)
481 struct ipa_ref *ref;
482 for (int i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
483 enqueue_node (ref->referred, &first, reachable);
487 /* Remove unreachable functions. */
488 for (node = cgraph_first_function (); node; node = next)
490 next = cgraph_next_function (node);
492 /* If node is not needed at all, remove it. */
493 if (!node->aux)
495 if (file)
496 fprintf (file, " %s/%i", node->name (), node->order);
497 cgraph_remove_node (node);
498 changed = true;
500 /* If node is unreachable, remove its body. */
501 else if (!pointer_set_contains (reachable, node))
503 if (!pointer_set_contains (body_needed_for_clonning, node->decl))
504 cgraph_release_function_body (node);
505 else if (!node->clone_of)
506 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
507 if (node->definition)
509 if (file)
510 fprintf (file, " %s/%i", node->name (), node->order);
511 node->body_removed = true;
512 node->analyzed = false;
513 node->definition = false;
514 node->cpp_implicit_alias = false;
515 node->alias = false;
516 node->thunk.thunk_p = false;
517 node->weakref = false;
518 /* After early inlining we drop always_inline attributes on
519 bodies of functions that are still referenced (have their
520 address taken). */
521 DECL_ATTRIBUTES (node->decl)
522 = remove_attribute ("always_inline",
523 DECL_ATTRIBUTES (node->decl));
524 if (!node->in_other_partition)
525 node->local.local = false;
526 cgraph_node_remove_callees (node);
527 symtab_remove_from_same_comdat_group (node);
528 ipa_remove_all_references (&node->ref_list);
529 changed = true;
532 else
533 gcc_assert (node->clone_of || !cgraph_function_with_gimple_body_p (node)
534 || in_lto_p || DECL_RESULT (node->decl));
537 /* Inline clones might be kept around so their materializing allows further
538 cloning. If the function the clone is inlined into is removed, we need
539 to turn it into normal cone. */
540 FOR_EACH_FUNCTION (node)
542 if (node->global.inlined_to
543 && !node->callers)
545 gcc_assert (node->clones);
546 node->global.inlined_to = NULL;
547 update_inlined_to_pointer (node, node);
549 node->aux = NULL;
552 /* Remove unreachable variables. */
553 if (file)
554 fprintf (file, "\nReclaiming variables:");
555 for (vnode = varpool_first_variable (); vnode; vnode = vnext)
557 vnext = varpool_next_variable (vnode);
558 if (!vnode->aux
559 /* For can_refer_decl_in_current_unit_p we want to track for
560 all external variables if they are defined in other partition
561 or not. */
562 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
564 if (file)
565 fprintf (file, " %s/%i", vnode->name (), vnode->order);
566 varpool_remove_node (vnode);
567 changed = true;
569 else if (!pointer_set_contains (reachable, vnode))
571 tree init;
572 if (vnode->definition)
574 if (file)
575 fprintf (file, " %s", vnode->name ());
576 changed = true;
578 vnode->body_removed = true;
579 vnode->definition = false;
580 vnode->analyzed = false;
581 vnode->aux = NULL;
583 symtab_remove_from_same_comdat_group (vnode);
585 /* Keep body if it may be useful for constant folding. */
586 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node)
587 varpool_remove_initializer (vnode);
588 else
589 DECL_INITIAL (vnode->decl) = init;
590 ipa_remove_all_references (&vnode->ref_list);
592 else
593 vnode->aux = NULL;
596 pointer_set_destroy (reachable);
597 pointer_set_destroy (body_needed_for_clonning);
598 pointer_set_destroy (reachable_call_targets);
600 /* Now update address_taken flags and try to promote functions to be local. */
601 if (file)
602 fprintf (file, "\nClearing address taken flags:");
603 FOR_EACH_DEFINED_FUNCTION (node)
604 if (node->address_taken
605 && !node->used_from_other_partition)
607 if (!cgraph_for_node_and_aliases (node, has_addr_references_p, NULL, true))
609 if (file)
610 fprintf (file, " %s", node->name ());
611 node->address_taken = false;
612 changed = true;
613 if (cgraph_local_node_p (node))
615 node->local.local = true;
616 if (file)
617 fprintf (file, " (local)");
621 if (file)
622 fprintf (file, "\n");
624 #ifdef ENABLE_CHECKING
625 verify_symtab ();
626 #endif
628 /* If we removed something, perhaps profile could be improved. */
629 if (changed && optimize && inline_edge_summary_vec.exists ())
630 FOR_EACH_DEFINED_FUNCTION (node)
631 ipa_propagate_frequency (node);
633 timevar_pop (TV_IPA_UNREACHABLE);
634 return changed;
637 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
638 as needed, also clear EXPLICIT_REFS if the references to given variable
639 do not need to be explicit. */
641 void
642 process_references (varpool_node *vnode,
643 bool *written, bool *address_taken,
644 bool *read, bool *explicit_refs)
646 int i;
647 struct ipa_ref *ref;
649 if (!varpool_all_refs_explicit_p (vnode)
650 || TREE_THIS_VOLATILE (vnode->decl))
651 *explicit_refs = false;
653 for (i = 0; ipa_ref_list_referring_iterate (&vnode->ref_list,
654 i, ref)
655 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
656 switch (ref->use)
658 case IPA_REF_ADDR:
659 *address_taken = true;
660 break;
661 case IPA_REF_LOAD:
662 *read = true;
663 break;
664 case IPA_REF_STORE:
665 *written = true;
666 break;
667 case IPA_REF_ALIAS:
668 process_references (varpool (ref->referring), written, address_taken,
669 read, explicit_refs);
670 break;
674 /* Set TREE_READONLY bit. */
676 bool
677 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
679 TREE_READONLY (vnode->decl) = true;
680 return false;
683 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
685 bool
686 set_writeonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
688 vnode->writeonly = true;
689 if (optimize)
691 DECL_INITIAL (vnode->decl) = NULL;
692 if (!vnode->alias)
693 ipa_remove_all_references (&vnode->ref_list);
695 return false;
698 /* Clear addressale bit of VNODE. */
700 bool
701 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
703 vnode->address_taken = false;
704 TREE_ADDRESSABLE (vnode->decl) = 0;
705 return false;
708 /* Discover variables that have no longer address taken or that are read only
709 and update their flags.
711 FIXME: This can not be done in between gimplify and omp_expand since
712 readonly flag plays role on what is shared and what is not. Currently we do
713 this transformation as part of whole program visibility and re-do at
714 ipa-reference pass (to take into account clonning), but it would
715 make sense to do it before early optimizations. */
717 void
718 ipa_discover_readonly_nonaddressable_vars (void)
720 varpool_node *vnode;
721 if (dump_file)
722 fprintf (dump_file, "Clearing variable flags:");
723 FOR_EACH_VARIABLE (vnode)
724 if (!vnode->alias
725 && (TREE_ADDRESSABLE (vnode->decl)
726 || !vnode->writeonly
727 || !TREE_READONLY (vnode->decl)))
729 bool written = false;
730 bool address_taken = false;
731 bool read = false;
732 bool explicit_refs = true;
734 process_references (vnode, &written, &address_taken, &read, &explicit_refs);
735 if (!explicit_refs)
736 continue;
737 if (!address_taken)
739 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
740 fprintf (dump_file, " %s (addressable)", vnode->name ());
741 varpool_for_node_and_aliases (vnode, clear_addressable_bit, NULL, true);
743 if (!address_taken && !written
744 /* Making variable in explicit section readonly can cause section
745 type conflict.
746 See e.g. gcc.c-torture/compile/pr23237.c */
747 && DECL_SECTION_NAME (vnode->decl) == NULL)
749 if (!TREE_READONLY (vnode->decl) && dump_file)
750 fprintf (dump_file, " %s (read-only)", vnode->name ());
751 varpool_for_node_and_aliases (vnode, set_readonly_bit, NULL, true);
753 if (!vnode->writeonly && !read && !address_taken)
755 if (dump_file)
756 fprintf (dump_file, " %s (write-only)", vnode->name ());
757 varpool_for_node_and_aliases (vnode, set_writeonly_bit, NULL, true);
760 if (dump_file)
761 fprintf (dump_file, "\n");
764 /* Return true when there is a reference to node and it is not vtable. */
765 static bool
766 address_taken_from_non_vtable_p (symtab_node *node)
768 int i;
769 struct ipa_ref *ref;
770 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
771 i, ref); i++)
772 if (ref->use == IPA_REF_ADDR)
774 varpool_node *node;
775 if (is_a <cgraph_node *> (ref->referring))
776 return true;
777 node = ipa_ref_referring_varpool_node (ref);
778 if (!DECL_VIRTUAL_P (node->decl))
779 return true;
781 return false;
784 /* A helper for comdat_can_be_unshared_p. */
786 static bool
787 comdat_can_be_unshared_p_1 (symtab_node *node)
789 if (!node->externally_visible)
790 return true;
791 /* When address is taken, we don't know if equality comparison won't
792 break eventually. Exception are virutal functions, C++
793 constructors/destructors and vtables, where this is not possible by
794 language standard. */
795 if (!DECL_VIRTUAL_P (node->decl)
796 && (TREE_CODE (node->decl) != FUNCTION_DECL
797 || (!DECL_CXX_CONSTRUCTOR_P (node->decl)
798 && !DECL_CXX_DESTRUCTOR_P (node->decl)))
799 && address_taken_from_non_vtable_p (node))
800 return false;
802 /* If the symbol is used in some weird way, better to not touch it. */
803 if (node->force_output)
804 return false;
806 /* Explicit instantiations needs to be output when possibly
807 used externally. */
808 if (node->forced_by_abi
809 && TREE_PUBLIC (node->decl)
810 && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
811 && !flag_whole_program))
812 return false;
814 /* Non-readonly and volatile variables can not be duplicated. */
815 if (is_a <varpool_node *> (node)
816 && (!TREE_READONLY (node->decl)
817 || TREE_THIS_VOLATILE (node->decl)))
818 return false;
819 return true;
822 /* COMDAT functions must be shared only if they have address taken,
823 otherwise we can produce our own private implementation with
824 -fwhole-program.
825 Return true when turning COMDAT functoin static can not lead to wrong
826 code when the resulting object links with a library defining same COMDAT.
828 Virtual functions do have their addresses taken from the vtables,
829 but in C++ there is no way to compare their addresses for equality. */
831 static bool
832 comdat_can_be_unshared_p (symtab_node *node)
834 if (!comdat_can_be_unshared_p_1 (node))
835 return false;
836 if (node->same_comdat_group)
838 symtab_node *next;
840 /* If more than one function is in the same COMDAT group, it must
841 be shared even if just one function in the comdat group has
842 address taken. */
843 for (next = node->same_comdat_group;
844 next != node; next = next->same_comdat_group)
845 if (!comdat_can_be_unshared_p_1 (next))
846 return false;
848 return true;
851 /* Return true when function NODE should be considered externally visible. */
853 static bool
854 cgraph_externally_visible_p (struct cgraph_node *node,
855 bool whole_program)
857 if (!node->definition)
858 return false;
859 if (!TREE_PUBLIC (node->decl)
860 || DECL_EXTERNAL (node->decl))
861 return false;
863 /* Do not try to localize built-in functions yet. One of problems is that we
864 end up mangling their asm for WHOPR that makes it impossible to call them
865 using the implicit built-in declarations anymore. Similarly this enables
866 us to remove them as unreachable before actual calls may appear during
867 expansion or folding. */
868 if (DECL_BUILT_IN (node->decl))
869 return true;
871 /* If linker counts on us, we must preserve the function. */
872 if (symtab_used_from_object_file_p (node))
873 return true;
874 if (DECL_PRESERVE_P (node->decl))
875 return true;
876 if (lookup_attribute ("externally_visible",
877 DECL_ATTRIBUTES (node->decl)))
878 return true;
879 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
880 && lookup_attribute ("dllexport",
881 DECL_ATTRIBUTES (node->decl)))
882 return true;
883 if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
884 return false;
885 /* When doing LTO or whole program, we can bring COMDAT functoins static.
886 This improves code quality and we know we will duplicate them at most twice
887 (in the case that we are not using plugin and link with object file
888 implementing same COMDAT) */
889 if ((in_lto_p || whole_program)
890 && DECL_COMDAT (node->decl)
891 && comdat_can_be_unshared_p (node))
892 return false;
894 /* When doing link time optimizations, hidden symbols become local. */
895 if (in_lto_p
896 && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
897 || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
898 /* Be sure that node is defined in IR file, not in other object
899 file. In that case we don't set used_from_other_object_file. */
900 && node->definition)
902 else if (!whole_program)
903 return true;
905 if (MAIN_NAME_P (DECL_NAME (node->decl)))
906 return true;
908 return false;
911 /* Return true when variable VNODE should be considered externally visible. */
913 bool
914 varpool_externally_visible_p (varpool_node *vnode)
916 if (DECL_EXTERNAL (vnode->decl))
917 return true;
919 if (!TREE_PUBLIC (vnode->decl))
920 return false;
922 /* If linker counts on us, we must preserve the function. */
923 if (symtab_used_from_object_file_p (vnode))
924 return true;
926 if (DECL_HARD_REGISTER (vnode->decl))
927 return true;
928 if (DECL_PRESERVE_P (vnode->decl))
929 return true;
930 if (lookup_attribute ("externally_visible",
931 DECL_ATTRIBUTES (vnode->decl)))
932 return true;
933 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
934 && lookup_attribute ("dllexport",
935 DECL_ATTRIBUTES (vnode->decl)))
936 return true;
938 /* See if we have linker information about symbol not being used or
939 if we need to make guess based on the declaration.
941 Even if the linker clams the symbol is unused, never bring internal
942 symbols that are declared by user as used or externally visible.
943 This is needed for i.e. references from asm statements. */
944 if (symtab_used_from_object_file_p (vnode))
945 return true;
946 if (vnode->resolution == LDPR_PREVAILING_DEF_IRONLY)
947 return false;
949 /* As a special case, the COMDAT virtual tables can be unshared.
950 In LTO mode turn vtables into static variables. The variable is readonly,
951 so this does not enable more optimization, but referring static var
952 is faster for dynamic linking. Also this match logic hidding vtables
953 from LTO symbol tables. */
954 if ((in_lto_p || flag_whole_program)
955 && DECL_COMDAT (vnode->decl)
956 && comdat_can_be_unshared_p (vnode))
957 return false;
959 /* When doing link time optimizations, hidden symbols become local. */
960 if (in_lto_p
961 && (DECL_VISIBILITY (vnode->decl) == VISIBILITY_HIDDEN
962 || DECL_VISIBILITY (vnode->decl) == VISIBILITY_INTERNAL)
963 /* Be sure that node is defined in IR file, not in other object
964 file. In that case we don't set used_from_other_object_file. */
965 && vnode->definition)
967 else if (!flag_whole_program)
968 return true;
970 /* Do not attempt to privatize COMDATS by default.
971 This would break linking with C++ libraries sharing
972 inline definitions.
974 FIXME: We can do so for readonly vars with no address taken and
975 possibly also for vtables since no direct pointer comparsion is done.
976 It might be interesting to do so to reduce linking overhead. */
977 if (DECL_COMDAT (vnode->decl) || DECL_WEAK (vnode->decl))
978 return true;
979 return false;
982 /* Return true if reference to NODE can be replaced by a local alias.
983 Local aliases save dynamic linking overhead and enable more optimizations.
986 bool
987 can_replace_by_local_alias (symtab_node *node)
989 return (symtab_node_availability (node) > AVAIL_OVERWRITABLE
990 && !symtab_can_be_discarded (node));
993 /* In LTO we can remove COMDAT groups and weak symbols.
994 Either turn them into normal symbols or external symbol depending on
995 resolution info. */
997 static void
998 update_visibility_by_resolution_info (symtab_node * node)
1000 bool define;
1002 if (!node->externally_visible
1003 || (!DECL_WEAK (node->decl) && !DECL_ONE_ONLY (node->decl))
1004 || node->resolution == LDPR_UNKNOWN)
1005 return;
1007 define = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
1008 || node->resolution == LDPR_PREVAILING_DEF
1009 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
1011 /* The linker decisions ought to agree in the whole group. */
1012 if (node->same_comdat_group)
1013 for (symtab_node *next = node->same_comdat_group;
1014 next != node; next = next->same_comdat_group)
1015 gcc_assert (!node->externally_visible
1016 || define == (next->resolution == LDPR_PREVAILING_DEF_IRONLY
1017 || next->resolution == LDPR_PREVAILING_DEF
1018 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP));
1020 if (node->same_comdat_group)
1021 for (symtab_node *next = node->same_comdat_group;
1022 next != node; next = next->same_comdat_group)
1024 DECL_COMDAT_GROUP (next->decl) = NULL;
1025 DECL_WEAK (next->decl) = false;
1026 if (next->externally_visible
1027 && !define)
1028 DECL_EXTERNAL (next->decl) = true;
1030 DECL_COMDAT_GROUP (node->decl) = NULL;
1031 DECL_WEAK (node->decl) = false;
1032 if (!define)
1033 DECL_EXTERNAL (node->decl) = true;
1034 symtab_dissolve_same_comdat_group_list (node);
1037 /* Mark visibility of all functions.
1039 A local function is one whose calls can occur only in the current
1040 compilation unit and all its calls are explicit, so we can change
1041 its calling convention. We simply mark all static functions whose
1042 address is not taken as local.
1044 We also change the TREE_PUBLIC flag of all declarations that are public
1045 in language point of view but we want to overwrite this default
1046 via visibilities for the backend point of view. */
1048 static unsigned int
1049 function_and_variable_visibility (bool whole_program)
1051 struct cgraph_node *node;
1052 varpool_node *vnode;
1054 /* All aliases should be procssed at this point. */
1055 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
1057 FOR_EACH_FUNCTION (node)
1059 int flags = flags_from_decl_or_type (node->decl);
1061 /* Optimize away PURE and CONST constructors and destructors. */
1062 if (optimize
1063 && (flags & (ECF_CONST | ECF_PURE))
1064 && !(flags & ECF_LOOPING_CONST_OR_PURE))
1066 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
1067 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
1070 /* Frontends and alias code marks nodes as needed before parsing is finished.
1071 We may end up marking as node external nodes where this flag is meaningless
1072 strip it. */
1073 if (DECL_EXTERNAL (node->decl) || !node->definition)
1075 node->force_output = 0;
1076 node->forced_by_abi = 0;
1079 /* C++ FE on lack of COMDAT support create local COMDAT functions
1080 (that ought to be shared but can not due to object format
1081 limitations). It is necessary to keep the flag to make rest of C++ FE
1082 happy. Clear the flag here to avoid confusion in middle-end. */
1083 if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
1084 DECL_COMDAT (node->decl) = 0;
1086 /* For external decls stop tracking same_comdat_group. It doesn't matter
1087 what comdat group they are in when they won't be emitted in this TU. */
1088 if (node->same_comdat_group && DECL_EXTERNAL (node->decl))
1090 #ifdef ENABLE_CHECKING
1091 symtab_node *n;
1093 for (n = node->same_comdat_group;
1094 n != node;
1095 n = n->same_comdat_group)
1096 /* If at least one of same comdat group functions is external,
1097 all of them have to be, otherwise it is a front-end bug. */
1098 gcc_assert (DECL_EXTERNAL (n->decl));
1099 #endif
1100 symtab_dissolve_same_comdat_group_list (node);
1102 gcc_assert ((!DECL_WEAK (node->decl)
1103 && !DECL_COMDAT (node->decl))
1104 || TREE_PUBLIC (node->decl)
1105 || node->weakref
1106 || DECL_EXTERNAL (node->decl));
1107 if (cgraph_externally_visible_p (node, whole_program))
1109 gcc_assert (!node->global.inlined_to);
1110 node->externally_visible = true;
1112 else
1114 node->externally_visible = false;
1115 node->forced_by_abi = false;
1117 if (!node->externally_visible
1118 && node->definition && !node->weakref
1119 && !DECL_EXTERNAL (node->decl))
1121 gcc_assert (whole_program || in_lto_p
1122 || !TREE_PUBLIC (node->decl));
1123 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
1124 || node->unique_name
1125 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
1126 && TREE_PUBLIC (node->decl));
1127 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
1128 if (node->same_comdat_group && TREE_PUBLIC (node->decl))
1130 symtab_node *next = node;
1132 /* Set all members of comdat group local. */
1133 if (node->same_comdat_group)
1134 for (next = node->same_comdat_group;
1135 next != node;
1136 next = next->same_comdat_group)
1138 symtab_make_decl_local (next->decl);
1139 next->unique_name = ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
1140 || next->unique_name
1141 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
1142 && TREE_PUBLIC (next->decl));
1144 /* cgraph_externally_visible_p has already checked all other nodes
1145 in the group and they will all be made local. We need to
1146 dissolve the group at once so that the predicate does not
1147 segfault though. */
1148 symtab_dissolve_same_comdat_group_list (node);
1150 symtab_make_decl_local (node->decl);
1153 if (node->thunk.thunk_p
1154 && TREE_PUBLIC (node->decl))
1156 struct cgraph_node *decl_node = node;
1158 decl_node = cgraph_function_node (decl_node->callees->callee, NULL);
1160 /* Thunks have the same visibility as function they are attached to.
1161 Make sure the C++ front end set this up properly. */
1162 if (DECL_ONE_ONLY (decl_node->decl))
1164 gcc_checking_assert (DECL_COMDAT (node->decl)
1165 == DECL_COMDAT (decl_node->decl));
1166 gcc_checking_assert (DECL_COMDAT_GROUP (node->decl)
1167 == DECL_COMDAT_GROUP (decl_node->decl));
1168 gcc_checking_assert (node->same_comdat_group);
1170 node->forced_by_abi = decl_node->forced_by_abi;
1171 if (DECL_EXTERNAL (decl_node->decl))
1172 DECL_EXTERNAL (node->decl) = 1;
1175 update_visibility_by_resolution_info (node);
1177 FOR_EACH_DEFINED_FUNCTION (node)
1179 node->local.local |= cgraph_local_node_p (node);
1181 /* If we know that function can not be overwritten by a different semantics
1182 and moreover its section can not be discarded, replace all direct calls
1183 by calls to an nonoverwritable alias. This make dynamic linking
1184 cheaper and enable more optimization.
1186 TODO: We can also update virtual tables. */
1187 if (node->callers && can_replace_by_local_alias (node))
1189 struct cgraph_node *alias = cgraph (symtab_nonoverwritable_alias (node));
1191 if (alias && alias != node)
1193 while (node->callers)
1195 struct cgraph_edge *e = node->callers;
1197 cgraph_redirect_edge_callee (e, alias);
1198 if (gimple_has_body_p (e->caller->decl))
1200 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1201 cgraph_redirect_edge_call_stmt_to_callee (e);
1202 pop_cfun ();
1208 FOR_EACH_VARIABLE (vnode)
1210 /* weak flag makes no sense on local variables. */
1211 gcc_assert (!DECL_WEAK (vnode->decl)
1212 || vnode->weakref
1213 || TREE_PUBLIC (vnode->decl)
1214 || DECL_EXTERNAL (vnode->decl));
1215 /* In several cases declarations can not be common:
1217 - when declaration has initializer
1218 - when it is in weak
1219 - when it has specific section
1220 - when it resides in non-generic address space.
1221 - if declaration is local, it will get into .local common section
1222 so common flag is not needed. Frontends still produce these in
1223 certain cases, such as for:
1225 static int a __attribute__ ((common))
1227 Canonicalize things here and clear the redundant flag. */
1228 if (DECL_COMMON (vnode->decl)
1229 && (!(TREE_PUBLIC (vnode->decl)
1230 || DECL_EXTERNAL (vnode->decl))
1231 || (DECL_INITIAL (vnode->decl)
1232 && DECL_INITIAL (vnode->decl) != error_mark_node)
1233 || DECL_WEAK (vnode->decl)
1234 || DECL_SECTION_NAME (vnode->decl) != NULL
1235 || ! (ADDR_SPACE_GENERIC_P
1236 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
1237 DECL_COMMON (vnode->decl) = 0;
1239 FOR_EACH_DEFINED_VARIABLE (vnode)
1241 if (!vnode->definition)
1242 continue;
1243 if (varpool_externally_visible_p (vnode))
1244 vnode->externally_visible = true;
1245 else
1247 vnode->externally_visible = false;
1248 vnode->forced_by_abi = false;
1250 if (!vnode->externally_visible
1251 && !vnode->weakref)
1253 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->decl));
1254 vnode->unique_name = ((vnode->resolution == LDPR_PREVAILING_DEF_IRONLY
1255 || vnode->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
1256 && TREE_PUBLIC (vnode->decl));
1257 symtab_make_decl_local (vnode->decl);
1258 if (vnode->same_comdat_group)
1259 symtab_dissolve_same_comdat_group_list (vnode);
1260 vnode->resolution = LDPR_PREVAILING_DEF_IRONLY;
1262 update_visibility_by_resolution_info (vnode);
1265 if (dump_file)
1267 fprintf (dump_file, "\nMarking local functions:");
1268 FOR_EACH_DEFINED_FUNCTION (node)
1269 if (node->local.local)
1270 fprintf (dump_file, " %s", node->name ());
1271 fprintf (dump_file, "\n\n");
1272 fprintf (dump_file, "\nMarking externally visible functions:");
1273 FOR_EACH_DEFINED_FUNCTION (node)
1274 if (node->externally_visible)
1275 fprintf (dump_file, " %s", node->name ());
1276 fprintf (dump_file, "\n\n");
1277 fprintf (dump_file, "\nMarking externally visible variables:");
1278 FOR_EACH_DEFINED_VARIABLE (vnode)
1279 if (vnode->externally_visible)
1280 fprintf (dump_file, " %s", vnode->name ());
1281 fprintf (dump_file, "\n\n");
1283 cgraph_function_flags_ready = true;
1284 return 0;
1287 /* Local function pass handling visibilities. This happens before LTO streaming
1288 so in particular -fwhole-program should be ignored at this level. */
1290 namespace {
1292 const pass_data pass_data_ipa_function_and_variable_visibility =
1294 SIMPLE_IPA_PASS, /* type */
1295 "visibility", /* name */
1296 OPTGROUP_NONE, /* optinfo_flags */
1297 true, /* has_execute */
1298 TV_CGRAPHOPT, /* tv_id */
1299 0, /* properties_required */
1300 0, /* properties_provided */
1301 0, /* properties_destroyed */
1302 0, /* todo_flags_start */
1303 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1306 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
1308 public:
1309 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1310 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
1311 ctxt)
1314 /* opt_pass methods: */
1315 virtual unsigned int execute (function *)
1317 return function_and_variable_visibility (flag_whole_program && !flag_lto);
1320 }; // class pass_ipa_function_and_variable_visibility
1322 } // anon namespace
1324 simple_ipa_opt_pass *
1325 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1327 return new pass_ipa_function_and_variable_visibility (ctxt);
1330 /* Free inline summary. */
1332 namespace {
1334 const pass_data pass_data_ipa_free_inline_summary =
1336 SIMPLE_IPA_PASS, /* type */
1337 "*free_inline_summary", /* name */
1338 OPTGROUP_NONE, /* optinfo_flags */
1339 true, /* has_execute */
1340 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
1341 0, /* properties_required */
1342 0, /* properties_provided */
1343 0, /* properties_destroyed */
1344 0, /* todo_flags_start */
1345 0, /* todo_flags_finish */
1348 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
1350 public:
1351 pass_ipa_free_inline_summary (gcc::context *ctxt)
1352 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
1355 /* opt_pass methods: */
1356 virtual unsigned int execute (function *)
1358 inline_free_summary ();
1359 return 0;
1362 }; // class pass_ipa_free_inline_summary
1364 } // anon namespace
1366 simple_ipa_opt_pass *
1367 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
1369 return new pass_ipa_free_inline_summary (ctxt);
1372 /* Bring functionss local at LTO time with -fwhole-program. */
1374 static unsigned int
1375 whole_program_function_and_variable_visibility (void)
1377 function_and_variable_visibility (flag_whole_program);
1378 if (optimize)
1379 ipa_discover_readonly_nonaddressable_vars ();
1380 return 0;
1383 namespace {
1385 const pass_data pass_data_ipa_whole_program_visibility =
1387 IPA_PASS, /* type */
1388 "whole-program", /* name */
1389 OPTGROUP_NONE, /* optinfo_flags */
1390 true, /* has_execute */
1391 TV_CGRAPHOPT, /* tv_id */
1392 0, /* properties_required */
1393 0, /* properties_provided */
1394 0, /* properties_destroyed */
1395 0, /* todo_flags_start */
1396 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1399 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
1401 public:
1402 pass_ipa_whole_program_visibility (gcc::context *ctxt)
1403 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
1404 NULL, /* generate_summary */
1405 NULL, /* write_summary */
1406 NULL, /* read_summary */
1407 NULL, /* write_optimization_summary */
1408 NULL, /* read_optimization_summary */
1409 NULL, /* stmt_fixup */
1410 0, /* function_transform_todo_flags_start */
1411 NULL, /* function_transform */
1412 NULL) /* variable_transform */
1415 /* opt_pass methods: */
1417 virtual bool gate (function *)
1419 /* Do not re-run on ltrans stage. */
1420 return !flag_ltrans;
1422 virtual unsigned int execute (function *)
1424 return whole_program_function_and_variable_visibility ();
1427 }; // class pass_ipa_whole_program_visibility
1429 } // anon namespace
1431 ipa_opt_pass_d *
1432 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
1434 return new pass_ipa_whole_program_visibility (ctxt);
1437 /* Generate and emit a static constructor or destructor. WHICH must
1438 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1439 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1440 initialization priority for this constructor or destructor.
1442 FINAL specify whether the externally visible name for collect2 should
1443 be produced. */
1445 static void
1446 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
1448 static int counter = 0;
1449 char which_buf[16];
1450 tree decl, name, resdecl;
1452 /* The priority is encoded in the constructor or destructor name.
1453 collect2 will sort the names and arrange that they are called at
1454 program startup. */
1455 if (final)
1456 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1457 else
1458 /* Proudce sane name but one not recognizable by collect2, just for the
1459 case we fail to inline the function. */
1460 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
1461 name = get_file_function_name (which_buf);
1463 decl = build_decl (input_location, FUNCTION_DECL, name,
1464 build_function_type_list (void_type_node, NULL_TREE));
1465 current_function_decl = decl;
1467 resdecl = build_decl (input_location,
1468 RESULT_DECL, NULL_TREE, void_type_node);
1469 DECL_ARTIFICIAL (resdecl) = 1;
1470 DECL_RESULT (decl) = resdecl;
1471 DECL_CONTEXT (resdecl) = decl;
1473 allocate_struct_function (decl, false);
1475 TREE_STATIC (decl) = 1;
1476 TREE_USED (decl) = 1;
1477 DECL_ARTIFICIAL (decl) = 1;
1478 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1479 DECL_SAVED_TREE (decl) = body;
1480 if (!targetm.have_ctors_dtors && final)
1482 TREE_PUBLIC (decl) = 1;
1483 DECL_PRESERVE_P (decl) = 1;
1485 DECL_UNINLINABLE (decl) = 1;
1487 DECL_INITIAL (decl) = make_node (BLOCK);
1488 TREE_USED (DECL_INITIAL (decl)) = 1;
1490 DECL_SOURCE_LOCATION (decl) = input_location;
1491 cfun->function_end_locus = input_location;
1493 switch (which)
1495 case 'I':
1496 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1497 decl_init_priority_insert (decl, priority);
1498 break;
1499 case 'D':
1500 DECL_STATIC_DESTRUCTOR (decl) = 1;
1501 decl_fini_priority_insert (decl, priority);
1502 break;
1503 default:
1504 gcc_unreachable ();
1507 gimplify_function_tree (decl);
1509 cgraph_add_new_function (decl, false);
1511 set_cfun (NULL);
1512 current_function_decl = NULL;
1515 /* Generate and emit a static constructor or destructor. WHICH must
1516 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1517 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1518 initialization priority for this constructor or destructor. */
1520 void
1521 cgraph_build_static_cdtor (char which, tree body, int priority)
1523 cgraph_build_static_cdtor_1 (which, body, priority, false);
1526 /* A vector of FUNCTION_DECLs declared as static constructors. */
1527 static vec<tree> static_ctors;
1528 /* A vector of FUNCTION_DECLs declared as static destructors. */
1529 static vec<tree> static_dtors;
1531 /* When target does not have ctors and dtors, we call all constructor
1532 and destructor by special initialization/destruction function
1533 recognized by collect2.
1535 When we are going to build this function, collect all constructors and
1536 destructors and turn them into normal functions. */
1538 static void
1539 record_cdtor_fn (struct cgraph_node *node)
1541 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1542 static_ctors.safe_push (node->decl);
1543 if (DECL_STATIC_DESTRUCTOR (node->decl))
1544 static_dtors.safe_push (node->decl);
1545 node = cgraph_get_node (node->decl);
1546 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
1549 /* Define global constructors/destructor functions for the CDTORS, of
1550 which they are LEN. The CDTORS are sorted by initialization
1551 priority. If CTOR_P is true, these are constructors; otherwise,
1552 they are destructors. */
1554 static void
1555 build_cdtor (bool ctor_p, vec<tree> cdtors)
1557 size_t i,j;
1558 size_t len = cdtors.length ();
1560 i = 0;
1561 while (i < len)
1563 tree body;
1564 tree fn;
1565 priority_type priority;
1567 priority = 0;
1568 body = NULL_TREE;
1569 j = i;
1572 priority_type p;
1573 fn = cdtors[j];
1574 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1575 if (j == i)
1576 priority = p;
1577 else if (p != priority)
1578 break;
1579 j++;
1581 while (j < len);
1583 /* When there is only one cdtor and target supports them, do nothing. */
1584 if (j == i + 1
1585 && targetm.have_ctors_dtors)
1587 i++;
1588 continue;
1590 /* Find the next batch of constructors/destructors with the same
1591 initialization priority. */
1592 for (;i < j; i++)
1594 tree call;
1595 fn = cdtors[i];
1596 call = build_call_expr (fn, 0);
1597 if (ctor_p)
1598 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1599 else
1600 DECL_STATIC_DESTRUCTOR (fn) = 0;
1601 /* We do not want to optimize away pure/const calls here.
1602 When optimizing, these should be already removed, when not
1603 optimizing, we want user to be able to breakpoint in them. */
1604 TREE_SIDE_EFFECTS (call) = 1;
1605 append_to_statement_list (call, &body);
1607 gcc_assert (body != NULL_TREE);
1608 /* Generate a function to call all the function of like
1609 priority. */
1610 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1614 /* Comparison function for qsort. P1 and P2 are actually of type
1615 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1616 used to determine the sort order. */
1618 static int
1619 compare_ctor (const void *p1, const void *p2)
1621 tree f1;
1622 tree f2;
1623 int priority1;
1624 int priority2;
1626 f1 = *(const tree *)p1;
1627 f2 = *(const tree *)p2;
1628 priority1 = DECL_INIT_PRIORITY (f1);
1629 priority2 = DECL_INIT_PRIORITY (f2);
1631 if (priority1 < priority2)
1632 return -1;
1633 else if (priority1 > priority2)
1634 return 1;
1635 else
1636 /* Ensure a stable sort. Constructors are executed in backwarding
1637 order to make LTO initialize braries first. */
1638 return DECL_UID (f2) - DECL_UID (f1);
1641 /* Comparison function for qsort. P1 and P2 are actually of type
1642 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1643 used to determine the sort order. */
1645 static int
1646 compare_dtor (const void *p1, const void *p2)
1648 tree f1;
1649 tree f2;
1650 int priority1;
1651 int priority2;
1653 f1 = *(const tree *)p1;
1654 f2 = *(const tree *)p2;
1655 priority1 = DECL_FINI_PRIORITY (f1);
1656 priority2 = DECL_FINI_PRIORITY (f2);
1658 if (priority1 < priority2)
1659 return -1;
1660 else if (priority1 > priority2)
1661 return 1;
1662 else
1663 /* Ensure a stable sort. */
1664 return DECL_UID (f1) - DECL_UID (f2);
1667 /* Generate functions to call static constructors and destructors
1668 for targets that do not support .ctors/.dtors sections. These
1669 functions have magic names which are detected by collect2. */
1671 static void
1672 build_cdtor_fns (void)
1674 if (!static_ctors.is_empty ())
1676 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1677 static_ctors.qsort (compare_ctor);
1678 build_cdtor (/*ctor_p=*/true, static_ctors);
1681 if (!static_dtors.is_empty ())
1683 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1684 static_dtors.qsort (compare_dtor);
1685 build_cdtor (/*ctor_p=*/false, static_dtors);
1689 /* Look for constructors and destructors and produce function calling them.
1690 This is needed for targets not supporting ctors or dtors, but we perform the
1691 transformation also at linktime to merge possibly numerous
1692 constructors/destructors into single function to improve code locality and
1693 reduce size. */
1695 static unsigned int
1696 ipa_cdtor_merge (void)
1698 struct cgraph_node *node;
1699 FOR_EACH_DEFINED_FUNCTION (node)
1700 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1701 || DECL_STATIC_DESTRUCTOR (node->decl))
1702 record_cdtor_fn (node);
1703 build_cdtor_fns ();
1704 static_ctors.release ();
1705 static_dtors.release ();
1706 return 0;
1709 namespace {
1711 const pass_data pass_data_ipa_cdtor_merge =
1713 IPA_PASS, /* type */
1714 "cdtor", /* name */
1715 OPTGROUP_NONE, /* optinfo_flags */
1716 true, /* has_execute */
1717 TV_CGRAPHOPT, /* tv_id */
1718 0, /* properties_required */
1719 0, /* properties_provided */
1720 0, /* properties_destroyed */
1721 0, /* todo_flags_start */
1722 0, /* todo_flags_finish */
1725 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1727 public:
1728 pass_ipa_cdtor_merge (gcc::context *ctxt)
1729 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1730 NULL, /* generate_summary */
1731 NULL, /* write_summary */
1732 NULL, /* read_summary */
1733 NULL, /* write_optimization_summary */
1734 NULL, /* read_optimization_summary */
1735 NULL, /* stmt_fixup */
1736 0, /* function_transform_todo_flags_start */
1737 NULL, /* function_transform */
1738 NULL) /* variable_transform */
1741 /* opt_pass methods: */
1742 virtual bool gate (function *);
1743 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1745 }; // class pass_ipa_cdtor_merge
1747 bool
1748 pass_ipa_cdtor_merge::gate (function *)
1750 /* Perform the pass when we have no ctors/dtors support
1751 or at LTO time to merge multiple constructors into single
1752 function. */
1753 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1756 } // anon namespace
1758 ipa_opt_pass_d *
1759 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1761 return new pass_ipa_cdtor_merge (ctxt);