* fold-const.c (fold_negate_expr): Add ANY_INTEGRAL_TYPE_P check.
[official-gcc.git] / gcc / ipa.c
blob4f87b75946cfb070b62e4c176b90a3418b7b06ab
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 "predict.h"
28 #include "basic-block.h"
29 #include "hash-map.h"
30 #include "is-a.h"
31 #include "plugin-api.h"
32 #include "vec.h"
33 #include "hashtab.h"
34 #include "hash-set.h"
35 #include "machmode.h"
36 #include "hard-reg-set.h"
37 #include "input.h"
38 #include "function.h"
39 #include "ipa-ref.h"
40 #include "cgraph.h"
41 #include "tree-pass.h"
42 #include "gimple-expr.h"
43 #include "gimplify.h"
44 #include "flags.h"
45 #include "target.h"
46 #include "tree-iterator.h"
47 #include "ipa-utils.h"
48 #include "alloc-pool.h"
49 #include "ipa-prop.h"
50 #include "ipa-inline.h"
51 #include "tree-inline.h"
52 #include "profile.h"
53 #include "params.h"
54 #include "internal-fn.h"
55 #include "tree-ssa-alias.h"
56 #include "gimple.h"
57 #include "dbgcnt.h"
60 /* Return true when NODE has ADDR reference. */
62 static bool
63 has_addr_references_p (struct cgraph_node *node,
64 void *data ATTRIBUTE_UNUSED)
66 int i;
67 struct ipa_ref *ref = NULL;
69 for (i = 0; node->iterate_referring (i, ref); i++)
70 if (ref->use == IPA_REF_ADDR)
71 return true;
72 return false;
75 /* Look for all functions inlined to NODE and update their inlined_to pointers
76 to INLINED_TO. */
78 static void
79 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
81 struct cgraph_edge *e;
82 for (e = node->callees; e; e = e->next_callee)
83 if (e->callee->global.inlined_to)
85 e->callee->global.inlined_to = inlined_to;
86 update_inlined_to_pointer (e->callee, inlined_to);
90 /* Add symtab NODE to queue starting at FIRST.
92 The queue is linked via AUX pointers and terminated by pointer to 1.
93 We enqueue nodes at two occasions: when we find them reachable or when we find
94 their bodies needed for further clonning. In the second case we mark them
95 by pointer to 2 after processing so they are re-queue when they become
96 reachable. */
98 static void
99 enqueue_node (symtab_node *node, symtab_node **first,
100 hash_set<symtab_node *> *reachable)
102 /* Node is still in queue; do nothing. */
103 if (node->aux && node->aux != (void *) 2)
104 return;
105 /* Node was already processed as unreachable, re-enqueue
106 only if it became reachable now. */
107 if (node->aux == (void *)2 && !reachable->contains (node))
108 return;
109 node->aux = *first;
110 *first = node;
113 /* Process references. */
115 static void
116 process_references (symtab_node *snode,
117 symtab_node **first,
118 bool before_inlining_p,
119 hash_set<symtab_node *> *reachable)
121 int i;
122 struct ipa_ref *ref = NULL;
123 for (i = 0; snode->iterate_reference (i, ref); i++)
125 symtab_node *node = ref->referred;
127 if (node->definition && !node->in_other_partition
128 && ((!DECL_EXTERNAL (node->decl) || node->alias)
129 || (((before_inlining_p
130 && (symtab->state < IPA_SSA
131 || !lookup_attribute ("always_inline",
132 DECL_ATTRIBUTES (node->decl)))))
133 /* We use variable constructors during late complation for
134 constant folding. Keep references alive so partitioning
135 knows about potential references. */
136 || (TREE_CODE (node->decl) == VAR_DECL
137 && flag_wpa
138 && ctor_for_folding (node->decl)
139 != error_mark_node))))
140 reachable->add (node);
141 enqueue_node (node, first, reachable);
145 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
146 all its potential targets as reachable to permit later inlining if
147 devirtualization happens. After inlining still keep their declarations
148 around, so we can devirtualize to a direct call.
150 Also try to make trivial devirutalization when no or only one target is
151 possible. */
153 static void
154 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
155 struct cgraph_edge *edge,
156 symtab_node **first,
157 hash_set<symtab_node *> *reachable,
158 bool before_inlining_p)
160 unsigned int i;
161 void *cache_token;
162 bool final;
163 vec <cgraph_node *>targets
164 = possible_polymorphic_call_targets
165 (edge, &final, &cache_token);
167 if (!reachable_call_targets->add (cache_token))
169 for (i = 0; i < targets.length (); i++)
171 struct cgraph_node *n = targets[i];
173 /* Do not bother to mark virtual methods in anonymous namespace;
174 either we will find use of virtual table defining it, or it is
175 unused. */
176 if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
177 && type_in_anonymous_namespace_p
178 (method_class_type (TREE_TYPE (n->decl))))
179 continue;
181 /* Prior inlining, keep alive bodies of possible targets for
182 devirtualization. */
183 if (n->definition
184 && (before_inlining_p
185 && (symtab->state < IPA_SSA
186 || !lookup_attribute ("always_inline",
187 DECL_ATTRIBUTES (n->decl)))))
188 reachable->add (n);
190 /* Even after inlining we want to keep the possible targets in the
191 boundary, so late passes can still produce direct call even if
192 the chance for inlining is lost. */
193 enqueue_node (n, first, reachable);
197 /* Very trivial devirtualization; when the type is
198 final or anonymous (so we know all its derivation)
199 and there is only one possible virtual call target,
200 make the edge direct. */
201 if (final)
203 if (targets.length () <= 1 && dbg_cnt (devirt))
205 cgraph_node *target, *node = edge->caller;
206 if (targets.length () == 1)
207 target = targets[0];
208 else
209 target = cgraph_node::get_create
210 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
212 if (dump_enabled_p ())
214 location_t locus;
215 if (edge->call_stmt)
216 locus = gimple_location (edge->call_stmt);
217 else
218 locus = UNKNOWN_LOCATION;
219 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
220 "devirtualizing call in %s/%i to %s/%i\n",
221 edge->caller->name (), edge->caller->order,
222 target->name (),
223 target->order);
225 edge = edge->make_direct (target);
226 if (inline_summary_vec)
227 inline_update_overall_summary (node);
228 else if (edge->call_stmt)
230 edge->redirect_call_stmt_to_callee ();
232 /* Call to __builtin_unreachable shouldn't be instrumented. */
233 if (!targets.length ())
234 gimple_call_set_with_bounds (edge->call_stmt, false);
240 /* Perform reachability analysis and reclaim all unreachable nodes.
242 The algorithm is basically mark&sweep but with some extra refinements:
244 - reachable extern inline functions needs special handling; the bodies needs
245 to stay in memory until inlining in hope that they will be inlined.
246 After inlining we release their bodies and turn them into unanalyzed
247 nodes even when they are reachable.
249 BEFORE_INLINING_P specify whether we are before or after inlining.
251 - virtual functions are kept in callgraph even if they seem unreachable in
252 hope calls to them will be devirtualized.
254 Again we remove them after inlining. In late optimization some
255 devirtualization may happen, but it is not important since we won't inline
256 the call. In theory early opts and IPA should work out all important cases.
258 - virtual clones needs bodies of their origins for later materialization;
259 this means that we want to keep the body even if the origin is unreachable
260 otherwise. To avoid origin from sitting in the callgraph and being
261 walked by IPA passes, we turn them into unanalyzed nodes with body
262 defined.
264 We maintain set of function declaration where body needs to stay in
265 body_needed_for_clonning
267 Inline clones represent special case: their declaration match the
268 declaration of origin and cgraph_remove_node already knows how to
269 reshape callgraph and preserve body when offline copy of function or
270 inline clone is being removed.
272 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
273 variables with DECL_INITIAL set. We finalize these and keep reachable
274 ones around for constant folding purposes. After inlining we however
275 stop walking their references to let everything static referneced by them
276 to be removed when it is otherwise unreachable.
278 We maintain queue of both reachable symbols (i.e. defined symbols that needs
279 to stay) and symbols that are in boundary (i.e. external symbols referenced
280 by reachable symbols or origins of clones). The queue is represented
281 as linked list by AUX pointer terminated by 1.
283 At the end we keep all reachable symbols. For symbols in boundary we always
284 turn definition into a declaration, but we may keep function body around
285 based on body_needed_for_clonning
287 All symbols that enter the queue have AUX pointer non-zero and are in the
288 boundary. Pointer set REACHABLE is used to track reachable symbols.
290 Every symbol can be visited twice - once as part of boundary and once
291 as real reachable symbol. enqueue_node needs to decide whether the
292 node needs to be re-queued for second processing. For this purpose
293 we set AUX pointer of processed symbols in the boundary to constant 2. */
295 bool
296 symbol_table::remove_unreachable_nodes (bool before_inlining_p, FILE *file)
298 symtab_node *first = (symtab_node *) (void *) 1;
299 struct cgraph_node *node, *next;
300 varpool_node *vnode, *vnext;
301 bool changed = false;
302 hash_set<symtab_node *> reachable;
303 hash_set<tree> body_needed_for_clonning;
304 hash_set<void *> reachable_call_targets;
306 timevar_push (TV_IPA_UNREACHABLE);
307 build_type_inheritance_graph ();
308 if (file)
309 fprintf (file, "\nReclaiming functions:");
310 #ifdef ENABLE_CHECKING
311 FOR_EACH_FUNCTION (node)
312 gcc_assert (!node->aux);
313 FOR_EACH_VARIABLE (vnode)
314 gcc_assert (!vnode->aux);
315 #endif
316 /* Mark functions whose bodies are obviously needed.
317 This is mostly when they can be referenced externally. Inline clones
318 are special since their declarations are shared with master clone and thus
319 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
320 FOR_EACH_FUNCTION (node)
322 node->used_as_abstract_origin = false;
323 if (node->definition
324 && !node->global.inlined_to
325 && !node->in_other_partition
326 && !node->can_remove_if_no_direct_calls_and_refs_p ())
328 gcc_assert (!node->global.inlined_to);
329 reachable.add (node);
330 enqueue_node (node, &first, &reachable);
332 else
333 gcc_assert (!node->aux);
336 /* Mark variables that are obviously needed. */
337 FOR_EACH_DEFINED_VARIABLE (vnode)
338 if (!vnode->can_remove_if_no_refs_p()
339 && !vnode->in_other_partition)
341 reachable.add (vnode);
342 enqueue_node (vnode, &first, &reachable);
345 /* Perform reachability analysis. */
346 while (first != (symtab_node *) (void *) 1)
348 bool in_boundary_p = !reachable.contains (first);
349 symtab_node *node = first;
351 first = (symtab_node *)first->aux;
353 /* If we are processing symbol in boundary, mark its AUX pointer for
354 possible later re-processing in enqueue_node. */
355 if (in_boundary_p)
356 node->aux = (void *)2;
357 else
359 if (TREE_CODE (node->decl) == FUNCTION_DECL
360 && DECL_ABSTRACT_ORIGIN (node->decl))
362 struct cgraph_node *origin_node
363 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
364 if (origin_node && !origin_node->used_as_abstract_origin)
366 origin_node->used_as_abstract_origin = true;
367 gcc_assert (!origin_node->prev_sibling_clone);
368 gcc_assert (!origin_node->next_sibling_clone);
369 for (cgraph_node *n = origin_node->clones; n;
370 n = n->next_sibling_clone)
371 if (n->decl == DECL_ABSTRACT_ORIGIN (node->decl))
372 n->used_as_abstract_origin = true;
373 enqueue_node (origin_node, &first, &reachable);
376 /* If any symbol in a comdat group is reachable, force
377 all externally visible symbols in the same comdat
378 group to be reachable as well. Comdat-local symbols
379 can be discarded if all uses were inlined. */
380 if (node->same_comdat_group)
382 symtab_node *next;
383 for (next = node->same_comdat_group;
384 next != node;
385 next = next->same_comdat_group)
386 if (!next->comdat_local_p ()
387 && !reachable.add (next))
388 enqueue_node (next, &first, &reachable);
390 /* Mark references as reachable. */
391 process_references (node, &first, before_inlining_p, &reachable);
394 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
396 /* Mark the callees reachable unless they are direct calls to extern
397 inline functions we decided to not inline. */
398 if (!in_boundary_p)
400 struct cgraph_edge *e;
401 /* Keep alive possible targets for devirtualization. */
402 if (opt_for_fn (cnode->decl, optimize)
403 && opt_for_fn (cnode->decl, flag_devirtualize))
405 struct cgraph_edge *next;
406 for (e = cnode->indirect_calls; e; e = next)
408 next = e->next_callee;
409 if (e->indirect_info->polymorphic)
410 walk_polymorphic_call_targets (&reachable_call_targets,
411 e, &first, &reachable,
412 before_inlining_p);
415 for (e = cnode->callees; e; e = e->next_callee)
417 if (e->callee->definition
418 && !e->callee->in_other_partition
419 && (!e->inline_failed
420 || !DECL_EXTERNAL (e->callee->decl)
421 || e->callee->alias
422 || before_inlining_p))
424 /* Be sure that we will not optimize out alias target
425 body. */
426 if (DECL_EXTERNAL (e->callee->decl)
427 && e->callee->alias
428 && before_inlining_p)
429 reachable.add (e->callee->function_symbol ());
430 reachable.add (e->callee);
432 enqueue_node (e->callee, &first, &reachable);
435 /* When inline clone exists, mark body to be preserved so when removing
436 offline copy of the function we don't kill it. */
437 if (cnode->global.inlined_to)
438 body_needed_for_clonning.add (cnode->decl);
440 /* For non-inline clones, force their origins to the boundary and ensure
441 that body is not removed. */
442 while (cnode->clone_of)
444 bool noninline = cnode->clone_of->decl != cnode->decl;
445 cnode = cnode->clone_of;
446 if (noninline)
448 body_needed_for_clonning.add (cnode->decl);
449 enqueue_node (cnode, &first, &reachable);
454 /* If any reachable function has simd clones, mark them as
455 reachable as well. */
456 if (cnode->simd_clones)
458 cgraph_node *next;
459 for (next = cnode->simd_clones;
460 next;
461 next = next->simdclone->next_clone)
462 if (in_boundary_p
463 || !reachable.add (next))
464 enqueue_node (next, &first, &reachable);
467 /* When we see constructor of external variable, keep referred nodes in the
468 boundary. This will also hold initializers of the external vars NODE
469 refers to. */
470 varpool_node *vnode = dyn_cast <varpool_node *> (node);
471 if (vnode
472 && DECL_EXTERNAL (node->decl)
473 && !vnode->alias
474 && in_boundary_p)
476 struct ipa_ref *ref = NULL;
477 for (int i = 0; node->iterate_reference (i, ref); i++)
478 enqueue_node (ref->referred, &first, &reachable);
482 /* Remove unreachable functions. */
483 for (node = first_function (); node; node = next)
485 next = next_function (node);
487 /* If node is not needed at all, remove it. */
488 if (!node->aux)
490 if (file)
491 fprintf (file, " %s/%i", node->name (), node->order);
492 node->remove ();
493 changed = true;
495 /* If node is unreachable, remove its body. */
496 else if (!reachable.contains (node))
498 if (!body_needed_for_clonning.contains (node->decl))
499 node->release_body ();
500 else if (!node->clone_of)
501 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
502 if (node->definition)
504 if (file)
505 fprintf (file, " %s/%i", node->name (), node->order);
506 node->body_removed = true;
507 node->analyzed = false;
508 node->definition = false;
509 node->cpp_implicit_alias = false;
510 node->alias = false;
511 node->thunk.thunk_p = false;
512 node->weakref = false;
513 /* After early inlining we drop always_inline attributes on
514 bodies of functions that are still referenced (have their
515 address taken). */
516 DECL_ATTRIBUTES (node->decl)
517 = remove_attribute ("always_inline",
518 DECL_ATTRIBUTES (node->decl));
519 if (!node->in_other_partition)
520 node->local.local = false;
521 node->remove_callees ();
522 node->remove_from_same_comdat_group ();
523 node->remove_all_references ();
524 changed = true;
525 if (node->thunk.thunk_p
526 && node->thunk.add_pointer_bounds_args)
528 node->thunk.thunk_p = false;
529 node->thunk.add_pointer_bounds_args = false;
533 else
534 gcc_assert (node->clone_of || !node->has_gimple_body_p ()
535 || in_lto_p || DECL_RESULT (node->decl));
538 /* Inline clones might be kept around so their materializing allows further
539 cloning. If the function the clone is inlined into is removed, we need
540 to turn it into normal cone. */
541 FOR_EACH_FUNCTION (node)
543 if (node->global.inlined_to
544 && !node->callers)
546 gcc_assert (node->clones);
547 node->global.inlined_to = NULL;
548 update_inlined_to_pointer (node, node);
550 node->aux = NULL;
553 /* Remove unreachable variables. */
554 if (file)
555 fprintf (file, "\nReclaiming variables:");
556 for (vnode = first_variable (); vnode; vnode = vnext)
558 vnext = next_variable (vnode);
559 if (!vnode->aux
560 /* For can_refer_decl_in_current_unit_p we want to track for
561 all external variables if they are defined in other partition
562 or not. */
563 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
565 if (file)
566 fprintf (file, " %s/%i", vnode->name (), vnode->order);
567 vnode->remove ();
568 changed = true;
570 else if (!reachable.contains (vnode))
572 tree init;
573 if (vnode->definition)
575 if (file)
576 fprintf (file, " %s", vnode->name ());
577 changed = true;
579 /* Keep body if it may be useful for constant folding. */
580 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node
581 && !POINTER_BOUNDS_P (vnode->decl))
582 vnode->remove_initializer ();
583 else
584 DECL_INITIAL (vnode->decl) = init;
585 vnode->body_removed = true;
586 vnode->definition = false;
587 vnode->analyzed = false;
588 vnode->aux = NULL;
590 vnode->remove_from_same_comdat_group ();
592 vnode->remove_all_references ();
594 else
595 vnode->aux = NULL;
598 /* Now update address_taken flags and try to promote functions to be local. */
599 if (file)
600 fprintf (file, "\nClearing address taken flags:");
601 FOR_EACH_DEFINED_FUNCTION (node)
602 if (node->address_taken
603 && !node->used_from_other_partition)
605 if (!node->call_for_symbol_thunks_and_aliases
606 (has_addr_references_p, NULL, true)
607 && (!node->instrumentation_clone
608 || !node->instrumented_version
609 || !node->instrumented_version->address_taken))
611 if (file)
612 fprintf (file, " %s", node->name ());
613 node->address_taken = false;
614 changed = true;
615 if (node->local_p ())
617 node->local.local = true;
618 if (file)
619 fprintf (file, " (local)");
623 if (file)
624 fprintf (file, "\n");
626 #ifdef ENABLE_CHECKING
627 symtab_node::verify_symtab_nodes ();
628 #endif
630 /* If we removed something, perhaps profile could be improved. */
631 if (changed && optimize && inline_edge_summary_vec.exists ())
632 FOR_EACH_DEFINED_FUNCTION (node)
633 ipa_propagate_frequency (node);
635 timevar_pop (TV_IPA_UNREACHABLE);
636 return changed;
639 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
640 as needed, also clear EXPLICIT_REFS if the references to given variable
641 do not need to be explicit. */
643 void
644 process_references (varpool_node *vnode,
645 bool *written, bool *address_taken,
646 bool *read, bool *explicit_refs)
648 int i;
649 struct ipa_ref *ref;
651 if (!vnode->all_refs_explicit_p ()
652 || TREE_THIS_VOLATILE (vnode->decl))
653 *explicit_refs = false;
655 for (i = 0; vnode->iterate_referring (i, ref)
656 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
657 switch (ref->use)
659 case IPA_REF_ADDR:
660 *address_taken = true;
661 break;
662 case IPA_REF_LOAD:
663 *read = true;
664 break;
665 case IPA_REF_STORE:
666 *written = true;
667 break;
668 case IPA_REF_ALIAS:
669 process_references (dyn_cast<varpool_node *> (ref->referring), written,
670 address_taken, read, explicit_refs);
671 break;
672 case IPA_REF_CHKP:
673 gcc_unreachable ();
677 /* Set TREE_READONLY bit. */
679 bool
680 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
682 TREE_READONLY (vnode->decl) = true;
683 return false;
686 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
688 bool
689 set_writeonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
691 vnode->writeonly = true;
692 if (optimize)
694 DECL_INITIAL (vnode->decl) = NULL;
695 if (!vnode->alias)
696 vnode->remove_all_references ();
698 return false;
701 /* Clear addressale bit of VNODE. */
703 bool
704 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
706 vnode->address_taken = false;
707 TREE_ADDRESSABLE (vnode->decl) = 0;
708 return false;
711 /* Discover variables that have no longer address taken or that are read only
712 and update their flags.
714 FIXME: This can not be done in between gimplify and omp_expand since
715 readonly flag plays role on what is shared and what is not. Currently we do
716 this transformation as part of whole program visibility and re-do at
717 ipa-reference pass (to take into account clonning), but it would
718 make sense to do it before early optimizations. */
720 void
721 ipa_discover_readonly_nonaddressable_vars (void)
723 varpool_node *vnode;
724 if (dump_file)
725 fprintf (dump_file, "Clearing variable flags:");
726 FOR_EACH_VARIABLE (vnode)
727 if (!vnode->alias
728 && (TREE_ADDRESSABLE (vnode->decl)
729 || !vnode->writeonly
730 || !TREE_READONLY (vnode->decl)))
732 bool written = false;
733 bool address_taken = false;
734 bool read = false;
735 bool explicit_refs = true;
737 process_references (vnode, &written, &address_taken, &read, &explicit_refs);
738 if (!explicit_refs)
739 continue;
740 if (!address_taken)
742 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
743 fprintf (dump_file, " %s (non-addressable)", vnode->name ());
744 vnode->call_for_node_and_aliases (clear_addressable_bit, NULL, true);
746 if (!address_taken && !written
747 /* Making variable in explicit section readonly can cause section
748 type conflict.
749 See e.g. gcc.c-torture/compile/pr23237.c */
750 && vnode->get_section () == NULL)
752 if (!TREE_READONLY (vnode->decl) && dump_file)
753 fprintf (dump_file, " %s (read-only)", vnode->name ());
754 vnode->call_for_node_and_aliases (set_readonly_bit, NULL, true);
756 if (!vnode->writeonly && !read && !address_taken && written)
758 if (dump_file)
759 fprintf (dump_file, " %s (write-only)", vnode->name ());
760 vnode->call_for_node_and_aliases (set_writeonly_bit, NULL, true);
763 if (dump_file)
764 fprintf (dump_file, "\n");
767 /* Free inline summary. */
769 namespace {
771 const pass_data pass_data_ipa_free_inline_summary =
773 SIMPLE_IPA_PASS, /* type */
774 "free-inline-summary", /* name */
775 OPTGROUP_NONE, /* optinfo_flags */
776 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
777 0, /* properties_required */
778 0, /* properties_provided */
779 0, /* properties_destroyed */
780 0, /* todo_flags_start */
781 /* Early optimizations may make function unreachable. We can not
782 remove unreachable functions as part of the ealry opts pass because
783 TODOs are run before subpasses. Do it here. */
784 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
787 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
789 public:
790 pass_ipa_free_inline_summary (gcc::context *ctxt)
791 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
794 /* opt_pass methods: */
795 virtual unsigned int execute (function *)
797 inline_free_summary ();
798 return 0;
801 }; // class pass_ipa_free_inline_summary
803 } // anon namespace
805 simple_ipa_opt_pass *
806 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
808 return new pass_ipa_free_inline_summary (ctxt);
811 /* Generate and emit a static constructor or destructor. WHICH must
812 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
813 (for chp static vars constructor) or 'B' (for chkp static bounds
814 constructor). BODY is a STATEMENT_LIST containing GENERIC
815 statements. PRIORITY is the initialization priority for this
816 constructor or destructor.
818 FINAL specify whether the externally visible name for collect2 should
819 be produced. */
821 static void
822 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
824 static int counter = 0;
825 char which_buf[16];
826 tree decl, name, resdecl;
828 /* The priority is encoded in the constructor or destructor name.
829 collect2 will sort the names and arrange that they are called at
830 program startup. */
831 if (final)
832 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
833 else
834 /* Proudce sane name but one not recognizable by collect2, just for the
835 case we fail to inline the function. */
836 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
837 name = get_file_function_name (which_buf);
839 decl = build_decl (input_location, FUNCTION_DECL, name,
840 build_function_type_list (void_type_node, NULL_TREE));
841 current_function_decl = decl;
843 resdecl = build_decl (input_location,
844 RESULT_DECL, NULL_TREE, void_type_node);
845 DECL_ARTIFICIAL (resdecl) = 1;
846 DECL_RESULT (decl) = resdecl;
847 DECL_CONTEXT (resdecl) = decl;
849 allocate_struct_function (decl, false);
851 TREE_STATIC (decl) = 1;
852 TREE_USED (decl) = 1;
853 DECL_ARTIFICIAL (decl) = 1;
854 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
855 DECL_SAVED_TREE (decl) = body;
856 if (!targetm.have_ctors_dtors && final)
858 TREE_PUBLIC (decl) = 1;
859 DECL_PRESERVE_P (decl) = 1;
861 DECL_UNINLINABLE (decl) = 1;
863 DECL_INITIAL (decl) = make_node (BLOCK);
864 TREE_USED (DECL_INITIAL (decl)) = 1;
866 DECL_SOURCE_LOCATION (decl) = input_location;
867 cfun->function_end_locus = input_location;
869 switch (which)
871 case 'I':
872 DECL_STATIC_CONSTRUCTOR (decl) = 1;
873 decl_init_priority_insert (decl, priority);
874 break;
875 case 'P':
876 DECL_STATIC_CONSTRUCTOR (decl) = 1;
877 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("chkp ctor"),
878 NULL,
879 NULL_TREE);
880 decl_init_priority_insert (decl, priority);
881 break;
882 case 'B':
883 DECL_STATIC_CONSTRUCTOR (decl) = 1;
884 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("bnd_legacy"),
885 NULL,
886 NULL_TREE);
887 decl_init_priority_insert (decl, priority);
888 break;
889 case 'D':
890 DECL_STATIC_DESTRUCTOR (decl) = 1;
891 decl_fini_priority_insert (decl, priority);
892 break;
893 default:
894 gcc_unreachable ();
897 gimplify_function_tree (decl);
899 cgraph_node::add_new_function (decl, false);
901 set_cfun (NULL);
902 current_function_decl = NULL;
905 /* Generate and emit a static constructor or destructor. WHICH must
906 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
907 (for chkp static vars constructor) or 'B' (for chkp static bounds
908 constructor). BODY is a STATEMENT_LIST containing GENERIC
909 statements. PRIORITY is the initialization priority for this
910 constructor or destructor. */
912 void
913 cgraph_build_static_cdtor (char which, tree body, int priority)
915 cgraph_build_static_cdtor_1 (which, body, priority, false);
918 /* A vector of FUNCTION_DECLs declared as static constructors. */
919 static vec<tree> static_ctors;
920 /* A vector of FUNCTION_DECLs declared as static destructors. */
921 static vec<tree> static_dtors;
923 /* When target does not have ctors and dtors, we call all constructor
924 and destructor by special initialization/destruction function
925 recognized by collect2.
927 When we are going to build this function, collect all constructors and
928 destructors and turn them into normal functions. */
930 static void
931 record_cdtor_fn (struct cgraph_node *node)
933 if (DECL_STATIC_CONSTRUCTOR (node->decl))
934 static_ctors.safe_push (node->decl);
935 if (DECL_STATIC_DESTRUCTOR (node->decl))
936 static_dtors.safe_push (node->decl);
937 node = cgraph_node::get (node->decl);
938 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
941 /* Define global constructors/destructor functions for the CDTORS, of
942 which they are LEN. The CDTORS are sorted by initialization
943 priority. If CTOR_P is true, these are constructors; otherwise,
944 they are destructors. */
946 static void
947 build_cdtor (bool ctor_p, vec<tree> cdtors)
949 size_t i,j;
950 size_t len = cdtors.length ();
952 i = 0;
953 while (i < len)
955 tree body;
956 tree fn;
957 priority_type priority;
959 priority = 0;
960 body = NULL_TREE;
961 j = i;
964 priority_type p;
965 fn = cdtors[j];
966 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
967 if (j == i)
968 priority = p;
969 else if (p != priority)
970 break;
971 j++;
973 while (j < len);
975 /* When there is only one cdtor and target supports them, do nothing. */
976 if (j == i + 1
977 && targetm.have_ctors_dtors)
979 i++;
980 continue;
982 /* Find the next batch of constructors/destructors with the same
983 initialization priority. */
984 for (;i < j; i++)
986 tree call;
987 fn = cdtors[i];
988 call = build_call_expr (fn, 0);
989 if (ctor_p)
990 DECL_STATIC_CONSTRUCTOR (fn) = 0;
991 else
992 DECL_STATIC_DESTRUCTOR (fn) = 0;
993 /* We do not want to optimize away pure/const calls here.
994 When optimizing, these should be already removed, when not
995 optimizing, we want user to be able to breakpoint in them. */
996 TREE_SIDE_EFFECTS (call) = 1;
997 append_to_statement_list (call, &body);
999 gcc_assert (body != NULL_TREE);
1000 /* Generate a function to call all the function of like
1001 priority. */
1002 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1006 /* Comparison function for qsort. P1 and P2 are actually of type
1007 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1008 used to determine the sort order. */
1010 static int
1011 compare_ctor (const void *p1, const void *p2)
1013 tree f1;
1014 tree f2;
1015 int priority1;
1016 int priority2;
1018 f1 = *(const tree *)p1;
1019 f2 = *(const tree *)p2;
1020 priority1 = DECL_INIT_PRIORITY (f1);
1021 priority2 = DECL_INIT_PRIORITY (f2);
1023 if (priority1 < priority2)
1024 return -1;
1025 else if (priority1 > priority2)
1026 return 1;
1027 else
1028 /* Ensure a stable sort. Constructors are executed in backwarding
1029 order to make LTO initialize braries first. */
1030 return DECL_UID (f2) - DECL_UID (f1);
1033 /* Comparison function for qsort. P1 and P2 are actually of type
1034 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1035 used to determine the sort order. */
1037 static int
1038 compare_dtor (const void *p1, const void *p2)
1040 tree f1;
1041 tree f2;
1042 int priority1;
1043 int priority2;
1045 f1 = *(const tree *)p1;
1046 f2 = *(const tree *)p2;
1047 priority1 = DECL_FINI_PRIORITY (f1);
1048 priority2 = DECL_FINI_PRIORITY (f2);
1050 if (priority1 < priority2)
1051 return -1;
1052 else if (priority1 > priority2)
1053 return 1;
1054 else
1055 /* Ensure a stable sort. */
1056 return DECL_UID (f1) - DECL_UID (f2);
1059 /* Generate functions to call static constructors and destructors
1060 for targets that do not support .ctors/.dtors sections. These
1061 functions have magic names which are detected by collect2. */
1063 static void
1064 build_cdtor_fns (void)
1066 if (!static_ctors.is_empty ())
1068 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1069 static_ctors.qsort (compare_ctor);
1070 build_cdtor (/*ctor_p=*/true, static_ctors);
1073 if (!static_dtors.is_empty ())
1075 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1076 static_dtors.qsort (compare_dtor);
1077 build_cdtor (/*ctor_p=*/false, static_dtors);
1081 /* Look for constructors and destructors and produce function calling them.
1082 This is needed for targets not supporting ctors or dtors, but we perform the
1083 transformation also at linktime to merge possibly numerous
1084 constructors/destructors into single function to improve code locality and
1085 reduce size. */
1087 static unsigned int
1088 ipa_cdtor_merge (void)
1090 struct cgraph_node *node;
1091 FOR_EACH_DEFINED_FUNCTION (node)
1092 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1093 || DECL_STATIC_DESTRUCTOR (node->decl))
1094 record_cdtor_fn (node);
1095 build_cdtor_fns ();
1096 static_ctors.release ();
1097 static_dtors.release ();
1098 return 0;
1101 namespace {
1103 const pass_data pass_data_ipa_cdtor_merge =
1105 IPA_PASS, /* type */
1106 "cdtor", /* name */
1107 OPTGROUP_NONE, /* optinfo_flags */
1108 TV_CGRAPHOPT, /* tv_id */
1109 0, /* properties_required */
1110 0, /* properties_provided */
1111 0, /* properties_destroyed */
1112 0, /* todo_flags_start */
1113 0, /* todo_flags_finish */
1116 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1118 public:
1119 pass_ipa_cdtor_merge (gcc::context *ctxt)
1120 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1121 NULL, /* generate_summary */
1122 NULL, /* write_summary */
1123 NULL, /* read_summary */
1124 NULL, /* write_optimization_summary */
1125 NULL, /* read_optimization_summary */
1126 NULL, /* stmt_fixup */
1127 0, /* function_transform_todo_flags_start */
1128 NULL, /* function_transform */
1129 NULL) /* variable_transform */
1132 /* opt_pass methods: */
1133 virtual bool gate (function *);
1134 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1136 }; // class pass_ipa_cdtor_merge
1138 bool
1139 pass_ipa_cdtor_merge::gate (function *)
1141 /* Perform the pass when we have no ctors/dtors support
1142 or at LTO time to merge multiple constructors into single
1143 function. */
1144 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1147 } // anon namespace
1149 ipa_opt_pass_d *
1150 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1152 return new pass_ipa_cdtor_merge (ctxt);
1155 /* Invalid pointer representing BOTTOM for single user dataflow. */
1156 #define BOTTOM ((cgraph_node *)(size_t) 2)
1158 /* Meet operation for single user dataflow.
1159 Here we want to associate variables with sigle function that may access it.
1161 FUNCTION is current single user of a variable, VAR is variable that uses it.
1162 Latttice is stored in SINGLE_USER_MAP.
1164 We represent:
1165 - TOP by no entry in SIGNLE_USER_MAP
1166 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1167 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1169 cgraph_node *
1170 meet (cgraph_node *function, varpool_node *var,
1171 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1173 struct cgraph_node *user, **f;
1175 if (var->aux == BOTTOM)
1176 return BOTTOM;
1178 f = single_user_map.get (var);
1179 if (!f)
1180 return function;
1181 user = *f;
1182 if (!function)
1183 return user;
1184 else if (function != user)
1185 return BOTTOM;
1186 else
1187 return function;
1190 /* Propagation step of single-use dataflow.
1192 Check all uses of VNODE and see if they are used by single function FUNCTION.
1193 SINGLE_USER_MAP represents the dataflow lattice. */
1195 cgraph_node *
1196 propagate_single_user (varpool_node *vnode, cgraph_node *function,
1197 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1199 int i;
1200 struct ipa_ref *ref;
1202 gcc_assert (!vnode->externally_visible);
1204 /* If node is an alias, first meet with its target. */
1205 if (vnode->alias)
1206 function = meet (function, vnode->get_alias_target (), single_user_map);
1208 /* Check all users and see if they correspond to a single function. */
1209 for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
1211 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
1212 if (cnode)
1214 if (cnode->global.inlined_to)
1215 cnode = cnode->global.inlined_to;
1216 if (!function)
1217 function = cnode;
1218 else if (function != cnode)
1219 function = BOTTOM;
1221 else
1222 function = meet (function, dyn_cast <varpool_node *> (ref->referring), single_user_map);
1224 return function;
1227 /* Pass setting used_by_single_function flag.
1228 This flag is set on variable when there is only one function that may possibly
1229 referr to it. */
1231 static unsigned int
1232 ipa_single_use (void)
1234 varpool_node *first = (varpool_node *) (void *) 1;
1235 varpool_node *var;
1236 hash_map<varpool_node *, cgraph_node *> single_user_map;
1238 FOR_EACH_DEFINED_VARIABLE (var)
1239 if (!var->all_refs_explicit_p ())
1240 var->aux = BOTTOM;
1241 else
1243 /* Enqueue symbol for dataflow. */
1244 var->aux = first;
1245 first = var;
1248 /* The actual dataflow. */
1250 while (first != (void *) 1)
1252 cgraph_node *user, *orig_user, **f;
1254 var = first;
1255 first = (varpool_node *)first->aux;
1257 f = single_user_map.get (var);
1258 if (f)
1259 orig_user = *f;
1260 else
1261 orig_user = NULL;
1262 user = propagate_single_user (var, orig_user, single_user_map);
1264 gcc_checking_assert (var->aux != BOTTOM);
1266 /* If user differs, enqueue all references. */
1267 if (user != orig_user)
1269 unsigned int i;
1270 ipa_ref *ref;
1272 single_user_map.put (var, user);
1274 /* Enqueue all aliases for re-processing. */
1275 for (i = 0; var->iterate_referring (i, ref); i++)
1276 if (ref->use == IPA_REF_ALIAS
1277 && !ref->referring->aux)
1279 ref->referring->aux = first;
1280 first = dyn_cast <varpool_node *> (ref->referring);
1282 /* Enqueue all users for re-processing. */
1283 for (i = 0; var->iterate_reference (i, ref); i++)
1284 if (!ref->referred->aux
1285 && ref->referred->definition
1286 && is_a <varpool_node *> (ref->referred))
1288 ref->referred->aux = first;
1289 first = dyn_cast <varpool_node *> (ref->referred);
1292 /* If user is BOTTOM, just punt on this var. */
1293 if (user == BOTTOM)
1294 var->aux = BOTTOM;
1295 else
1296 var->aux = NULL;
1298 else
1299 var->aux = NULL;
1302 FOR_EACH_DEFINED_VARIABLE (var)
1304 if (var->aux != BOTTOM)
1306 #ifdef ENABLE_CHECKING
1307 if (!single_user_map.get (var))
1308 gcc_assert (single_user_map.get (var));
1309 #endif
1310 if (dump_file)
1312 fprintf (dump_file, "Variable %s/%i is used by single function\n",
1313 var->name (), var->order);
1315 var->used_by_single_function = true;
1317 var->aux = NULL;
1319 return 0;
1322 namespace {
1324 const pass_data pass_data_ipa_single_use =
1326 IPA_PASS, /* type */
1327 "single-use", /* name */
1328 OPTGROUP_NONE, /* optinfo_flags */
1329 TV_CGRAPHOPT, /* tv_id */
1330 0, /* properties_required */
1331 0, /* properties_provided */
1332 0, /* properties_destroyed */
1333 0, /* todo_flags_start */
1334 0, /* todo_flags_finish */
1337 class pass_ipa_single_use : public ipa_opt_pass_d
1339 public:
1340 pass_ipa_single_use (gcc::context *ctxt)
1341 : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
1342 NULL, /* generate_summary */
1343 NULL, /* write_summary */
1344 NULL, /* read_summary */
1345 NULL, /* write_optimization_summary */
1346 NULL, /* read_optimization_summary */
1347 NULL, /* stmt_fixup */
1348 0, /* function_transform_todo_flags_start */
1349 NULL, /* function_transform */
1350 NULL) /* variable_transform */
1353 /* opt_pass methods: */
1354 virtual bool gate (function *);
1355 virtual unsigned int execute (function *) { return ipa_single_use (); }
1357 }; // class pass_ipa_single_use
1359 bool
1360 pass_ipa_single_use::gate (function *)
1362 return optimize;
1365 } // anon namespace
1367 ipa_opt_pass_d *
1368 make_pass_ipa_single_use (gcc::context *ctxt)
1370 return new pass_ipa_single_use (ctxt);