2015-01-14 Sandra Loosemore <sandra@codesourcery.com>
[official-gcc.git] / gcc / ipa.c
blobdf96515140f0ac500d526158ec589acafbfbd5d8
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2015 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 "hash-set.h"
25 #include "machmode.h"
26 #include "vec.h"
27 #include "double-int.h"
28 #include "input.h"
29 #include "alias.h"
30 #include "symtab.h"
31 #include "options.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "calls.h"
37 #include "stringpool.h"
38 #include "predict.h"
39 #include "basic-block.h"
40 #include "hash-map.h"
41 #include "is-a.h"
42 #include "plugin-api.h"
43 #include "hard-reg-set.h"
44 #include "input.h"
45 #include "function.h"
46 #include "ipa-ref.h"
47 #include "cgraph.h"
48 #include "tree-pass.h"
49 #include "gimple-expr.h"
50 #include "gimplify.h"
51 #include "flags.h"
52 #include "target.h"
53 #include "tree-iterator.h"
54 #include "ipa-utils.h"
55 #include "alloc-pool.h"
56 #include "symbol-summary.h"
57 #include "ipa-prop.h"
58 #include "ipa-inline.h"
59 #include "tree-inline.h"
60 #include "profile.h"
61 #include "params.h"
62 #include "internal-fn.h"
63 #include "tree-ssa-alias.h"
64 #include "gimple.h"
65 #include "dbgcnt.h"
68 /* Return true when NODE has ADDR reference. */
70 static bool
71 has_addr_references_p (struct cgraph_node *node,
72 void *data ATTRIBUTE_UNUSED)
74 int i;
75 struct ipa_ref *ref = NULL;
77 for (i = 0; node->iterate_referring (i, ref); i++)
78 if (ref->use == IPA_REF_ADDR)
79 return true;
80 return false;
83 /* Look for all functions inlined to NODE and update their inlined_to pointers
84 to INLINED_TO. */
86 static void
87 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
89 struct cgraph_edge *e;
90 for (e = node->callees; e; e = e->next_callee)
91 if (e->callee->global.inlined_to)
93 e->callee->global.inlined_to = inlined_to;
94 update_inlined_to_pointer (e->callee, inlined_to);
98 /* Add symtab NODE to queue starting at FIRST.
100 The queue is linked via AUX pointers and terminated by pointer to 1.
101 We enqueue nodes at two occasions: when we find them reachable or when we find
102 their bodies needed for further clonning. In the second case we mark them
103 by pointer to 2 after processing so they are re-queue when they become
104 reachable. */
106 static void
107 enqueue_node (symtab_node *node, symtab_node **first,
108 hash_set<symtab_node *> *reachable)
110 /* Node is still in queue; do nothing. */
111 if (node->aux && node->aux != (void *) 2)
112 return;
113 /* Node was already processed as unreachable, re-enqueue
114 only if it became reachable now. */
115 if (node->aux == (void *)2 && !reachable->contains (node))
116 return;
117 node->aux = *first;
118 *first = node;
121 /* Process references. */
123 static void
124 process_references (symtab_node *snode,
125 symtab_node **first,
126 bool before_inlining_p,
127 hash_set<symtab_node *> *reachable)
129 int i;
130 struct ipa_ref *ref = NULL;
131 for (i = 0; snode->iterate_reference (i, ref); i++)
133 symtab_node *node = ref->referred;
134 symtab_node *body = node->ultimate_alias_target ();
136 if (node->definition && !node->in_other_partition
137 && ((!DECL_EXTERNAL (node->decl) || node->alias)
138 || (((before_inlining_p
139 && ((TREE_CODE (node->decl) != FUNCTION_DECL
140 && optimize)
141 || (TREE_CODE (node->decl) == FUNCTION_DECL
142 && opt_for_fn (body->decl, optimize))
143 || (symtab->state < IPA_SSA
144 && lookup_attribute
145 ("always_inline",
146 DECL_ATTRIBUTES (body->decl))))))
147 /* We use variable constructors during late compilation for
148 constant folding. Keep references alive so partitioning
149 knows about potential references. */
150 || (TREE_CODE (node->decl) == VAR_DECL
151 && flag_wpa
152 && ctor_for_folding (node->decl)
153 != error_mark_node))))
155 /* Be sure that we will not optimize out alias target
156 body. */
157 if (DECL_EXTERNAL (node->decl)
158 && node->alias
159 && before_inlining_p)
160 reachable->add (body);
161 reachable->add (node);
163 enqueue_node (node, first, reachable);
167 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
168 all its potential targets as reachable to permit later inlining if
169 devirtualization happens. After inlining still keep their declarations
170 around, so we can devirtualize to a direct call.
172 Also try to make trivial devirutalization when no or only one target is
173 possible. */
175 static void
176 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
177 struct cgraph_edge *edge,
178 symtab_node **first,
179 hash_set<symtab_node *> *reachable,
180 bool before_inlining_p)
182 unsigned int i;
183 void *cache_token;
184 bool final;
185 vec <cgraph_node *>targets
186 = possible_polymorphic_call_targets
187 (edge, &final, &cache_token);
189 if (!reachable_call_targets->add (cache_token))
191 for (i = 0; i < targets.length (); i++)
193 struct cgraph_node *n = targets[i];
195 /* Do not bother to mark virtual methods in anonymous namespace;
196 either we will find use of virtual table defining it, or it is
197 unused. */
198 if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
199 && type_in_anonymous_namespace_p
200 (method_class_type (TREE_TYPE (n->decl))))
201 continue;
203 symtab_node *body = n->function_symbol ();
205 /* Prior inlining, keep alive bodies of possible targets for
206 devirtualization. */
207 if (n->definition
208 && (before_inlining_p
209 && opt_for_fn (body->decl, optimize)
210 && opt_for_fn (body->decl, flag_devirtualize)))
212 /* Be sure that we will not optimize out alias target
213 body. */
214 if (DECL_EXTERNAL (n->decl)
215 && n->alias
216 && before_inlining_p)
217 reachable->add (body);
218 reachable->add (n);
220 /* Even after inlining we want to keep the possible targets in the
221 boundary, so late passes can still produce direct call even if
222 the chance for inlining is lost. */
223 enqueue_node (n, first, reachable);
227 /* Very trivial devirtualization; when the type is
228 final or anonymous (so we know all its derivation)
229 and there is only one possible virtual call target,
230 make the edge direct. */
231 if (final)
233 if (targets.length () <= 1 && dbg_cnt (devirt))
235 cgraph_node *target, *node = edge->caller;
236 if (targets.length () == 1)
237 target = targets[0];
238 else
239 target = cgraph_node::get_create
240 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
242 if (dump_enabled_p ())
244 location_t locus;
245 if (edge->call_stmt)
246 locus = gimple_location (edge->call_stmt);
247 else
248 locus = UNKNOWN_LOCATION;
249 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
250 "devirtualizing call in %s/%i to %s/%i\n",
251 edge->caller->name (), edge->caller->order,
252 target->name (),
253 target->order);
255 edge = edge->make_direct (target);
256 if (inline_summaries)
257 inline_update_overall_summary (node);
258 else if (edge->call_stmt)
260 edge->redirect_call_stmt_to_callee ();
262 /* Call to __builtin_unreachable shouldn't be instrumented. */
263 if (!targets.length ())
264 gimple_call_set_with_bounds (edge->call_stmt, false);
270 /* Perform reachability analysis and reclaim all unreachable nodes.
272 The algorithm is basically mark&sweep but with some extra refinements:
274 - reachable extern inline functions needs special handling; the bodies needs
275 to stay in memory until inlining in hope that they will be inlined.
276 After inlining we release their bodies and turn them into unanalyzed
277 nodes even when they are reachable.
279 - virtual functions are kept in callgraph even if they seem unreachable in
280 hope calls to them will be devirtualized.
282 Again we remove them after inlining. In late optimization some
283 devirtualization may happen, but it is not important since we won't inline
284 the call. In theory early opts and IPA should work out all important cases.
286 - virtual clones needs bodies of their origins for later materialization;
287 this means that we want to keep the body even if the origin is unreachable
288 otherwise. To avoid origin from sitting in the callgraph and being
289 walked by IPA passes, we turn them into unanalyzed nodes with body
290 defined.
292 We maintain set of function declaration where body needs to stay in
293 body_needed_for_clonning
295 Inline clones represent special case: their declaration match the
296 declaration of origin and cgraph_remove_node already knows how to
297 reshape callgraph and preserve body when offline copy of function or
298 inline clone is being removed.
300 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
301 variables with DECL_INITIAL set. We finalize these and keep reachable
302 ones around for constant folding purposes. After inlining we however
303 stop walking their references to let everything static referneced by them
304 to be removed when it is otherwise unreachable.
306 We maintain queue of both reachable symbols (i.e. defined symbols that needs
307 to stay) and symbols that are in boundary (i.e. external symbols referenced
308 by reachable symbols or origins of clones). The queue is represented
309 as linked list by AUX pointer terminated by 1.
311 At the end we keep all reachable symbols. For symbols in boundary we always
312 turn definition into a declaration, but we may keep function body around
313 based on body_needed_for_clonning
315 All symbols that enter the queue have AUX pointer non-zero and are in the
316 boundary. Pointer set REACHABLE is used to track reachable symbols.
318 Every symbol can be visited twice - once as part of boundary and once
319 as real reachable symbol. enqueue_node needs to decide whether the
320 node needs to be re-queued for second processing. For this purpose
321 we set AUX pointer of processed symbols in the boundary to constant 2. */
323 bool
324 symbol_table::remove_unreachable_nodes (FILE *file)
326 symtab_node *first = (symtab_node *) (void *) 1;
327 struct cgraph_node *node, *next;
328 varpool_node *vnode, *vnext;
329 bool changed = false;
330 hash_set<symtab_node *> reachable;
331 hash_set<tree> body_needed_for_clonning;
332 hash_set<void *> reachable_call_targets;
333 bool before_inlining_p = symtab->state < (!optimize ? IPA_SSA
334 : IPA_SSA_AFTER_INLINING);
336 timevar_push (TV_IPA_UNREACHABLE);
337 build_type_inheritance_graph ();
338 if (file)
339 fprintf (file, "\nReclaiming functions:");
340 #ifdef ENABLE_CHECKING
341 FOR_EACH_FUNCTION (node)
342 gcc_assert (!node->aux);
343 FOR_EACH_VARIABLE (vnode)
344 gcc_assert (!vnode->aux);
345 #endif
346 /* Mark functions whose bodies are obviously needed.
347 This is mostly when they can be referenced externally. Inline clones
348 are special since their declarations are shared with master clone and thus
349 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
350 FOR_EACH_FUNCTION (node)
352 node->used_as_abstract_origin = false;
353 if (node->definition
354 && !node->global.inlined_to
355 && !node->in_other_partition
356 && !node->can_remove_if_no_direct_calls_and_refs_p ())
358 gcc_assert (!node->global.inlined_to);
359 reachable.add (node);
360 enqueue_node (node, &first, &reachable);
362 else
363 gcc_assert (!node->aux);
366 /* Mark variables that are obviously needed. */
367 FOR_EACH_DEFINED_VARIABLE (vnode)
368 if (!vnode->can_remove_if_no_refs_p()
369 && !vnode->in_other_partition)
371 reachable.add (vnode);
372 enqueue_node (vnode, &first, &reachable);
375 /* Perform reachability analysis. */
376 while (first != (symtab_node *) (void *) 1)
378 bool in_boundary_p = !reachable.contains (first);
379 symtab_node *node = first;
381 first = (symtab_node *)first->aux;
383 /* If we are processing symbol in boundary, mark its AUX pointer for
384 possible later re-processing in enqueue_node. */
385 if (in_boundary_p)
386 node->aux = (void *)2;
387 else
389 if (TREE_CODE (node->decl) == FUNCTION_DECL
390 && DECL_ABSTRACT_ORIGIN (node->decl))
392 struct cgraph_node *origin_node
393 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
394 if (origin_node && !origin_node->used_as_abstract_origin)
396 origin_node->used_as_abstract_origin = true;
397 gcc_assert (!origin_node->prev_sibling_clone);
398 gcc_assert (!origin_node->next_sibling_clone);
399 for (cgraph_node *n = origin_node->clones; n;
400 n = n->next_sibling_clone)
401 if (n->decl == DECL_ABSTRACT_ORIGIN (node->decl))
402 n->used_as_abstract_origin = true;
403 enqueue_node (origin_node, &first, &reachable);
406 /* If any symbol in a comdat group is reachable, force
407 all externally visible symbols in the same comdat
408 group to be reachable as well. Comdat-local symbols
409 can be discarded if all uses were inlined. */
410 if (node->same_comdat_group)
412 symtab_node *next;
413 for (next = node->same_comdat_group;
414 next != node;
415 next = next->same_comdat_group)
416 if (!next->comdat_local_p ()
417 && !reachable.add (next))
418 enqueue_node (next, &first, &reachable);
420 /* Mark references as reachable. */
421 process_references (node, &first, before_inlining_p, &reachable);
424 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
426 /* Mark the callees reachable unless they are direct calls to extern
427 inline functions we decided to not inline. */
428 if (!in_boundary_p)
430 struct cgraph_edge *e;
431 /* Keep alive possible targets for devirtualization. */
432 if (opt_for_fn (cnode->decl, optimize)
433 && opt_for_fn (cnode->decl, flag_devirtualize))
435 struct cgraph_edge *next;
436 for (e = cnode->indirect_calls; e; e = next)
438 next = e->next_callee;
439 if (e->indirect_info->polymorphic)
440 walk_polymorphic_call_targets (&reachable_call_targets,
441 e, &first, &reachable,
442 before_inlining_p);
445 for (e = cnode->callees; e; e = e->next_callee)
447 symtab_node *body = e->callee->function_symbol ();
448 if (e->callee->definition
449 && !e->callee->in_other_partition
450 && (!e->inline_failed
451 || !DECL_EXTERNAL (e->callee->decl)
452 || e->callee->alias
453 || (before_inlining_p
454 && (opt_for_fn (body->decl, optimize)
455 || (symtab->state < IPA_SSA
456 && lookup_attribute
457 ("always_inline",
458 DECL_ATTRIBUTES (body->decl)))))))
460 /* Be sure that we will not optimize out alias target
461 body. */
462 if (DECL_EXTERNAL (e->callee->decl)
463 && e->callee->alias
464 && before_inlining_p)
465 reachable.add (body);
466 reachable.add (e->callee);
468 enqueue_node (e->callee, &first, &reachable);
471 /* When inline clone exists, mark body to be preserved so when removing
472 offline copy of the function we don't kill it. */
473 if (cnode->global.inlined_to)
474 body_needed_for_clonning.add (cnode->decl);
476 /* For non-inline clones, force their origins to the boundary and ensure
477 that body is not removed. */
478 while (cnode->clone_of)
480 bool noninline = cnode->clone_of->decl != cnode->decl;
481 cnode = cnode->clone_of;
482 if (noninline)
484 body_needed_for_clonning.add (cnode->decl);
485 enqueue_node (cnode, &first, &reachable);
490 /* If any reachable function has simd clones, mark them as
491 reachable as well. */
492 if (cnode->simd_clones)
494 cgraph_node *next;
495 for (next = cnode->simd_clones;
496 next;
497 next = next->simdclone->next_clone)
498 if (in_boundary_p
499 || !reachable.add (next))
500 enqueue_node (next, &first, &reachable);
503 /* When we see constructor of external variable, keep referred nodes in the
504 boundary. This will also hold initializers of the external vars NODE
505 refers to. */
506 varpool_node *vnode = dyn_cast <varpool_node *> (node);
507 if (vnode
508 && DECL_EXTERNAL (node->decl)
509 && !vnode->alias
510 && in_boundary_p)
512 struct ipa_ref *ref = NULL;
513 for (int i = 0; node->iterate_reference (i, ref); i++)
514 enqueue_node (ref->referred, &first, &reachable);
518 /* Remove unreachable functions. */
519 for (node = first_function (); node; node = next)
521 next = next_function (node);
523 /* If node is not needed at all, remove it. */
524 if (!node->aux)
526 if (file)
527 fprintf (file, " %s/%i", node->name (), node->order);
528 node->remove ();
529 changed = true;
531 /* If node is unreachable, remove its body. */
532 else if (!reachable.contains (node))
534 if (!body_needed_for_clonning.contains (node->decl))
535 node->release_body ();
536 else if (!node->clone_of)
537 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
538 if (node->definition)
540 if (file)
541 fprintf (file, " %s/%i", node->name (), node->order);
542 node->body_removed = true;
543 node->analyzed = false;
544 node->definition = false;
545 node->cpp_implicit_alias = false;
546 node->alias = false;
547 node->thunk.thunk_p = false;
548 node->weakref = false;
549 /* After early inlining we drop always_inline attributes on
550 bodies of functions that are still referenced (have their
551 address taken). */
552 DECL_ATTRIBUTES (node->decl)
553 = remove_attribute ("always_inline",
554 DECL_ATTRIBUTES (node->decl));
555 if (!node->in_other_partition)
556 node->local.local = false;
557 node->remove_callees ();
558 node->remove_from_same_comdat_group ();
559 node->remove_all_references ();
560 changed = true;
561 if (node->thunk.thunk_p
562 && node->thunk.add_pointer_bounds_args)
564 node->thunk.thunk_p = false;
565 node->thunk.add_pointer_bounds_args = false;
569 else
570 gcc_assert (node->clone_of || !node->has_gimple_body_p ()
571 || in_lto_p || DECL_RESULT (node->decl));
574 /* Inline clones might be kept around so their materializing allows further
575 cloning. If the function the clone is inlined into is removed, we need
576 to turn it into normal cone. */
577 FOR_EACH_FUNCTION (node)
579 if (node->global.inlined_to
580 && !node->callers)
582 gcc_assert (node->clones);
583 node->global.inlined_to = NULL;
584 update_inlined_to_pointer (node, node);
586 node->aux = NULL;
589 /* Remove unreachable variables. */
590 if (file)
591 fprintf (file, "\nReclaiming variables:");
592 for (vnode = first_variable (); vnode; vnode = vnext)
594 vnext = next_variable (vnode);
595 if (!vnode->aux
596 /* For can_refer_decl_in_current_unit_p we want to track for
597 all external variables if they are defined in other partition
598 or not. */
599 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
601 if (file)
602 fprintf (file, " %s/%i", vnode->name (), vnode->order);
603 vnode->remove ();
604 changed = true;
606 else if (!reachable.contains (vnode))
608 tree init;
609 if (vnode->definition)
611 if (file)
612 fprintf (file, " %s", vnode->name ());
613 changed = true;
615 /* Keep body if it may be useful for constant folding. */
616 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node
617 && !POINTER_BOUNDS_P (vnode->decl))
618 vnode->remove_initializer ();
619 else
620 DECL_INITIAL (vnode->decl) = init;
621 vnode->body_removed = true;
622 vnode->definition = false;
623 vnode->analyzed = false;
624 vnode->aux = NULL;
626 vnode->remove_from_same_comdat_group ();
628 vnode->remove_all_references ();
630 else
631 vnode->aux = NULL;
634 /* Now update address_taken flags and try to promote functions to be local. */
635 if (file)
636 fprintf (file, "\nClearing address taken flags:");
637 FOR_EACH_DEFINED_FUNCTION (node)
638 if (node->address_taken
639 && !node->used_from_other_partition)
641 if (!node->call_for_symbol_thunks_and_aliases
642 (has_addr_references_p, NULL, true)
643 && (!node->instrumentation_clone
644 || !node->instrumented_version
645 || !node->instrumented_version->address_taken))
647 if (file)
648 fprintf (file, " %s", node->name ());
649 node->address_taken = false;
650 changed = true;
651 if (node->local_p ())
653 node->local.local = true;
654 if (file)
655 fprintf (file, " (local)");
659 if (file)
660 fprintf (file, "\n");
662 #ifdef ENABLE_CHECKING
663 symtab_node::verify_symtab_nodes ();
664 #endif
666 /* If we removed something, perhaps profile could be improved. */
667 if (changed && optimize && inline_edge_summary_vec.exists ())
668 FOR_EACH_DEFINED_FUNCTION (node)
669 ipa_propagate_frequency (node);
671 timevar_pop (TV_IPA_UNREACHABLE);
672 return changed;
675 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
676 as needed, also clear EXPLICIT_REFS if the references to given variable
677 do not need to be explicit. */
679 void
680 process_references (varpool_node *vnode,
681 bool *written, bool *address_taken,
682 bool *read, bool *explicit_refs)
684 int i;
685 struct ipa_ref *ref;
687 if (!vnode->all_refs_explicit_p ()
688 || TREE_THIS_VOLATILE (vnode->decl))
689 *explicit_refs = false;
691 for (i = 0; vnode->iterate_referring (i, ref)
692 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
693 switch (ref->use)
695 case IPA_REF_ADDR:
696 *address_taken = true;
697 break;
698 case IPA_REF_LOAD:
699 *read = true;
700 break;
701 case IPA_REF_STORE:
702 *written = true;
703 break;
704 case IPA_REF_ALIAS:
705 process_references (dyn_cast<varpool_node *> (ref->referring), written,
706 address_taken, read, explicit_refs);
707 break;
708 case IPA_REF_CHKP:
709 gcc_unreachable ();
713 /* Set TREE_READONLY bit. */
715 bool
716 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
718 TREE_READONLY (vnode->decl) = true;
719 return false;
722 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
724 bool
725 set_writeonly_bit (varpool_node *vnode, void *data)
727 vnode->writeonly = true;
728 if (optimize)
730 DECL_INITIAL (vnode->decl) = NULL;
731 if (!vnode->alias)
733 if (vnode->num_references ())
734 *(bool *)data = true;
735 vnode->remove_all_references ();
738 return false;
741 /* Clear addressale bit of VNODE. */
743 bool
744 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
746 vnode->address_taken = false;
747 TREE_ADDRESSABLE (vnode->decl) = 0;
748 return false;
751 /* Discover variables that have no longer address taken or that are read only
752 and update their flags.
754 Return true when unreachable symbol removan should be done.
756 FIXME: This can not be done in between gimplify and omp_expand since
757 readonly flag plays role on what is shared and what is not. Currently we do
758 this transformation as part of whole program visibility and re-do at
759 ipa-reference pass (to take into account clonning), but it would
760 make sense to do it before early optimizations. */
762 bool
763 ipa_discover_readonly_nonaddressable_vars (void)
765 bool remove_p = false;
766 varpool_node *vnode;
767 if (dump_file)
768 fprintf (dump_file, "Clearing variable flags:");
769 FOR_EACH_VARIABLE (vnode)
770 if (!vnode->alias
771 && (TREE_ADDRESSABLE (vnode->decl)
772 || !vnode->writeonly
773 || !TREE_READONLY (vnode->decl)))
775 bool written = false;
776 bool address_taken = false;
777 bool read = false;
778 bool explicit_refs = true;
780 process_references (vnode, &written, &address_taken, &read,
781 &explicit_refs);
782 if (!explicit_refs)
783 continue;
784 if (!address_taken)
786 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
787 fprintf (dump_file, " %s (non-addressable)", vnode->name ());
788 vnode->call_for_node_and_aliases (clear_addressable_bit, NULL,
789 true);
791 if (!address_taken && !written
792 /* Making variable in explicit section readonly can cause section
793 type conflict.
794 See e.g. gcc.c-torture/compile/pr23237.c */
795 && vnode->get_section () == NULL)
797 if (!TREE_READONLY (vnode->decl) && dump_file)
798 fprintf (dump_file, " %s (read-only)", vnode->name ());
799 vnode->call_for_node_and_aliases (set_readonly_bit, NULL, true);
801 if (!vnode->writeonly && !read && !address_taken && written)
803 if (dump_file)
804 fprintf (dump_file, " %s (write-only)", vnode->name ());
805 vnode->call_for_node_and_aliases (set_writeonly_bit, &remove_p,
806 true);
809 if (dump_file)
810 fprintf (dump_file, "\n");
811 return remove_p;
814 /* Free inline summary. */
816 namespace {
818 const pass_data pass_data_ipa_free_inline_summary =
820 SIMPLE_IPA_PASS, /* type */
821 "free-inline-summary", /* name */
822 OPTGROUP_NONE, /* optinfo_flags */
823 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
824 0, /* properties_required */
825 0, /* properties_provided */
826 0, /* properties_destroyed */
827 0, /* todo_flags_start */
828 /* Early optimizations may make function unreachable. We can not
829 remove unreachable functions as part of the ealry opts pass because
830 TODOs are run before subpasses. Do it here. */
831 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
834 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
836 public:
837 pass_ipa_free_inline_summary (gcc::context *ctxt)
838 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
841 /* opt_pass methods: */
842 virtual unsigned int execute (function *)
844 inline_free_summary ();
845 return 0;
848 }; // class pass_ipa_free_inline_summary
850 } // anon namespace
852 simple_ipa_opt_pass *
853 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
855 return new pass_ipa_free_inline_summary (ctxt);
858 /* Generate and emit a static constructor or destructor. WHICH must
859 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
860 (for chp static vars constructor) or 'B' (for chkp static bounds
861 constructor). BODY is a STATEMENT_LIST containing GENERIC
862 statements. PRIORITY is the initialization priority for this
863 constructor or destructor.
865 FINAL specify whether the externally visible name for collect2 should
866 be produced. */
868 static void
869 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
871 static int counter = 0;
872 char which_buf[16];
873 tree decl, name, resdecl;
875 /* The priority is encoded in the constructor or destructor name.
876 collect2 will sort the names and arrange that they are called at
877 program startup. */
878 if (final)
879 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
880 else
881 /* Proudce sane name but one not recognizable by collect2, just for the
882 case we fail to inline the function. */
883 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
884 name = get_file_function_name (which_buf);
886 decl = build_decl (input_location, FUNCTION_DECL, name,
887 build_function_type_list (void_type_node, NULL_TREE));
888 current_function_decl = decl;
890 resdecl = build_decl (input_location,
891 RESULT_DECL, NULL_TREE, void_type_node);
892 DECL_ARTIFICIAL (resdecl) = 1;
893 DECL_RESULT (decl) = resdecl;
894 DECL_CONTEXT (resdecl) = decl;
896 allocate_struct_function (decl, false);
898 TREE_STATIC (decl) = 1;
899 TREE_USED (decl) = 1;
900 DECL_ARTIFICIAL (decl) = 1;
901 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
902 DECL_SAVED_TREE (decl) = body;
903 if (!targetm.have_ctors_dtors && final)
905 TREE_PUBLIC (decl) = 1;
906 DECL_PRESERVE_P (decl) = 1;
908 DECL_UNINLINABLE (decl) = 1;
910 DECL_INITIAL (decl) = make_node (BLOCK);
911 TREE_USED (DECL_INITIAL (decl)) = 1;
913 DECL_SOURCE_LOCATION (decl) = input_location;
914 cfun->function_end_locus = input_location;
916 switch (which)
918 case 'I':
919 DECL_STATIC_CONSTRUCTOR (decl) = 1;
920 decl_init_priority_insert (decl, priority);
921 break;
922 case 'P':
923 DECL_STATIC_CONSTRUCTOR (decl) = 1;
924 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("chkp ctor"),
925 NULL,
926 NULL_TREE);
927 decl_init_priority_insert (decl, priority);
928 break;
929 case 'B':
930 DECL_STATIC_CONSTRUCTOR (decl) = 1;
931 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("bnd_legacy"),
932 NULL,
933 NULL_TREE);
934 decl_init_priority_insert (decl, priority);
935 break;
936 case 'D':
937 DECL_STATIC_DESTRUCTOR (decl) = 1;
938 decl_fini_priority_insert (decl, priority);
939 break;
940 default:
941 gcc_unreachable ();
944 gimplify_function_tree (decl);
946 cgraph_node::add_new_function (decl, false);
948 set_cfun (NULL);
949 current_function_decl = NULL;
952 /* Generate and emit a static constructor or destructor. WHICH must
953 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
954 (for chkp static vars constructor) or 'B' (for chkp static bounds
955 constructor). BODY is a STATEMENT_LIST containing GENERIC
956 statements. PRIORITY is the initialization priority for this
957 constructor or destructor. */
959 void
960 cgraph_build_static_cdtor (char which, tree body, int priority)
962 cgraph_build_static_cdtor_1 (which, body, priority, false);
965 /* A vector of FUNCTION_DECLs declared as static constructors. */
966 static vec<tree> static_ctors;
967 /* A vector of FUNCTION_DECLs declared as static destructors. */
968 static vec<tree> static_dtors;
970 /* When target does not have ctors and dtors, we call all constructor
971 and destructor by special initialization/destruction function
972 recognized by collect2.
974 When we are going to build this function, collect all constructors and
975 destructors and turn them into normal functions. */
977 static void
978 record_cdtor_fn (struct cgraph_node *node)
980 if (DECL_STATIC_CONSTRUCTOR (node->decl))
981 static_ctors.safe_push (node->decl);
982 if (DECL_STATIC_DESTRUCTOR (node->decl))
983 static_dtors.safe_push (node->decl);
984 node = cgraph_node::get (node->decl);
985 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
988 /* Define global constructors/destructor functions for the CDTORS, of
989 which they are LEN. The CDTORS are sorted by initialization
990 priority. If CTOR_P is true, these are constructors; otherwise,
991 they are destructors. */
993 static void
994 build_cdtor (bool ctor_p, vec<tree> cdtors)
996 size_t i,j;
997 size_t len = cdtors.length ();
999 i = 0;
1000 while (i < len)
1002 tree body;
1003 tree fn;
1004 priority_type priority;
1006 priority = 0;
1007 body = NULL_TREE;
1008 j = i;
1011 priority_type p;
1012 fn = cdtors[j];
1013 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1014 if (j == i)
1015 priority = p;
1016 else if (p != priority)
1017 break;
1018 j++;
1020 while (j < len);
1022 /* When there is only one cdtor and target supports them, do nothing. */
1023 if (j == i + 1
1024 && targetm.have_ctors_dtors)
1026 i++;
1027 continue;
1029 /* Find the next batch of constructors/destructors with the same
1030 initialization priority. */
1031 for (;i < j; i++)
1033 tree call;
1034 fn = cdtors[i];
1035 call = build_call_expr (fn, 0);
1036 if (ctor_p)
1037 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1038 else
1039 DECL_STATIC_DESTRUCTOR (fn) = 0;
1040 /* We do not want to optimize away pure/const calls here.
1041 When optimizing, these should be already removed, when not
1042 optimizing, we want user to be able to breakpoint in them. */
1043 TREE_SIDE_EFFECTS (call) = 1;
1044 append_to_statement_list (call, &body);
1046 gcc_assert (body != NULL_TREE);
1047 /* Generate a function to call all the function of like
1048 priority. */
1049 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1053 /* Comparison function for qsort. P1 and P2 are actually of type
1054 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1055 used to determine the sort order. */
1057 static int
1058 compare_ctor (const void *p1, const void *p2)
1060 tree f1;
1061 tree f2;
1062 int priority1;
1063 int priority2;
1065 f1 = *(const tree *)p1;
1066 f2 = *(const tree *)p2;
1067 priority1 = DECL_INIT_PRIORITY (f1);
1068 priority2 = DECL_INIT_PRIORITY (f2);
1070 if (priority1 < priority2)
1071 return -1;
1072 else if (priority1 > priority2)
1073 return 1;
1074 else
1075 /* Ensure a stable sort. Constructors are executed in backwarding
1076 order to make LTO initialize braries first. */
1077 return DECL_UID (f2) - DECL_UID (f1);
1080 /* Comparison function for qsort. P1 and P2 are actually of type
1081 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1082 used to determine the sort order. */
1084 static int
1085 compare_dtor (const void *p1, const void *p2)
1087 tree f1;
1088 tree f2;
1089 int priority1;
1090 int priority2;
1092 f1 = *(const tree *)p1;
1093 f2 = *(const tree *)p2;
1094 priority1 = DECL_FINI_PRIORITY (f1);
1095 priority2 = DECL_FINI_PRIORITY (f2);
1097 if (priority1 < priority2)
1098 return -1;
1099 else if (priority1 > priority2)
1100 return 1;
1101 else
1102 /* Ensure a stable sort. */
1103 return DECL_UID (f1) - DECL_UID (f2);
1106 /* Generate functions to call static constructors and destructors
1107 for targets that do not support .ctors/.dtors sections. These
1108 functions have magic names which are detected by collect2. */
1110 static void
1111 build_cdtor_fns (void)
1113 if (!static_ctors.is_empty ())
1115 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1116 static_ctors.qsort (compare_ctor);
1117 build_cdtor (/*ctor_p=*/true, static_ctors);
1120 if (!static_dtors.is_empty ())
1122 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1123 static_dtors.qsort (compare_dtor);
1124 build_cdtor (/*ctor_p=*/false, static_dtors);
1128 /* Look for constructors and destructors and produce function calling them.
1129 This is needed for targets not supporting ctors or dtors, but we perform the
1130 transformation also at linktime to merge possibly numerous
1131 constructors/destructors into single function to improve code locality and
1132 reduce size. */
1134 static unsigned int
1135 ipa_cdtor_merge (void)
1137 struct cgraph_node *node;
1138 FOR_EACH_DEFINED_FUNCTION (node)
1139 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1140 || DECL_STATIC_DESTRUCTOR (node->decl))
1141 record_cdtor_fn (node);
1142 build_cdtor_fns ();
1143 static_ctors.release ();
1144 static_dtors.release ();
1145 return 0;
1148 namespace {
1150 const pass_data pass_data_ipa_cdtor_merge =
1152 IPA_PASS, /* type */
1153 "cdtor", /* name */
1154 OPTGROUP_NONE, /* optinfo_flags */
1155 TV_CGRAPHOPT, /* tv_id */
1156 0, /* properties_required */
1157 0, /* properties_provided */
1158 0, /* properties_destroyed */
1159 0, /* todo_flags_start */
1160 0, /* todo_flags_finish */
1163 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1165 public:
1166 pass_ipa_cdtor_merge (gcc::context *ctxt)
1167 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1168 NULL, /* generate_summary */
1169 NULL, /* write_summary */
1170 NULL, /* read_summary */
1171 NULL, /* write_optimization_summary */
1172 NULL, /* read_optimization_summary */
1173 NULL, /* stmt_fixup */
1174 0, /* function_transform_todo_flags_start */
1175 NULL, /* function_transform */
1176 NULL) /* variable_transform */
1179 /* opt_pass methods: */
1180 virtual bool gate (function *);
1181 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1183 }; // class pass_ipa_cdtor_merge
1185 bool
1186 pass_ipa_cdtor_merge::gate (function *)
1188 /* Perform the pass when we have no ctors/dtors support
1189 or at LTO time to merge multiple constructors into single
1190 function. */
1191 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1194 } // anon namespace
1196 ipa_opt_pass_d *
1197 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1199 return new pass_ipa_cdtor_merge (ctxt);
1202 /* Invalid pointer representing BOTTOM for single user dataflow. */
1203 #define BOTTOM ((cgraph_node *)(size_t) 2)
1205 /* Meet operation for single user dataflow.
1206 Here we want to associate variables with sigle function that may access it.
1208 FUNCTION is current single user of a variable, VAR is variable that uses it.
1209 Latttice is stored in SINGLE_USER_MAP.
1211 We represent:
1212 - TOP by no entry in SIGNLE_USER_MAP
1213 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1214 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1216 cgraph_node *
1217 meet (cgraph_node *function, varpool_node *var,
1218 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1220 struct cgraph_node *user, **f;
1222 if (var->aux == BOTTOM)
1223 return BOTTOM;
1225 f = single_user_map.get (var);
1226 if (!f)
1227 return function;
1228 user = *f;
1229 if (!function)
1230 return user;
1231 else if (function != user)
1232 return BOTTOM;
1233 else
1234 return function;
1237 /* Propagation step of single-use dataflow.
1239 Check all uses of VNODE and see if they are used by single function FUNCTION.
1240 SINGLE_USER_MAP represents the dataflow lattice. */
1242 cgraph_node *
1243 propagate_single_user (varpool_node *vnode, cgraph_node *function,
1244 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1246 int i;
1247 struct ipa_ref *ref;
1249 gcc_assert (!vnode->externally_visible);
1251 /* If node is an alias, first meet with its target. */
1252 if (vnode->alias)
1253 function = meet (function, vnode->get_alias_target (), single_user_map);
1255 /* Check all users and see if they correspond to a single function. */
1256 for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
1258 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
1259 if (cnode)
1261 if (cnode->global.inlined_to)
1262 cnode = cnode->global.inlined_to;
1263 if (!function)
1264 function = cnode;
1265 else if (function != cnode)
1266 function = BOTTOM;
1268 else
1269 function = meet (function, dyn_cast <varpool_node *> (ref->referring),
1270 single_user_map);
1272 return function;
1275 /* Pass setting used_by_single_function flag.
1276 This flag is set on variable when there is only one function that may
1277 possibly referr to it. */
1279 static unsigned int
1280 ipa_single_use (void)
1282 varpool_node *first = (varpool_node *) (void *) 1;
1283 varpool_node *var;
1284 hash_map<varpool_node *, cgraph_node *> single_user_map;
1286 FOR_EACH_DEFINED_VARIABLE (var)
1287 if (!var->all_refs_explicit_p ())
1288 var->aux = BOTTOM;
1289 else
1291 /* Enqueue symbol for dataflow. */
1292 var->aux = first;
1293 first = var;
1296 /* The actual dataflow. */
1298 while (first != (void *) 1)
1300 cgraph_node *user, *orig_user, **f;
1302 var = first;
1303 first = (varpool_node *)first->aux;
1305 f = single_user_map.get (var);
1306 if (f)
1307 orig_user = *f;
1308 else
1309 orig_user = NULL;
1310 user = propagate_single_user (var, orig_user, single_user_map);
1312 gcc_checking_assert (var->aux != BOTTOM);
1314 /* If user differs, enqueue all references. */
1315 if (user != orig_user)
1317 unsigned int i;
1318 ipa_ref *ref;
1320 single_user_map.put (var, user);
1322 /* Enqueue all aliases for re-processing. */
1323 for (i = 0; var->iterate_referring (i, ref); i++)
1324 if (ref->use == IPA_REF_ALIAS
1325 && !ref->referring->aux)
1327 ref->referring->aux = first;
1328 first = dyn_cast <varpool_node *> (ref->referring);
1330 /* Enqueue all users for re-processing. */
1331 for (i = 0; var->iterate_reference (i, ref); i++)
1332 if (!ref->referred->aux
1333 && ref->referred->definition
1334 && is_a <varpool_node *> (ref->referred))
1336 ref->referred->aux = first;
1337 first = dyn_cast <varpool_node *> (ref->referred);
1340 /* If user is BOTTOM, just punt on this var. */
1341 if (user == BOTTOM)
1342 var->aux = BOTTOM;
1343 else
1344 var->aux = NULL;
1346 else
1347 var->aux = NULL;
1350 FOR_EACH_DEFINED_VARIABLE (var)
1352 if (var->aux != BOTTOM)
1354 #ifdef ENABLE_CHECKING
1355 /* Not having the single user known means that the VAR is
1356 unreachable. Either someone forgot to remove unreachable
1357 variables or the reachability here is wrong. */
1359 gcc_assert (single_user_map.get (var));
1360 #endif
1361 if (dump_file)
1363 fprintf (dump_file, "Variable %s/%i is used by single function\n",
1364 var->name (), var->order);
1366 var->used_by_single_function = true;
1368 var->aux = NULL;
1370 return 0;
1373 namespace {
1375 const pass_data pass_data_ipa_single_use =
1377 IPA_PASS, /* type */
1378 "single-use", /* name */
1379 OPTGROUP_NONE, /* optinfo_flags */
1380 TV_CGRAPHOPT, /* tv_id */
1381 0, /* properties_required */
1382 0, /* properties_provided */
1383 0, /* properties_destroyed */
1384 0, /* todo_flags_start */
1385 0, /* todo_flags_finish */
1388 class pass_ipa_single_use : public ipa_opt_pass_d
1390 public:
1391 pass_ipa_single_use (gcc::context *ctxt)
1392 : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
1393 NULL, /* generate_summary */
1394 NULL, /* write_summary */
1395 NULL, /* read_summary */
1396 NULL, /* write_optimization_summary */
1397 NULL, /* read_optimization_summary */
1398 NULL, /* stmt_fixup */
1399 0, /* function_transform_todo_flags_start */
1400 NULL, /* function_transform */
1401 NULL) /* variable_transform */
1404 /* opt_pass methods: */
1405 virtual bool gate (function *);
1406 virtual unsigned int execute (function *) { return ipa_single_use (); }
1408 }; // class pass_ipa_single_use
1410 bool
1411 pass_ipa_single_use::gate (function *)
1413 return optimize;
1416 } // anon namespace
1418 ipa_opt_pass_d *
1419 make_pass_ipa_single_use (gcc::context *ctxt)
1421 return new pass_ipa_single_use (ctxt);