PR c/61077
[official-gcc.git] / gcc / ipa.c
blob8198b174920f97cc2388c57e3fefc47d21f5de4c
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "calls.h"
26 #include "stringpool.h"
27 #include "cgraph.h"
28 #include "tree-pass.h"
29 #include "hash-map.h"
30 #include "pointer-set.h"
31 #include "gimple-expr.h"
32 #include "gimplify.h"
33 #include "flags.h"
34 #include "target.h"
35 #include "tree-iterator.h"
36 #include "ipa-utils.h"
37 #include "ipa-inline.h"
38 #include "tree-inline.h"
39 #include "profile.h"
40 #include "params.h"
41 #include "internal-fn.h"
42 #include "tree-ssa-alias.h"
43 #include "gimple.h"
44 #include "dbgcnt.h"
47 /* Return true when NODE has ADDR reference. */
49 static bool
50 has_addr_references_p (struct cgraph_node *node,
51 void *data ATTRIBUTE_UNUSED)
53 int i;
54 struct ipa_ref *ref = NULL;
56 for (i = 0; node->iterate_referring (i, ref); i++)
57 if (ref->use == IPA_REF_ADDR)
58 return true;
59 return false;
62 /* Look for all functions inlined to NODE and update their inlined_to pointers
63 to INLINED_TO. */
65 static void
66 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
68 struct cgraph_edge *e;
69 for (e = node->callees; e; e = e->next_callee)
70 if (e->callee->global.inlined_to)
72 e->callee->global.inlined_to = inlined_to;
73 update_inlined_to_pointer (e->callee, inlined_to);
77 /* Add symtab NODE to queue starting at FIRST.
79 The queue is linked via AUX pointers and terminated by pointer to 1.
80 We enqueue nodes at two occasions: when we find them reachable or when we find
81 their bodies needed for further clonning. In the second case we mark them
82 by pointer to 2 after processing so they are re-queue when they become
83 reachable. */
85 static void
86 enqueue_node (symtab_node *node, symtab_node **first,
87 struct pointer_set_t *reachable)
89 /* Node is still in queue; do nothing. */
90 if (node->aux && node->aux != (void *) 2)
91 return;
92 /* Node was already processed as unreachable, re-enqueue
93 only if it became reachable now. */
94 if (node->aux == (void *)2 && !pointer_set_contains (reachable, node))
95 return;
96 node->aux = *first;
97 *first = node;
100 /* Process references. */
102 static void
103 process_references (symtab_node *snode,
104 symtab_node **first,
105 bool before_inlining_p,
106 struct pointer_set_t *reachable)
108 int i;
109 struct ipa_ref *ref = NULL;
110 for (i = 0; snode->iterate_reference (i, ref); i++)
112 symtab_node *node = ref->referred;
114 if (node->definition && !node->in_other_partition
115 && ((!DECL_EXTERNAL (node->decl) || node->alias)
116 || (((before_inlining_p
117 && (cgraph_state < CGRAPH_STATE_IPA_SSA
118 || !lookup_attribute ("always_inline",
119 DECL_ATTRIBUTES (node->decl)))))
120 /* We use variable constructors during late complation for
121 constant folding. Keep references alive so partitioning
122 knows about potential references. */
123 || (TREE_CODE (node->decl) == VAR_DECL
124 && flag_wpa
125 && ctor_for_folding (node->decl)
126 != error_mark_node))))
127 pointer_set_insert (reachable, node);
128 enqueue_node (node, first, reachable);
132 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
133 all its potential targets as reachable to permit later inlining if
134 devirtualization happens. After inlining still keep their declarations
135 around, so we can devirtualize to a direct call.
137 Also try to make trivial devirutalization when no or only one target is
138 possible. */
140 static void
141 walk_polymorphic_call_targets (pointer_set_t *reachable_call_targets,
142 struct cgraph_edge *edge,
143 symtab_node **first,
144 pointer_set_t *reachable, bool before_inlining_p)
146 unsigned int i;
147 void *cache_token;
148 bool final;
149 vec <cgraph_node *>targets
150 = possible_polymorphic_call_targets
151 (edge, &final, &cache_token);
153 if (!pointer_set_insert (reachable_call_targets,
154 cache_token))
156 for (i = 0; i < targets.length (); i++)
158 struct cgraph_node *n = targets[i];
160 /* Do not bother to mark virtual methods in anonymous namespace;
161 either we will find use of virtual table defining it, or it is
162 unused. */
163 if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
164 && type_in_anonymous_namespace_p
165 (method_class_type (TREE_TYPE (n->decl))))
166 continue;
168 /* Prior inlining, keep alive bodies of possible targets for
169 devirtualization. */
170 if (n->definition
171 && (before_inlining_p
172 && (cgraph_state < CGRAPH_STATE_IPA_SSA
173 || !lookup_attribute ("always_inline",
174 DECL_ATTRIBUTES (n->decl)))))
175 pointer_set_insert (reachable, n);
177 /* Even after inlining we want to keep the possible targets in the
178 boundary, so late passes can still produce direct call even if
179 the chance for inlining is lost. */
180 enqueue_node (n, first, reachable);
184 /* Very trivial devirtualization; when the type is
185 final or anonymous (so we know all its derivation)
186 and there is only one possible virtual call target,
187 make the edge direct. */
188 if (final)
190 if (targets.length () <= 1 && dbg_cnt (devirt))
192 cgraph_node *target, *node = edge->caller;
193 if (targets.length () == 1)
194 target = targets[0];
195 else
196 target = cgraph_node::get_create
197 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
199 if (dump_enabled_p ())
201 location_t locus = gimple_location (edge->call_stmt);
202 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
203 "devirtualizing call in %s/%i to %s/%i\n",
204 edge->caller->name (), edge->caller->order,
205 target->name (),
206 target->order);
208 edge = cgraph_make_edge_direct (edge, target);
209 if (inline_summary_vec)
210 inline_update_overall_summary (node);
211 else if (edge->call_stmt)
212 cgraph_redirect_edge_call_stmt_to_callee (edge);
217 /* Perform reachability analysis and reclaim all unreachable nodes.
219 The algorithm is basically mark&sweep but with some extra refinements:
221 - reachable extern inline functions needs special handling; the bodies needs
222 to stay in memory until inlining in hope that they will be inlined.
223 After inlining we release their bodies and turn them into unanalyzed
224 nodes even when they are reachable.
226 BEFORE_INLINING_P specify whether we are before or after inlining.
228 - virtual functions are kept in callgraph even if they seem unreachable in
229 hope calls to them will be devirtualized.
231 Again we remove them after inlining. In late optimization some
232 devirtualization may happen, but it is not important since we won't inline
233 the call. In theory early opts and IPA should work out all important cases.
235 - virtual clones needs bodies of their origins for later materialization;
236 this means that we want to keep the body even if the origin is unreachable
237 otherwise. To avoid origin from sitting in the callgraph and being
238 walked by IPA passes, we turn them into unanalyzed nodes with body
239 defined.
241 We maintain set of function declaration where body needs to stay in
242 body_needed_for_clonning
244 Inline clones represent special case: their declaration match the
245 declaration of origin and cgraph_remove_node already knows how to
246 reshape callgraph and preserve body when offline copy of function or
247 inline clone is being removed.
249 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
250 variables with DECL_INITIAL set. We finalize these and keep reachable
251 ones around for constant folding purposes. After inlining we however
252 stop walking their references to let everything static referneced by them
253 to be removed when it is otherwise unreachable.
255 We maintain queue of both reachable symbols (i.e. defined symbols that needs
256 to stay) and symbols that are in boundary (i.e. external symbols referenced
257 by reachable symbols or origins of clones). The queue is represented
258 as linked list by AUX pointer terminated by 1.
260 At the end we keep all reachable symbols. For symbols in boundary we always
261 turn definition into a declaration, but we may keep function body around
262 based on body_needed_for_clonning
264 All symbols that enter the queue have AUX pointer non-zero and are in the
265 boundary. Pointer set REACHABLE is used to track reachable symbols.
267 Every symbol can be visited twice - once as part of boundary and once
268 as real reachable symbol. enqueue_node needs to decide whether the
269 node needs to be re-queued for second processing. For this purpose
270 we set AUX pointer of processed symbols in the boundary to constant 2. */
272 bool
273 symtab_remove_unreachable_nodes (bool before_inlining_p, FILE *file)
275 symtab_node *first = (symtab_node *) (void *) 1;
276 struct cgraph_node *node, *next;
277 varpool_node *vnode, *vnext;
278 bool changed = false;
279 struct pointer_set_t *reachable = pointer_set_create ();
280 struct pointer_set_t *body_needed_for_clonning = pointer_set_create ();
281 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
283 timevar_push (TV_IPA_UNREACHABLE);
284 if (optimize && flag_devirtualize)
285 build_type_inheritance_graph ();
286 if (file)
287 fprintf (file, "\nReclaiming functions:");
288 #ifdef ENABLE_CHECKING
289 FOR_EACH_FUNCTION (node)
290 gcc_assert (!node->aux);
291 FOR_EACH_VARIABLE (vnode)
292 gcc_assert (!vnode->aux);
293 #endif
294 /* Mark functions whose bodies are obviously needed.
295 This is mostly when they can be referenced externally. Inline clones
296 are special since their declarations are shared with master clone and thus
297 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
298 FOR_EACH_FUNCTION (node)
300 node->used_as_abstract_origin = false;
301 if (node->definition
302 && !node->global.inlined_to
303 && !node->in_other_partition
304 && !node->can_remove_if_no_direct_calls_and_refs_p ())
306 gcc_assert (!node->global.inlined_to);
307 pointer_set_insert (reachable, node);
308 enqueue_node (node, &first, reachable);
310 else
311 gcc_assert (!node->aux);
314 /* Mark variables that are obviously needed. */
315 FOR_EACH_DEFINED_VARIABLE (vnode)
316 if (!vnode->can_remove_if_no_refs_p()
317 && !vnode->in_other_partition)
319 pointer_set_insert (reachable, vnode);
320 enqueue_node (vnode, &first, reachable);
323 /* Perform reachability analysis. */
324 while (first != (symtab_node *) (void *) 1)
326 bool in_boundary_p = !pointer_set_contains (reachable, first);
327 symtab_node *node = first;
329 first = (symtab_node *)first->aux;
331 /* If we are processing symbol in boundary, mark its AUX pointer for
332 possible later re-processing in enqueue_node. */
333 if (in_boundary_p)
334 node->aux = (void *)2;
335 else
337 if (TREE_CODE (node->decl) == FUNCTION_DECL
338 && DECL_ABSTRACT_ORIGIN (node->decl))
340 struct cgraph_node *origin_node
341 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (node->decl));
342 origin_node->used_as_abstract_origin = true;
343 enqueue_node (origin_node, &first, reachable);
345 /* If any symbol in a comdat group is reachable, force
346 all externally visible symbols in the same comdat
347 group to be reachable as well. Comdat-local symbols
348 can be discarded if all uses were inlined. */
349 if (node->same_comdat_group)
351 symtab_node *next;
352 for (next = node->same_comdat_group;
353 next != node;
354 next = next->same_comdat_group)
355 if (!next->comdat_local_p ()
356 && !pointer_set_insert (reachable, next))
357 enqueue_node (next, &first, reachable);
359 /* Mark references as reachable. */
360 process_references (node, &first, before_inlining_p, reachable);
363 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
365 /* Mark the callees reachable unless they are direct calls to extern
366 inline functions we decided to not inline. */
367 if (!in_boundary_p)
369 struct cgraph_edge *e;
370 /* Keep alive possible targets for devirtualization. */
371 if (optimize && flag_devirtualize)
373 struct cgraph_edge *next;
374 for (e = cnode->indirect_calls; e; e = next)
376 next = e->next_callee;
377 if (e->indirect_info->polymorphic)
378 walk_polymorphic_call_targets (reachable_call_targets,
379 e, &first, reachable,
380 before_inlining_p);
383 for (e = cnode->callees; e; e = e->next_callee)
385 if (e->callee->definition
386 && !e->callee->in_other_partition
387 && (!e->inline_failed
388 || !DECL_EXTERNAL (e->callee->decl)
389 || e->callee->alias
390 || before_inlining_p))
392 /* Be sure that we will not optimize out alias target
393 body. */
394 if (DECL_EXTERNAL (e->callee->decl)
395 && e->callee->alias
396 && before_inlining_p)
397 pointer_set_insert (reachable,
398 e->callee->function_symbol ());
399 pointer_set_insert (reachable, e->callee);
401 enqueue_node (e->callee, &first, reachable);
404 /* When inline clone exists, mark body to be preserved so when removing
405 offline copy of the function we don't kill it. */
406 if (cnode->global.inlined_to)
407 pointer_set_insert (body_needed_for_clonning, cnode->decl);
409 /* For non-inline clones, force their origins to the boundary and ensure
410 that body is not removed. */
411 while (cnode->clone_of)
413 bool noninline = cnode->clone_of->decl != cnode->decl;
414 cnode = cnode->clone_of;
415 if (noninline)
417 pointer_set_insert (body_needed_for_clonning, cnode->decl);
418 enqueue_node (cnode, &first, reachable);
423 /* If any reachable function has simd clones, mark them as
424 reachable as well. */
425 if (cnode->simd_clones)
427 cgraph_node *next;
428 for (next = cnode->simd_clones;
429 next;
430 next = next->simdclone->next_clone)
431 if (in_boundary_p
432 || !pointer_set_insert (reachable, next))
433 enqueue_node (next, &first, reachable);
436 /* When we see constructor of external variable, keep referred nodes in the
437 boundary. This will also hold initializers of the external vars NODE
438 refers to. */
439 varpool_node *vnode = dyn_cast <varpool_node *> (node);
440 if (vnode
441 && DECL_EXTERNAL (node->decl)
442 && !vnode->alias
443 && in_boundary_p)
445 struct ipa_ref *ref = NULL;
446 for (int i = 0; node->iterate_reference (i, ref); i++)
447 enqueue_node (ref->referred, &first, reachable);
451 /* Remove unreachable functions. */
452 for (node = cgraph_first_function (); node; node = next)
454 next = cgraph_next_function (node);
456 /* If node is not needed at all, remove it. */
457 if (!node->aux)
459 if (file)
460 fprintf (file, " %s/%i", node->name (), node->order);
461 node->remove ();
462 changed = true;
464 /* If node is unreachable, remove its body. */
465 else if (!pointer_set_contains (reachable, node))
467 if (!pointer_set_contains (body_needed_for_clonning, node->decl))
468 node->release_body ();
469 else if (!node->clone_of)
470 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
471 if (node->definition)
473 if (file)
474 fprintf (file, " %s/%i", node->name (), node->order);
475 node->body_removed = true;
476 node->analyzed = false;
477 node->definition = false;
478 node->cpp_implicit_alias = false;
479 node->alias = false;
480 node->thunk.thunk_p = false;
481 node->weakref = false;
482 /* After early inlining we drop always_inline attributes on
483 bodies of functions that are still referenced (have their
484 address taken). */
485 DECL_ATTRIBUTES (node->decl)
486 = remove_attribute ("always_inline",
487 DECL_ATTRIBUTES (node->decl));
488 if (!node->in_other_partition)
489 node->local.local = false;
490 node->remove_callees ();
491 node->remove_from_same_comdat_group ();
492 node->remove_all_references ();
493 changed = true;
496 else
497 gcc_assert (node->clone_of || !node->has_gimple_body_p ()
498 || in_lto_p || DECL_RESULT (node->decl));
501 /* Inline clones might be kept around so their materializing allows further
502 cloning. If the function the clone is inlined into is removed, we need
503 to turn it into normal cone. */
504 FOR_EACH_FUNCTION (node)
506 if (node->global.inlined_to
507 && !node->callers)
509 gcc_assert (node->clones);
510 node->global.inlined_to = NULL;
511 update_inlined_to_pointer (node, node);
513 node->aux = NULL;
516 /* Remove unreachable variables. */
517 if (file)
518 fprintf (file, "\nReclaiming variables:");
519 for (vnode = varpool_first_variable (); vnode; vnode = vnext)
521 vnext = varpool_next_variable (vnode);
522 if (!vnode->aux
523 /* For can_refer_decl_in_current_unit_p we want to track for
524 all external variables if they are defined in other partition
525 or not. */
526 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
528 if (file)
529 fprintf (file, " %s/%i", vnode->name (), vnode->order);
530 vnode->remove ();
531 changed = true;
533 else if (!pointer_set_contains (reachable, vnode))
535 tree init;
536 if (vnode->definition)
538 if (file)
539 fprintf (file, " %s", vnode->name ());
540 changed = true;
542 vnode->body_removed = true;
543 vnode->definition = false;
544 vnode->analyzed = false;
545 vnode->aux = NULL;
547 vnode->remove_from_same_comdat_group ();
549 /* Keep body if it may be useful for constant folding. */
550 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node)
551 vnode->remove_initializer ();
552 else
553 DECL_INITIAL (vnode->decl) = init;
554 vnode->remove_all_references ();
556 else
557 vnode->aux = NULL;
560 pointer_set_destroy (reachable);
561 pointer_set_destroy (body_needed_for_clonning);
562 pointer_set_destroy (reachable_call_targets);
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)
571 if (!node->call_for_symbol_thunks_and_aliases
572 (has_addr_references_p, NULL, true))
574 if (file)
575 fprintf (file, " %s", node->name ());
576 node->address_taken = false;
577 changed = true;
578 if (node->local_p ())
580 node->local.local = true;
581 if (file)
582 fprintf (file, " (local)");
586 if (file)
587 fprintf (file, "\n");
589 #ifdef ENABLE_CHECKING
590 symtab_node::verify_symtab_nodes ();
591 #endif
593 /* If we removed something, perhaps profile could be improved. */
594 if (changed && optimize && inline_edge_summary_vec.exists ())
595 FOR_EACH_DEFINED_FUNCTION (node)
596 ipa_propagate_frequency (node);
598 timevar_pop (TV_IPA_UNREACHABLE);
599 return changed;
602 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
603 as needed, also clear EXPLICIT_REFS if the references to given variable
604 do not need to be explicit. */
606 void
607 process_references (varpool_node *vnode,
608 bool *written, bool *address_taken,
609 bool *read, bool *explicit_refs)
611 int i;
612 struct ipa_ref *ref;
614 if (!vnode->all_refs_explicit_p ()
615 || TREE_THIS_VOLATILE (vnode->decl))
616 *explicit_refs = false;
618 for (i = 0; vnode->iterate_referring (i, ref)
619 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
620 switch (ref->use)
622 case IPA_REF_ADDR:
623 *address_taken = true;
624 break;
625 case IPA_REF_LOAD:
626 *read = true;
627 break;
628 case IPA_REF_STORE:
629 *written = true;
630 break;
631 case IPA_REF_ALIAS:
632 process_references (dyn_cast<varpool_node *> (ref->referring), written,
633 address_taken, read, explicit_refs);
634 break;
638 /* Set TREE_READONLY bit. */
640 bool
641 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
643 TREE_READONLY (vnode->decl) = true;
644 return false;
647 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
649 bool
650 set_writeonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
652 vnode->writeonly = true;
653 if (optimize)
655 DECL_INITIAL (vnode->decl) = NULL;
656 if (!vnode->alias)
657 vnode->remove_all_references ();
659 return false;
662 /* Clear addressale bit of VNODE. */
664 bool
665 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
667 vnode->address_taken = false;
668 TREE_ADDRESSABLE (vnode->decl) = 0;
669 return false;
672 /* Discover variables that have no longer address taken or that are read only
673 and update their flags.
675 FIXME: This can not be done in between gimplify and omp_expand since
676 readonly flag plays role on what is shared and what is not. Currently we do
677 this transformation as part of whole program visibility and re-do at
678 ipa-reference pass (to take into account clonning), but it would
679 make sense to do it before early optimizations. */
681 void
682 ipa_discover_readonly_nonaddressable_vars (void)
684 varpool_node *vnode;
685 if (dump_file)
686 fprintf (dump_file, "Clearing variable flags:");
687 FOR_EACH_VARIABLE (vnode)
688 if (!vnode->alias
689 && (TREE_ADDRESSABLE (vnode->decl)
690 || !vnode->writeonly
691 || !TREE_READONLY (vnode->decl)))
693 bool written = false;
694 bool address_taken = false;
695 bool read = false;
696 bool explicit_refs = true;
698 process_references (vnode, &written, &address_taken, &read, &explicit_refs);
699 if (!explicit_refs)
700 continue;
701 if (!address_taken)
703 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
704 fprintf (dump_file, " %s (non-addressable)", vnode->name ());
705 vnode->call_for_node_and_aliases (clear_addressable_bit, NULL, true);
707 if (!address_taken && !written
708 /* Making variable in explicit section readonly can cause section
709 type conflict.
710 See e.g. gcc.c-torture/compile/pr23237.c */
711 && vnode->get_section () == NULL)
713 if (!TREE_READONLY (vnode->decl) && dump_file)
714 fprintf (dump_file, " %s (read-only)", vnode->name ());
715 vnode->call_for_node_and_aliases (set_readonly_bit, NULL, true);
717 if (!vnode->writeonly && !read && !address_taken && written)
719 if (dump_file)
720 fprintf (dump_file, " %s (write-only)", vnode->name ());
721 vnode->call_for_node_and_aliases (set_writeonly_bit, NULL, true);
724 if (dump_file)
725 fprintf (dump_file, "\n");
728 /* Free inline summary. */
730 namespace {
732 const pass_data pass_data_ipa_free_inline_summary =
734 SIMPLE_IPA_PASS, /* type */
735 "*free_inline_summary", /* name */
736 OPTGROUP_NONE, /* optinfo_flags */
737 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
738 0, /* properties_required */
739 0, /* properties_provided */
740 0, /* properties_destroyed */
741 0, /* todo_flags_start */
742 0, /* todo_flags_finish */
745 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
747 public:
748 pass_ipa_free_inline_summary (gcc::context *ctxt)
749 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
752 /* opt_pass methods: */
753 virtual unsigned int execute (function *)
755 inline_free_summary ();
756 return 0;
759 }; // class pass_ipa_free_inline_summary
761 } // anon namespace
763 simple_ipa_opt_pass *
764 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
766 return new pass_ipa_free_inline_summary (ctxt);
769 /* Generate and emit a static constructor or destructor. WHICH must
770 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
771 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
772 initialization priority for this constructor or destructor.
774 FINAL specify whether the externally visible name for collect2 should
775 be produced. */
777 static void
778 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
780 static int counter = 0;
781 char which_buf[16];
782 tree decl, name, resdecl;
784 /* The priority is encoded in the constructor or destructor name.
785 collect2 will sort the names and arrange that they are called at
786 program startup. */
787 if (final)
788 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
789 else
790 /* Proudce sane name but one not recognizable by collect2, just for the
791 case we fail to inline the function. */
792 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
793 name = get_file_function_name (which_buf);
795 decl = build_decl (input_location, FUNCTION_DECL, name,
796 build_function_type_list (void_type_node, NULL_TREE));
797 current_function_decl = decl;
799 resdecl = build_decl (input_location,
800 RESULT_DECL, NULL_TREE, void_type_node);
801 DECL_ARTIFICIAL (resdecl) = 1;
802 DECL_RESULT (decl) = resdecl;
803 DECL_CONTEXT (resdecl) = decl;
805 allocate_struct_function (decl, false);
807 TREE_STATIC (decl) = 1;
808 TREE_USED (decl) = 1;
809 DECL_ARTIFICIAL (decl) = 1;
810 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
811 DECL_SAVED_TREE (decl) = body;
812 if (!targetm.have_ctors_dtors && final)
814 TREE_PUBLIC (decl) = 1;
815 DECL_PRESERVE_P (decl) = 1;
817 DECL_UNINLINABLE (decl) = 1;
819 DECL_INITIAL (decl) = make_node (BLOCK);
820 TREE_USED (DECL_INITIAL (decl)) = 1;
822 DECL_SOURCE_LOCATION (decl) = input_location;
823 cfun->function_end_locus = input_location;
825 switch (which)
827 case 'I':
828 DECL_STATIC_CONSTRUCTOR (decl) = 1;
829 decl_init_priority_insert (decl, priority);
830 break;
831 case 'D':
832 DECL_STATIC_DESTRUCTOR (decl) = 1;
833 decl_fini_priority_insert (decl, priority);
834 break;
835 default:
836 gcc_unreachable ();
839 gimplify_function_tree (decl);
841 cgraph_node::add_new_function (decl, false);
843 set_cfun (NULL);
844 current_function_decl = NULL;
847 /* Generate and emit a static constructor or destructor. WHICH must
848 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
849 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
850 initialization priority for this constructor or destructor. */
852 void
853 cgraph_build_static_cdtor (char which, tree body, int priority)
855 cgraph_build_static_cdtor_1 (which, body, priority, false);
858 /* A vector of FUNCTION_DECLs declared as static constructors. */
859 static vec<tree> static_ctors;
860 /* A vector of FUNCTION_DECLs declared as static destructors. */
861 static vec<tree> static_dtors;
863 /* When target does not have ctors and dtors, we call all constructor
864 and destructor by special initialization/destruction function
865 recognized by collect2.
867 When we are going to build this function, collect all constructors and
868 destructors and turn them into normal functions. */
870 static void
871 record_cdtor_fn (struct cgraph_node *node)
873 if (DECL_STATIC_CONSTRUCTOR (node->decl))
874 static_ctors.safe_push (node->decl);
875 if (DECL_STATIC_DESTRUCTOR (node->decl))
876 static_dtors.safe_push (node->decl);
877 node = cgraph_node::get (node->decl);
878 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
881 /* Define global constructors/destructor functions for the CDTORS, of
882 which they are LEN. The CDTORS are sorted by initialization
883 priority. If CTOR_P is true, these are constructors; otherwise,
884 they are destructors. */
886 static void
887 build_cdtor (bool ctor_p, vec<tree> cdtors)
889 size_t i,j;
890 size_t len = cdtors.length ();
892 i = 0;
893 while (i < len)
895 tree body;
896 tree fn;
897 priority_type priority;
899 priority = 0;
900 body = NULL_TREE;
901 j = i;
904 priority_type p;
905 fn = cdtors[j];
906 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
907 if (j == i)
908 priority = p;
909 else if (p != priority)
910 break;
911 j++;
913 while (j < len);
915 /* When there is only one cdtor and target supports them, do nothing. */
916 if (j == i + 1
917 && targetm.have_ctors_dtors)
919 i++;
920 continue;
922 /* Find the next batch of constructors/destructors with the same
923 initialization priority. */
924 for (;i < j; i++)
926 tree call;
927 fn = cdtors[i];
928 call = build_call_expr (fn, 0);
929 if (ctor_p)
930 DECL_STATIC_CONSTRUCTOR (fn) = 0;
931 else
932 DECL_STATIC_DESTRUCTOR (fn) = 0;
933 /* We do not want to optimize away pure/const calls here.
934 When optimizing, these should be already removed, when not
935 optimizing, we want user to be able to breakpoint in them. */
936 TREE_SIDE_EFFECTS (call) = 1;
937 append_to_statement_list (call, &body);
939 gcc_assert (body != NULL_TREE);
940 /* Generate a function to call all the function of like
941 priority. */
942 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
946 /* Comparison function for qsort. P1 and P2 are actually of type
947 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
948 used to determine the sort order. */
950 static int
951 compare_ctor (const void *p1, const void *p2)
953 tree f1;
954 tree f2;
955 int priority1;
956 int priority2;
958 f1 = *(const tree *)p1;
959 f2 = *(const tree *)p2;
960 priority1 = DECL_INIT_PRIORITY (f1);
961 priority2 = DECL_INIT_PRIORITY (f2);
963 if (priority1 < priority2)
964 return -1;
965 else if (priority1 > priority2)
966 return 1;
967 else
968 /* Ensure a stable sort. Constructors are executed in backwarding
969 order to make LTO initialize braries first. */
970 return DECL_UID (f2) - DECL_UID (f1);
973 /* Comparison function for qsort. P1 and P2 are actually of type
974 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
975 used to determine the sort order. */
977 static int
978 compare_dtor (const void *p1, const void *p2)
980 tree f1;
981 tree f2;
982 int priority1;
983 int priority2;
985 f1 = *(const tree *)p1;
986 f2 = *(const tree *)p2;
987 priority1 = DECL_FINI_PRIORITY (f1);
988 priority2 = DECL_FINI_PRIORITY (f2);
990 if (priority1 < priority2)
991 return -1;
992 else if (priority1 > priority2)
993 return 1;
994 else
995 /* Ensure a stable sort. */
996 return DECL_UID (f1) - DECL_UID (f2);
999 /* Generate functions to call static constructors and destructors
1000 for targets that do not support .ctors/.dtors sections. These
1001 functions have magic names which are detected by collect2. */
1003 static void
1004 build_cdtor_fns (void)
1006 if (!static_ctors.is_empty ())
1008 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1009 static_ctors.qsort (compare_ctor);
1010 build_cdtor (/*ctor_p=*/true, static_ctors);
1013 if (!static_dtors.is_empty ())
1015 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1016 static_dtors.qsort (compare_dtor);
1017 build_cdtor (/*ctor_p=*/false, static_dtors);
1021 /* Look for constructors and destructors and produce function calling them.
1022 This is needed for targets not supporting ctors or dtors, but we perform the
1023 transformation also at linktime to merge possibly numerous
1024 constructors/destructors into single function to improve code locality and
1025 reduce size. */
1027 static unsigned int
1028 ipa_cdtor_merge (void)
1030 struct cgraph_node *node;
1031 FOR_EACH_DEFINED_FUNCTION (node)
1032 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1033 || DECL_STATIC_DESTRUCTOR (node->decl))
1034 record_cdtor_fn (node);
1035 build_cdtor_fns ();
1036 static_ctors.release ();
1037 static_dtors.release ();
1038 return 0;
1041 namespace {
1043 const pass_data pass_data_ipa_cdtor_merge =
1045 IPA_PASS, /* type */
1046 "cdtor", /* name */
1047 OPTGROUP_NONE, /* optinfo_flags */
1048 TV_CGRAPHOPT, /* tv_id */
1049 0, /* properties_required */
1050 0, /* properties_provided */
1051 0, /* properties_destroyed */
1052 0, /* todo_flags_start */
1053 0, /* todo_flags_finish */
1056 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1058 public:
1059 pass_ipa_cdtor_merge (gcc::context *ctxt)
1060 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1061 NULL, /* generate_summary */
1062 NULL, /* write_summary */
1063 NULL, /* read_summary */
1064 NULL, /* write_optimization_summary */
1065 NULL, /* read_optimization_summary */
1066 NULL, /* stmt_fixup */
1067 0, /* function_transform_todo_flags_start */
1068 NULL, /* function_transform */
1069 NULL) /* variable_transform */
1072 /* opt_pass methods: */
1073 virtual bool gate (function *);
1074 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1076 }; // class pass_ipa_cdtor_merge
1078 bool
1079 pass_ipa_cdtor_merge::gate (function *)
1081 /* Perform the pass when we have no ctors/dtors support
1082 or at LTO time to merge multiple constructors into single
1083 function. */
1084 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1087 } // anon namespace
1089 ipa_opt_pass_d *
1090 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1092 return new pass_ipa_cdtor_merge (ctxt);
1095 /* Invalid pointer representing BOTTOM for single user dataflow. */
1096 #define BOTTOM ((cgraph_node *)(size_t) 2)
1098 /* Meet operation for single user dataflow.
1099 Here we want to associate variables with sigle function that may access it.
1101 FUNCTION is current single user of a variable, VAR is variable that uses it.
1102 Latttice is stored in SINGLE_USER_MAP.
1104 We represent:
1105 - TOP by no entry in SIGNLE_USER_MAP
1106 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1107 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1109 cgraph_node *
1110 meet (cgraph_node *function, varpool_node *var,
1111 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1113 struct cgraph_node *user, **f;
1115 if (var->aux == BOTTOM)
1116 return BOTTOM;
1118 f = single_user_map.get (var);
1119 if (!f)
1120 return function;
1121 user = *f;
1122 if (!function)
1123 return user;
1124 else if (function != user)
1125 return BOTTOM;
1126 else
1127 return function;
1130 /* Propagation step of single-use dataflow.
1132 Check all uses of VNODE and see if they are used by single function FUNCTION.
1133 SINGLE_USER_MAP represents the dataflow lattice. */
1135 cgraph_node *
1136 propagate_single_user (varpool_node *vnode, cgraph_node *function,
1137 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1139 int i;
1140 struct ipa_ref *ref;
1142 gcc_assert (!vnode->externally_visible);
1144 /* If node is an alias, first meet with its target. */
1145 if (vnode->alias)
1146 function = meet (function, vnode->get_alias_target (), single_user_map);
1148 /* Check all users and see if they correspond to a single function. */
1149 for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
1151 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
1152 if (cnode)
1154 if (cnode->global.inlined_to)
1155 cnode = cnode->global.inlined_to;
1156 if (!function)
1157 function = cnode;
1158 else if (function != cnode)
1159 function = BOTTOM;
1161 else
1162 function = meet (function, dyn_cast <varpool_node *> (ref->referring), single_user_map);
1164 return function;
1167 /* Pass setting used_by_single_function flag.
1168 This flag is set on variable when there is only one function that may possibly
1169 referr to it. */
1171 static unsigned int
1172 ipa_single_use (void)
1174 varpool_node *first = (varpool_node *) (void *) 1;
1175 varpool_node *var;
1176 hash_map<varpool_node *, cgraph_node *> single_user_map;
1178 FOR_EACH_DEFINED_VARIABLE (var)
1179 if (!var->all_refs_explicit_p ())
1180 var->aux = BOTTOM;
1181 else
1183 /* Enqueue symbol for dataflow. */
1184 var->aux = first;
1185 first = var;
1188 /* The actual dataflow. */
1190 while (first != (void *) 1)
1192 cgraph_node *user, *orig_user, **f;
1194 var = first;
1195 first = (varpool_node *)first->aux;
1197 f = single_user_map.get (var);
1198 if (f)
1199 orig_user = *f;
1200 else
1201 orig_user = NULL;
1202 user = propagate_single_user (var, orig_user, single_user_map);
1204 gcc_checking_assert (var->aux != BOTTOM);
1206 /* If user differs, enqueue all references. */
1207 if (user != orig_user)
1209 unsigned int i;
1210 ipa_ref *ref;
1212 single_user_map.put (var, user);
1214 /* Enqueue all aliases for re-processing. */
1215 for (i = 0; var->iterate_referring (i, ref); i++)
1216 if (ref->use == IPA_REF_ALIAS
1217 && !ref->referring->aux)
1219 ref->referring->aux = first;
1220 first = dyn_cast <varpool_node *> (ref->referring);
1222 /* Enqueue all users for re-processing. */
1223 for (i = 0; var->iterate_reference (i, ref); i++)
1224 if (!ref->referred->aux
1225 && ref->referred->definition
1226 && is_a <varpool_node *> (ref->referred))
1228 ref->referred->aux = first;
1229 first = dyn_cast <varpool_node *> (ref->referred);
1232 /* If user is BOTTOM, just punt on this var. */
1233 if (user == BOTTOM)
1234 var->aux = BOTTOM;
1235 else
1236 var->aux = NULL;
1238 else
1239 var->aux = NULL;
1242 FOR_EACH_DEFINED_VARIABLE (var)
1244 if (var->aux != BOTTOM)
1246 #ifdef ENABLE_CHECKING
1247 if (!single_user_map.get (var))
1248 gcc_assert (single_user_map.get (var));
1249 #endif
1250 if (dump_file)
1252 fprintf (dump_file, "Variable %s/%i is used by single function\n",
1253 var->name (), var->order);
1255 var->used_by_single_function = true;
1257 var->aux = NULL;
1259 return 0;
1262 namespace {
1264 const pass_data pass_data_ipa_single_use =
1266 IPA_PASS, /* type */
1267 "single-use", /* name */
1268 OPTGROUP_NONE, /* optinfo_flags */
1269 TV_CGRAPHOPT, /* tv_id */
1270 0, /* properties_required */
1271 0, /* properties_provided */
1272 0, /* properties_destroyed */
1273 0, /* todo_flags_start */
1274 0, /* todo_flags_finish */
1277 class pass_ipa_single_use : public ipa_opt_pass_d
1279 public:
1280 pass_ipa_single_use (gcc::context *ctxt)
1281 : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
1282 NULL, /* generate_summary */
1283 NULL, /* write_summary */
1284 NULL, /* read_summary */
1285 NULL, /* write_optimization_summary */
1286 NULL, /* read_optimization_summary */
1287 NULL, /* stmt_fixup */
1288 0, /* function_transform_todo_flags_start */
1289 NULL, /* function_transform */
1290 NULL) /* variable_transform */
1293 /* opt_pass methods: */
1294 virtual bool gate (function *);
1295 virtual unsigned int execute (function *) { return ipa_single_use (); }
1297 }; // class pass_ipa_single_use
1299 bool
1300 pass_ipa_single_use::gate (function *)
1302 return optimize;
1305 } // anon namespace
1307 ipa_opt_pass_d *
1308 make_pass_ipa_single_use (gcc::context *ctxt)
1310 return new pass_ipa_single_use (ctxt);