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