sparc.c (sparc_do_work_around_errata): Implement work around for store forwarding...
[gcc.git] / gcc / ipa.c
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "calls.h"
26 #include "stringpool.h"
27 #include "cgraph.h"
28 #include "tree-pass.h"
29 #include "pointer-set.h"
30 #include "gimple-expr.h"
31 #include "gimplify.h"
32 #include "flags.h"
33 #include "target.h"
34 #include "tree-iterator.h"
35 #include "ipa-utils.h"
36 #include "ipa-inline.h"
37 #include "tree-inline.h"
38 #include "profile.h"
39 #include "params.h"
40
41 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
42
43 static bool
44 cgraph_non_local_node_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
45 {
46 /* FIXME: Aliases can be local, but i386 gets thunks wrong then. */
47 return !(cgraph_only_called_directly_or_aliased_p (node)
48 && !ipa_ref_has_aliases_p (&node->ref_list)
49 && node->definition
50 && !DECL_EXTERNAL (node->decl)
51 && !node->externally_visible
52 && !node->used_from_other_partition
53 && !node->in_other_partition);
54 }
55
56 /* Return true when function can be marked local. */
57
58 static bool
59 cgraph_local_node_p (struct cgraph_node *node)
60 {
61 struct cgraph_node *n = cgraph_function_or_thunk_node (node, NULL);
62
63 /* FIXME: thunks can be considered local, but we need prevent i386
64 from attempting to change calling convention of them. */
65 if (n->thunk.thunk_p)
66 return false;
67 return !cgraph_for_node_and_aliases (n,
68 cgraph_non_local_node_p_1, NULL, true);
69
70 }
71
72 /* Return true when NODE has ADDR reference. */
73
74 static bool
75 has_addr_references_p (struct cgraph_node *node,
76 void *data ATTRIBUTE_UNUSED)
77 {
78 int i;
79 struct ipa_ref *ref;
80
81 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
82 i, ref); i++)
83 if (ref->use == IPA_REF_ADDR)
84 return true;
85 return false;
86 }
87
88 /* Look for all functions inlined to NODE and update their inlined_to pointers
89 to INLINED_TO. */
90
91 static void
92 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
93 {
94 struct cgraph_edge *e;
95 for (e = node->callees; e; e = e->next_callee)
96 if (e->callee->global.inlined_to)
97 {
98 e->callee->global.inlined_to = inlined_to;
99 update_inlined_to_pointer (e->callee, inlined_to);
100 }
101 }
102
103 /* Add symtab NODE to queue starting at FIRST.
104
105 The queue is linked via AUX pointers and terminated by pointer to 1.
106 We enqueue nodes at two occasions: when we find them reachable or when we find
107 their bodies needed for further clonning. In the second case we mark them
108 by pointer to 2 after processing so they are re-queue when they become
109 reachable. */
110
111 static void
112 enqueue_node (symtab_node *node, symtab_node **first,
113 struct pointer_set_t *reachable)
114 {
115 /* Node is still in queue; do nothing. */
116 if (node->aux && node->aux != (void *) 2)
117 return;
118 /* Node was already processed as unreachable, re-enqueue
119 only if it became reachable now. */
120 if (node->aux == (void *)2 && !pointer_set_contains (reachable, node))
121 return;
122 node->aux = *first;
123 *first = node;
124 }
125
126 /* Process references. */
127
128 static void
129 process_references (struct ipa_ref_list *list,
130 symtab_node **first,
131 bool before_inlining_p,
132 struct pointer_set_t *reachable)
133 {
134 int i;
135 struct ipa_ref *ref;
136 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
137 {
138 symtab_node *node = ref->referred;
139
140 if (node->definition && !node->in_other_partition
141 && ((!DECL_EXTERNAL (node->decl) || node->alias)
142 || (before_inlining_p
143 /* We use variable constructors during late complation for
144 constant folding. Keep references alive so partitioning
145 knows about potential references. */
146 || (TREE_CODE (node->decl) == VAR_DECL
147 && flag_wpa
148 && ctor_for_folding (node->decl)
149 != error_mark_node))))
150 pointer_set_insert (reachable, node);
151 enqueue_node (node, first, reachable);
152 }
153 }
154
155 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
156 all its potential targets as reachable to permit later inlining if
157 devirtualization happens. After inlining still keep their declarations
158 around, so we can devirtualize to a direct call.
159
160 Also try to make trivial devirutalization when no or only one target is
161 possible. */
162
163 static void
164 walk_polymorphic_call_targets (pointer_set_t *reachable_call_targets,
165 struct cgraph_edge *edge,
166 symtab_node **first,
167 pointer_set_t *reachable, bool before_inlining_p)
168 {
169 unsigned int i;
170 void *cache_token;
171 bool final;
172 vec <cgraph_node *>targets
173 = possible_polymorphic_call_targets
174 (edge, &final, &cache_token);
175
176 if (!pointer_set_insert (reachable_call_targets,
177 cache_token))
178 {
179 for (i = 0; i < targets.length (); i++)
180 {
181 struct cgraph_node *n = targets[i];
182
183 /* Do not bother to mark virtual methods in anonymous namespace;
184 either we will find use of virtual table defining it, or it is
185 unused. */
186 if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
187 && type_in_anonymous_namespace_p
188 (method_class_type (TREE_TYPE (n->decl))))
189 continue;
190
191 /* Prior inlining, keep alive bodies of possible targets for
192 devirtualization. */
193 if (n->definition
194 && before_inlining_p)
195 pointer_set_insert (reachable, n);
196
197 /* Even after inlining we want to keep the possible targets in the
198 boundary, so late passes can still produce direct call even if
199 the chance for inlining is lost. */
200 enqueue_node (n, first, reachable);
201 }
202 }
203
204 /* Very trivial devirtualization; when the type is
205 final or anonymous (so we know all its derivation)
206 and there is only one possible virtual call target,
207 make the edge direct. */
208 if (final)
209 {
210 if (targets.length () <= 1)
211 {
212 cgraph_node *target, *node = edge->caller;
213 if (targets.length () == 1)
214 target = targets[0];
215 else
216 target = cgraph_get_create_node
217 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
218
219 if (dump_file)
220 fprintf (dump_file,
221 "Devirtualizing call in %s/%i to %s/%i\n",
222 edge->caller->name (),
223 edge->caller->order,
224 target->name (), target->order);
225 edge = cgraph_make_edge_direct (edge, target);
226 if (inline_summary_vec)
227 inline_update_overall_summary (node);
228 else if (edge->call_stmt)
229 cgraph_redirect_edge_call_stmt_to_callee (edge);
230 }
231 }
232 }
233
234 /* Perform reachability analysis and reclaim all unreachable nodes.
235
236 The algorithm is basically mark&sweep but with some extra refinements:
237
238 - reachable extern inline functions needs special handling; the bodies needs
239 to stay in memory until inlining in hope that they will be inlined.
240 After inlining we release their bodies and turn them into unanalyzed
241 nodes even when they are reachable.
242
243 BEFORE_INLINING_P specify whether we are before or after inlining.
244
245 - virtual functions are kept in callgraph even if they seem unreachable in
246 hope calls to them will be devirtualized.
247
248 Again we remove them after inlining. In late optimization some
249 devirtualization may happen, but it is not important since we won't inline
250 the call. In theory early opts and IPA should work out all important cases.
251
252 - virtual clones needs bodies of their origins for later materialization;
253 this means that we want to keep the body even if the origin is unreachable
254 otherwise. To avoid origin from sitting in the callgraph and being
255 walked by IPA passes, we turn them into unanalyzed nodes with body
256 defined.
257
258 We maintain set of function declaration where body needs to stay in
259 body_needed_for_clonning
260
261 Inline clones represent special case: their declaration match the
262 declaration of origin and cgraph_remove_node already knows how to
263 reshape callgraph and preserve body when offline copy of function or
264 inline clone is being removed.
265
266 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
267 variables with DECL_INITIAL set. We finalize these and keep reachable
268 ones around for constant folding purposes. After inlining we however
269 stop walking their references to let everything static referneced by them
270 to be removed when it is otherwise unreachable.
271
272 We maintain queue of both reachable symbols (i.e. defined symbols that needs
273 to stay) and symbols that are in boundary (i.e. external symbols referenced
274 by reachable symbols or origins of clones). The queue is represented
275 as linked list by AUX pointer terminated by 1.
276
277 At the end we keep all reachable symbols. For symbols in boundary we always
278 turn definition into a declaration, but we may keep function body around
279 based on body_needed_for_clonning
280
281 All symbols that enter the queue have AUX pointer non-zero and are in the
282 boundary. Pointer set REACHABLE is used to track reachable symbols.
283
284 Every symbol can be visited twice - once as part of boundary and once
285 as real reachable symbol. enqueue_node needs to decide whether the
286 node needs to be re-queued for second processing. For this purpose
287 we set AUX pointer of processed symbols in the boundary to constant 2. */
288
289 bool
290 symtab_remove_unreachable_nodes (bool before_inlining_p, FILE *file)
291 {
292 symtab_node *first = (symtab_node *) (void *) 1;
293 struct cgraph_node *node, *next;
294 varpool_node *vnode, *vnext;
295 bool changed = false;
296 struct pointer_set_t *reachable = pointer_set_create ();
297 struct pointer_set_t *body_needed_for_clonning = pointer_set_create ();
298 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
299
300 timevar_push (TV_IPA_UNREACHABLE);
301 #ifdef ENABLE_CHECKING
302 verify_symtab ();
303 #endif
304 if (optimize && flag_devirtualize)
305 build_type_inheritance_graph ();
306 if (file)
307 fprintf (file, "\nReclaiming functions:");
308 #ifdef ENABLE_CHECKING
309 FOR_EACH_FUNCTION (node)
310 gcc_assert (!node->aux);
311 FOR_EACH_VARIABLE (vnode)
312 gcc_assert (!vnode->aux);
313 #endif
314 /* Mark functions whose bodies are obviously needed.
315 This is mostly when they can be referenced externally. Inline clones
316 are special since their declarations are shared with master clone and thus
317 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
318 FOR_EACH_FUNCTION (node)
319 {
320 node->used_as_abstract_origin = false;
321 if (node->definition
322 && !node->global.inlined_to
323 && !node->in_other_partition
324 && !cgraph_can_remove_if_no_direct_calls_and_refs_p (node))
325 {
326 gcc_assert (!node->global.inlined_to);
327 pointer_set_insert (reachable, node);
328 enqueue_node (node, &first, reachable);
329 }
330 else
331 gcc_assert (!node->aux);
332 }
333
334 /* Mark variables that are obviously needed. */
335 FOR_EACH_DEFINED_VARIABLE (vnode)
336 if (!varpool_can_remove_if_no_refs (vnode)
337 && !vnode->in_other_partition)
338 {
339 pointer_set_insert (reachable, vnode);
340 enqueue_node (vnode, &first, reachable);
341 }
342
343 /* Perform reachability analysis. */
344 while (first != (symtab_node *) (void *) 1)
345 {
346 bool in_boundary_p = !pointer_set_contains (reachable, first);
347 symtab_node *node = first;
348
349 first = (symtab_node *)first->aux;
350
351 /* If we are processing symbol in boundary, mark its AUX pointer for
352 possible later re-processing in enqueue_node. */
353 if (in_boundary_p)
354 node->aux = (void *)2;
355 else
356 {
357 if (TREE_CODE (node->decl) == FUNCTION_DECL
358 && DECL_ABSTRACT_ORIGIN (node->decl))
359 {
360 struct cgraph_node *origin_node
361 = cgraph_get_create_node (DECL_ABSTRACT_ORIGIN (node->decl));
362 origin_node->used_as_abstract_origin = true;
363 enqueue_node (origin_node, &first, reachable);
364 }
365 /* If any symbol in a comdat group is reachable, force
366 all externally visible symbols in the same comdat
367 group to be reachable as well. Comdat-local symbols
368 can be discarded if all uses were inlined. */
369 if (node->same_comdat_group)
370 {
371 symtab_node *next;
372 for (next = node->same_comdat_group;
373 next != node;
374 next = next->same_comdat_group)
375 if (!symtab_comdat_local_p (next)
376 && !pointer_set_insert (reachable, next))
377 enqueue_node (next, &first, reachable);
378 }
379 /* Mark references as reachable. */
380 process_references (&node->ref_list, &first,
381 before_inlining_p, reachable);
382 }
383
384 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
385 {
386 /* Mark the callees reachable unless they are direct calls to extern
387 inline functions we decided to not inline. */
388 if (!in_boundary_p)
389 {
390 struct cgraph_edge *e;
391 /* Keep alive possible targets for devirtualization. */
392 if (optimize && flag_devirtualize)
393 {
394 struct cgraph_edge *next;
395 for (e = cnode->indirect_calls; e; e = next)
396 {
397 next = e->next_callee;
398 if (e->indirect_info->polymorphic)
399 walk_polymorphic_call_targets (reachable_call_targets,
400 e, &first, reachable,
401 before_inlining_p);
402 }
403 }
404 for (e = cnode->callees; e; e = e->next_callee)
405 {
406 if (e->callee->definition
407 && !e->callee->in_other_partition
408 && (!e->inline_failed
409 || !DECL_EXTERNAL (e->callee->decl)
410 || e->callee->alias
411 || before_inlining_p))
412 pointer_set_insert (reachable, e->callee);
413 enqueue_node (e->callee, &first, reachable);
414 }
415
416 /* When inline clone exists, mark body to be preserved so when removing
417 offline copy of the function we don't kill it. */
418 if (cnode->global.inlined_to)
419 pointer_set_insert (body_needed_for_clonning, cnode->decl);
420
421 /* For non-inline clones, force their origins to the boundary and ensure
422 that body is not removed. */
423 while (cnode->clone_of)
424 {
425 bool noninline = cnode->clone_of->decl != cnode->decl;
426 cnode = cnode->clone_of;
427 if (noninline)
428 {
429 pointer_set_insert (body_needed_for_clonning, cnode->decl);
430 enqueue_node (cnode, &first, reachable);
431 }
432 }
433
434 }
435 /* If any reachable function has simd clones, mark them as
436 reachable as well. */
437 if (cnode->simd_clones)
438 {
439 cgraph_node *next;
440 for (next = cnode->simd_clones;
441 next;
442 next = next->simdclone->next_clone)
443 if (in_boundary_p
444 || !pointer_set_insert (reachable, next))
445 enqueue_node (next, &first, reachable);
446 }
447 }
448 /* When we see constructor of external variable, keep referred nodes in the
449 boundary. This will also hold initializers of the external vars NODE
450 refers to. */
451 varpool_node *vnode = dyn_cast <varpool_node> (node);
452 if (vnode
453 && DECL_EXTERNAL (node->decl)
454 && !vnode->alias
455 && in_boundary_p)
456 {
457 struct ipa_ref *ref;
458 for (int i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
459 enqueue_node (ref->referred, &first, reachable);
460 }
461 }
462
463 /* Remove unreachable functions. */
464 for (node = cgraph_first_function (); node; node = next)
465 {
466 next = cgraph_next_function (node);
467
468 /* If node is not needed at all, remove it. */
469 if (!node->aux)
470 {
471 if (file)
472 fprintf (file, " %s", node->name ());
473 cgraph_remove_node (node);
474 changed = true;
475 }
476 /* If node is unreachable, remove its body. */
477 else if (!pointer_set_contains (reachable, node))
478 {
479 if (!pointer_set_contains (body_needed_for_clonning, node->decl))
480 cgraph_release_function_body (node);
481 else if (!node->clone_of)
482 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
483 if (node->definition)
484 {
485 if (file)
486 fprintf (file, " %s", node->name ());
487 node->analyzed = false;
488 node->definition = false;
489 node->cpp_implicit_alias = false;
490 node->alias = false;
491 node->weakref = false;
492 if (!node->in_other_partition)
493 node->local.local = false;
494 cgraph_node_remove_callees (node);
495 ipa_remove_all_references (&node->ref_list);
496 changed = true;
497 }
498 }
499 else
500 gcc_assert (node->clone_of || !cgraph_function_with_gimple_body_p (node)
501 || in_lto_p || DECL_RESULT (node->decl));
502 }
503
504 /* Inline clones might be kept around so their materializing allows further
505 cloning. If the function the clone is inlined into is removed, we need
506 to turn it into normal cone. */
507 FOR_EACH_FUNCTION (node)
508 {
509 if (node->global.inlined_to
510 && !node->callers)
511 {
512 gcc_assert (node->clones);
513 node->global.inlined_to = NULL;
514 update_inlined_to_pointer (node, node);
515 }
516 node->aux = NULL;
517 }
518
519 /* Remove unreachable variables. */
520 if (file)
521 fprintf (file, "\nReclaiming variables:");
522 for (vnode = varpool_first_variable (); vnode; vnode = vnext)
523 {
524 vnext = varpool_next_variable (vnode);
525 if (!vnode->aux
526 /* For can_refer_decl_in_current_unit_p we want to track for
527 all external variables if they are defined in other partition
528 or not. */
529 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
530 {
531 if (file)
532 fprintf (file, " %s", vnode->name ());
533 varpool_remove_node (vnode);
534 changed = true;
535 }
536 else if (!pointer_set_contains (reachable, vnode))
537 {
538 tree init;
539 if (vnode->definition)
540 {
541 if (file)
542 fprintf (file, " %s", vnode->name ());
543 changed = true;
544 }
545 vnode->definition = false;
546 vnode->analyzed = false;
547 vnode->aux = NULL;
548
549 /* Keep body if it may be useful for constant folding. */
550 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node)
551 varpool_remove_initializer (vnode);
552 else
553 DECL_INITIAL (vnode->decl) = init;
554 ipa_remove_all_references (&vnode->ref_list);
555 }
556 else
557 vnode->aux = NULL;
558 }
559
560 pointer_set_destroy (reachable);
561 pointer_set_destroy (body_needed_for_clonning);
562 pointer_set_destroy (reachable_call_targets);
563
564 /* Now update address_taken flags and try to promote functions to be local. */
565 if (file)
566 fprintf (file, "\nClearing address taken flags:");
567 FOR_EACH_DEFINED_FUNCTION (node)
568 if (node->address_taken
569 && !node->used_from_other_partition)
570 {
571 if (!cgraph_for_node_and_aliases (node, has_addr_references_p, NULL, true))
572 {
573 if (file)
574 fprintf (file, " %s", node->name ());
575 node->address_taken = false;
576 changed = true;
577 if (cgraph_local_node_p (node))
578 {
579 node->local.local = true;
580 if (file)
581 fprintf (file, " (local)");
582 }
583 }
584 }
585 if (file)
586 fprintf (file, "\n");
587
588 #ifdef ENABLE_CHECKING
589 verify_symtab ();
590 #endif
591
592 /* If we removed something, perhaps profile could be improved. */
593 if (changed && optimize && inline_edge_summary_vec.exists ())
594 FOR_EACH_DEFINED_FUNCTION (node)
595 ipa_propagate_frequency (node);
596
597 timevar_pop (TV_IPA_UNREACHABLE);
598 return changed;
599 }
600
601 /* Discover variables that have no longer address taken or that are read only
602 and update their flags.
603
604 FIXME: This can not be done in between gimplify and omp_expand since
605 readonly flag plays role on what is shared and what is not. Currently we do
606 this transformation as part of whole program visibility and re-do at
607 ipa-reference pass (to take into account clonning), but it would
608 make sense to do it before early optimizations. */
609
610 void
611 ipa_discover_readonly_nonaddressable_vars (void)
612 {
613 varpool_node *vnode;
614 if (dump_file)
615 fprintf (dump_file, "Clearing variable flags:");
616 FOR_EACH_VARIABLE (vnode)
617 if (vnode->definition && varpool_all_refs_explicit_p (vnode)
618 && (TREE_ADDRESSABLE (vnode->decl)
619 || !TREE_READONLY (vnode->decl)))
620 {
621 bool written = false;
622 bool address_taken = false;
623 int i;
624 struct ipa_ref *ref;
625 for (i = 0; ipa_ref_list_referring_iterate (&vnode->ref_list,
626 i, ref)
627 && (!written || !address_taken); i++)
628 switch (ref->use)
629 {
630 case IPA_REF_ADDR:
631 address_taken = true;
632 break;
633 case IPA_REF_LOAD:
634 break;
635 case IPA_REF_STORE:
636 written = true;
637 break;
638 }
639 if (TREE_ADDRESSABLE (vnode->decl) && !address_taken)
640 {
641 if (dump_file)
642 fprintf (dump_file, " %s (addressable)", vnode->name ());
643 TREE_ADDRESSABLE (vnode->decl) = 0;
644 }
645 if (!TREE_READONLY (vnode->decl) && !address_taken && !written
646 /* Making variable in explicit section readonly can cause section
647 type conflict.
648 See e.g. gcc.c-torture/compile/pr23237.c */
649 && DECL_SECTION_NAME (vnode->decl) == NULL)
650 {
651 if (dump_file)
652 fprintf (dump_file, " %s (read-only)", vnode->name ());
653 TREE_READONLY (vnode->decl) = 1;
654 }
655 }
656 if (dump_file)
657 fprintf (dump_file, "\n");
658 }
659
660 /* Return true when there is a reference to node and it is not vtable. */
661 static bool
662 address_taken_from_non_vtable_p (symtab_node *node)
663 {
664 int i;
665 struct ipa_ref *ref;
666 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
667 i, ref); i++)
668 if (ref->use == IPA_REF_ADDR)
669 {
670 varpool_node *node;
671 if (is_a <cgraph_node> (ref->referring))
672 return true;
673 node = ipa_ref_referring_varpool_node (ref);
674 if (!DECL_VIRTUAL_P (node->decl))
675 return true;
676 }
677 return false;
678 }
679
680 /* A helper for comdat_can_be_unshared_p. */
681
682 static bool
683 comdat_can_be_unshared_p_1 (symtab_node *node)
684 {
685 /* When address is taken, we don't know if equality comparison won't
686 break eventually. Exception are virutal functions, C++
687 constructors/destructors and vtables, where this is not possible by
688 language standard. */
689 if (!DECL_VIRTUAL_P (node->decl)
690 && (TREE_CODE (node->decl) != FUNCTION_DECL
691 || (!DECL_CXX_CONSTRUCTOR_P (node->decl)
692 && !DECL_CXX_DESTRUCTOR_P (node->decl)))
693 && address_taken_from_non_vtable_p (node))
694 return false;
695
696 /* If the symbol is used in some weird way, better to not touch it. */
697 if (node->force_output)
698 return false;
699
700 /* Explicit instantiations needs to be output when possibly
701 used externally. */
702 if (node->forced_by_abi
703 && TREE_PUBLIC (node->decl)
704 && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
705 && !flag_whole_program))
706 return false;
707
708 /* Non-readonly and volatile variables can not be duplicated. */
709 if (is_a <varpool_node> (node)
710 && (!TREE_READONLY (node->decl)
711 || TREE_THIS_VOLATILE (node->decl)))
712 return false;
713 return true;
714 }
715
716 /* COMDAT functions must be shared only if they have address taken,
717 otherwise we can produce our own private implementation with
718 -fwhole-program.
719 Return true when turning COMDAT functoin static can not lead to wrong
720 code when the resulting object links with a library defining same COMDAT.
721
722 Virtual functions do have their addresses taken from the vtables,
723 but in C++ there is no way to compare their addresses for equality. */
724
725 static bool
726 comdat_can_be_unshared_p (symtab_node *node)
727 {
728 if (!comdat_can_be_unshared_p_1 (node))
729 return false;
730 if (node->same_comdat_group)
731 {
732 symtab_node *next;
733
734 /* If more than one function is in the same COMDAT group, it must
735 be shared even if just one function in the comdat group has
736 address taken. */
737 for (next = node->same_comdat_group;
738 next != node; next = next->same_comdat_group)
739 if (!comdat_can_be_unshared_p_1 (next))
740 return false;
741 }
742 return true;
743 }
744
745 /* Return true when function NODE should be considered externally visible. */
746
747 static bool
748 cgraph_externally_visible_p (struct cgraph_node *node,
749 bool whole_program)
750 {
751 if (!node->definition)
752 return false;
753 if (!TREE_PUBLIC (node->decl)
754 || DECL_EXTERNAL (node->decl))
755 return false;
756
757 /* Do not try to localize built-in functions yet. One of problems is that we
758 end up mangling their asm for WHOPR that makes it impossible to call them
759 using the implicit built-in declarations anymore. Similarly this enables
760 us to remove them as unreachable before actual calls may appear during
761 expansion or folding. */
762 if (DECL_BUILT_IN (node->decl))
763 return true;
764
765 /* If linker counts on us, we must preserve the function. */
766 if (symtab_used_from_object_file_p (node))
767 return true;
768 if (DECL_PRESERVE_P (node->decl))
769 return true;
770 if (lookup_attribute ("externally_visible",
771 DECL_ATTRIBUTES (node->decl)))
772 return true;
773 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
774 && lookup_attribute ("dllexport",
775 DECL_ATTRIBUTES (node->decl)))
776 return true;
777 if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
778 return false;
779 /* When doing LTO or whole program, we can bring COMDAT functoins static.
780 This improves code quality and we know we will duplicate them at most twice
781 (in the case that we are not using plugin and link with object file
782 implementing same COMDAT) */
783 if ((in_lto_p || whole_program)
784 && DECL_COMDAT (node->decl)
785 && comdat_can_be_unshared_p (node))
786 return false;
787
788 /* When doing link time optimizations, hidden symbols become local. */
789 if (in_lto_p
790 && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
791 || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
792 /* Be sure that node is defined in IR file, not in other object
793 file. In that case we don't set used_from_other_object_file. */
794 && node->definition)
795 ;
796 else if (!whole_program)
797 return true;
798
799 if (MAIN_NAME_P (DECL_NAME (node->decl)))
800 return true;
801
802 return false;
803 }
804
805 /* Return true when variable VNODE should be considered externally visible. */
806
807 bool
808 varpool_externally_visible_p (varpool_node *vnode)
809 {
810 if (DECL_EXTERNAL (vnode->decl))
811 return true;
812
813 if (!TREE_PUBLIC (vnode->decl))
814 return false;
815
816 /* If linker counts on us, we must preserve the function. */
817 if (symtab_used_from_object_file_p (vnode))
818 return true;
819
820 if (DECL_HARD_REGISTER (vnode->decl))
821 return true;
822 if (DECL_PRESERVE_P (vnode->decl))
823 return true;
824 if (lookup_attribute ("externally_visible",
825 DECL_ATTRIBUTES (vnode->decl)))
826 return true;
827 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
828 && lookup_attribute ("dllexport",
829 DECL_ATTRIBUTES (vnode->decl)))
830 return true;
831
832 /* See if we have linker information about symbol not being used or
833 if we need to make guess based on the declaration.
834
835 Even if the linker clams the symbol is unused, never bring internal
836 symbols that are declared by user as used or externally visible.
837 This is needed for i.e. references from asm statements. */
838 if (symtab_used_from_object_file_p (vnode))
839 return true;
840 if (vnode->resolution == LDPR_PREVAILING_DEF_IRONLY)
841 return false;
842
843 /* As a special case, the COMDAT virtual tables can be unshared.
844 In LTO mode turn vtables into static variables. The variable is readonly,
845 so this does not enable more optimization, but referring static var
846 is faster for dynamic linking. Also this match logic hidding vtables
847 from LTO symbol tables. */
848 if ((in_lto_p || flag_whole_program)
849 && DECL_COMDAT (vnode->decl)
850 && comdat_can_be_unshared_p (vnode))
851 return false;
852
853 /* When doing link time optimizations, hidden symbols become local. */
854 if (in_lto_p
855 && (DECL_VISIBILITY (vnode->decl) == VISIBILITY_HIDDEN
856 || DECL_VISIBILITY (vnode->decl) == VISIBILITY_INTERNAL)
857 /* Be sure that node is defined in IR file, not in other object
858 file. In that case we don't set used_from_other_object_file. */
859 && vnode->definition)
860 ;
861 else if (!flag_whole_program)
862 return true;
863
864 /* Do not attempt to privatize COMDATS by default.
865 This would break linking with C++ libraries sharing
866 inline definitions.
867
868 FIXME: We can do so for readonly vars with no address taken and
869 possibly also for vtables since no direct pointer comparsion is done.
870 It might be interesting to do so to reduce linking overhead. */
871 if (DECL_COMDAT (vnode->decl) || DECL_WEAK (vnode->decl))
872 return true;
873 return false;
874 }
875
876 /* Return true if reference to NODE can be replaced by a local alias.
877 Local aliases save dynamic linking overhead and enable more optimizations.
878 */
879
880 bool
881 can_replace_by_local_alias (symtab_node *node)
882 {
883 return (symtab_node_availability (node) > AVAIL_OVERWRITABLE
884 && !symtab_can_be_discarded (node));
885 }
886
887 /* Mark visibility of all functions.
888
889 A local function is one whose calls can occur only in the current
890 compilation unit and all its calls are explicit, so we can change
891 its calling convention. We simply mark all static functions whose
892 address is not taken as local.
893
894 We also change the TREE_PUBLIC flag of all declarations that are public
895 in language point of view but we want to overwrite this default
896 via visibilities for the backend point of view. */
897
898 static unsigned int
899 function_and_variable_visibility (bool whole_program)
900 {
901 struct cgraph_node *node;
902 varpool_node *vnode;
903
904 /* All aliases should be procssed at this point. */
905 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
906
907 FOR_EACH_FUNCTION (node)
908 {
909 int flags = flags_from_decl_or_type (node->decl);
910
911 /* Optimize away PURE and CONST constructors and destructors. */
912 if (optimize
913 && (flags & (ECF_CONST | ECF_PURE))
914 && !(flags & ECF_LOOPING_CONST_OR_PURE))
915 {
916 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
917 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
918 }
919
920 /* Frontends and alias code marks nodes as needed before parsing is finished.
921 We may end up marking as node external nodes where this flag is meaningless
922 strip it. */
923 if (DECL_EXTERNAL (node->decl) || !node->definition)
924 {
925 node->force_output = 0;
926 node->forced_by_abi = 0;
927 }
928
929 /* C++ FE on lack of COMDAT support create local COMDAT functions
930 (that ought to be shared but can not due to object format
931 limitations). It is necessary to keep the flag to make rest of C++ FE
932 happy. Clear the flag here to avoid confusion in middle-end. */
933 if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
934 DECL_COMDAT (node->decl) = 0;
935
936 /* For external decls stop tracking same_comdat_group. It doesn't matter
937 what comdat group they are in when they won't be emitted in this TU. */
938 if (node->same_comdat_group && DECL_EXTERNAL (node->decl))
939 {
940 #ifdef ENABLE_CHECKING
941 symtab_node *n;
942
943 for (n = node->same_comdat_group;
944 n != node;
945 n = n->same_comdat_group)
946 /* If at least one of same comdat group functions is external,
947 all of them have to be, otherwise it is a front-end bug. */
948 gcc_assert (DECL_EXTERNAL (n->decl));
949 #endif
950 symtab_dissolve_same_comdat_group_list (node);
951 }
952 gcc_assert ((!DECL_WEAK (node->decl)
953 && !DECL_COMDAT (node->decl))
954 || TREE_PUBLIC (node->decl)
955 || node->weakref
956 || DECL_EXTERNAL (node->decl));
957 if (cgraph_externally_visible_p (node, whole_program))
958 {
959 gcc_assert (!node->global.inlined_to);
960 node->externally_visible = true;
961 }
962 else
963 {
964 node->externally_visible = false;
965 node->forced_by_abi = false;
966 }
967 if (!node->externally_visible
968 && node->definition && !node->weakref
969 && !DECL_EXTERNAL (node->decl))
970 {
971 gcc_assert (whole_program || in_lto_p
972 || !TREE_PUBLIC (node->decl));
973 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
974 || node->unique_name
975 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
976 && TREE_PUBLIC (node->decl));
977 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
978 if (node->same_comdat_group && TREE_PUBLIC (node->decl))
979 {
980 symtab_node *next = node;
981
982 /* Set all members of comdat group local. */
983 if (node->same_comdat_group)
984 for (next = node->same_comdat_group;
985 next != node;
986 next = next->same_comdat_group)
987 {
988 symtab_make_decl_local (next->decl);
989 next->unique_name = ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
990 || next->unique_name
991 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
992 && TREE_PUBLIC (next->decl));
993 }
994 /* cgraph_externally_visible_p has already checked all other nodes
995 in the group and they will all be made local. We need to
996 dissolve the group at once so that the predicate does not
997 segfault though. */
998 symtab_dissolve_same_comdat_group_list (node);
999 }
1000 symtab_make_decl_local (node->decl);
1001 }
1002
1003 if (node->thunk.thunk_p
1004 && TREE_PUBLIC (node->decl))
1005 {
1006 struct cgraph_node *decl_node = node;
1007
1008 decl_node = cgraph_function_node (decl_node->callees->callee, NULL);
1009
1010 /* Thunks have the same visibility as function they are attached to.
1011 Make sure the C++ front end set this up properly. */
1012 if (DECL_ONE_ONLY (decl_node->decl))
1013 {
1014 gcc_checking_assert (DECL_COMDAT (node->decl)
1015 == DECL_COMDAT (decl_node->decl));
1016 gcc_checking_assert (DECL_COMDAT_GROUP (node->decl)
1017 == DECL_COMDAT_GROUP (decl_node->decl));
1018 gcc_checking_assert (node->same_comdat_group);
1019 }
1020 if (DECL_EXTERNAL (decl_node->decl))
1021 DECL_EXTERNAL (node->decl) = 1;
1022 }
1023
1024 /* If whole comdat group is used only within LTO code, we can dissolve it,
1025 we handle the unification ourselves.
1026 We keep COMDAT and weak so visibility out of DSO does not change.
1027 Later we may bring the symbols static if they are not exported. */
1028 if (DECL_ONE_ONLY (node->decl)
1029 && (node->resolution == LDPR_PREVAILING_DEF_IRONLY
1030 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP))
1031 {
1032 symtab_node *next = node;
1033
1034 if (node->same_comdat_group)
1035 for (next = node->same_comdat_group;
1036 next != node;
1037 next = next->same_comdat_group)
1038 if (next->externally_visible
1039 && (next->resolution != LDPR_PREVAILING_DEF_IRONLY
1040 && next->resolution != LDPR_PREVAILING_DEF_IRONLY_EXP))
1041 break;
1042 if (node == next)
1043 {
1044 if (node->same_comdat_group)
1045 for (next = node->same_comdat_group;
1046 next != node;
1047 next = next->same_comdat_group)
1048 {
1049 DECL_COMDAT_GROUP (next->decl) = NULL;
1050 DECL_WEAK (next->decl) = false;
1051 }
1052 DECL_COMDAT_GROUP (node->decl) = NULL;
1053 symtab_dissolve_same_comdat_group_list (node);
1054 }
1055 }
1056 }
1057 FOR_EACH_DEFINED_FUNCTION (node)
1058 {
1059 node->local.local |= cgraph_local_node_p (node);
1060
1061 /* If we know that function can not be overwritten by a different semantics
1062 and moreover its section can not be discarded, replace all direct calls
1063 by calls to an nonoverwritable alias. This make dynamic linking
1064 cheaper and enable more optimization.
1065
1066 TODO: We can also update virtual tables. */
1067 if (node->callers && can_replace_by_local_alias (node))
1068 {
1069 struct cgraph_node *alias = cgraph (symtab_nonoverwritable_alias (node));
1070
1071 if (alias && alias != node)
1072 {
1073 while (node->callers)
1074 {
1075 struct cgraph_edge *e = node->callers;
1076
1077 cgraph_redirect_edge_callee (e, alias);
1078 if (gimple_has_body_p (e->caller->decl))
1079 {
1080 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1081 cgraph_redirect_edge_call_stmt_to_callee (e);
1082 pop_cfun ();
1083 }
1084 }
1085 }
1086 }
1087 }
1088 FOR_EACH_VARIABLE (vnode)
1089 {
1090 /* weak flag makes no sense on local variables. */
1091 gcc_assert (!DECL_WEAK (vnode->decl)
1092 || vnode->weakref
1093 || TREE_PUBLIC (vnode->decl)
1094 || DECL_EXTERNAL (vnode->decl));
1095 /* In several cases declarations can not be common:
1096
1097 - when declaration has initializer
1098 - when it is in weak
1099 - when it has specific section
1100 - when it resides in non-generic address space.
1101 - if declaration is local, it will get into .local common section
1102 so common flag is not needed. Frontends still produce these in
1103 certain cases, such as for:
1104
1105 static int a __attribute__ ((common))
1106
1107 Canonicalize things here and clear the redundant flag. */
1108 if (DECL_COMMON (vnode->decl)
1109 && (!(TREE_PUBLIC (vnode->decl)
1110 || DECL_EXTERNAL (vnode->decl))
1111 || (DECL_INITIAL (vnode->decl)
1112 && DECL_INITIAL (vnode->decl) != error_mark_node)
1113 || DECL_WEAK (vnode->decl)
1114 || DECL_SECTION_NAME (vnode->decl) != NULL
1115 || ! (ADDR_SPACE_GENERIC_P
1116 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
1117 DECL_COMMON (vnode->decl) = 0;
1118 }
1119 FOR_EACH_DEFINED_VARIABLE (vnode)
1120 {
1121 if (!vnode->definition)
1122 continue;
1123 if (varpool_externally_visible_p (vnode))
1124 vnode->externally_visible = true;
1125 else
1126 {
1127 vnode->externally_visible = false;
1128 vnode->forced_by_abi = false;
1129 }
1130 if (!vnode->externally_visible
1131 && !vnode->weakref)
1132 {
1133 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->decl));
1134 vnode->unique_name = ((vnode->resolution == LDPR_PREVAILING_DEF_IRONLY
1135 || vnode->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
1136 && TREE_PUBLIC (vnode->decl));
1137 symtab_make_decl_local (vnode->decl);
1138 if (vnode->same_comdat_group)
1139 symtab_dissolve_same_comdat_group_list (vnode);
1140 vnode->resolution = LDPR_PREVAILING_DEF_IRONLY;
1141 }
1142 }
1143
1144 if (dump_file)
1145 {
1146 fprintf (dump_file, "\nMarking local functions:");
1147 FOR_EACH_DEFINED_FUNCTION (node)
1148 if (node->local.local)
1149 fprintf (dump_file, " %s", node->name ());
1150 fprintf (dump_file, "\n\n");
1151 fprintf (dump_file, "\nMarking externally visible functions:");
1152 FOR_EACH_DEFINED_FUNCTION (node)
1153 if (node->externally_visible)
1154 fprintf (dump_file, " %s", node->name ());
1155 fprintf (dump_file, "\n\n");
1156 fprintf (dump_file, "\nMarking externally visible variables:");
1157 FOR_EACH_DEFINED_VARIABLE (vnode)
1158 if (vnode->externally_visible)
1159 fprintf (dump_file, " %s", vnode->name ());
1160 fprintf (dump_file, "\n\n");
1161 }
1162 cgraph_function_flags_ready = true;
1163 return 0;
1164 }
1165
1166 /* Local function pass handling visibilities. This happens before LTO streaming
1167 so in particular -fwhole-program should be ignored at this level. */
1168
1169 static unsigned int
1170 local_function_and_variable_visibility (void)
1171 {
1172 return function_and_variable_visibility (flag_whole_program && !flag_lto);
1173 }
1174
1175 namespace {
1176
1177 const pass_data pass_data_ipa_function_and_variable_visibility =
1178 {
1179 SIMPLE_IPA_PASS, /* type */
1180 "visibility", /* name */
1181 OPTGROUP_NONE, /* optinfo_flags */
1182 false, /* has_gate */
1183 true, /* has_execute */
1184 TV_CGRAPHOPT, /* tv_id */
1185 0, /* properties_required */
1186 0, /* properties_provided */
1187 0, /* properties_destroyed */
1188 0, /* todo_flags_start */
1189 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1190 };
1191
1192 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
1193 {
1194 public:
1195 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1196 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
1197 ctxt)
1198 {}
1199
1200 /* opt_pass methods: */
1201 unsigned int execute () {
1202 return local_function_and_variable_visibility ();
1203 }
1204
1205 }; // class pass_ipa_function_and_variable_visibility
1206
1207 } // anon namespace
1208
1209 simple_ipa_opt_pass *
1210 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1211 {
1212 return new pass_ipa_function_and_variable_visibility (ctxt);
1213 }
1214
1215 /* Free inline summary. */
1216
1217 static unsigned
1218 free_inline_summary (void)
1219 {
1220 inline_free_summary ();
1221 return 0;
1222 }
1223
1224 namespace {
1225
1226 const pass_data pass_data_ipa_free_inline_summary =
1227 {
1228 SIMPLE_IPA_PASS, /* type */
1229 "*free_inline_summary", /* name */
1230 OPTGROUP_NONE, /* optinfo_flags */
1231 false, /* has_gate */
1232 true, /* has_execute */
1233 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
1234 0, /* properties_required */
1235 0, /* properties_provided */
1236 0, /* properties_destroyed */
1237 0, /* todo_flags_start */
1238 0, /* todo_flags_finish */
1239 };
1240
1241 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
1242 {
1243 public:
1244 pass_ipa_free_inline_summary (gcc::context *ctxt)
1245 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
1246 {}
1247
1248 /* opt_pass methods: */
1249 unsigned int execute () { return free_inline_summary (); }
1250
1251 }; // class pass_ipa_free_inline_summary
1252
1253 } // anon namespace
1254
1255 simple_ipa_opt_pass *
1256 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
1257 {
1258 return new pass_ipa_free_inline_summary (ctxt);
1259 }
1260
1261 /* Do not re-run on ltrans stage. */
1262
1263 static bool
1264 gate_whole_program_function_and_variable_visibility (void)
1265 {
1266 return !flag_ltrans;
1267 }
1268
1269 /* Bring functionss local at LTO time with -fwhole-program. */
1270
1271 static unsigned int
1272 whole_program_function_and_variable_visibility (void)
1273 {
1274 function_and_variable_visibility (flag_whole_program);
1275 if (optimize)
1276 ipa_discover_readonly_nonaddressable_vars ();
1277 return 0;
1278 }
1279
1280 namespace {
1281
1282 const pass_data pass_data_ipa_whole_program_visibility =
1283 {
1284 IPA_PASS, /* type */
1285 "whole-program", /* name */
1286 OPTGROUP_NONE, /* optinfo_flags */
1287 true, /* has_gate */
1288 true, /* has_execute */
1289 TV_CGRAPHOPT, /* tv_id */
1290 0, /* properties_required */
1291 0, /* properties_provided */
1292 0, /* properties_destroyed */
1293 0, /* todo_flags_start */
1294 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1295 };
1296
1297 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
1298 {
1299 public:
1300 pass_ipa_whole_program_visibility (gcc::context *ctxt)
1301 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
1302 NULL, /* generate_summary */
1303 NULL, /* write_summary */
1304 NULL, /* read_summary */
1305 NULL, /* write_optimization_summary */
1306 NULL, /* read_optimization_summary */
1307 NULL, /* stmt_fixup */
1308 0, /* function_transform_todo_flags_start */
1309 NULL, /* function_transform */
1310 NULL) /* variable_transform */
1311 {}
1312
1313 /* opt_pass methods: */
1314 bool gate () {
1315 return gate_whole_program_function_and_variable_visibility ();
1316 }
1317 unsigned int execute () {
1318 return whole_program_function_and_variable_visibility ();
1319 }
1320
1321 }; // class pass_ipa_whole_program_visibility
1322
1323 } // anon namespace
1324
1325 ipa_opt_pass_d *
1326 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
1327 {
1328 return new pass_ipa_whole_program_visibility (ctxt);
1329 }
1330
1331 /* Generate and emit a static constructor or destructor. WHICH must
1332 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1333 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1334 initialization priority for this constructor or destructor.
1335
1336 FINAL specify whether the externally visible name for collect2 should
1337 be produced. */
1338
1339 static void
1340 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
1341 {
1342 static int counter = 0;
1343 char which_buf[16];
1344 tree decl, name, resdecl;
1345
1346 /* The priority is encoded in the constructor or destructor name.
1347 collect2 will sort the names and arrange that they are called at
1348 program startup. */
1349 if (final)
1350 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1351 else
1352 /* Proudce sane name but one not recognizable by collect2, just for the
1353 case we fail to inline the function. */
1354 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
1355 name = get_file_function_name (which_buf);
1356
1357 decl = build_decl (input_location, FUNCTION_DECL, name,
1358 build_function_type_list (void_type_node, NULL_TREE));
1359 current_function_decl = decl;
1360
1361 resdecl = build_decl (input_location,
1362 RESULT_DECL, NULL_TREE, void_type_node);
1363 DECL_ARTIFICIAL (resdecl) = 1;
1364 DECL_RESULT (decl) = resdecl;
1365 DECL_CONTEXT (resdecl) = decl;
1366
1367 allocate_struct_function (decl, false);
1368
1369 TREE_STATIC (decl) = 1;
1370 TREE_USED (decl) = 1;
1371 DECL_ARTIFICIAL (decl) = 1;
1372 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1373 DECL_SAVED_TREE (decl) = body;
1374 if (!targetm.have_ctors_dtors && final)
1375 {
1376 TREE_PUBLIC (decl) = 1;
1377 DECL_PRESERVE_P (decl) = 1;
1378 }
1379 DECL_UNINLINABLE (decl) = 1;
1380
1381 DECL_INITIAL (decl) = make_node (BLOCK);
1382 TREE_USED (DECL_INITIAL (decl)) = 1;
1383
1384 DECL_SOURCE_LOCATION (decl) = input_location;
1385 cfun->function_end_locus = input_location;
1386
1387 switch (which)
1388 {
1389 case 'I':
1390 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1391 decl_init_priority_insert (decl, priority);
1392 break;
1393 case 'D':
1394 DECL_STATIC_DESTRUCTOR (decl) = 1;
1395 decl_fini_priority_insert (decl, priority);
1396 break;
1397 default:
1398 gcc_unreachable ();
1399 }
1400
1401 gimplify_function_tree (decl);
1402
1403 cgraph_add_new_function (decl, false);
1404
1405 set_cfun (NULL);
1406 current_function_decl = NULL;
1407 }
1408
1409 /* Generate and emit a static constructor or destructor. WHICH must
1410 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1411 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1412 initialization priority for this constructor or destructor. */
1413
1414 void
1415 cgraph_build_static_cdtor (char which, tree body, int priority)
1416 {
1417 cgraph_build_static_cdtor_1 (which, body, priority, false);
1418 }
1419
1420 /* A vector of FUNCTION_DECLs declared as static constructors. */
1421 static vec<tree> static_ctors;
1422 /* A vector of FUNCTION_DECLs declared as static destructors. */
1423 static vec<tree> static_dtors;
1424
1425 /* When target does not have ctors and dtors, we call all constructor
1426 and destructor by special initialization/destruction function
1427 recognized by collect2.
1428
1429 When we are going to build this function, collect all constructors and
1430 destructors and turn them into normal functions. */
1431
1432 static void
1433 record_cdtor_fn (struct cgraph_node *node)
1434 {
1435 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1436 static_ctors.safe_push (node->decl);
1437 if (DECL_STATIC_DESTRUCTOR (node->decl))
1438 static_dtors.safe_push (node->decl);
1439 node = cgraph_get_node (node->decl);
1440 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
1441 }
1442
1443 /* Define global constructors/destructor functions for the CDTORS, of
1444 which they are LEN. The CDTORS are sorted by initialization
1445 priority. If CTOR_P is true, these are constructors; otherwise,
1446 they are destructors. */
1447
1448 static void
1449 build_cdtor (bool ctor_p, vec<tree> cdtors)
1450 {
1451 size_t i,j;
1452 size_t len = cdtors.length ();
1453
1454 i = 0;
1455 while (i < len)
1456 {
1457 tree body;
1458 tree fn;
1459 priority_type priority;
1460
1461 priority = 0;
1462 body = NULL_TREE;
1463 j = i;
1464 do
1465 {
1466 priority_type p;
1467 fn = cdtors[j];
1468 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1469 if (j == i)
1470 priority = p;
1471 else if (p != priority)
1472 break;
1473 j++;
1474 }
1475 while (j < len);
1476
1477 /* When there is only one cdtor and target supports them, do nothing. */
1478 if (j == i + 1
1479 && targetm.have_ctors_dtors)
1480 {
1481 i++;
1482 continue;
1483 }
1484 /* Find the next batch of constructors/destructors with the same
1485 initialization priority. */
1486 for (;i < j; i++)
1487 {
1488 tree call;
1489 fn = cdtors[i];
1490 call = build_call_expr (fn, 0);
1491 if (ctor_p)
1492 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1493 else
1494 DECL_STATIC_DESTRUCTOR (fn) = 0;
1495 /* We do not want to optimize away pure/const calls here.
1496 When optimizing, these should be already removed, when not
1497 optimizing, we want user to be able to breakpoint in them. */
1498 TREE_SIDE_EFFECTS (call) = 1;
1499 append_to_statement_list (call, &body);
1500 }
1501 gcc_assert (body != NULL_TREE);
1502 /* Generate a function to call all the function of like
1503 priority. */
1504 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1505 }
1506 }
1507
1508 /* Comparison function for qsort. P1 and P2 are actually of type
1509 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1510 used to determine the sort order. */
1511
1512 static int
1513 compare_ctor (const void *p1, const void *p2)
1514 {
1515 tree f1;
1516 tree f2;
1517 int priority1;
1518 int priority2;
1519
1520 f1 = *(const tree *)p1;
1521 f2 = *(const tree *)p2;
1522 priority1 = DECL_INIT_PRIORITY (f1);
1523 priority2 = DECL_INIT_PRIORITY (f2);
1524
1525 if (priority1 < priority2)
1526 return -1;
1527 else if (priority1 > priority2)
1528 return 1;
1529 else
1530 /* Ensure a stable sort. Constructors are executed in backwarding
1531 order to make LTO initialize braries first. */
1532 return DECL_UID (f2) - DECL_UID (f1);
1533 }
1534
1535 /* Comparison function for qsort. P1 and P2 are actually of type
1536 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1537 used to determine the sort order. */
1538
1539 static int
1540 compare_dtor (const void *p1, const void *p2)
1541 {
1542 tree f1;
1543 tree f2;
1544 int priority1;
1545 int priority2;
1546
1547 f1 = *(const tree *)p1;
1548 f2 = *(const tree *)p2;
1549 priority1 = DECL_FINI_PRIORITY (f1);
1550 priority2 = DECL_FINI_PRIORITY (f2);
1551
1552 if (priority1 < priority2)
1553 return -1;
1554 else if (priority1 > priority2)
1555 return 1;
1556 else
1557 /* Ensure a stable sort. */
1558 return DECL_UID (f1) - DECL_UID (f2);
1559 }
1560
1561 /* Generate functions to call static constructors and destructors
1562 for targets that do not support .ctors/.dtors sections. These
1563 functions have magic names which are detected by collect2. */
1564
1565 static void
1566 build_cdtor_fns (void)
1567 {
1568 if (!static_ctors.is_empty ())
1569 {
1570 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1571 static_ctors.qsort (compare_ctor);
1572 build_cdtor (/*ctor_p=*/true, static_ctors);
1573 }
1574
1575 if (!static_dtors.is_empty ())
1576 {
1577 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1578 static_dtors.qsort (compare_dtor);
1579 build_cdtor (/*ctor_p=*/false, static_dtors);
1580 }
1581 }
1582
1583 /* Look for constructors and destructors and produce function calling them.
1584 This is needed for targets not supporting ctors or dtors, but we perform the
1585 transformation also at linktime to merge possibly numerous
1586 constructors/destructors into single function to improve code locality and
1587 reduce size. */
1588
1589 static unsigned int
1590 ipa_cdtor_merge (void)
1591 {
1592 struct cgraph_node *node;
1593 FOR_EACH_DEFINED_FUNCTION (node)
1594 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1595 || DECL_STATIC_DESTRUCTOR (node->decl))
1596 record_cdtor_fn (node);
1597 build_cdtor_fns ();
1598 static_ctors.release ();
1599 static_dtors.release ();
1600 return 0;
1601 }
1602
1603 /* Perform the pass when we have no ctors/dtors support
1604 or at LTO time to merge multiple constructors into single
1605 function. */
1606
1607 static bool
1608 gate_ipa_cdtor_merge (void)
1609 {
1610 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1611 }
1612
1613 namespace {
1614
1615 const pass_data pass_data_ipa_cdtor_merge =
1616 {
1617 IPA_PASS, /* type */
1618 "cdtor", /* name */
1619 OPTGROUP_NONE, /* optinfo_flags */
1620 true, /* has_gate */
1621 true, /* has_execute */
1622 TV_CGRAPHOPT, /* tv_id */
1623 0, /* properties_required */
1624 0, /* properties_provided */
1625 0, /* properties_destroyed */
1626 0, /* todo_flags_start */
1627 0, /* todo_flags_finish */
1628 };
1629
1630 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1631 {
1632 public:
1633 pass_ipa_cdtor_merge (gcc::context *ctxt)
1634 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1635 NULL, /* generate_summary */
1636 NULL, /* write_summary */
1637 NULL, /* read_summary */
1638 NULL, /* write_optimization_summary */
1639 NULL, /* read_optimization_summary */
1640 NULL, /* stmt_fixup */
1641 0, /* function_transform_todo_flags_start */
1642 NULL, /* function_transform */
1643 NULL) /* variable_transform */
1644 {}
1645
1646 /* opt_pass methods: */
1647 bool gate () { return gate_ipa_cdtor_merge (); }
1648 unsigned int execute () { return ipa_cdtor_merge (); }
1649
1650 }; // class pass_ipa_cdtor_merge
1651
1652 } // anon namespace
1653
1654 ipa_opt_pass_d *
1655 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1656 {
1657 return new pass_ipa_cdtor_merge (ctxt);
1658 }