]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ipa.c
2015-10-29 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / ipa.c
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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 "alias.h"
32 #include "fold-const.h"
33 #include "calls.h"
34 #include "gimplify.h"
35 #include "flags.h"
36 #include "tree-iterator.h"
37 #include "ipa-utils.h"
38 #include "symbol-summary.h"
39 #include "ipa-prop.h"
40 #include "ipa-inline.h"
41 #include "tree-inline.h"
42 #include "profile.h"
43 #include "params.h"
44 #include "internal-fn.h"
45 #include "dbgcnt.h"
46
47
48 /* Return true when NODE has ADDR reference. */
49
50 static bool
51 has_addr_references_p (struct cgraph_node *node,
52 void *data ATTRIBUTE_UNUSED)
53 {
54 int i;
55 struct ipa_ref *ref = NULL;
56
57 for (i = 0; node->iterate_referring (i, ref); i++)
58 if (ref->use == IPA_REF_ADDR)
59 return true;
60 return false;
61 }
62
63 /* Look for all functions inlined to NODE and update their inlined_to pointers
64 to INLINED_TO. */
65
66 static void
67 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
68 {
69 struct cgraph_edge *e;
70 for (e = node->callees; e; e = e->next_callee)
71 if (e->callee->global.inlined_to)
72 {
73 e->callee->global.inlined_to = inlined_to;
74 update_inlined_to_pointer (e->callee, inlined_to);
75 }
76 }
77
78 /* Add symtab NODE to queue starting at FIRST.
79
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. */
85
86 static void
87 enqueue_node (symtab_node *node, symtab_node **first,
88 hash_set<symtab_node *> *reachable)
89 {
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;
99 }
100
101 /* Process references. */
102
103 static void
104 process_references (symtab_node *snode,
105 symtab_node **first,
106 bool before_inlining_p,
107 hash_set<symtab_node *> *reachable)
108 {
109 int i;
110 struct ipa_ref *ref = NULL;
111 for (i = 0; snode->iterate_reference (i, ref); i++)
112 {
113 symtab_node *node = ref->referred;
114 symtab_node *body = node->ultimate_alias_target ();
115
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))))
134 {
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);
142 }
143 enqueue_node (node, first, reachable);
144 }
145 }
146
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.
151
152 Also try to make trivial devirutalization when no or only one target is
153 possible. */
154
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)
161 {
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);
168
169 if (!reachable_call_targets->add (cache_token))
170 {
171 for (i = 0; i < targets.length (); i++)
172 {
173 struct cgraph_node *n = targets[i];
174
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;
182
183 symtab_node *body = n->function_symbol ();
184
185 /* Prior inlining, keep alive bodies of possible targets for
186 devirtualization. */
187 if (n->definition
188 && (before_inlining_p
189 && opt_for_fn (body->decl, optimize)
190 && opt_for_fn (body->decl, flag_devirtualize)))
191 {
192 /* Be sure that we will not optimize out alias target
193 body. */
194 if (DECL_EXTERNAL (n->decl)
195 && n->alias
196 && before_inlining_p)
197 reachable->add (body);
198 reachable->add (n);
199 }
200 /* Even after inlining we want to keep the possible targets in the
201 boundary, so late passes can still produce direct call even if
202 the chance for inlining is lost. */
203 enqueue_node (n, first, reachable);
204 }
205 }
206
207 /* Very trivial devirtualization; when the type is
208 final or anonymous (so we know all its derivation)
209 and there is only one possible virtual call target,
210 make the edge direct. */
211 if (final)
212 {
213 if (targets.length () <= 1 && dbg_cnt (devirt))
214 {
215 cgraph_node *target, *node = edge->caller;
216 if (targets.length () == 1)
217 target = targets[0];
218 else
219 target = cgraph_node::get_create
220 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
221
222 if (dump_enabled_p ())
223 {
224 location_t locus;
225 if (edge->call_stmt)
226 locus = gimple_location (edge->call_stmt);
227 else
228 locus = UNKNOWN_LOCATION;
229 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
230 "devirtualizing call in %s/%i to %s/%i\n",
231 edge->caller->name (), edge->caller->order,
232 target->name (),
233 target->order);
234 }
235 edge = edge->make_direct (target);
236 if (inline_summaries)
237 inline_update_overall_summary (node);
238 else if (edge->call_stmt)
239 {
240 edge->redirect_call_stmt_to_callee ();
241
242 /* Call to __builtin_unreachable shouldn't be instrumented. */
243 if (!targets.length ())
244 gimple_call_set_with_bounds (edge->call_stmt, false);
245 }
246 }
247 }
248 }
249
250 /* Perform reachability analysis and reclaim all unreachable nodes.
251
252 The algorithm is basically mark&sweep but with some extra refinements:
253
254 - reachable extern inline functions needs special handling; the bodies needs
255 to stay in memory until inlining in hope that they will be inlined.
256 After inlining we release their bodies and turn them into unanalyzed
257 nodes even when they are reachable.
258
259 - virtual functions are kept in callgraph even if they seem unreachable in
260 hope calls to them will be devirtualized.
261
262 Again we remove them after inlining. In late optimization some
263 devirtualization may happen, but it is not important since we won't inline
264 the call. In theory early opts and IPA should work out all important cases.
265
266 - virtual clones needs bodies of their origins for later materialization;
267 this means that we want to keep the body even if the origin is unreachable
268 otherwise. To avoid origin from sitting in the callgraph and being
269 walked by IPA passes, we turn them into unanalyzed nodes with body
270 defined.
271
272 We maintain set of function declaration where body needs to stay in
273 body_needed_for_clonning
274
275 Inline clones represent special case: their declaration match the
276 declaration of origin and cgraph_remove_node already knows how to
277 reshape callgraph and preserve body when offline copy of function or
278 inline clone is being removed.
279
280 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
281 variables with DECL_INITIAL set. We finalize these and keep reachable
282 ones around for constant folding purposes. After inlining we however
283 stop walking their references to let everything static referneced by them
284 to be removed when it is otherwise unreachable.
285
286 We maintain queue of both reachable symbols (i.e. defined symbols that needs
287 to stay) and symbols that are in boundary (i.e. external symbols referenced
288 by reachable symbols or origins of clones). The queue is represented
289 as linked list by AUX pointer terminated by 1.
290
291 At the end we keep all reachable symbols. For symbols in boundary we always
292 turn definition into a declaration, but we may keep function body around
293 based on body_needed_for_clonning
294
295 All symbols that enter the queue have AUX pointer non-zero and are in the
296 boundary. Pointer set REACHABLE is used to track reachable symbols.
297
298 Every symbol can be visited twice - once as part of boundary and once
299 as real reachable symbol. enqueue_node needs to decide whether the
300 node needs to be re-queued for second processing. For this purpose
301 we set AUX pointer of processed symbols in the boundary to constant 2. */
302
303 bool
304 symbol_table::remove_unreachable_nodes (FILE *file)
305 {
306 symtab_node *first = (symtab_node *) (void *) 1;
307 struct cgraph_node *node, *next;
308 varpool_node *vnode, *vnext;
309 bool changed = false;
310 hash_set<symtab_node *> reachable;
311 hash_set<tree> body_needed_for_clonning;
312 hash_set<void *> reachable_call_targets;
313 bool before_inlining_p = symtab->state < (!optimize ? IPA_SSA
314 : IPA_SSA_AFTER_INLINING);
315
316 timevar_push (TV_IPA_UNREACHABLE);
317 build_type_inheritance_graph ();
318 if (file)
319 fprintf (file, "\nReclaiming functions:");
320 if (flag_checking)
321 {
322 FOR_EACH_FUNCTION (node)
323 gcc_assert (!node->aux);
324 FOR_EACH_VARIABLE (vnode)
325 gcc_assert (!vnode->aux);
326 }
327 /* Mark functions whose bodies are obviously needed.
328 This is mostly when they can be referenced externally. Inline clones
329 are special since their declarations are shared with master clone and thus
330 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
331 FOR_EACH_FUNCTION (node)
332 {
333 node->used_as_abstract_origin = false;
334 if (node->definition
335 && !node->global.inlined_to
336 && !node->in_other_partition
337 && !node->can_remove_if_no_direct_calls_and_refs_p ())
338 {
339 gcc_assert (!node->global.inlined_to);
340 reachable.add (node);
341 enqueue_node (node, &first, &reachable);
342 }
343 else
344 gcc_assert (!node->aux);
345 }
346
347 /* Mark variables that are obviously needed. */
348 FOR_EACH_DEFINED_VARIABLE (vnode)
349 if (!vnode->can_remove_if_no_refs_p()
350 && !vnode->in_other_partition)
351 {
352 reachable.add (vnode);
353 enqueue_node (vnode, &first, &reachable);
354 }
355
356 /* Perform reachability analysis. */
357 while (first != (symtab_node *) (void *) 1)
358 {
359 bool in_boundary_p = !reachable.contains (first);
360 symtab_node *node = first;
361
362 first = (symtab_node *)first->aux;
363
364 /* If we are processing symbol in boundary, mark its AUX pointer for
365 possible later re-processing in enqueue_node. */
366 if (in_boundary_p)
367 {
368 node->aux = (void *)2;
369 if (node->alias && node->analyzed)
370 enqueue_node (node->get_alias_target (), &first, &reachable);
371 }
372 else
373 {
374 if (TREE_CODE (node->decl) == FUNCTION_DECL
375 && DECL_ABSTRACT_ORIGIN (node->decl))
376 {
377 struct cgraph_node *origin_node
378 = cgraph_node::get (DECL_ABSTRACT_ORIGIN (node->decl));
379 if (origin_node && !origin_node->used_as_abstract_origin)
380 {
381 origin_node->used_as_abstract_origin = true;
382 gcc_assert (!origin_node->prev_sibling_clone);
383 gcc_assert (!origin_node->next_sibling_clone);
384 for (cgraph_node *n = origin_node->clones; n;
385 n = n->next_sibling_clone)
386 if (n->decl == DECL_ABSTRACT_ORIGIN (node->decl))
387 n->used_as_abstract_origin = true;
388 }
389 }
390 /* If any symbol in a comdat group is reachable, force
391 all externally visible symbols in the same comdat
392 group to be reachable as well. Comdat-local symbols
393 can be discarded if all uses were inlined. */
394 if (node->same_comdat_group)
395 {
396 symtab_node *next;
397 for (next = node->same_comdat_group;
398 next != node;
399 next = next->same_comdat_group)
400 if (!next->comdat_local_p ()
401 && !reachable.add (next))
402 enqueue_node (next, &first, &reachable);
403 }
404 /* Mark references as reachable. */
405 process_references (node, &first, before_inlining_p, &reachable);
406 }
407
408 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
409 {
410 /* Mark the callees reachable unless they are direct calls to extern
411 inline functions we decided to not inline. */
412 if (!in_boundary_p)
413 {
414 struct cgraph_edge *e;
415 /* Keep alive possible targets for devirtualization. */
416 if (opt_for_fn (cnode->decl, optimize)
417 && opt_for_fn (cnode->decl, flag_devirtualize))
418 {
419 struct cgraph_edge *next;
420 for (e = cnode->indirect_calls; e; e = next)
421 {
422 next = e->next_callee;
423 if (e->indirect_info->polymorphic)
424 walk_polymorphic_call_targets (&reachable_call_targets,
425 e, &first, &reachable,
426 before_inlining_p);
427 }
428 }
429 for (e = cnode->callees; e; e = e->next_callee)
430 {
431 symtab_node *body = e->callee->function_symbol ();
432 if (e->callee->definition
433 && !e->callee->in_other_partition
434 && (!e->inline_failed
435 || !DECL_EXTERNAL (e->callee->decl)
436 || e->callee->alias
437 || (before_inlining_p
438 && (opt_for_fn (body->decl, optimize)
439 || (symtab->state < IPA_SSA
440 && lookup_attribute
441 ("always_inline",
442 DECL_ATTRIBUTES (body->decl)))))))
443 {
444 /* Be sure that we will not optimize out alias target
445 body. */
446 if (DECL_EXTERNAL (e->callee->decl)
447 && e->callee->alias
448 && before_inlining_p)
449 reachable.add (body);
450 reachable.add (e->callee);
451 }
452 enqueue_node (e->callee, &first, &reachable);
453 }
454
455 /* When inline clone exists, mark body to be preserved so when removing
456 offline copy of the function we don't kill it. */
457 if (cnode->global.inlined_to)
458 body_needed_for_clonning.add (cnode->decl);
459
460 /* For instrumentation clones we always need original
461 function node for proper LTO privatization. */
462 if (cnode->instrumentation_clone
463 && cnode->definition)
464 {
465 gcc_assert (cnode->instrumented_version || in_lto_p);
466 if (cnode->instrumented_version)
467 {
468 enqueue_node (cnode->instrumented_version, &first,
469 &reachable);
470 reachable.add (cnode->instrumented_version);
471 }
472 }
473
474 /* For non-inline clones, force their origins to the boundary and ensure
475 that body is not removed. */
476 while (cnode->clone_of)
477 {
478 bool noninline = cnode->clone_of->decl != cnode->decl;
479 cnode = cnode->clone_of;
480 if (noninline)
481 {
482 body_needed_for_clonning.add (cnode->decl);
483 enqueue_node (cnode, &first, &reachable);
484 }
485 }
486
487 }
488 else if (cnode->thunk.thunk_p)
489 enqueue_node (cnode->callees->callee, &first, &reachable);
490
491 /* If any reachable function has simd clones, mark them as
492 reachable as well. */
493 if (cnode->simd_clones)
494 {
495 cgraph_node *next;
496 for (next = cnode->simd_clones;
497 next;
498 next = next->simdclone->next_clone)
499 if (in_boundary_p
500 || !reachable.add (next))
501 enqueue_node (next, &first, &reachable);
502 }
503 }
504 /* When we see constructor of external variable, keep referred nodes in the
505 boundary. This will also hold initializers of the external vars NODE
506 refers to. */
507 varpool_node *vnode = dyn_cast <varpool_node *> (node);
508 if (vnode
509 && DECL_EXTERNAL (node->decl)
510 && !vnode->alias
511 && in_boundary_p)
512 {
513 struct ipa_ref *ref = NULL;
514 for (int i = 0; node->iterate_reference (i, ref); i++)
515 enqueue_node (ref->referred, &first, &reachable);
516 }
517 }
518
519 /* Remove unreachable functions. */
520 for (node = first_function (); node; node = next)
521 {
522 next = next_function (node);
523
524 /* If node is not needed at all, remove it. */
525 if (!node->aux)
526 {
527 if (file)
528 fprintf (file, " %s/%i", node->name (), node->order);
529 node->remove ();
530 changed = true;
531 }
532 /* If node is unreachable, remove its body. */
533 else if (!reachable.contains (node))
534 {
535 /* We keep definitions of thunks and aliases in the boundary so
536 we can walk to the ultimate alias targets and function symbols
537 reliably. */
538 if (node->alias || node->thunk.thunk_p)
539 ;
540 else if (!body_needed_for_clonning.contains (node->decl)
541 && !node->alias && !node->thunk.thunk_p)
542 node->release_body ();
543 else if (!node->clone_of)
544 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
545 if (node->definition && !node->alias && !node->thunk.thunk_p)
546 {
547 if (file)
548 fprintf (file, " %s/%i", node->name (), node->order);
549 node->body_removed = true;
550 node->analyzed = false;
551 node->definition = false;
552 node->cpp_implicit_alias = false;
553 node->alias = false;
554 node->thunk.thunk_p = false;
555 node->weakref = false;
556 /* After early inlining we drop always_inline attributes on
557 bodies of functions that are still referenced (have their
558 address taken). */
559 DECL_ATTRIBUTES (node->decl)
560 = remove_attribute ("always_inline",
561 DECL_ATTRIBUTES (node->decl));
562 if (!node->in_other_partition)
563 node->local.local = false;
564 node->remove_callees ();
565 node->remove_all_references ();
566 changed = true;
567 if (node->thunk.thunk_p
568 && node->thunk.add_pointer_bounds_args)
569 {
570 node->thunk.thunk_p = false;
571 node->thunk.add_pointer_bounds_args = false;
572 }
573 }
574 }
575 else
576 gcc_assert (node->clone_of || !node->has_gimple_body_p ()
577 || in_lto_p || DECL_RESULT (node->decl));
578 }
579
580 /* Inline clones might be kept around so their materializing allows further
581 cloning. If the function the clone is inlined into is removed, we need
582 to turn it into normal cone. */
583 FOR_EACH_FUNCTION (node)
584 {
585 if (node->global.inlined_to
586 && !node->callers)
587 {
588 gcc_assert (node->clones);
589 node->global.inlined_to = NULL;
590 update_inlined_to_pointer (node, node);
591 }
592 node->aux = NULL;
593 }
594
595 /* Remove unreachable variables. */
596 if (file)
597 fprintf (file, "\nReclaiming variables:");
598 for (vnode = first_variable (); vnode; vnode = vnext)
599 {
600 vnext = next_variable (vnode);
601 if (!vnode->aux
602 /* For can_refer_decl_in_current_unit_p we want to track for
603 all external variables if they are defined in other partition
604 or not. */
605 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
606 {
607 struct ipa_ref *ref = NULL;
608
609 /* First remove the aliases, so varpool::remove can possibly lookup
610 the constructor and save it for future use. */
611 while (vnode->iterate_direct_aliases (0, ref))
612 {
613 if (file)
614 fprintf (file, " %s/%i", ref->referred->name (),
615 ref->referred->order);
616 ref->referring->remove ();
617 }
618 if (file)
619 fprintf (file, " %s/%i", vnode->name (), vnode->order);
620 vnext = next_variable (vnode);
621 vnode->remove ();
622 changed = true;
623 }
624 else if (!reachable.contains (vnode) && !vnode->alias)
625 {
626 tree init;
627 if (vnode->definition)
628 {
629 if (file)
630 fprintf (file, " %s", vnode->name ());
631 changed = true;
632 }
633 /* Keep body if it may be useful for constant folding. */
634 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node
635 && !POINTER_BOUNDS_P (vnode->decl))
636 vnode->remove_initializer ();
637 else
638 DECL_INITIAL (vnode->decl) = init;
639 vnode->body_removed = true;
640 vnode->definition = false;
641 vnode->analyzed = false;
642 vnode->aux = NULL;
643
644 vnode->remove_from_same_comdat_group ();
645
646 vnode->remove_all_references ();
647 }
648 else
649 vnode->aux = NULL;
650 }
651
652 /* Now update address_taken flags and try to promote functions to be local. */
653 if (file)
654 fprintf (file, "\nClearing address taken flags:");
655 FOR_EACH_DEFINED_FUNCTION (node)
656 if (node->address_taken
657 && !node->used_from_other_partition)
658 {
659 if (!node->call_for_symbol_and_aliases
660 (has_addr_references_p, NULL, true)
661 && (!node->instrumentation_clone
662 || !node->instrumented_version
663 || !node->instrumented_version->address_taken))
664 {
665 if (file)
666 fprintf (file, " %s", node->name ());
667 node->address_taken = false;
668 changed = true;
669 if (node->local_p ())
670 {
671 node->local.local = true;
672 if (file)
673 fprintf (file, " (local)");
674 }
675 }
676 }
677 if (file)
678 fprintf (file, "\n");
679
680 symtab_node::checking_verify_symtab_nodes ();
681
682 /* If we removed something, perhaps profile could be improved. */
683 if (changed && optimize && inline_edge_summary_vec.exists ())
684 FOR_EACH_DEFINED_FUNCTION (node)
685 ipa_propagate_frequency (node);
686
687 timevar_pop (TV_IPA_UNREACHABLE);
688 return changed;
689 }
690
691 /* Process references to VNODE and set flags WRITTEN, ADDRESS_TAKEN, READ
692 as needed, also clear EXPLICIT_REFS if the references to given variable
693 do not need to be explicit. */
694
695 void
696 process_references (varpool_node *vnode,
697 bool *written, bool *address_taken,
698 bool *read, bool *explicit_refs)
699 {
700 int i;
701 struct ipa_ref *ref;
702
703 if (!vnode->all_refs_explicit_p ()
704 || TREE_THIS_VOLATILE (vnode->decl))
705 *explicit_refs = false;
706
707 for (i = 0; vnode->iterate_referring (i, ref)
708 && *explicit_refs && (!*written || !*address_taken || !*read); i++)
709 switch (ref->use)
710 {
711 case IPA_REF_ADDR:
712 *address_taken = true;
713 break;
714 case IPA_REF_LOAD:
715 *read = true;
716 break;
717 case IPA_REF_STORE:
718 *written = true;
719 break;
720 case IPA_REF_ALIAS:
721 process_references (dyn_cast<varpool_node *> (ref->referring), written,
722 address_taken, read, explicit_refs);
723 break;
724 case IPA_REF_CHKP:
725 gcc_unreachable ();
726 }
727 }
728
729 /* Set TREE_READONLY bit. */
730
731 bool
732 set_readonly_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
733 {
734 TREE_READONLY (vnode->decl) = true;
735 return false;
736 }
737
738 /* Set writeonly bit and clear the initalizer, since it will not be needed. */
739
740 bool
741 set_writeonly_bit (varpool_node *vnode, void *data)
742 {
743 vnode->writeonly = true;
744 if (optimize)
745 {
746 DECL_INITIAL (vnode->decl) = NULL;
747 if (!vnode->alias)
748 {
749 if (vnode->num_references ())
750 *(bool *)data = true;
751 vnode->remove_all_references ();
752 }
753 }
754 return false;
755 }
756
757 /* Clear addressale bit of VNODE. */
758
759 bool
760 clear_addressable_bit (varpool_node *vnode, void *data ATTRIBUTE_UNUSED)
761 {
762 vnode->address_taken = false;
763 TREE_ADDRESSABLE (vnode->decl) = 0;
764 return false;
765 }
766
767 /* Discover variables that have no longer address taken or that are read only
768 and update their flags.
769
770 Return true when unreachable symbol removan should be done.
771
772 FIXME: This can not be done in between gimplify and omp_expand since
773 readonly flag plays role on what is shared and what is not. Currently we do
774 this transformation as part of whole program visibility and re-do at
775 ipa-reference pass (to take into account clonning), but it would
776 make sense to do it before early optimizations. */
777
778 bool
779 ipa_discover_readonly_nonaddressable_vars (void)
780 {
781 bool remove_p = false;
782 varpool_node *vnode;
783 if (dump_file)
784 fprintf (dump_file, "Clearing variable flags:");
785 FOR_EACH_VARIABLE (vnode)
786 if (!vnode->alias
787 && (TREE_ADDRESSABLE (vnode->decl)
788 || !vnode->writeonly
789 || !TREE_READONLY (vnode->decl)))
790 {
791 bool written = false;
792 bool address_taken = false;
793 bool read = false;
794 bool explicit_refs = true;
795
796 process_references (vnode, &written, &address_taken, &read,
797 &explicit_refs);
798 if (!explicit_refs)
799 continue;
800 if (!address_taken)
801 {
802 if (TREE_ADDRESSABLE (vnode->decl) && dump_file)
803 fprintf (dump_file, " %s (non-addressable)", vnode->name ());
804 vnode->call_for_symbol_and_aliases (clear_addressable_bit, NULL,
805 true);
806 }
807 if (!address_taken && !written
808 /* Making variable in explicit section readonly can cause section
809 type conflict.
810 See e.g. gcc.c-torture/compile/pr23237.c */
811 && vnode->get_section () == NULL)
812 {
813 if (!TREE_READONLY (vnode->decl) && dump_file)
814 fprintf (dump_file, " %s (read-only)", vnode->name ());
815 vnode->call_for_symbol_and_aliases (set_readonly_bit, NULL, true);
816 }
817 if (!vnode->writeonly && !read && !address_taken && written)
818 {
819 if (dump_file)
820 fprintf (dump_file, " %s (write-only)", vnode->name ());
821 vnode->call_for_symbol_and_aliases (set_writeonly_bit, &remove_p,
822 true);
823 }
824 }
825 if (dump_file)
826 fprintf (dump_file, "\n");
827 return remove_p;
828 }
829
830 /* Free inline summary. */
831
832 namespace {
833
834 const pass_data pass_data_ipa_free_inline_summary =
835 {
836 SIMPLE_IPA_PASS, /* type */
837 "free-inline-summary", /* name */
838 OPTGROUP_NONE, /* optinfo_flags */
839 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
840 0, /* properties_required */
841 0, /* properties_provided */
842 0, /* properties_destroyed */
843 0, /* todo_flags_start */
844 /* Early optimizations may make function unreachable. We can not
845 remove unreachable functions as part of the ealry opts pass because
846 TODOs are run before subpasses. Do it here. */
847 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
848 };
849
850 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
851 {
852 public:
853 pass_ipa_free_inline_summary (gcc::context *ctxt)
854 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
855 {}
856
857 /* opt_pass methods: */
858 virtual unsigned int execute (function *)
859 {
860 inline_free_summary ();
861 return 0;
862 }
863
864 }; // class pass_ipa_free_inline_summary
865
866 } // anon namespace
867
868 simple_ipa_opt_pass *
869 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
870 {
871 return new pass_ipa_free_inline_summary (ctxt);
872 }
873
874 /* Generate and emit a static constructor or destructor. WHICH must
875 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
876 (for chp static vars constructor) or 'B' (for chkp static bounds
877 constructor). BODY is a STATEMENT_LIST containing GENERIC
878 statements. PRIORITY is the initialization priority for this
879 constructor or destructor.
880
881 FINAL specify whether the externally visible name for collect2 should
882 be produced. */
883
884 static void
885 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
886 {
887 static int counter = 0;
888 char which_buf[16];
889 tree decl, name, resdecl;
890
891 /* The priority is encoded in the constructor or destructor name.
892 collect2 will sort the names and arrange that they are called at
893 program startup. */
894 if (final)
895 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
896 else
897 /* Proudce sane name but one not recognizable by collect2, just for the
898 case we fail to inline the function. */
899 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
900 name = get_file_function_name (which_buf);
901
902 decl = build_decl (input_location, FUNCTION_DECL, name,
903 build_function_type_list (void_type_node, NULL_TREE));
904 current_function_decl = decl;
905
906 resdecl = build_decl (input_location,
907 RESULT_DECL, NULL_TREE, void_type_node);
908 DECL_ARTIFICIAL (resdecl) = 1;
909 DECL_RESULT (decl) = resdecl;
910 DECL_CONTEXT (resdecl) = decl;
911
912 allocate_struct_function (decl, false);
913
914 TREE_STATIC (decl) = 1;
915 TREE_USED (decl) = 1;
916 DECL_ARTIFICIAL (decl) = 1;
917 DECL_IGNORED_P (decl) = 1;
918 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
919 DECL_SAVED_TREE (decl) = body;
920 if (!targetm.have_ctors_dtors && final)
921 {
922 TREE_PUBLIC (decl) = 1;
923 DECL_PRESERVE_P (decl) = 1;
924 }
925 DECL_UNINLINABLE (decl) = 1;
926
927 DECL_INITIAL (decl) = make_node (BLOCK);
928 TREE_USED (DECL_INITIAL (decl)) = 1;
929
930 DECL_SOURCE_LOCATION (decl) = input_location;
931 cfun->function_end_locus = input_location;
932
933 switch (which)
934 {
935 case 'I':
936 DECL_STATIC_CONSTRUCTOR (decl) = 1;
937 decl_init_priority_insert (decl, priority);
938 break;
939 case 'P':
940 DECL_STATIC_CONSTRUCTOR (decl) = 1;
941 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("chkp ctor"),
942 NULL,
943 NULL_TREE);
944 decl_init_priority_insert (decl, priority);
945 break;
946 case 'B':
947 DECL_STATIC_CONSTRUCTOR (decl) = 1;
948 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("bnd_legacy"),
949 NULL,
950 NULL_TREE);
951 decl_init_priority_insert (decl, priority);
952 break;
953 case 'D':
954 DECL_STATIC_DESTRUCTOR (decl) = 1;
955 decl_fini_priority_insert (decl, priority);
956 break;
957 default:
958 gcc_unreachable ();
959 }
960
961 gimplify_function_tree (decl);
962
963 cgraph_node::add_new_function (decl, false);
964
965 set_cfun (NULL);
966 current_function_decl = NULL;
967 }
968
969 /* Generate and emit a static constructor or destructor. WHICH must
970 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
971 (for chkp static vars constructor) or 'B' (for chkp static bounds
972 constructor). BODY is a STATEMENT_LIST containing GENERIC
973 statements. PRIORITY is the initialization priority for this
974 constructor or destructor. */
975
976 void
977 cgraph_build_static_cdtor (char which, tree body, int priority)
978 {
979 cgraph_build_static_cdtor_1 (which, body, priority, false);
980 }
981
982 /* A vector of FUNCTION_DECLs declared as static constructors. */
983 static vec<tree> static_ctors;
984 /* A vector of FUNCTION_DECLs declared as static destructors. */
985 static vec<tree> static_dtors;
986
987 /* When target does not have ctors and dtors, we call all constructor
988 and destructor by special initialization/destruction function
989 recognized by collect2.
990
991 When we are going to build this function, collect all constructors and
992 destructors and turn them into normal functions. */
993
994 static void
995 record_cdtor_fn (struct cgraph_node *node)
996 {
997 if (DECL_STATIC_CONSTRUCTOR (node->decl))
998 static_ctors.safe_push (node->decl);
999 if (DECL_STATIC_DESTRUCTOR (node->decl))
1000 static_dtors.safe_push (node->decl);
1001 node = cgraph_node::get (node->decl);
1002 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
1003 }
1004
1005 /* Define global constructors/destructor functions for the CDTORS, of
1006 which they are LEN. The CDTORS are sorted by initialization
1007 priority. If CTOR_P is true, these are constructors; otherwise,
1008 they are destructors. */
1009
1010 static void
1011 build_cdtor (bool ctor_p, vec<tree> cdtors)
1012 {
1013 size_t i,j;
1014 size_t len = cdtors.length ();
1015
1016 i = 0;
1017 while (i < len)
1018 {
1019 tree body;
1020 tree fn;
1021 priority_type priority;
1022
1023 priority = 0;
1024 body = NULL_TREE;
1025 j = i;
1026 do
1027 {
1028 priority_type p;
1029 fn = cdtors[j];
1030 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1031 if (j == i)
1032 priority = p;
1033 else if (p != priority)
1034 break;
1035 j++;
1036 }
1037 while (j < len);
1038
1039 /* When there is only one cdtor and target supports them, do nothing. */
1040 if (j == i + 1
1041 && targetm.have_ctors_dtors)
1042 {
1043 i++;
1044 continue;
1045 }
1046 /* Find the next batch of constructors/destructors with the same
1047 initialization priority. */
1048 for (;i < j; i++)
1049 {
1050 tree call;
1051 fn = cdtors[i];
1052 call = build_call_expr (fn, 0);
1053 if (ctor_p)
1054 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1055 else
1056 DECL_STATIC_DESTRUCTOR (fn) = 0;
1057 /* We do not want to optimize away pure/const calls here.
1058 When optimizing, these should be already removed, when not
1059 optimizing, we want user to be able to breakpoint in them. */
1060 TREE_SIDE_EFFECTS (call) = 1;
1061 append_to_statement_list (call, &body);
1062 }
1063 gcc_assert (body != NULL_TREE);
1064 /* Generate a function to call all the function of like
1065 priority. */
1066 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1067 }
1068 }
1069
1070 /* Comparison function for qsort. P1 and P2 are actually of type
1071 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1072 used to determine the sort order. */
1073
1074 static int
1075 compare_ctor (const void *p1, const void *p2)
1076 {
1077 tree f1;
1078 tree f2;
1079 int priority1;
1080 int priority2;
1081
1082 f1 = *(const tree *)p1;
1083 f2 = *(const tree *)p2;
1084 priority1 = DECL_INIT_PRIORITY (f1);
1085 priority2 = DECL_INIT_PRIORITY (f2);
1086
1087 if (priority1 < priority2)
1088 return -1;
1089 else if (priority1 > priority2)
1090 return 1;
1091 else
1092 /* Ensure a stable sort. Constructors are executed in backwarding
1093 order to make LTO initialize braries first. */
1094 return DECL_UID (f2) - DECL_UID (f1);
1095 }
1096
1097 /* Comparison function for qsort. P1 and P2 are actually of type
1098 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1099 used to determine the sort order. */
1100
1101 static int
1102 compare_dtor (const void *p1, const void *p2)
1103 {
1104 tree f1;
1105 tree f2;
1106 int priority1;
1107 int priority2;
1108
1109 f1 = *(const tree *)p1;
1110 f2 = *(const tree *)p2;
1111 priority1 = DECL_FINI_PRIORITY (f1);
1112 priority2 = DECL_FINI_PRIORITY (f2);
1113
1114 if (priority1 < priority2)
1115 return -1;
1116 else if (priority1 > priority2)
1117 return 1;
1118 else
1119 /* Ensure a stable sort. */
1120 return DECL_UID (f1) - DECL_UID (f2);
1121 }
1122
1123 /* Generate functions to call static constructors and destructors
1124 for targets that do not support .ctors/.dtors sections. These
1125 functions have magic names which are detected by collect2. */
1126
1127 static void
1128 build_cdtor_fns (void)
1129 {
1130 if (!static_ctors.is_empty ())
1131 {
1132 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1133 static_ctors.qsort (compare_ctor);
1134 build_cdtor (/*ctor_p=*/true, static_ctors);
1135 }
1136
1137 if (!static_dtors.is_empty ())
1138 {
1139 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1140 static_dtors.qsort (compare_dtor);
1141 build_cdtor (/*ctor_p=*/false, static_dtors);
1142 }
1143 }
1144
1145 /* Look for constructors and destructors and produce function calling them.
1146 This is needed for targets not supporting ctors or dtors, but we perform the
1147 transformation also at linktime to merge possibly numerous
1148 constructors/destructors into single function to improve code locality and
1149 reduce size. */
1150
1151 static unsigned int
1152 ipa_cdtor_merge (void)
1153 {
1154 struct cgraph_node *node;
1155 FOR_EACH_DEFINED_FUNCTION (node)
1156 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1157 || DECL_STATIC_DESTRUCTOR (node->decl))
1158 record_cdtor_fn (node);
1159 build_cdtor_fns ();
1160 static_ctors.release ();
1161 static_dtors.release ();
1162 return 0;
1163 }
1164
1165 namespace {
1166
1167 const pass_data pass_data_ipa_cdtor_merge =
1168 {
1169 IPA_PASS, /* type */
1170 "cdtor", /* name */
1171 OPTGROUP_NONE, /* optinfo_flags */
1172 TV_CGRAPHOPT, /* tv_id */
1173 0, /* properties_required */
1174 0, /* properties_provided */
1175 0, /* properties_destroyed */
1176 0, /* todo_flags_start */
1177 0, /* todo_flags_finish */
1178 };
1179
1180 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1181 {
1182 public:
1183 pass_ipa_cdtor_merge (gcc::context *ctxt)
1184 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1185 NULL, /* generate_summary */
1186 NULL, /* write_summary */
1187 NULL, /* read_summary */
1188 NULL, /* write_optimization_summary */
1189 NULL, /* read_optimization_summary */
1190 NULL, /* stmt_fixup */
1191 0, /* function_transform_todo_flags_start */
1192 NULL, /* function_transform */
1193 NULL) /* variable_transform */
1194 {}
1195
1196 /* opt_pass methods: */
1197 virtual bool gate (function *);
1198 virtual unsigned int execute (function *) { return ipa_cdtor_merge (); }
1199
1200 }; // class pass_ipa_cdtor_merge
1201
1202 bool
1203 pass_ipa_cdtor_merge::gate (function *)
1204 {
1205 /* Perform the pass when we have no ctors/dtors support
1206 or at LTO time to merge multiple constructors into single
1207 function. */
1208 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1209 }
1210
1211 } // anon namespace
1212
1213 ipa_opt_pass_d *
1214 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1215 {
1216 return new pass_ipa_cdtor_merge (ctxt);
1217 }
1218
1219 /* Invalid pointer representing BOTTOM for single user dataflow. */
1220 #define BOTTOM ((cgraph_node *)(size_t) 2)
1221
1222 /* Meet operation for single user dataflow.
1223 Here we want to associate variables with sigle function that may access it.
1224
1225 FUNCTION is current single user of a variable, VAR is variable that uses it.
1226 Latttice is stored in SINGLE_USER_MAP.
1227
1228 We represent:
1229 - TOP by no entry in SIGNLE_USER_MAP
1230 - BOTTOM by BOTTOM in AUX pointer (to save lookups)
1231 - known single user by cgraph pointer in SINGLE_USER_MAP. */
1232
1233 cgraph_node *
1234 meet (cgraph_node *function, varpool_node *var,
1235 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1236 {
1237 struct cgraph_node *user, **f;
1238
1239 if (var->aux == BOTTOM)
1240 return BOTTOM;
1241
1242 f = single_user_map.get (var);
1243 if (!f)
1244 return function;
1245 user = *f;
1246 if (!function)
1247 return user;
1248 else if (function != user)
1249 return BOTTOM;
1250 else
1251 return function;
1252 }
1253
1254 /* Propagation step of single-use dataflow.
1255
1256 Check all uses of VNODE and see if they are used by single function FUNCTION.
1257 SINGLE_USER_MAP represents the dataflow lattice. */
1258
1259 cgraph_node *
1260 propagate_single_user (varpool_node *vnode, cgraph_node *function,
1261 hash_map<varpool_node *, cgraph_node *> &single_user_map)
1262 {
1263 int i;
1264 struct ipa_ref *ref;
1265
1266 gcc_assert (!vnode->externally_visible);
1267
1268 /* If node is an alias, first meet with its target. */
1269 if (vnode->alias)
1270 function = meet (function, vnode->get_alias_target (), single_user_map);
1271
1272 /* Check all users and see if they correspond to a single function. */
1273 for (i = 0; vnode->iterate_referring (i, ref) && function != BOTTOM; i++)
1274 {
1275 struct cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
1276 if (cnode)
1277 {
1278 if (cnode->global.inlined_to)
1279 cnode = cnode->global.inlined_to;
1280 if (!function)
1281 function = cnode;
1282 else if (function != cnode)
1283 function = BOTTOM;
1284 }
1285 else
1286 function = meet (function, dyn_cast <varpool_node *> (ref->referring),
1287 single_user_map);
1288 }
1289 return function;
1290 }
1291
1292 /* Pass setting used_by_single_function flag.
1293 This flag is set on variable when there is only one function that may
1294 possibly referr to it. */
1295
1296 static unsigned int
1297 ipa_single_use (void)
1298 {
1299 varpool_node *first = (varpool_node *) (void *) 1;
1300 varpool_node *var;
1301 hash_map<varpool_node *, cgraph_node *> single_user_map;
1302
1303 FOR_EACH_DEFINED_VARIABLE (var)
1304 if (!var->all_refs_explicit_p ())
1305 var->aux = BOTTOM;
1306 else
1307 {
1308 /* Enqueue symbol for dataflow. */
1309 var->aux = first;
1310 first = var;
1311 }
1312
1313 /* The actual dataflow. */
1314
1315 while (first != (void *) 1)
1316 {
1317 cgraph_node *user, *orig_user, **f;
1318
1319 var = first;
1320 first = (varpool_node *)first->aux;
1321
1322 f = single_user_map.get (var);
1323 if (f)
1324 orig_user = *f;
1325 else
1326 orig_user = NULL;
1327 user = propagate_single_user (var, orig_user, single_user_map);
1328
1329 gcc_checking_assert (var->aux != BOTTOM);
1330
1331 /* If user differs, enqueue all references. */
1332 if (user != orig_user)
1333 {
1334 unsigned int i;
1335 ipa_ref *ref;
1336
1337 single_user_map.put (var, user);
1338
1339 /* Enqueue all aliases for re-processing. */
1340 for (i = 0; var->iterate_direct_aliases (i, ref); i++)
1341 if (!ref->referring->aux)
1342 {
1343 ref->referring->aux = first;
1344 first = dyn_cast <varpool_node *> (ref->referring);
1345 }
1346 /* Enqueue all users for re-processing. */
1347 for (i = 0; var->iterate_reference (i, ref); i++)
1348 if (!ref->referred->aux
1349 && ref->referred->definition
1350 && is_a <varpool_node *> (ref->referred))
1351 {
1352 ref->referred->aux = first;
1353 first = dyn_cast <varpool_node *> (ref->referred);
1354 }
1355
1356 /* If user is BOTTOM, just punt on this var. */
1357 if (user == BOTTOM)
1358 var->aux = BOTTOM;
1359 else
1360 var->aux = NULL;
1361 }
1362 else
1363 var->aux = NULL;
1364 }
1365
1366 FOR_EACH_DEFINED_VARIABLE (var)
1367 {
1368 if (var->aux != BOTTOM)
1369 {
1370 /* Not having the single user known means that the VAR is
1371 unreachable. Either someone forgot to remove unreachable
1372 variables or the reachability here is wrong. */
1373
1374 gcc_checking_assert (single_user_map.get (var));
1375
1376 if (dump_file)
1377 {
1378 fprintf (dump_file, "Variable %s/%i is used by single function\n",
1379 var->name (), var->order);
1380 }
1381 var->used_by_single_function = true;
1382 }
1383 var->aux = NULL;
1384 }
1385 return 0;
1386 }
1387
1388 namespace {
1389
1390 const pass_data pass_data_ipa_single_use =
1391 {
1392 IPA_PASS, /* type */
1393 "single-use", /* name */
1394 OPTGROUP_NONE, /* optinfo_flags */
1395 TV_CGRAPHOPT, /* tv_id */
1396 0, /* properties_required */
1397 0, /* properties_provided */
1398 0, /* properties_destroyed */
1399 0, /* todo_flags_start */
1400 0, /* todo_flags_finish */
1401 };
1402
1403 class pass_ipa_single_use : public ipa_opt_pass_d
1404 {
1405 public:
1406 pass_ipa_single_use (gcc::context *ctxt)
1407 : ipa_opt_pass_d (pass_data_ipa_single_use, ctxt,
1408 NULL, /* generate_summary */
1409 NULL, /* write_summary */
1410 NULL, /* read_summary */
1411 NULL, /* write_optimization_summary */
1412 NULL, /* read_optimization_summary */
1413 NULL, /* stmt_fixup */
1414 0, /* function_transform_todo_flags_start */
1415 NULL, /* function_transform */
1416 NULL) /* variable_transform */
1417 {}
1418
1419 /* opt_pass methods: */
1420 virtual bool gate (function *);
1421 virtual unsigned int execute (function *) { return ipa_single_use (); }
1422
1423 }; // class pass_ipa_single_use
1424
1425 bool
1426 pass_ipa_single_use::gate (function *)
1427 {
1428 return optimize;
1429 }
1430
1431 } // anon namespace
1432
1433 ipa_opt_pass_d *
1434 make_pass_ipa_single_use (gcc::context *ctxt)
1435 {
1436 return new pass_ipa_single_use (ctxt);
1437 }