2014-12-12 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ipa.c
blobbed20e9d664cfb885517fd2a696009d7d735f0c6
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;
126 symtab_node *body = node->ultimate_alias_target ();
128 if (node->definition && !node->in_other_partition
129 && ((!DECL_EXTERNAL (node->decl) || node->alias)
130 || (((before_inlining_p
131 && (TREE_CODE (node->decl) != FUNCTION_DECL
132 || opt_for_fn (body->decl, optimize)
133 || (symtab->state < IPA_SSA
134 && lookup_attribute
135 ("always_inline",
136 DECL_ATTRIBUTES (body->decl))))))
137 /* We use variable constructors during late compilation for
138 constant folding. Keep references alive so partitioning
139 knows about potential references. */
140 || (TREE_CODE (node->decl) == VAR_DECL
141 && flag_wpa
142 && ctor_for_folding (node->decl)
143 != error_mark_node))))
145 /* Be sure that we will not optimize out alias target
146 body. */
147 if (DECL_EXTERNAL (node->decl)
148 && node->alias
149 && before_inlining_p)
150 reachable->add (body);
151 reachable->add (node);
153 enqueue_node (node, first, reachable);
157 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
158 all its potential targets as reachable to permit later inlining if
159 devirtualization happens. After inlining still keep their declarations
160 around, so we can devirtualize to a direct call.
162 Also try to make trivial devirutalization when no or only one target is
163 possible. */
165 static void
166 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
167 struct cgraph_edge *edge,
168 symtab_node **first,
169 hash_set<symtab_node *> *reachable,
170 bool before_inlining_p)
172 unsigned int i;
173 void *cache_token;
174 bool final;
175 vec <cgraph_node *>targets
176 = possible_polymorphic_call_targets
177 (edge, &final, &cache_token);
179 if (!reachable_call_targets->add (cache_token))
181 for (i = 0; i < targets.length (); i++)
183 struct cgraph_node *n = targets[i];
185 /* Do not bother to mark virtual methods in anonymous namespace;
186 either we will find use of virtual table defining it, or it is
187 unused. */
188 if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
189 && type_in_anonymous_namespace_p
190 (method_class_type (TREE_TYPE (n->decl))))
191 continue;
193 symtab_node *body = n->function_symbol ();
195 /* Prior inlining, keep alive bodies of possible targets for
196 devirtualization. */
197 if (n->definition
198 && (before_inlining_p
199 && opt_for_fn (body->decl, optimize)
200 && opt_for_fn (body->decl, flag_devirtualize)))
202 /* Be sure that we will not optimize out alias target
203 body. */
204 if (DECL_EXTERNAL (n->decl)
205 && n->alias
206 && before_inlining_p)
207 reachable->add (body);
208 reachable->add (n);
210 /* Even after inlining we want to keep the possible targets in the
211 boundary, so late passes can still produce direct call even if
212 the chance for inlining is lost. */
213 enqueue_node (n, first, reachable);
217 /* Very trivial devirtualization; when the type is
218 final or anonymous (so we know all its derivation)
219 and there is only one possible virtual call target,
220 make the edge direct. */
221 if (final)
223 if (targets.length () <= 1 && dbg_cnt (devirt))
225 cgraph_node *target, *node = edge->caller;
226 if (targets.length () == 1)
227 target = targets[0];
228 else
229 target = cgraph_node::get_create
230 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
232 if (dump_enabled_p ())
234 location_t locus;
235 if (edge->call_stmt)
236 locus = gimple_location (edge->call_stmt);
237 else
238 locus = UNKNOWN_LOCATION;
239 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
240 "devirtualizing call in %s/%i to %s/%i\n",
241 edge->caller->name (), edge->caller->order,
242 target->name (),
243 target->order);
245 edge = edge->make_direct (target);
246 if (inline_summary_vec)
247 inline_update_overall_summary (node);
248 else if (edge->call_stmt)
250 edge->redirect_call_stmt_to_callee ();
252 /* Call to __builtin_unreachable shouldn't be instrumented. */
253 if (!targets.length ())
254 gimple_call_set_with_bounds (edge->call_stmt, false);
260 /* Perform reachability analysis and reclaim all unreachable nodes.
262 The algorithm is basically mark&sweep but with some extra refinements:
264 - reachable extern inline functions needs special handling; the bodies needs
265 to stay in memory until inlining in hope that they will be inlined.
266 After inlining we release their bodies and turn them into unanalyzed
267 nodes even when they are reachable.
269 - virtual functions are kept in callgraph even if they seem unreachable in
270 hope calls to them will be devirtualized.
272 Again we remove them after inlining. In late optimization some
273 devirtualization may happen, but it is not important since we won't inline
274 the call. In theory early opts and IPA should work out all important cases.
276 - virtual clones needs bodies of their origins for later materialization;
277 this means that we want to keep the body even if the origin is unreachable
278 otherwise. To avoid origin from sitting in the callgraph and being
279 walked by IPA passes, we turn them into unanalyzed nodes with body
280 defined.
282 We maintain set of function declaration where body needs to stay in
283 body_needed_for_clonning
285 Inline clones represent special case: their declaration match the
286 declaration of origin and cgraph_remove_node already knows how to
287 reshape callgraph and preserve body when offline copy of function or
288 inline clone is being removed.
290 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
291 variables with DECL_INITIAL set. We finalize these and keep reachable
292 ones around for constant folding purposes. After inlining we however
293 stop walking their references to let everything static referneced by them
294 to be removed when it is otherwise unreachable.
296 We maintain queue of both reachable symbols (i.e. defined symbols that needs
297 to stay) and symbols that are in boundary (i.e. external symbols referenced
298 by reachable symbols or origins of clones). The queue is represented
299 as linked list by AUX pointer terminated by 1.
301 At the end we keep all reachable symbols. For symbols in boundary we always
302 turn definition into a declaration, but we may keep function body around
303 based on body_needed_for_clonning
305 All symbols that enter the queue have AUX pointer non-zero and are in the
306 boundary. Pointer set REACHABLE is used to track reachable symbols.
308 Every symbol can be visited twice - once as part of boundary and once
309 as real reachable symbol. enqueue_node needs to decide whether the
310 node needs to be re-queued for second processing. For this purpose
311 we set AUX pointer of processed symbols in the boundary to constant 2. */
313 bool
314 symbol_table::remove_unreachable_nodes (FILE *file)
316 symtab_node *first = (symtab_node *) (void *) 1;
317 struct cgraph_node *node, *next;
318 varpool_node *vnode, *vnext;
319 bool changed = false;
320 hash_set<symtab_node *> reachable;
321 hash_set<tree> body_needed_for_clonning;
322 hash_set<void *> reachable_call_targets;
323 bool before_inlining_p = symtab->state < (!optimize ? IPA_SSA
324 : IPA_SSA_AFTER_INLINING);
326 timevar_push (TV_IPA_UNREACHABLE);
327 build_type_inheritance_graph ();
328 if (file)
329 fprintf (file, "\nReclaiming functions:");
330 #ifdef ENABLE_CHECKING
331 FOR_EACH_FUNCTION (node)
332 gcc_assert (!node->aux);
333 FOR_EACH_VARIABLE (vnode)
334 gcc_assert (!vnode->aux);
335 #endif
336 /* Mark functions whose bodies are obviously needed.
337 This is mostly when they can be referenced externally. Inline clones
338 are special since their declarations are shared with master clone and thus
339 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
340 FOR_EACH_FUNCTION (node)
342 node->used_as_abstract_origin = false;
343 if (node->definition
344 && !node->global.inlined_to
345 && !node->in_other_partition
346 && !node->can_remove_if_no_direct_calls_and_refs_p ())
348 gcc_assert (!node->global.inlined_to);
349 reachable.add (node);
350 enqueue_node (node, &first, &reachable);
352 else
353 gcc_assert (!node->aux);
356 /* Mark variables that are obviously needed. */
357 FOR_EACH_DEFINED_VARIABLE (vnode)
358 if (!vnode->can_remove_if_no_refs_p()
359 && !vnode->in_other_partition)
361 reachable.add (vnode);
362 enqueue_node (vnode, &first, &reachable);
365 /* Perform reachability analysis. */
366 while (first != (symtab_node *) (void *) 1)
368 bool in_boundary_p = !reachable.contains (first);
369 symtab_node *node = first;
371 first = (symtab_node *)first->aux;
373 /* If we are processing symbol in boundary, mark its AUX pointer for
374 possible later re-processing in enqueue_node. */
375 if (in_boundary_p)
376 node->aux = (void *)2;
377 else
379 if (TREE_CODE (node->decl) == FUNCTION_DECL
380 && DECL_ABSTRACT_ORIGIN (node->decl))
382 struct cgraph_node *origin_node
383 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
384 if (origin_node && !origin_node->used_as_abstract_origin)
386 origin_node->used_as_abstract_origin = true;
387 gcc_assert (!origin_node->prev_sibling_clone);
388 gcc_assert (!origin_node->next_sibling_clone);
389 for (cgraph_node *n = origin_node->clones; n;
390 n = n->next_sibling_clone)
391 if (n->decl == DECL_ABSTRACT_ORIGIN (node->decl))
392 n->used_as_abstract_origin = true;
393 enqueue_node (origin_node, &first, &reachable);
396 /* If any symbol in a comdat group is reachable, force
397 all externally visible symbols in the same comdat
398 group to be reachable as well. Comdat-local symbols
399 can be discarded if all uses were inlined. */
400 if (node->same_comdat_group)
402 symtab_node *next;
403 for (next = node->same_comdat_group;
404 next != node;
405 next = next->same_comdat_group)
406 if (!next->comdat_local_p ()
407 && !reachable.add (next))
408 enqueue_node (next, &first, &reachable);
410 /* Mark references as reachable. */
411 process_references (node, &first, before_inlining_p, &reachable);
414 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
416 /* Mark the callees reachable unless they are direct calls to extern
417 inline functions we decided to not inline. */
418 if (!in_boundary_p)
420 struct cgraph_edge *e;
421 /* Keep alive possible targets for devirtualization. */
422 if (opt_for_fn (cnode->decl, optimize)
423 && opt_for_fn (cnode->decl, flag_devirtualize))
425 struct cgraph_edge *next;
426 for (e = cnode->indirect_calls; e; e = next)
428 next = e->next_callee;
429 if (e->indirect_info->polymorphic)
430 walk_polymorphic_call_targets (&reachable_call_targets,
431 e, &first, &reachable,
432 before_inlining_p);
435 for (e = cnode->callees; e; e = e->next_callee)
437 symtab_node *body = e->callee->function_symbol ();
438 if (e->callee->definition
439 && !e->callee->in_other_partition
440 && (!e->inline_failed
441 || !DECL_EXTERNAL (e->callee->decl)
442 || e->callee->alias
443 || (before_inlining_p
444 && (opt_for_fn (body->decl, optimize)
445 || (symtab->state < IPA_SSA
446 && lookup_attribute
447 ("always_inline",
448 DECL_ATTRIBUTES (body->decl)))))))
450 /* Be sure that we will not optimize out alias target
451 body. */
452 if (DECL_EXTERNAL (e->callee->decl)
453 && e->callee->alias
454 && before_inlining_p)
455 reachable.add (body);
456 reachable.add (e->callee);
458 enqueue_node (e->callee, &first, &reachable);
461 /* When inline clone exists, mark body to be preserved so when removing
462 offline copy of the function we don't kill it. */
463 if (cnode->global.inlined_to)
464 body_needed_for_clonning.add (cnode->decl);
466 /* For non-inline clones, force their origins to the boundary and ensure
467 that body is not removed. */
468 while (cnode->clone_of)
470 bool noninline = cnode->clone_of->decl != cnode->decl;
471 cnode = cnode->clone_of;
472 if (noninline)
474 body_needed_for_clonning.add (cnode->decl);
475 enqueue_node (cnode, &first, &reachable);
480 /* If any reachable function has simd clones, mark them as
481 reachable as well. */
482 if (cnode->simd_clones)
484 cgraph_node *next;
485 for (next = cnode->simd_clones;
486 next;
487 next = next->simdclone->next_clone)
488 if (in_boundary_p
489 || !reachable.add (next))
490 enqueue_node (next, &first, &reachable);
493 /* When we see constructor of external variable, keep referred nodes in the
494 boundary. This will also hold initializers of the external vars NODE
495 refers to. */
496 varpool_node *vnode = dyn_cast <varpool_node *> (node);
497 if (vnode
498 && DECL_EXTERNAL (node->decl)
499 && !vnode->alias
500 && in_boundary_p)
502 struct ipa_ref *ref = NULL;
503 for (int i = 0; node->iterate_reference (i, ref); i++)
504 enqueue_node (ref->referred, &first, &reachable);
508 /* Remove unreachable functions. */
509 for (node = first_function (); node; node = next)
511 next = next_function (node);
513 /* If node is not needed at all, remove it. */
514 if (!node->aux)
516 if (file)
517 fprintf (file, " %s/%i", node->name (), node->order);
518 node->remove ();
519 changed = true;
521 /* If node is unreachable, remove its body. */
522 else if (!reachable.contains (node))
524 if (!body_needed_for_clonning.contains (node->decl))
525 node->release_body ();
526 else if (!node->clone_of)
527 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
528 if (node->definition)
530 if (file)
531 fprintf (file, " %s/%i", node->name (), node->order);
532 node->body_removed = true;
533 node->analyzed = false;
534 node->definition = false;
535 node->cpp_implicit_alias = false;
536 node->alias = false;
537 node->thunk.thunk_p = false;
538 node->weakref = false;
539 /* After early inlining we drop always_inline attributes on
540 bodies of functions that are still referenced (have their
541 address taken). */
542 DECL_ATTRIBUTES (node->decl)
543 = remove_attribute ("always_inline",
544 DECL_ATTRIBUTES (node->decl));
545 if (!node->in_other_partition)
546 node->local.local = false;
547 node->remove_callees ();
548 node->remove_from_same_comdat_group ();
549 node->remove_all_references ();
550 changed = true;
551 if (node->thunk.thunk_p
552 && node->thunk.add_pointer_bounds_args)
554 node->thunk.thunk_p = false;
555 node->thunk.add_pointer_bounds_args = false;
559 else
560 gcc_assert (node->clone_of || !node->has_gimple_body_p ()
561 || in_lto_p || DECL_RESULT (node->decl));
564 /* Inline clones might be kept around so their materializing allows further
565 cloning. If the function the clone is inlined into is removed, we need
566 to turn it into normal cone. */
567 FOR_EACH_FUNCTION (node)
569 if (node->global.inlined_to
570 && !node->callers)
572 gcc_assert (node->clones);
573 node->global.inlined_to = NULL;
574 update_inlined_to_pointer (node, node);
576 node->aux = NULL;
579 /* Remove unreachable variables. */
580 if (file)
581 fprintf (file, "\nReclaiming variables:");
582 for (vnode = first_variable (); vnode; vnode = vnext)
584 vnext = next_variable (vnode);
585 if (!vnode->aux
586 /* For can_refer_decl_in_current_unit_p we want to track for
587 all external variables if they are defined in other partition
588 or not. */
589 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
591 if (file)
592 fprintf (file, " %s/%i", vnode->name (), vnode->order);
593 vnode->remove ();
594 changed = true;
596 else if (!reachable.contains (vnode))
598 tree init;
599 if (vnode->definition)
601 if (file)
602 fprintf (file, " %s", vnode->name ());
603 changed = true;
605 /* Keep body if it may be useful for constant folding. */
606 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node
607 && !POINTER_BOUNDS_P (vnode->decl))
608 vnode->remove_initializer ();
609 else
610 DECL_INITIAL (vnode->decl) = init;
611 vnode->body_removed = true;
612 vnode->definition = false;
613 vnode->analyzed = false;
614 vnode->aux = NULL;
616 vnode->remove_from_same_comdat_group ();
618 vnode->remove_all_references ();
620 else
621 vnode->aux = NULL;
624 /* Now update address_taken flags and try to promote functions to be local. */
625 if (file)
626 fprintf (file, "\nClearing address taken flags:");
627 FOR_EACH_DEFINED_FUNCTION (node)
628 if (node->address_taken
629 && !node->used_from_other_partition)
631 if (!node->call_for_symbol_thunks_and_aliases
632 (has_addr_references_p, NULL, true)
633 && (!node->instrumentation_clone
634 || !node->instrumented_version
635 || !node->instrumented_version->address_taken))
637 if (file)
638 fprintf (file, " %s", node->name ());
639 node->address_taken = false;
640 changed = true;
641 if (node->local_p ())
643 node->local.local = true;
644 if (file)
645 fprintf (file, " (local)");
649 if (file)
650 fprintf (file, "\n");
652 #ifdef ENABLE_CHECKING
653 symtab_node::verify_symtab_nodes ();
654 #endif
656 /* If we removed something, perhaps profile could be improved. */
657 if (changed && optimize && inline_edge_summary_vec.exists ())
658 FOR_EACH_DEFINED_FUNCTION (node)
659 ipa_propagate_frequency (node);
661 timevar_pop (TV_IPA_UNREACHABLE);
662 return changed;
665 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
666 as needed, also clear EXPLICIT_REFS if the references to given variable
667 do not need to be explicit. */
669 void
670 process_references (varpool_node *vnode,
671 bool *written, bool *address_taken,
672 bool *read, bool *explicit_refs)
674 int i;
675 struct ipa_ref *ref;
677 if (!vnode->all_refs_explicit_p ()
678 || TREE_THIS_VOLATILE (vnode->decl))
679 *explicit_refs = false;
681 for (i = 0; vnode->iterate_referring (i, ref)
682 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
683 switch (ref->use)
685 case IPA_REF_ADDR:
686 *address_taken = true;
687 break;
688 case IPA_REF_LOAD:
689 *read = true;
690 break;
691 case IPA_REF_STORE:
692 *written = true;
693 break;
694 case IPA_REF_ALIAS:
695 process_references (dyn_cast<varpool_node *> (ref->referring), written,
696 address_taken, read, explicit_refs);
697 break;
698 case IPA_REF_CHKP:
699 gcc_unreachable ();
703 /* Set TREE_READONLY bit. */
705 bool
706 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
708 TREE_READONLY (vnode->decl) = true;
709 return false;
712 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
714 bool
715 set_writeonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
717 vnode->writeonly = true;
718 if (optimize)
720 DECL_INITIAL (vnode->decl) = NULL;
721 if (!vnode->alias)
722 vnode->remove_all_references ();
724 return false;
727 /* Clear addressale bit of VNODE. */
729 bool
730 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
732 vnode->address_taken = false;
733 TREE_ADDRESSABLE (vnode->decl) = 0;
734 return false;
737 /* Discover variables that have no longer address taken or that are read only
738 and update their flags.
740 FIXME: This can not be done in between gimplify and omp_expand since
741 readonly flag plays role on what is shared and what is not. Currently we do
742 this transformation as part of whole program visibility and re-do at
743 ipa-reference pass (to take into account clonning), but it would
744 make sense to do it before early optimizations. */
746 void
747 ipa_discover_readonly_nonaddressable_vars (void)
749 varpool_node *vnode;
750 if (dump_file)
751 fprintf (dump_file, "Clearing variable flags:");
752 FOR_EACH_VARIABLE (vnode)
753 if (!vnode->alias
754 && (TREE_ADDRESSABLE (vnode->decl)
755 || !vnode->writeonly
756 || !TREE_READONLY (vnode->decl)))
758 bool written = false;
759 bool address_taken = false;
760 bool read = false;
761 bool explicit_refs = true;
763 process_references (vnode, &written, &address_taken, &read, &explicit_refs);
764 if (!explicit_refs)
765 continue;
766 if (!address_taken)
768 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
769 fprintf (dump_file, " %s (non-addressable)", vnode->name ());
770 vnode->call_for_node_and_aliases (clear_addressable_bit, NULL, true);
772 if (!address_taken && !written
773 /* Making variable in explicit section readonly can cause section
774 type conflict.
775 See e.g. gcc.c-torture/compile/pr23237.c */
776 && vnode->get_section () == NULL)
778 if (!TREE_READONLY (vnode->decl) && dump_file)
779 fprintf (dump_file, " %s (read-only)", vnode->name ());
780 vnode->call_for_node_and_aliases (set_readonly_bit, NULL, true);
782 if (!vnode->writeonly && !read && !address_taken && written)
784 if (dump_file)
785 fprintf (dump_file, " %s (write-only)", vnode->name ());
786 vnode->call_for_node_and_aliases (set_writeonly_bit, NULL, true);
789 if (dump_file)
790 fprintf (dump_file, "\n");
793 /* Free inline summary. */
795 namespace {
797 const pass_data pass_data_ipa_free_inline_summary =
799 SIMPLE_IPA_PASS, /* type */
800 "free-inline-summary", /* name */
801 OPTGROUP_NONE, /* optinfo_flags */
802 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
803 0, /* properties_required */
804 0, /* properties_provided */
805 0, /* properties_destroyed */
806 0, /* todo_flags_start */
807 /* Early optimizations may make function unreachable. We can not
808 remove unreachable functions as part of the ealry opts pass because
809 TODOs are run before subpasses. Do it here. */
810 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
813 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
815 public:
816 pass_ipa_free_inline_summary (gcc::context *ctxt)
817 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
820 /* opt_pass methods: */
821 virtual unsigned int execute (function *)
823 inline_free_summary ();
824 return 0;
827 }; // class pass_ipa_free_inline_summary
829 } // anon namespace
831 simple_ipa_opt_pass *
832 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
834 return new pass_ipa_free_inline_summary (ctxt);
837 /* Generate and emit a static constructor or destructor. WHICH must
838 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
839 (for chp static vars constructor) or 'B' (for chkp static bounds
840 constructor). BODY is a STATEMENT_LIST containing GENERIC
841 statements. PRIORITY is the initialization priority for this
842 constructor or destructor.
844 FINAL specify whether the externally visible name for collect2 should
845 be produced. */
847 static void
848 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
850 static int counter = 0;
851 char which_buf[16];
852 tree decl, name, resdecl;
854 /* The priority is encoded in the constructor or destructor name.
855 collect2 will sort the names and arrange that they are called at
856 program startup. */
857 if (final)
858 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
859 else
860 /* Proudce sane name but one not recognizable by collect2, just for the
861 case we fail to inline the function. */
862 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
863 name = get_file_function_name (which_buf);
865 decl = build_decl (input_location, FUNCTION_DECL, name,
866 build_function_type_list (void_type_node, NULL_TREE));
867 current_function_decl = decl;
869 resdecl = build_decl (input_location,
870 RESULT_DECL, NULL_TREE, void_type_node);
871 DECL_ARTIFICIAL (resdecl) = 1;
872 DECL_RESULT (decl) = resdecl;
873 DECL_CONTEXT (resdecl) = decl;
875 allocate_struct_function (decl, false);
877 TREE_STATIC (decl) = 1;
878 TREE_USED (decl) = 1;
879 DECL_ARTIFICIAL (decl) = 1;
880 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
881 DECL_SAVED_TREE (decl) = body;
882 if (!targetm.have_ctors_dtors && final)
884 TREE_PUBLIC (decl) = 1;
885 DECL_PRESERVE_P (decl) = 1;
887 DECL_UNINLINABLE (decl) = 1;
889 DECL_INITIAL (decl) = make_node (BLOCK);
890 TREE_USED (DECL_INITIAL (decl)) = 1;
892 DECL_SOURCE_LOCATION (decl) = input_location;
893 cfun->function_end_locus = input_location;
895 switch (which)
897 case 'I':
898 DECL_STATIC_CONSTRUCTOR (decl) = 1;
899 decl_init_priority_insert (decl, priority);
900 break;
901 case 'P':
902 DECL_STATIC_CONSTRUCTOR (decl) = 1;
903 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("chkp ctor"),
904 NULL,
905 NULL_TREE);
906 decl_init_priority_insert (decl, priority);
907 break;
908 case 'B':
909 DECL_STATIC_CONSTRUCTOR (decl) = 1;
910 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("bnd_legacy"),
911 NULL,
912 NULL_TREE);
913 decl_init_priority_insert (decl, priority);
914 break;
915 case 'D':
916 DECL_STATIC_DESTRUCTOR (decl) = 1;
917 decl_fini_priority_insert (decl, priority);
918 break;
919 default:
920 gcc_unreachable ();
923 gimplify_function_tree (decl);
925 cgraph_node::add_new_function (decl, false);
927 set_cfun (NULL);
928 current_function_decl = NULL;
931 /* Generate and emit a static constructor or destructor. WHICH must
932 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
933 (for chkp static vars constructor) or 'B' (for chkp static bounds
934 constructor). BODY is a STATEMENT_LIST containing GENERIC
935 statements. PRIORITY is the initialization priority for this
936 constructor or destructor. */
938 void
939 cgraph_build_static_cdtor (char which, tree body, int priority)
941 cgraph_build_static_cdtor_1 (which, body, priority, false);
944 /* A vector of FUNCTION_DECLs declared as static constructors. */
945 static vec<tree> static_ctors;
946 /* A vector of FUNCTION_DECLs declared as static destructors. */
947 static vec<tree> static_dtors;
949 /* When target does not have ctors and dtors, we call all constructor
950 and destructor by special initialization/destruction function
951 recognized by collect2.
953 When we are going to build this function, collect all constructors and
954 destructors and turn them into normal functions. */
956 static void
957 record_cdtor_fn (struct cgraph_node *node)
959 if (DECL_STATIC_CONSTRUCTOR (node->decl))
960 static_ctors.safe_push (node->decl);
961 if (DECL_STATIC_DESTRUCTOR (node->decl))
962 static_dtors.safe_push (node->decl);
963 node = cgraph_node::get (node->decl);
964 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
967 /* Define global constructors/destructor functions for the CDTORS, of
968 which they are LEN. The CDTORS are sorted by initialization
969 priority. If CTOR_P is true, these are constructors; otherwise,
970 they are destructors. */
972 static void
973 build_cdtor (bool ctor_p, vec<tree> cdtors)
975 size_t i,j;
976 size_t len = cdtors.length ();
978 i = 0;
979 while (i < len)
981 tree body;
982 tree fn;
983 priority_type priority;
985 priority = 0;
986 body = NULL_TREE;
987 j = i;
990 priority_type p;
991 fn = cdtors[j];
992 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
993 if (j == i)
994 priority = p;
995 else if (p != priority)
996 break;
997 j++;
999 while (j < len);
1001 /* When there is only one cdtor and target supports them, do nothing. */
1002 if (j == i + 1
1003 && targetm.have_ctors_dtors)
1005 i++;
1006 continue;
1008 /* Find the next batch of constructors/destructors with the same
1009 initialization priority. */
1010 for (;i < j; i++)
1012 tree call;
1013 fn = cdtors[i];
1014 call = build_call_expr (fn, 0);
1015 if (ctor_p)
1016 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1017 else
1018 DECL_STATIC_DESTRUCTOR (fn) = 0;
1019 /* We do not want to optimize away pure/const calls here.
1020 When optimizing, these should be already removed, when not
1021 optimizing, we want user to be able to breakpoint in them. */
1022 TREE_SIDE_EFFECTS (call) = 1;
1023 append_to_statement_list (call, &body);
1025 gcc_assert (body != NULL_TREE);
1026 /* Generate a function to call all the function of like
1027 priority. */
1028 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1032 /* Comparison function for qsort. P1 and P2 are actually of type
1033 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1034 used to determine the sort order. */
1036 static int
1037 compare_ctor (const void *p1, const void *p2)
1039 tree f1;
1040 tree f2;
1041 int priority1;
1042 int priority2;
1044 f1 = *(const tree *)p1;
1045 f2 = *(const tree *)p2;
1046 priority1 = DECL_INIT_PRIORITY (f1);
1047 priority2 = DECL_INIT_PRIORITY (f2);
1049 if (priority1 < priority2)
1050 return -1;
1051 else if (priority1 > priority2)
1052 return 1;
1053 else
1054 /* Ensure a stable sort. Constructors are executed in backwarding
1055 order to make LTO initialize braries first. */
1056 return DECL_UID (f2) - DECL_UID (f1);
1059 /* Comparison function for qsort. P1 and P2 are actually of type
1060 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1061 used to determine the sort order. */
1063 static int
1064 compare_dtor (const void *p1, const void *p2)
1066 tree f1;
1067 tree f2;
1068 int priority1;
1069 int priority2;
1071 f1 = *(const tree *)p1;
1072 f2 = *(const tree *)p2;
1073 priority1 = DECL_FINI_PRIORITY (f1);
1074 priority2 = DECL_FINI_PRIORITY (f2);
1076 if (priority1 < priority2)
1077 return -1;
1078 else if (priority1 > priority2)
1079 return 1;
1080 else
1081 /* Ensure a stable sort. */
1082 return DECL_UID (f1) - DECL_UID (f2);
1085 /* Generate functions to call static constructors and destructors
1086 for targets that do not support .ctors/.dtors sections. These
1087 functions have magic names which are detected by collect2. */
1089 static void
1090 build_cdtor_fns (void)
1092 if (!static_ctors.is_empty ())
1094 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1095 static_ctors.qsort (compare_ctor);
1096 build_cdtor (/*ctor_p=*/true, static_ctors);
1099 if (!static_dtors.is_empty ())
1101 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1102 static_dtors.qsort (compare_dtor);
1103 build_cdtor (/*ctor_p=*/false, static_dtors);
1107 /* Look for constructors and destructors and produce function calling them.
1108 This is needed for targets not supporting ctors or dtors, but we perform the
1109 transformation also at linktime to merge possibly numerous
1110 constructors/destructors into single function to improve code locality and
1111 reduce size. */
1113 static unsigned int
1114 ipa_cdtor_merge (void)
1116 struct cgraph_node *node;
1117 FOR_EACH_DEFINED_FUNCTION (node)
1118 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1119 || DECL_STATIC_DESTRUCTOR (node->decl))
1120 record_cdtor_fn (node);
1121 build_cdtor_fns ();
1122 static_ctors.release ();
1123 static_dtors.release ();
1124 return 0;
1127 namespace {
1129 const pass_data pass_data_ipa_cdtor_merge =
1131 IPA_PASS, /* type */
1132 "cdtor", /* name */
1133 OPTGROUP_NONE, /* optinfo_flags */
1134 TV_CGRAPHOPT, /* tv_id */
1135 0, /* properties_required */
1136 0, /* properties_provided */
1137 0, /* properties_destroyed */
1138 0, /* todo_flags_start */
1139 0, /* todo_flags_finish */
1142 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1144 public:
1145 pass_ipa_cdtor_merge (gcc::context *ctxt)
1146 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1147 NULL, /* generate_summary */
1148 NULL, /* write_summary */
1149 NULL, /* read_summary */
1150 NULL, /* write_optimization_summary */
1151 NULL, /* read_optimization_summary */
1152 NULL, /* stmt_fixup */
1153 0, /* function_transform_todo_flags_start */
1154 NULL, /* function_transform */
1155 NULL) /* variable_transform */
1158 /* opt_pass methods: */
1159 virtual bool gate (function *);
1160 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1162 }; // class pass_ipa_cdtor_merge
1164 bool
1165 pass_ipa_cdtor_merge::gate (function *)
1167 /* Perform the pass when we have no ctors/dtors support
1168 or at LTO time to merge multiple constructors into single
1169 function. */
1170 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1173 } // anon namespace
1175 ipa_opt_pass_d *
1176 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1178 return new pass_ipa_cdtor_merge (ctxt);
1181 /* Invalid pointer representing BOTTOM for single user dataflow. */
1182 #define BOTTOM ((cgraph_node *)(size_t) 2)
1184 /* Meet operation for single user dataflow.
1185 Here we want to associate variables with sigle function that may access it.
1187 FUNCTION is current single user of a variable, VAR is variable that uses it.
1188 Latttice is stored in SINGLE_USER_MAP.
1190 We represent:
1191 - TOP by no entry in SIGNLE_USER_MAP
1192 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1193 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1195 cgraph_node *
1196 meet (cgraph_node *function, varpool_node *var,
1197 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1199 struct cgraph_node *user, **f;
1201 if (var->aux == BOTTOM)
1202 return BOTTOM;
1204 f = single_user_map.get (var);
1205 if (!f)
1206 return function;
1207 user = *f;
1208 if (!function)
1209 return user;
1210 else if (function != user)
1211 return BOTTOM;
1212 else
1213 return function;
1216 /* Propagation step of single-use dataflow.
1218 Check all uses of VNODE and see if they are used by single function FUNCTION.
1219 SINGLE_USER_MAP represents the dataflow lattice. */
1221 cgraph_node *
1222 propagate_single_user (varpool_node *vnode, cgraph_node *function,
1223 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1225 int i;
1226 struct ipa_ref *ref;
1228 gcc_assert (!vnode->externally_visible);
1230 /* If node is an alias, first meet with its target. */
1231 if (vnode->alias)
1232 function = meet (function, vnode->get_alias_target (), single_user_map);
1234 /* Check all users and see if they correspond to a single function. */
1235 for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
1237 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
1238 if (cnode)
1240 if (cnode->global.inlined_to)
1241 cnode = cnode->global.inlined_to;
1242 if (!function)
1243 function = cnode;
1244 else if (function != cnode)
1245 function = BOTTOM;
1247 else
1248 function = meet (function, dyn_cast <varpool_node *> (ref->referring),
1249 single_user_map);
1251 return function;
1254 /* Pass setting used_by_single_function flag.
1255 This flag is set on variable when there is only one function that may
1256 possibly referr to it. */
1258 static unsigned int
1259 ipa_single_use (void)
1261 varpool_node *first = (varpool_node *) (void *) 1;
1262 varpool_node *var;
1263 hash_map<varpool_node *, cgraph_node *> single_user_map;
1265 FOR_EACH_DEFINED_VARIABLE (var)
1266 if (!var->all_refs_explicit_p ())
1267 var->aux = BOTTOM;
1268 else
1270 /* Enqueue symbol for dataflow. */
1271 var->aux = first;
1272 first = var;
1275 /* The actual dataflow. */
1277 while (first != (void *) 1)
1279 cgraph_node *user, *orig_user, **f;
1281 var = first;
1282 first = (varpool_node *)first->aux;
1284 f = single_user_map.get (var);
1285 if (f)
1286 orig_user = *f;
1287 else
1288 orig_user = NULL;
1289 user = propagate_single_user (var, orig_user, single_user_map);
1291 gcc_checking_assert (var->aux != BOTTOM);
1293 /* If user differs, enqueue all references. */
1294 if (user != orig_user)
1296 unsigned int i;
1297 ipa_ref *ref;
1299 single_user_map.put (var, user);
1301 /* Enqueue all aliases for re-processing. */
1302 for (i = 0; var->iterate_referring (i, ref); i++)
1303 if (ref->use == IPA_REF_ALIAS
1304 && !ref->referring->aux)
1306 ref->referring->aux = first;
1307 first = dyn_cast <varpool_node *> (ref->referring);
1309 /* Enqueue all users for re-processing. */
1310 for (i = 0; var->iterate_reference (i, ref); i++)
1311 if (!ref->referred->aux
1312 && ref->referred->definition
1313 && is_a <varpool_node *> (ref->referred))
1315 ref->referred->aux = first;
1316 first = dyn_cast <varpool_node *> (ref->referred);
1319 /* If user is BOTTOM, just punt on this var. */
1320 if (user == BOTTOM)
1321 var->aux = BOTTOM;
1322 else
1323 var->aux = NULL;
1325 else
1326 var->aux = NULL;
1329 FOR_EACH_DEFINED_VARIABLE (var)
1331 if (var->aux != BOTTOM)
1333 #ifdef ENABLE_CHECKING
1334 /* Not having the single user known means that the VAR is
1335 unreachable. Either someone forgot to remove unreachable
1336 variables or the reachability here is wrong. */
1338 gcc_assert (single_user_map.get (var));
1339 #endif
1340 if (dump_file)
1342 fprintf (dump_file, "Variable %s/%i is used by single function\n",
1343 var->name (), var->order);
1345 var->used_by_single_function = true;
1347 var->aux = NULL;
1349 return 0;
1352 namespace {
1354 const pass_data pass_data_ipa_single_use =
1356 IPA_PASS, /* type */
1357 "single-use", /* name */
1358 OPTGROUP_NONE, /* optinfo_flags */
1359 TV_CGRAPHOPT, /* tv_id */
1360 0, /* properties_required */
1361 0, /* properties_provided */
1362 0, /* properties_destroyed */
1363 0, /* todo_flags_start */
1364 0, /* todo_flags_finish */
1367 class pass_ipa_single_use : public ipa_opt_pass_d
1369 public:
1370 pass_ipa_single_use (gcc::context *ctxt)
1371 : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
1372 NULL, /* generate_summary */
1373 NULL, /* write_summary */
1374 NULL, /* read_summary */
1375 NULL, /* write_optimization_summary */
1376 NULL, /* read_optimization_summary */
1377 NULL, /* stmt_fixup */
1378 0, /* function_transform_todo_flags_start */
1379 NULL, /* function_transform */
1380 NULL) /* variable_transform */
1383 /* opt_pass methods: */
1384 virtual bool gate (function *);
1385 virtual unsigned int execute (function *) { return ipa_single_use (); }
1387 }; // class pass_ipa_single_use
1389 bool
1390 pass_ipa_single_use::gate (function *)
1392 return optimize;
1395 } // anon namespace
1397 ipa_opt_pass_d *
1398 make_pass_ipa_single_use (gcc::context *ctxt)
1400 return new pass_ipa_single_use (ctxt);