cgraph.h (FOR_EACH_VARIABLE, [...]): New macros.
[gcc.git] / gcc / ipa.c
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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"
35
36 /* Look for all functions inlined to NODE and update their inlined_to pointers
37 to INLINED_TO. */
38
39 static void
40 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
41 {
42 struct cgraph_edge *e;
43 for (e = node->callees; e; e = e->next_callee)
44 if (e->callee->global.inlined_to)
45 {
46 e->callee->global.inlined_to = inlined_to;
47 update_inlined_to_pointer (e->callee, inlined_to);
48 }
49 }
50
51 /* Add cgraph NODE to queue starting at FIRST.
52
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. */
58
59 static void
60 enqueue_cgraph_node (struct cgraph_node *node, struct cgraph_node **first)
61 {
62 /* Node is still in queue; do nothing. */
63 if (node->symbol.aux && node->symbol.aux != (void *) 2)
64 return;
65 /* Node was already processed as unreachable, re-enqueue
66 only if it became reachable now. */
67 if (node->symbol.aux == (void *)2 && !node->reachable)
68 return;
69 node->symbol.aux = *first;
70 *first = node;
71 }
72
73 /* Add varpool NODE to queue starting at FIRST. */
74
75 static void
76 enqueue_varpool_node (struct varpool_node *node, struct varpool_node **first)
77 {
78 node->symbol.aux = *first;
79 *first = node;
80 }
81
82 /* Process references. */
83
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)
89 {
90 int i;
91 struct ipa_ref *ref;
92 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
93 {
94 if (ref->refered_type == IPA_REF_CGRAPH)
95 {
96 struct cgraph_node *node = ipa_ref_node (ref);
97 if (!node->reachable
98 && node->analyzed
99 && (!DECL_EXTERNAL (node->symbol.decl)
100 || before_inlining_p))
101 node->reachable = true;
102 enqueue_cgraph_node (node, first);
103 }
104 else
105 {
106 struct varpool_node *node = ipa_ref_varpool_node (ref);
107 if (!node->needed)
108 {
109 varpool_mark_needed_node (node);
110 enqueue_varpool_node (node, first_varpool);
111 }
112 }
113 }
114 }
115
116
117 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
118
119 static bool
120 cgraph_non_local_node_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
121 {
122 /* FIXME: Aliases can be local, but i386 gets thunks wrong then. */
123 return !(cgraph_only_called_directly_or_aliased_p (node)
124 && !ipa_ref_has_aliases_p (&node->symbol.ref_list)
125 && node->analyzed
126 && !DECL_EXTERNAL (node->symbol.decl)
127 && !node->symbol.externally_visible
128 && !node->symbol.used_from_other_partition
129 && !node->symbol.in_other_partition);
130 }
131
132 /* Return true when function can be marked local. */
133
134 static bool
135 cgraph_local_node_p (struct cgraph_node *node)
136 {
137 struct cgraph_node *n = cgraph_function_or_thunk_node (node, NULL);
138
139 /* FIXME: thunks can be considered local, but we need prevent i386
140 from attempting to change calling convention of them. */
141 if (n->thunk.thunk_p)
142 return false;
143 return !cgraph_for_node_and_aliases (n,
144 cgraph_non_local_node_p_1, NULL, true);
145
146 }
147
148 /* Return true when NODE has ADDR reference. */
149
150 static bool
151 has_addr_references_p (struct cgraph_node *node,
152 void *data ATTRIBUTE_UNUSED)
153 {
154 int i;
155 struct ipa_ref *ref;
156
157 for (i = 0; ipa_ref_list_refering_iterate (&node->symbol.ref_list,
158 i, ref); i++)
159 if (ref->use == IPA_REF_ADDR)
160 return true;
161 return false;
162 }
163
164 /* Perform reachability analysis and reclaim all unreachable nodes.
165 If BEFORE_INLINING_P is true this function is called before inlining
166 decisions has been made. If BEFORE_INLINING_P is false this function also
167 removes unneeded bodies of extern inline functions. */
168
169 bool
170 cgraph_remove_unreachable_nodes (bool before_inlining_p, FILE *file)
171 {
172 struct cgraph_node *first = (struct cgraph_node *) (void *) 1;
173 struct varpool_node *first_varpool = (struct varpool_node *) (void *) 1;
174 struct cgraph_node *node, *next;
175 struct varpool_node *vnode, *vnext;
176 bool changed = false;
177
178 #ifdef ENABLE_CHECKING
179 verify_cgraph ();
180 #endif
181 if (file)
182 fprintf (file, "\nReclaiming functions:");
183 #ifdef ENABLE_CHECKING
184 FOR_EACH_FUNCTION (node)
185 gcc_assert (!node->symbol.aux);
186 FOR_EACH_VARIABLE (vnode)
187 gcc_assert (!vnode->symbol.aux);
188 #endif
189 varpool_reset_queue ();
190 /* Mark functions whose bodies are obviously needed.
191 This is mostly when they can be referenced externally. Inline clones
192 are special since their declarations are shared with master clone and thus
193 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
194 FOR_EACH_FUNCTION (node)
195 if (node->analyzed && !node->global.inlined_to
196 && (!cgraph_can_remove_if_no_direct_calls_and_refs_p (node)
197 /* Keep around virtual functions for possible devirtualization. */
198 || (before_inlining_p
199 && DECL_VIRTUAL_P (node->symbol.decl)
200 && (DECL_COMDAT (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl)))))
201 {
202 gcc_assert (!node->global.inlined_to);
203 enqueue_cgraph_node (node, &first);
204 node->reachable = true;
205 }
206 else
207 {
208 gcc_assert (!node->symbol.aux);
209 node->reachable = false;
210 }
211
212 /* Mark variables that are obviously needed. */
213 FOR_EACH_VARIABLE (vnode)
214 {
215 vnode->next_needed = NULL;
216 vnode->prev_needed = NULL;
217 if ((vnode->analyzed || vnode->force_output)
218 && !varpool_can_remove_if_no_refs (vnode))
219 {
220 vnode->needed = false;
221 varpool_mark_needed_node (vnode);
222 enqueue_varpool_node (vnode, &first_varpool);
223 }
224 else
225 vnode->needed = false;
226 }
227
228 /* Perform reachability analysis. As a special case do not consider
229 extern inline functions not inlined as live because we won't output
230 them at all.
231
232 We maintain two worklist, one for cgraph nodes other for varpools and
233 are finished once both are empty. */
234
235 while (first != (struct cgraph_node *) (void *) 1
236 || first_varpool != (struct varpool_node *) (void *) 1)
237 {
238 if (first != (struct cgraph_node *) (void *) 1)
239 {
240 struct cgraph_edge *e;
241 node = first;
242 first = (struct cgraph_node *) first->symbol.aux;
243 if (!node->reachable)
244 node->symbol.aux = (void *)2;
245
246 /* If we found this node reachable, first mark on the callees
247 reachable too, unless they are direct calls to extern inline functions
248 we decided to not inline. */
249 if (node->reachable)
250 {
251 for (e = node->callees; e; e = e->next_callee)
252 {
253 if (!e->callee->reachable
254 && node->analyzed
255 && (!e->inline_failed
256 || !DECL_EXTERNAL (e->callee->symbol.decl)
257 || before_inlining_p))
258 e->callee->reachable = true;
259 enqueue_cgraph_node (e->callee, &first);
260 }
261 process_references (&node->symbol.ref_list, &first,
262 &first_varpool, before_inlining_p);
263 }
264
265 /* If any function in a comdat group is reachable, force
266 all other functions in the same comdat group to be
267 also reachable. */
268 if (node->symbol.same_comdat_group
269 && node->reachable
270 && !node->global.inlined_to)
271 {
272 for (next = cgraph (node->symbol.same_comdat_group);
273 next != node;
274 next = cgraph (next->symbol.same_comdat_group))
275 if (!next->reachable)
276 {
277 next->reachable = true;
278 enqueue_cgraph_node (next, &first);
279 }
280 }
281
282 /* We can freely remove inline clones even if they are cloned, however if
283 function is clone of real clone, we must keep it around in order to
284 make materialize_clones produce function body with the changes
285 applied. */
286 while (node->clone_of && !node->clone_of->symbol.aux
287 && !gimple_has_body_p (node->symbol.decl))
288 {
289 bool noninline = node->clone_of->symbol.decl != node->symbol.decl;
290 node = node->clone_of;
291 if (noninline && !node->reachable && !node->symbol.aux)
292 {
293 enqueue_cgraph_node (node, &first);
294 break;
295 }
296 }
297 }
298 if (first_varpool != (struct varpool_node *) (void *) 1)
299 {
300 vnode = first_varpool;
301 first_varpool = (struct varpool_node *)first_varpool->symbol.aux;
302 vnode->symbol.aux = NULL;
303 process_references (&vnode->symbol.ref_list, &first,
304 &first_varpool, before_inlining_p);
305 /* If any function in a comdat group is reachable, force
306 all other functions in the same comdat group to be
307 also reachable. */
308 if (vnode->symbol.same_comdat_group)
309 {
310 struct varpool_node *next;
311 for (next = varpool (vnode->symbol.same_comdat_group);
312 next != vnode;
313 next = varpool (next->symbol.same_comdat_group))
314 if (!next->needed)
315 {
316 varpool_mark_needed_node (next);
317 enqueue_varpool_node (next, &first_varpool);
318 }
319 }
320 }
321 }
322
323 /* Remove unreachable nodes.
324
325 Completely unreachable functions can be fully removed from the callgraph.
326 Extern inline functions that we decided to not inline need to become unanalyzed nodes of
327 callgraph (so we still have edges to them). We remove function body then.
328
329 Also we need to care functions that are unreachable but we need to keep them around
330 for later clonning. In this case we also turn them to unanalyzed nodes, but
331 keep the body around. */
332 for (node = cgraph_nodes; node; node = next)
333 {
334 next = node->next;
335 if (node->symbol.aux && !node->reachable)
336 {
337 cgraph_node_remove_callees (node);
338 ipa_remove_all_references (&node->symbol.ref_list);
339 node->analyzed = false;
340 }
341 if (!node->symbol.aux)
342 {
343 struct cgraph_edge *e;
344 bool found = false;
345 int i;
346 struct ipa_ref *ref;
347
348 node->global.inlined_to = NULL;
349 if (file)
350 fprintf (file, " %s", cgraph_node_name (node));
351 /* See if there is reachable caller. */
352 for (e = node->callers; e && !found; e = e->next_caller)
353 if (e->caller->reachable)
354 found = true;
355 for (i = 0; (ipa_ref_list_refering_iterate (&node->symbol.ref_list,
356 i, ref)
357 && !found); i++)
358 if (ref->refering_type == IPA_REF_CGRAPH
359 && ipa_ref_refering_node (ref)->reachable)
360 found = true;
361 else if (ref->refering_type == IPA_REF_VARPOOL
362 && ipa_ref_refering_varpool_node (ref)->needed)
363 found = true;
364
365 /* If so, we need to keep node in the callgraph. */
366 if (found)
367 {
368 if (node->analyzed)
369 {
370 struct cgraph_node *clone;
371
372 /* If there are still clones, we must keep body around.
373 Otherwise we can just remove the body but keep the clone. */
374 for (clone = node->clones; clone;
375 clone = clone->next_sibling_clone)
376 if (clone->symbol.aux)
377 break;
378 if (!clone)
379 {
380 cgraph_release_function_body (node);
381 if (node->prev_sibling_clone)
382 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
383 else if (node->clone_of)
384 node->clone_of->clones = node->next_sibling_clone;
385 if (node->next_sibling_clone)
386 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
387 if (node->clone_of)
388 node->former_clone_of = node->clone_of->symbol.decl;
389 node->clone_of = NULL;
390 node->next_sibling_clone = NULL;
391 node->prev_sibling_clone = NULL;
392 }
393 else
394 gcc_assert (!clone->symbol.in_other_partition);
395 node->analyzed = false;
396 changed = true;
397 cgraph_node_remove_callees (node);
398 ipa_remove_all_references (&node->symbol.ref_list);
399 }
400 }
401 else
402 {
403 cgraph_remove_node (node);
404 changed = true;
405 }
406 }
407 }
408 FOR_EACH_FUNCTION (node)
409 {
410 /* Inline clones might be kept around so their materializing allows further
411 cloning. If the function the clone is inlined into is removed, we need
412 to turn it into normal cone. */
413 if (node->global.inlined_to
414 && !node->callers)
415 {
416 gcc_assert (node->clones);
417 node->global.inlined_to = NULL;
418 update_inlined_to_pointer (node, node);
419 }
420 node->symbol.aux = NULL;
421 }
422
423 if (file)
424 fprintf (file, "\n");
425
426 if (file)
427 fprintf (file, "Reclaiming variables:");
428 for (vnode = varpool_nodes; vnode; vnode = vnext)
429 {
430 vnext = vnode->next;
431 if (!vnode->needed)
432 {
433 if (file)
434 fprintf (file, " %s", varpool_node_name (vnode));
435 varpool_remove_node (vnode);
436 changed = true;
437 }
438 }
439
440 /* Now update address_taken flags and try to promote functions to be local. */
441
442 if (file)
443 fprintf (file, "\nClearing address taken flags:");
444 FOR_EACH_DEFINED_FUNCTION (node)
445 if (node->symbol.address_taken
446 && !node->symbol.used_from_other_partition)
447 {
448 if (!cgraph_for_node_and_aliases (node, has_addr_references_p, NULL, true))
449 {
450 if (file)
451 fprintf (file, " %s", cgraph_node_name (node));
452 node->symbol.address_taken = false;
453 changed = true;
454 if (cgraph_local_node_p (node))
455 {
456 node->local.local = true;
457 if (file)
458 fprintf (file, " (local)");
459 }
460 }
461 }
462 if (file)
463 fprintf (file, "\n");
464
465 /* Rest of transformations are undesirable at -O0. */
466 if (!optimize)
467 return changed;
468
469 #ifdef ENABLE_CHECKING
470 verify_cgraph ();
471 #endif
472
473 /* Reclaim alias pairs for functions that have disappeared from the
474 call graph. */
475 remove_unreachable_alias_pairs ();
476
477 return changed;
478 }
479
480 /* Discover variables that have no longer address taken or that are read only
481 and update their flags.
482
483 FIXME: This can not be done in between gimplify and omp_expand since
484 readonly flag plays role on what is shared and what is not. Currently we do
485 this transformation as part of whole program visibility and re-do at
486 ipa-reference pass (to take into account clonning), but it would
487 make sense to do it before early optimizations. */
488
489 void
490 ipa_discover_readonly_nonaddressable_vars (void)
491 {
492 struct varpool_node *vnode;
493 if (dump_file)
494 fprintf (dump_file, "Clearing variable flags:");
495 FOR_EACH_VARIABLE (vnode)
496 if (vnode->finalized && varpool_all_refs_explicit_p (vnode)
497 && (TREE_ADDRESSABLE (vnode->symbol.decl)
498 || !TREE_READONLY (vnode->symbol.decl)))
499 {
500 bool written = false;
501 bool address_taken = false;
502 int i;
503 struct ipa_ref *ref;
504 for (i = 0; ipa_ref_list_refering_iterate (&vnode->symbol.ref_list,
505 i, ref)
506 && (!written || !address_taken); i++)
507 switch (ref->use)
508 {
509 case IPA_REF_ADDR:
510 address_taken = true;
511 break;
512 case IPA_REF_LOAD:
513 break;
514 case IPA_REF_STORE:
515 written = true;
516 break;
517 }
518 if (TREE_ADDRESSABLE (vnode->symbol.decl) && !address_taken)
519 {
520 if (dump_file)
521 fprintf (dump_file, " %s (addressable)", varpool_node_name (vnode));
522 TREE_ADDRESSABLE (vnode->symbol.decl) = 0;
523 }
524 if (!TREE_READONLY (vnode->symbol.decl) && !address_taken && !written
525 /* Making variable in explicit section readonly can cause section
526 type conflict.
527 See e.g. gcc.c-torture/compile/pr23237.c */
528 && DECL_SECTION_NAME (vnode->symbol.decl) == NULL)
529 {
530 if (dump_file)
531 fprintf (dump_file, " %s (read-only)", varpool_node_name (vnode));
532 TREE_READONLY (vnode->symbol.decl) = 1;
533 }
534 }
535 if (dump_file)
536 fprintf (dump_file, "\n");
537 }
538
539 /* Return true when there is a reference to node and it is not vtable. */
540 static bool
541 cgraph_address_taken_from_non_vtable_p (struct cgraph_node *node)
542 {
543 int i;
544 struct ipa_ref *ref;
545 for (i = 0; ipa_ref_list_refering_iterate (&node->symbol.ref_list,
546 i, ref); i++)
547 if (ref->use == IPA_REF_ADDR)
548 {
549 struct varpool_node *node;
550 if (ref->refering_type == IPA_REF_CGRAPH)
551 return true;
552 node = ipa_ref_refering_varpool_node (ref);
553 if (!DECL_VIRTUAL_P (node->symbol.decl))
554 return true;
555 }
556 return false;
557 }
558
559 /* COMDAT functions must be shared only if they have address taken,
560 otherwise we can produce our own private implementation with
561 -fwhole-program.
562 Return true when turning COMDAT functoin static can not lead to wrong
563 code when the resulting object links with a library defining same COMDAT.
564
565 Virtual functions do have their addresses taken from the vtables,
566 but in C++ there is no way to compare their addresses for equality. */
567
568 bool
569 cgraph_comdat_can_be_unshared_p (struct cgraph_node *node)
570 {
571 if ((cgraph_address_taken_from_non_vtable_p (node)
572 && !DECL_VIRTUAL_P (node->symbol.decl))
573 || !node->analyzed)
574 return false;
575 if (node->symbol.same_comdat_group)
576 {
577 struct cgraph_node *next;
578
579 /* If more than one function is in the same COMDAT group, it must
580 be shared even if just one function in the comdat group has
581 address taken. */
582 for (next = cgraph (node->symbol.same_comdat_group);
583 next != node; next = cgraph (next->symbol.same_comdat_group))
584 if (cgraph_address_taken_from_non_vtable_p (next)
585 && !DECL_VIRTUAL_P (next->symbol.decl))
586 return false;
587 }
588 return true;
589 }
590
591 /* Return true when function NODE should be considered externally visible. */
592
593 static bool
594 cgraph_externally_visible_p (struct cgraph_node *node,
595 bool whole_program, bool aliased)
596 {
597 if (!node->local.finalized)
598 return false;
599 if (!DECL_COMDAT (node->symbol.decl)
600 && (!TREE_PUBLIC (node->symbol.decl)
601 || DECL_EXTERNAL (node->symbol.decl)))
602 return false;
603
604 /* Do not even try to be smart about aliased nodes. Until we properly
605 represent everything by same body alias, these are just evil. */
606 if (aliased)
607 return true;
608
609 /* Do not try to localize built-in functions yet. One of problems is that we
610 end up mangling their asm for WHOPR that makes it impossible to call them
611 using the implicit built-in declarations anymore. Similarly this enables
612 us to remove them as unreachable before actual calls may appear during
613 expansion or folding. */
614 if (DECL_BUILT_IN (node->symbol.decl))
615 return true;
616
617 /* If linker counts on us, we must preserve the function. */
618 if (cgraph_used_from_object_file_p (node))
619 return true;
620 if (DECL_PRESERVE_P (node->symbol.decl))
621 return true;
622 if (lookup_attribute ("externally_visible",
623 DECL_ATTRIBUTES (node->symbol.decl)))
624 return true;
625 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
626 && lookup_attribute ("dllexport",
627 DECL_ATTRIBUTES (node->symbol.decl)))
628 return true;
629 if (node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY)
630 return false;
631 /* When doing LTO or whole program, we can bring COMDAT functoins static.
632 This improves code quality and we know we will duplicate them at most twice
633 (in the case that we are not using plugin and link with object file
634 implementing same COMDAT) */
635 if ((in_lto_p || whole_program)
636 && DECL_COMDAT (node->symbol.decl)
637 && cgraph_comdat_can_be_unshared_p (node))
638 return false;
639
640 /* When doing link time optimizations, hidden symbols become local. */
641 if (in_lto_p
642 && (DECL_VISIBILITY (node->symbol.decl) == VISIBILITY_HIDDEN
643 || DECL_VISIBILITY (node->symbol.decl) == VISIBILITY_INTERNAL)
644 /* Be sure that node is defined in IR file, not in other object
645 file. In that case we don't set used_from_other_object_file. */
646 && node->analyzed)
647 ;
648 else if (!whole_program)
649 return true;
650
651 if (MAIN_NAME_P (DECL_NAME (node->symbol.decl)))
652 return true;
653
654 return false;
655 }
656
657 /* Return true when variable VNODE should be considered externally visible. */
658
659 bool
660 varpool_externally_visible_p (struct varpool_node *vnode, bool aliased)
661 {
662 if (!DECL_COMDAT (vnode->symbol.decl) && !TREE_PUBLIC (vnode->symbol.decl))
663 return false;
664
665 /* Do not even try to be smart about aliased nodes. Until we properly
666 represent everything by same body alias, these are just evil. */
667 if (aliased)
668 return true;
669
670 /* If linker counts on us, we must preserve the function. */
671 if (varpool_used_from_object_file_p (vnode))
672 return true;
673
674 if (DECL_HARD_REGISTER (vnode->symbol.decl))
675 return true;
676 if (DECL_PRESERVE_P (vnode->symbol.decl))
677 return true;
678 if (lookup_attribute ("externally_visible",
679 DECL_ATTRIBUTES (vnode->symbol.decl)))
680 return true;
681 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
682 && lookup_attribute ("dllexport",
683 DECL_ATTRIBUTES (vnode->symbol.decl)))
684 return true;
685
686 /* See if we have linker information about symbol not being used or
687 if we need to make guess based on the declaration.
688
689 Even if the linker clams the symbol is unused, never bring internal
690 symbols that are declared by user as used or externally visible.
691 This is needed for i.e. references from asm statements. */
692 if (varpool_used_from_object_file_p (vnode))
693 return true;
694 if (vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY)
695 return false;
696
697 /* As a special case, the COMDAT virutal tables can be unshared.
698 In LTO mode turn vtables into static variables. The variable is readonly,
699 so this does not enable more optimization, but referring static var
700 is faster for dynamic linking. Also this match logic hidding vtables
701 from LTO symbol tables. */
702 if ((in_lto_p || flag_whole_program)
703 && !vnode->force_output
704 && DECL_COMDAT (vnode->symbol.decl) && DECL_VIRTUAL_P (vnode->symbol.decl))
705 return false;
706
707 /* When doing link time optimizations, hidden symbols become local. */
708 if (in_lto_p
709 && (DECL_VISIBILITY (vnode->symbol.decl) == VISIBILITY_HIDDEN
710 || DECL_VISIBILITY (vnode->symbol.decl) == VISIBILITY_INTERNAL)
711 /* Be sure that node is defined in IR file, not in other object
712 file. In that case we don't set used_from_other_object_file. */
713 && vnode->finalized)
714 ;
715 else if (!flag_whole_program)
716 return true;
717
718 /* Do not attempt to privatize COMDATS by default.
719 This would break linking with C++ libraries sharing
720 inline definitions.
721
722 FIXME: We can do so for readonly vars with no address taken and
723 possibly also for vtables since no direct pointer comparsion is done.
724 It might be interesting to do so to reduce linking overhead. */
725 if (DECL_COMDAT (vnode->symbol.decl) || DECL_WEAK (vnode->symbol.decl))
726 return true;
727 return false;
728 }
729
730 /* Dissolve the same_comdat_group list in which NODE resides. */
731
732 static void
733 dissolve_same_comdat_group_list (struct cgraph_node *node)
734 {
735 struct cgraph_node *n = node, *next;
736 do
737 {
738 next = cgraph (n->symbol.same_comdat_group);
739 n->symbol.same_comdat_group = NULL;
740 n = next;
741 }
742 while (n != node);
743 }
744
745 /* Mark visibility of all functions.
746
747 A local function is one whose calls can occur only in the current
748 compilation unit and all its calls are explicit, so we can change
749 its calling convention. We simply mark all static functions whose
750 address is not taken as local.
751
752 We also change the TREE_PUBLIC flag of all declarations that are public
753 in language point of view but we want to overwrite this default
754 via visibilities for the backend point of view. */
755
756 static unsigned int
757 function_and_variable_visibility (bool whole_program)
758 {
759 struct cgraph_node *node;
760 struct varpool_node *vnode;
761 struct pointer_set_t *aliased_nodes = pointer_set_create ();
762 struct pointer_set_t *aliased_vnodes = pointer_set_create ();
763 unsigned i;
764 alias_pair *p;
765
766 /* Discover aliased nodes. */
767 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
768 {
769 if (dump_file)
770 fprintf (dump_file, "Alias %s->%s",
771 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (p->decl)),
772 IDENTIFIER_POINTER (p->target));
773
774 if ((node = cgraph_node_for_asm (p->target)) != NULL
775 && !DECL_EXTERNAL (node->symbol.decl))
776 {
777 if (!node->analyzed)
778 continue;
779 cgraph_mark_needed_node (node);
780 gcc_assert (node->needed);
781 pointer_set_insert (aliased_nodes, node);
782 if (dump_file)
783 fprintf (dump_file, " node %s/%i",
784 cgraph_node_name (node), node->uid);
785 }
786 else if ((vnode = varpool_node_for_asm (p->target)) != NULL
787 && !DECL_EXTERNAL (vnode->symbol.decl))
788 {
789 varpool_mark_needed_node (vnode);
790 gcc_assert (vnode->needed);
791 pointer_set_insert (aliased_vnodes, vnode);
792 if (dump_file)
793 fprintf (dump_file, " varpool node %s",
794 varpool_node_name (vnode));
795 }
796 if (dump_file)
797 fprintf (dump_file, "\n");
798 }
799
800 FOR_EACH_FUNCTION (node)
801 {
802 int flags = flags_from_decl_or_type (node->symbol.decl);
803
804 /* Optimize away PURE and CONST constructors and destructors. */
805 if (optimize
806 && (flags & (ECF_CONST | ECF_PURE))
807 && !(flags & ECF_LOOPING_CONST_OR_PURE))
808 {
809 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
810 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
811 }
812
813 /* Frontends and alias code marks nodes as needed before parsing is finished.
814 We may end up marking as node external nodes where this flag is meaningless
815 strip it. */
816 if (node->needed
817 && (DECL_EXTERNAL (node->symbol.decl) || !node->analyzed))
818 node->needed = 0;
819
820 /* C++ FE on lack of COMDAT support create local COMDAT functions
821 (that ought to be shared but can not due to object format
822 limitations). It is neccesary to keep the flag to make rest of C++ FE
823 happy. Clear the flag here to avoid confusion in middle-end. */
824 if (DECL_COMDAT (node->symbol.decl) && !TREE_PUBLIC (node->symbol.decl))
825 DECL_COMDAT (node->symbol.decl) = 0;
826 /* For external decls stop tracking same_comdat_group, it doesn't matter
827 what comdat group they are in when they won't be emitted in this TU,
828 and simplifies later passes. */
829 if (node->symbol.same_comdat_group && DECL_EXTERNAL (node->symbol.decl))
830 {
831 #ifdef ENABLE_CHECKING
832 symtab_node n;
833
834 for (n = node->symbol.same_comdat_group;
835 n != (symtab_node)node;
836 n = n->symbol.same_comdat_group)
837 /* If at least one of same comdat group functions is external,
838 all of them have to be, otherwise it is a front-end bug. */
839 gcc_assert (DECL_EXTERNAL (n->symbol.decl));
840 #endif
841 dissolve_same_comdat_group_list (node);
842 }
843 gcc_assert ((!DECL_WEAK (node->symbol.decl)
844 && !DECL_COMDAT (node->symbol.decl))
845 || TREE_PUBLIC (node->symbol.decl)
846 || DECL_EXTERNAL (node->symbol.decl));
847 if (cgraph_externally_visible_p (node, whole_program,
848 pointer_set_contains (aliased_nodes,
849 node)))
850 {
851 gcc_assert (!node->global.inlined_to);
852 node->symbol.externally_visible = true;
853 }
854 else
855 node->symbol.externally_visible = false;
856 if (!node->symbol.externally_visible && node->analyzed
857 && !DECL_EXTERNAL (node->symbol.decl))
858 {
859 gcc_assert (whole_program || in_lto_p
860 || !TREE_PUBLIC (node->symbol.decl));
861 cgraph_make_decl_local (node->symbol.decl);
862 node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
863 if (node->symbol.same_comdat_group)
864 /* cgraph_externally_visible_p has already checked all other nodes
865 in the group and they will all be made local. We need to
866 dissolve the group at once so that the predicate does not
867 segfault though. */
868 dissolve_same_comdat_group_list (node);
869 }
870
871 if (node->thunk.thunk_p
872 && TREE_PUBLIC (node->symbol.decl))
873 {
874 struct cgraph_node *decl_node = node;
875
876 decl_node = cgraph_function_node (decl_node->callees->callee, NULL);
877
878 /* Thunks have the same visibility as function they are attached to.
879 Make sure the C++ front end set this up properly. */
880 if (DECL_ONE_ONLY (decl_node->symbol.decl))
881 {
882 gcc_checking_assert (DECL_COMDAT (node->symbol.decl)
883 == DECL_COMDAT (decl_node->symbol.decl));
884 gcc_checking_assert (DECL_COMDAT_GROUP (node->symbol.decl)
885 == DECL_COMDAT_GROUP (decl_node->symbol.decl));
886 gcc_checking_assert (node->symbol.same_comdat_group);
887 }
888 if (DECL_EXTERNAL (decl_node->symbol.decl))
889 DECL_EXTERNAL (node->symbol.decl) = 1;
890 }
891 }
892 FOR_EACH_DEFINED_FUNCTION (node)
893 node->local.local = cgraph_local_node_p (node);
894 FOR_EACH_VARIABLE (vnode)
895 {
896 /* weak flag makes no sense on local variables. */
897 gcc_assert (!DECL_WEAK (vnode->symbol.decl)
898 || TREE_PUBLIC (vnode->symbol.decl)
899 || DECL_EXTERNAL (vnode->symbol.decl));
900 /* In several cases declarations can not be common:
901
902 - when declaration has initializer
903 - when it is in weak
904 - when it has specific section
905 - when it resides in non-generic address space.
906 - if declaration is local, it will get into .local common section
907 so common flag is not needed. Frontends still produce these in
908 certain cases, such as for:
909
910 static int a __attribute__ ((common))
911
912 Canonicalize things here and clear the redundant flag. */
913 if (DECL_COMMON (vnode->symbol.decl)
914 && (!(TREE_PUBLIC (vnode->symbol.decl)
915 || DECL_EXTERNAL (vnode->symbol.decl))
916 || (DECL_INITIAL (vnode->symbol.decl)
917 && DECL_INITIAL (vnode->symbol.decl) != error_mark_node)
918 || DECL_WEAK (vnode->symbol.decl)
919 || DECL_SECTION_NAME (vnode->symbol.decl) != NULL
920 || ! (ADDR_SPACE_GENERIC_P
921 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->symbol.decl))))))
922 DECL_COMMON (vnode->symbol.decl) = 0;
923 }
924 FOR_EACH_DEFINED_VARIABLE (vnode)
925 {
926 if (!vnode->finalized)
927 continue;
928 if (vnode->needed
929 && varpool_externally_visible_p
930 (vnode,
931 pointer_set_contains (aliased_vnodes, vnode)))
932 vnode->symbol.externally_visible = true;
933 else
934 vnode->symbol.externally_visible = false;
935 if (!vnode->symbol.externally_visible)
936 {
937 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->symbol.decl));
938 cgraph_make_decl_local (vnode->symbol.decl);
939 vnode->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
940 }
941 gcc_assert (TREE_STATIC (vnode->symbol.decl));
942 }
943 pointer_set_destroy (aliased_nodes);
944 pointer_set_destroy (aliased_vnodes);
945
946 if (dump_file)
947 {
948 fprintf (dump_file, "\nMarking local functions:");
949 FOR_EACH_DEFINED_FUNCTION (node)
950 if (node->local.local)
951 fprintf (dump_file, " %s", cgraph_node_name (node));
952 fprintf (dump_file, "\n\n");
953 fprintf (dump_file, "\nMarking externally visible functions:");
954 FOR_EACH_DEFINED_FUNCTION (node)
955 if (node->symbol.externally_visible)
956 fprintf (dump_file, " %s", cgraph_node_name (node));
957 fprintf (dump_file, "\n\n");
958 fprintf (dump_file, "\nMarking externally visible variables:");
959 FOR_EACH_DEFINED_VARIABLE (vnode)
960 if (vnode->symbol.externally_visible)
961 fprintf (dump_file, " %s", varpool_node_name (vnode));
962 fprintf (dump_file, "\n\n");
963 }
964 cgraph_function_flags_ready = true;
965 return 0;
966 }
967
968 /* Local function pass handling visibilities. This happens before LTO streaming
969 so in particular -fwhole-program should be ignored at this level. */
970
971 static unsigned int
972 local_function_and_variable_visibility (void)
973 {
974 return function_and_variable_visibility (flag_whole_program && !flag_lto);
975 }
976
977 struct simple_ipa_opt_pass pass_ipa_function_and_variable_visibility =
978 {
979 {
980 SIMPLE_IPA_PASS,
981 "visibility", /* name */
982 NULL, /* gate */
983 local_function_and_variable_visibility,/* execute */
984 NULL, /* sub */
985 NULL, /* next */
986 0, /* static_pass_number */
987 TV_CGRAPHOPT, /* tv_id */
988 0, /* properties_required */
989 0, /* properties_provided */
990 0, /* properties_destroyed */
991 0, /* todo_flags_start */
992 TODO_remove_functions | TODO_dump_cgraph
993 | TODO_ggc_collect /* todo_flags_finish */
994 }
995 };
996
997 /* Do not re-run on ltrans stage. */
998
999 static bool
1000 gate_whole_program_function_and_variable_visibility (void)
1001 {
1002 return !flag_ltrans;
1003 }
1004
1005 /* Bring functionss local at LTO time whith -fwhole-program. */
1006
1007 static unsigned int
1008 whole_program_function_and_variable_visibility (void)
1009 {
1010 struct cgraph_node *node;
1011 struct varpool_node *vnode;
1012
1013 function_and_variable_visibility (flag_whole_program);
1014
1015 FOR_EACH_DEFINED_FUNCTION (node)
1016 if ((node->symbol.externally_visible && !DECL_COMDAT (node->symbol.decl))
1017 && node->local.finalized)
1018 cgraph_mark_needed_node (node);
1019 FOR_EACH_DEFINED_VARIABLE (vnode)
1020 if (vnode->symbol.externally_visible && !DECL_COMDAT (vnode->symbol.decl))
1021 varpool_mark_needed_node (vnode);
1022 if (dump_file)
1023 {
1024 fprintf (dump_file, "\nNeeded variables:");
1025 FOR_EACH_DEFINED_VARIABLE (vnode)
1026 if (vnode->needed)
1027 fprintf (dump_file, " %s", varpool_node_name (vnode));
1028 fprintf (dump_file, "\n\n");
1029 }
1030 if (optimize)
1031 ipa_discover_readonly_nonaddressable_vars ();
1032 return 0;
1033 }
1034
1035 struct ipa_opt_pass_d pass_ipa_whole_program_visibility =
1036 {
1037 {
1038 IPA_PASS,
1039 "whole-program", /* name */
1040 gate_whole_program_function_and_variable_visibility,/* gate */
1041 whole_program_function_and_variable_visibility,/* execute */
1042 NULL, /* sub */
1043 NULL, /* next */
1044 0, /* static_pass_number */
1045 TV_CGRAPHOPT, /* tv_id */
1046 0, /* properties_required */
1047 0, /* properties_provided */
1048 0, /* properties_destroyed */
1049 0, /* todo_flags_start */
1050 TODO_remove_functions | TODO_dump_cgraph
1051 | TODO_ggc_collect /* todo_flags_finish */
1052 },
1053 NULL, /* generate_summary */
1054 NULL, /* write_summary */
1055 NULL, /* read_summary */
1056 NULL, /* write_optimization_summary */
1057 NULL, /* read_optimization_summary */
1058 NULL, /* stmt_fixup */
1059 0, /* TODOs */
1060 NULL, /* function_transform */
1061 NULL, /* variable_transform */
1062 };
1063
1064
1065 /* Simple ipa profile pass propagating frequencies across the callgraph. */
1066
1067 static unsigned int
1068 ipa_profile (void)
1069 {
1070 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1071 struct cgraph_edge *e;
1072 int order_pos;
1073 bool something_changed = false;
1074 int i;
1075
1076 order_pos = ipa_reverse_postorder (order);
1077 for (i = order_pos - 1; i >= 0; i--)
1078 {
1079 if (order[i]->local.local && cgraph_propagate_frequency (order[i]))
1080 {
1081 for (e = order[i]->callees; e; e = e->next_callee)
1082 if (e->callee->local.local && !e->callee->symbol.aux)
1083 {
1084 something_changed = true;
1085 e->callee->symbol.aux = (void *)1;
1086 }
1087 }
1088 order[i]->symbol.aux = NULL;
1089 }
1090
1091 while (something_changed)
1092 {
1093 something_changed = false;
1094 for (i = order_pos - 1; i >= 0; i--)
1095 {
1096 if (order[i]->symbol.aux && cgraph_propagate_frequency (order[i]))
1097 {
1098 for (e = order[i]->callees; e; e = e->next_callee)
1099 if (e->callee->local.local && !e->callee->symbol.aux)
1100 {
1101 something_changed = true;
1102 e->callee->symbol.aux = (void *)1;
1103 }
1104 }
1105 order[i]->symbol.aux = NULL;
1106 }
1107 }
1108 free (order);
1109 return 0;
1110 }
1111
1112 static bool
1113 gate_ipa_profile (void)
1114 {
1115 return flag_ipa_profile;
1116 }
1117
1118 struct ipa_opt_pass_d pass_ipa_profile =
1119 {
1120 {
1121 IPA_PASS,
1122 "profile_estimate", /* name */
1123 gate_ipa_profile, /* gate */
1124 ipa_profile, /* execute */
1125 NULL, /* sub */
1126 NULL, /* next */
1127 0, /* static_pass_number */
1128 TV_IPA_PROFILE, /* tv_id */
1129 0, /* properties_required */
1130 0, /* properties_provided */
1131 0, /* properties_destroyed */
1132 0, /* todo_flags_start */
1133 0 /* todo_flags_finish */
1134 },
1135 NULL, /* generate_summary */
1136 NULL, /* write_summary */
1137 NULL, /* read_summary */
1138 NULL, /* write_optimization_summary */
1139 NULL, /* read_optimization_summary */
1140 NULL, /* stmt_fixup */
1141 0, /* TODOs */
1142 NULL, /* function_transform */
1143 NULL /* variable_transform */
1144 };
1145
1146 /* Generate and emit a static constructor or destructor. WHICH must
1147 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1148 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1149 initialization priority for this constructor or destructor.
1150
1151 FINAL specify whether the externally visible name for collect2 should
1152 be produced. */
1153
1154 static void
1155 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
1156 {
1157 static int counter = 0;
1158 char which_buf[16];
1159 tree decl, name, resdecl;
1160
1161 /* The priority is encoded in the constructor or destructor name.
1162 collect2 will sort the names and arrange that they are called at
1163 program startup. */
1164 if (final)
1165 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1166 else
1167 /* Proudce sane name but one not recognizable by collect2, just for the
1168 case we fail to inline the function. */
1169 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
1170 name = get_file_function_name (which_buf);
1171
1172 decl = build_decl (input_location, FUNCTION_DECL, name,
1173 build_function_type_list (void_type_node, NULL_TREE));
1174 current_function_decl = decl;
1175
1176 resdecl = build_decl (input_location,
1177 RESULT_DECL, NULL_TREE, void_type_node);
1178 DECL_ARTIFICIAL (resdecl) = 1;
1179 DECL_RESULT (decl) = resdecl;
1180 DECL_CONTEXT (resdecl) = decl;
1181
1182 allocate_struct_function (decl, false);
1183
1184 TREE_STATIC (decl) = 1;
1185 TREE_USED (decl) = 1;
1186 DECL_ARTIFICIAL (decl) = 1;
1187 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1188 DECL_SAVED_TREE (decl) = body;
1189 if (!targetm.have_ctors_dtors && final)
1190 {
1191 TREE_PUBLIC (decl) = 1;
1192 DECL_PRESERVE_P (decl) = 1;
1193 }
1194 DECL_UNINLINABLE (decl) = 1;
1195
1196 DECL_INITIAL (decl) = make_node (BLOCK);
1197 TREE_USED (DECL_INITIAL (decl)) = 1;
1198
1199 DECL_SOURCE_LOCATION (decl) = input_location;
1200 cfun->function_end_locus = input_location;
1201
1202 switch (which)
1203 {
1204 case 'I':
1205 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1206 decl_init_priority_insert (decl, priority);
1207 break;
1208 case 'D':
1209 DECL_STATIC_DESTRUCTOR (decl) = 1;
1210 decl_fini_priority_insert (decl, priority);
1211 break;
1212 default:
1213 gcc_unreachable ();
1214 }
1215
1216 gimplify_function_tree (decl);
1217
1218 cgraph_add_new_function (decl, false);
1219
1220 set_cfun (NULL);
1221 current_function_decl = NULL;
1222 }
1223
1224 /* Generate and emit a static constructor or destructor. WHICH must
1225 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1226 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1227 initialization priority for this constructor or destructor. */
1228
1229 void
1230 cgraph_build_static_cdtor (char which, tree body, int priority)
1231 {
1232 cgraph_build_static_cdtor_1 (which, body, priority, false);
1233 }
1234
1235 /* A vector of FUNCTION_DECLs declared as static constructors. */
1236 static VEC(tree, heap) *static_ctors;
1237 /* A vector of FUNCTION_DECLs declared as static destructors. */
1238 static VEC(tree, heap) *static_dtors;
1239
1240 /* When target does not have ctors and dtors, we call all constructor
1241 and destructor by special initialization/destruction function
1242 recognized by collect2.
1243
1244 When we are going to build this function, collect all constructors and
1245 destructors and turn them into normal functions. */
1246
1247 static void
1248 record_cdtor_fn (struct cgraph_node *node)
1249 {
1250 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1251 VEC_safe_push (tree, heap, static_ctors, node->symbol.decl);
1252 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1253 VEC_safe_push (tree, heap, static_dtors, node->symbol.decl);
1254 node = cgraph_get_node (node->symbol.decl);
1255 DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl) = 1;
1256 }
1257
1258 /* Define global constructors/destructor functions for the CDTORS, of
1259 which they are LEN. The CDTORS are sorted by initialization
1260 priority. If CTOR_P is true, these are constructors; otherwise,
1261 they are destructors. */
1262
1263 static void
1264 build_cdtor (bool ctor_p, VEC (tree, heap) *cdtors)
1265 {
1266 size_t i,j;
1267 size_t len = VEC_length (tree, cdtors);
1268
1269 i = 0;
1270 while (i < len)
1271 {
1272 tree body;
1273 tree fn;
1274 priority_type priority;
1275
1276 priority = 0;
1277 body = NULL_TREE;
1278 j = i;
1279 do
1280 {
1281 priority_type p;
1282 fn = VEC_index (tree, cdtors, j);
1283 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1284 if (j == i)
1285 priority = p;
1286 else if (p != priority)
1287 break;
1288 j++;
1289 }
1290 while (j < len);
1291
1292 /* When there is only one cdtor and target supports them, do nothing. */
1293 if (j == i + 1
1294 && targetm.have_ctors_dtors)
1295 {
1296 i++;
1297 continue;
1298 }
1299 /* Find the next batch of constructors/destructors with the same
1300 initialization priority. */
1301 for (;i < j; i++)
1302 {
1303 tree call;
1304 fn = VEC_index (tree, cdtors, i);
1305 call = build_call_expr (fn, 0);
1306 if (ctor_p)
1307 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1308 else
1309 DECL_STATIC_DESTRUCTOR (fn) = 0;
1310 /* We do not want to optimize away pure/const calls here.
1311 When optimizing, these should be already removed, when not
1312 optimizing, we want user to be able to breakpoint in them. */
1313 TREE_SIDE_EFFECTS (call) = 1;
1314 append_to_statement_list (call, &body);
1315 }
1316 gcc_assert (body != NULL_TREE);
1317 /* Generate a function to call all the function of like
1318 priority. */
1319 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1320 }
1321 }
1322
1323 /* Comparison function for qsort. P1 and P2 are actually of type
1324 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1325 used to determine the sort order. */
1326
1327 static int
1328 compare_ctor (const void *p1, const void *p2)
1329 {
1330 tree f1;
1331 tree f2;
1332 int priority1;
1333 int priority2;
1334
1335 f1 = *(const tree *)p1;
1336 f2 = *(const tree *)p2;
1337 priority1 = DECL_INIT_PRIORITY (f1);
1338 priority2 = DECL_INIT_PRIORITY (f2);
1339
1340 if (priority1 < priority2)
1341 return -1;
1342 else if (priority1 > priority2)
1343 return 1;
1344 else
1345 /* Ensure a stable sort. Constructors are executed in backwarding
1346 order to make LTO initialize braries first. */
1347 return DECL_UID (f2) - DECL_UID (f1);
1348 }
1349
1350 /* Comparison function for qsort. P1 and P2 are actually of type
1351 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1352 used to determine the sort order. */
1353
1354 static int
1355 compare_dtor (const void *p1, const void *p2)
1356 {
1357 tree f1;
1358 tree f2;
1359 int priority1;
1360 int priority2;
1361
1362 f1 = *(const tree *)p1;
1363 f2 = *(const tree *)p2;
1364 priority1 = DECL_FINI_PRIORITY (f1);
1365 priority2 = DECL_FINI_PRIORITY (f2);
1366
1367 if (priority1 < priority2)
1368 return -1;
1369 else if (priority1 > priority2)
1370 return 1;
1371 else
1372 /* Ensure a stable sort. */
1373 return DECL_UID (f1) - DECL_UID (f2);
1374 }
1375
1376 /* Generate functions to call static constructors and destructors
1377 for targets that do not support .ctors/.dtors sections. These
1378 functions have magic names which are detected by collect2. */
1379
1380 static void
1381 build_cdtor_fns (void)
1382 {
1383 if (!VEC_empty (tree, static_ctors))
1384 {
1385 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1386 VEC_qsort (tree, static_ctors, compare_ctor);
1387 build_cdtor (/*ctor_p=*/true, static_ctors);
1388 }
1389
1390 if (!VEC_empty (tree, static_dtors))
1391 {
1392 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1393 VEC_qsort (tree, static_dtors, compare_dtor);
1394 build_cdtor (/*ctor_p=*/false, static_dtors);
1395 }
1396 }
1397
1398 /* Look for constructors and destructors and produce function calling them.
1399 This is needed for targets not supporting ctors or dtors, but we perform the
1400 transformation also at linktime to merge possibly numberous
1401 constructors/destructors into single function to improve code locality and
1402 reduce size. */
1403
1404 static unsigned int
1405 ipa_cdtor_merge (void)
1406 {
1407 struct cgraph_node *node;
1408 FOR_EACH_DEFINED_FUNCTION (node)
1409 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
1410 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1411 record_cdtor_fn (node);
1412 build_cdtor_fns ();
1413 VEC_free (tree, heap, static_ctors);
1414 VEC_free (tree, heap, static_dtors);
1415 return 0;
1416 }
1417
1418 /* Perform the pass when we have no ctors/dtors support
1419 or at LTO time to merge multiple constructors into single
1420 function. */
1421
1422 static bool
1423 gate_ipa_cdtor_merge (void)
1424 {
1425 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1426 }
1427
1428 struct ipa_opt_pass_d pass_ipa_cdtor_merge =
1429 {
1430 {
1431 IPA_PASS,
1432 "cdtor", /* name */
1433 gate_ipa_cdtor_merge, /* gate */
1434 ipa_cdtor_merge, /* execute */
1435 NULL, /* sub */
1436 NULL, /* next */
1437 0, /* static_pass_number */
1438 TV_CGRAPHOPT, /* tv_id */
1439 0, /* properties_required */
1440 0, /* properties_provided */
1441 0, /* properties_destroyed */
1442 0, /* todo_flags_start */
1443 0 /* todo_flags_finish */
1444 },
1445 NULL, /* generate_summary */
1446 NULL, /* write_summary */
1447 NULL, /* read_summary */
1448 NULL, /* write_optimization_summary */
1449 NULL, /* read_optimization_summary */
1450 NULL, /* stmt_fixup */
1451 0, /* TODOs */
1452 NULL, /* function_transform */
1453 NULL /* variable_transform */
1454 };