* doc/invoke.texi (core-avx2): Document.
[official-gcc.git] / gcc / ipa.c
bloba73cbb1c08962f5bc2a574774d6ccc788a67c1e0
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "cgraph.h"
25 #include "tree-pass.h"
26 #include "gimple.h"
27 #include "ggc.h"
28 #include "flags.h"
29 #include "pointer-set.h"
30 #include "target.h"
31 #include "tree-iterator.h"
32 #include "ipa-utils.h"
33 #include "pointer-set.h"
34 #include "ipa-inline.h"
35 #include "hash-table.h"
36 #include "tree-inline.h"
37 #include "profile.h"
38 #include "params.h"
39 #include "lto-streamer.h"
40 #include "data-streamer.h"
42 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
44 static bool
45 cgraph_non_local_node_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
47 /* FIXME: Aliases can be local, but i386 gets thunks wrong then. */
48 return !(cgraph_only_called_directly_or_aliased_p (node)
49 && !ipa_ref_has_aliases_p (&node->symbol.ref_list)
50 && node->symbol.definition
51 && !DECL_EXTERNAL (node->symbol.decl)
52 && !node->symbol.externally_visible
53 && !node->symbol.used_from_other_partition
54 && !node->symbol.in_other_partition);
57 /* Return true when function can be marked local. */
59 static bool
60 cgraph_local_node_p (struct cgraph_node *node)
62 struct cgraph_node *n = cgraph_function_or_thunk_node (node, NULL);
64 /* FIXME: thunks can be considered local, but we need prevent i386
65 from attempting to change calling convention of them. */
66 if (n->thunk.thunk_p)
67 return false;
68 return !cgraph_for_node_and_aliases (n,
69 cgraph_non_local_node_p_1, NULL, true);
73 /* Return true when NODE has ADDR reference. */
75 static bool
76 has_addr_references_p (struct cgraph_node *node,
77 void *data ATTRIBUTE_UNUSED)
79 int i;
80 struct ipa_ref *ref;
82 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list,
83 i, ref); i++)
84 if (ref->use == IPA_REF_ADDR)
85 return true;
86 return false;
89 /* Look for all functions inlined to NODE and update their inlined_to pointers
90 to INLINED_TO. */
92 static void
93 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
95 struct cgraph_edge *e;
96 for (e = node->callees; e; e = e->next_callee)
97 if (e->callee->global.inlined_to)
99 e->callee->global.inlined_to = inlined_to;
100 update_inlined_to_pointer (e->callee, inlined_to);
104 /* Add symtab NODE to queue starting at FIRST.
106 The queue is linked via AUX pointers and terminated by pointer to 1.
107 We enqueue nodes at two occasions: when we find them reachable or when we find
108 their bodies needed for further clonning. In the second case we mark them
109 by pointer to 2 after processing so they are re-queue when they become
110 reachable. */
112 static void
113 enqueue_node (symtab_node node, symtab_node *first,
114 struct pointer_set_t *reachable)
116 /* Node is still in queue; do nothing. */
117 if (node->symbol.aux && node->symbol.aux != (void *) 2)
118 return;
119 /* Node was already processed as unreachable, re-enqueue
120 only if it became reachable now. */
121 if (node->symbol.aux == (void *)2 && !pointer_set_contains (reachable, node))
122 return;
123 node->symbol.aux = *first;
124 *first = node;
127 /* Process references. */
129 static void
130 process_references (struct ipa_ref_list *list,
131 symtab_node *first,
132 bool before_inlining_p,
133 struct pointer_set_t *reachable)
135 int i;
136 struct ipa_ref *ref;
137 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
139 symtab_node node = ref->referred;
141 if (node->symbol.definition
142 && ((!DECL_EXTERNAL (node->symbol.decl) || node->symbol.alias)
143 || (before_inlining_p
144 /* We use variable constructors during late complation for
145 constant folding. Keep references alive so partitioning
146 knows about potential references. */
147 || (TREE_CODE (node->symbol.decl) == VAR_DECL
148 && flag_wpa && const_value_known_p (node->symbol.decl)))))
149 pointer_set_insert (reachable, node);
150 enqueue_node ((symtab_node) node, first, reachable);
155 /* Perform reachability analysis and reclaim all unreachable nodes.
157 The algorithm is basically mark&sweep but with some extra refinements:
159 - reachable extern inline functions needs special handling; the bodies needs
160 to stay in memory until inlining in hope that they will be inlined.
161 After inlining we release their bodies and turn them into unanalyzed
162 nodes even when they are reachable.
164 BEFORE_INLINING_P specify whether we are before or after inlining.
166 - virtual functions are kept in callgraph even if they seem unreachable in
167 hope calls to them will be devirtualized.
169 Again we remove them after inlining. In late optimization some
170 devirtualization may happen, but it is not importnat since we won't inline
171 the call. In theory early opts and IPA should work out all important cases.
173 - virtual clones needs bodies of their origins for later materialization;
174 this means that we want to keep the body even if the origin is unreachable
175 otherwise. To avoid origin from sitting in the callgraph and being
176 walked by IPA passes, we turn them into unanalyzed nodes with body
177 defined.
179 We maintain set of function declaration where body needs to stay in
180 body_needed_for_clonning
182 Inline clones represent special case: their declaration match the
183 declaration of origin and cgraph_remove_node already knows how to
184 reshape callgraph and preserve body when offline copy of function or
185 inline clone is being removed.
187 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
188 variables with DECL_INITIAL set. We finalize these and keep reachable
189 ones around for constant folding purposes. After inlining we however
190 stop walking their references to let everything static referneced by them
191 to be removed when it is otherwise unreachable.
193 We maintain queue of both reachable symbols (i.e. defined symbols that needs
194 to stay) and symbols that are in boundary (i.e. external symbols referenced
195 by reachable symbols or origins of clones). The queue is represented
196 as linked list by AUX pointer terminated by 1.
198 A the end we keep all reachable symbols. For symbols in boundary we always
199 turn definition into a declaration, but we may keep function body around
200 based on body_needed_for_clonning
202 All symbols that enter the queue have AUX pointer non-zero and are in the
203 boundary. Pointer set REACHABLE is used to track reachable symbols.
205 Every symbol can be visited twice - once as part of boundary and once
206 as real reachable symbol. enqueue_node needs to decide whether the
207 node needs to be re-queued for second processing. For this purpose
208 we set AUX pointer of processed symbols in the boundary to constant 2. */
210 bool
211 symtab_remove_unreachable_nodes (bool before_inlining_p, FILE *file)
213 symtab_node first = (symtab_node) (void *) 1;
214 struct cgraph_node *node, *next;
215 struct varpool_node *vnode, *vnext;
216 bool changed = false;
217 struct pointer_set_t *reachable = pointer_set_create ();
218 struct pointer_set_t *body_needed_for_clonning = pointer_set_create ();
220 #ifdef ENABLE_CHECKING
221 verify_symtab ();
222 #endif
223 if (file)
224 fprintf (file, "\nReclaiming functions:");
225 #ifdef ENABLE_CHECKING
226 FOR_EACH_FUNCTION (node)
227 gcc_assert (!node->symbol.aux);
228 FOR_EACH_VARIABLE (vnode)
229 gcc_assert (!vnode->symbol.aux);
230 #endif
231 /* Mark functions whose bodies are obviously needed.
232 This is mostly when they can be referenced externally. Inline clones
233 are special since their declarations are shared with master clone and thus
234 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
235 FOR_EACH_DEFINED_FUNCTION (node)
236 if (!node->global.inlined_to
237 && (!cgraph_can_remove_if_no_direct_calls_and_refs_p (node)
238 /* Keep around virtual functions for possible devirtualization. */
239 || (before_inlining_p
240 && DECL_VIRTUAL_P (node->symbol.decl))))
242 gcc_assert (!node->global.inlined_to);
243 pointer_set_insert (reachable, node);
244 enqueue_node ((symtab_node)node, &first, reachable);
246 else
247 gcc_assert (!node->symbol.aux);
249 /* Mark variables that are obviously needed. */
250 FOR_EACH_DEFINED_VARIABLE (vnode)
251 if (!varpool_can_remove_if_no_refs (vnode))
253 pointer_set_insert (reachable, vnode);
254 enqueue_node ((symtab_node)vnode, &first, reachable);
257 /* Perform reachability analysis. */
258 while (first != (symtab_node) (void *) 1)
260 bool in_boundary_p = !pointer_set_contains (reachable, first);
261 symtab_node node = first;
263 first = (symtab_node)first->symbol.aux;
265 /* If we are processing symbol in boundary, mark its AUX pointer for
266 possible later re-processing in enqueue_node. */
267 if (in_boundary_p)
268 node->symbol.aux = (void *)2;
269 else
271 /* If any symbol in a comdat group is reachable, force
272 all other in the same comdat group to be also reachable. */
273 if (node->symbol.same_comdat_group)
275 symtab_node next;
276 for (next = node->symbol.same_comdat_group;
277 next != node;
278 next = next->symbol.same_comdat_group)
279 if (!pointer_set_insert (reachable, next))
280 enqueue_node ((symtab_node) next, &first, reachable);
282 /* Mark references as reachable. */
283 process_references (&node->symbol.ref_list, &first,
284 before_inlining_p, reachable);
287 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
289 /* Mark the callees reachable unless they are direct calls to extern
290 inline functions we decided to not inline. */
291 if (!in_boundary_p)
293 struct cgraph_edge *e;
294 for (e = cnode->callees; e; e = e->next_callee)
296 if (e->callee->symbol.definition
297 && (!e->inline_failed
298 || !DECL_EXTERNAL (e->callee->symbol.decl)
299 || e->callee->symbol.alias
300 || before_inlining_p))
301 pointer_set_insert (reachable, e->callee);
302 enqueue_node ((symtab_node) e->callee, &first, reachable);
305 /* When inline clone exists, mark body to be preserved so when removing
306 offline copy of the function we don't kill it. */
307 if (!cnode->symbol.alias && cnode->global.inlined_to)
308 pointer_set_insert (body_needed_for_clonning, cnode->symbol.decl);
311 /* For non-inline clones, force their origins to the boundary and ensure
312 that body is not removed. */
313 while (cnode->clone_of
314 && !gimple_has_body_p (cnode->symbol.decl))
316 bool noninline = cnode->clone_of->symbol.decl != cnode->symbol.decl;
317 cnode = cnode->clone_of;
318 if (noninline)
320 pointer_set_insert (body_needed_for_clonning, cnode->symbol.decl);
321 enqueue_node ((symtab_node)cnode, &first, reachable);
322 break;
326 /* When we see constructor of external variable, keep referred nodes in the
327 boundary. This will also hold initializers of the external vars NODE
328 refers to. */
329 varpool_node *vnode = dyn_cast <varpool_node> (node);
330 if (vnode
331 && DECL_EXTERNAL (node->symbol.decl)
332 && !vnode->symbol.alias
333 && in_boundary_p)
335 struct ipa_ref *ref;
336 for (int i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
337 enqueue_node (ref->referred, &first, reachable);
341 /* Remove unreachable functions. */
342 for (node = cgraph_first_function (); node; node = next)
344 next = cgraph_next_function (node);
346 /* If node is not needed at all, remove it. */
347 if (!node->symbol.aux)
349 if (file)
350 fprintf (file, " %s", cgraph_node_name (node));
351 cgraph_remove_node (node);
352 changed = true;
354 /* If node is unreachable, remove its body. */
355 else if (!pointer_set_contains (reachable, node))
357 if (!pointer_set_contains (body_needed_for_clonning, node->symbol.decl))
358 cgraph_release_function_body (node);
359 if (node->symbol.definition)
361 if (file)
362 fprintf (file, " %s", cgraph_node_name (node));
363 cgraph_reset_node (node);
364 changed = true;
369 /* Inline clones might be kept around so their materializing allows further
370 cloning. If the function the clone is inlined into is removed, we need
371 to turn it into normal cone. */
372 FOR_EACH_FUNCTION (node)
374 if (node->global.inlined_to
375 && !node->callers)
377 gcc_assert (node->clones);
378 node->global.inlined_to = NULL;
379 update_inlined_to_pointer (node, node);
381 node->symbol.aux = NULL;
384 /* Remove unreachable variables. */
385 if (file)
386 fprintf (file, "\nReclaiming variables:");
387 for (vnode = varpool_first_variable (); vnode; vnode = vnext)
389 vnext = varpool_next_variable (vnode);
390 if (!vnode->symbol.aux
391 /* For can_refer_decl_in_current_unit_p we want to track for
392 all external variables if they are defined in other partition
393 or not. */
394 && (!flag_ltrans || !DECL_EXTERNAL (vnode->symbol.decl)))
396 if (file)
397 fprintf (file, " %s", varpool_node_name (vnode));
398 varpool_remove_node (vnode);
399 changed = true;
401 else if (!pointer_set_contains (reachable, vnode))
403 if (vnode->symbol.definition)
405 if (file)
406 fprintf (file, " %s", varpool_node_name (vnode));
407 changed = true;
409 vnode->symbol.definition = false;
410 vnode->symbol.analyzed = false;
411 vnode->symbol.aux = NULL;
413 /* Keep body if it may be useful for constant folding. */
414 if (!const_value_known_p (vnode->symbol.decl))
415 varpool_remove_initializer (vnode);
416 ipa_remove_all_references (&vnode->symbol.ref_list);
418 else
419 vnode->symbol.aux = NULL;
422 pointer_set_destroy (reachable);
423 pointer_set_destroy (body_needed_for_clonning);
425 /* Now update address_taken flags and try to promote functions to be local. */
426 if (file)
427 fprintf (file, "\nClearing address taken flags:");
428 FOR_EACH_DEFINED_FUNCTION (node)
429 if (node->symbol.address_taken
430 && !node->symbol.used_from_other_partition)
432 if (!cgraph_for_node_and_aliases (node, has_addr_references_p, NULL, true))
434 if (file)
435 fprintf (file, " %s", cgraph_node_name (node));
436 node->symbol.address_taken = false;
437 changed = true;
438 if (cgraph_local_node_p (node))
440 node->local.local = true;
441 if (file)
442 fprintf (file, " (local)");
446 if (file)
447 fprintf (file, "\n");
449 #ifdef ENABLE_CHECKING
450 verify_symtab ();
451 #endif
453 /* If we removed something, perhaps profile could be improved. */
454 if (changed && optimize && inline_edge_summary_vec.exists ())
455 FOR_EACH_DEFINED_FUNCTION (node)
456 cgraph_propagate_frequency (node);
458 return changed;
461 /* Discover variables that have no longer address taken or that are read only
462 and update their flags.
464 FIXME: This can not be done in between gimplify and omp_expand since
465 readonly flag plays role on what is shared and what is not. Currently we do
466 this transformation as part of whole program visibility and re-do at
467 ipa-reference pass (to take into account clonning), but it would
468 make sense to do it before early optimizations. */
470 void
471 ipa_discover_readonly_nonaddressable_vars (void)
473 struct varpool_node *vnode;
474 if (dump_file)
475 fprintf (dump_file, "Clearing variable flags:");
476 FOR_EACH_VARIABLE (vnode)
477 if (vnode->symbol.definition && varpool_all_refs_explicit_p (vnode)
478 && (TREE_ADDRESSABLE (vnode->symbol.decl)
479 || !TREE_READONLY (vnode->symbol.decl)))
481 bool written = false;
482 bool address_taken = false;
483 int i;
484 struct ipa_ref *ref;
485 for (i = 0; ipa_ref_list_referring_iterate (&vnode->symbol.ref_list,
486 i, ref)
487 && (!written || !address_taken); i++)
488 switch (ref->use)
490 case IPA_REF_ADDR:
491 address_taken = true;
492 break;
493 case IPA_REF_LOAD:
494 break;
495 case IPA_REF_STORE:
496 written = true;
497 break;
499 if (TREE_ADDRESSABLE (vnode->symbol.decl) && !address_taken)
501 if (dump_file)
502 fprintf (dump_file, " %s (addressable)", varpool_node_name (vnode));
503 TREE_ADDRESSABLE (vnode->symbol.decl) = 0;
505 if (!TREE_READONLY (vnode->symbol.decl) && !address_taken && !written
506 /* Making variable in explicit section readonly can cause section
507 type conflict.
508 See e.g. gcc.c-torture/compile/pr23237.c */
509 && DECL_SECTION_NAME (vnode->symbol.decl) == NULL)
511 if (dump_file)
512 fprintf (dump_file, " %s (read-only)", varpool_node_name (vnode));
513 TREE_READONLY (vnode->symbol.decl) = 1;
516 if (dump_file)
517 fprintf (dump_file, "\n");
520 /* Return true when there is a reference to node and it is not vtable. */
521 static bool
522 address_taken_from_non_vtable_p (symtab_node node)
524 int i;
525 struct ipa_ref *ref;
526 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list,
527 i, ref); i++)
528 if (ref->use == IPA_REF_ADDR)
530 struct varpool_node *node;
531 if (is_a <cgraph_node> (ref->referring))
532 return true;
533 node = ipa_ref_referring_varpool_node (ref);
534 if (!DECL_VIRTUAL_P (node->symbol.decl))
535 return true;
537 return false;
540 /* A helper for comdat_can_be_unshared_p. */
542 static bool
543 comdat_can_be_unshared_p_1 (symtab_node node)
545 /* When address is taken, we don't know if equality comparison won't
546 break eventaully. Exception are virutal functions and vtables, where
547 this is not possible by language standard. */
548 if (!DECL_VIRTUAL_P (node->symbol.decl)
549 && address_taken_from_non_vtable_p (node))
550 return false;
552 /* If the symbol is used in some weird way, better to not touch it. */
553 if (node->symbol.force_output)
554 return false;
556 /* Explicit instantiations needs to be output when possibly
557 used externally. */
558 if (node->symbol.forced_by_abi
559 && TREE_PUBLIC (node->symbol.decl)
560 && (node->symbol.resolution != LDPR_PREVAILING_DEF_IRONLY
561 && !flag_whole_program))
562 return false;
564 /* Non-readonly and volatile variables can not be duplicated. */
565 if (is_a <varpool_node> (node)
566 && (!TREE_READONLY (node->symbol.decl)
567 || TREE_THIS_VOLATILE (node->symbol.decl)))
568 return false;
569 return true;
572 /* COMDAT functions must be shared only if they have address taken,
573 otherwise we can produce our own private implementation with
574 -fwhole-program.
575 Return true when turning COMDAT functoin static can not lead to wrong
576 code when the resulting object links with a library defining same COMDAT.
578 Virtual functions do have their addresses taken from the vtables,
579 but in C++ there is no way to compare their addresses for equality. */
581 static bool
582 comdat_can_be_unshared_p (symtab_node node)
584 if (!comdat_can_be_unshared_p_1 (node))
585 return false;
586 if (node->symbol.same_comdat_group)
588 symtab_node next;
590 /* If more than one function is in the same COMDAT group, it must
591 be shared even if just one function in the comdat group has
592 address taken. */
593 for (next = node->symbol.same_comdat_group;
594 next != node; next = next->symbol.same_comdat_group)
595 if (!comdat_can_be_unshared_p_1 (next))
596 return false;
598 return true;
601 /* Return true when function NODE should be considered externally visible. */
603 static bool
604 cgraph_externally_visible_p (struct cgraph_node *node,
605 bool whole_program)
607 if (!node->symbol.definition)
608 return false;
609 if (!DECL_COMDAT (node->symbol.decl)
610 && (!TREE_PUBLIC (node->symbol.decl)
611 || DECL_EXTERNAL (node->symbol.decl)))
612 return false;
614 /* Do not try to localize built-in functions yet. One of problems is that we
615 end up mangling their asm for WHOPR that makes it impossible to call them
616 using the implicit built-in declarations anymore. Similarly this enables
617 us to remove them as unreachable before actual calls may appear during
618 expansion or folding. */
619 if (DECL_BUILT_IN (node->symbol.decl))
620 return true;
622 /* If linker counts on us, we must preserve the function. */
623 if (symtab_used_from_object_file_p ((symtab_node) node))
624 return true;
625 if (DECL_PRESERVE_P (node->symbol.decl))
626 return true;
627 if (lookup_attribute ("externally_visible",
628 DECL_ATTRIBUTES (node->symbol.decl)))
629 return true;
630 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
631 && lookup_attribute ("dllexport",
632 DECL_ATTRIBUTES (node->symbol.decl)))
633 return true;
634 if (node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY)
635 return false;
636 /* When doing LTO or whole program, we can bring COMDAT functoins static.
637 This improves code quality and we know we will duplicate them at most twice
638 (in the case that we are not using plugin and link with object file
639 implementing same COMDAT) */
640 if ((in_lto_p || whole_program)
641 && DECL_COMDAT (node->symbol.decl)
642 && comdat_can_be_unshared_p ((symtab_node) node))
643 return false;
645 /* When doing link time optimizations, hidden symbols become local. */
646 if (in_lto_p
647 && (DECL_VISIBILITY (node->symbol.decl) == VISIBILITY_HIDDEN
648 || DECL_VISIBILITY (node->symbol.decl) == VISIBILITY_INTERNAL)
649 /* Be sure that node is defined in IR file, not in other object
650 file. In that case we don't set used_from_other_object_file. */
651 && node->symbol.definition)
653 else if (!whole_program)
654 return true;
656 if (MAIN_NAME_P (DECL_NAME (node->symbol.decl)))
657 return true;
659 return false;
662 /* Return true when variable VNODE should be considered externally visible. */
664 bool
665 varpool_externally_visible_p (struct varpool_node *vnode)
667 /* Do not touch weakrefs; while they are not externally visible,
668 dropping their DECL_EXTERNAL flags confuse most
669 of code handling them. */
670 if (vnode->symbol.alias && DECL_EXTERNAL (vnode->symbol.decl))
671 return true;
673 if (DECL_EXTERNAL (vnode->symbol.decl))
674 return true;
676 if (!DECL_COMDAT (vnode->symbol.decl) && !TREE_PUBLIC (vnode->symbol.decl))
677 return false;
679 /* If linker counts on us, we must preserve the function. */
680 if (symtab_used_from_object_file_p ((symtab_node) vnode))
681 return true;
683 if (DECL_HARD_REGISTER (vnode->symbol.decl))
684 return true;
685 if (DECL_PRESERVE_P (vnode->symbol.decl))
686 return true;
687 if (lookup_attribute ("externally_visible",
688 DECL_ATTRIBUTES (vnode->symbol.decl)))
689 return true;
690 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
691 && lookup_attribute ("dllexport",
692 DECL_ATTRIBUTES (vnode->symbol.decl)))
693 return true;
695 /* See if we have linker information about symbol not being used or
696 if we need to make guess based on the declaration.
698 Even if the linker clams the symbol is unused, never bring internal
699 symbols that are declared by user as used or externally visible.
700 This is needed for i.e. references from asm statements. */
701 if (symtab_used_from_object_file_p ((symtab_node) vnode))
702 return true;
703 if (vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY)
704 return false;
706 /* As a special case, the COMDAT virtual tables can be unshared.
707 In LTO mode turn vtables into static variables. The variable is readonly,
708 so this does not enable more optimization, but referring static var
709 is faster for dynamic linking. Also this match logic hidding vtables
710 from LTO symbol tables. */
711 if ((in_lto_p || flag_whole_program)
712 && DECL_COMDAT (vnode->symbol.decl)
713 && comdat_can_be_unshared_p ((symtab_node) vnode))
714 return false;
716 /* When doing link time optimizations, hidden symbols become local. */
717 if (in_lto_p
718 && (DECL_VISIBILITY (vnode->symbol.decl) == VISIBILITY_HIDDEN
719 || DECL_VISIBILITY (vnode->symbol.decl) == VISIBILITY_INTERNAL)
720 /* Be sure that node is defined in IR file, not in other object
721 file. In that case we don't set used_from_other_object_file. */
722 && vnode->symbol.definition)
724 else if (!flag_whole_program)
725 return true;
727 /* Do not attempt to privatize COMDATS by default.
728 This would break linking with C++ libraries sharing
729 inline definitions.
731 FIXME: We can do so for readonly vars with no address taken and
732 possibly also for vtables since no direct pointer comparsion is done.
733 It might be interesting to do so to reduce linking overhead. */
734 if (DECL_COMDAT (vnode->symbol.decl) || DECL_WEAK (vnode->symbol.decl))
735 return true;
736 return false;
739 /* Mark visibility of all functions.
741 A local function is one whose calls can occur only in the current
742 compilation unit and all its calls are explicit, so we can change
743 its calling convention. We simply mark all static functions whose
744 address is not taken as local.
746 We also change the TREE_PUBLIC flag of all declarations that are public
747 in language point of view but we want to overwrite this default
748 via visibilities for the backend point of view. */
750 static unsigned int
751 function_and_variable_visibility (bool whole_program)
753 struct cgraph_node *node;
754 struct varpool_node *vnode;
756 /* All aliases should be procssed at this point. */
757 gcc_checking_assert (!alias_pairs || !alias_pairs->length());
759 FOR_EACH_FUNCTION (node)
761 int flags = flags_from_decl_or_type (node->symbol.decl);
763 /* Optimize away PURE and CONST constructors and destructors. */
764 if (optimize
765 && (flags & (ECF_CONST | ECF_PURE))
766 && !(flags & ECF_LOOPING_CONST_OR_PURE))
768 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
769 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
772 /* Frontends and alias code marks nodes as needed before parsing is finished.
773 We may end up marking as node external nodes where this flag is meaningless
774 strip it. */
775 if (DECL_EXTERNAL (node->symbol.decl) || !node->symbol.definition)
777 node->symbol.force_output = 0;
778 node->symbol.forced_by_abi = 0;
781 /* C++ FE on lack of COMDAT support create local COMDAT functions
782 (that ought to be shared but can not due to object format
783 limitations). It is necessary to keep the flag to make rest of C++ FE
784 happy. Clear the flag here to avoid confusion in middle-end. */
785 if (DECL_COMDAT (node->symbol.decl) && !TREE_PUBLIC (node->symbol.decl))
786 DECL_COMDAT (node->symbol.decl) = 0;
787 /* For external decls stop tracking same_comdat_group, it doesn't matter
788 what comdat group they are in when they won't be emitted in this TU,
789 and simplifies later passes. */
790 if (node->symbol.same_comdat_group && DECL_EXTERNAL (node->symbol.decl))
792 #ifdef ENABLE_CHECKING
793 symtab_node n;
795 for (n = node->symbol.same_comdat_group;
796 n != (symtab_node)node;
797 n = n->symbol.same_comdat_group)
798 /* If at least one of same comdat group functions is external,
799 all of them have to be, otherwise it is a front-end bug. */
800 gcc_assert (DECL_EXTERNAL (n->symbol.decl));
801 #endif
802 symtab_dissolve_same_comdat_group_list ((symtab_node) node);
804 gcc_assert ((!DECL_WEAK (node->symbol.decl)
805 && !DECL_COMDAT (node->symbol.decl))
806 || TREE_PUBLIC (node->symbol.decl)
807 || DECL_EXTERNAL (node->symbol.decl));
808 if (cgraph_externally_visible_p (node, whole_program))
810 gcc_assert (!node->global.inlined_to);
811 node->symbol.externally_visible = true;
813 else
815 node->symbol.externally_visible = false;
816 node->symbol.forced_by_abi = false;
818 if (!node->symbol.externally_visible && node->symbol.definition
819 && !DECL_EXTERNAL (node->symbol.decl))
821 gcc_assert (whole_program || in_lto_p
822 || !TREE_PUBLIC (node->symbol.decl));
823 node->symbol.unique_name = ((node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
824 || node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
825 && TREE_PUBLIC (node->symbol.decl));
826 symtab_make_decl_local (node->symbol.decl);
827 node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
828 if (node->symbol.same_comdat_group)
829 /* cgraph_externally_visible_p has already checked all other nodes
830 in the group and they will all be made local. We need to
831 dissolve the group at once so that the predicate does not
832 segfault though. */
833 symtab_dissolve_same_comdat_group_list ((symtab_node) node);
836 if (node->thunk.thunk_p
837 && TREE_PUBLIC (node->symbol.decl))
839 struct cgraph_node *decl_node = node;
841 decl_node = cgraph_function_node (decl_node->callees->callee, NULL);
843 /* Thunks have the same visibility as function they are attached to.
844 Make sure the C++ front end set this up properly. */
845 if (DECL_ONE_ONLY (decl_node->symbol.decl))
847 gcc_checking_assert (DECL_COMDAT (node->symbol.decl)
848 == DECL_COMDAT (decl_node->symbol.decl));
849 gcc_checking_assert (DECL_COMDAT_GROUP (node->symbol.decl)
850 == DECL_COMDAT_GROUP (decl_node->symbol.decl));
851 gcc_checking_assert (node->symbol.same_comdat_group);
853 if (DECL_EXTERNAL (decl_node->symbol.decl))
854 DECL_EXTERNAL (node->symbol.decl) = 1;
857 FOR_EACH_DEFINED_FUNCTION (node)
858 node->local.local = cgraph_local_node_p (node);
859 FOR_EACH_VARIABLE (vnode)
861 /* weak flag makes no sense on local variables. */
862 gcc_assert (!DECL_WEAK (vnode->symbol.decl)
863 || TREE_PUBLIC (vnode->symbol.decl)
864 || DECL_EXTERNAL (vnode->symbol.decl));
865 /* In several cases declarations can not be common:
867 - when declaration has initializer
868 - when it is in weak
869 - when it has specific section
870 - when it resides in non-generic address space.
871 - if declaration is local, it will get into .local common section
872 so common flag is not needed. Frontends still produce these in
873 certain cases, such as for:
875 static int a __attribute__ ((common))
877 Canonicalize things here and clear the redundant flag. */
878 if (DECL_COMMON (vnode->symbol.decl)
879 && (!(TREE_PUBLIC (vnode->symbol.decl)
880 || DECL_EXTERNAL (vnode->symbol.decl))
881 || (DECL_INITIAL (vnode->symbol.decl)
882 && DECL_INITIAL (vnode->symbol.decl) != error_mark_node)
883 || DECL_WEAK (vnode->symbol.decl)
884 || DECL_SECTION_NAME (vnode->symbol.decl) != NULL
885 || ! (ADDR_SPACE_GENERIC_P
886 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->symbol.decl))))))
887 DECL_COMMON (vnode->symbol.decl) = 0;
889 FOR_EACH_DEFINED_VARIABLE (vnode)
891 if (!vnode->symbol.definition)
892 continue;
893 if (varpool_externally_visible_p (vnode))
894 vnode->symbol.externally_visible = true;
895 else
897 vnode->symbol.externally_visible = false;
898 vnode->symbol.forced_by_abi = false;
900 if (!vnode->symbol.externally_visible)
902 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->symbol.decl));
903 symtab_make_decl_local (vnode->symbol.decl);
904 vnode->symbol.unique_name = ((vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
905 || vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
906 && TREE_PUBLIC (vnode->symbol.decl));
907 if (vnode->symbol.same_comdat_group)
908 symtab_dissolve_same_comdat_group_list ((symtab_node) vnode);
909 vnode->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
913 if (dump_file)
915 fprintf (dump_file, "\nMarking local functions:");
916 FOR_EACH_DEFINED_FUNCTION (node)
917 if (node->local.local)
918 fprintf (dump_file, " %s", cgraph_node_name (node));
919 fprintf (dump_file, "\n\n");
920 fprintf (dump_file, "\nMarking externally visible functions:");
921 FOR_EACH_DEFINED_FUNCTION (node)
922 if (node->symbol.externally_visible)
923 fprintf (dump_file, " %s", cgraph_node_name (node));
924 fprintf (dump_file, "\n\n");
925 fprintf (dump_file, "\nMarking externally visible variables:");
926 FOR_EACH_DEFINED_VARIABLE (vnode)
927 if (vnode->symbol.externally_visible)
928 fprintf (dump_file, " %s", varpool_node_name (vnode));
929 fprintf (dump_file, "\n\n");
931 cgraph_function_flags_ready = true;
932 return 0;
935 /* Local function pass handling visibilities. This happens before LTO streaming
936 so in particular -fwhole-program should be ignored at this level. */
938 static unsigned int
939 local_function_and_variable_visibility (void)
941 return function_and_variable_visibility (flag_whole_program && !flag_lto);
944 struct simple_ipa_opt_pass pass_ipa_function_and_variable_visibility =
947 SIMPLE_IPA_PASS,
948 "visibility", /* name */
949 OPTGROUP_NONE, /* optinfo_flags */
950 NULL, /* gate */
951 local_function_and_variable_visibility,/* execute */
952 NULL, /* sub */
953 NULL, /* next */
954 0, /* static_pass_number */
955 TV_CGRAPHOPT, /* tv_id */
956 0, /* properties_required */
957 0, /* properties_provided */
958 0, /* properties_destroyed */
959 0, /* todo_flags_start */
960 TODO_remove_functions | TODO_dump_symtab /* todo_flags_finish */
964 /* Free inline summary. */
966 static unsigned
967 free_inline_summary (void)
969 inline_free_summary ();
970 return 0;
973 struct simple_ipa_opt_pass pass_ipa_free_inline_summary =
976 SIMPLE_IPA_PASS,
977 "*free_inline_summary", /* name */
978 OPTGROUP_NONE, /* optinfo_flags */
979 NULL, /* gate */
980 free_inline_summary, /* execute */
981 NULL, /* sub */
982 NULL, /* next */
983 0, /* static_pass_number */
984 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
985 0, /* properties_required */
986 0, /* properties_provided */
987 0, /* properties_destroyed */
988 0, /* todo_flags_start */
989 0 /* todo_flags_finish */
993 /* Do not re-run on ltrans stage. */
995 static bool
996 gate_whole_program_function_and_variable_visibility (void)
998 return !flag_ltrans;
1001 /* Bring functionss local at LTO time with -fwhole-program. */
1003 static unsigned int
1004 whole_program_function_and_variable_visibility (void)
1006 function_and_variable_visibility (flag_whole_program);
1007 if (optimize)
1008 ipa_discover_readonly_nonaddressable_vars ();
1009 return 0;
1012 struct ipa_opt_pass_d pass_ipa_whole_program_visibility =
1015 IPA_PASS,
1016 "whole-program", /* name */
1017 OPTGROUP_NONE, /* optinfo_flags */
1018 gate_whole_program_function_and_variable_visibility,/* gate */
1019 whole_program_function_and_variable_visibility,/* execute */
1020 NULL, /* sub */
1021 NULL, /* next */
1022 0, /* static_pass_number */
1023 TV_CGRAPHOPT, /* tv_id */
1024 0, /* properties_required */
1025 0, /* properties_provided */
1026 0, /* properties_destroyed */
1027 0, /* todo_flags_start */
1028 TODO_remove_functions | TODO_dump_symtab /* todo_flags_finish */
1030 NULL, /* generate_summary */
1031 NULL, /* write_summary */
1032 NULL, /* read_summary */
1033 NULL, /* write_optimization_summary */
1034 NULL, /* read_optimization_summary */
1035 NULL, /* stmt_fixup */
1036 0, /* TODOs */
1037 NULL, /* function_transform */
1038 NULL, /* variable_transform */
1041 /* Entry in the histogram. */
1043 struct histogram_entry
1045 gcov_type count;
1046 int time;
1047 int size;
1050 /* Histogram of profile values.
1051 The histogram is represented as an ordered vector of entries allocated via
1052 histogram_pool. During construction a separate hashtable is kept to lookup
1053 duplicate entries. */
1055 vec<histogram_entry *> histogram;
1056 static alloc_pool histogram_pool;
1058 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1060 struct histogram_hash : typed_noop_remove <histogram_entry>
1062 typedef histogram_entry value_type;
1063 typedef histogram_entry compare_type;
1064 static inline hashval_t hash (const value_type *);
1065 static inline int equal (const value_type *, const compare_type *);
1068 inline hashval_t
1069 histogram_hash::hash (const histogram_entry *val)
1071 return val->count;
1074 inline int
1075 histogram_hash::equal (const histogram_entry *val, const histogram_entry *val2)
1077 return val->count == val2->count;
1080 /* Account TIME and SIZE executed COUNT times into HISTOGRAM.
1081 HASHTABLE is the on-side hash kept to avoid duplicates. */
1083 static void
1084 account_time_size (hash_table <histogram_hash> hashtable,
1085 vec<histogram_entry *> &histogram,
1086 gcov_type count, int time, int size)
1088 histogram_entry key = {count, 0, 0};
1089 histogram_entry **val = hashtable.find_slot (&key, INSERT);
1091 if (!*val)
1093 *val = (histogram_entry *) pool_alloc (histogram_pool);
1094 **val = key;
1095 histogram.safe_push (*val);
1097 (*val)->time += time;
1098 (*val)->size += size;
1102 cmp_counts (const void *v1, const void *v2)
1104 const histogram_entry *h1 = *(const histogram_entry * const *)v1;
1105 const histogram_entry *h2 = *(const histogram_entry * const *)v2;
1106 if (h1->count < h2->count)
1107 return 1;
1108 if (h1->count > h2->count)
1109 return -1;
1110 return 0;
1113 /* Dump HISTOGRAM to FILE. */
1115 static void
1116 dump_histogram (FILE *file, vec<histogram_entry *> histogram)
1118 unsigned int i;
1119 gcov_type overall_time = 0, cumulated_time = 0, cumulated_size = 0, overall_size = 0;
1121 fprintf (dump_file, "Histogram:\n");
1122 for (i = 0; i < histogram.length (); i++)
1124 overall_time += histogram[i]->count * histogram[i]->time;
1125 overall_size += histogram[i]->size;
1127 if (!overall_time)
1128 overall_time = 1;
1129 if (!overall_size)
1130 overall_size = 1;
1131 for (i = 0; i < histogram.length (); i++)
1133 cumulated_time += histogram[i]->count * histogram[i]->time;
1134 cumulated_size += histogram[i]->size;
1135 fprintf (file, " "HOST_WIDEST_INT_PRINT_DEC": time:%i (%2.2f) size:%i (%2.2f)\n",
1136 (HOST_WIDEST_INT) histogram[i]->count,
1137 histogram[i]->time,
1138 cumulated_time * 100.0 / overall_time,
1139 histogram[i]->size,
1140 cumulated_size * 100.0 / overall_size);
1144 /* Collect histogram from CFG profiles. */
1146 static void
1147 ipa_profile_generate_summary (void)
1149 struct cgraph_node *node;
1150 gimple_stmt_iterator gsi;
1151 hash_table <histogram_hash> hashtable;
1152 basic_block bb;
1154 hashtable.create (10);
1155 histogram_pool = create_alloc_pool ("IPA histogram", sizeof (struct histogram_entry),
1156 10);
1158 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
1159 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (node->symbol.decl))
1161 int time = 0;
1162 int size = 0;
1163 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1165 time += estimate_num_insns (gsi_stmt (gsi), &eni_time_weights);
1166 size += estimate_num_insns (gsi_stmt (gsi), &eni_size_weights);
1168 account_time_size (hashtable, histogram, bb->count, time, size);
1170 hashtable.dispose ();
1171 histogram.qsort (cmp_counts);
1174 /* Serialize the ipa info for lto. */
1176 static void
1177 ipa_profile_write_summary (void)
1179 struct lto_simple_output_block *ob
1180 = lto_create_simple_output_block (LTO_section_ipa_profile);
1181 unsigned int i;
1183 streamer_write_uhwi_stream (ob->main_stream, histogram.length());
1184 for (i = 0; i < histogram.length (); i++)
1186 streamer_write_gcov_count_stream (ob->main_stream, histogram[i]->count);
1187 streamer_write_uhwi_stream (ob->main_stream, histogram[i]->time);
1188 streamer_write_uhwi_stream (ob->main_stream, histogram[i]->size);
1190 lto_destroy_simple_output_block (ob);
1193 /* Deserialize the ipa info for lto. */
1195 static void
1196 ipa_profile_read_summary (void)
1198 struct lto_file_decl_data ** file_data_vec
1199 = lto_get_file_decl_data ();
1200 struct lto_file_decl_data * file_data;
1201 hash_table <histogram_hash> hashtable;
1202 int j = 0;
1204 hashtable.create (10);
1205 histogram_pool = create_alloc_pool ("IPA histogram", sizeof (struct histogram_entry),
1206 10);
1208 while ((file_data = file_data_vec[j++]))
1210 const char *data;
1211 size_t len;
1212 struct lto_input_block *ib
1213 = lto_create_simple_input_block (file_data,
1214 LTO_section_ipa_profile,
1215 &data, &len);
1216 if (ib)
1218 unsigned int num = streamer_read_uhwi (ib);
1219 unsigned int n;
1220 for (n = 0; n < num; n++)
1222 gcov_type count = streamer_read_gcov_count (ib);
1223 int time = streamer_read_uhwi (ib);
1224 int size = streamer_read_uhwi (ib);
1225 account_time_size (hashtable, histogram,
1226 count, time, size);
1228 lto_destroy_simple_input_block (file_data,
1229 LTO_section_ipa_profile,
1230 ib, data, len);
1233 hashtable.dispose ();
1234 histogram.qsort (cmp_counts);
1237 /* Simple ipa profile pass propagating frequencies across the callgraph. */
1239 static unsigned int
1240 ipa_profile (void)
1242 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1243 struct cgraph_edge *e;
1244 int order_pos;
1245 bool something_changed = false;
1246 int i;
1247 gcov_type overall_time = 0, cutoff = 0, cumulated = 0, overall_size = 0;
1249 if (dump_file)
1250 dump_histogram (dump_file, histogram);
1251 for (i = 0; i < (int)histogram.length (); i++)
1253 overall_time += histogram[i]->count * histogram[i]->time;
1254 overall_size += histogram[i]->size;
1256 if (overall_time)
1258 gcov_type threshold;
1260 gcc_assert (overall_size);
1261 if (dump_file)
1263 gcov_type min, cumulated_time = 0, cumulated_size = 0;
1265 fprintf (dump_file, "Overall time: "HOST_WIDEST_INT_PRINT_DEC"\n",
1266 (HOST_WIDEST_INT)overall_time);
1267 min = get_hot_bb_threshold ();
1268 for (i = 0; i < (int)histogram.length () && histogram[i]->count >= min;
1269 i++)
1271 cumulated_time += histogram[i]->count * histogram[i]->time;
1272 cumulated_size += histogram[i]->size;
1274 fprintf (dump_file, "GCOV min count: "HOST_WIDEST_INT_PRINT_DEC
1275 " Time:%3.2f%% Size:%3.2f%%\n",
1276 (HOST_WIDEST_INT)min,
1277 cumulated_time * 100.0 / overall_time,
1278 cumulated_size * 100.0 / overall_size);
1280 cutoff = (overall_time * PARAM_VALUE (HOT_BB_COUNT_WS_PERMILLE) + 500) / 1000;
1281 threshold = 0;
1282 for (i = 0; cumulated < cutoff; i++)
1284 cumulated += histogram[i]->count * histogram[i]->time;
1285 threshold = histogram[i]->count;
1287 if (!threshold)
1288 threshold = 1;
1289 if (dump_file)
1291 gcov_type cumulated_time = 0, cumulated_size = 0;
1293 for (i = 0;
1294 i < (int)histogram.length () && histogram[i]->count >= threshold;
1295 i++)
1297 cumulated_time += histogram[i]->count * histogram[i]->time;
1298 cumulated_size += histogram[i]->size;
1300 fprintf (dump_file, "Determined min count: "HOST_WIDEST_INT_PRINT_DEC
1301 " Time:%3.2f%% Size:%3.2f%%\n",
1302 (HOST_WIDEST_INT)threshold,
1303 cumulated_time * 100.0 / overall_time,
1304 cumulated_size * 100.0 / overall_size);
1306 if (threshold > get_hot_bb_threshold ()
1307 || in_lto_p)
1309 if (dump_file)
1310 fprintf (dump_file, "Threshold updated.\n");
1311 set_hot_bb_threshold (threshold);
1314 histogram.release();
1315 free_alloc_pool (histogram_pool);
1317 order_pos = ipa_reverse_postorder (order);
1318 for (i = order_pos - 1; i >= 0; i--)
1320 if (order[i]->local.local && cgraph_propagate_frequency (order[i]))
1322 for (e = order[i]->callees; e; e = e->next_callee)
1323 if (e->callee->local.local && !e->callee->symbol.aux)
1325 something_changed = true;
1326 e->callee->symbol.aux = (void *)1;
1329 order[i]->symbol.aux = NULL;
1332 while (something_changed)
1334 something_changed = false;
1335 for (i = order_pos - 1; i >= 0; i--)
1337 if (order[i]->symbol.aux && cgraph_propagate_frequency (order[i]))
1339 for (e = order[i]->callees; e; e = e->next_callee)
1340 if (e->callee->local.local && !e->callee->symbol.aux)
1342 something_changed = true;
1343 e->callee->symbol.aux = (void *)1;
1346 order[i]->symbol.aux = NULL;
1349 free (order);
1350 return 0;
1353 static bool
1354 gate_ipa_profile (void)
1356 return flag_ipa_profile;
1359 struct ipa_opt_pass_d pass_ipa_profile =
1362 IPA_PASS,
1363 "profile_estimate", /* name */
1364 OPTGROUP_NONE, /* optinfo_flags */
1365 gate_ipa_profile, /* gate */
1366 ipa_profile, /* execute */
1367 NULL, /* sub */
1368 NULL, /* next */
1369 0, /* static_pass_number */
1370 TV_IPA_PROFILE, /* tv_id */
1371 0, /* properties_required */
1372 0, /* properties_provided */
1373 0, /* properties_destroyed */
1374 0, /* todo_flags_start */
1375 0 /* todo_flags_finish */
1377 ipa_profile_generate_summary, /* generate_summary */
1378 ipa_profile_write_summary, /* write_summary */
1379 ipa_profile_read_summary, /* read_summary */
1380 NULL, /* write_optimization_summary */
1381 NULL, /* read_optimization_summary */
1382 NULL, /* stmt_fixup */
1383 0, /* TODOs */
1384 NULL, /* function_transform */
1385 NULL /* variable_transform */
1388 /* Generate and emit a static constructor or destructor. WHICH must
1389 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1390 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1391 initialization priority for this constructor or destructor.
1393 FINAL specify whether the externally visible name for collect2 should
1394 be produced. */
1396 static void
1397 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
1399 static int counter = 0;
1400 char which_buf[16];
1401 tree decl, name, resdecl;
1403 /* The priority is encoded in the constructor or destructor name.
1404 collect2 will sort the names and arrange that they are called at
1405 program startup. */
1406 if (final)
1407 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1408 else
1409 /* Proudce sane name but one not recognizable by collect2, just for the
1410 case we fail to inline the function. */
1411 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
1412 name = get_file_function_name (which_buf);
1414 decl = build_decl (input_location, FUNCTION_DECL, name,
1415 build_function_type_list (void_type_node, NULL_TREE));
1416 current_function_decl = decl;
1418 resdecl = build_decl (input_location,
1419 RESULT_DECL, NULL_TREE, void_type_node);
1420 DECL_ARTIFICIAL (resdecl) = 1;
1421 DECL_RESULT (decl) = resdecl;
1422 DECL_CONTEXT (resdecl) = decl;
1424 allocate_struct_function (decl, false);
1426 TREE_STATIC (decl) = 1;
1427 TREE_USED (decl) = 1;
1428 DECL_ARTIFICIAL (decl) = 1;
1429 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1430 DECL_SAVED_TREE (decl) = body;
1431 if (!targetm.have_ctors_dtors && final)
1433 TREE_PUBLIC (decl) = 1;
1434 DECL_PRESERVE_P (decl) = 1;
1436 DECL_UNINLINABLE (decl) = 1;
1438 DECL_INITIAL (decl) = make_node (BLOCK);
1439 TREE_USED (DECL_INITIAL (decl)) = 1;
1441 DECL_SOURCE_LOCATION (decl) = input_location;
1442 cfun->function_end_locus = input_location;
1444 switch (which)
1446 case 'I':
1447 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1448 decl_init_priority_insert (decl, priority);
1449 break;
1450 case 'D':
1451 DECL_STATIC_DESTRUCTOR (decl) = 1;
1452 decl_fini_priority_insert (decl, priority);
1453 break;
1454 default:
1455 gcc_unreachable ();
1458 gimplify_function_tree (decl);
1460 cgraph_add_new_function (decl, false);
1462 set_cfun (NULL);
1463 current_function_decl = NULL;
1466 /* Generate and emit a static constructor or destructor. WHICH must
1467 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1468 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1469 initialization priority for this constructor or destructor. */
1471 void
1472 cgraph_build_static_cdtor (char which, tree body, int priority)
1474 cgraph_build_static_cdtor_1 (which, body, priority, false);
1477 /* A vector of FUNCTION_DECLs declared as static constructors. */
1478 static vec<tree> static_ctors;
1479 /* A vector of FUNCTION_DECLs declared as static destructors. */
1480 static vec<tree> static_dtors;
1482 /* When target does not have ctors and dtors, we call all constructor
1483 and destructor by special initialization/destruction function
1484 recognized by collect2.
1486 When we are going to build this function, collect all constructors and
1487 destructors and turn them into normal functions. */
1489 static void
1490 record_cdtor_fn (struct cgraph_node *node)
1492 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1493 static_ctors.safe_push (node->symbol.decl);
1494 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1495 static_dtors.safe_push (node->symbol.decl);
1496 node = cgraph_get_node (node->symbol.decl);
1497 DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl) = 1;
1500 /* Define global constructors/destructor functions for the CDTORS, of
1501 which they are LEN. The CDTORS are sorted by initialization
1502 priority. If CTOR_P is true, these are constructors; otherwise,
1503 they are destructors. */
1505 static void
1506 build_cdtor (bool ctor_p, vec<tree> cdtors)
1508 size_t i,j;
1509 size_t len = cdtors.length ();
1511 i = 0;
1512 while (i < len)
1514 tree body;
1515 tree fn;
1516 priority_type priority;
1518 priority = 0;
1519 body = NULL_TREE;
1520 j = i;
1523 priority_type p;
1524 fn = cdtors[j];
1525 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1526 if (j == i)
1527 priority = p;
1528 else if (p != priority)
1529 break;
1530 j++;
1532 while (j < len);
1534 /* When there is only one cdtor and target supports them, do nothing. */
1535 if (j == i + 1
1536 && targetm.have_ctors_dtors)
1538 i++;
1539 continue;
1541 /* Find the next batch of constructors/destructors with the same
1542 initialization priority. */
1543 for (;i < j; i++)
1545 tree call;
1546 fn = cdtors[i];
1547 call = build_call_expr (fn, 0);
1548 if (ctor_p)
1549 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1550 else
1551 DECL_STATIC_DESTRUCTOR (fn) = 0;
1552 /* We do not want to optimize away pure/const calls here.
1553 When optimizing, these should be already removed, when not
1554 optimizing, we want user to be able to breakpoint in them. */
1555 TREE_SIDE_EFFECTS (call) = 1;
1556 append_to_statement_list (call, &body);
1558 gcc_assert (body != NULL_TREE);
1559 /* Generate a function to call all the function of like
1560 priority. */
1561 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1565 /* Comparison function for qsort. P1 and P2 are actually of type
1566 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1567 used to determine the sort order. */
1569 static int
1570 compare_ctor (const void *p1, const void *p2)
1572 tree f1;
1573 tree f2;
1574 int priority1;
1575 int priority2;
1577 f1 = *(const tree *)p1;
1578 f2 = *(const tree *)p2;
1579 priority1 = DECL_INIT_PRIORITY (f1);
1580 priority2 = DECL_INIT_PRIORITY (f2);
1582 if (priority1 < priority2)
1583 return -1;
1584 else if (priority1 > priority2)
1585 return 1;
1586 else
1587 /* Ensure a stable sort. Constructors are executed in backwarding
1588 order to make LTO initialize braries first. */
1589 return DECL_UID (f2) - DECL_UID (f1);
1592 /* Comparison function for qsort. P1 and P2 are actually of type
1593 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1594 used to determine the sort order. */
1596 static int
1597 compare_dtor (const void *p1, const void *p2)
1599 tree f1;
1600 tree f2;
1601 int priority1;
1602 int priority2;
1604 f1 = *(const tree *)p1;
1605 f2 = *(const tree *)p2;
1606 priority1 = DECL_FINI_PRIORITY (f1);
1607 priority2 = DECL_FINI_PRIORITY (f2);
1609 if (priority1 < priority2)
1610 return -1;
1611 else if (priority1 > priority2)
1612 return 1;
1613 else
1614 /* Ensure a stable sort. */
1615 return DECL_UID (f1) - DECL_UID (f2);
1618 /* Generate functions to call static constructors and destructors
1619 for targets that do not support .ctors/.dtors sections. These
1620 functions have magic names which are detected by collect2. */
1622 static void
1623 build_cdtor_fns (void)
1625 if (!static_ctors.is_empty ())
1627 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1628 static_ctors.qsort (compare_ctor);
1629 build_cdtor (/*ctor_p=*/true, static_ctors);
1632 if (!static_dtors.is_empty ())
1634 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1635 static_dtors.qsort (compare_dtor);
1636 build_cdtor (/*ctor_p=*/false, static_dtors);
1640 /* Look for constructors and destructors and produce function calling them.
1641 This is needed for targets not supporting ctors or dtors, but we perform the
1642 transformation also at linktime to merge possibly numerous
1643 constructors/destructors into single function to improve code locality and
1644 reduce size. */
1646 static unsigned int
1647 ipa_cdtor_merge (void)
1649 struct cgraph_node *node;
1650 FOR_EACH_DEFINED_FUNCTION (node)
1651 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
1652 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1653 record_cdtor_fn (node);
1654 build_cdtor_fns ();
1655 static_ctors.release ();
1656 static_dtors.release ();
1657 return 0;
1660 /* Perform the pass when we have no ctors/dtors support
1661 or at LTO time to merge multiple constructors into single
1662 function. */
1664 static bool
1665 gate_ipa_cdtor_merge (void)
1667 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1670 struct ipa_opt_pass_d pass_ipa_cdtor_merge =
1673 IPA_PASS,
1674 "cdtor", /* name */
1675 OPTGROUP_NONE, /* optinfo_flags */
1676 gate_ipa_cdtor_merge, /* gate */
1677 ipa_cdtor_merge, /* execute */
1678 NULL, /* sub */
1679 NULL, /* next */
1680 0, /* static_pass_number */
1681 TV_CGRAPHOPT, /* tv_id */
1682 0, /* properties_required */
1683 0, /* properties_provided */
1684 0, /* properties_destroyed */
1685 0, /* todo_flags_start */
1686 0 /* todo_flags_finish */
1688 NULL, /* generate_summary */
1689 NULL, /* write_summary */
1690 NULL, /* read_summary */
1691 NULL, /* write_optimization_summary */
1692 NULL, /* read_optimization_summary */
1693 NULL, /* stmt_fixup */
1694 0, /* TODOs */
1695 NULL, /* function_transform */
1696 NULL /* variable_transform */