* tree-ssa.c (target_for_debug_bind, verify_phi_args,
[official-gcc.git] / gcc / ipa.c
blobe20e4953e8fdc3d3fa319fba67f2f80114f2d459
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 "tree-vrp.h"
36 #include "ipa-prop.h"
37 #include "ipa-inline.h"
38 #include "dbgcnt.h"
41 /* Return true when NODE has ADDR reference. */
43 static bool
44 has_addr_references_p (struct cgraph_node *node,
45 void *)
47 int i;
48 struct ipa_ref *ref = NULL;
50 for (i = 0; node->iterate_referring (i, ref); i++)
51 if (ref->use == IPA_REF_ADDR)
52 return true;
53 return false;
56 /* Return true when NODE can be target of an indirect call. */
58 static bool
59 is_indirect_call_target_p (struct cgraph_node *node, void *)
61 return node->indirect_call_target;
64 /* Look for all functions inlined to NODE and update their inlined_to pointers
65 to INLINED_TO. */
67 static void
68 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
70 struct cgraph_edge *e;
71 for (e = node->callees; e; e = e->next_callee)
72 if (e->callee->global.inlined_to)
74 e->callee->global.inlined_to = inlined_to;
75 update_inlined_to_pointer (e->callee, inlined_to);
79 /* Add symtab NODE to queue starting at FIRST.
81 The queue is linked via AUX pointers and terminated by pointer to 1.
82 We enqueue nodes at two occasions: when we find them reachable or when we find
83 their bodies needed for further clonning. In the second case we mark them
84 by pointer to 2 after processing so they are re-queue when they become
85 reachable. */
87 static void
88 enqueue_node (symtab_node *node, symtab_node **first,
89 hash_set<symtab_node *> *reachable)
91 /* Node is still in queue; do nothing. */
92 if (node->aux && node->aux != (void *) 2)
93 return;
94 /* Node was already processed as unreachable, re-enqueue
95 only if it became reachable now. */
96 if (node->aux == (void *)2 && !reachable->contains (node))
97 return;
98 node->aux = *first;
99 *first = node;
102 /* Process references. */
104 static void
105 process_references (symtab_node *snode,
106 symtab_node **first,
107 bool before_inlining_p,
108 hash_set<symtab_node *> *reachable)
110 int i;
111 struct ipa_ref *ref = NULL;
112 for (i = 0; snode->iterate_reference (i, ref); i++)
114 symtab_node *node = ref->referred;
115 symtab_node *body = node->ultimate_alias_target ();
117 if (node->definition && !node->in_other_partition
118 && ((!DECL_EXTERNAL (node->decl) || node->alias)
119 || (((before_inlining_p
120 && ((TREE_CODE (node->decl) != FUNCTION_DECL
121 && optimize)
122 || (TREE_CODE (node->decl) == FUNCTION_DECL
123 && opt_for_fn (body->decl, optimize))
124 || (symtab->state < IPA_SSA
125 && lookup_attribute
126 ("always_inline",
127 DECL_ATTRIBUTES (body->decl))))))
128 /* We use variable constructors during late compilation for
129 constant folding. Keep references alive so partitioning
130 knows about potential references. */
131 || (VAR_P (node->decl)
132 && flag_wpa
133 && ctor_for_folding (node->decl)
134 != error_mark_node))))
136 /* Be sure that we will not optimize out alias target
137 body. */
138 if (DECL_EXTERNAL (node->decl)
139 && node->alias
140 && before_inlining_p)
141 reachable->add (body);
142 reachable->add (node);
144 enqueue_node (node, first, reachable);
148 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
149 all its potential targets as reachable to permit later inlining if
150 devirtualization happens. After inlining still keep their declarations
151 around, so we can devirtualize to a direct call.
153 Also try to make trivial devirutalization when no or only one target is
154 possible. */
156 static void
157 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
158 struct cgraph_edge *edge,
159 symtab_node **first,
160 hash_set<symtab_node *> *reachable,
161 bool before_inlining_p)
163 unsigned int i;
164 void *cache_token;
165 bool final;
166 vec <cgraph_node *>targets
167 = possible_polymorphic_call_targets
168 (edge, &final, &cache_token);
170 if (!reachable_call_targets->add (cache_token))
172 for (i = 0; i < targets.length (); i++)
174 struct cgraph_node *n = targets[i];
176 /* Do not bother to mark virtual methods in anonymous namespace;
177 either we will find use of virtual table defining it, or it is
178 unused. */
179 if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
180 && type_in_anonymous_namespace_p
181 (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl))))
182 continue;
184 n->indirect_call_target = true;
185 symtab_node *body = n->function_symbol ();
187 /* Prior inlining, keep alive bodies of possible targets for
188 devirtualization. */
189 if (n->definition
190 && (before_inlining_p
191 && opt_for_fn (body->decl, optimize)
192 && opt_for_fn (body->decl, flag_devirtualize)))
194 /* Be sure that we will not optimize out alias target
195 body. */
196 if (DECL_EXTERNAL (n->decl)
197 && n->alias
198 && before_inlining_p)
199 reachable->add (body);
200 reachable->add (n);
202 /* Even after inlining we want to keep the possible targets in the
203 boundary, so late passes can still produce direct call even if
204 the chance for inlining is lost. */
205 enqueue_node (n, first, reachable);
209 /* Very trivial devirtualization; when the type is
210 final or anonymous (so we know all its derivation)
211 and there is only one possible virtual call target,
212 make the edge direct. */
213 if (final)
215 if (targets.length () <= 1 && dbg_cnt (devirt))
217 cgraph_node *target, *node = edge->caller;
218 if (targets.length () == 1)
219 target = targets[0];
220 else
221 target = cgraph_node::get_create
222 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
224 if (dump_enabled_p ())
226 location_t locus;
227 if (edge->call_stmt)
228 locus = gimple_location (edge->call_stmt);
229 else
230 locus = UNKNOWN_LOCATION;
231 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
232 "devirtualizing call in %s/%i to %s/%i\n",
233 edge->caller->name (), edge->caller->order,
234 target->name (),
235 target->order);
237 edge = edge->make_direct (target);
238 if (inline_summaries)
239 inline_update_overall_summary (node);
240 else if (edge->call_stmt)
242 edge->redirect_call_stmt_to_callee ();
244 /* Call to __builtin_unreachable shouldn't be instrumented. */
245 if (!targets.length ())
246 gimple_call_set_with_bounds (edge->call_stmt, false);
252 /* Perform reachability analysis and reclaim all unreachable nodes.
254 The algorithm is basically mark&sweep but with some extra refinements:
256 - reachable extern inline functions needs special handling; the bodies needs
257 to stay in memory until inlining in hope that they will be inlined.
258 After inlining we release their bodies and turn them into unanalyzed
259 nodes even when they are reachable.
261 - virtual functions are kept in callgraph even if they seem unreachable in
262 hope calls to them will be devirtualized.
264 Again we remove them after inlining. In late optimization some
265 devirtualization may happen, but it is not important since we won't inline
266 the call. In theory early opts and IPA should work out all important cases.
268 - virtual clones needs bodies of their origins for later materialization;
269 this means that we want to keep the body even if the origin is unreachable
270 otherwise. To avoid origin from sitting in the callgraph and being
271 walked by IPA passes, we turn them into unanalyzed nodes with body
272 defined.
274 We maintain set of function declaration where body needs to stay in
275 body_needed_for_clonning
277 Inline clones represent special case: their declaration match the
278 declaration of origin and cgraph_remove_node already knows how to
279 reshape callgraph and preserve body when offline copy of function or
280 inline clone is being removed.
282 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
283 variables with DECL_INITIAL set. We finalize these and keep reachable
284 ones around for constant folding purposes. After inlining we however
285 stop walking their references to let everything static referneced by them
286 to be removed when it is otherwise unreachable.
288 We maintain queue of both reachable symbols (i.e. defined symbols that needs
289 to stay) and symbols that are in boundary (i.e. external symbols referenced
290 by reachable symbols or origins of clones). The queue is represented
291 as linked list by AUX pointer terminated by 1.
293 At the end we keep all reachable symbols. For symbols in boundary we always
294 turn definition into a declaration, but we may keep function body around
295 based on body_needed_for_clonning
297 All symbols that enter the queue have AUX pointer non-zero and are in the
298 boundary. Pointer set REACHABLE is used to track reachable symbols.
300 Every symbol can be visited twice - once as part of boundary and once
301 as real reachable symbol. enqueue_node needs to decide whether the
302 node needs to be re-queued for second processing. For this purpose
303 we set AUX pointer of processed symbols in the boundary to constant 2. */
305 bool
306 symbol_table::remove_unreachable_nodes (FILE *file)
308 symtab_node *first = (symtab_node *) (void *) 1;
309 struct cgraph_node *node, *next;
310 varpool_node *vnode, *vnext;
311 bool changed = false;
312 hash_set<symtab_node *> reachable;
313 hash_set<tree> body_needed_for_clonning;
314 hash_set<void *> reachable_call_targets;
315 bool before_inlining_p = symtab->state < (!optimize ? IPA_SSA
316 : IPA_SSA_AFTER_INLINING);
318 timevar_push (TV_IPA_UNREACHABLE);
319 build_type_inheritance_graph ();
320 if (file)
321 fprintf (file, "\nReclaiming functions:");
322 if (flag_checking)
324 FOR_EACH_FUNCTION (node)
325 gcc_assert (!node->aux);
326 FOR_EACH_VARIABLE (vnode)
327 gcc_assert (!vnode->aux);
329 /* Mark functions whose bodies are obviously needed.
330 This is mostly when they can be referenced externally. Inline clones
331 are special since their declarations are shared with master clone and thus
332 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
333 FOR_EACH_FUNCTION (node)
335 node->used_as_abstract_origin = false;
336 node->indirect_call_target = false;
337 if (node->definition
338 && !node->global.inlined_to
339 && !node->in_other_partition
340 && !node->can_remove_if_no_direct_calls_and_refs_p ())
342 gcc_assert (!node->global.inlined_to);
343 reachable.add (node);
344 enqueue_node (node, &first, &reachable);
346 else
347 gcc_assert (!node->aux);
350 /* Mark variables that are obviously needed. */
351 FOR_EACH_DEFINED_VARIABLE (vnode)
352 if (!vnode->can_remove_if_no_refs_p()
353 && !vnode->in_other_partition)
355 reachable.add (vnode);
356 enqueue_node (vnode, &first, &reachable);
359 /* Perform reachability analysis. */
360 while (first != (symtab_node *) (void *) 1)
362 bool in_boundary_p = !reachable.contains (first);
363 symtab_node *node = first;
365 first = (symtab_node *)first->aux;
367 /* If we are processing symbol in boundary, mark its AUX pointer for
368 possible later re-processing in enqueue_node. */
369 if (in_boundary_p)
371 node->aux = (void *)2;
372 if (node->alias && node->analyzed)
373 enqueue_node (node->get_alias_target (), &first, &reachable);
375 else
377 if (TREE_CODE (node->decl) == FUNCTION_DECL
378 && DECL_ABSTRACT_ORIGIN (node->decl))
380 struct cgraph_node *origin_node
381 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
382 if (origin_node && !origin_node->used_as_abstract_origin)
384 origin_node->used_as_abstract_origin = true;
385 gcc_assert (!origin_node->prev_sibling_clone);
386 gcc_assert (!origin_node->next_sibling_clone);
387 for (cgraph_node *n = origin_node->clones; n;
388 n = n->next_sibling_clone)
389 if (n->decl == DECL_ABSTRACT_ORIGIN (node->decl))
390 n->used_as_abstract_origin = true;
393 /* If any symbol in a comdat group is reachable, force
394 all externally visible symbols in the same comdat
395 group to be reachable as well. Comdat-local symbols
396 can be discarded if all uses were inlined. */
397 if (node->same_comdat_group)
399 symtab_node *next;
400 for (next = node->same_comdat_group;
401 next != node;
402 next = next->same_comdat_group)
403 if (!next->comdat_local_p ()
404 && !reachable.add (next))
405 enqueue_node (next, &first, &reachable);
407 /* Mark references as reachable. */
408 process_references (node, &first, before_inlining_p, &reachable);
411 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
413 /* Mark the callees reachable unless they are direct calls to extern
414 inline functions we decided to not inline. */
415 if (!in_boundary_p)
417 struct cgraph_edge *e;
418 /* Keep alive possible targets for devirtualization. */
419 if (opt_for_fn (cnode->decl, optimize)
420 && opt_for_fn (cnode->decl, flag_devirtualize))
422 struct cgraph_edge *next;
423 for (e = cnode->indirect_calls; e; e = next)
425 next = e->next_callee;
426 if (e->indirect_info->polymorphic)
427 walk_polymorphic_call_targets (&reachable_call_targets,
428 e, &first, &reachable,
429 before_inlining_p);
432 for (e = cnode->callees; e; e = e->next_callee)
434 symtab_node *body = e->callee->function_symbol ();
435 if (e->callee->definition
436 && !e->callee->in_other_partition
437 && (!e->inline_failed
438 || !DECL_EXTERNAL (e->callee->decl)
439 || e->callee->alias
440 || (before_inlining_p
441 && (opt_for_fn (body->decl, optimize)
442 || (symtab->state < IPA_SSA
443 && lookup_attribute
444 ("always_inline",
445 DECL_ATTRIBUTES (body->decl)))))))
447 /* Be sure that we will not optimize out alias target
448 body. */
449 if (DECL_EXTERNAL (e->callee->decl)
450 && e->callee->alias
451 && before_inlining_p)
452 reachable.add (body);
453 reachable.add (e->callee);
455 enqueue_node (e->callee, &first, &reachable);
458 /* When inline clone exists, mark body to be preserved so when removing
459 offline copy of the function we don't kill it. */
460 if (cnode->global.inlined_to)
461 body_needed_for_clonning.add (cnode->decl);
463 /* For instrumentation clones we always need original
464 function node for proper LTO privatization. */
465 if (cnode->instrumentation_clone
466 && cnode->definition)
468 gcc_assert (cnode->instrumented_version || in_lto_p);
469 if (cnode->instrumented_version)
471 enqueue_node (cnode->instrumented_version, &first,
472 &reachable);
473 reachable.add (cnode->instrumented_version);
477 /* For non-inline clones, force their origins to the boundary and ensure
478 that body is not removed. */
479 while (cnode->clone_of)
481 bool noninline = cnode->clone_of->decl != cnode->decl;
482 cnode = cnode->clone_of;
483 if (noninline)
485 body_needed_for_clonning.add (cnode->decl);
486 enqueue_node (cnode, &first, &reachable);
491 else if (cnode->thunk.thunk_p)
492 enqueue_node (cnode->callees->callee, &first, &reachable);
494 /* If any reachable function has simd clones, mark them as
495 reachable as well. */
496 if (cnode->simd_clones)
498 cgraph_node *next;
499 for (next = cnode->simd_clones;
500 next;
501 next = next->simdclone->next_clone)
502 if (in_boundary_p
503 || !reachable.add (next))
504 enqueue_node (next, &first, &reachable);
507 /* When we see constructor of external variable, keep referred nodes in the
508 boundary. This will also hold initializers of the external vars NODE
509 refers to. */
510 varpool_node *vnode = dyn_cast <varpool_node *> (node);
511 if (vnode
512 && DECL_EXTERNAL (node->decl)
513 && !vnode->alias
514 && in_boundary_p)
516 struct ipa_ref *ref = NULL;
517 for (int i = 0; node->iterate_reference (i, ref); i++)
518 enqueue_node (ref->referred, &first, &reachable);
522 /* Remove unreachable functions. */
523 for (node = first_function (); node; node = next)
525 next = next_function (node);
527 /* If node is not needed at all, remove it. */
528 if (!node->aux)
530 if (file)
531 fprintf (file, " %s/%i", node->name (), node->order);
532 node->remove ();
533 changed = true;
535 /* If node is unreachable, remove its body. */
536 else if (!reachable.contains (node))
538 /* We keep definitions of thunks and aliases in the boundary so
539 we can walk to the ultimate alias targets and function symbols
540 reliably. */
541 if (node->alias || node->thunk.thunk_p)
543 else if (!body_needed_for_clonning.contains (node->decl)
544 && !node->alias && !node->thunk.thunk_p)
545 node->release_body ();
546 else if (!node->clone_of)
547 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
548 if (node->definition && !node->alias && !node->thunk.thunk_p)
550 if (file)
551 fprintf (file, " %s/%i", node->name (), node->order);
552 node->body_removed = true;
553 node->analyzed = false;
554 node->definition = false;
555 node->cpp_implicit_alias = false;
556 node->alias = false;
557 node->transparent_alias = false;
558 node->thunk.thunk_p = false;
559 node->weakref = false;
560 /* After early inlining we drop always_inline attributes on
561 bodies of functions that are still referenced (have their
562 address taken). */
563 DECL_ATTRIBUTES (node->decl)
564 = remove_attribute ("always_inline",
565 DECL_ATTRIBUTES (node->decl));
566 if (!node->in_other_partition)
567 node->local.local = false;
568 node->remove_callees ();
569 node->remove_all_references ();
570 changed = true;
571 if (node->thunk.thunk_p
572 && node->thunk.add_pointer_bounds_args)
574 node->thunk.thunk_p = false;
575 node->thunk.add_pointer_bounds_args = false;
579 else
580 gcc_assert (node->clone_of || !node->has_gimple_body_p ()
581 || in_lto_p || DECL_RESULT (node->decl));
584 /* Inline clones might be kept around so their materializing allows further
585 cloning. If the function the clone is inlined into is removed, we need
586 to turn it into normal cone. */
587 FOR_EACH_FUNCTION (node)
589 if (node->global.inlined_to
590 && !node->callers)
592 gcc_assert (node->clones);
593 node->global.inlined_to = NULL;
594 update_inlined_to_pointer (node, node);
596 node->aux = NULL;
599 /* Remove unreachable variables. */
600 if (file)
601 fprintf (file, "\nReclaiming variables:");
602 for (vnode = first_variable (); vnode; vnode = vnext)
604 vnext = next_variable (vnode);
605 if (!vnode->aux
606 /* For can_refer_decl_in_current_unit_p we want to track for
607 all external variables if they are defined in other partition
608 or not. */
609 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
611 struct ipa_ref *ref = NULL;
613 /* First remove the aliases, so varpool::remove can possibly lookup
614 the constructor and save it for future use. */
615 while (vnode->iterate_direct_aliases (0, ref))
617 if (file)
618 fprintf (file, " %s/%i", ref->referred->name (),
619 ref->referred->order);
620 ref->referring->remove ();
622 if (file)
623 fprintf (file, " %s/%i", vnode->name (), vnode->order);
624 vnext = next_variable (vnode);
625 vnode->remove ();
626 changed = true;
628 else if (!reachable.contains (vnode) && !vnode->alias)
630 tree init;
631 if (vnode->definition)
633 if (file)
634 fprintf (file, " %s", vnode->name ());
635 changed = true;
637 /* Keep body if it may be useful for constant folding. */
638 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node
639 && !POINTER_BOUNDS_P (vnode->decl))
640 vnode->remove_initializer ();
641 else
642 DECL_INITIAL (vnode->decl) = init;
643 vnode->body_removed = true;
644 vnode->definition = false;
645 vnode->analyzed = false;
646 vnode->aux = NULL;
648 vnode->remove_from_same_comdat_group ();
650 vnode->remove_all_references ();
652 else
653 vnode->aux = NULL;
656 /* Now update address_taken flags and try to promote functions to be local. */
657 if (file)
658 fprintf (file, "\nClearing address taken flags:");
659 FOR_EACH_DEFINED_FUNCTION (node)
660 if (node->address_taken
661 && !node->used_from_other_partition)
663 if (!node->call_for_symbol_and_aliases
664 (has_addr_references_p, NULL, true)
665 && (!node->instrumentation_clone
666 || !node->instrumented_version
667 || !node->instrumented_version->address_taken))
669 if (file)
670 fprintf (file, " %s", node->name ());
671 node->address_taken = false;
672 changed = true;
673 if (node->local_p ()
674 /* Virtual functions may be kept in cgraph just because
675 of possible later devirtualization. Do not mark them as
676 local too early so we won't optimize them out before
677 we are done with polymorphic call analysis. */
678 && (!before_inlining_p
679 || !node->call_for_symbol_and_aliases
680 (is_indirect_call_target_p, NULL, true)))
682 node->local.local = true;
683 if (file)
684 fprintf (file, " (local)");
688 if (file)
689 fprintf (file, "\n");
691 symtab_node::checking_verify_symtab_nodes ();
693 /* If we removed something, perhaps profile could be improved. */
694 if (changed && optimize && inline_edge_summary_vec.exists ())
695 FOR_EACH_DEFINED_FUNCTION (node)
696 ipa_propagate_frequency (node);
698 timevar_pop (TV_IPA_UNREACHABLE);
699 return changed;
702 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
703 as needed, also clear EXPLICIT_REFS if the references to given variable
704 do not need to be explicit. */
706 void
707 process_references (varpool_node *vnode,
708 bool *written, bool *address_taken,
709 bool *read, bool *explicit_refs)
711 int i;
712 struct ipa_ref *ref;
714 if (!vnode->all_refs_explicit_p ()
715 || TREE_THIS_VOLATILE (vnode->decl))
716 *explicit_refs = false;
718 for (i = 0; vnode->iterate_referring (i, ref)
719 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
720 switch (ref->use)
722 case IPA_REF_ADDR:
723 *address_taken = true;
724 break;
725 case IPA_REF_LOAD:
726 *read = true;
727 break;
728 case IPA_REF_STORE:
729 *written = true;
730 break;
731 case IPA_REF_ALIAS:
732 process_references (dyn_cast<varpool_node *> (ref->referring), written,
733 address_taken, read, explicit_refs);
734 break;
735 case IPA_REF_CHKP:
736 gcc_unreachable ();
740 /* Set TREE_READONLY bit. */
742 bool
743 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
745 TREE_READONLY (vnode->decl) = true;
746 return false;
749 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
751 bool
752 set_writeonly_bit (varpool_node *vnode, void *data)
754 vnode->writeonly = true;
755 if (optimize)
757 DECL_INITIAL (vnode->decl) = NULL;
758 if (!vnode->alias)
760 if (vnode->num_references ())
761 *(bool *)data = true;
762 vnode->remove_all_references ();
765 return false;
768 /* Clear addressale bit of VNODE. */
770 bool
771 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
773 vnode->address_taken = false;
774 TREE_ADDRESSABLE (vnode->decl) = 0;
775 return false;
778 /* Discover variables that have no longer address taken or that are read only
779 and update their flags.
781 Return true when unreachable symbol removan should be done.
783 FIXME: This can not be done in between gimplify and omp_expand since
784 readonly flag plays role on what is shared and what is not. Currently we do
785 this transformation as part of whole program visibility and re-do at
786 ipa-reference pass (to take into account clonning), but it would
787 make sense to do it before early optimizations. */
789 bool
790 ipa_discover_readonly_nonaddressable_vars (void)
792 bool remove_p = false;
793 varpool_node *vnode;
794 if (dump_file)
795 fprintf (dump_file, "Clearing variable flags:");
796 FOR_EACH_VARIABLE (vnode)
797 if (!vnode->alias
798 && (TREE_ADDRESSABLE (vnode->decl)
799 || !vnode->writeonly
800 || !TREE_READONLY (vnode->decl)))
802 bool written = false;
803 bool address_taken = false;
804 bool read = false;
805 bool explicit_refs = true;
807 process_references (vnode, &written, &address_taken, &read,
808 &explicit_refs);
809 if (!explicit_refs)
810 continue;
811 if (!address_taken)
813 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
814 fprintf (dump_file, " %s (non-addressable)", vnode->name ());
815 vnode->call_for_symbol_and_aliases (clear_addressable_bit, NULL,
816 true);
818 if (!address_taken && !written
819 /* Making variable in explicit section readonly can cause section
820 type conflict.
821 See e.g. gcc.c-torture/compile/pr23237.c */
822 && vnode->get_section () == NULL)
824 if (!TREE_READONLY (vnode->decl) && dump_file)
825 fprintf (dump_file, " %s (read-only)", vnode->name ());
826 vnode->call_for_symbol_and_aliases (set_readonly_bit, NULL, true);
828 if (!vnode->writeonly && !read && !address_taken && written)
830 if (dump_file)
831 fprintf (dump_file, " %s (write-only)", vnode->name ());
832 vnode->call_for_symbol_and_aliases (set_writeonly_bit, &remove_p,
833 true);
836 if (dump_file)
837 fprintf (dump_file, "\n");
838 return remove_p;
841 /* Free inline summary. */
843 namespace {
845 const pass_data pass_data_ipa_free_inline_summary =
847 SIMPLE_IPA_PASS, /* type */
848 "free-inline-summary", /* name */
849 OPTGROUP_NONE, /* optinfo_flags */
850 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
851 0, /* properties_required */
852 0, /* properties_provided */
853 0, /* properties_destroyed */
854 0, /* todo_flags_start */
855 /* Early optimizations may make function unreachable. We can not
856 remove unreachable functions as part of the ealry opts pass because
857 TODOs are run before subpasses. Do it here. */
858 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
861 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
863 public:
864 pass_ipa_free_inline_summary (gcc::context *ctxt)
865 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
868 /* opt_pass methods: */
869 virtual unsigned int execute (function *)
871 inline_free_summary ();
872 return 0;
875 }; // class pass_ipa_free_inline_summary
877 } // anon namespace
879 simple_ipa_opt_pass *
880 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
882 return new pass_ipa_free_inline_summary (ctxt);
885 /* Generate and emit a static constructor or destructor. WHICH must
886 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
887 (for chp static vars constructor) or 'B' (for chkp static bounds
888 constructor). BODY is a STATEMENT_LIST containing GENERIC
889 statements. PRIORITY is the initialization priority for this
890 constructor or destructor.
892 FINAL specify whether the externally visible name for collect2 should
893 be produced. */
895 static void
896 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
898 static int counter = 0;
899 char which_buf[16];
900 tree decl, name, resdecl;
902 /* The priority is encoded in the constructor or destructor name.
903 collect2 will sort the names and arrange that they are called at
904 program startup. */
905 if (final)
906 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
907 else
908 /* Proudce sane name but one not recognizable by collect2, just for the
909 case we fail to inline the function. */
910 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
911 name = get_file_function_name (which_buf);
913 decl = build_decl (input_location, FUNCTION_DECL, name,
914 build_function_type_list (void_type_node, NULL_TREE));
915 current_function_decl = decl;
917 resdecl = build_decl (input_location,
918 RESULT_DECL, NULL_TREE, void_type_node);
919 DECL_ARTIFICIAL (resdecl) = 1;
920 DECL_RESULT (decl) = resdecl;
921 DECL_CONTEXT (resdecl) = decl;
923 allocate_struct_function (decl, false);
925 TREE_STATIC (decl) = 1;
926 TREE_USED (decl) = 1;
927 DECL_ARTIFICIAL (decl) = 1;
928 DECL_IGNORED_P (decl) = 1;
929 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
930 DECL_SAVED_TREE (decl) = body;
931 if (!targetm.have_ctors_dtors && final)
933 TREE_PUBLIC (decl) = 1;
934 DECL_PRESERVE_P (decl) = 1;
936 DECL_UNINLINABLE (decl) = 1;
938 DECL_INITIAL (decl) = make_node (BLOCK);
939 BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
940 TREE_USED (DECL_INITIAL (decl)) = 1;
942 DECL_SOURCE_LOCATION (decl) = input_location;
943 cfun->function_end_locus = input_location;
945 switch (which)
947 case 'I':
948 DECL_STATIC_CONSTRUCTOR (decl) = 1;
949 decl_init_priority_insert (decl, priority);
950 break;
951 case 'P':
952 DECL_STATIC_CONSTRUCTOR (decl) = 1;
953 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("chkp ctor"),
954 NULL,
955 NULL_TREE);
956 decl_init_priority_insert (decl, priority);
957 break;
958 case 'B':
959 DECL_STATIC_CONSTRUCTOR (decl) = 1;
960 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("bnd_legacy"),
961 NULL,
962 NULL_TREE);
963 decl_init_priority_insert (decl, priority);
964 break;
965 case 'D':
966 DECL_STATIC_DESTRUCTOR (decl) = 1;
967 decl_fini_priority_insert (decl, priority);
968 break;
969 default:
970 gcc_unreachable ();
973 gimplify_function_tree (decl);
975 cgraph_node::add_new_function (decl, false);
977 set_cfun (NULL);
978 current_function_decl = NULL;
981 /* Generate and emit a static constructor or destructor. WHICH must
982 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
983 (for chkp static vars constructor) or 'B' (for chkp static bounds
984 constructor). BODY is a STATEMENT_LIST containing GENERIC
985 statements. PRIORITY is the initialization priority for this
986 constructor or destructor. */
988 void
989 cgraph_build_static_cdtor (char which, tree body, int priority)
991 cgraph_build_static_cdtor_1 (which, body, priority, false);
994 /* When target does not have ctors and dtors, we call all constructor
995 and destructor by special initialization/destruction function
996 recognized by collect2.
998 When we are going to build this function, collect all constructors and
999 destructors and turn them into normal functions. */
1001 static void
1002 record_cdtor_fn (struct cgraph_node *node, vec<tree> *ctors, vec<tree> *dtors)
1004 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1005 ctors->safe_push (node->decl);
1006 if (DECL_STATIC_DESTRUCTOR (node->decl))
1007 dtors->safe_push (node->decl);
1008 node = cgraph_node::get (node->decl);
1009 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
1012 /* Define global constructors/destructor functions for the CDTORS, of
1013 which they are LEN. The CDTORS are sorted by initialization
1014 priority. If CTOR_P is true, these are constructors; otherwise,
1015 they are destructors. */
1017 static void
1018 build_cdtor (bool ctor_p, const vec<tree> &cdtors)
1020 size_t i,j;
1021 size_t len = cdtors.length ();
1023 i = 0;
1024 while (i < len)
1026 tree body;
1027 tree fn;
1028 priority_type priority;
1030 priority = 0;
1031 body = NULL_TREE;
1032 j = i;
1035 priority_type p;
1036 fn = cdtors[j];
1037 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1038 if (j == i)
1039 priority = p;
1040 else if (p != priority)
1041 break;
1042 j++;
1044 while (j < len);
1046 /* When there is only one cdtor and target supports them, do nothing. */
1047 if (j == i + 1
1048 && targetm.have_ctors_dtors)
1050 i++;
1051 continue;
1053 /* Find the next batch of constructors/destructors with the same
1054 initialization priority. */
1055 for (;i < j; i++)
1057 tree call;
1058 fn = cdtors[i];
1059 call = build_call_expr (fn, 0);
1060 if (ctor_p)
1061 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1062 else
1063 DECL_STATIC_DESTRUCTOR (fn) = 0;
1064 /* We do not want to optimize away pure/const calls here.
1065 When optimizing, these should be already removed, when not
1066 optimizing, we want user to be able to breakpoint in them. */
1067 TREE_SIDE_EFFECTS (call) = 1;
1068 append_to_statement_list (call, &body);
1070 gcc_assert (body != NULL_TREE);
1071 /* Generate a function to call all the function of like
1072 priority. */
1073 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1077 /* Comparison function for qsort. P1 and P2 are actually of type
1078 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1079 used to determine the sort order. */
1081 static int
1082 compare_ctor (const void *p1, const void *p2)
1084 tree f1;
1085 tree f2;
1086 int priority1;
1087 int priority2;
1089 f1 = *(const tree *)p1;
1090 f2 = *(const tree *)p2;
1091 priority1 = DECL_INIT_PRIORITY (f1);
1092 priority2 = DECL_INIT_PRIORITY (f2);
1094 if (priority1 < priority2)
1095 return -1;
1096 else if (priority1 > priority2)
1097 return 1;
1098 else
1099 /* Ensure a stable sort. Constructors are executed in backwarding
1100 order to make LTO initialize braries first. */
1101 return DECL_UID (f2) - DECL_UID (f1);
1104 /* Comparison function for qsort. P1 and P2 are actually of type
1105 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1106 used to determine the sort order. */
1108 static int
1109 compare_dtor (const void *p1, const void *p2)
1111 tree f1;
1112 tree f2;
1113 int priority1;
1114 int priority2;
1116 f1 = *(const tree *)p1;
1117 f2 = *(const tree *)p2;
1118 priority1 = DECL_FINI_PRIORITY (f1);
1119 priority2 = DECL_FINI_PRIORITY (f2);
1121 if (priority1 < priority2)
1122 return -1;
1123 else if (priority1 > priority2)
1124 return 1;
1125 else
1126 /* Ensure a stable sort. */
1127 return DECL_UID (f1) - DECL_UID (f2);
1130 /* Generate functions to call static constructors and destructors
1131 for targets that do not support .ctors/.dtors sections. These
1132 functions have magic names which are detected by collect2. */
1134 static void
1135 build_cdtor_fns (vec<tree> *ctors, vec<tree> *dtors)
1137 if (!ctors->is_empty ())
1139 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1140 ctors->qsort (compare_ctor);
1141 build_cdtor (/*ctor_p=*/true, *ctors);
1144 if (!dtors->is_empty ())
1146 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1147 dtors->qsort (compare_dtor);
1148 build_cdtor (/*ctor_p=*/false, *dtors);
1152 /* Look for constructors and destructors and produce function calling them.
1153 This is needed for targets not supporting ctors or dtors, but we perform the
1154 transformation also at linktime to merge possibly numerous
1155 constructors/destructors into single function to improve code locality and
1156 reduce size. */
1158 static unsigned int
1159 ipa_cdtor_merge (void)
1161 /* A vector of FUNCTION_DECLs declared as static constructors. */
1162 auto_vec<tree, 20> ctors;
1163 /* A vector of FUNCTION_DECLs declared as static destructors. */
1164 auto_vec<tree, 20> dtors;
1165 struct cgraph_node *node;
1166 FOR_EACH_DEFINED_FUNCTION (node)
1167 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1168 || DECL_STATIC_DESTRUCTOR (node->decl))
1169 record_cdtor_fn (node, &ctors, &dtors);
1170 build_cdtor_fns (&ctors, &dtors);
1171 return 0;
1174 namespace {
1176 const pass_data pass_data_ipa_cdtor_merge =
1178 IPA_PASS, /* type */
1179 "cdtor", /* name */
1180 OPTGROUP_NONE, /* optinfo_flags */
1181 TV_CGRAPHOPT, /* tv_id */
1182 0, /* properties_required */
1183 0, /* properties_provided */
1184 0, /* properties_destroyed */
1185 0, /* todo_flags_start */
1186 0, /* todo_flags_finish */
1189 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1191 public:
1192 pass_ipa_cdtor_merge (gcc::context *ctxt)
1193 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1194 NULL, /* generate_summary */
1195 NULL, /* write_summary */
1196 NULL, /* read_summary */
1197 NULL, /* write_optimization_summary */
1198 NULL, /* read_optimization_summary */
1199 NULL, /* stmt_fixup */
1200 0, /* function_transform_todo_flags_start */
1201 NULL, /* function_transform */
1202 NULL) /* variable_transform */
1205 /* opt_pass methods: */
1206 virtual bool gate (function *);
1207 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1209 }; // class pass_ipa_cdtor_merge
1211 bool
1212 pass_ipa_cdtor_merge::gate (function *)
1214 /* Perform the pass when we have no ctors/dtors support
1215 or at LTO time to merge multiple constructors into single
1216 function. */
1217 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1220 } // anon namespace
1222 ipa_opt_pass_d *
1223 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1225 return new pass_ipa_cdtor_merge (ctxt);
1228 /* Invalid pointer representing BOTTOM for single user dataflow. */
1229 #define BOTTOM ((cgraph_node *)(size_t) 2)
1231 /* Meet operation for single user dataflow.
1232 Here we want to associate variables with sigle function that may access it.
1234 FUNCTION is current single user of a variable, VAR is variable that uses it.
1235 Latttice is stored in SINGLE_USER_MAP.
1237 We represent:
1238 - TOP by no entry in SIGNLE_USER_MAP
1239 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1240 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1242 cgraph_node *
1243 meet (cgraph_node *function, varpool_node *var,
1244 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1246 struct cgraph_node *user, **f;
1248 if (var->aux == BOTTOM)
1249 return BOTTOM;
1251 f = single_user_map.get (var);
1252 if (!f)
1253 return function;
1254 user = *f;
1255 if (!function)
1256 return user;
1257 else if (function != user)
1258 return BOTTOM;
1259 else
1260 return function;
1263 /* Propagation step of single-use dataflow.
1265 Check all uses of VNODE and see if they are used by single function FUNCTION.
1266 SINGLE_USER_MAP represents the dataflow lattice. */
1268 cgraph_node *
1269 propagate_single_user (varpool_node *vnode, cgraph_node *function,
1270 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1272 int i;
1273 struct ipa_ref *ref;
1275 gcc_assert (!vnode->externally_visible);
1277 /* If node is an alias, first meet with its target. */
1278 if (vnode->alias)
1279 function = meet (function, vnode->get_alias_target (), single_user_map);
1281 /* Check all users and see if they correspond to a single function. */
1282 for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
1284 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
1285 if (cnode)
1287 if (cnode->global.inlined_to)
1288 cnode = cnode->global.inlined_to;
1289 if (!function)
1290 function = cnode;
1291 else if (function != cnode)
1292 function = BOTTOM;
1294 else
1295 function = meet (function, dyn_cast <varpool_node *> (ref->referring),
1296 single_user_map);
1298 return function;
1301 /* Pass setting used_by_single_function flag.
1302 This flag is set on variable when there is only one function that may
1303 possibly referr to it. */
1305 static unsigned int
1306 ipa_single_use (void)
1308 varpool_node *first = (varpool_node *) (void *) 1;
1309 varpool_node *var;
1310 hash_map<varpool_node *, cgraph_node *> single_user_map;
1312 FOR_EACH_DEFINED_VARIABLE (var)
1313 if (!var->all_refs_explicit_p ())
1314 var->aux = BOTTOM;
1315 else
1317 /* Enqueue symbol for dataflow. */
1318 var->aux = first;
1319 first = var;
1322 /* The actual dataflow. */
1324 while (first != (void *) 1)
1326 cgraph_node *user, *orig_user, **f;
1328 var = first;
1329 first = (varpool_node *)first->aux;
1331 f = single_user_map.get (var);
1332 if (f)
1333 orig_user = *f;
1334 else
1335 orig_user = NULL;
1336 user = propagate_single_user (var, orig_user, single_user_map);
1338 gcc_checking_assert (var->aux != BOTTOM);
1340 /* If user differs, enqueue all references. */
1341 if (user != orig_user)
1343 unsigned int i;
1344 ipa_ref *ref;
1346 single_user_map.put (var, user);
1348 /* Enqueue all aliases for re-processing. */
1349 for (i = 0; var->iterate_direct_aliases (i, ref); i++)
1350 if (!ref->referring->aux)
1352 ref->referring->aux = first;
1353 first = dyn_cast <varpool_node *> (ref->referring);
1355 /* Enqueue all users for re-processing. */
1356 for (i = 0; var->iterate_reference (i, ref); i++)
1357 if (!ref->referred->aux
1358 && ref->referred->definition
1359 && is_a <varpool_node *> (ref->referred))
1361 ref->referred->aux = first;
1362 first = dyn_cast <varpool_node *> (ref->referred);
1365 /* If user is BOTTOM, just punt on this var. */
1366 if (user == BOTTOM)
1367 var->aux = BOTTOM;
1368 else
1369 var->aux = NULL;
1371 else
1372 var->aux = NULL;
1375 FOR_EACH_DEFINED_VARIABLE (var)
1377 if (var->aux != BOTTOM)
1379 /* Not having the single user known means that the VAR is
1380 unreachable. Either someone forgot to remove unreachable
1381 variables or the reachability here is wrong. */
1383 gcc_checking_assert (single_user_map.get (var));
1385 if (dump_file)
1387 fprintf (dump_file, "Variable %s/%i is used by single function\n",
1388 var->name (), var->order);
1390 var->used_by_single_function = true;
1392 var->aux = NULL;
1394 return 0;
1397 namespace {
1399 const pass_data pass_data_ipa_single_use =
1401 IPA_PASS, /* type */
1402 "single-use", /* name */
1403 OPTGROUP_NONE, /* optinfo_flags */
1404 TV_CGRAPHOPT, /* tv_id */
1405 0, /* properties_required */
1406 0, /* properties_provided */
1407 0, /* properties_destroyed */
1408 0, /* todo_flags_start */
1409 0, /* todo_flags_finish */
1412 class pass_ipa_single_use : public ipa_opt_pass_d
1414 public:
1415 pass_ipa_single_use (gcc::context *ctxt)
1416 : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
1417 NULL, /* generate_summary */
1418 NULL, /* write_summary */
1419 NULL, /* read_summary */
1420 NULL, /* write_optimization_summary */
1421 NULL, /* read_optimization_summary */
1422 NULL, /* stmt_fixup */
1423 0, /* function_transform_todo_flags_start */
1424 NULL, /* function_transform */
1425 NULL) /* variable_transform */
1428 /* opt_pass methods: */
1429 virtual bool gate (function *);
1430 virtual unsigned int execute (function *) { return ipa_single_use (); }
1432 }; // class pass_ipa_single_use
1434 bool
1435 pass_ipa_single_use::gate (function *)
1437 return optimize;
1440 } // anon namespace
1442 ipa_opt_pass_d *
1443 make_pass_ipa_single_use (gcc::context *ctxt)
1445 return new pass_ipa_single_use (ctxt);
1448 /* Materialize all clones. */
1450 namespace {
1452 const pass_data pass_data_materialize_all_clones =
1454 SIMPLE_IPA_PASS, /* type */
1455 "materialize-all-clones", /* name */
1456 OPTGROUP_NONE, /* optinfo_flags */
1457 TV_IPA_OPT, /* tv_id */
1458 0, /* properties_required */
1459 0, /* properties_provided */
1460 0, /* properties_destroyed */
1461 0, /* todo_flags_start */
1462 0, /* todo_flags_finish */
1465 class pass_materialize_all_clones : public simple_ipa_opt_pass
1467 public:
1468 pass_materialize_all_clones (gcc::context *ctxt)
1469 : simple_ipa_opt_pass (pass_data_materialize_all_clones, ctxt)
1472 /* opt_pass methods: */
1473 virtual unsigned int execute (function *)
1475 symtab->materialize_all_clones ();
1476 return 0;
1479 }; // class pass_materialize_all_clones
1481 } // anon namespace
1483 simple_ipa_opt_pass *
1484 make_pass_materialize_all_clones (gcc::context *ctxt)
1486 return new pass_materialize_all_clones (ctxt);