[Patch AArch64 1/3] Enable CRC by default for armv8.1-a
[official-gcc.git] / gcc / ipa.c
blob6722d3b806ed55b0f1aeaeeb1a612bfbbaa6a73e
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 *)
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 /* Return true when NODE can be target of an indirect call. */
57 static bool
58 is_indirect_call_target_p (struct cgraph_node *node, void *)
60 return node->indirect_call_target;
63 /* Look for all functions inlined to NODE and update their inlined_to pointers
64 to INLINED_TO. */
66 static void
67 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
69 struct cgraph_edge *e;
70 for (e = node->callees; e; e = e->next_callee)
71 if (e->callee->global.inlined_to)
73 e->callee->global.inlined_to = inlined_to;
74 update_inlined_to_pointer (e->callee, inlined_to);
78 /* Add symtab NODE to queue starting at FIRST.
80 The queue is linked via AUX pointers and terminated by pointer to 1.
81 We enqueue nodes at two occasions: when we find them reachable or when we find
82 their bodies needed for further clonning. In the second case we mark them
83 by pointer to 2 after processing so they are re-queue when they become
84 reachable. */
86 static void
87 enqueue_node (symtab_node *node, symtab_node **first,
88 hash_set<symtab_node *> *reachable)
90 /* Node is still in queue; do nothing. */
91 if (node->aux && node->aux != (void *) 2)
92 return;
93 /* Node was already processed as unreachable, re-enqueue
94 only if it became reachable now. */
95 if (node->aux == (void *)2 && !reachable->contains (node))
96 return;
97 node->aux = *first;
98 *first = node;
101 /* Process references. */
103 static void
104 process_references (symtab_node *snode,
105 symtab_node **first,
106 bool before_inlining_p,
107 hash_set<symtab_node *> *reachable)
109 int i;
110 struct ipa_ref *ref = NULL;
111 for (i = 0; snode->iterate_reference (i, ref); i++)
113 symtab_node *node = ref->referred;
114 symtab_node *body = node->ultimate_alias_target ();
116 if (node->definition && !node->in_other_partition
117 && ((!DECL_EXTERNAL (node->decl) || node->alias)
118 || (((before_inlining_p
119 && ((TREE_CODE (node->decl) != FUNCTION_DECL
120 && optimize)
121 || (TREE_CODE (node->decl) == FUNCTION_DECL
122 && opt_for_fn (body->decl, optimize))
123 || (symtab->state < IPA_SSA
124 && lookup_attribute
125 ("always_inline",
126 DECL_ATTRIBUTES (body->decl))))))
127 /* We use variable constructors during late compilation for
128 constant folding. Keep references alive so partitioning
129 knows about potential references. */
130 || (TREE_CODE (node->decl) == VAR_DECL
131 && flag_wpa
132 && ctor_for_folding (node->decl)
133 != error_mark_node))))
135 /* Be sure that we will not optimize out alias target
136 body. */
137 if (DECL_EXTERNAL (node->decl)
138 && node->alias
139 && before_inlining_p)
140 reachable->add (body);
141 reachable->add (node);
143 enqueue_node (node, first, reachable);
147 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
148 all its potential targets as reachable to permit later inlining if
149 devirtualization happens. After inlining still keep their declarations
150 around, so we can devirtualize to a direct call.
152 Also try to make trivial devirutalization when no or only one target is
153 possible. */
155 static void
156 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
157 struct cgraph_edge *edge,
158 symtab_node **first,
159 hash_set<symtab_node *> *reachable,
160 bool before_inlining_p)
162 unsigned int i;
163 void *cache_token;
164 bool final;
165 vec <cgraph_node *>targets
166 = possible_polymorphic_call_targets
167 (edge, &final, &cache_token);
169 if (!reachable_call_targets->add (cache_token))
171 for (i = 0; i < targets.length (); i++)
173 struct cgraph_node *n = targets[i];
175 /* Do not bother to mark virtual methods in anonymous namespace;
176 either we will find use of virtual table defining it, or it is
177 unused. */
178 if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
179 && type_in_anonymous_namespace_p
180 (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl))))
181 continue;
183 n->indirect_call_target = true;
184 symtab_node *body = n->function_symbol ();
186 /* Prior inlining, keep alive bodies of possible targets for
187 devirtualization. */
188 if (n->definition
189 && (before_inlining_p
190 && opt_for_fn (body->decl, optimize)
191 && opt_for_fn (body->decl, flag_devirtualize)))
193 /* Be sure that we will not optimize out alias target
194 body. */
195 if (DECL_EXTERNAL (n->decl)
196 && n->alias
197 && before_inlining_p)
198 reachable->add (body);
199 reachable->add (n);
201 /* Even after inlining we want to keep the possible targets in the
202 boundary, so late passes can still produce direct call even if
203 the chance for inlining is lost. */
204 enqueue_node (n, first, reachable);
208 /* Very trivial devirtualization; when the type is
209 final or anonymous (so we know all its derivation)
210 and there is only one possible virtual call target,
211 make the edge direct. */
212 if (final)
214 if (targets.length () <= 1 && dbg_cnt (devirt))
216 cgraph_node *target, *node = edge->caller;
217 if (targets.length () == 1)
218 target = targets[0];
219 else
220 target = cgraph_node::get_create
221 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
223 if (dump_enabled_p ())
225 location_t locus;
226 if (edge->call_stmt)
227 locus = gimple_location (edge->call_stmt);
228 else
229 locus = UNKNOWN_LOCATION;
230 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
231 "devirtualizing call in %s/%i to %s/%i\n",
232 edge->caller->name (), edge->caller->order,
233 target->name (),
234 target->order);
236 edge = edge->make_direct (target);
237 if (inline_summaries)
238 inline_update_overall_summary (node);
239 else if (edge->call_stmt)
241 edge->redirect_call_stmt_to_callee ();
243 /* Call to __builtin_unreachable shouldn't be instrumented. */
244 if (!targets.length ())
245 gimple_call_set_with_bounds (edge->call_stmt, false);
251 /* Perform reachability analysis and reclaim all unreachable nodes.
253 The algorithm is basically mark&sweep but with some extra refinements:
255 - reachable extern inline functions needs special handling; the bodies needs
256 to stay in memory until inlining in hope that they will be inlined.
257 After inlining we release their bodies and turn them into unanalyzed
258 nodes even when they are reachable.
260 - virtual functions are kept in callgraph even if they seem unreachable in
261 hope calls to them will be devirtualized.
263 Again we remove them after inlining. In late optimization some
264 devirtualization may happen, but it is not important since we won't inline
265 the call. In theory early opts and IPA should work out all important cases.
267 - virtual clones needs bodies of their origins for later materialization;
268 this means that we want to keep the body even if the origin is unreachable
269 otherwise. To avoid origin from sitting in the callgraph and being
270 walked by IPA passes, we turn them into unanalyzed nodes with body
271 defined.
273 We maintain set of function declaration where body needs to stay in
274 body_needed_for_clonning
276 Inline clones represent special case: their declaration match the
277 declaration of origin and cgraph_remove_node already knows how to
278 reshape callgraph and preserve body when offline copy of function or
279 inline clone is being removed.
281 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
282 variables with DECL_INITIAL set. We finalize these and keep reachable
283 ones around for constant folding purposes. After inlining we however
284 stop walking their references to let everything static referneced by them
285 to be removed when it is otherwise unreachable.
287 We maintain queue of both reachable symbols (i.e. defined symbols that needs
288 to stay) and symbols that are in boundary (i.e. external symbols referenced
289 by reachable symbols or origins of clones). The queue is represented
290 as linked list by AUX pointer terminated by 1.
292 At the end we keep all reachable symbols. For symbols in boundary we always
293 turn definition into a declaration, but we may keep function body around
294 based on body_needed_for_clonning
296 All symbols that enter the queue have AUX pointer non-zero and are in the
297 boundary. Pointer set REACHABLE is used to track reachable symbols.
299 Every symbol can be visited twice - once as part of boundary and once
300 as real reachable symbol. enqueue_node needs to decide whether the
301 node needs to be re-queued for second processing. For this purpose
302 we set AUX pointer of processed symbols in the boundary to constant 2. */
304 bool
305 symbol_table::remove_unreachable_nodes (FILE *file)
307 symtab_node *first = (symtab_node *) (void *) 1;
308 struct cgraph_node *node, *next;
309 varpool_node *vnode, *vnext;
310 bool changed = false;
311 hash_set<symtab_node *> reachable;
312 hash_set<tree> body_needed_for_clonning;
313 hash_set<void *> reachable_call_targets;
314 bool before_inlining_p = symtab->state < (!optimize ? IPA_SSA
315 : IPA_SSA_AFTER_INLINING);
317 timevar_push (TV_IPA_UNREACHABLE);
318 build_type_inheritance_graph ();
319 if (file)
320 fprintf (file, "\nReclaiming functions:");
321 if (flag_checking)
323 FOR_EACH_FUNCTION (node)
324 gcc_assert (!node->aux);
325 FOR_EACH_VARIABLE (vnode)
326 gcc_assert (!vnode->aux);
328 /* Mark functions whose bodies are obviously needed.
329 This is mostly when they can be referenced externally. Inline clones
330 are special since their declarations are shared with master clone and thus
331 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
332 FOR_EACH_FUNCTION (node)
334 node->used_as_abstract_origin = false;
335 node->indirect_call_target = false;
336 if (node->definition
337 && !node->global.inlined_to
338 && !node->in_other_partition
339 && !node->can_remove_if_no_direct_calls_and_refs_p ())
341 gcc_assert (!node->global.inlined_to);
342 reachable.add (node);
343 enqueue_node (node, &first, &reachable);
345 else
346 gcc_assert (!node->aux);
349 /* Mark variables that are obviously needed. */
350 FOR_EACH_DEFINED_VARIABLE (vnode)
351 if (!vnode->can_remove_if_no_refs_p()
352 && !vnode->in_other_partition)
354 reachable.add (vnode);
355 enqueue_node (vnode, &first, &reachable);
358 /* Perform reachability analysis. */
359 while (first != (symtab_node *) (void *) 1)
361 bool in_boundary_p = !reachable.contains (first);
362 symtab_node *node = first;
364 first = (symtab_node *)first->aux;
366 /* If we are processing symbol in boundary, mark its AUX pointer for
367 possible later re-processing in enqueue_node. */
368 if (in_boundary_p)
370 node->aux = (void *)2;
371 if (node->alias && node->analyzed)
372 enqueue_node (node->get_alias_target (), &first, &reachable);
374 else
376 if (TREE_CODE (node->decl) == FUNCTION_DECL
377 && DECL_ABSTRACT_ORIGIN (node->decl))
379 struct cgraph_node *origin_node
380 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
381 if (origin_node && !origin_node->used_as_abstract_origin)
383 origin_node->used_as_abstract_origin = true;
384 gcc_assert (!origin_node->prev_sibling_clone);
385 gcc_assert (!origin_node->next_sibling_clone);
386 for (cgraph_node *n = origin_node->clones; n;
387 n = n->next_sibling_clone)
388 if (n->decl == DECL_ABSTRACT_ORIGIN (node->decl))
389 n->used_as_abstract_origin = true;
392 /* If any symbol in a comdat group is reachable, force
393 all externally visible symbols in the same comdat
394 group to be reachable as well. Comdat-local symbols
395 can be discarded if all uses were inlined. */
396 if (node->same_comdat_group)
398 symtab_node *next;
399 for (next = node->same_comdat_group;
400 next != node;
401 next = next->same_comdat_group)
402 if (!next->comdat_local_p ()
403 && !reachable.add (next))
404 enqueue_node (next, &first, &reachable);
406 /* Mark references as reachable. */
407 process_references (node, &first, before_inlining_p, &reachable);
410 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
412 /* Mark the callees reachable unless they are direct calls to extern
413 inline functions we decided to not inline. */
414 if (!in_boundary_p)
416 struct cgraph_edge *e;
417 /* Keep alive possible targets for devirtualization. */
418 if (opt_for_fn (cnode->decl, optimize)
419 && opt_for_fn (cnode->decl, flag_devirtualize))
421 struct cgraph_edge *next;
422 for (e = cnode->indirect_calls; e; e = next)
424 next = e->next_callee;
425 if (e->indirect_info->polymorphic)
426 walk_polymorphic_call_targets (&reachable_call_targets,
427 e, &first, &reachable,
428 before_inlining_p);
431 for (e = cnode->callees; e; e = e->next_callee)
433 symtab_node *body = e->callee->function_symbol ();
434 if (e->callee->definition
435 && !e->callee->in_other_partition
436 && (!e->inline_failed
437 || !DECL_EXTERNAL (e->callee->decl)
438 || e->callee->alias
439 || (before_inlining_p
440 && (opt_for_fn (body->decl, optimize)
441 || (symtab->state < IPA_SSA
442 && lookup_attribute
443 ("always_inline",
444 DECL_ATTRIBUTES (body->decl)))))))
446 /* Be sure that we will not optimize out alias target
447 body. */
448 if (DECL_EXTERNAL (e->callee->decl)
449 && e->callee->alias
450 && before_inlining_p)
451 reachable.add (body);
452 reachable.add (e->callee);
454 enqueue_node (e->callee, &first, &reachable);
457 /* When inline clone exists, mark body to be preserved so when removing
458 offline copy of the function we don't kill it. */
459 if (cnode->global.inlined_to)
460 body_needed_for_clonning.add (cnode->decl);
462 /* For instrumentation clones we always need original
463 function node for proper LTO privatization. */
464 if (cnode->instrumentation_clone
465 && cnode->definition)
467 gcc_assert (cnode->instrumented_version || in_lto_p);
468 if (cnode->instrumented_version)
470 enqueue_node (cnode->instrumented_version, &first,
471 &reachable);
472 reachable.add (cnode->instrumented_version);
476 /* For non-inline clones, force their origins to the boundary and ensure
477 that body is not removed. */
478 while (cnode->clone_of)
480 bool noninline = cnode->clone_of->decl != cnode->decl;
481 cnode = cnode->clone_of;
482 if (noninline)
484 body_needed_for_clonning.add (cnode->decl);
485 enqueue_node (cnode, &first, &reachable);
490 else if (cnode->thunk.thunk_p)
491 enqueue_node (cnode->callees->callee, &first, &reachable);
493 /* If any reachable function has simd clones, mark them as
494 reachable as well. */
495 if (cnode->simd_clones)
497 cgraph_node *next;
498 for (next = cnode->simd_clones;
499 next;
500 next = next->simdclone->next_clone)
501 if (in_boundary_p
502 || !reachable.add (next))
503 enqueue_node (next, &first, &reachable);
506 /* When we see constructor of external variable, keep referred nodes in the
507 boundary. This will also hold initializers of the external vars NODE
508 refers to. */
509 varpool_node *vnode = dyn_cast <varpool_node *> (node);
510 if (vnode
511 && DECL_EXTERNAL (node->decl)
512 && !vnode->alias
513 && in_boundary_p)
515 struct ipa_ref *ref = NULL;
516 for (int i = 0; node->iterate_reference (i, ref); i++)
517 enqueue_node (ref->referred, &first, &reachable);
521 /* Remove unreachable functions. */
522 for (node = first_function (); node; node = next)
524 next = next_function (node);
526 /* If node is not needed at all, remove it. */
527 if (!node->aux)
529 if (file)
530 fprintf (file, " %s/%i", node->name (), node->order);
531 node->remove ();
532 changed = true;
534 /* If node is unreachable, remove its body. */
535 else if (!reachable.contains (node))
537 /* We keep definitions of thunks and aliases in the boundary so
538 we can walk to the ultimate alias targets and function symbols
539 reliably. */
540 if (node->alias || node->thunk.thunk_p)
542 else if (!body_needed_for_clonning.contains (node->decl)
543 && !node->alias && !node->thunk.thunk_p)
544 node->release_body ();
545 else if (!node->clone_of)
546 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
547 if (node->definition && !node->alias && !node->thunk.thunk_p)
549 if (file)
550 fprintf (file, " %s/%i", node->name (), node->order);
551 node->body_removed = true;
552 node->analyzed = false;
553 node->definition = false;
554 node->cpp_implicit_alias = false;
555 node->alias = false;
556 node->transparent_alias = false;
557 node->thunk.thunk_p = false;
558 node->weakref = false;
559 /* After early inlining we drop always_inline attributes on
560 bodies of functions that are still referenced (have their
561 address taken). */
562 DECL_ATTRIBUTES (node->decl)
563 = remove_attribute ("always_inline",
564 DECL_ATTRIBUTES (node->decl));
565 if (!node->in_other_partition)
566 node->local.local = false;
567 node->remove_callees ();
568 node->remove_all_references ();
569 changed = true;
570 if (node->thunk.thunk_p
571 && node->thunk.add_pointer_bounds_args)
573 node->thunk.thunk_p = false;
574 node->thunk.add_pointer_bounds_args = false;
578 else
579 gcc_assert (node->clone_of || !node->has_gimple_body_p ()
580 || in_lto_p || DECL_RESULT (node->decl));
583 /* Inline clones might be kept around so their materializing allows further
584 cloning. If the function the clone is inlined into is removed, we need
585 to turn it into normal cone. */
586 FOR_EACH_FUNCTION (node)
588 if (node->global.inlined_to
589 && !node->callers)
591 gcc_assert (node->clones);
592 node->global.inlined_to = NULL;
593 update_inlined_to_pointer (node, node);
595 node->aux = NULL;
598 /* Remove unreachable variables. */
599 if (file)
600 fprintf (file, "\nReclaiming variables:");
601 for (vnode = first_variable (); vnode; vnode = vnext)
603 vnext = next_variable (vnode);
604 if (!vnode->aux
605 /* For can_refer_decl_in_current_unit_p we want to track for
606 all external variables if they are defined in other partition
607 or not. */
608 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
610 struct ipa_ref *ref = NULL;
612 /* First remove the aliases, so varpool::remove can possibly lookup
613 the constructor and save it for future use. */
614 while (vnode->iterate_direct_aliases (0, ref))
616 if (file)
617 fprintf (file, " %s/%i", ref->referred->name (),
618 ref->referred->order);
619 ref->referring->remove ();
621 if (file)
622 fprintf (file, " %s/%i", vnode->name (), vnode->order);
623 vnext = next_variable (vnode);
624 vnode->remove ();
625 changed = true;
627 else if (!reachable.contains (vnode) && !vnode->alias)
629 tree init;
630 if (vnode->definition)
632 if (file)
633 fprintf (file, " %s", vnode->name ());
634 changed = true;
636 /* Keep body if it may be useful for constant folding. */
637 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node
638 && !POINTER_BOUNDS_P (vnode->decl))
639 vnode->remove_initializer ();
640 else
641 DECL_INITIAL (vnode->decl) = init;
642 vnode->body_removed = true;
643 vnode->definition = false;
644 vnode->analyzed = false;
645 vnode->aux = NULL;
647 vnode->remove_from_same_comdat_group ();
649 vnode->remove_all_references ();
651 else
652 vnode->aux = NULL;
655 /* Now update address_taken flags and try to promote functions to be local. */
656 if (file)
657 fprintf (file, "\nClearing address taken flags:");
658 FOR_EACH_DEFINED_FUNCTION (node)
659 if (node->address_taken
660 && !node->used_from_other_partition)
662 if (!node->call_for_symbol_and_aliases
663 (has_addr_references_p, NULL, true)
664 && (!node->instrumentation_clone
665 || !node->instrumented_version
666 || !node->instrumented_version->address_taken))
668 if (file)
669 fprintf (file, " %s", node->name ());
670 node->address_taken = false;
671 changed = true;
672 if (node->local_p ()
673 /* Virtual functions may be kept in cgraph just because
674 of possible later devirtualization. Do not mark them as
675 local too early so we won't optimize them out before
676 we are done with polymorphic call analysis. */
677 && (!before_inlining_p
678 || !node->call_for_symbol_and_aliases
679 (is_indirect_call_target_p, NULL, true)))
681 node->local.local = true;
682 if (file)
683 fprintf (file, " (local)");
687 if (file)
688 fprintf (file, "\n");
690 symtab_node::checking_verify_symtab_nodes ();
692 /* If we removed something, perhaps profile could be improved. */
693 if (changed && optimize && inline_edge_summary_vec.exists ())
694 FOR_EACH_DEFINED_FUNCTION (node)
695 ipa_propagate_frequency (node);
697 timevar_pop (TV_IPA_UNREACHABLE);
698 return changed;
701 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
702 as needed, also clear EXPLICIT_REFS if the references to given variable
703 do not need to be explicit. */
705 void
706 process_references (varpool_node *vnode,
707 bool *written, bool *address_taken,
708 bool *read, bool *explicit_refs)
710 int i;
711 struct ipa_ref *ref;
713 if (!vnode->all_refs_explicit_p ()
714 || TREE_THIS_VOLATILE (vnode->decl))
715 *explicit_refs = false;
717 for (i = 0; vnode->iterate_referring (i, ref)
718 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
719 switch (ref->use)
721 case IPA_REF_ADDR:
722 *address_taken = true;
723 break;
724 case IPA_REF_LOAD:
725 *read = true;
726 break;
727 case IPA_REF_STORE:
728 *written = true;
729 break;
730 case IPA_REF_ALIAS:
731 process_references (dyn_cast<varpool_node *> (ref->referring), written,
732 address_taken, read, explicit_refs);
733 break;
734 case IPA_REF_CHKP:
735 gcc_unreachable ();
739 /* Set TREE_READONLY bit. */
741 bool
742 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
744 TREE_READONLY (vnode->decl) = true;
745 return false;
748 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
750 bool
751 set_writeonly_bit (varpool_node *vnode, void *data)
753 vnode->writeonly = true;
754 if (optimize)
756 DECL_INITIAL (vnode->decl) = NULL;
757 if (!vnode->alias)
759 if (vnode->num_references ())
760 *(bool *)data = true;
761 vnode->remove_all_references ();
764 return false;
767 /* Clear addressale bit of VNODE. */
769 bool
770 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
772 vnode->address_taken = false;
773 TREE_ADDRESSABLE (vnode->decl) = 0;
774 return false;
777 /* Discover variables that have no longer address taken or that are read only
778 and update their flags.
780 Return true when unreachable symbol removan should be done.
782 FIXME: This can not be done in between gimplify and omp_expand since
783 readonly flag plays role on what is shared and what is not. Currently we do
784 this transformation as part of whole program visibility and re-do at
785 ipa-reference pass (to take into account clonning), but it would
786 make sense to do it before early optimizations. */
788 bool
789 ipa_discover_readonly_nonaddressable_vars (void)
791 bool remove_p = false;
792 varpool_node *vnode;
793 if (dump_file)
794 fprintf (dump_file, "Clearing variable flags:");
795 FOR_EACH_VARIABLE (vnode)
796 if (!vnode->alias
797 && (TREE_ADDRESSABLE (vnode->decl)
798 || !vnode->writeonly
799 || !TREE_READONLY (vnode->decl)))
801 bool written = false;
802 bool address_taken = false;
803 bool read = false;
804 bool explicit_refs = true;
806 process_references (vnode, &written, &address_taken, &read,
807 &explicit_refs);
808 if (!explicit_refs)
809 continue;
810 if (!address_taken)
812 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
813 fprintf (dump_file, " %s (non-addressable)", vnode->name ());
814 vnode->call_for_symbol_and_aliases (clear_addressable_bit, NULL,
815 true);
817 if (!address_taken && !written
818 /* Making variable in explicit section readonly can cause section
819 type conflict.
820 See e.g. gcc.c-torture/compile/pr23237.c */
821 && vnode->get_section () == NULL)
823 if (!TREE_READONLY (vnode->decl) && dump_file)
824 fprintf (dump_file, " %s (read-only)", vnode->name ());
825 vnode->call_for_symbol_and_aliases (set_readonly_bit, NULL, true);
827 if (!vnode->writeonly && !read && !address_taken && written)
829 if (dump_file)
830 fprintf (dump_file, " %s (write-only)", vnode->name ());
831 vnode->call_for_symbol_and_aliases (set_writeonly_bit, &remove_p,
832 true);
835 if (dump_file)
836 fprintf (dump_file, "\n");
837 return remove_p;
840 /* Free inline summary. */
842 namespace {
844 const pass_data pass_data_ipa_free_inline_summary =
846 SIMPLE_IPA_PASS, /* type */
847 "free-inline-summary", /* name */
848 OPTGROUP_NONE, /* optinfo_flags */
849 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
850 0, /* properties_required */
851 0, /* properties_provided */
852 0, /* properties_destroyed */
853 0, /* todo_flags_start */
854 /* Early optimizations may make function unreachable. We can not
855 remove unreachable functions as part of the ealry opts pass because
856 TODOs are run before subpasses. Do it here. */
857 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
860 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
862 public:
863 pass_ipa_free_inline_summary (gcc::context *ctxt)
864 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
867 /* opt_pass methods: */
868 virtual unsigned int execute (function *)
870 inline_free_summary ();
871 return 0;
874 }; // class pass_ipa_free_inline_summary
876 } // anon namespace
878 simple_ipa_opt_pass *
879 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
881 return new pass_ipa_free_inline_summary (ctxt);
884 /* Generate and emit a static constructor or destructor. WHICH must
885 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
886 (for chp static vars constructor) or 'B' (for chkp static bounds
887 constructor). BODY is a STATEMENT_LIST containing GENERIC
888 statements. PRIORITY is the initialization priority for this
889 constructor or destructor.
891 FINAL specify whether the externally visible name for collect2 should
892 be produced. */
894 static void
895 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
897 static int counter = 0;
898 char which_buf[16];
899 tree decl, name, resdecl;
901 /* The priority is encoded in the constructor or destructor name.
902 collect2 will sort the names and arrange that they are called at
903 program startup. */
904 if (final)
905 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
906 else
907 /* Proudce sane name but one not recognizable by collect2, just for the
908 case we fail to inline the function. */
909 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
910 name = get_file_function_name (which_buf);
912 decl = build_decl (input_location, FUNCTION_DECL, name,
913 build_function_type_list (void_type_node, NULL_TREE));
914 current_function_decl = decl;
916 resdecl = build_decl (input_location,
917 RESULT_DECL, NULL_TREE, void_type_node);
918 DECL_ARTIFICIAL (resdecl) = 1;
919 DECL_RESULT (decl) = resdecl;
920 DECL_CONTEXT (resdecl) = decl;
922 allocate_struct_function (decl, false);
924 TREE_STATIC (decl) = 1;
925 TREE_USED (decl) = 1;
926 DECL_ARTIFICIAL (decl) = 1;
927 DECL_IGNORED_P (decl) = 1;
928 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
929 DECL_SAVED_TREE (decl) = body;
930 if (!targetm.have_ctors_dtors && final)
932 TREE_PUBLIC (decl) = 1;
933 DECL_PRESERVE_P (decl) = 1;
935 DECL_UNINLINABLE (decl) = 1;
937 DECL_INITIAL (decl) = make_node (BLOCK);
938 TREE_USED (DECL_INITIAL (decl)) = 1;
940 DECL_SOURCE_LOCATION (decl) = input_location;
941 cfun->function_end_locus = input_location;
943 switch (which)
945 case 'I':
946 DECL_STATIC_CONSTRUCTOR (decl) = 1;
947 decl_init_priority_insert (decl, priority);
948 break;
949 case 'P':
950 DECL_STATIC_CONSTRUCTOR (decl) = 1;
951 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("chkp ctor"),
952 NULL,
953 NULL_TREE);
954 decl_init_priority_insert (decl, priority);
955 break;
956 case 'B':
957 DECL_STATIC_CONSTRUCTOR (decl) = 1;
958 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("bnd_legacy"),
959 NULL,
960 NULL_TREE);
961 decl_init_priority_insert (decl, priority);
962 break;
963 case 'D':
964 DECL_STATIC_DESTRUCTOR (decl) = 1;
965 decl_fini_priority_insert (decl, priority);
966 break;
967 default:
968 gcc_unreachable ();
971 gimplify_function_tree (decl);
973 cgraph_node::add_new_function (decl, false);
975 set_cfun (NULL);
976 current_function_decl = NULL;
979 /* Generate and emit a static constructor or destructor. WHICH must
980 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
981 (for chkp static vars constructor) or 'B' (for chkp static bounds
982 constructor). BODY is a STATEMENT_LIST containing GENERIC
983 statements. PRIORITY is the initialization priority for this
984 constructor or destructor. */
986 void
987 cgraph_build_static_cdtor (char which, tree body, int priority)
989 cgraph_build_static_cdtor_1 (which, body, priority, false);
992 /* A vector of FUNCTION_DECLs declared as static constructors. */
993 static vec<tree> static_ctors;
994 /* A vector of FUNCTION_DECLs declared as static destructors. */
995 static vec<tree> static_dtors;
997 /* When target does not have ctors and dtors, we call all constructor
998 and destructor by special initialization/destruction function
999 recognized by collect2.
1001 When we are going to build this function, collect all constructors and
1002 destructors and turn them into normal functions. */
1004 static void
1005 record_cdtor_fn (struct cgraph_node *node)
1007 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1008 static_ctors.safe_push (node->decl);
1009 if (DECL_STATIC_DESTRUCTOR (node->decl))
1010 static_dtors.safe_push (node->decl);
1011 node = cgraph_node::get (node->decl);
1012 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
1015 /* Define global constructors/destructor functions for the CDTORS, of
1016 which they are LEN. The CDTORS are sorted by initialization
1017 priority. If CTOR_P is true, these are constructors; otherwise,
1018 they are destructors. */
1020 static void
1021 build_cdtor (bool ctor_p, vec<tree> cdtors)
1023 size_t i,j;
1024 size_t len = cdtors.length ();
1026 i = 0;
1027 while (i < len)
1029 tree body;
1030 tree fn;
1031 priority_type priority;
1033 priority = 0;
1034 body = NULL_TREE;
1035 j = i;
1038 priority_type p;
1039 fn = cdtors[j];
1040 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1041 if (j == i)
1042 priority = p;
1043 else if (p != priority)
1044 break;
1045 j++;
1047 while (j < len);
1049 /* When there is only one cdtor and target supports them, do nothing. */
1050 if (j == i + 1
1051 && targetm.have_ctors_dtors)
1053 i++;
1054 continue;
1056 /* Find the next batch of constructors/destructors with the same
1057 initialization priority. */
1058 for (;i < j; i++)
1060 tree call;
1061 fn = cdtors[i];
1062 call = build_call_expr (fn, 0);
1063 if (ctor_p)
1064 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1065 else
1066 DECL_STATIC_DESTRUCTOR (fn) = 0;
1067 /* We do not want to optimize away pure/const calls here.
1068 When optimizing, these should be already removed, when not
1069 optimizing, we want user to be able to breakpoint in them. */
1070 TREE_SIDE_EFFECTS (call) = 1;
1071 append_to_statement_list (call, &body);
1073 gcc_assert (body != NULL_TREE);
1074 /* Generate a function to call all the function of like
1075 priority. */
1076 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1080 /* Comparison function for qsort. P1 and P2 are actually of type
1081 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1082 used to determine the sort order. */
1084 static int
1085 compare_ctor (const void *p1, const void *p2)
1087 tree f1;
1088 tree f2;
1089 int priority1;
1090 int priority2;
1092 f1 = *(const tree *)p1;
1093 f2 = *(const tree *)p2;
1094 priority1 = DECL_INIT_PRIORITY (f1);
1095 priority2 = DECL_INIT_PRIORITY (f2);
1097 if (priority1 < priority2)
1098 return -1;
1099 else if (priority1 > priority2)
1100 return 1;
1101 else
1102 /* Ensure a stable sort. Constructors are executed in backwarding
1103 order to make LTO initialize braries first. */
1104 return DECL_UID (f2) - DECL_UID (f1);
1107 /* Comparison function for qsort. P1 and P2 are actually of type
1108 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1109 used to determine the sort order. */
1111 static int
1112 compare_dtor (const void *p1, const void *p2)
1114 tree f1;
1115 tree f2;
1116 int priority1;
1117 int priority2;
1119 f1 = *(const tree *)p1;
1120 f2 = *(const tree *)p2;
1121 priority1 = DECL_FINI_PRIORITY (f1);
1122 priority2 = DECL_FINI_PRIORITY (f2);
1124 if (priority1 < priority2)
1125 return -1;
1126 else if (priority1 > priority2)
1127 return 1;
1128 else
1129 /* Ensure a stable sort. */
1130 return DECL_UID (f1) - DECL_UID (f2);
1133 /* Generate functions to call static constructors and destructors
1134 for targets that do not support .ctors/.dtors sections. These
1135 functions have magic names which are detected by collect2. */
1137 static void
1138 build_cdtor_fns (void)
1140 if (!static_ctors.is_empty ())
1142 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1143 static_ctors.qsort (compare_ctor);
1144 build_cdtor (/*ctor_p=*/true, static_ctors);
1147 if (!static_dtors.is_empty ())
1149 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1150 static_dtors.qsort (compare_dtor);
1151 build_cdtor (/*ctor_p=*/false, static_dtors);
1155 /* Look for constructors and destructors and produce function calling them.
1156 This is needed for targets not supporting ctors or dtors, but we perform the
1157 transformation also at linktime to merge possibly numerous
1158 constructors/destructors into single function to improve code locality and
1159 reduce size. */
1161 static unsigned int
1162 ipa_cdtor_merge (void)
1164 struct cgraph_node *node;
1165 FOR_EACH_DEFINED_FUNCTION (node)
1166 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1167 || DECL_STATIC_DESTRUCTOR (node->decl))
1168 record_cdtor_fn (node);
1169 build_cdtor_fns ();
1170 static_ctors.release ();
1171 static_dtors.release ();
1172 return 0;
1175 namespace {
1177 const pass_data pass_data_ipa_cdtor_merge =
1179 IPA_PASS, /* type */
1180 "cdtor", /* name */
1181 OPTGROUP_NONE, /* optinfo_flags */
1182 TV_CGRAPHOPT, /* tv_id */
1183 0, /* properties_required */
1184 0, /* properties_provided */
1185 0, /* properties_destroyed */
1186 0, /* todo_flags_start */
1187 0, /* todo_flags_finish */
1190 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1192 public:
1193 pass_ipa_cdtor_merge (gcc::context *ctxt)
1194 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1195 NULL, /* generate_summary */
1196 NULL, /* write_summary */
1197 NULL, /* read_summary */
1198 NULL, /* write_optimization_summary */
1199 NULL, /* read_optimization_summary */
1200 NULL, /* stmt_fixup */
1201 0, /* function_transform_todo_flags_start */
1202 NULL, /* function_transform */
1203 NULL) /* variable_transform */
1206 /* opt_pass methods: */
1207 virtual bool gate (function *);
1208 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1210 }; // class pass_ipa_cdtor_merge
1212 bool
1213 pass_ipa_cdtor_merge::gate (function *)
1215 /* Perform the pass when we have no ctors/dtors support
1216 or at LTO time to merge multiple constructors into single
1217 function. */
1218 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1221 } // anon namespace
1223 ipa_opt_pass_d *
1224 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1226 return new pass_ipa_cdtor_merge (ctxt);
1229 /* Invalid pointer representing BOTTOM for single user dataflow. */
1230 #define BOTTOM ((cgraph_node *)(size_t) 2)
1232 /* Meet operation for single user dataflow.
1233 Here we want to associate variables with sigle function that may access it.
1235 FUNCTION is current single user of a variable, VAR is variable that uses it.
1236 Latttice is stored in SINGLE_USER_MAP.
1238 We represent:
1239 - TOP by no entry in SIGNLE_USER_MAP
1240 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1241 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1243 cgraph_node *
1244 meet (cgraph_node *function, varpool_node *var,
1245 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1247 struct cgraph_node *user, **f;
1249 if (var->aux == BOTTOM)
1250 return BOTTOM;
1252 f = single_user_map.get (var);
1253 if (!f)
1254 return function;
1255 user = *f;
1256 if (!function)
1257 return user;
1258 else if (function != user)
1259 return BOTTOM;
1260 else
1261 return function;
1264 /* Propagation step of single-use dataflow.
1266 Check all uses of VNODE and see if they are used by single function FUNCTION.
1267 SINGLE_USER_MAP represents the dataflow lattice. */
1269 cgraph_node *
1270 propagate_single_user (varpool_node *vnode, cgraph_node *function,
1271 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1273 int i;
1274 struct ipa_ref *ref;
1276 gcc_assert (!vnode->externally_visible);
1278 /* If node is an alias, first meet with its target. */
1279 if (vnode->alias)
1280 function = meet (function, vnode->get_alias_target (), single_user_map);
1282 /* Check all users and see if they correspond to a single function. */
1283 for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
1285 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
1286 if (cnode)
1288 if (cnode->global.inlined_to)
1289 cnode = cnode->global.inlined_to;
1290 if (!function)
1291 function = cnode;
1292 else if (function != cnode)
1293 function = BOTTOM;
1295 else
1296 function = meet (function, dyn_cast <varpool_node *> (ref->referring),
1297 single_user_map);
1299 return function;
1302 /* Pass setting used_by_single_function flag.
1303 This flag is set on variable when there is only one function that may
1304 possibly referr to it. */
1306 static unsigned int
1307 ipa_single_use (void)
1309 varpool_node *first = (varpool_node *) (void *) 1;
1310 varpool_node *var;
1311 hash_map<varpool_node *, cgraph_node *> single_user_map;
1313 FOR_EACH_DEFINED_VARIABLE (var)
1314 if (!var->all_refs_explicit_p ())
1315 var->aux = BOTTOM;
1316 else
1318 /* Enqueue symbol for dataflow. */
1319 var->aux = first;
1320 first = var;
1323 /* The actual dataflow. */
1325 while (first != (void *) 1)
1327 cgraph_node *user, *orig_user, **f;
1329 var = first;
1330 first = (varpool_node *)first->aux;
1332 f = single_user_map.get (var);
1333 if (f)
1334 orig_user = *f;
1335 else
1336 orig_user = NULL;
1337 user = propagate_single_user (var, orig_user, single_user_map);
1339 gcc_checking_assert (var->aux != BOTTOM);
1341 /* If user differs, enqueue all references. */
1342 if (user != orig_user)
1344 unsigned int i;
1345 ipa_ref *ref;
1347 single_user_map.put (var, user);
1349 /* Enqueue all aliases for re-processing. */
1350 for (i = 0; var->iterate_direct_aliases (i, ref); i++)
1351 if (!ref->referring->aux)
1353 ref->referring->aux = first;
1354 first = dyn_cast <varpool_node *> (ref->referring);
1356 /* Enqueue all users for re-processing. */
1357 for (i = 0; var->iterate_reference (i, ref); i++)
1358 if (!ref->referred->aux
1359 && ref->referred->definition
1360 && is_a <varpool_node *> (ref->referred))
1362 ref->referred->aux = first;
1363 first = dyn_cast <varpool_node *> (ref->referred);
1366 /* If user is BOTTOM, just punt on this var. */
1367 if (user == BOTTOM)
1368 var->aux = BOTTOM;
1369 else
1370 var->aux = NULL;
1372 else
1373 var->aux = NULL;
1376 FOR_EACH_DEFINED_VARIABLE (var)
1378 if (var->aux != BOTTOM)
1380 /* Not having the single user known means that the VAR is
1381 unreachable. Either someone forgot to remove unreachable
1382 variables or the reachability here is wrong. */
1384 gcc_checking_assert (single_user_map.get (var));
1386 if (dump_file)
1388 fprintf (dump_file, "Variable %s/%i is used by single function\n",
1389 var->name (), var->order);
1391 var->used_by_single_function = true;
1393 var->aux = NULL;
1395 return 0;
1398 namespace {
1400 const pass_data pass_data_ipa_single_use =
1402 IPA_PASS, /* type */
1403 "single-use", /* name */
1404 OPTGROUP_NONE, /* optinfo_flags */
1405 TV_CGRAPHOPT, /* tv_id */
1406 0, /* properties_required */
1407 0, /* properties_provided */
1408 0, /* properties_destroyed */
1409 0, /* todo_flags_start */
1410 0, /* todo_flags_finish */
1413 class pass_ipa_single_use : public ipa_opt_pass_d
1415 public:
1416 pass_ipa_single_use (gcc::context *ctxt)
1417 : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
1418 NULL, /* generate_summary */
1419 NULL, /* write_summary */
1420 NULL, /* read_summary */
1421 NULL, /* write_optimization_summary */
1422 NULL, /* read_optimization_summary */
1423 NULL, /* stmt_fixup */
1424 0, /* function_transform_todo_flags_start */
1425 NULL, /* function_transform */
1426 NULL) /* variable_transform */
1429 /* opt_pass methods: */
1430 virtual bool gate (function *);
1431 virtual unsigned int execute (function *) { return ipa_single_use (); }
1433 }; // class pass_ipa_single_use
1435 bool
1436 pass_ipa_single_use::gate (function *)
1438 return optimize;
1441 } // anon namespace
1443 ipa_opt_pass_d *
1444 make_pass_ipa_single_use (gcc::context *ctxt)
1446 return new pass_ipa_single_use (ctxt);