2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ipa.c
blobe42ad5e625a97c33dc9eed1bb083e299610d4f22
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 "input.h"
25 #include "alias.h"
26 #include "symtab.h"
27 #include "options.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "calls.h"
31 #include "stringpool.h"
32 #include "predict.h"
33 #include "basic-block.h"
34 #include "is-a.h"
35 #include "plugin-api.h"
36 #include "hard-reg-set.h"
37 #include "input.h"
38 #include "function.h"
39 #include "ipa-ref.h"
40 #include "cgraph.h"
41 #include "tree-pass.h"
42 #include "gimple-expr.h"
43 #include "gimplify.h"
44 #include "flags.h"
45 #include "target.h"
46 #include "tree-iterator.h"
47 #include "ipa-utils.h"
48 #include "alloc-pool.h"
49 #include "symbol-summary.h"
50 #include "ipa-prop.h"
51 #include "ipa-inline.h"
52 #include "tree-inline.h"
53 #include "profile.h"
54 #include "params.h"
55 #include "internal-fn.h"
56 #include "tree-ssa-alias.h"
57 #include "gimple.h"
58 #include "dbgcnt.h"
61 /* Return true when NODE has ADDR reference. */
63 static bool
64 has_addr_references_p (struct cgraph_node *node,
65 void *data ATTRIBUTE_UNUSED)
67 int i;
68 struct ipa_ref *ref = NULL;
70 for (i = 0; node->iterate_referring (i, ref); i++)
71 if (ref->use == IPA_REF_ADDR)
72 return true;
73 return false;
76 /* Look for all functions inlined to NODE and update their inlined_to pointers
77 to INLINED_TO. */
79 static void
80 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
82 struct cgraph_edge *e;
83 for (e = node->callees; e; e = e->next_callee)
84 if (e->callee->global.inlined_to)
86 e->callee->global.inlined_to = inlined_to;
87 update_inlined_to_pointer (e->callee, inlined_to);
91 /* Add symtab NODE to queue starting at FIRST.
93 The queue is linked via AUX pointers and terminated by pointer to 1.
94 We enqueue nodes at two occasions: when we find them reachable or when we find
95 their bodies needed for further clonning. In the second case we mark them
96 by pointer to 2 after processing so they are re-queue when they become
97 reachable. */
99 static void
100 enqueue_node (symtab_node *node, symtab_node **first,
101 hash_set<symtab_node *> *reachable)
103 /* Node is still in queue; do nothing. */
104 if (node->aux && node->aux != (void *) 2)
105 return;
106 /* Node was already processed as unreachable, re-enqueue
107 only if it became reachable now. */
108 if (node->aux == (void *)2 && !reachable->contains (node))
109 return;
110 node->aux = *first;
111 *first = node;
114 /* Process references. */
116 static void
117 process_references (symtab_node *snode,
118 symtab_node **first,
119 bool before_inlining_p,
120 hash_set<symtab_node *> *reachable)
122 int i;
123 struct ipa_ref *ref = NULL;
124 for (i = 0; snode->iterate_reference (i, ref); i++)
126 symtab_node *node = ref->referred;
127 symtab_node *body = node->ultimate_alias_target ();
129 if (node->definition && !node->in_other_partition
130 && ((!DECL_EXTERNAL (node->decl) || node->alias)
131 || (((before_inlining_p
132 && ((TREE_CODE (node->decl) != FUNCTION_DECL
133 && optimize)
134 || (TREE_CODE (node->decl) == FUNCTION_DECL
135 && opt_for_fn (body->decl, optimize))
136 || (symtab->state < IPA_SSA
137 && lookup_attribute
138 ("always_inline",
139 DECL_ATTRIBUTES (body->decl))))))
140 /* We use variable constructors during late compilation for
141 constant folding. Keep references alive so partitioning
142 knows about potential references. */
143 || (TREE_CODE (node->decl) == VAR_DECL
144 && flag_wpa
145 && ctor_for_folding (node->decl)
146 != error_mark_node))))
148 /* Be sure that we will not optimize out alias target
149 body. */
150 if (DECL_EXTERNAL (node->decl)
151 && node->alias
152 && before_inlining_p)
153 reachable->add (body);
154 reachable->add (node);
156 enqueue_node (node, first, reachable);
160 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
161 all its potential targets as reachable to permit later inlining if
162 devirtualization happens. After inlining still keep their declarations
163 around, so we can devirtualize to a direct call.
165 Also try to make trivial devirutalization when no or only one target is
166 possible. */
168 static void
169 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
170 struct cgraph_edge *edge,
171 symtab_node **first,
172 hash_set<symtab_node *> *reachable,
173 bool before_inlining_p)
175 unsigned int i;
176 void *cache_token;
177 bool final;
178 vec <cgraph_node *>targets
179 = possible_polymorphic_call_targets
180 (edge, &final, &cache_token);
182 if (!reachable_call_targets->add (cache_token))
184 for (i = 0; i < targets.length (); i++)
186 struct cgraph_node *n = targets[i];
188 /* Do not bother to mark virtual methods in anonymous namespace;
189 either we will find use of virtual table defining it, or it is
190 unused. */
191 if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
192 && type_in_anonymous_namespace_p
193 (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl))))
194 continue;
196 symtab_node *body = n->function_symbol ();
198 /* Prior inlining, keep alive bodies of possible targets for
199 devirtualization. */
200 if (n->definition
201 && (before_inlining_p
202 && opt_for_fn (body->decl, optimize)
203 && opt_for_fn (body->decl, flag_devirtualize)))
205 /* Be sure that we will not optimize out alias target
206 body. */
207 if (DECL_EXTERNAL (n->decl)
208 && n->alias
209 && before_inlining_p)
210 reachable->add (body);
211 reachable->add (n);
213 /* Even after inlining we want to keep the possible targets in the
214 boundary, so late passes can still produce direct call even if
215 the chance for inlining is lost. */
216 enqueue_node (n, first, reachable);
220 /* Very trivial devirtualization; when the type is
221 final or anonymous (so we know all its derivation)
222 and there is only one possible virtual call target,
223 make the edge direct. */
224 if (final)
226 if (targets.length () <= 1 && dbg_cnt (devirt))
228 cgraph_node *target, *node = edge->caller;
229 if (targets.length () == 1)
230 target = targets[0];
231 else
232 target = cgraph_node::get_create
233 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
235 if (dump_enabled_p ())
237 location_t locus;
238 if (edge->call_stmt)
239 locus = gimple_location (edge->call_stmt);
240 else
241 locus = UNKNOWN_LOCATION;
242 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
243 "devirtualizing call in %s/%i to %s/%i\n",
244 edge->caller->name (), edge->caller->order,
245 target->name (),
246 target->order);
248 edge = edge->make_direct (target);
249 if (inline_summaries)
250 inline_update_overall_summary (node);
251 else if (edge->call_stmt)
253 edge->redirect_call_stmt_to_callee ();
255 /* Call to __builtin_unreachable shouldn't be instrumented. */
256 if (!targets.length ())
257 gimple_call_set_with_bounds (edge->call_stmt, false);
263 /* Perform reachability analysis and reclaim all unreachable nodes.
265 The algorithm is basically mark&sweep but with some extra refinements:
267 - reachable extern inline functions needs special handling; the bodies needs
268 to stay in memory until inlining in hope that they will be inlined.
269 After inlining we release their bodies and turn them into unanalyzed
270 nodes even when they are reachable.
272 - virtual functions are kept in callgraph even if they seem unreachable in
273 hope calls to them will be devirtualized.
275 Again we remove them after inlining. In late optimization some
276 devirtualization may happen, but it is not important since we won't inline
277 the call. In theory early opts and IPA should work out all important cases.
279 - virtual clones needs bodies of their origins for later materialization;
280 this means that we want to keep the body even if the origin is unreachable
281 otherwise. To avoid origin from sitting in the callgraph and being
282 walked by IPA passes, we turn them into unanalyzed nodes with body
283 defined.
285 We maintain set of function declaration where body needs to stay in
286 body_needed_for_clonning
288 Inline clones represent special case: their declaration match the
289 declaration of origin and cgraph_remove_node already knows how to
290 reshape callgraph and preserve body when offline copy of function or
291 inline clone is being removed.
293 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
294 variables with DECL_INITIAL set. We finalize these and keep reachable
295 ones around for constant folding purposes. After inlining we however
296 stop walking their references to let everything static referneced by them
297 to be removed when it is otherwise unreachable.
299 We maintain queue of both reachable symbols (i.e. defined symbols that needs
300 to stay) and symbols that are in boundary (i.e. external symbols referenced
301 by reachable symbols or origins of clones). The queue is represented
302 as linked list by AUX pointer terminated by 1.
304 At the end we keep all reachable symbols. For symbols in boundary we always
305 turn definition into a declaration, but we may keep function body around
306 based on body_needed_for_clonning
308 All symbols that enter the queue have AUX pointer non-zero and are in the
309 boundary. Pointer set REACHABLE is used to track reachable symbols.
311 Every symbol can be visited twice - once as part of boundary and once
312 as real reachable symbol. enqueue_node needs to decide whether the
313 node needs to be re-queued for second processing. For this purpose
314 we set AUX pointer of processed symbols in the boundary to constant 2. */
316 bool
317 symbol_table::remove_unreachable_nodes (FILE *file)
319 symtab_node *first = (symtab_node *) (void *) 1;
320 struct cgraph_node *node, *next;
321 varpool_node *vnode, *vnext;
322 bool changed = false;
323 hash_set<symtab_node *> reachable;
324 hash_set<tree> body_needed_for_clonning;
325 hash_set<void *> reachable_call_targets;
326 bool before_inlining_p = symtab->state < (!optimize ? IPA_SSA
327 : IPA_SSA_AFTER_INLINING);
329 timevar_push (TV_IPA_UNREACHABLE);
330 build_type_inheritance_graph ();
331 if (file)
332 fprintf (file, "\nReclaiming functions:");
333 #ifdef ENABLE_CHECKING
334 FOR_EACH_FUNCTION (node)
335 gcc_assert (!node->aux);
336 FOR_EACH_VARIABLE (vnode)
337 gcc_assert (!vnode->aux);
338 #endif
339 /* Mark functions whose bodies are obviously needed.
340 This is mostly when they can be referenced externally. Inline clones
341 are special since their declarations are shared with master clone and thus
342 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
343 FOR_EACH_FUNCTION (node)
345 node->used_as_abstract_origin = false;
346 if (node->definition
347 && !node->global.inlined_to
348 && !node->in_other_partition
349 && !node->can_remove_if_no_direct_calls_and_refs_p ())
351 gcc_assert (!node->global.inlined_to);
352 reachable.add (node);
353 enqueue_node (node, &first, &reachable);
355 else
356 gcc_assert (!node->aux);
359 /* Mark variables that are obviously needed. */
360 FOR_EACH_DEFINED_VARIABLE (vnode)
361 if (!vnode->can_remove_if_no_refs_p()
362 && !vnode->in_other_partition)
364 reachable.add (vnode);
365 enqueue_node (vnode, &first, &reachable);
368 /* Perform reachability analysis. */
369 while (first != (symtab_node *) (void *) 1)
371 bool in_boundary_p = !reachable.contains (first);
372 symtab_node *node = first;
374 first = (symtab_node *)first->aux;
376 /* If we are processing symbol in boundary, mark its AUX pointer for
377 possible later re-processing in enqueue_node. */
378 if (in_boundary_p)
380 node->aux = (void *)2;
381 if (node->alias && node->analyzed)
382 enqueue_node (node->get_alias_target (), &first, &reachable);
384 else
386 if (TREE_CODE (node->decl) == FUNCTION_DECL
387 && DECL_ABSTRACT_ORIGIN (node->decl))
389 struct cgraph_node *origin_node
390 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
391 if (origin_node && !origin_node->used_as_abstract_origin)
393 origin_node->used_as_abstract_origin = true;
394 gcc_assert (!origin_node->prev_sibling_clone);
395 gcc_assert (!origin_node->next_sibling_clone);
396 for (cgraph_node *n = origin_node->clones; n;
397 n = n->next_sibling_clone)
398 if (n->decl == DECL_ABSTRACT_ORIGIN (node->decl))
399 n->used_as_abstract_origin = true;
402 /* If any symbol in a comdat group is reachable, force
403 all externally visible symbols in the same comdat
404 group to be reachable as well. Comdat-local symbols
405 can be discarded if all uses were inlined. */
406 if (node->same_comdat_group)
408 symtab_node *next;
409 for (next = node->same_comdat_group;
410 next != node;
411 next = next->same_comdat_group)
412 if (!next->comdat_local_p ()
413 && !reachable.add (next))
414 enqueue_node (next, &first, &reachable);
416 /* Mark references as reachable. */
417 process_references (node, &first, before_inlining_p, &reachable);
420 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
422 /* Mark the callees reachable unless they are direct calls to extern
423 inline functions we decided to not inline. */
424 if (!in_boundary_p)
426 struct cgraph_edge *e;
427 /* Keep alive possible targets for devirtualization. */
428 if (opt_for_fn (cnode->decl, optimize)
429 && opt_for_fn (cnode->decl, flag_devirtualize))
431 struct cgraph_edge *next;
432 for (e = cnode->indirect_calls; e; e = next)
434 next = e->next_callee;
435 if (e->indirect_info->polymorphic)
436 walk_polymorphic_call_targets (&reachable_call_targets,
437 e, &first, &reachable,
438 before_inlining_p);
441 for (e = cnode->callees; e; e = e->next_callee)
443 symtab_node *body = e->callee->function_symbol ();
444 if (e->callee->definition
445 && !e->callee->in_other_partition
446 && (!e->inline_failed
447 || !DECL_EXTERNAL (e->callee->decl)
448 || e->callee->alias
449 || (before_inlining_p
450 && (opt_for_fn (body->decl, optimize)
451 || (symtab->state < IPA_SSA
452 && lookup_attribute
453 ("always_inline",
454 DECL_ATTRIBUTES (body->decl)))))))
456 /* Be sure that we will not optimize out alias target
457 body. */
458 if (DECL_EXTERNAL (e->callee->decl)
459 && e->callee->alias
460 && before_inlining_p)
461 reachable.add (body);
462 reachable.add (e->callee);
464 enqueue_node (e->callee, &first, &reachable);
467 /* When inline clone exists, mark body to be preserved so when removing
468 offline copy of the function we don't kill it. */
469 if (cnode->global.inlined_to)
470 body_needed_for_clonning.add (cnode->decl);
472 /* For instrumentation clones we always need original
473 function node for proper LTO privatization. */
474 if (cnode->instrumentation_clone
475 && cnode->definition)
477 gcc_assert (cnode->instrumented_version || in_lto_p);
478 if (cnode->instrumented_version)
480 enqueue_node (cnode->instrumented_version, &first,
481 &reachable);
482 reachable.add (cnode->instrumented_version);
486 /* For non-inline clones, force their origins to the boundary and ensure
487 that body is not removed. */
488 while (cnode->clone_of)
490 bool noninline = cnode->clone_of->decl != cnode->decl;
491 cnode = cnode->clone_of;
492 if (noninline)
494 body_needed_for_clonning.add (cnode->decl);
495 enqueue_node (cnode, &first, &reachable);
500 else if (cnode->thunk.thunk_p)
501 enqueue_node (cnode->callees->callee, &first, &reachable);
503 /* If any reachable function has simd clones, mark them as
504 reachable as well. */
505 if (cnode->simd_clones)
507 cgraph_node *next;
508 for (next = cnode->simd_clones;
509 next;
510 next = next->simdclone->next_clone)
511 if (in_boundary_p
512 || !reachable.add (next))
513 enqueue_node (next, &first, &reachable);
516 /* When we see constructor of external variable, keep referred nodes in the
517 boundary. This will also hold initializers of the external vars NODE
518 refers to. */
519 varpool_node *vnode = dyn_cast <varpool_node *> (node);
520 if (vnode
521 && DECL_EXTERNAL (node->decl)
522 && !vnode->alias
523 && in_boundary_p)
525 struct ipa_ref *ref = NULL;
526 for (int i = 0; node->iterate_reference (i, ref); i++)
527 enqueue_node (ref->referred, &first, &reachable);
531 /* Remove unreachable functions. */
532 for (node = first_function (); node; node = next)
534 next = next_function (node);
536 /* If node is not needed at all, remove it. */
537 if (!node->aux)
539 if (file)
540 fprintf (file, " %s/%i", node->name (), node->order);
541 node->remove ();
542 changed = true;
544 /* If node is unreachable, remove its body. */
545 else if (!reachable.contains (node))
547 /* We keep definitions of thunks and aliases in the boundary so
548 we can walk to the ultimate alias targets and function symbols
549 reliably. */
550 if (node->alias || node->thunk.thunk_p)
552 else if (!body_needed_for_clonning.contains (node->decl)
553 && !node->alias && !node->thunk.thunk_p)
554 node->release_body ();
555 else if (!node->clone_of)
556 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
557 if (node->definition && !node->alias && !node->thunk.thunk_p)
559 if (file)
560 fprintf (file, " %s/%i", node->name (), node->order);
561 node->body_removed = true;
562 node->analyzed = false;
563 node->definition = false;
564 node->cpp_implicit_alias = false;
565 node->alias = false;
566 node->thunk.thunk_p = false;
567 node->weakref = false;
568 /* After early inlining we drop always_inline attributes on
569 bodies of functions that are still referenced (have their
570 address taken). */
571 DECL_ATTRIBUTES (node->decl)
572 = remove_attribute ("always_inline",
573 DECL_ATTRIBUTES (node->decl));
574 if (!node->in_other_partition)
575 node->local.local = false;
576 node->remove_callees ();
577 node->remove_all_references ();
578 changed = true;
579 if (node->thunk.thunk_p
580 && node->thunk.add_pointer_bounds_args)
582 node->thunk.thunk_p = false;
583 node->thunk.add_pointer_bounds_args = false;
587 else
588 gcc_assert (node->clone_of || !node->has_gimple_body_p ()
589 || in_lto_p || DECL_RESULT (node->decl));
592 /* Inline clones might be kept around so their materializing allows further
593 cloning. If the function the clone is inlined into is removed, we need
594 to turn it into normal cone. */
595 FOR_EACH_FUNCTION (node)
597 if (node->global.inlined_to
598 && !node->callers)
600 gcc_assert (node->clones);
601 node->global.inlined_to = NULL;
602 update_inlined_to_pointer (node, node);
604 node->aux = NULL;
607 /* Remove unreachable variables. */
608 if (file)
609 fprintf (file, "\nReclaiming variables:");
610 for (vnode = first_variable (); vnode; vnode = vnext)
612 vnext = next_variable (vnode);
613 if (!vnode->aux
614 /* For can_refer_decl_in_current_unit_p we want to track for
615 all external variables if they are defined in other partition
616 or not. */
617 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
619 struct ipa_ref *ref = NULL;
621 /* First remove the aliases, so varpool::remove can possibly lookup
622 the constructor and save it for future use. */
623 while (vnode->iterate_direct_aliases (0, ref))
625 if (file)
626 fprintf (file, " %s/%i", ref->referred->name (),
627 ref->referred->order);
628 ref->referring->remove ();
630 if (file)
631 fprintf (file, " %s/%i", vnode->name (), vnode->order);
632 vnext = next_variable (vnode);
633 vnode->remove ();
634 changed = true;
636 else if (!reachable.contains (vnode) && !vnode->alias)
638 tree init;
639 if (vnode->definition)
641 if (file)
642 fprintf (file, " %s", vnode->name ());
643 changed = true;
645 /* Keep body if it may be useful for constant folding. */
646 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node
647 && !POINTER_BOUNDS_P (vnode->decl))
648 vnode->remove_initializer ();
649 else
650 DECL_INITIAL (vnode->decl) = init;
651 vnode->body_removed = true;
652 vnode->definition = false;
653 vnode->analyzed = false;
654 vnode->aux = NULL;
656 vnode->remove_from_same_comdat_group ();
658 vnode->remove_all_references ();
660 else
661 vnode->aux = NULL;
664 /* Now update address_taken flags and try to promote functions to be local. */
665 if (file)
666 fprintf (file, "\nClearing address taken flags:");
667 FOR_EACH_DEFINED_FUNCTION (node)
668 if (node->address_taken
669 && !node->used_from_other_partition)
671 if (!node->call_for_symbol_and_aliases
672 (has_addr_references_p, NULL, true)
673 && (!node->instrumentation_clone
674 || !node->instrumented_version
675 || !node->instrumented_version->address_taken))
677 if (file)
678 fprintf (file, " %s", node->name ());
679 node->address_taken = false;
680 changed = true;
681 if (node->local_p ())
683 node->local.local = true;
684 if (file)
685 fprintf (file, " (local)");
689 if (file)
690 fprintf (file, "\n");
692 #ifdef ENABLE_CHECKING
693 symtab_node::verify_symtab_nodes ();
694 #endif
696 /* If we removed something, perhaps profile could be improved. */
697 if (changed && optimize && inline_edge_summary_vec.exists ())
698 FOR_EACH_DEFINED_FUNCTION (node)
699 ipa_propagate_frequency (node);
701 timevar_pop (TV_IPA_UNREACHABLE);
702 return changed;
705 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
706 as needed, also clear EXPLICIT_REFS if the references to given variable
707 do not need to be explicit. */
709 void
710 process_references (varpool_node *vnode,
711 bool *written, bool *address_taken,
712 bool *read, bool *explicit_refs)
714 int i;
715 struct ipa_ref *ref;
717 if (!vnode->all_refs_explicit_p ()
718 || TREE_THIS_VOLATILE (vnode->decl))
719 *explicit_refs = false;
721 for (i = 0; vnode->iterate_referring (i, ref)
722 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
723 switch (ref->use)
725 case IPA_REF_ADDR:
726 *address_taken = true;
727 break;
728 case IPA_REF_LOAD:
729 *read = true;
730 break;
731 case IPA_REF_STORE:
732 *written = true;
733 break;
734 case IPA_REF_ALIAS:
735 process_references (dyn_cast<varpool_node *> (ref->referring), written,
736 address_taken, read, explicit_refs);
737 break;
738 case IPA_REF_CHKP:
739 gcc_unreachable ();
743 /* Set TREE_READONLY bit. */
745 bool
746 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
748 TREE_READONLY (vnode->decl) = true;
749 return false;
752 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
754 bool
755 set_writeonly_bit (varpool_node *vnode, void *data)
757 vnode->writeonly = true;
758 if (optimize)
760 DECL_INITIAL (vnode->decl) = NULL;
761 if (!vnode->alias)
763 if (vnode->num_references ())
764 *(bool *)data = true;
765 vnode->remove_all_references ();
768 return false;
771 /* Clear addressale bit of VNODE. */
773 bool
774 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
776 vnode->address_taken = false;
777 TREE_ADDRESSABLE (vnode->decl) = 0;
778 return false;
781 /* Discover variables that have no longer address taken or that are read only
782 and update their flags.
784 Return true when unreachable symbol removan should be done.
786 FIXME: This can not be done in between gimplify and omp_expand since
787 readonly flag plays role on what is shared and what is not. Currently we do
788 this transformation as part of whole program visibility and re-do at
789 ipa-reference pass (to take into account clonning), but it would
790 make sense to do it before early optimizations. */
792 bool
793 ipa_discover_readonly_nonaddressable_vars (void)
795 bool remove_p = false;
796 varpool_node *vnode;
797 if (dump_file)
798 fprintf (dump_file, "Clearing variable flags:");
799 FOR_EACH_VARIABLE (vnode)
800 if (!vnode->alias
801 && (TREE_ADDRESSABLE (vnode->decl)
802 || !vnode->writeonly
803 || !TREE_READONLY (vnode->decl)))
805 bool written = false;
806 bool address_taken = false;
807 bool read = false;
808 bool explicit_refs = true;
810 process_references (vnode, &written, &address_taken, &read,
811 &explicit_refs);
812 if (!explicit_refs)
813 continue;
814 if (!address_taken)
816 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
817 fprintf (dump_file, " %s (non-addressable)", vnode->name ());
818 vnode->call_for_symbol_and_aliases (clear_addressable_bit, NULL,
819 true);
821 if (!address_taken && !written
822 /* Making variable in explicit section readonly can cause section
823 type conflict.
824 See e.g. gcc.c-torture/compile/pr23237.c */
825 && vnode->get_section () == NULL)
827 if (!TREE_READONLY (vnode->decl) && dump_file)
828 fprintf (dump_file, " %s (read-only)", vnode->name ());
829 vnode->call_for_symbol_and_aliases (set_readonly_bit, NULL, true);
831 if (!vnode->writeonly && !read && !address_taken && written)
833 if (dump_file)
834 fprintf (dump_file, " %s (write-only)", vnode->name ());
835 vnode->call_for_symbol_and_aliases (set_writeonly_bit, &remove_p,
836 true);
839 if (dump_file)
840 fprintf (dump_file, "\n");
841 return remove_p;
844 /* Free inline summary. */
846 namespace {
848 const pass_data pass_data_ipa_free_inline_summary =
850 SIMPLE_IPA_PASS, /* type */
851 "free-inline-summary", /* name */
852 OPTGROUP_NONE, /* optinfo_flags */
853 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
854 0, /* properties_required */
855 0, /* properties_provided */
856 0, /* properties_destroyed */
857 0, /* todo_flags_start */
858 /* Early optimizations may make function unreachable. We can not
859 remove unreachable functions as part of the ealry opts pass because
860 TODOs are run before subpasses. Do it here. */
861 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
864 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
866 public:
867 pass_ipa_free_inline_summary (gcc::context *ctxt)
868 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
871 /* opt_pass methods: */
872 virtual unsigned int execute (function *)
874 inline_free_summary ();
875 return 0;
878 }; // class pass_ipa_free_inline_summary
880 } // anon namespace
882 simple_ipa_opt_pass *
883 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
885 return new pass_ipa_free_inline_summary (ctxt);
888 /* Generate and emit a static constructor or destructor. WHICH must
889 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
890 (for chp static vars constructor) or 'B' (for chkp static bounds
891 constructor). BODY is a STATEMENT_LIST containing GENERIC
892 statements. PRIORITY is the initialization priority for this
893 constructor or destructor.
895 FINAL specify whether the externally visible name for collect2 should
896 be produced. */
898 static void
899 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
901 static int counter = 0;
902 char which_buf[16];
903 tree decl, name, resdecl;
905 /* The priority is encoded in the constructor or destructor name.
906 collect2 will sort the names and arrange that they are called at
907 program startup. */
908 if (final)
909 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
910 else
911 /* Proudce sane name but one not recognizable by collect2, just for the
912 case we fail to inline the function. */
913 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
914 name = get_file_function_name (which_buf);
916 decl = build_decl (input_location, FUNCTION_DECL, name,
917 build_function_type_list (void_type_node, NULL_TREE));
918 current_function_decl = decl;
920 resdecl = build_decl (input_location,
921 RESULT_DECL, NULL_TREE, void_type_node);
922 DECL_ARTIFICIAL (resdecl) = 1;
923 DECL_RESULT (decl) = resdecl;
924 DECL_CONTEXT (resdecl) = decl;
926 allocate_struct_function (decl, false);
928 TREE_STATIC (decl) = 1;
929 TREE_USED (decl) = 1;
930 DECL_ARTIFICIAL (decl) = 1;
931 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
932 DECL_SAVED_TREE (decl) = body;
933 if (!targetm.have_ctors_dtors && final)
935 TREE_PUBLIC (decl) = 1;
936 DECL_PRESERVE_P (decl) = 1;
938 DECL_UNINLINABLE (decl) = 1;
940 DECL_INITIAL (decl) = make_node (BLOCK);
941 TREE_USED (DECL_INITIAL (decl)) = 1;
943 DECL_SOURCE_LOCATION (decl) = input_location;
944 cfun->function_end_locus = input_location;
946 switch (which)
948 case 'I':
949 DECL_STATIC_CONSTRUCTOR (decl) = 1;
950 decl_init_priority_insert (decl, priority);
951 break;
952 case 'P':
953 DECL_STATIC_CONSTRUCTOR (decl) = 1;
954 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("chkp ctor"),
955 NULL,
956 NULL_TREE);
957 decl_init_priority_insert (decl, priority);
958 break;
959 case 'B':
960 DECL_STATIC_CONSTRUCTOR (decl) = 1;
961 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("bnd_legacy"),
962 NULL,
963 NULL_TREE);
964 decl_init_priority_insert (decl, priority);
965 break;
966 case 'D':
967 DECL_STATIC_DESTRUCTOR (decl) = 1;
968 decl_fini_priority_insert (decl, priority);
969 break;
970 default:
971 gcc_unreachable ();
974 gimplify_function_tree (decl);
976 cgraph_node::add_new_function (decl, false);
978 set_cfun (NULL);
979 current_function_decl = NULL;
982 /* Generate and emit a static constructor or destructor. WHICH must
983 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
984 (for chkp static vars constructor) or 'B' (for chkp static bounds
985 constructor). BODY is a STATEMENT_LIST containing GENERIC
986 statements. PRIORITY is the initialization priority for this
987 constructor or destructor. */
989 void
990 cgraph_build_static_cdtor (char which, tree body, int priority)
992 cgraph_build_static_cdtor_1 (which, body, priority, false);
995 /* A vector of FUNCTION_DECLs declared as static constructors. */
996 static vec<tree> static_ctors;
997 /* A vector of FUNCTION_DECLs declared as static destructors. */
998 static vec<tree> static_dtors;
1000 /* When target does not have ctors and dtors, we call all constructor
1001 and destructor by special initialization/destruction function
1002 recognized by collect2.
1004 When we are going to build this function, collect all constructors and
1005 destructors and turn them into normal functions. */
1007 static void
1008 record_cdtor_fn (struct cgraph_node *node)
1010 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1011 static_ctors.safe_push (node->decl);
1012 if (DECL_STATIC_DESTRUCTOR (node->decl))
1013 static_dtors.safe_push (node->decl);
1014 node = cgraph_node::get (node->decl);
1015 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
1018 /* Define global constructors/destructor functions for the CDTORS, of
1019 which they are LEN. The CDTORS are sorted by initialization
1020 priority. If CTOR_P is true, these are constructors; otherwise,
1021 they are destructors. */
1023 static void
1024 build_cdtor (bool ctor_p, vec<tree> cdtors)
1026 size_t i,j;
1027 size_t len = cdtors.length ();
1029 i = 0;
1030 while (i < len)
1032 tree body;
1033 tree fn;
1034 priority_type priority;
1036 priority = 0;
1037 body = NULL_TREE;
1038 j = i;
1041 priority_type p;
1042 fn = cdtors[j];
1043 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1044 if (j == i)
1045 priority = p;
1046 else if (p != priority)
1047 break;
1048 j++;
1050 while (j < len);
1052 /* When there is only one cdtor and target supports them, do nothing. */
1053 if (j == i + 1
1054 && targetm.have_ctors_dtors)
1056 i++;
1057 continue;
1059 /* Find the next batch of constructors/destructors with the same
1060 initialization priority. */
1061 for (;i < j; i++)
1063 tree call;
1064 fn = cdtors[i];
1065 call = build_call_expr (fn, 0);
1066 if (ctor_p)
1067 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1068 else
1069 DECL_STATIC_DESTRUCTOR (fn) = 0;
1070 /* We do not want to optimize away pure/const calls here.
1071 When optimizing, these should be already removed, when not
1072 optimizing, we want user to be able to breakpoint in them. */
1073 TREE_SIDE_EFFECTS (call) = 1;
1074 append_to_statement_list (call, &body);
1076 gcc_assert (body != NULL_TREE);
1077 /* Generate a function to call all the function of like
1078 priority. */
1079 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1083 /* Comparison function for qsort. P1 and P2 are actually of type
1084 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1085 used to determine the sort order. */
1087 static int
1088 compare_ctor (const void *p1, const void *p2)
1090 tree f1;
1091 tree f2;
1092 int priority1;
1093 int priority2;
1095 f1 = *(const tree *)p1;
1096 f2 = *(const tree *)p2;
1097 priority1 = DECL_INIT_PRIORITY (f1);
1098 priority2 = DECL_INIT_PRIORITY (f2);
1100 if (priority1 < priority2)
1101 return -1;
1102 else if (priority1 > priority2)
1103 return 1;
1104 else
1105 /* Ensure a stable sort. Constructors are executed in backwarding
1106 order to make LTO initialize braries first. */
1107 return DECL_UID (f2) - DECL_UID (f1);
1110 /* Comparison function for qsort. P1 and P2 are actually of type
1111 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1112 used to determine the sort order. */
1114 static int
1115 compare_dtor (const void *p1, const void *p2)
1117 tree f1;
1118 tree f2;
1119 int priority1;
1120 int priority2;
1122 f1 = *(const tree *)p1;
1123 f2 = *(const tree *)p2;
1124 priority1 = DECL_FINI_PRIORITY (f1);
1125 priority2 = DECL_FINI_PRIORITY (f2);
1127 if (priority1 < priority2)
1128 return -1;
1129 else if (priority1 > priority2)
1130 return 1;
1131 else
1132 /* Ensure a stable sort. */
1133 return DECL_UID (f1) - DECL_UID (f2);
1136 /* Generate functions to call static constructors and destructors
1137 for targets that do not support .ctors/.dtors sections. These
1138 functions have magic names which are detected by collect2. */
1140 static void
1141 build_cdtor_fns (void)
1143 if (!static_ctors.is_empty ())
1145 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1146 static_ctors.qsort (compare_ctor);
1147 build_cdtor (/*ctor_p=*/true, static_ctors);
1150 if (!static_dtors.is_empty ())
1152 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1153 static_dtors.qsort (compare_dtor);
1154 build_cdtor (/*ctor_p=*/false, static_dtors);
1158 /* Look for constructors and destructors and produce function calling them.
1159 This is needed for targets not supporting ctors or dtors, but we perform the
1160 transformation also at linktime to merge possibly numerous
1161 constructors/destructors into single function to improve code locality and
1162 reduce size. */
1164 static unsigned int
1165 ipa_cdtor_merge (void)
1167 struct cgraph_node *node;
1168 FOR_EACH_DEFINED_FUNCTION (node)
1169 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1170 || DECL_STATIC_DESTRUCTOR (node->decl))
1171 record_cdtor_fn (node);
1172 build_cdtor_fns ();
1173 static_ctors.release ();
1174 static_dtors.release ();
1175 return 0;
1178 namespace {
1180 const pass_data pass_data_ipa_cdtor_merge =
1182 IPA_PASS, /* type */
1183 "cdtor", /* name */
1184 OPTGROUP_NONE, /* optinfo_flags */
1185 TV_CGRAPHOPT, /* tv_id */
1186 0, /* properties_required */
1187 0, /* properties_provided */
1188 0, /* properties_destroyed */
1189 0, /* todo_flags_start */
1190 0, /* todo_flags_finish */
1193 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1195 public:
1196 pass_ipa_cdtor_merge (gcc::context *ctxt)
1197 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1198 NULL, /* generate_summary */
1199 NULL, /* write_summary */
1200 NULL, /* read_summary */
1201 NULL, /* write_optimization_summary */
1202 NULL, /* read_optimization_summary */
1203 NULL, /* stmt_fixup */
1204 0, /* function_transform_todo_flags_start */
1205 NULL, /* function_transform */
1206 NULL) /* variable_transform */
1209 /* opt_pass methods: */
1210 virtual bool gate (function *);
1211 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1213 }; // class pass_ipa_cdtor_merge
1215 bool
1216 pass_ipa_cdtor_merge::gate (function *)
1218 /* Perform the pass when we have no ctors/dtors support
1219 or at LTO time to merge multiple constructors into single
1220 function. */
1221 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1224 } // anon namespace
1226 ipa_opt_pass_d *
1227 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1229 return new pass_ipa_cdtor_merge (ctxt);
1232 /* Invalid pointer representing BOTTOM for single user dataflow. */
1233 #define BOTTOM ((cgraph_node *)(size_t) 2)
1235 /* Meet operation for single user dataflow.
1236 Here we want to associate variables with sigle function that may access it.
1238 FUNCTION is current single user of a variable, VAR is variable that uses it.
1239 Latttice is stored in SINGLE_USER_MAP.
1241 We represent:
1242 - TOP by no entry in SIGNLE_USER_MAP
1243 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1244 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1246 cgraph_node *
1247 meet (cgraph_node *function, varpool_node *var,
1248 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1250 struct cgraph_node *user, **f;
1252 if (var->aux == BOTTOM)
1253 return BOTTOM;
1255 f = single_user_map.get (var);
1256 if (!f)
1257 return function;
1258 user = *f;
1259 if (!function)
1260 return user;
1261 else if (function != user)
1262 return BOTTOM;
1263 else
1264 return function;
1267 /* Propagation step of single-use dataflow.
1269 Check all uses of VNODE and see if they are used by single function FUNCTION.
1270 SINGLE_USER_MAP represents the dataflow lattice. */
1272 cgraph_node *
1273 propagate_single_user (varpool_node *vnode, cgraph_node *function,
1274 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1276 int i;
1277 struct ipa_ref *ref;
1279 gcc_assert (!vnode->externally_visible);
1281 /* If node is an alias, first meet with its target. */
1282 if (vnode->alias)
1283 function = meet (function, vnode->get_alias_target (), single_user_map);
1285 /* Check all users and see if they correspond to a single function. */
1286 for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
1288 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
1289 if (cnode)
1291 if (cnode->global.inlined_to)
1292 cnode = cnode->global.inlined_to;
1293 if (!function)
1294 function = cnode;
1295 else if (function != cnode)
1296 function = BOTTOM;
1298 else
1299 function = meet (function, dyn_cast <varpool_node *> (ref->referring),
1300 single_user_map);
1302 return function;
1305 /* Pass setting used_by_single_function flag.
1306 This flag is set on variable when there is only one function that may
1307 possibly referr to it. */
1309 static unsigned int
1310 ipa_single_use (void)
1312 varpool_node *first = (varpool_node *) (void *) 1;
1313 varpool_node *var;
1314 hash_map<varpool_node *, cgraph_node *> single_user_map;
1316 FOR_EACH_DEFINED_VARIABLE (var)
1317 if (!var->all_refs_explicit_p ())
1318 var->aux = BOTTOM;
1319 else
1321 /* Enqueue symbol for dataflow. */
1322 var->aux = first;
1323 first = var;
1326 /* The actual dataflow. */
1328 while (first != (void *) 1)
1330 cgraph_node *user, *orig_user, **f;
1332 var = first;
1333 first = (varpool_node *)first->aux;
1335 f = single_user_map.get (var);
1336 if (f)
1337 orig_user = *f;
1338 else
1339 orig_user = NULL;
1340 user = propagate_single_user (var, orig_user, single_user_map);
1342 gcc_checking_assert (var->aux != BOTTOM);
1344 /* If user differs, enqueue all references. */
1345 if (user != orig_user)
1347 unsigned int i;
1348 ipa_ref *ref;
1350 single_user_map.put (var, user);
1352 /* Enqueue all aliases for re-processing. */
1353 for (i = 0; var->iterate_direct_aliases (i, ref); i++)
1354 if (!ref->referring->aux)
1356 ref->referring->aux = first;
1357 first = dyn_cast <varpool_node *> (ref->referring);
1359 /* Enqueue all users for re-processing. */
1360 for (i = 0; var->iterate_reference (i, ref); i++)
1361 if (!ref->referred->aux
1362 && ref->referred->definition
1363 && is_a <varpool_node *> (ref->referred))
1365 ref->referred->aux = first;
1366 first = dyn_cast <varpool_node *> (ref->referred);
1369 /* If user is BOTTOM, just punt on this var. */
1370 if (user == BOTTOM)
1371 var->aux = BOTTOM;
1372 else
1373 var->aux = NULL;
1375 else
1376 var->aux = NULL;
1379 FOR_EACH_DEFINED_VARIABLE (var)
1381 if (var->aux != BOTTOM)
1383 #ifdef ENABLE_CHECKING
1384 /* Not having the single user known means that the VAR is
1385 unreachable. Either someone forgot to remove unreachable
1386 variables or the reachability here is wrong. */
1388 gcc_assert (single_user_map.get (var));
1389 #endif
1390 if (dump_file)
1392 fprintf (dump_file, "Variable %s/%i is used by single function\n",
1393 var->name (), var->order);
1395 var->used_by_single_function = true;
1397 var->aux = NULL;
1399 return 0;
1402 namespace {
1404 const pass_data pass_data_ipa_single_use =
1406 IPA_PASS, /* type */
1407 "single-use", /* name */
1408 OPTGROUP_NONE, /* optinfo_flags */
1409 TV_CGRAPHOPT, /* tv_id */
1410 0, /* properties_required */
1411 0, /* properties_provided */
1412 0, /* properties_destroyed */
1413 0, /* todo_flags_start */
1414 0, /* todo_flags_finish */
1417 class pass_ipa_single_use : public ipa_opt_pass_d
1419 public:
1420 pass_ipa_single_use (gcc::context *ctxt)
1421 : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
1422 NULL, /* generate_summary */
1423 NULL, /* write_summary */
1424 NULL, /* read_summary */
1425 NULL, /* write_optimization_summary */
1426 NULL, /* read_optimization_summary */
1427 NULL, /* stmt_fixup */
1428 0, /* function_transform_todo_flags_start */
1429 NULL, /* function_transform */
1430 NULL) /* variable_transform */
1433 /* opt_pass methods: */
1434 virtual bool gate (function *);
1435 virtual unsigned int execute (function *) { return ipa_single_use (); }
1437 }; // class pass_ipa_single_use
1439 bool
1440 pass_ipa_single_use::gate (function *)
1442 return optimize;
1445 } // anon namespace
1447 ipa_opt_pass_d *
1448 make_pass_ipa_single_use (gcc::context *ctxt)
1450 return new pass_ipa_single_use (ctxt);