new tests
[official-gcc.git] / gcc / ipa.c
blob606a9f374d1e610dee2f1f5a6b1fb8611b175bea
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "cgraph.h"
26 #include "tree-pass.h"
27 #include "timevar.h"
28 #include "gimple.h"
29 #include "ggc.h"
30 #include "flags.h"
31 #include "pointer-set.h"
32 #include "target.h"
33 #include "tree-iterator.h"
34 #include "ipa-utils.h"
36 /* Look for all functions inlined to NODE and update their inlined_to pointers
37 to INLINED_TO. */
39 static void
40 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
42 struct cgraph_edge *e;
43 for (e = node->callees; e; e = e->next_callee)
44 if (e->callee->global.inlined_to)
46 e->callee->global.inlined_to = inlined_to;
47 update_inlined_to_pointer (e->callee, inlined_to);
51 /* Add cgraph NODE to queue starting at FIRST.
53 The queue is linked via AUX pointers and terminated by pointer to 1.
54 We enqueue nodes at two occasions: when we find them reachable or when we find
55 their bodies needed for further clonning. In the second case we mark them
56 by pointer to 2 after processing so they are re-queue when they become
57 reachable. */
59 static void
60 enqueue_cgraph_node (struct cgraph_node *node, struct cgraph_node **first)
62 /* Node is still in queue; do nothing. */
63 if (node->aux && node->aux != (void *) 2)
64 return;
65 /* Node was already processed as unreachable, re-enqueue
66 only if it became reachable now. */
67 if (node->aux == (void *)2 && !node->reachable)
68 return;
69 node->aux = *first;
70 *first = node;
73 /* Add varpool NODE to queue starting at FIRST. */
75 static void
76 enqueue_varpool_node (struct varpool_node *node, struct varpool_node **first)
78 node->aux = *first;
79 *first = node;
82 /* Process references. */
84 static void
85 process_references (struct ipa_ref_list *list,
86 struct cgraph_node **first,
87 struct varpool_node **first_varpool,
88 bool before_inlining_p)
90 int i;
91 struct ipa_ref *ref;
92 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
94 if (ref->refered_type == IPA_REF_CGRAPH)
96 struct cgraph_node *node = ipa_ref_node (ref);
97 if (!node->reachable
98 && node->analyzed
99 && (!DECL_EXTERNAL (node->decl)
100 || before_inlining_p))
101 node->reachable = true;
102 enqueue_cgraph_node (node, first);
104 else
106 struct varpool_node *node = ipa_ref_varpool_node (ref);
107 if (!node->needed)
109 varpool_mark_needed_node (node);
110 enqueue_varpool_node (node, first_varpool);
116 /* Return true when function can be marked local. */
118 static bool
119 cgraph_local_node_p (struct cgraph_node *node)
121 return (cgraph_only_called_directly_p (node)
122 && node->analyzed
123 && !DECL_EXTERNAL (node->decl)
124 && !node->local.externally_visible
125 && !node->reachable_from_other_partition
126 && !node->in_other_partition);
129 /* Perform reachability analysis and reclaim all unreachable nodes.
130 If BEFORE_INLINING_P is true this function is called before inlining
131 decisions has been made. If BEFORE_INLINING_P is false this function also
132 removes unneeded bodies of extern inline functions. */
134 bool
135 cgraph_remove_unreachable_nodes (bool before_inlining_p, FILE *file)
137 struct cgraph_node *first = (struct cgraph_node *) (void *) 1;
138 struct varpool_node *first_varpool = (struct varpool_node *) (void *) 1;
139 struct cgraph_node *node, *next;
140 struct varpool_node *vnode, *vnext;
141 bool changed = false;
143 #ifdef ENABLE_CHECKING
144 verify_cgraph ();
145 #endif
146 if (file)
147 fprintf (file, "\nReclaiming functions:");
148 #ifdef ENABLE_CHECKING
149 for (node = cgraph_nodes; node; node = node->next)
150 gcc_assert (!node->aux);
151 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
152 gcc_assert (!vnode->aux);
153 #endif
154 varpool_reset_queue ();
155 /* Mark functions whose bodies are obviously needed.
156 This is mostly when they can be referenced externally. Inline clones
157 are special since their declarations are shared with master clone and thus
158 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
159 for (node = cgraph_nodes; node; node = node->next)
160 if (node->analyzed && !node->global.inlined_to
161 && (!cgraph_can_remove_if_no_direct_calls_and_refs_p (node)
162 /* Keep around virtual functions for possible devirtualization. */
163 || (before_inlining_p
164 && DECL_VIRTUAL_P (node->decl)
165 && (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl)))
166 /* Also external functions with address taken are better to stay
167 for indirect inlining. */
168 || (before_inlining_p
169 && DECL_EXTERNAL (node->decl)
170 && node->address_taken)))
172 gcc_assert (!node->global.inlined_to);
173 enqueue_cgraph_node (node, &first);
174 node->reachable = true;
176 else
178 gcc_assert (!node->aux);
179 node->reachable = false;
182 /* Mark variables that are obviously needed. */
183 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
185 vnode->next_needed = NULL;
186 vnode->prev_needed = NULL;
187 if ((vnode->analyzed || vnode->force_output)
188 && !varpool_can_remove_if_no_refs (vnode))
190 vnode->needed = false;
191 varpool_mark_needed_node (vnode);
192 enqueue_varpool_node (vnode, &first_varpool);
194 else
195 vnode->needed = false;
198 /* Perform reachability analysis. As a special case do not consider
199 extern inline functions not inlined as live because we won't output
200 them at all.
202 We maintain two worklist, one for cgraph nodes other for varpools and
203 are finished once both are empty. */
205 while (first != (struct cgraph_node *) (void *) 1
206 || first_varpool != (struct varpool_node *) (void *) 1)
208 if (first != (struct cgraph_node *) (void *) 1)
210 struct cgraph_edge *e;
211 node = first;
212 first = (struct cgraph_node *) first->aux;
213 if (!node->reachable)
214 node->aux = (void *)2;
216 /* If we found this node reachable, first mark on the callees
217 reachable too, unless they are direct calls to extern inline functions
218 we decided to not inline. */
219 if (node->reachable)
221 for (e = node->callees; e; e = e->next_callee)
223 if (!e->callee->reachable
224 && node->analyzed
225 && (!e->inline_failed
226 || !DECL_EXTERNAL (e->callee->decl)
227 || before_inlining_p))
228 e->callee->reachable = true;
229 enqueue_cgraph_node (e->callee, &first);
231 process_references (&node->ref_list, &first, &first_varpool, before_inlining_p);
234 /* If any function in a comdat group is reachable, force
235 all other functions in the same comdat group to be
236 also reachable. */
237 if (node->same_comdat_group
238 && node->reachable
239 && !node->global.inlined_to)
241 for (next = node->same_comdat_group;
242 next != node;
243 next = next->same_comdat_group)
244 if (!next->reachable)
246 next->reachable = true;
247 enqueue_cgraph_node (next, &first);
251 /* We can freely remove inline clones even if they are cloned, however if
252 function is clone of real clone, we must keep it around in order to
253 make materialize_clones produce function body with the changes
254 applied. */
255 while (node->clone_of && !node->clone_of->aux
256 && !gimple_has_body_p (node->decl))
258 bool noninline = node->clone_of->decl != node->decl;
259 node = node->clone_of;
260 if (noninline && !node->reachable && !node->aux)
262 enqueue_cgraph_node (node, &first);
263 break;
267 if (first_varpool != (struct varpool_node *) (void *) 1)
269 vnode = first_varpool;
270 first_varpool = (struct varpool_node *)first_varpool->aux;
271 vnode->aux = NULL;
272 process_references (&vnode->ref_list, &first, &first_varpool, before_inlining_p);
273 /* If any function in a comdat group is reachable, force
274 all other functions in the same comdat group to be
275 also reachable. */
276 if (vnode->same_comdat_group)
278 struct varpool_node *next;
279 for (next = vnode->same_comdat_group;
280 next != vnode;
281 next = next->same_comdat_group)
282 if (!next->needed)
284 varpool_mark_needed_node (next);
285 enqueue_varpool_node (next, &first_varpool);
291 /* Remove unreachable nodes.
293 Completely unreachable functions can be fully removed from the callgraph.
294 Extern inline functions that we decided to not inline need to become unanalyzed nodes of
295 callgraph (so we still have edges to them). We remove function body then.
297 Also we need to care functions that are unreachable but we need to keep them around
298 for later clonning. In this case we also turn them to unanalyzed nodes, but
299 keep the body around. */
300 for (node = cgraph_nodes; node; node = next)
302 next = node->next;
303 if (node->aux && !node->reachable)
305 cgraph_node_remove_callees (node);
306 ipa_remove_all_references (&node->ref_list);
307 node->analyzed = false;
309 if (!node->aux)
311 struct cgraph_edge *e;
312 bool found = false;
313 int i;
314 struct ipa_ref *ref;
316 node->global.inlined_to = NULL;
317 if (file)
318 fprintf (file, " %s", cgraph_node_name (node));
319 /* See if there is reachable caller. */
320 for (e = node->callers; e && !found; e = e->next_caller)
321 if (e->caller->reachable)
322 found = true;
323 for (i = 0; (ipa_ref_list_refering_iterate (&node->ref_list, i, ref)
324 && !found); i++)
325 if (ref->refering_type == IPA_REF_CGRAPH
326 && ipa_ref_refering_node (ref)->reachable)
327 found = true;
328 else if (ref->refering_type == IPA_REF_VARPOOL
329 && ipa_ref_refering_varpool_node (ref)->needed)
330 found = true;
332 /* If so, we need to keep node in the callgraph. */
333 if (found)
335 if (node->analyzed)
337 struct cgraph_node *clone;
339 /* If there are still clones, we must keep body around.
340 Otherwise we can just remove the body but keep the clone. */
341 for (clone = node->clones; clone;
342 clone = clone->next_sibling_clone)
343 if (clone->aux)
344 break;
345 if (!clone)
347 cgraph_release_function_body (node);
348 if (node->prev_sibling_clone)
349 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
350 else if (node->clone_of)
351 node->clone_of->clones = node->next_sibling_clone;
352 if (node->next_sibling_clone)
353 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
354 if (node->clone_of)
355 node->former_clone_of = node->clone_of->decl;
356 node->clone_of = NULL;
357 node->next_sibling_clone = NULL;
358 node->prev_sibling_clone = NULL;
360 else
361 gcc_assert (!clone->in_other_partition);
362 node->analyzed = false;
363 changed = true;
364 cgraph_node_remove_callees (node);
365 ipa_remove_all_references (&node->ref_list);
368 else
370 cgraph_remove_node (node);
371 changed = true;
375 for (node = cgraph_nodes; node; node = node->next)
377 /* Inline clones might be kept around so their materializing allows further
378 cloning. If the function the clone is inlined into is removed, we need
379 to turn it into normal cone. */
380 if (node->global.inlined_to
381 && !node->callers)
383 gcc_assert (node->clones);
384 node->global.inlined_to = NULL;
385 update_inlined_to_pointer (node, node);
387 node->aux = NULL;
390 if (file)
391 fprintf (file, "\n");
393 /* We must release unused extern inlines or sanity checking will fail. Rest of transformations
394 are undesirable at -O0 since we do not want to remove anything. */
395 if (!optimize)
396 return changed;
398 if (file)
399 fprintf (file, "Reclaiming variables:");
400 for (vnode = varpool_nodes; vnode; vnode = vnext)
402 vnext = vnode->next;
403 if (!vnode->needed)
405 if (file)
406 fprintf (file, " %s", varpool_node_name (vnode));
407 varpool_remove_node (vnode);
408 changed = true;
412 /* Now update address_taken flags and try to promote functions to be local. */
414 if (file)
415 fprintf (file, "\nClearing address taken flags:");
416 for (node = cgraph_nodes; node; node = node->next)
417 if (node->address_taken
418 && !node->reachable_from_other_partition)
420 int i;
421 struct ipa_ref *ref;
422 bool found = false;
423 for (i = 0; ipa_ref_list_refering_iterate (&node->ref_list, i, ref)
424 && !found; i++)
426 gcc_assert (ref->use == IPA_REF_ADDR);
427 found = true;
429 if (!found)
431 if (file)
432 fprintf (file, " %s", cgraph_node_name (node));
433 node->address_taken = false;
434 changed = true;
435 if (cgraph_local_node_p (node))
437 node->local.local = true;
438 if (file)
439 fprintf (file, " (local)");
443 if (file)
444 fprintf (file, "\n");
446 #ifdef ENABLE_CHECKING
447 verify_cgraph ();
448 #endif
450 /* Reclaim alias pairs for functions that have disappeared from the
451 call graph. */
452 remove_unreachable_alias_pairs ();
454 return changed;
457 /* Discover variables that have no longer address taken or that are read only
458 and update their flags.
460 FIXME: This can not be done in between gimplify and omp_expand since
461 readonly flag plays role on what is shared and what is not. Currently we do
462 this transformation as part of whole program visibility and re-do at
463 ipa-reference pass (to take into account clonning), but it would
464 make sense to do it before early optimizations. */
466 void
467 ipa_discover_readonly_nonaddressable_vars (void)
469 struct varpool_node *vnode;
470 if (dump_file)
471 fprintf (dump_file, "Clearing variable flags:");
472 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
473 if (vnode->finalized && varpool_all_refs_explicit_p (vnode)
474 && (TREE_ADDRESSABLE (vnode->decl) || !TREE_READONLY (vnode->decl)))
476 bool written = false;
477 bool address_taken = false;
478 int i;
479 struct ipa_ref *ref;
480 for (i = 0; ipa_ref_list_refering_iterate (&vnode->ref_list, i, ref)
481 && (!written || !address_taken); i++)
482 switch (ref->use)
484 case IPA_REF_ADDR:
485 address_taken = true;
486 break;
487 case IPA_REF_LOAD:
488 break;
489 case IPA_REF_STORE:
490 written = true;
491 break;
493 if (TREE_ADDRESSABLE (vnode->decl) && !address_taken)
495 if (dump_file)
496 fprintf (dump_file, " %s (addressable)", varpool_node_name (vnode));
497 TREE_ADDRESSABLE (vnode->decl) = 0;
499 if (!TREE_READONLY (vnode->decl) && !address_taken && !written
500 /* Making variable in explicit section readonly can cause section
501 type conflict.
502 See e.g. gcc.c-torture/compile/pr23237.c */
503 && DECL_SECTION_NAME (vnode->decl) == NULL)
505 if (dump_file)
506 fprintf (dump_file, " %s (read-only)", varpool_node_name (vnode));
507 TREE_READONLY (vnode->decl) = 1;
510 if (dump_file)
511 fprintf (dump_file, "\n");
514 /* Return true when there is a reference to node and it is not vtable. */
515 static bool
516 cgraph_address_taken_from_non_vtable_p (struct cgraph_node *node)
518 int i;
519 struct ipa_ref *ref;
520 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
522 struct varpool_node *node;
523 if (ref->refered_type == IPA_REF_CGRAPH)
524 return true;
525 node = ipa_ref_varpool_node (ref);
526 if (!DECL_VIRTUAL_P (node->decl))
527 return true;
529 return false;
532 /* COMDAT functions must be shared only if they have address taken,
533 otherwise we can produce our own private implementation with
534 -fwhole-program.
535 Return true when turning COMDAT functoin static can not lead to wrong
536 code when the resulting object links with a library defining same COMDAT.
538 Virtual functions do have their addresses taken from the vtables,
539 but in C++ there is no way to compare their addresses for equality. */
541 bool
542 cgraph_comdat_can_be_unshared_p (struct cgraph_node *node)
544 if ((cgraph_address_taken_from_non_vtable_p (node)
545 && !DECL_VIRTUAL_P (node->decl))
546 || !node->analyzed)
547 return false;
548 if (node->same_comdat_group)
550 struct cgraph_node *next;
552 /* If more than one function is in the same COMDAT group, it must
553 be shared even if just one function in the comdat group has
554 address taken. */
555 for (next = node->same_comdat_group;
556 next != node; next = next->same_comdat_group)
557 if (cgraph_address_taken_from_non_vtable_p (node)
558 && !DECL_VIRTUAL_P (next->decl))
559 return false;
561 return true;
564 /* Return true when function NODE should be considered externally visible. */
566 static bool
567 cgraph_externally_visible_p (struct cgraph_node *node, bool whole_program, bool aliased)
569 struct cgraph_node *alias;
570 if (!node->local.finalized)
571 return false;
572 if (!DECL_COMDAT (node->decl)
573 && (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl)))
574 return false;
576 /* Do not even try to be smart about aliased nodes. Until we properly
577 represent everything by same body alias, these are just evil. */
578 if (aliased)
579 return true;
581 /* Do not try to localize built-in functions yet. One of problems is that we
582 end up mangling their asm for WHOPR that makes it impossible to call them
583 using the implicit built-in declarations anymore. Similarly this enables
584 us to remove them as unreachable before actual calls may appear during
585 expansion or folding. */
586 if (DECL_BUILT_IN (node->decl))
587 return true;
589 /* FIXME: We get wrong symbols with asm aliases in callgraph and LTO.
590 This is because very little of code knows that assembler name needs to
591 mangled. Avoid touching declarations with user asm name set to mask
592 some of the problems. */
593 if (DECL_ASSEMBLER_NAME_SET_P (node->decl)
594 && IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl))[0]=='*')
595 return true;
597 /* If linker counts on us, we must preserve the function. */
598 if (cgraph_used_from_object_file_p (node))
599 return true;
600 if (DECL_PRESERVE_P (node->decl))
601 return true;
602 if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (node->decl)))
603 return true;
604 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
605 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (node->decl)))
606 return true;
607 /* When doing LTO or whole program, we can bring COMDAT functoins static.
608 This improves code quality and we know we will duplicate them at most twice
609 (in the case that we are not using plugin and link with object file
610 implementing same COMDAT) */
611 if ((in_lto_p || whole_program)
612 && DECL_COMDAT (node->decl)
613 && cgraph_comdat_can_be_unshared_p (node))
614 return false;
616 /* See if we have linker information about symbol not being used or
617 if we need to make guess based on the declaration.
619 Even if the linker clams the symbol is unused, never bring internal
620 symbols that are declared by user as used or externally visible.
621 This is needed for i.e. references from asm statements. */
622 for (alias = node->same_body; alias; alias = alias->next)
623 if (alias->resolution != LDPR_PREVAILING_DEF_IRONLY)
624 break;
625 if (!alias && node->resolution == LDPR_PREVAILING_DEF_IRONLY)
626 return false;
628 /* When doing link time optimizations, hidden symbols become local. */
629 if (in_lto_p
630 && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
631 || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
632 /* Be sure that node is defined in IR file, not in other object
633 file. In that case we don't set used_from_other_object_file. */
634 && node->analyzed)
636 else if (!whole_program)
637 return true;
639 if (MAIN_NAME_P (DECL_NAME (node->decl)))
640 return true;
642 return false;
645 /* Return true when variable VNODE should be considered externally visible. */
647 static bool
648 varpool_externally_visible_p (struct varpool_node *vnode, bool aliased)
650 struct varpool_node *alias;
651 if (!DECL_COMDAT (vnode->decl) && !TREE_PUBLIC (vnode->decl))
652 return false;
654 /* Do not even try to be smart about aliased nodes. Until we properly
655 represent everything by same body alias, these are just evil. */
656 if (aliased)
657 return true;
659 /* If linker counts on us, we must preserve the function. */
660 if (varpool_used_from_object_file_p (vnode))
661 return true;
663 /* FIXME: We get wrong symbols with asm aliases in callgraph and LTO.
664 This is because very little of code knows that assembler name needs to
665 mangled. Avoid touching declarations with user asm name set to mask
666 some of the problems. */
667 if (DECL_ASSEMBLER_NAME_SET_P (vnode->decl)
668 && IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (vnode->decl))[0]=='*')
669 return true;
671 if (DECL_PRESERVE_P (vnode->decl))
672 return true;
673 if (lookup_attribute ("externally_visible",
674 DECL_ATTRIBUTES (vnode->decl)))
675 return true;
676 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
677 && lookup_attribute ("dllexport",
678 DECL_ATTRIBUTES (vnode->decl)))
679 return true;
681 /* See if we have linker information about symbol not being used or
682 if we need to make guess based on the declaration.
684 Even if the linker clams the symbol is unused, never bring internal
685 symbols that are declared by user as used or externally visible.
686 This is needed for i.e. references from asm statements. */
687 if (varpool_used_from_object_file_p (vnode))
688 return true;
689 for (alias = vnode->extra_name; alias; alias = alias->next)
690 if (alias->resolution != LDPR_PREVAILING_DEF_IRONLY)
691 break;
692 if (!alias && vnode->resolution == LDPR_PREVAILING_DEF_IRONLY)
693 return false;
695 /* As a special case, the COMDAT virutal tables can be unshared.
696 In LTO mode turn vtables into static variables. The variable is readonly,
697 so this does not enable more optimization, but referring static var
698 is faster for dynamic linking. Also this match logic hidding vtables
699 from LTO symbol tables. */
700 if ((in_lto_p || flag_whole_program)
701 && !vnode->force_output
702 && DECL_COMDAT (vnode->decl) && DECL_VIRTUAL_P (vnode->decl))
703 return false;
705 /* When doing link time optimizations, hidden symbols become local. */
706 if (in_lto_p
707 && (DECL_VISIBILITY (vnode->decl) == VISIBILITY_HIDDEN
708 || DECL_VISIBILITY (vnode->decl) == VISIBILITY_INTERNAL)
709 /* Be sure that node is defined in IR file, not in other object
710 file. In that case we don't set used_from_other_object_file. */
711 && vnode->finalized)
713 else if (!flag_whole_program)
714 return true;
716 /* Do not attempt to privatize COMDATS by default.
717 This would break linking with C++ libraries sharing
718 inline definitions.
720 FIXME: We can do so for readonly vars with no address taken and
721 possibly also for vtables since no direct pointer comparsion is done.
722 It might be interesting to do so to reduce linking overhead. */
723 if (DECL_COMDAT (vnode->decl) || DECL_WEAK (vnode->decl))
724 return true;
725 return false;
728 /* Dissolve the same_comdat_group list in which NODE resides. */
730 static void
731 dissolve_same_comdat_group_list (struct cgraph_node *node)
733 struct cgraph_node *n = node, *next;
736 next = n->same_comdat_group;
737 n->same_comdat_group = NULL;
738 n = next;
740 while (n != node);
743 /* Mark visibility of all functions.
745 A local function is one whose calls can occur only in the current
746 compilation unit and all its calls are explicit, so we can change
747 its calling convention. We simply mark all static functions whose
748 address is not taken as local.
750 We also change the TREE_PUBLIC flag of all declarations that are public
751 in language point of view but we want to overwrite this default
752 via visibilities for the backend point of view. */
754 static unsigned int
755 function_and_variable_visibility (bool whole_program)
757 struct cgraph_node *node;
758 struct varpool_node *vnode;
759 struct pointer_set_t *aliased_nodes = pointer_set_create ();
760 struct pointer_set_t *aliased_vnodes = pointer_set_create ();
761 unsigned i;
762 alias_pair *p;
764 /* Discover aliased nodes. */
765 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
767 if (dump_file)
768 fprintf (dump_file, "Alias %s->%s",
769 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (p->decl)),
770 IDENTIFIER_POINTER (p->target));
772 if ((node = cgraph_node_for_asm (p->target)) != NULL
773 && !DECL_EXTERNAL (node->decl))
775 if (!node->analyzed)
776 continue;
777 /* Weakrefs alias symbols from other compilation unit. In the case
778 the destination of weakref became available because of LTO, we must
779 mark it as needed. */
780 if (in_lto_p
781 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl))
782 && !node->needed)
783 cgraph_mark_needed_node (node);
784 gcc_assert (node->needed);
785 pointer_set_insert (aliased_nodes, node);
786 if (dump_file)
787 fprintf (dump_file, " node %s/%i",
788 cgraph_node_name (node), node->uid);
790 else if ((vnode = varpool_node_for_asm (p->target)) != NULL
791 && !DECL_EXTERNAL (vnode->decl))
793 /* Weakrefs alias symbols from other compilation unit. In the case
794 the destination of weakref became available because of LTO, we must
795 mark it as needed. */
796 if (in_lto_p
797 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl))
798 && !vnode->needed)
799 varpool_mark_needed_node (vnode);
800 gcc_assert (vnode->needed);
801 pointer_set_insert (aliased_vnodes, vnode);
802 if (dump_file)
803 fprintf (dump_file, " varpool node %s",
804 varpool_node_name (vnode));
806 if (dump_file)
807 fprintf (dump_file, "\n");
810 for (node = cgraph_nodes; node; node = node->next)
812 int flags = flags_from_decl_or_type (node->decl);
814 /* Optimize away PURE and CONST constructors and destructors. */
815 if (optimize
816 && (flags & (ECF_CONST | ECF_PURE))
817 && !(flags & ECF_LOOPING_CONST_OR_PURE))
819 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
820 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
823 /* Frontends and alias code marks nodes as needed before parsing is finished.
824 We may end up marking as node external nodes where this flag is meaningless
825 strip it. */
826 if (node->needed
827 && (DECL_EXTERNAL (node->decl) || !node->analyzed))
828 node->needed = 0;
830 /* C++ FE on lack of COMDAT support create local COMDAT functions
831 (that ought to be shared but can not due to object format
832 limitations). It is neccesary to keep the flag to make rest of C++ FE
833 happy. Clear the flag here to avoid confusion in middle-end. */
834 if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
835 DECL_COMDAT (node->decl) = 0;
836 /* For external decls stop tracking same_comdat_group, it doesn't matter
837 what comdat group they are in when they won't be emitted in this TU,
838 and simplifies later passes. */
839 if (node->same_comdat_group && DECL_EXTERNAL (node->decl))
841 #ifdef ENABLE_CHECKING
842 struct cgraph_node *n;
844 for (n = node->same_comdat_group;
845 n != node;
846 n = n->same_comdat_group)
847 /* If at least one of same comdat group functions is external,
848 all of them have to be, otherwise it is a front-end bug. */
849 gcc_assert (DECL_EXTERNAL (n->decl));
850 #endif
851 dissolve_same_comdat_group_list (node);
853 gcc_assert ((!DECL_WEAK (node->decl) && !DECL_COMDAT (node->decl))
854 || TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl));
855 if (cgraph_externally_visible_p (node, whole_program,
856 pointer_set_contains (aliased_nodes,
857 node)))
859 gcc_assert (!node->global.inlined_to);
860 node->local.externally_visible = true;
862 else
863 node->local.externally_visible = false;
864 if (!node->local.externally_visible && node->analyzed
865 && !DECL_EXTERNAL (node->decl))
867 struct cgraph_node *alias;
868 gcc_assert (whole_program || in_lto_p || !TREE_PUBLIC (node->decl));
869 cgraph_make_decl_local (node->decl);
870 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
871 for (alias = node->same_body; alias; alias = alias->next)
872 cgraph_make_decl_local (alias->decl);
873 if (node->same_comdat_group)
874 /* cgraph_externally_visible_p has already checked all other nodes
875 in the group and they will all be made local. We need to
876 dissolve the group at once so that the predicate does not
877 segfault though. */
878 dissolve_same_comdat_group_list (node);
880 node->local.local = cgraph_local_node_p (node);
882 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
884 /* weak flag makes no sense on local variables. */
885 gcc_assert (!DECL_WEAK (vnode->decl)
886 || TREE_PUBLIC (vnode->decl) || DECL_EXTERNAL (vnode->decl));
887 /* In several cases declarations can not be common:
889 - when declaration has initializer
890 - when it is in weak
891 - when it has specific section
892 - when it resides in non-generic address space.
893 - if declaration is local, it will get into .local common section
894 so common flag is not needed. Frontends still produce these in
895 certain cases, such as for:
897 static int a __attribute__ ((common))
899 Canonicalize things here and clear the redundant flag. */
900 if (DECL_COMMON (vnode->decl)
901 && (!(TREE_PUBLIC (vnode->decl) || DECL_EXTERNAL (vnode->decl))
902 || (DECL_INITIAL (vnode->decl)
903 && DECL_INITIAL (vnode->decl) != error_mark_node)
904 || DECL_WEAK (vnode->decl)
905 || DECL_SECTION_NAME (vnode->decl) != NULL
906 || ! (ADDR_SPACE_GENERIC_P
907 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
908 DECL_COMMON (vnode->decl) = 0;
910 for (vnode = varpool_nodes_queue; vnode; vnode = vnode->next_needed)
912 if (!vnode->finalized)
913 continue;
914 if (vnode->needed
915 && varpool_externally_visible_p
916 (vnode,
917 pointer_set_contains (aliased_vnodes, vnode)))
918 vnode->externally_visible = true;
919 else
920 vnode->externally_visible = false;
921 if (!vnode->externally_visible)
923 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->decl));
924 cgraph_make_decl_local (vnode->decl);
925 vnode->resolution = LDPR_PREVAILING_DEF_IRONLY;
927 gcc_assert (TREE_STATIC (vnode->decl));
929 pointer_set_destroy (aliased_nodes);
930 pointer_set_destroy (aliased_vnodes);
932 if (dump_file)
934 fprintf (dump_file, "\nMarking local functions:");
935 for (node = cgraph_nodes; node; node = node->next)
936 if (node->local.local)
937 fprintf (dump_file, " %s", cgraph_node_name (node));
938 fprintf (dump_file, "\n\n");
939 fprintf (dump_file, "\nMarking externally visible functions:");
940 for (node = cgraph_nodes; node; node = node->next)
941 if (node->local.externally_visible)
942 fprintf (dump_file, " %s", cgraph_node_name (node));
943 fprintf (dump_file, "\n\n");
944 fprintf (dump_file, "\nMarking externally visible variables:");
945 for (vnode = varpool_nodes_queue; vnode; vnode = vnode->next_needed)
946 if (vnode->externally_visible)
947 fprintf (dump_file, " %s", varpool_node_name (vnode));
948 fprintf (dump_file, "\n\n");
950 cgraph_function_flags_ready = true;
951 return 0;
954 /* Local function pass handling visibilities. This happens before LTO streaming
955 so in particular -fwhole-program should be ignored at this level. */
957 static unsigned int
958 local_function_and_variable_visibility (void)
960 return function_and_variable_visibility (flag_whole_program && !flag_lto);
963 struct simple_ipa_opt_pass pass_ipa_function_and_variable_visibility =
966 SIMPLE_IPA_PASS,
967 "visibility", /* name */
968 NULL, /* gate */
969 local_function_and_variable_visibility,/* execute */
970 NULL, /* sub */
971 NULL, /* next */
972 0, /* static_pass_number */
973 TV_CGRAPHOPT, /* tv_id */
974 0, /* properties_required */
975 0, /* properties_provided */
976 0, /* properties_destroyed */
977 0, /* todo_flags_start */
978 TODO_remove_functions | TODO_dump_cgraph
979 | TODO_ggc_collect /* todo_flags_finish */
983 /* Do not re-run on ltrans stage. */
985 static bool
986 gate_whole_program_function_and_variable_visibility (void)
988 return !flag_ltrans;
991 /* Bring functionss local at LTO time whith -fwhole-program. */
993 static unsigned int
994 whole_program_function_and_variable_visibility (void)
996 struct cgraph_node *node;
997 struct varpool_node *vnode;
999 function_and_variable_visibility (flag_whole_program);
1001 for (node = cgraph_nodes; node; node = node->next)
1002 if ((node->local.externally_visible && !DECL_COMDAT (node->decl))
1003 && node->local.finalized)
1004 cgraph_mark_needed_node (node);
1005 for (vnode = varpool_nodes_queue; vnode; vnode = vnode->next_needed)
1006 if (vnode->externally_visible && !DECL_COMDAT (vnode->decl))
1007 varpool_mark_needed_node (vnode);
1008 if (dump_file)
1010 fprintf (dump_file, "\nNeeded variables:");
1011 for (vnode = varpool_nodes_queue; vnode; vnode = vnode->next_needed)
1012 if (vnode->needed)
1013 fprintf (dump_file, " %s", varpool_node_name (vnode));
1014 fprintf (dump_file, "\n\n");
1016 if (optimize)
1017 ipa_discover_readonly_nonaddressable_vars ();
1018 return 0;
1021 struct ipa_opt_pass_d pass_ipa_whole_program_visibility =
1024 IPA_PASS,
1025 "whole-program", /* name */
1026 gate_whole_program_function_and_variable_visibility,/* gate */
1027 whole_program_function_and_variable_visibility,/* execute */
1028 NULL, /* sub */
1029 NULL, /* next */
1030 0, /* static_pass_number */
1031 TV_CGRAPHOPT, /* tv_id */
1032 0, /* properties_required */
1033 0, /* properties_provided */
1034 0, /* properties_destroyed */
1035 0, /* todo_flags_start */
1036 TODO_remove_functions | TODO_dump_cgraph
1037 | TODO_ggc_collect /* todo_flags_finish */
1039 NULL, /* generate_summary */
1040 NULL, /* write_summary */
1041 NULL, /* read_summary */
1042 NULL, /* write_optimization_summary */
1043 NULL, /* read_optimization_summary */
1044 NULL, /* stmt_fixup */
1045 0, /* TODOs */
1046 NULL, /* function_transform */
1047 NULL, /* variable_transform */
1050 /* Hash a cgraph node set element. */
1052 static hashval_t
1053 hash_cgraph_node_set_element (const void *p)
1055 const_cgraph_node_set_element element = (const_cgraph_node_set_element) p;
1056 return htab_hash_pointer (element->node);
1059 /* Compare two cgraph node set elements. */
1061 static int
1062 eq_cgraph_node_set_element (const void *p1, const void *p2)
1064 const_cgraph_node_set_element e1 = (const_cgraph_node_set_element) p1;
1065 const_cgraph_node_set_element e2 = (const_cgraph_node_set_element) p2;
1067 return e1->node == e2->node;
1070 /* Create a new cgraph node set. */
1072 cgraph_node_set
1073 cgraph_node_set_new (void)
1075 cgraph_node_set new_node_set;
1077 new_node_set = ggc_alloc_cgraph_node_set_def ();
1078 new_node_set->hashtab = htab_create_ggc (10,
1079 hash_cgraph_node_set_element,
1080 eq_cgraph_node_set_element,
1081 NULL);
1082 new_node_set->nodes = NULL;
1083 return new_node_set;
1086 /* Add cgraph_node NODE to cgraph_node_set SET. */
1088 void
1089 cgraph_node_set_add (cgraph_node_set set, struct cgraph_node *node)
1091 void **slot;
1092 cgraph_node_set_element element;
1093 struct cgraph_node_set_element_def dummy;
1095 dummy.node = node;
1096 slot = htab_find_slot (set->hashtab, &dummy, INSERT);
1098 if (*slot != HTAB_EMPTY_ENTRY)
1100 element = (cgraph_node_set_element) *slot;
1101 gcc_assert (node == element->node
1102 && (VEC_index (cgraph_node_ptr, set->nodes, element->index)
1103 == node));
1104 return;
1107 /* Insert node into hash table. */
1108 element = ggc_alloc_cgraph_node_set_element_def ();
1109 element->node = node;
1110 element->index = VEC_length (cgraph_node_ptr, set->nodes);
1111 *slot = element;
1113 /* Insert into node vector. */
1114 VEC_safe_push (cgraph_node_ptr, gc, set->nodes, node);
1117 /* Remove cgraph_node NODE from cgraph_node_set SET. */
1119 void
1120 cgraph_node_set_remove (cgraph_node_set set, struct cgraph_node *node)
1122 void **slot, **last_slot;
1123 cgraph_node_set_element element, last_element;
1124 struct cgraph_node *last_node;
1125 struct cgraph_node_set_element_def dummy;
1127 dummy.node = node;
1128 slot = htab_find_slot (set->hashtab, &dummy, NO_INSERT);
1129 if (slot == NULL)
1130 return;
1132 element = (cgraph_node_set_element) *slot;
1133 gcc_assert (VEC_index (cgraph_node_ptr, set->nodes, element->index)
1134 == node);
1136 /* Remove from vector. We do this by swapping node with the last element
1137 of the vector. */
1138 last_node = VEC_pop (cgraph_node_ptr, set->nodes);
1139 if (last_node != node)
1141 dummy.node = last_node;
1142 last_slot = htab_find_slot (set->hashtab, &dummy, NO_INSERT);
1143 last_element = (cgraph_node_set_element) *last_slot;
1144 gcc_assert (last_element);
1146 /* Move the last element to the original spot of NODE. */
1147 last_element->index = element->index;
1148 VEC_replace (cgraph_node_ptr, set->nodes, last_element->index,
1149 last_node);
1152 /* Remove element from hash table. */
1153 htab_clear_slot (set->hashtab, slot);
1154 ggc_free (element);
1157 /* Find NODE in SET and return an iterator to it if found. A null iterator
1158 is returned if NODE is not in SET. */
1160 cgraph_node_set_iterator
1161 cgraph_node_set_find (cgraph_node_set set, struct cgraph_node *node)
1163 void **slot;
1164 struct cgraph_node_set_element_def dummy;
1165 cgraph_node_set_element element;
1166 cgraph_node_set_iterator csi;
1168 dummy.node = node;
1169 slot = htab_find_slot (set->hashtab, &dummy, NO_INSERT);
1170 if (slot == NULL)
1171 csi.index = (unsigned) ~0;
1172 else
1174 element = (cgraph_node_set_element) *slot;
1175 gcc_assert (VEC_index (cgraph_node_ptr, set->nodes, element->index)
1176 == node);
1177 csi.index = element->index;
1179 csi.set = set;
1181 return csi;
1184 /* Dump content of SET to file F. */
1186 void
1187 dump_cgraph_node_set (FILE *f, cgraph_node_set set)
1189 cgraph_node_set_iterator iter;
1191 for (iter = csi_start (set); !csi_end_p (iter); csi_next (&iter))
1193 struct cgraph_node *node = csi_node (iter);
1194 fprintf (f, " %s/%i", cgraph_node_name (node), node->uid);
1196 fprintf (f, "\n");
1199 /* Dump content of SET to stderr. */
1201 DEBUG_FUNCTION void
1202 debug_cgraph_node_set (cgraph_node_set set)
1204 dump_cgraph_node_set (stderr, set);
1207 /* Hash a varpool node set element. */
1209 static hashval_t
1210 hash_varpool_node_set_element (const void *p)
1212 const_varpool_node_set_element element = (const_varpool_node_set_element) p;
1213 return htab_hash_pointer (element->node);
1216 /* Compare two varpool node set elements. */
1218 static int
1219 eq_varpool_node_set_element (const void *p1, const void *p2)
1221 const_varpool_node_set_element e1 = (const_varpool_node_set_element) p1;
1222 const_varpool_node_set_element e2 = (const_varpool_node_set_element) p2;
1224 return e1->node == e2->node;
1227 /* Create a new varpool node set. */
1229 varpool_node_set
1230 varpool_node_set_new (void)
1232 varpool_node_set new_node_set;
1234 new_node_set = ggc_alloc_varpool_node_set_def ();
1235 new_node_set->hashtab = htab_create_ggc (10,
1236 hash_varpool_node_set_element,
1237 eq_varpool_node_set_element,
1238 NULL);
1239 new_node_set->nodes = NULL;
1240 return new_node_set;
1243 /* Add varpool_node NODE to varpool_node_set SET. */
1245 void
1246 varpool_node_set_add (varpool_node_set set, struct varpool_node *node)
1248 void **slot;
1249 varpool_node_set_element element;
1250 struct varpool_node_set_element_def dummy;
1252 dummy.node = node;
1253 slot = htab_find_slot (set->hashtab, &dummy, INSERT);
1255 if (*slot != HTAB_EMPTY_ENTRY)
1257 element = (varpool_node_set_element) *slot;
1258 gcc_assert (node == element->node
1259 && (VEC_index (varpool_node_ptr, set->nodes, element->index)
1260 == node));
1261 return;
1264 /* Insert node into hash table. */
1265 element = ggc_alloc_varpool_node_set_element_def ();
1266 element->node = node;
1267 element->index = VEC_length (varpool_node_ptr, set->nodes);
1268 *slot = element;
1270 /* Insert into node vector. */
1271 VEC_safe_push (varpool_node_ptr, gc, set->nodes, node);
1274 /* Remove varpool_node NODE from varpool_node_set SET. */
1276 void
1277 varpool_node_set_remove (varpool_node_set set, struct varpool_node *node)
1279 void **slot, **last_slot;
1280 varpool_node_set_element element, last_element;
1281 struct varpool_node *last_node;
1282 struct varpool_node_set_element_def dummy;
1284 dummy.node = node;
1285 slot = htab_find_slot (set->hashtab, &dummy, NO_INSERT);
1286 if (slot == NULL)
1287 return;
1289 element = (varpool_node_set_element) *slot;
1290 gcc_assert (VEC_index (varpool_node_ptr, set->nodes, element->index)
1291 == node);
1293 /* Remove from vector. We do this by swapping node with the last element
1294 of the vector. */
1295 last_node = VEC_pop (varpool_node_ptr, set->nodes);
1296 if (last_node != node)
1298 dummy.node = last_node;
1299 last_slot = htab_find_slot (set->hashtab, &dummy, NO_INSERT);
1300 last_element = (varpool_node_set_element) *last_slot;
1301 gcc_assert (last_element);
1303 /* Move the last element to the original spot of NODE. */
1304 last_element->index = element->index;
1305 VEC_replace (varpool_node_ptr, set->nodes, last_element->index,
1306 last_node);
1309 /* Remove element from hash table. */
1310 htab_clear_slot (set->hashtab, slot);
1311 ggc_free (element);
1314 /* Find NODE in SET and return an iterator to it if found. A null iterator
1315 is returned if NODE is not in SET. */
1317 varpool_node_set_iterator
1318 varpool_node_set_find (varpool_node_set set, struct varpool_node *node)
1320 void **slot;
1321 struct varpool_node_set_element_def dummy;
1322 varpool_node_set_element element;
1323 varpool_node_set_iterator vsi;
1325 dummy.node = node;
1326 slot = htab_find_slot (set->hashtab, &dummy, NO_INSERT);
1327 if (slot == NULL)
1328 vsi.index = (unsigned) ~0;
1329 else
1331 element = (varpool_node_set_element) *slot;
1332 gcc_assert (VEC_index (varpool_node_ptr, set->nodes, element->index)
1333 == node);
1334 vsi.index = element->index;
1336 vsi.set = set;
1338 return vsi;
1341 /* Dump content of SET to file F. */
1343 void
1344 dump_varpool_node_set (FILE *f, varpool_node_set set)
1346 varpool_node_set_iterator iter;
1348 for (iter = vsi_start (set); !vsi_end_p (iter); vsi_next (&iter))
1350 struct varpool_node *node = vsi_node (iter);
1351 fprintf (f, " %s", varpool_node_name (node));
1353 fprintf (f, "\n");
1356 /* Dump content of SET to stderr. */
1358 DEBUG_FUNCTION void
1359 debug_varpool_node_set (varpool_node_set set)
1361 dump_varpool_node_set (stderr, set);
1365 /* Simple ipa profile pass propagating frequencies across the callgraph. */
1367 static unsigned int
1368 ipa_profile (void)
1370 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1371 struct cgraph_edge *e;
1372 int order_pos;
1373 bool something_changed = false;
1374 int i;
1376 order_pos = ipa_reverse_postorder (order);
1377 for (i = order_pos - 1; i >= 0; i--)
1379 if (order[i]->local.local && cgraph_propagate_frequency (order[i]))
1381 for (e = order[i]->callees; e; e = e->next_callee)
1382 if (e->callee->local.local && !e->callee->aux)
1384 something_changed = true;
1385 e->callee->aux = (void *)1;
1388 order[i]->aux = NULL;
1391 while (something_changed)
1393 something_changed = false;
1394 for (i = order_pos - 1; i >= 0; i--)
1396 if (order[i]->aux && cgraph_propagate_frequency (order[i]))
1398 for (e = order[i]->callees; e; e = e->next_callee)
1399 if (e->callee->local.local && !e->callee->aux)
1401 something_changed = true;
1402 e->callee->aux = (void *)1;
1405 order[i]->aux = NULL;
1408 free (order);
1409 return 0;
1412 static bool
1413 gate_ipa_profile (void)
1415 return flag_ipa_profile;
1418 struct ipa_opt_pass_d pass_ipa_profile =
1421 IPA_PASS,
1422 "ipa-profile", /* name */
1423 gate_ipa_profile, /* gate */
1424 ipa_profile, /* execute */
1425 NULL, /* sub */
1426 NULL, /* next */
1427 0, /* static_pass_number */
1428 TV_IPA_PROFILE, /* tv_id */
1429 0, /* properties_required */
1430 0, /* properties_provided */
1431 0, /* properties_destroyed */
1432 0, /* todo_flags_start */
1433 0 /* todo_flags_finish */
1435 NULL, /* generate_summary */
1436 NULL, /* write_summary */
1437 NULL, /* read_summary */
1438 NULL, /* write_optimization_summary */
1439 NULL, /* read_optimization_summary */
1440 NULL, /* stmt_fixup */
1441 0, /* TODOs */
1442 NULL, /* function_transform */
1443 NULL /* variable_transform */
1446 /* Generate and emit a static constructor or destructor. WHICH must
1447 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1448 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1449 initialization priority for this constructor or destructor.
1451 FINAL specify whether the externally visible name for collect2 should
1452 be produced. */
1454 static void
1455 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
1457 static int counter = 0;
1458 char which_buf[16];
1459 tree decl, name, resdecl;
1461 /* The priority is encoded in the constructor or destructor name.
1462 collect2 will sort the names and arrange that they are called at
1463 program startup. */
1464 if (final)
1465 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1466 else
1467 /* Proudce sane name but one not recognizable by collect2, just for the
1468 case we fail to inline the function. */
1469 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
1470 name = get_file_function_name (which_buf);
1472 decl = build_decl (input_location, FUNCTION_DECL, name,
1473 build_function_type_list (void_type_node, NULL_TREE));
1474 current_function_decl = decl;
1476 resdecl = build_decl (input_location,
1477 RESULT_DECL, NULL_TREE, void_type_node);
1478 DECL_ARTIFICIAL (resdecl) = 1;
1479 DECL_RESULT (decl) = resdecl;
1480 DECL_CONTEXT (resdecl) = decl;
1482 allocate_struct_function (decl, false);
1484 TREE_STATIC (decl) = 1;
1485 TREE_USED (decl) = 1;
1486 DECL_ARTIFICIAL (decl) = 1;
1487 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1488 DECL_SAVED_TREE (decl) = body;
1489 if (!targetm.have_ctors_dtors && final)
1491 TREE_PUBLIC (decl) = 1;
1492 DECL_PRESERVE_P (decl) = 1;
1494 DECL_UNINLINABLE (decl) = 1;
1496 DECL_INITIAL (decl) = make_node (BLOCK);
1497 TREE_USED (DECL_INITIAL (decl)) = 1;
1499 DECL_SOURCE_LOCATION (decl) = input_location;
1500 cfun->function_end_locus = input_location;
1502 switch (which)
1504 case 'I':
1505 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1506 decl_init_priority_insert (decl, priority);
1507 break;
1508 case 'D':
1509 DECL_STATIC_DESTRUCTOR (decl) = 1;
1510 decl_fini_priority_insert (decl, priority);
1511 break;
1512 default:
1513 gcc_unreachable ();
1516 gimplify_function_tree (decl);
1518 cgraph_add_new_function (decl, false);
1520 set_cfun (NULL);
1521 current_function_decl = NULL;
1524 /* Generate and emit a static constructor or destructor. WHICH must
1525 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1526 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1527 initialization priority for this constructor or destructor. */
1529 void
1530 cgraph_build_static_cdtor (char which, tree body, int priority)
1532 cgraph_build_static_cdtor_1 (which, body, priority, false);
1535 /* A vector of FUNCTION_DECLs declared as static constructors. */
1536 static VEC(tree, heap) *static_ctors;
1537 /* A vector of FUNCTION_DECLs declared as static destructors. */
1538 static VEC(tree, heap) *static_dtors;
1540 /* When target does not have ctors and dtors, we call all constructor
1541 and destructor by special initialization/destruction function
1542 recognized by collect2.
1544 When we are going to build this function, collect all constructors and
1545 destructors and turn them into normal functions. */
1547 static void
1548 record_cdtor_fn (struct cgraph_node *node)
1550 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1551 VEC_safe_push (tree, heap, static_ctors, node->decl);
1552 if (DECL_STATIC_DESTRUCTOR (node->decl))
1553 VEC_safe_push (tree, heap, static_dtors, node->decl);
1554 node = cgraph_get_node (node->decl);
1555 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
1558 /* Define global constructors/destructor functions for the CDTORS, of
1559 which they are LEN. The CDTORS are sorted by initialization
1560 priority. If CTOR_P is true, these are constructors; otherwise,
1561 they are destructors. */
1563 static void
1564 build_cdtor (bool ctor_p, VEC (tree, heap) *cdtors)
1566 size_t i,j;
1567 size_t len = VEC_length (tree, cdtors);
1569 i = 0;
1570 while (i < len)
1572 tree body;
1573 tree fn;
1574 priority_type priority;
1576 priority = 0;
1577 body = NULL_TREE;
1578 j = i;
1581 priority_type p;
1582 fn = VEC_index (tree, cdtors, j);
1583 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1584 if (j == i)
1585 priority = p;
1586 else if (p != priority)
1587 break;
1588 j++;
1590 while (j < len);
1592 /* When there is only one cdtor and target supports them, do nothing. */
1593 if (j == i + 1
1594 && targetm.have_ctors_dtors)
1596 i++;
1597 continue;
1599 /* Find the next batch of constructors/destructors with the same
1600 initialization priority. */
1601 for (;i < j; i++)
1603 tree call;
1604 fn = VEC_index (tree, cdtors, i);
1605 call = build_call_expr (fn, 0);
1606 if (ctor_p)
1607 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1608 else
1609 DECL_STATIC_DESTRUCTOR (fn) = 0;
1610 /* We do not want to optimize away pure/const calls here.
1611 When optimizing, these should be already removed, when not
1612 optimizing, we want user to be able to breakpoint in them. */
1613 TREE_SIDE_EFFECTS (call) = 1;
1614 append_to_statement_list (call, &body);
1616 gcc_assert (body != NULL_TREE);
1617 /* Generate a function to call all the function of like
1618 priority. */
1619 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1623 /* Comparison function for qsort. P1 and P2 are actually of type
1624 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1625 used to determine the sort order. */
1627 static int
1628 compare_ctor (const void *p1, const void *p2)
1630 tree f1;
1631 tree f2;
1632 int priority1;
1633 int priority2;
1635 f1 = *(const tree *)p1;
1636 f2 = *(const tree *)p2;
1637 priority1 = DECL_INIT_PRIORITY (f1);
1638 priority2 = DECL_INIT_PRIORITY (f2);
1640 if (priority1 < priority2)
1641 return -1;
1642 else if (priority1 > priority2)
1643 return 1;
1644 else
1645 /* Ensure a stable sort. Constructors are executed in backwarding
1646 order to make LTO initialize braries first. */
1647 return DECL_UID (f2) - DECL_UID (f1);
1650 /* Comparison function for qsort. P1 and P2 are actually of type
1651 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1652 used to determine the sort order. */
1654 static int
1655 compare_dtor (const void *p1, const void *p2)
1657 tree f1;
1658 tree f2;
1659 int priority1;
1660 int priority2;
1662 f1 = *(const tree *)p1;
1663 f2 = *(const tree *)p2;
1664 priority1 = DECL_FINI_PRIORITY (f1);
1665 priority2 = DECL_FINI_PRIORITY (f2);
1667 if (priority1 < priority2)
1668 return -1;
1669 else if (priority1 > priority2)
1670 return 1;
1671 else
1672 /* Ensure a stable sort. */
1673 return DECL_UID (f1) - DECL_UID (f2);
1676 /* Generate functions to call static constructors and destructors
1677 for targets that do not support .ctors/.dtors sections. These
1678 functions have magic names which are detected by collect2. */
1680 static void
1681 build_cdtor_fns (void)
1683 if (!VEC_empty (tree, static_ctors))
1685 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1686 VEC_qsort (tree, static_ctors, compare_ctor);
1687 build_cdtor (/*ctor_p=*/true, static_ctors);
1690 if (!VEC_empty (tree, static_dtors))
1692 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1693 VEC_qsort (tree, static_dtors, compare_dtor);
1694 build_cdtor (/*ctor_p=*/false, static_dtors);
1698 /* Look for constructors and destructors and produce function calling them.
1699 This is needed for targets not supporting ctors or dtors, but we perform the
1700 transformation also at linktime to merge possibly numberous
1701 constructors/destructors into single function to improve code locality and
1702 reduce size. */
1704 static unsigned int
1705 ipa_cdtor_merge (void)
1707 struct cgraph_node *node;
1708 for (node = cgraph_nodes; node; node = node->next)
1709 if (node->analyzed
1710 && (DECL_STATIC_CONSTRUCTOR (node->decl)
1711 || DECL_STATIC_DESTRUCTOR (node->decl)))
1712 record_cdtor_fn (node);
1713 build_cdtor_fns ();
1714 VEC_free (tree, heap, static_ctors);
1715 VEC_free (tree, heap, static_dtors);
1716 return 0;
1719 /* Perform the pass when we have no ctors/dtors support
1720 or at LTO time to merge multiple constructors into single
1721 function. */
1723 static bool
1724 gate_ipa_cdtor_merge (void)
1726 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1729 struct ipa_opt_pass_d pass_ipa_cdtor_merge =
1732 IPA_PASS,
1733 "cdtor", /* name */
1734 gate_ipa_cdtor_merge, /* gate */
1735 ipa_cdtor_merge, /* execute */
1736 NULL, /* sub */
1737 NULL, /* next */
1738 0, /* static_pass_number */
1739 TV_CGRAPHOPT, /* tv_id */
1740 0, /* properties_required */
1741 0, /* properties_provided */
1742 0, /* properties_destroyed */
1743 0, /* todo_flags_start */
1744 0 /* todo_flags_finish */
1746 NULL, /* generate_summary */
1747 NULL, /* write_summary */
1748 NULL, /* read_summary */
1749 NULL, /* write_optimization_summary */
1750 NULL, /* read_optimization_summary */
1751 NULL, /* stmt_fixup */
1752 0, /* TODOs */
1753 NULL, /* function_transform */
1754 NULL /* variable_transform */