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