Missing copyright for mem-stats header files.
[official-gcc.git] / gcc / ipa.c
blob3c8fc00e6b54ea92af32e0948e963d2e4d722c23
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2016 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->transparent_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_all_references ();
559 changed = true;
560 if (node->thunk.thunk_p
561 && node->thunk.add_pointer_bounds_args)
563 node->thunk.thunk_p = false;
564 node->thunk.add_pointer_bounds_args = false;
568 else
569 gcc_assert (node->clone_of || !node->has_gimple_body_p ()
570 || in_lto_p || DECL_RESULT (node->decl));
573 /* Inline clones might be kept around so their materializing allows further
574 cloning. If the function the clone is inlined into is removed, we need
575 to turn it into normal cone. */
576 FOR_EACH_FUNCTION (node)
578 if (node->global.inlined_to
579 && !node->callers)
581 gcc_assert (node->clones);
582 node->global.inlined_to = NULL;
583 update_inlined_to_pointer (node, node);
585 node->aux = NULL;
588 /* Remove unreachable variables. */
589 if (file)
590 fprintf (file, "\nReclaiming variables:");
591 for (vnode = first_variable (); vnode; vnode = vnext)
593 vnext = next_variable (vnode);
594 if (!vnode->aux
595 /* For can_refer_decl_in_current_unit_p we want to track for
596 all external variables if they are defined in other partition
597 or not. */
598 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
600 struct ipa_ref *ref = NULL;
602 /* First remove the aliases, so varpool::remove can possibly lookup
603 the constructor and save it for future use. */
604 while (vnode->iterate_direct_aliases (0, ref))
606 if (file)
607 fprintf (file, " %s/%i", ref->referred->name (),
608 ref->referred->order);
609 ref->referring->remove ();
611 if (file)
612 fprintf (file, " %s/%i", vnode->name (), vnode->order);
613 vnext = next_variable (vnode);
614 vnode->remove ();
615 changed = true;
617 else if (!reachable.contains (vnode) && !vnode->alias)
619 tree init;
620 if (vnode->definition)
622 if (file)
623 fprintf (file, " %s", vnode->name ());
624 changed = true;
626 /* Keep body if it may be useful for constant folding. */
627 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node
628 && !POINTER_BOUNDS_P (vnode->decl))
629 vnode->remove_initializer ();
630 else
631 DECL_INITIAL (vnode->decl) = init;
632 vnode->body_removed = true;
633 vnode->definition = false;
634 vnode->analyzed = false;
635 vnode->aux = NULL;
637 vnode->remove_from_same_comdat_group ();
639 vnode->remove_all_references ();
641 else
642 vnode->aux = NULL;
645 /* Now update address_taken flags and try to promote functions to be local. */
646 if (file)
647 fprintf (file, "\nClearing address taken flags:");
648 FOR_EACH_DEFINED_FUNCTION (node)
649 if (node->address_taken
650 && !node->used_from_other_partition)
652 if (!node->call_for_symbol_and_aliases
653 (has_addr_references_p, NULL, true)
654 && (!node->instrumentation_clone
655 || !node->instrumented_version
656 || !node->instrumented_version->address_taken))
658 if (file)
659 fprintf (file, " %s", node->name ());
660 node->address_taken = false;
661 changed = true;
662 if (node->local_p ())
664 node->local.local = true;
665 if (file)
666 fprintf (file, " (local)");
670 if (file)
671 fprintf (file, "\n");
673 symtab_node::checking_verify_symtab_nodes ();
675 /* If we removed something, perhaps profile could be improved. */
676 if (changed && optimize && inline_edge_summary_vec.exists ())
677 FOR_EACH_DEFINED_FUNCTION (node)
678 ipa_propagate_frequency (node);
680 timevar_pop (TV_IPA_UNREACHABLE);
681 return changed;
684 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
685 as needed, also clear EXPLICIT_REFS if the references to given variable
686 do not need to be explicit. */
688 void
689 process_references (varpool_node *vnode,
690 bool *written, bool *address_taken,
691 bool *read, bool *explicit_refs)
693 int i;
694 struct ipa_ref *ref;
696 if (!vnode->all_refs_explicit_p ()
697 || TREE_THIS_VOLATILE (vnode->decl))
698 *explicit_refs = false;
700 for (i = 0; vnode->iterate_referring (i, ref)
701 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
702 switch (ref->use)
704 case IPA_REF_ADDR:
705 *address_taken = true;
706 break;
707 case IPA_REF_LOAD:
708 *read = true;
709 break;
710 case IPA_REF_STORE:
711 *written = true;
712 break;
713 case IPA_REF_ALIAS:
714 process_references (dyn_cast<varpool_node *> (ref->referring), written,
715 address_taken, read, explicit_refs);
716 break;
717 case IPA_REF_CHKP:
718 gcc_unreachable ();
722 /* Set TREE_READONLY bit. */
724 bool
725 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
727 TREE_READONLY (vnode->decl) = true;
728 return false;
731 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
733 bool
734 set_writeonly_bit (varpool_node *vnode, void *data)
736 vnode->writeonly = true;
737 if (optimize)
739 DECL_INITIAL (vnode->decl) = NULL;
740 if (!vnode->alias)
742 if (vnode->num_references ())
743 *(bool *)data = true;
744 vnode->remove_all_references ();
747 return false;
750 /* Clear addressale bit of VNODE. */
752 bool
753 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
755 vnode->address_taken = false;
756 TREE_ADDRESSABLE (vnode->decl) = 0;
757 return false;
760 /* Discover variables that have no longer address taken or that are read only
761 and update their flags.
763 Return true when unreachable symbol removan should be done.
765 FIXME: This can not be done in between gimplify and omp_expand since
766 readonly flag plays role on what is shared and what is not. Currently we do
767 this transformation as part of whole program visibility and re-do at
768 ipa-reference pass (to take into account clonning), but it would
769 make sense to do it before early optimizations. */
771 bool
772 ipa_discover_readonly_nonaddressable_vars (void)
774 bool remove_p = false;
775 varpool_node *vnode;
776 if (dump_file)
777 fprintf (dump_file, "Clearing variable flags:");
778 FOR_EACH_VARIABLE (vnode)
779 if (!vnode->alias
780 && (TREE_ADDRESSABLE (vnode->decl)
781 || !vnode->writeonly
782 || !TREE_READONLY (vnode->decl)))
784 bool written = false;
785 bool address_taken = false;
786 bool read = false;
787 bool explicit_refs = true;
789 process_references (vnode, &written, &address_taken, &read,
790 &explicit_refs);
791 if (!explicit_refs)
792 continue;
793 if (!address_taken)
795 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
796 fprintf (dump_file, " %s (non-addressable)", vnode->name ());
797 vnode->call_for_symbol_and_aliases (clear_addressable_bit, NULL,
798 true);
800 if (!address_taken && !written
801 /* Making variable in explicit section readonly can cause section
802 type conflict.
803 See e.g. gcc.c-torture/compile/pr23237.c */
804 && vnode->get_section () == NULL)
806 if (!TREE_READONLY (vnode->decl) && dump_file)
807 fprintf (dump_file, " %s (read-only)", vnode->name ());
808 vnode->call_for_symbol_and_aliases (set_readonly_bit, NULL, true);
810 if (!vnode->writeonly && !read && !address_taken && written)
812 if (dump_file)
813 fprintf (dump_file, " %s (write-only)", vnode->name ());
814 vnode->call_for_symbol_and_aliases (set_writeonly_bit, &remove_p,
815 true);
818 if (dump_file)
819 fprintf (dump_file, "\n");
820 return remove_p;
823 /* Free inline summary. */
825 namespace {
827 const pass_data pass_data_ipa_free_inline_summary =
829 SIMPLE_IPA_PASS, /* type */
830 "free-inline-summary", /* name */
831 OPTGROUP_NONE, /* optinfo_flags */
832 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
833 0, /* properties_required */
834 0, /* properties_provided */
835 0, /* properties_destroyed */
836 0, /* todo_flags_start */
837 /* Early optimizations may make function unreachable. We can not
838 remove unreachable functions as part of the ealry opts pass because
839 TODOs are run before subpasses. Do it here. */
840 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
843 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
845 public:
846 pass_ipa_free_inline_summary (gcc::context *ctxt)
847 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
850 /* opt_pass methods: */
851 virtual unsigned int execute (function *)
853 inline_free_summary ();
854 return 0;
857 }; // class pass_ipa_free_inline_summary
859 } // anon namespace
861 simple_ipa_opt_pass *
862 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
864 return new pass_ipa_free_inline_summary (ctxt);
867 /* Generate and emit a static constructor or destructor. WHICH must
868 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
869 (for chp static vars constructor) or 'B' (for chkp static bounds
870 constructor). BODY is a STATEMENT_LIST containing GENERIC
871 statements. PRIORITY is the initialization priority for this
872 constructor or destructor.
874 FINAL specify whether the externally visible name for collect2 should
875 be produced. */
877 static void
878 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
880 static int counter = 0;
881 char which_buf[16];
882 tree decl, name, resdecl;
884 /* The priority is encoded in the constructor or destructor name.
885 collect2 will sort the names and arrange that they are called at
886 program startup. */
887 if (final)
888 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
889 else
890 /* Proudce sane name but one not recognizable by collect2, just for the
891 case we fail to inline the function. */
892 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
893 name = get_file_function_name (which_buf);
895 decl = build_decl (input_location, FUNCTION_DECL, name,
896 build_function_type_list (void_type_node, NULL_TREE));
897 current_function_decl = decl;
899 resdecl = build_decl (input_location,
900 RESULT_DECL, NULL_TREE, void_type_node);
901 DECL_ARTIFICIAL (resdecl) = 1;
902 DECL_RESULT (decl) = resdecl;
903 DECL_CONTEXT (resdecl) = decl;
905 allocate_struct_function (decl, false);
907 TREE_STATIC (decl) = 1;
908 TREE_USED (decl) = 1;
909 DECL_ARTIFICIAL (decl) = 1;
910 DECL_IGNORED_P (decl) = 1;
911 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
912 DECL_SAVED_TREE (decl) = body;
913 if (!targetm.have_ctors_dtors && final)
915 TREE_PUBLIC (decl) = 1;
916 DECL_PRESERVE_P (decl) = 1;
918 DECL_UNINLINABLE (decl) = 1;
920 DECL_INITIAL (decl) = make_node (BLOCK);
921 TREE_USED (DECL_INITIAL (decl)) = 1;
923 DECL_SOURCE_LOCATION (decl) = input_location;
924 cfun->function_end_locus = input_location;
926 switch (which)
928 case 'I':
929 DECL_STATIC_CONSTRUCTOR (decl) = 1;
930 decl_init_priority_insert (decl, priority);
931 break;
932 case 'P':
933 DECL_STATIC_CONSTRUCTOR (decl) = 1;
934 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("chkp ctor"),
935 NULL,
936 NULL_TREE);
937 decl_init_priority_insert (decl, priority);
938 break;
939 case 'B':
940 DECL_STATIC_CONSTRUCTOR (decl) = 1;
941 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("bnd_legacy"),
942 NULL,
943 NULL_TREE);
944 decl_init_priority_insert (decl, priority);
945 break;
946 case 'D':
947 DECL_STATIC_DESTRUCTOR (decl) = 1;
948 decl_fini_priority_insert (decl, priority);
949 break;
950 default:
951 gcc_unreachable ();
954 gimplify_function_tree (decl);
956 cgraph_node::add_new_function (decl, false);
958 set_cfun (NULL);
959 current_function_decl = NULL;
962 /* Generate and emit a static constructor or destructor. WHICH must
963 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
964 (for chkp static vars constructor) or 'B' (for chkp static bounds
965 constructor). BODY is a STATEMENT_LIST containing GENERIC
966 statements. PRIORITY is the initialization priority for this
967 constructor or destructor. */
969 void
970 cgraph_build_static_cdtor (char which, tree body, int priority)
972 cgraph_build_static_cdtor_1 (which, body, priority, false);
975 /* A vector of FUNCTION_DECLs declared as static constructors. */
976 static vec<tree> static_ctors;
977 /* A vector of FUNCTION_DECLs declared as static destructors. */
978 static vec<tree> static_dtors;
980 /* When target does not have ctors and dtors, we call all constructor
981 and destructor by special initialization/destruction function
982 recognized by collect2.
984 When we are going to build this function, collect all constructors and
985 destructors and turn them into normal functions. */
987 static void
988 record_cdtor_fn (struct cgraph_node *node)
990 if (DECL_STATIC_CONSTRUCTOR (node->decl))
991 static_ctors.safe_push (node->decl);
992 if (DECL_STATIC_DESTRUCTOR (node->decl))
993 static_dtors.safe_push (node->decl);
994 node = cgraph_node::get (node->decl);
995 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
998 /* Define global constructors/destructor functions for the CDTORS, of
999 which they are LEN. The CDTORS are sorted by initialization
1000 priority. If CTOR_P is true, these are constructors; otherwise,
1001 they are destructors. */
1003 static void
1004 build_cdtor (bool ctor_p, vec<tree> cdtors)
1006 size_t i,j;
1007 size_t len = cdtors.length ();
1009 i = 0;
1010 while (i < len)
1012 tree body;
1013 tree fn;
1014 priority_type priority;
1016 priority = 0;
1017 body = NULL_TREE;
1018 j = i;
1021 priority_type p;
1022 fn = cdtors[j];
1023 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1024 if (j == i)
1025 priority = p;
1026 else if (p != priority)
1027 break;
1028 j++;
1030 while (j < len);
1032 /* When there is only one cdtor and target supports them, do nothing. */
1033 if (j == i + 1
1034 && targetm.have_ctors_dtors)
1036 i++;
1037 continue;
1039 /* Find the next batch of constructors/destructors with the same
1040 initialization priority. */
1041 for (;i < j; i++)
1043 tree call;
1044 fn = cdtors[i];
1045 call = build_call_expr (fn, 0);
1046 if (ctor_p)
1047 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1048 else
1049 DECL_STATIC_DESTRUCTOR (fn) = 0;
1050 /* We do not want to optimize away pure/const calls here.
1051 When optimizing, these should be already removed, when not
1052 optimizing, we want user to be able to breakpoint in them. */
1053 TREE_SIDE_EFFECTS (call) = 1;
1054 append_to_statement_list (call, &body);
1056 gcc_assert (body != NULL_TREE);
1057 /* Generate a function to call all the function of like
1058 priority. */
1059 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1063 /* Comparison function for qsort. P1 and P2 are actually of type
1064 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1065 used to determine the sort order. */
1067 static int
1068 compare_ctor (const void *p1, const void *p2)
1070 tree f1;
1071 tree f2;
1072 int priority1;
1073 int priority2;
1075 f1 = *(const tree *)p1;
1076 f2 = *(const tree *)p2;
1077 priority1 = DECL_INIT_PRIORITY (f1);
1078 priority2 = DECL_INIT_PRIORITY (f2);
1080 if (priority1 < priority2)
1081 return -1;
1082 else if (priority1 > priority2)
1083 return 1;
1084 else
1085 /* Ensure a stable sort. Constructors are executed in backwarding
1086 order to make LTO initialize braries first. */
1087 return DECL_UID (f2) - DECL_UID (f1);
1090 /* Comparison function for qsort. P1 and P2 are actually of type
1091 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1092 used to determine the sort order. */
1094 static int
1095 compare_dtor (const void *p1, const void *p2)
1097 tree f1;
1098 tree f2;
1099 int priority1;
1100 int priority2;
1102 f1 = *(const tree *)p1;
1103 f2 = *(const tree *)p2;
1104 priority1 = DECL_FINI_PRIORITY (f1);
1105 priority2 = DECL_FINI_PRIORITY (f2);
1107 if (priority1 < priority2)
1108 return -1;
1109 else if (priority1 > priority2)
1110 return 1;
1111 else
1112 /* Ensure a stable sort. */
1113 return DECL_UID (f1) - DECL_UID (f2);
1116 /* Generate functions to call static constructors and destructors
1117 for targets that do not support .ctors/.dtors sections. These
1118 functions have magic names which are detected by collect2. */
1120 static void
1121 build_cdtor_fns (void)
1123 if (!static_ctors.is_empty ())
1125 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1126 static_ctors.qsort (compare_ctor);
1127 build_cdtor (/*ctor_p=*/true, static_ctors);
1130 if (!static_dtors.is_empty ())
1132 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1133 static_dtors.qsort (compare_dtor);
1134 build_cdtor (/*ctor_p=*/false, static_dtors);
1138 /* Look for constructors and destructors and produce function calling them.
1139 This is needed for targets not supporting ctors or dtors, but we perform the
1140 transformation also at linktime to merge possibly numerous
1141 constructors/destructors into single function to improve code locality and
1142 reduce size. */
1144 static unsigned int
1145 ipa_cdtor_merge (void)
1147 struct cgraph_node *node;
1148 FOR_EACH_DEFINED_FUNCTION (node)
1149 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1150 || DECL_STATIC_DESTRUCTOR (node->decl))
1151 record_cdtor_fn (node);
1152 build_cdtor_fns ();
1153 static_ctors.release ();
1154 static_dtors.release ();
1155 return 0;
1158 namespace {
1160 const pass_data pass_data_ipa_cdtor_merge =
1162 IPA_PASS, /* type */
1163 "cdtor", /* name */
1164 OPTGROUP_NONE, /* optinfo_flags */
1165 TV_CGRAPHOPT, /* tv_id */
1166 0, /* properties_required */
1167 0, /* properties_provided */
1168 0, /* properties_destroyed */
1169 0, /* todo_flags_start */
1170 0, /* todo_flags_finish */
1173 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1175 public:
1176 pass_ipa_cdtor_merge (gcc::context *ctxt)
1177 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1178 NULL, /* generate_summary */
1179 NULL, /* write_summary */
1180 NULL, /* read_summary */
1181 NULL, /* write_optimization_summary */
1182 NULL, /* read_optimization_summary */
1183 NULL, /* stmt_fixup */
1184 0, /* function_transform_todo_flags_start */
1185 NULL, /* function_transform */
1186 NULL) /* variable_transform */
1189 /* opt_pass methods: */
1190 virtual bool gate (function *);
1191 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1193 }; // class pass_ipa_cdtor_merge
1195 bool
1196 pass_ipa_cdtor_merge::gate (function *)
1198 /* Perform the pass when we have no ctors/dtors support
1199 or at LTO time to merge multiple constructors into single
1200 function. */
1201 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1204 } // anon namespace
1206 ipa_opt_pass_d *
1207 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1209 return new pass_ipa_cdtor_merge (ctxt);
1212 /* Invalid pointer representing BOTTOM for single user dataflow. */
1213 #define BOTTOM ((cgraph_node *)(size_t) 2)
1215 /* Meet operation for single user dataflow.
1216 Here we want to associate variables with sigle function that may access it.
1218 FUNCTION is current single user of a variable, VAR is variable that uses it.
1219 Latttice is stored in SINGLE_USER_MAP.
1221 We represent:
1222 - TOP by no entry in SIGNLE_USER_MAP
1223 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1224 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1226 cgraph_node *
1227 meet (cgraph_node *function, varpool_node *var,
1228 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1230 struct cgraph_node *user, **f;
1232 if (var->aux == BOTTOM)
1233 return BOTTOM;
1235 f = single_user_map.get (var);
1236 if (!f)
1237 return function;
1238 user = *f;
1239 if (!function)
1240 return user;
1241 else if (function != user)
1242 return BOTTOM;
1243 else
1244 return function;
1247 /* Propagation step of single-use dataflow.
1249 Check all uses of VNODE and see if they are used by single function FUNCTION.
1250 SINGLE_USER_MAP represents the dataflow lattice. */
1252 cgraph_node *
1253 propagate_single_user (varpool_node *vnode, cgraph_node *function,
1254 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1256 int i;
1257 struct ipa_ref *ref;
1259 gcc_assert (!vnode->externally_visible);
1261 /* If node is an alias, first meet with its target. */
1262 if (vnode->alias)
1263 function = meet (function, vnode->get_alias_target (), single_user_map);
1265 /* Check all users and see if they correspond to a single function. */
1266 for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
1268 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
1269 if (cnode)
1271 if (cnode->global.inlined_to)
1272 cnode = cnode->global.inlined_to;
1273 if (!function)
1274 function = cnode;
1275 else if (function != cnode)
1276 function = BOTTOM;
1278 else
1279 function = meet (function, dyn_cast <varpool_node *> (ref->referring),
1280 single_user_map);
1282 return function;
1285 /* Pass setting used_by_single_function flag.
1286 This flag is set on variable when there is only one function that may
1287 possibly referr to it. */
1289 static unsigned int
1290 ipa_single_use (void)
1292 varpool_node *first = (varpool_node *) (void *) 1;
1293 varpool_node *var;
1294 hash_map<varpool_node *, cgraph_node *> single_user_map;
1296 FOR_EACH_DEFINED_VARIABLE (var)
1297 if (!var->all_refs_explicit_p ())
1298 var->aux = BOTTOM;
1299 else
1301 /* Enqueue symbol for dataflow. */
1302 var->aux = first;
1303 first = var;
1306 /* The actual dataflow. */
1308 while (first != (void *) 1)
1310 cgraph_node *user, *orig_user, **f;
1312 var = first;
1313 first = (varpool_node *)first->aux;
1315 f = single_user_map.get (var);
1316 if (f)
1317 orig_user = *f;
1318 else
1319 orig_user = NULL;
1320 user = propagate_single_user (var, orig_user, single_user_map);
1322 gcc_checking_assert (var->aux != BOTTOM);
1324 /* If user differs, enqueue all references. */
1325 if (user != orig_user)
1327 unsigned int i;
1328 ipa_ref *ref;
1330 single_user_map.put (var, user);
1332 /* Enqueue all aliases for re-processing. */
1333 for (i = 0; var->iterate_direct_aliases (i, ref); i++)
1334 if (!ref->referring->aux)
1336 ref->referring->aux = first;
1337 first = dyn_cast <varpool_node *> (ref->referring);
1339 /* Enqueue all users for re-processing. */
1340 for (i = 0; var->iterate_reference (i, ref); i++)
1341 if (!ref->referred->aux
1342 && ref->referred->definition
1343 && is_a <varpool_node *> (ref->referred))
1345 ref->referred->aux = first;
1346 first = dyn_cast <varpool_node *> (ref->referred);
1349 /* If user is BOTTOM, just punt on this var. */
1350 if (user == BOTTOM)
1351 var->aux = BOTTOM;
1352 else
1353 var->aux = NULL;
1355 else
1356 var->aux = NULL;
1359 FOR_EACH_DEFINED_VARIABLE (var)
1361 if (var->aux != BOTTOM)
1363 /* Not having the single user known means that the VAR is
1364 unreachable. Either someone forgot to remove unreachable
1365 variables or the reachability here is wrong. */
1367 gcc_checking_assert (single_user_map.get (var));
1369 if (dump_file)
1371 fprintf (dump_file, "Variable %s/%i is used by single function\n",
1372 var->name (), var->order);
1374 var->used_by_single_function = true;
1376 var->aux = NULL;
1378 return 0;
1381 namespace {
1383 const pass_data pass_data_ipa_single_use =
1385 IPA_PASS, /* type */
1386 "single-use", /* name */
1387 OPTGROUP_NONE, /* optinfo_flags */
1388 TV_CGRAPHOPT, /* tv_id */
1389 0, /* properties_required */
1390 0, /* properties_provided */
1391 0, /* properties_destroyed */
1392 0, /* todo_flags_start */
1393 0, /* todo_flags_finish */
1396 class pass_ipa_single_use : public ipa_opt_pass_d
1398 public:
1399 pass_ipa_single_use (gcc::context *ctxt)
1400 : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
1401 NULL, /* generate_summary */
1402 NULL, /* write_summary */
1403 NULL, /* read_summary */
1404 NULL, /* write_optimization_summary */
1405 NULL, /* read_optimization_summary */
1406 NULL, /* stmt_fixup */
1407 0, /* function_transform_todo_flags_start */
1408 NULL, /* function_transform */
1409 NULL) /* variable_transform */
1412 /* opt_pass methods: */
1413 virtual bool gate (function *);
1414 virtual unsigned int execute (function *) { return ipa_single_use (); }
1416 }; // class pass_ipa_single_use
1418 bool
1419 pass_ipa_single_use::gate (function *)
1421 return optimize;
1424 } // anon namespace
1426 ipa_opt_pass_d *
1427 make_pass_ipa_single_use (gcc::context *ctxt)
1429 return new pass_ipa_single_use (ctxt);