Fix missing ChangeLog entry for Graphite head files fix.
[official-gcc.git] / gcc / ipa.c
blob77f2dd1451dd56e5562ae11dbba609b140ef30f5
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 "backend.h"
24 #include "target.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "alloc-pool.h"
28 #include "tree-pass.h"
29 #include "stringpool.h"
30 #include "cgraph.h"
31 #include "gimplify.h"
32 #include "tree-iterator.h"
33 #include "ipa-utils.h"
34 #include "symbol-summary.h"
35 #include "ipa-prop.h"
36 #include "ipa-inline.h"
37 #include "dbgcnt.h"
40 /* Return true when NODE has ADDR reference. */
42 static bool
43 has_addr_references_p (struct cgraph_node *node,
44 void *data ATTRIBUTE_UNUSED)
46 int i;
47 struct ipa_ref *ref = NULL;
49 for (i = 0; node->iterate_referring (i, ref); i++)
50 if (ref->use == IPA_REF_ADDR)
51 return true;
52 return false;
55 /* Look for all functions inlined to NODE and update their inlined_to pointers
56 to INLINED_TO. */
58 static void
59 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
61 struct cgraph_edge *e;
62 for (e = node->callees; e; e = e->next_callee)
63 if (e->callee->global.inlined_to)
65 e->callee->global.inlined_to = inlined_to;
66 update_inlined_to_pointer (e->callee, inlined_to);
70 /* Add symtab NODE to queue starting at FIRST.
72 The queue is linked via AUX pointers and terminated by pointer to 1.
73 We enqueue nodes at two occasions: when we find them reachable or when we find
74 their bodies needed for further clonning. In the second case we mark them
75 by pointer to 2 after processing so they are re-queue when they become
76 reachable. */
78 static void
79 enqueue_node (symtab_node *node, symtab_node **first,
80 hash_set<symtab_node *> *reachable)
82 /* Node is still in queue; do nothing. */
83 if (node->aux && node->aux != (void *) 2)
84 return;
85 /* Node was already processed as unreachable, re-enqueue
86 only if it became reachable now. */
87 if (node->aux == (void *)2 && !reachable->contains (node))
88 return;
89 node->aux = *first;
90 *first = node;
93 /* Process references. */
95 static void
96 process_references (symtab_node *snode,
97 symtab_node **first,
98 bool before_inlining_p,
99 hash_set<symtab_node *> *reachable)
101 int i;
102 struct ipa_ref *ref = NULL;
103 for (i = 0; snode->iterate_reference (i, ref); i++)
105 symtab_node *node = ref->referred;
106 symtab_node *body = node->ultimate_alias_target ();
108 if (node->definition && !node->in_other_partition
109 && ((!DECL_EXTERNAL (node->decl) || node->alias)
110 || (((before_inlining_p
111 && ((TREE_CODE (node->decl) != FUNCTION_DECL
112 && optimize)
113 || (TREE_CODE (node->decl) == FUNCTION_DECL
114 && opt_for_fn (body->decl, optimize))
115 || (symtab->state < IPA_SSA
116 && lookup_attribute
117 ("always_inline",
118 DECL_ATTRIBUTES (body->decl))))))
119 /* We use variable constructors during late compilation for
120 constant folding. Keep references alive so partitioning
121 knows about potential references. */
122 || (TREE_CODE (node->decl) == VAR_DECL
123 && flag_wpa
124 && ctor_for_folding (node->decl)
125 != error_mark_node))))
127 /* Be sure that we will not optimize out alias target
128 body. */
129 if (DECL_EXTERNAL (node->decl)
130 && node->alias
131 && before_inlining_p)
132 reachable->add (body);
133 reachable->add (node);
135 enqueue_node (node, first, reachable);
139 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
140 all its potential targets as reachable to permit later inlining if
141 devirtualization happens. After inlining still keep their declarations
142 around, so we can devirtualize to a direct call.
144 Also try to make trivial devirutalization when no or only one target is
145 possible. */
147 static void
148 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
149 struct cgraph_edge *edge,
150 symtab_node **first,
151 hash_set<symtab_node *> *reachable,
152 bool before_inlining_p)
154 unsigned int i;
155 void *cache_token;
156 bool final;
157 vec <cgraph_node *>targets
158 = possible_polymorphic_call_targets
159 (edge, &final, &cache_token);
161 if (!reachable_call_targets->add (cache_token))
163 for (i = 0; i < targets.length (); i++)
165 struct cgraph_node *n = targets[i];
167 /* Do not bother to mark virtual methods in anonymous namespace;
168 either we will find use of virtual table defining it, or it is
169 unused. */
170 if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
171 && type_in_anonymous_namespace_p
172 (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl))))
173 continue;
175 symtab_node *body = n->function_symbol ();
177 /* Prior inlining, keep alive bodies of possible targets for
178 devirtualization. */
179 if (n->definition
180 && (before_inlining_p
181 && opt_for_fn (body->decl, optimize)
182 && opt_for_fn (body->decl, flag_devirtualize)))
184 /* Be sure that we will not optimize out alias target
185 body. */
186 if (DECL_EXTERNAL (n->decl)
187 && n->alias
188 && before_inlining_p)
189 reachable->add (body);
190 reachable->add (n);
192 /* Even after inlining we want to keep the possible targets in the
193 boundary, so late passes can still produce direct call even if
194 the chance for inlining is lost. */
195 enqueue_node (n, first, reachable);
199 /* Very trivial devirtualization; when the type is
200 final or anonymous (so we know all its derivation)
201 and there is only one possible virtual call target,
202 make the edge direct. */
203 if (final)
205 if (targets.length () <= 1 && dbg_cnt (devirt))
207 cgraph_node *target, *node = edge->caller;
208 if (targets.length () == 1)
209 target = targets[0];
210 else
211 target = cgraph_node::get_create
212 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
214 if (dump_enabled_p ())
216 location_t locus;
217 if (edge->call_stmt)
218 locus = gimple_location (edge->call_stmt);
219 else
220 locus = UNKNOWN_LOCATION;
221 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
222 "devirtualizing call in %s/%i to %s/%i\n",
223 edge->caller->name (), edge->caller->order,
224 target->name (),
225 target->order);
227 edge = edge->make_direct (target);
228 if (inline_summaries)
229 inline_update_overall_summary (node);
230 else if (edge->call_stmt)
232 edge->redirect_call_stmt_to_callee ();
234 /* Call to __builtin_unreachable shouldn't be instrumented. */
235 if (!targets.length ())
236 gimple_call_set_with_bounds (edge->call_stmt, false);
242 /* Perform reachability analysis and reclaim all unreachable nodes.
244 The algorithm is basically mark&sweep but with some extra refinements:
246 - reachable extern inline functions needs special handling; the bodies needs
247 to stay in memory until inlining in hope that they will be inlined.
248 After inlining we release their bodies and turn them into unanalyzed
249 nodes even when they are reachable.
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 (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;
305 bool before_inlining_p = symtab->state < (!optimize ? IPA_SSA
306 : IPA_SSA_AFTER_INLINING);
308 timevar_push (TV_IPA_UNREACHABLE);
309 build_type_inheritance_graph ();
310 if (file)
311 fprintf (file, "\nReclaiming functions:");
312 if (flag_checking)
314 FOR_EACH_FUNCTION (node)
315 gcc_assert (!node->aux);
316 FOR_EACH_VARIABLE (vnode)
317 gcc_assert (!vnode->aux);
319 /* Mark functions whose bodies are obviously needed.
320 This is mostly when they can be referenced externally. Inline clones
321 are special since their declarations are shared with master clone and thus
322 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
323 FOR_EACH_FUNCTION (node)
325 node->used_as_abstract_origin = false;
326 if (node->definition
327 && !node->global.inlined_to
328 && !node->in_other_partition
329 && !node->can_remove_if_no_direct_calls_and_refs_p ())
331 gcc_assert (!node->global.inlined_to);
332 reachable.add (node);
333 enqueue_node (node, &first, &reachable);
335 else
336 gcc_assert (!node->aux);
339 /* Mark variables that are obviously needed. */
340 FOR_EACH_DEFINED_VARIABLE (vnode)
341 if (!vnode->can_remove_if_no_refs_p()
342 && !vnode->in_other_partition)
344 reachable.add (vnode);
345 enqueue_node (vnode, &first, &reachable);
348 /* Perform reachability analysis. */
349 while (first != (symtab_node *) (void *) 1)
351 bool in_boundary_p = !reachable.contains (first);
352 symtab_node *node = first;
354 first = (symtab_node *)first->aux;
356 /* If we are processing symbol in boundary, mark its AUX pointer for
357 possible later re-processing in enqueue_node. */
358 if (in_boundary_p)
360 node->aux = (void *)2;
361 if (node->alias && node->analyzed)
362 enqueue_node (node->get_alias_target (), &first, &reachable);
364 else
366 if (TREE_CODE (node->decl) == FUNCTION_DECL
367 && DECL_ABSTRACT_ORIGIN (node->decl))
369 struct cgraph_node *origin_node
370 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
371 if (origin_node && !origin_node->used_as_abstract_origin)
373 origin_node->used_as_abstract_origin = true;
374 gcc_assert (!origin_node->prev_sibling_clone);
375 gcc_assert (!origin_node->next_sibling_clone);
376 for (cgraph_node *n = origin_node->clones; n;
377 n = n->next_sibling_clone)
378 if (n->decl == DECL_ABSTRACT_ORIGIN (node->decl))
379 n->used_as_abstract_origin = true;
382 /* If any symbol in a comdat group is reachable, force
383 all externally visible symbols in the same comdat
384 group to be reachable as well. Comdat-local symbols
385 can be discarded if all uses were inlined. */
386 if (node->same_comdat_group)
388 symtab_node *next;
389 for (next = node->same_comdat_group;
390 next != node;
391 next = next->same_comdat_group)
392 if (!next->comdat_local_p ()
393 && !reachable.add (next))
394 enqueue_node (next, &first, &reachable);
396 /* Mark references as reachable. */
397 process_references (node, &first, before_inlining_p, &reachable);
400 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
402 /* Mark the callees reachable unless they are direct calls to extern
403 inline functions we decided to not inline. */
404 if (!in_boundary_p)
406 struct cgraph_edge *e;
407 /* Keep alive possible targets for devirtualization. */
408 if (opt_for_fn (cnode->decl, optimize)
409 && opt_for_fn (cnode->decl, flag_devirtualize))
411 struct cgraph_edge *next;
412 for (e = cnode->indirect_calls; e; e = next)
414 next = e->next_callee;
415 if (e->indirect_info->polymorphic)
416 walk_polymorphic_call_targets (&reachable_call_targets,
417 e, &first, &reachable,
418 before_inlining_p);
421 for (e = cnode->callees; e; e = e->next_callee)
423 symtab_node *body = e->callee->function_symbol ();
424 if (e->callee->definition
425 && !e->callee->in_other_partition
426 && (!e->inline_failed
427 || !DECL_EXTERNAL (e->callee->decl)
428 || e->callee->alias
429 || (before_inlining_p
430 && (opt_for_fn (body->decl, optimize)
431 || (symtab->state < IPA_SSA
432 && lookup_attribute
433 ("always_inline",
434 DECL_ATTRIBUTES (body->decl)))))))
436 /* Be sure that we will not optimize out alias target
437 body. */
438 if (DECL_EXTERNAL (e->callee->decl)
439 && e->callee->alias
440 && before_inlining_p)
441 reachable.add (body);
442 reachable.add (e->callee);
444 enqueue_node (e->callee, &first, &reachable);
447 /* When inline clone exists, mark body to be preserved so when removing
448 offline copy of the function we don't kill it. */
449 if (cnode->global.inlined_to)
450 body_needed_for_clonning.add (cnode->decl);
452 /* For instrumentation clones we always need original
453 function node for proper LTO privatization. */
454 if (cnode->instrumentation_clone
455 && cnode->definition)
457 gcc_assert (cnode->instrumented_version || in_lto_p);
458 if (cnode->instrumented_version)
460 enqueue_node (cnode->instrumented_version, &first,
461 &reachable);
462 reachable.add (cnode->instrumented_version);
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 else if (cnode->thunk.thunk_p)
481 enqueue_node (cnode->callees->callee, &first, &reachable);
483 /* If any reachable function has simd clones, mark them as
484 reachable as well. */
485 if (cnode->simd_clones)
487 cgraph_node *next;
488 for (next = cnode->simd_clones;
489 next;
490 next = next->simdclone->next_clone)
491 if (in_boundary_p
492 || !reachable.add (next))
493 enqueue_node (next, &first, &reachable);
496 /* When we see constructor of external variable, keep referred nodes in the
497 boundary. This will also hold initializers of the external vars NODE
498 refers to. */
499 varpool_node *vnode = dyn_cast <varpool_node *> (node);
500 if (vnode
501 && DECL_EXTERNAL (node->decl)
502 && !vnode->alias
503 && in_boundary_p)
505 struct ipa_ref *ref = NULL;
506 for (int i = 0; node->iterate_reference (i, ref); i++)
507 enqueue_node (ref->referred, &first, &reachable);
511 /* Remove unreachable functions. */
512 for (node = first_function (); node; node = next)
514 next = next_function (node);
516 /* If node is not needed at all, remove it. */
517 if (!node->aux)
519 if (file)
520 fprintf (file, " %s/%i", node->name (), node->order);
521 node->remove ();
522 changed = true;
524 /* If node is unreachable, remove its body. */
525 else if (!reachable.contains (node))
527 /* We keep definitions of thunks and aliases in the boundary so
528 we can walk to the ultimate alias targets and function symbols
529 reliably. */
530 if (node->alias || node->thunk.thunk_p)
532 else if (!body_needed_for_clonning.contains (node->decl)
533 && !node->alias && !node->thunk.thunk_p)
534 node->release_body ();
535 else if (!node->clone_of)
536 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
537 if (node->definition && !node->alias && !node->thunk.thunk_p)
539 if (file)
540 fprintf (file, " %s/%i", node->name (), node->order);
541 node->body_removed = true;
542 node->analyzed = false;
543 node->definition = false;
544 node->cpp_implicit_alias = false;
545 node->alias = false;
546 node->thunk.thunk_p = false;
547 node->weakref = false;
548 /* After early inlining we drop always_inline attributes on
549 bodies of functions that are still referenced (have their
550 address taken). */
551 DECL_ATTRIBUTES (node->decl)
552 = remove_attribute ("always_inline",
553 DECL_ATTRIBUTES (node->decl));
554 if (!node->in_other_partition)
555 node->local.local = false;
556 node->remove_callees ();
557 node->remove_all_references ();
558 changed = true;
559 if (node->thunk.thunk_p
560 && node->thunk.add_pointer_bounds_args)
562 node->thunk.thunk_p = false;
563 node->thunk.add_pointer_bounds_args = false;
567 else
568 gcc_assert (node->clone_of || !node->has_gimple_body_p ()
569 || in_lto_p || DECL_RESULT (node->decl));
572 /* Inline clones might be kept around so their materializing allows further
573 cloning. If the function the clone is inlined into is removed, we need
574 to turn it into normal cone. */
575 FOR_EACH_FUNCTION (node)
577 if (node->global.inlined_to
578 && !node->callers)
580 gcc_assert (node->clones);
581 node->global.inlined_to = NULL;
582 update_inlined_to_pointer (node, node);
584 node->aux = NULL;
587 /* Remove unreachable variables. */
588 if (file)
589 fprintf (file, "\nReclaiming variables:");
590 for (vnode = first_variable (); vnode; vnode = vnext)
592 vnext = next_variable (vnode);
593 if (!vnode->aux
594 /* For can_refer_decl_in_current_unit_p we want to track for
595 all external variables if they are defined in other partition
596 or not. */
597 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
599 struct ipa_ref *ref = NULL;
601 /* First remove the aliases, so varpool::remove can possibly lookup
602 the constructor and save it for future use. */
603 while (vnode->iterate_direct_aliases (0, ref))
605 if (file)
606 fprintf (file, " %s/%i", ref->referred->name (),
607 ref->referred->order);
608 ref->referring->remove ();
610 if (file)
611 fprintf (file, " %s/%i", vnode->name (), vnode->order);
612 vnext = next_variable (vnode);
613 vnode->remove ();
614 changed = true;
616 else if (!reachable.contains (vnode) && !vnode->alias)
618 tree init;
619 if (vnode->definition)
621 if (file)
622 fprintf (file, " %s", vnode->name ());
623 changed = true;
625 /* Keep body if it may be useful for constant folding. */
626 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node
627 && !POINTER_BOUNDS_P (vnode->decl))
628 vnode->remove_initializer ();
629 else
630 DECL_INITIAL (vnode->decl) = init;
631 vnode->body_removed = true;
632 vnode->definition = false;
633 vnode->analyzed = false;
634 vnode->aux = NULL;
636 vnode->remove_from_same_comdat_group ();
638 vnode->remove_all_references ();
640 else
641 vnode->aux = NULL;
644 /* Now update address_taken flags and try to promote functions to be local. */
645 if (file)
646 fprintf (file, "\nClearing address taken flags:");
647 FOR_EACH_DEFINED_FUNCTION (node)
648 if (node->address_taken
649 && !node->used_from_other_partition)
651 if (!node->call_for_symbol_and_aliases
652 (has_addr_references_p, NULL, true)
653 && (!node->instrumentation_clone
654 || !node->instrumented_version
655 || !node->instrumented_version->address_taken))
657 if (file)
658 fprintf (file, " %s", node->name ());
659 node->address_taken = false;
660 changed = true;
661 if (node->local_p ())
663 node->local.local = true;
664 if (file)
665 fprintf (file, " (local)");
669 if (file)
670 fprintf (file, "\n");
672 symtab_node::checking_verify_symtab_nodes ();
674 /* If we removed something, perhaps profile could be improved. */
675 if (changed && optimize && inline_edge_summary_vec.exists ())
676 FOR_EACH_DEFINED_FUNCTION (node)
677 ipa_propagate_frequency (node);
679 timevar_pop (TV_IPA_UNREACHABLE);
680 return changed;
683 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
684 as needed, also clear EXPLICIT_REFS if the references to given variable
685 do not need to be explicit. */
687 void
688 process_references (varpool_node *vnode,
689 bool *written, bool *address_taken,
690 bool *read, bool *explicit_refs)
692 int i;
693 struct ipa_ref *ref;
695 if (!vnode->all_refs_explicit_p ()
696 || TREE_THIS_VOLATILE (vnode->decl))
697 *explicit_refs = false;
699 for (i = 0; vnode->iterate_referring (i, ref)
700 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
701 switch (ref->use)
703 case IPA_REF_ADDR:
704 *address_taken = true;
705 break;
706 case IPA_REF_LOAD:
707 *read = true;
708 break;
709 case IPA_REF_STORE:
710 *written = true;
711 break;
712 case IPA_REF_ALIAS:
713 process_references (dyn_cast<varpool_node *> (ref->referring), written,
714 address_taken, read, explicit_refs);
715 break;
716 case IPA_REF_CHKP:
717 gcc_unreachable ();
721 /* Set TREE_READONLY bit. */
723 bool
724 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
726 TREE_READONLY (vnode->decl) = true;
727 return false;
730 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
732 bool
733 set_writeonly_bit (varpool_node *vnode, void *data)
735 vnode->writeonly = true;
736 if (optimize)
738 DECL_INITIAL (vnode->decl) = NULL;
739 if (!vnode->alias)
741 if (vnode->num_references ())
742 *(bool *)data = true;
743 vnode->remove_all_references ();
746 return false;
749 /* Clear addressale bit of VNODE. */
751 bool
752 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
754 vnode->address_taken = false;
755 TREE_ADDRESSABLE (vnode->decl) = 0;
756 return false;
759 /* Discover variables that have no longer address taken or that are read only
760 and update their flags.
762 Return true when unreachable symbol removan should be done.
764 FIXME: This can not be done in between gimplify and omp_expand since
765 readonly flag plays role on what is shared and what is not. Currently we do
766 this transformation as part of whole program visibility and re-do at
767 ipa-reference pass (to take into account clonning), but it would
768 make sense to do it before early optimizations. */
770 bool
771 ipa_discover_readonly_nonaddressable_vars (void)
773 bool remove_p = false;
774 varpool_node *vnode;
775 if (dump_file)
776 fprintf (dump_file, "Clearing variable flags:");
777 FOR_EACH_VARIABLE (vnode)
778 if (!vnode->alias
779 && (TREE_ADDRESSABLE (vnode->decl)
780 || !vnode->writeonly
781 || !TREE_READONLY (vnode->decl)))
783 bool written = false;
784 bool address_taken = false;
785 bool read = false;
786 bool explicit_refs = true;
788 process_references (vnode, &written, &address_taken, &read,
789 &explicit_refs);
790 if (!explicit_refs)
791 continue;
792 if (!address_taken)
794 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
795 fprintf (dump_file, " %s (non-addressable)", vnode->name ());
796 vnode->call_for_symbol_and_aliases (clear_addressable_bit, NULL,
797 true);
799 if (!address_taken && !written
800 /* Making variable in explicit section readonly can cause section
801 type conflict.
802 See e.g. gcc.c-torture/compile/pr23237.c */
803 && vnode->get_section () == NULL)
805 if (!TREE_READONLY (vnode->decl) && dump_file)
806 fprintf (dump_file, " %s (read-only)", vnode->name ());
807 vnode->call_for_symbol_and_aliases (set_readonly_bit, NULL, true);
809 if (!vnode->writeonly && !read && !address_taken && written)
811 if (dump_file)
812 fprintf (dump_file, " %s (write-only)", vnode->name ());
813 vnode->call_for_symbol_and_aliases (set_writeonly_bit, &remove_p,
814 true);
817 if (dump_file)
818 fprintf (dump_file, "\n");
819 return remove_p;
822 /* Free inline summary. */
824 namespace {
826 const pass_data pass_data_ipa_free_inline_summary =
828 SIMPLE_IPA_PASS, /* type */
829 "free-inline-summary", /* name */
830 OPTGROUP_NONE, /* optinfo_flags */
831 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
832 0, /* properties_required */
833 0, /* properties_provided */
834 0, /* properties_destroyed */
835 0, /* todo_flags_start */
836 /* Early optimizations may make function unreachable. We can not
837 remove unreachable functions as part of the ealry opts pass because
838 TODOs are run before subpasses. Do it here. */
839 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
842 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
844 public:
845 pass_ipa_free_inline_summary (gcc::context *ctxt)
846 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
849 /* opt_pass methods: */
850 virtual unsigned int execute (function *)
852 inline_free_summary ();
853 return 0;
856 }; // class pass_ipa_free_inline_summary
858 } // anon namespace
860 simple_ipa_opt_pass *
861 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
863 return new pass_ipa_free_inline_summary (ctxt);
866 /* Generate and emit a static constructor or destructor. WHICH must
867 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
868 (for chp static vars constructor) or 'B' (for chkp static bounds
869 constructor). BODY is a STATEMENT_LIST containing GENERIC
870 statements. PRIORITY is the initialization priority for this
871 constructor or destructor.
873 FINAL specify whether the externally visible name for collect2 should
874 be produced. */
876 static void
877 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
879 static int counter = 0;
880 char which_buf[16];
881 tree decl, name, resdecl;
883 /* The priority is encoded in the constructor or destructor name.
884 collect2 will sort the names and arrange that they are called at
885 program startup. */
886 if (final)
887 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
888 else
889 /* Proudce sane name but one not recognizable by collect2, just for the
890 case we fail to inline the function. */
891 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
892 name = get_file_function_name (which_buf);
894 decl = build_decl (input_location, FUNCTION_DECL, name,
895 build_function_type_list (void_type_node, NULL_TREE));
896 current_function_decl = decl;
898 resdecl = build_decl (input_location,
899 RESULT_DECL, NULL_TREE, void_type_node);
900 DECL_ARTIFICIAL (resdecl) = 1;
901 DECL_RESULT (decl) = resdecl;
902 DECL_CONTEXT (resdecl) = decl;
904 allocate_struct_function (decl, false);
906 TREE_STATIC (decl) = 1;
907 TREE_USED (decl) = 1;
908 DECL_ARTIFICIAL (decl) = 1;
909 DECL_IGNORED_P (decl) = 1;
910 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
911 DECL_SAVED_TREE (decl) = body;
912 if (!targetm.have_ctors_dtors && final)
914 TREE_PUBLIC (decl) = 1;
915 DECL_PRESERVE_P (decl) = 1;
917 DECL_UNINLINABLE (decl) = 1;
919 DECL_INITIAL (decl) = make_node (BLOCK);
920 TREE_USED (DECL_INITIAL (decl)) = 1;
922 DECL_SOURCE_LOCATION (decl) = input_location;
923 cfun->function_end_locus = input_location;
925 switch (which)
927 case 'I':
928 DECL_STATIC_CONSTRUCTOR (decl) = 1;
929 decl_init_priority_insert (decl, priority);
930 break;
931 case 'P':
932 DECL_STATIC_CONSTRUCTOR (decl) = 1;
933 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("chkp ctor"),
934 NULL,
935 NULL_TREE);
936 decl_init_priority_insert (decl, priority);
937 break;
938 case 'B':
939 DECL_STATIC_CONSTRUCTOR (decl) = 1;
940 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("bnd_legacy"),
941 NULL,
942 NULL_TREE);
943 decl_init_priority_insert (decl, priority);
944 break;
945 case 'D':
946 DECL_STATIC_DESTRUCTOR (decl) = 1;
947 decl_fini_priority_insert (decl, priority);
948 break;
949 default:
950 gcc_unreachable ();
953 gimplify_function_tree (decl);
955 cgraph_node::add_new_function (decl, false);
957 set_cfun (NULL);
958 current_function_decl = NULL;
961 /* Generate and emit a static constructor or destructor. WHICH must
962 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
963 (for chkp static vars constructor) or 'B' (for chkp static bounds
964 constructor). BODY is a STATEMENT_LIST containing GENERIC
965 statements. PRIORITY is the initialization priority for this
966 constructor or destructor. */
968 void
969 cgraph_build_static_cdtor (char which, tree body, int priority)
971 cgraph_build_static_cdtor_1 (which, body, priority, false);
974 /* A vector of FUNCTION_DECLs declared as static constructors. */
975 static vec<tree> static_ctors;
976 /* A vector of FUNCTION_DECLs declared as static destructors. */
977 static vec<tree> static_dtors;
979 /* When target does not have ctors and dtors, we call all constructor
980 and destructor by special initialization/destruction function
981 recognized by collect2.
983 When we are going to build this function, collect all constructors and
984 destructors and turn them into normal functions. */
986 static void
987 record_cdtor_fn (struct cgraph_node *node)
989 if (DECL_STATIC_CONSTRUCTOR (node->decl))
990 static_ctors.safe_push (node->decl);
991 if (DECL_STATIC_DESTRUCTOR (node->decl))
992 static_dtors.safe_push (node->decl);
993 node = cgraph_node::get (node->decl);
994 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
997 /* Define global constructors/destructor functions for the CDTORS, of
998 which they are LEN. The CDTORS are sorted by initialization
999 priority. If CTOR_P is true, these are constructors; otherwise,
1000 they are destructors. */
1002 static void
1003 build_cdtor (bool ctor_p, vec<tree> cdtors)
1005 size_t i,j;
1006 size_t len = cdtors.length ();
1008 i = 0;
1009 while (i < len)
1011 tree body;
1012 tree fn;
1013 priority_type priority;
1015 priority = 0;
1016 body = NULL_TREE;
1017 j = i;
1020 priority_type p;
1021 fn = cdtors[j];
1022 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1023 if (j == i)
1024 priority = p;
1025 else if (p != priority)
1026 break;
1027 j++;
1029 while (j < len);
1031 /* When there is only one cdtor and target supports them, do nothing. */
1032 if (j == i + 1
1033 && targetm.have_ctors_dtors)
1035 i++;
1036 continue;
1038 /* Find the next batch of constructors/destructors with the same
1039 initialization priority. */
1040 for (;i < j; i++)
1042 tree call;
1043 fn = cdtors[i];
1044 call = build_call_expr (fn, 0);
1045 if (ctor_p)
1046 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1047 else
1048 DECL_STATIC_DESTRUCTOR (fn) = 0;
1049 /* We do not want to optimize away pure/const calls here.
1050 When optimizing, these should be already removed, when not
1051 optimizing, we want user to be able to breakpoint in them. */
1052 TREE_SIDE_EFFECTS (call) = 1;
1053 append_to_statement_list (call, &body);
1055 gcc_assert (body != NULL_TREE);
1056 /* Generate a function to call all the function of like
1057 priority. */
1058 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1062 /* Comparison function for qsort. P1 and P2 are actually of type
1063 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1064 used to determine the sort order. */
1066 static int
1067 compare_ctor (const void *p1, const void *p2)
1069 tree f1;
1070 tree f2;
1071 int priority1;
1072 int priority2;
1074 f1 = *(const tree *)p1;
1075 f2 = *(const tree *)p2;
1076 priority1 = DECL_INIT_PRIORITY (f1);
1077 priority2 = DECL_INIT_PRIORITY (f2);
1079 if (priority1 < priority2)
1080 return -1;
1081 else if (priority1 > priority2)
1082 return 1;
1083 else
1084 /* Ensure a stable sort. Constructors are executed in backwarding
1085 order to make LTO initialize braries first. */
1086 return DECL_UID (f2) - DECL_UID (f1);
1089 /* Comparison function for qsort. P1 and P2 are actually of type
1090 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1091 used to determine the sort order. */
1093 static int
1094 compare_dtor (const void *p1, const void *p2)
1096 tree f1;
1097 tree f2;
1098 int priority1;
1099 int priority2;
1101 f1 = *(const tree *)p1;
1102 f2 = *(const tree *)p2;
1103 priority1 = DECL_FINI_PRIORITY (f1);
1104 priority2 = DECL_FINI_PRIORITY (f2);
1106 if (priority1 < priority2)
1107 return -1;
1108 else if (priority1 > priority2)
1109 return 1;
1110 else
1111 /* Ensure a stable sort. */
1112 return DECL_UID (f1) - DECL_UID (f2);
1115 /* Generate functions to call static constructors and destructors
1116 for targets that do not support .ctors/.dtors sections. These
1117 functions have magic names which are detected by collect2. */
1119 static void
1120 build_cdtor_fns (void)
1122 if (!static_ctors.is_empty ())
1124 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1125 static_ctors.qsort (compare_ctor);
1126 build_cdtor (/*ctor_p=*/true, static_ctors);
1129 if (!static_dtors.is_empty ())
1131 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1132 static_dtors.qsort (compare_dtor);
1133 build_cdtor (/*ctor_p=*/false, static_dtors);
1137 /* Look for constructors and destructors and produce function calling them.
1138 This is needed for targets not supporting ctors or dtors, but we perform the
1139 transformation also at linktime to merge possibly numerous
1140 constructors/destructors into single function to improve code locality and
1141 reduce size. */
1143 static unsigned int
1144 ipa_cdtor_merge (void)
1146 struct cgraph_node *node;
1147 FOR_EACH_DEFINED_FUNCTION (node)
1148 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1149 || DECL_STATIC_DESTRUCTOR (node->decl))
1150 record_cdtor_fn (node);
1151 build_cdtor_fns ();
1152 static_ctors.release ();
1153 static_dtors.release ();
1154 return 0;
1157 namespace {
1159 const pass_data pass_data_ipa_cdtor_merge =
1161 IPA_PASS, /* type */
1162 "cdtor", /* name */
1163 OPTGROUP_NONE, /* optinfo_flags */
1164 TV_CGRAPHOPT, /* tv_id */
1165 0, /* properties_required */
1166 0, /* properties_provided */
1167 0, /* properties_destroyed */
1168 0, /* todo_flags_start */
1169 0, /* todo_flags_finish */
1172 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1174 public:
1175 pass_ipa_cdtor_merge (gcc::context *ctxt)
1176 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1177 NULL, /* generate_summary */
1178 NULL, /* write_summary */
1179 NULL, /* read_summary */
1180 NULL, /* write_optimization_summary */
1181 NULL, /* read_optimization_summary */
1182 NULL, /* stmt_fixup */
1183 0, /* function_transform_todo_flags_start */
1184 NULL, /* function_transform */
1185 NULL) /* variable_transform */
1188 /* opt_pass methods: */
1189 virtual bool gate (function *);
1190 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1192 }; // class pass_ipa_cdtor_merge
1194 bool
1195 pass_ipa_cdtor_merge::gate (function *)
1197 /* Perform the pass when we have no ctors/dtors support
1198 or at LTO time to merge multiple constructors into single
1199 function. */
1200 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1203 } // anon namespace
1205 ipa_opt_pass_d *
1206 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1208 return new pass_ipa_cdtor_merge (ctxt);
1211 /* Invalid pointer representing BOTTOM for single user dataflow. */
1212 #define BOTTOM ((cgraph_node *)(size_t) 2)
1214 /* Meet operation for single user dataflow.
1215 Here we want to associate variables with sigle function that may access it.
1217 FUNCTION is current single user of a variable, VAR is variable that uses it.
1218 Latttice is stored in SINGLE_USER_MAP.
1220 We represent:
1221 - TOP by no entry in SIGNLE_USER_MAP
1222 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1223 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1225 cgraph_node *
1226 meet (cgraph_node *function, varpool_node *var,
1227 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1229 struct cgraph_node *user, **f;
1231 if (var->aux == BOTTOM)
1232 return BOTTOM;
1234 f = single_user_map.get (var);
1235 if (!f)
1236 return function;
1237 user = *f;
1238 if (!function)
1239 return user;
1240 else if (function != user)
1241 return BOTTOM;
1242 else
1243 return function;
1246 /* Propagation step of single-use dataflow.
1248 Check all uses of VNODE and see if they are used by single function FUNCTION.
1249 SINGLE_USER_MAP represents the dataflow lattice. */
1251 cgraph_node *
1252 propagate_single_user (varpool_node *vnode, cgraph_node *function,
1253 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1255 int i;
1256 struct ipa_ref *ref;
1258 gcc_assert (!vnode->externally_visible);
1260 /* If node is an alias, first meet with its target. */
1261 if (vnode->alias)
1262 function = meet (function, vnode->get_alias_target (), single_user_map);
1264 /* Check all users and see if they correspond to a single function. */
1265 for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
1267 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
1268 if (cnode)
1270 if (cnode->global.inlined_to)
1271 cnode = cnode->global.inlined_to;
1272 if (!function)
1273 function = cnode;
1274 else if (function != cnode)
1275 function = BOTTOM;
1277 else
1278 function = meet (function, dyn_cast <varpool_node *> (ref->referring),
1279 single_user_map);
1281 return function;
1284 /* Pass setting used_by_single_function flag.
1285 This flag is set on variable when there is only one function that may
1286 possibly referr to it. */
1288 static unsigned int
1289 ipa_single_use (void)
1291 varpool_node *first = (varpool_node *) (void *) 1;
1292 varpool_node *var;
1293 hash_map<varpool_node *, cgraph_node *> single_user_map;
1295 FOR_EACH_DEFINED_VARIABLE (var)
1296 if (!var->all_refs_explicit_p ())
1297 var->aux = BOTTOM;
1298 else
1300 /* Enqueue symbol for dataflow. */
1301 var->aux = first;
1302 first = var;
1305 /* The actual dataflow. */
1307 while (first != (void *) 1)
1309 cgraph_node *user, *orig_user, **f;
1311 var = first;
1312 first = (varpool_node *)first->aux;
1314 f = single_user_map.get (var);
1315 if (f)
1316 orig_user = *f;
1317 else
1318 orig_user = NULL;
1319 user = propagate_single_user (var, orig_user, single_user_map);
1321 gcc_checking_assert (var->aux != BOTTOM);
1323 /* If user differs, enqueue all references. */
1324 if (user != orig_user)
1326 unsigned int i;
1327 ipa_ref *ref;
1329 single_user_map.put (var, user);
1331 /* Enqueue all aliases for re-processing. */
1332 for (i = 0; var->iterate_direct_aliases (i, ref); i++)
1333 if (!ref->referring->aux)
1335 ref->referring->aux = first;
1336 first = dyn_cast <varpool_node *> (ref->referring);
1338 /* Enqueue all users for re-processing. */
1339 for (i = 0; var->iterate_reference (i, ref); i++)
1340 if (!ref->referred->aux
1341 && ref->referred->definition
1342 && is_a <varpool_node *> (ref->referred))
1344 ref->referred->aux = first;
1345 first = dyn_cast <varpool_node *> (ref->referred);
1348 /* If user is BOTTOM, just punt on this var. */
1349 if (user == BOTTOM)
1350 var->aux = BOTTOM;
1351 else
1352 var->aux = NULL;
1354 else
1355 var->aux = NULL;
1358 FOR_EACH_DEFINED_VARIABLE (var)
1360 if (var->aux != BOTTOM)
1362 /* Not having the single user known means that the VAR is
1363 unreachable. Either someone forgot to remove unreachable
1364 variables or the reachability here is wrong. */
1366 gcc_checking_assert (single_user_map.get (var));
1368 if (dump_file)
1370 fprintf (dump_file, "Variable %s/%i is used by single function\n",
1371 var->name (), var->order);
1373 var->used_by_single_function = true;
1375 var->aux = NULL;
1377 return 0;
1380 namespace {
1382 const pass_data pass_data_ipa_single_use =
1384 IPA_PASS, /* type */
1385 "single-use", /* name */
1386 OPTGROUP_NONE, /* optinfo_flags */
1387 TV_CGRAPHOPT, /* tv_id */
1388 0, /* properties_required */
1389 0, /* properties_provided */
1390 0, /* properties_destroyed */
1391 0, /* todo_flags_start */
1392 0, /* todo_flags_finish */
1395 class pass_ipa_single_use : public ipa_opt_pass_d
1397 public:
1398 pass_ipa_single_use (gcc::context *ctxt)
1399 : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
1400 NULL, /* generate_summary */
1401 NULL, /* write_summary */
1402 NULL, /* read_summary */
1403 NULL, /* write_optimization_summary */
1404 NULL, /* read_optimization_summary */
1405 NULL, /* stmt_fixup */
1406 0, /* function_transform_todo_flags_start */
1407 NULL, /* function_transform */
1408 NULL) /* variable_transform */
1411 /* opt_pass methods: */
1412 virtual bool gate (function *);
1413 virtual unsigned int execute (function *) { return ipa_single_use (); }
1415 }; // class pass_ipa_single_use
1417 bool
1418 pass_ipa_single_use::gate (function *)
1420 return optimize;
1423 } // anon namespace
1425 ipa_opt_pass_d *
1426 make_pass_ipa_single_use (gcc::context *ctxt)
1428 return new pass_ipa_single_use (ctxt);