PR c++/86342 - -Wdeprecated-copy and system headers.
[official-gcc.git] / gcc / tree-ssa-live.c
blob7447f7a6f0ccd4e2d0cc5e0afd6bf89754db9c16
1 /* Liveness for SSA trees.
2 Copyright (C) 2003-2018 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "timevar.h"
29 #include "ssa.h"
30 #include "cgraph.h"
31 #include "gimple-pretty-print.h"
32 #include "diagnostic-core.h"
33 #include "gimple-iterator.h"
34 #include "tree-dfa.h"
35 #include "dumpfile.h"
36 #include "tree-ssa-live.h"
37 #include "debug.h"
38 #include "tree-ssa.h"
39 #include "ipa-utils.h"
40 #include "cfgloop.h"
41 #include "stringpool.h"
42 #include "attribs.h"
44 static void verify_live_on_entry (tree_live_info_p);
47 /* VARMAP maintains a mapping from SSA version number to real variables.
49 All SSA_NAMES are divided into partitions. Initially each ssa_name is the
50 only member of it's own partition. Coalescing will attempt to group any
51 ssa_names which occur in a copy or in a PHI node into the same partition.
53 At the end of out-of-ssa, each partition becomes a "real" variable and is
54 rewritten as a compiler variable.
56 The var_map data structure is used to manage these partitions. It allows
57 partitions to be combined, and determines which partition belongs to what
58 ssa_name or variable, and vice versa. */
61 /* Remove the base table in MAP. */
63 static void
64 var_map_base_fini (var_map map)
66 /* Free the basevar info if it is present. */
67 if (map->partition_to_base_index != NULL)
69 free (map->partition_to_base_index);
70 map->partition_to_base_index = NULL;
71 map->num_basevars = 0;
74 /* Create a variable partition map of SIZE for region, initialize and return
75 it. Region is a loop if LOOP is non-NULL, otherwise is the current
76 function. */
78 var_map
79 init_var_map (int size, struct loop *loop)
81 var_map map;
83 map = (var_map) xmalloc (sizeof (struct _var_map));
84 map->var_partition = partition_new (size);
86 map->partition_to_view = NULL;
87 map->view_to_partition = NULL;
88 map->num_partitions = size;
89 map->partition_size = size;
90 map->num_basevars = 0;
91 map->partition_to_base_index = NULL;
92 map->vec_bbs = vNULL;
93 if (loop)
95 map->bmp_bbs = BITMAP_ALLOC (NULL);
96 map->outofssa_p = false;
97 basic_block *bbs = get_loop_body_in_dom_order (loop);
98 for (unsigned i = 0; i < loop->num_nodes; ++i)
100 bitmap_set_bit (map->bmp_bbs, bbs[i]->index);
101 map->vec_bbs.safe_push (bbs[i]);
103 free (bbs);
105 else
107 map->bmp_bbs = NULL;
108 map->outofssa_p = true;
109 basic_block bb;
110 FOR_EACH_BB_FN (bb, cfun)
111 map->vec_bbs.safe_push (bb);
113 return map;
117 /* Free memory associated with MAP. */
119 void
120 delete_var_map (var_map map)
122 var_map_base_fini (map);
123 partition_delete (map->var_partition);
124 free (map->partition_to_view);
125 free (map->view_to_partition);
126 if (map->bmp_bbs)
127 BITMAP_FREE (map->bmp_bbs);
128 map->vec_bbs.release ();
129 free (map);
133 /* This function will combine the partitions in MAP for VAR1 and VAR2. It
134 Returns the partition which represents the new partition. If the two
135 partitions cannot be combined, NO_PARTITION is returned. */
138 var_union (var_map map, tree var1, tree var2)
140 int p1, p2, p3;
142 gcc_assert (TREE_CODE (var1) == SSA_NAME);
143 gcc_assert (TREE_CODE (var2) == SSA_NAME);
145 /* This is independent of partition_to_view. If partition_to_view is
146 on, then whichever one of these partitions is absorbed will never have a
147 dereference into the partition_to_view array any more. */
149 p1 = partition_find (map->var_partition, SSA_NAME_VERSION (var1));
150 p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2));
152 gcc_assert (p1 != NO_PARTITION);
153 gcc_assert (p2 != NO_PARTITION);
155 if (p1 == p2)
156 p3 = p1;
157 else
158 p3 = partition_union (map->var_partition, p1, p2);
160 if (map->partition_to_view)
161 p3 = map->partition_to_view[p3];
163 return p3;
167 /* Compress the partition numbers in MAP such that they fall in the range
168 0..(num_partitions-1) instead of wherever they turned out during
169 the partitioning exercise. This removes any references to unused
170 partitions, thereby allowing bitmaps and other vectors to be much
171 denser.
173 This is implemented such that compaction doesn't affect partitioning.
174 Ie., once partitions are created and possibly merged, running one
175 or more different kind of compaction will not affect the partitions
176 themselves. Their index might change, but all the same variables will
177 still be members of the same partition group. This allows work on reduced
178 sets, and no loss of information when a larger set is later desired.
180 In particular, coalescing can work on partitions which have 2 or more
181 definitions, and then 'recompact' later to include all the single
182 definitions for assignment to program variables. */
185 /* Set MAP back to the initial state of having no partition view. Return a
186 bitmap which has a bit set for each partition number which is in use in the
187 varmap. */
189 static bitmap
190 partition_view_init (var_map map)
192 bitmap used;
193 int tmp;
194 unsigned int x;
196 used = BITMAP_ALLOC (NULL);
198 /* Already in a view? Abandon the old one. */
199 if (map->partition_to_view)
201 free (map->partition_to_view);
202 map->partition_to_view = NULL;
204 if (map->view_to_partition)
206 free (map->view_to_partition);
207 map->view_to_partition = NULL;
210 /* Find out which partitions are actually referenced. */
211 for (x = 0; x < map->partition_size; x++)
213 tmp = partition_find (map->var_partition, x);
214 if (ssa_name (tmp) != NULL_TREE && !virtual_operand_p (ssa_name (tmp))
215 && (!has_zero_uses (ssa_name (tmp))
216 || !SSA_NAME_IS_DEFAULT_DEF (ssa_name (tmp))
217 || (SSA_NAME_VAR (ssa_name (tmp))
218 && !VAR_P (SSA_NAME_VAR (ssa_name (tmp))))))
219 bitmap_set_bit (used, tmp);
222 map->num_partitions = map->partition_size;
223 return used;
227 /* This routine will finalize the view data for MAP based on the partitions
228 set in SELECTED. This is either the same bitmap returned from
229 partition_view_init, or a trimmed down version if some of those partitions
230 were not desired in this view. SELECTED is freed before returning. */
232 static void
233 partition_view_fini (var_map map, bitmap selected)
235 bitmap_iterator bi;
236 unsigned count, i, x, limit;
238 gcc_assert (selected);
240 count = bitmap_count_bits (selected);
241 limit = map->partition_size;
243 /* If its a one-to-one ratio, we don't need any view compaction. */
244 if (count < limit)
246 map->partition_to_view = (int *)xmalloc (limit * sizeof (int));
247 memset (map->partition_to_view, 0xff, (limit * sizeof (int)));
248 map->view_to_partition = (int *)xmalloc (count * sizeof (int));
250 i = 0;
251 /* Give each selected partition an index. */
252 EXECUTE_IF_SET_IN_BITMAP (selected, 0, x, bi)
254 map->partition_to_view[x] = i;
255 map->view_to_partition[i] = x;
256 i++;
258 gcc_assert (i == count);
259 map->num_partitions = i;
262 BITMAP_FREE (selected);
266 /* Create a partition view which includes all the used partitions in MAP. */
268 void
269 partition_view_normal (var_map map)
271 bitmap used;
273 used = partition_view_init (map);
274 partition_view_fini (map, used);
276 var_map_base_fini (map);
280 /* Create a partition view in MAP which includes just partitions which occur in
281 the bitmap ONLY. If WANT_BASES is true, create the base variable map
282 as well. */
284 void
285 partition_view_bitmap (var_map map, bitmap only)
287 bitmap used;
288 bitmap new_partitions = BITMAP_ALLOC (NULL);
289 unsigned x, p;
290 bitmap_iterator bi;
292 used = partition_view_init (map);
293 EXECUTE_IF_SET_IN_BITMAP (only, 0, x, bi)
295 p = partition_find (map->var_partition, x);
296 gcc_assert (bitmap_bit_p (used, p));
297 bitmap_set_bit (new_partitions, p);
299 partition_view_fini (map, new_partitions);
301 var_map_base_fini (map);
305 static bitmap usedvars;
307 /* Mark VAR as used, so that it'll be preserved during rtl expansion.
308 Returns true if VAR wasn't marked before. */
310 static inline bool
311 set_is_used (tree var)
313 return bitmap_set_bit (usedvars, DECL_UID (var));
316 /* Return true if VAR is marked as used. */
318 static inline bool
319 is_used_p (tree var)
321 return bitmap_bit_p (usedvars, DECL_UID (var));
324 static inline void mark_all_vars_used (tree *);
326 /* Helper function for mark_all_vars_used, called via walk_tree. */
328 static tree
329 mark_all_vars_used_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
331 tree t = *tp;
332 enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
333 tree b;
335 if (TREE_CODE (t) == SSA_NAME)
337 *walk_subtrees = 0;
338 t = SSA_NAME_VAR (t);
339 if (!t)
340 return NULL;
343 if (IS_EXPR_CODE_CLASS (c)
344 && (b = TREE_BLOCK (t)) != NULL)
345 TREE_USED (b) = true;
347 /* Ignore TMR_OFFSET and TMR_STEP for TARGET_MEM_REFS, as those
348 fields do not contain vars. */
349 if (TREE_CODE (t) == TARGET_MEM_REF)
351 mark_all_vars_used (&TMR_BASE (t));
352 mark_all_vars_used (&TMR_INDEX (t));
353 mark_all_vars_used (&TMR_INDEX2 (t));
354 *walk_subtrees = 0;
355 return NULL;
358 /* Only need to mark VAR_DECLS; parameters and return results are not
359 eliminated as unused. */
360 if (VAR_P (t))
362 /* When a global var becomes used for the first time also walk its
363 initializer (non global ones don't have any). */
364 if (set_is_used (t) && is_global_var (t)
365 && DECL_CONTEXT (t) == current_function_decl)
366 mark_all_vars_used (&DECL_INITIAL (t));
368 /* remove_unused_scope_block_p requires information about labels
369 which are not DECL_IGNORED_P to tell if they might be used in the IL. */
370 else if (TREE_CODE (t) == LABEL_DECL)
371 /* Although the TREE_USED values that the frontend uses would be
372 acceptable (albeit slightly over-conservative) for our purposes,
373 init_vars_expansion clears TREE_USED for LABEL_DECLs too, so we
374 must re-compute it here. */
375 TREE_USED (t) = 1;
377 if (IS_TYPE_OR_DECL_P (t))
378 *walk_subtrees = 0;
380 return NULL;
383 /* Mark the scope block SCOPE and its subblocks unused when they can be
384 possibly eliminated if dead. */
386 static void
387 mark_scope_block_unused (tree scope)
389 tree t;
390 TREE_USED (scope) = false;
391 if (!(*debug_hooks->ignore_block) (scope))
392 TREE_USED (scope) = true;
393 for (t = BLOCK_SUBBLOCKS (scope); t ; t = BLOCK_CHAIN (t))
394 mark_scope_block_unused (t);
397 /* Look if the block is dead (by possibly eliminating its dead subblocks)
398 and return true if so.
399 Block is declared dead if:
400 1) No statements are associated with it.
401 2) Declares no live variables
402 3) All subblocks are dead
403 or there is precisely one subblocks and the block
404 has same abstract origin as outer block and declares
405 no variables, so it is pure wrapper.
406 When we are not outputting full debug info, we also eliminate dead variables
407 out of scope blocks to let them to be recycled by GGC and to save copying work
408 done by the inliner. */
410 static bool
411 remove_unused_scope_block_p (tree scope, bool in_ctor_dtor_block)
413 tree *t, *next;
414 bool unused = !TREE_USED (scope);
415 int nsubblocks = 0;
417 /* For ipa-polymorphic-call.c purposes, preserve blocks:
418 1) with BLOCK_ABSTRACT_ORIGIN of a ctor/dtor or their clones */
419 if (inlined_polymorphic_ctor_dtor_block_p (scope, true))
421 in_ctor_dtor_block = true;
422 unused = false;
424 /* 2) inside such blocks, the outermost block with block_ultimate_origin
425 being a FUNCTION_DECL. */
426 else if (in_ctor_dtor_block)
428 tree fn = block_ultimate_origin (scope);
429 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
431 in_ctor_dtor_block = false;
432 unused = false;
436 for (t = &BLOCK_VARS (scope); *t; t = next)
438 next = &DECL_CHAIN (*t);
440 /* Debug info of nested function refers to the block of the
441 function. We might stil call it even if all statements
442 of function it was nested into was elliminated.
444 TODO: We can actually look into cgraph to see if function
445 will be output to file. */
446 if (TREE_CODE (*t) == FUNCTION_DECL)
447 unused = false;
449 /* If a decl has a value expr, we need to instantiate it
450 regardless of debug info generation, to avoid codegen
451 differences in memory overlap tests. update_equiv_regs() may
452 indirectly call validate_equiv_mem() to test whether a
453 SET_DEST overlaps with others, and if the value expr changes
454 by virtual register instantiation, we may get end up with
455 different results. */
456 else if (VAR_P (*t) && DECL_HAS_VALUE_EXPR_P (*t))
457 unused = false;
459 /* Remove everything we don't generate debug info for. */
460 else if (DECL_IGNORED_P (*t))
462 *t = DECL_CHAIN (*t);
463 next = t;
466 /* When we are outputting debug info, we usually want to output
467 info about optimized-out variables in the scope blocks.
468 Exception are the scope blocks not containing any instructions
469 at all so user can't get into the scopes at first place. */
470 else if (is_used_p (*t))
471 unused = false;
472 else if (TREE_CODE (*t) == LABEL_DECL && TREE_USED (*t))
473 /* For labels that are still used in the IL, the decision to
474 preserve them must not depend DEBUG_INFO_LEVEL, otherwise we
475 risk having different ordering in debug vs. non-debug builds
476 during inlining or versioning.
477 A label appearing here (we have already checked DECL_IGNORED_P)
478 should not be used in the IL unless it has been explicitly used
479 before, so we use TREE_USED as an approximation. */
480 /* In principle, we should do the same here as for the debug case
481 below, however, when debugging, there might be additional nested
482 levels that keep an upper level with a label live, so we have to
483 force this block to be considered used, too. */
484 unused = false;
486 /* When we are not doing full debug info, we however can keep around
487 only the used variables for cfgexpand's memory packing saving quite
488 a lot of memory.
490 For sake of -g3, we keep around those vars but we don't count this as
491 use of block, so innermost block with no used vars and no instructions
492 can be considered dead. We only want to keep around blocks user can
493 breakpoint into and ask about value of optimized out variables.
495 Similarly we need to keep around types at least until all
496 variables of all nested blocks are gone. We track no
497 information on whether given type is used or not, so we have
498 to keep them even when not emitting debug information,
499 otherwise we may end up remapping variables and their (local)
500 types in different orders depending on whether debug
501 information is being generated. */
503 else if (TREE_CODE (*t) == TYPE_DECL
504 || debug_info_level == DINFO_LEVEL_NORMAL
505 || debug_info_level == DINFO_LEVEL_VERBOSE)
507 else
509 *t = DECL_CHAIN (*t);
510 next = t;
514 for (t = &BLOCK_SUBBLOCKS (scope); *t ;)
515 if (remove_unused_scope_block_p (*t, in_ctor_dtor_block))
517 if (BLOCK_SUBBLOCKS (*t))
519 tree next = BLOCK_CHAIN (*t);
520 tree supercontext = BLOCK_SUPERCONTEXT (*t);
522 *t = BLOCK_SUBBLOCKS (*t);
523 while (BLOCK_CHAIN (*t))
525 BLOCK_SUPERCONTEXT (*t) = supercontext;
526 t = &BLOCK_CHAIN (*t);
528 BLOCK_CHAIN (*t) = next;
529 BLOCK_SUPERCONTEXT (*t) = supercontext;
530 t = &BLOCK_CHAIN (*t);
531 nsubblocks ++;
533 else
534 *t = BLOCK_CHAIN (*t);
536 else
538 t = &BLOCK_CHAIN (*t);
539 nsubblocks ++;
543 if (!unused)
545 /* Outer scope is always used. */
546 else if (!BLOCK_SUPERCONTEXT (scope)
547 || TREE_CODE (BLOCK_SUPERCONTEXT (scope)) == FUNCTION_DECL)
548 unused = false;
549 /* Innermost blocks with no live variables nor statements can be always
550 eliminated. */
551 else if (!nsubblocks)
553 /* When not generating debug info we can eliminate info on unused
554 variables. */
555 else if (!flag_auto_profile && debug_info_level == DINFO_LEVEL_NONE)
557 /* Even for -g0 don't prune outer scopes from artificial
558 functions, otherwise diagnostics using tree_nonartificial_location
559 will not be emitted properly. */
560 if (inlined_function_outer_scope_p (scope))
562 tree ao = scope;
564 while (ao
565 && TREE_CODE (ao) == BLOCK
566 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
567 ao = BLOCK_ABSTRACT_ORIGIN (ao);
568 if (ao
569 && TREE_CODE (ao) == FUNCTION_DECL
570 && DECL_DECLARED_INLINE_P (ao)
571 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
572 unused = false;
575 else if (BLOCK_VARS (scope) || BLOCK_NUM_NONLOCALIZED_VARS (scope))
576 unused = false;
577 /* See if this block is important for representation of inlined
578 function. Inlined functions are always represented by block
579 with block_ultimate_origin being set to FUNCTION_DECL and
580 DECL_SOURCE_LOCATION set, unless they expand to nothing... */
581 else if (inlined_function_outer_scope_p (scope))
582 unused = false;
583 else
584 /* Verfify that only blocks with source location set
585 are entry points to the inlined functions. */
586 gcc_assert (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (scope))
587 == UNKNOWN_LOCATION);
589 TREE_USED (scope) = !unused;
590 return unused;
593 /* Mark all VAR_DECLS under *EXPR_P as used, so that they won't be
594 eliminated during the tree->rtl conversion process. */
596 static inline void
597 mark_all_vars_used (tree *expr_p)
599 walk_tree (expr_p, mark_all_vars_used_1, NULL, NULL);
602 /* Helper function for clear_unused_block_pointer, called via walk_tree. */
604 static tree
605 clear_unused_block_pointer_1 (tree *tp, int *, void *)
607 if (EXPR_P (*tp) && TREE_BLOCK (*tp)
608 && !TREE_USED (TREE_BLOCK (*tp)))
609 TREE_SET_BLOCK (*tp, NULL);
610 return NULL_TREE;
613 /* Set all block pointer in debug or clobber stmt to NULL if the block
614 is unused, so that they will not be streamed out. */
616 static void
617 clear_unused_block_pointer (void)
619 basic_block bb;
620 gimple_stmt_iterator gsi;
622 FOR_EACH_BB_FN (bb, cfun)
623 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
625 unsigned i;
626 tree b;
627 gimple *stmt = gsi_stmt (gsi);
629 if (!is_gimple_debug (stmt) && !gimple_clobber_p (stmt))
630 continue;
631 b = gimple_block (stmt);
632 if (b && !TREE_USED (b))
633 gimple_set_block (stmt, NULL);
634 for (i = 0; i < gimple_num_ops (stmt); i++)
635 walk_tree (gimple_op_ptr (stmt, i), clear_unused_block_pointer_1,
636 NULL, NULL);
640 /* Dump scope blocks starting at SCOPE to FILE. INDENT is the
641 indentation level and FLAGS is as in print_generic_expr. */
643 static void
644 dump_scope_block (FILE *file, int indent, tree scope, dump_flags_t flags)
646 tree var, t;
647 unsigned int i;
649 fprintf (file, "\n%*s{ Scope block #%i%s%s",indent, "" , BLOCK_NUMBER (scope),
650 TREE_USED (scope) ? "" : " (unused)",
651 BLOCK_ABSTRACT (scope) ? " (abstract)": "");
652 if (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (scope)) != UNKNOWN_LOCATION)
654 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (scope));
655 fprintf (file, " %s:%i", s.file, s.line);
657 if (BLOCK_ABSTRACT_ORIGIN (scope))
659 tree origin = block_ultimate_origin (scope);
660 if (origin)
662 fprintf (file, " Originating from :");
663 if (DECL_P (origin))
664 print_generic_decl (file, origin, flags);
665 else
666 fprintf (file, "#%i", BLOCK_NUMBER (origin));
669 if (BLOCK_FRAGMENT_ORIGIN (scope))
670 fprintf (file, " Fragment of : #%i",
671 BLOCK_NUMBER (BLOCK_FRAGMENT_ORIGIN (scope)));
672 else if (BLOCK_FRAGMENT_CHAIN (scope))
674 fprintf (file, " Fragment chain :");
675 for (t = BLOCK_FRAGMENT_CHAIN (scope); t ;
676 t = BLOCK_FRAGMENT_CHAIN (t))
677 fprintf (file, " #%i", BLOCK_NUMBER (t));
679 fprintf (file, " \n");
680 for (var = BLOCK_VARS (scope); var; var = DECL_CHAIN (var))
682 fprintf (file, "%*s", indent, "");
683 print_generic_decl (file, var, flags);
684 fprintf (file, "\n");
686 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (scope); i++)
688 fprintf (file, "%*s",indent, "");
689 print_generic_decl (file, BLOCK_NONLOCALIZED_VAR (scope, i),
690 flags);
691 fprintf (file, " (nonlocalized)\n");
693 for (t = BLOCK_SUBBLOCKS (scope); t ; t = BLOCK_CHAIN (t))
694 dump_scope_block (file, indent + 2, t, flags);
695 fprintf (file, "\n%*s}\n",indent, "");
698 /* Dump the tree of lexical scopes starting at SCOPE to stderr. FLAGS
699 is as in print_generic_expr. */
701 DEBUG_FUNCTION void
702 debug_scope_block (tree scope, dump_flags_t flags)
704 dump_scope_block (stderr, 0, scope, flags);
708 /* Dump the tree of lexical scopes of current_function_decl to FILE.
709 FLAGS is as in print_generic_expr. */
711 void
712 dump_scope_blocks (FILE *file, dump_flags_t flags)
714 dump_scope_block (file, 0, DECL_INITIAL (current_function_decl), flags);
718 /* Dump the tree of lexical scopes of current_function_decl to stderr.
719 FLAGS is as in print_generic_expr. */
721 DEBUG_FUNCTION void
722 debug_scope_blocks (dump_flags_t flags)
724 dump_scope_blocks (stderr, flags);
727 /* Remove local variables that are not referenced in the IL. */
729 void
730 remove_unused_locals (void)
732 basic_block bb;
733 tree var;
734 unsigned srcidx, dstidx, num;
735 bool have_local_clobbers = false;
737 /* Removing declarations from lexical blocks when not optimizing is
738 not only a waste of time, it actually causes differences in stack
739 layout. */
740 if (!optimize)
741 return;
743 timevar_push (TV_REMOVE_UNUSED);
745 mark_scope_block_unused (DECL_INITIAL (current_function_decl));
747 usedvars = BITMAP_ALLOC (NULL);
749 /* Walk the CFG marking all referenced symbols. */
750 FOR_EACH_BB_FN (bb, cfun)
752 gimple_stmt_iterator gsi;
753 size_t i;
754 edge_iterator ei;
755 edge e;
757 /* Walk the statements. */
758 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
760 gimple *stmt = gsi_stmt (gsi);
761 tree b = gimple_block (stmt);
763 /* If we wanted to mark the block referenced by the inline
764 entry point marker as used, this would be a good spot to
765 do it. If the block is not otherwise used, the stmt will
766 be cleaned up in clean_unused_block_pointer. */
767 if (is_gimple_debug (stmt))
768 continue;
770 if (gimple_clobber_p (stmt))
772 have_local_clobbers = true;
773 continue;
776 if (b)
777 TREE_USED (b) = true;
779 for (i = 0; i < gimple_num_ops (stmt); i++)
780 mark_all_vars_used (gimple_op_ptr (gsi_stmt (gsi), i));
783 for (gphi_iterator gpi = gsi_start_phis (bb);
784 !gsi_end_p (gpi);
785 gsi_next (&gpi))
787 use_operand_p arg_p;
788 ssa_op_iter i;
789 tree def;
790 gphi *phi = gpi.phi ();
792 if (virtual_operand_p (gimple_phi_result (phi)))
793 continue;
795 def = gimple_phi_result (phi);
796 mark_all_vars_used (&def);
798 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_ALL_USES)
800 tree arg = USE_FROM_PTR (arg_p);
801 int index = PHI_ARG_INDEX_FROM_USE (arg_p);
802 tree block =
803 LOCATION_BLOCK (gimple_phi_arg_location (phi, index));
804 if (block != NULL)
805 TREE_USED (block) = true;
806 mark_all_vars_used (&arg);
810 FOR_EACH_EDGE (e, ei, bb->succs)
811 if (LOCATION_BLOCK (e->goto_locus) != NULL)
812 TREE_USED (LOCATION_BLOCK (e->goto_locus)) = true;
815 /* We do a two-pass approach about the out-of-scope clobbers. We want
816 to remove them if they are the only references to a local variable,
817 but we want to retain them when there's any other. So the first pass
818 ignores them, and the second pass (if there were any) tries to remove
819 them. */
820 if (have_local_clobbers)
821 FOR_EACH_BB_FN (bb, cfun)
823 gimple_stmt_iterator gsi;
825 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
827 gimple *stmt = gsi_stmt (gsi);
828 tree b = gimple_block (stmt);
830 if (gimple_clobber_p (stmt))
832 tree lhs = gimple_assign_lhs (stmt);
833 tree base = get_base_address (lhs);
834 /* Remove clobbers referencing unused vars, or clobbers
835 with MEM_REF lhs referencing uninitialized pointers. */
836 if ((VAR_P (base) && !is_used_p (base))
837 || (TREE_CODE (lhs) == MEM_REF
838 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME
839 && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (lhs, 0))
840 && (TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (lhs, 0)))
841 != PARM_DECL)))
843 unlink_stmt_vdef (stmt);
844 gsi_remove (&gsi, true);
845 release_defs (stmt);
846 continue;
848 if (b)
849 TREE_USED (b) = true;
851 gsi_next (&gsi);
855 if (cfun->has_simduid_loops)
857 struct loop *loop;
858 FOR_EACH_LOOP (loop, 0)
859 if (loop->simduid && !is_used_p (loop->simduid))
860 loop->simduid = NULL_TREE;
863 cfun->has_local_explicit_reg_vars = false;
865 /* Remove unmarked local and global vars from local_decls. */
866 num = vec_safe_length (cfun->local_decls);
867 for (srcidx = 0, dstidx = 0; srcidx < num; srcidx++)
869 var = (*cfun->local_decls)[srcidx];
870 if (VAR_P (var))
872 if (!is_used_p (var))
874 tree def;
875 if (cfun->nonlocal_goto_save_area
876 && TREE_OPERAND (cfun->nonlocal_goto_save_area, 0) == var)
877 cfun->nonlocal_goto_save_area = NULL;
878 /* Release any default def associated with var. */
879 if ((def = ssa_default_def (cfun, var)) != NULL_TREE)
881 set_ssa_default_def (cfun, var, NULL_TREE);
882 release_ssa_name (def);
884 continue;
887 if (VAR_P (var) && DECL_HARD_REGISTER (var) && !is_global_var (var))
888 cfun->has_local_explicit_reg_vars = true;
890 if (srcidx != dstidx)
891 (*cfun->local_decls)[dstidx] = var;
892 dstidx++;
894 if (dstidx != num)
896 statistics_counter_event (cfun, "unused VAR_DECLs removed", num - dstidx);
897 cfun->local_decls->truncate (dstidx);
900 remove_unused_scope_block_p (DECL_INITIAL (current_function_decl),
901 polymorphic_ctor_dtor_p (current_function_decl,
902 true) != NULL_TREE);
903 clear_unused_block_pointer ();
905 BITMAP_FREE (usedvars);
907 if (dump_file && (dump_flags & TDF_DETAILS))
909 fprintf (dump_file, "Scope blocks after cleanups:\n");
910 dump_scope_blocks (dump_file, dump_flags);
913 timevar_pop (TV_REMOVE_UNUSED);
916 /* Allocate and return a new live range information object base on MAP. */
918 static tree_live_info_p
919 new_tree_live_info (var_map map)
921 tree_live_info_p live;
922 basic_block bb;
924 live = XNEW (struct tree_live_info_d);
925 live->map = map;
926 live->num_blocks = last_basic_block_for_fn (cfun);
928 bitmap_obstack_initialize (&live->livein_obstack);
929 bitmap_obstack_initialize (&live->liveout_obstack);
931 live->livein = XCNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
932 live->liveout = XCNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
933 for (unsigned i = 0; map->vec_bbs.iterate (i, &bb); ++i)
935 bitmap_initialize (&live->livein[bb->index], &live->livein_obstack);
936 bitmap_initialize (&live->liveout[bb->index], &live->liveout_obstack);
939 live->work_stack = XNEWVEC (int, last_basic_block_for_fn (cfun));
940 live->stack_top = live->work_stack;
942 live->global = BITMAP_ALLOC (NULL);
943 return live;
947 /* Free storage for live range info object LIVE. */
949 void
950 delete_tree_live_info (tree_live_info_p live)
952 if (live->livein)
954 bitmap_obstack_release (&live->livein_obstack);
955 free (live->livein);
957 if (live->liveout)
959 bitmap_obstack_release (&live->liveout_obstack);
960 free (live->liveout);
962 BITMAP_FREE (live->global);
963 free (live->work_stack);
964 free (live);
968 /* Visit basic block BB and propagate any required live on entry bits from
969 LIVE into the predecessors. VISITED is the bitmap of visited blocks.
970 TMP is a temporary work bitmap which is passed in to avoid reallocating
971 it each time. */
973 static void
974 loe_visit_block (tree_live_info_p live, basic_block bb, sbitmap visited)
976 edge e;
977 bool change;
978 edge_iterator ei;
979 basic_block pred_bb;
980 bitmap loe;
982 gcc_checking_assert (!bitmap_bit_p (visited, bb->index));
983 bitmap_set_bit (visited, bb->index);
985 loe = live_on_entry (live, bb);
987 FOR_EACH_EDGE (e, ei, bb->preds)
989 pred_bb = e->src;
990 if (!region_contains_p (live->map, pred_bb))
991 continue;
992 /* Variables live-on-entry from BB that aren't defined in the
993 predecessor block. This should be the live on entry vars to pred.
994 Note that liveout is the DEFs in a block while live on entry is
995 being calculated.
996 Add these bits to live-on-entry for the pred. if there are any
997 changes, and pred_bb has been visited already, add it to the
998 revisit stack. */
999 change = bitmap_ior_and_compl_into (live_on_entry (live, pred_bb),
1000 loe, &live->liveout[pred_bb->index]);
1001 if (change
1002 && bitmap_bit_p (visited, pred_bb->index))
1004 bitmap_clear_bit (visited, pred_bb->index);
1005 *(live->stack_top)++ = pred_bb->index;
1011 /* Using LIVE, fill in all the live-on-entry blocks between the defs and uses
1012 of all the variables. */
1014 static void
1015 live_worklist (tree_live_info_p live)
1017 unsigned b;
1018 basic_block bb;
1019 auto_sbitmap visited (last_basic_block_for_fn (cfun) + 1);
1021 bitmap_clear (visited);
1023 /* Visit region's blocks in reverse order and propagate live on entry values
1024 into the predecessors blocks. */
1025 for (unsigned i = live->map->vec_bbs.length () - 1;
1026 live->map->vec_bbs.iterate (i, &bb); --i)
1027 loe_visit_block (live, bb, visited);
1029 /* Process any blocks which require further iteration. */
1030 while (live->stack_top != live->work_stack)
1032 b = *--(live->stack_top);
1033 loe_visit_block (live, BASIC_BLOCK_FOR_FN (cfun, b), visited);
1038 /* Calculate the initial live on entry vector for SSA_NAME using immediate_use
1039 links. Set the live on entry fields in LIVE. Def's are marked temporarily
1040 in the liveout vector. */
1042 static void
1043 set_var_live_on_entry (tree ssa_name, tree_live_info_p live)
1045 int p;
1046 gimple *stmt;
1047 use_operand_p use;
1048 basic_block def_bb = NULL;
1049 imm_use_iterator imm_iter;
1050 bool global = false;
1052 p = var_to_partition (live->map, ssa_name);
1053 if (p == NO_PARTITION)
1054 return;
1056 stmt = SSA_NAME_DEF_STMT (ssa_name);
1057 if (stmt)
1059 def_bb = gimple_bb (stmt);
1060 /* Mark defs in liveout bitmap temporarily. */
1061 if (def_bb && region_contains_p (live->map, def_bb))
1062 bitmap_set_bit (&live->liveout[def_bb->index], p);
1064 else
1065 def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1067 /* An undefined local variable does not need to be very alive. */
1068 if (ssa_undefined_value_p (ssa_name, false))
1069 return;
1071 /* Visit each use of SSA_NAME and if it isn't in the same block as the def,
1072 add it to the list of live on entry blocks. */
1073 FOR_EACH_IMM_USE_FAST (use, imm_iter, ssa_name)
1075 gimple *use_stmt = USE_STMT (use);
1076 basic_block add_block = NULL;
1078 if (gimple_code (use_stmt) == GIMPLE_PHI)
1080 /* Uses in PHI's are considered to be live at exit of the SRC block
1081 as this is where a copy would be inserted. Check to see if it is
1082 defined in that block, or whether its live on entry. */
1083 int index = PHI_ARG_INDEX_FROM_USE (use);
1084 edge e = gimple_phi_arg_edge (as_a <gphi *> (use_stmt), index);
1085 if (e->src != def_bb && region_contains_p (live->map, e->src))
1086 add_block = e->src;
1088 else if (is_gimple_debug (use_stmt))
1089 continue;
1090 else
1092 /* If its not defined in this block, its live on entry. */
1093 basic_block use_bb = gimple_bb (use_stmt);
1094 if (use_bb != def_bb && region_contains_p (live->map, use_bb))
1095 add_block = use_bb;
1098 /* If there was a live on entry use, set the bit. */
1099 if (add_block)
1101 global = true;
1102 bitmap_set_bit (&live->livein[add_block->index], p);
1106 /* If SSA_NAME is live on entry to at least one block, fill in all the live
1107 on entry blocks between the def and all the uses. */
1108 if (global)
1109 bitmap_set_bit (live->global, p);
1113 /* Calculate the live on exit vectors based on the entry info in LIVEINFO. */
1115 static void
1116 calculate_live_on_exit (tree_live_info_p liveinfo)
1118 basic_block bb;
1119 edge e;
1120 edge_iterator ei;
1122 /* live on entry calculations used liveout vectors for defs, clear them. */
1123 for (unsigned i = 0; liveinfo->map->vec_bbs.iterate (i, &bb); ++i)
1124 bitmap_clear (&liveinfo->liveout[bb->index]);
1126 /* Set all the live-on-exit bits for uses in PHIs. */
1127 FOR_EACH_BB_FN (bb, cfun)
1129 gphi_iterator gsi;
1130 size_t i;
1132 /* Mark the PHI arguments which are live on exit to the pred block. */
1133 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1135 gphi *phi = gsi.phi ();
1136 if (virtual_operand_p (gimple_phi_result (phi)))
1137 continue;
1138 for (i = 0; i < gimple_phi_num_args (phi); i++)
1140 tree t = PHI_ARG_DEF (phi, i);
1141 int p;
1143 if (TREE_CODE (t) != SSA_NAME)
1144 continue;
1146 p = var_to_partition (liveinfo->map, t);
1147 if (p == NO_PARTITION)
1148 continue;
1149 e = gimple_phi_arg_edge (phi, i);
1150 if (region_contains_p (liveinfo->map, e->src))
1151 bitmap_set_bit (&liveinfo->liveout[e->src->index], p);
1155 if (!region_contains_p (liveinfo->map, bb))
1156 continue;
1158 /* Add each successors live on entry to this bock live on exit. */
1159 FOR_EACH_EDGE (e, ei, bb->succs)
1160 if (region_contains_p (liveinfo->map, e->dest))
1161 bitmap_ior_into (&liveinfo->liveout[bb->index],
1162 live_on_entry (liveinfo, e->dest));
1167 /* Given partition map MAP, calculate all the live on entry bitmaps for
1168 each partition. Return a new live info object. */
1170 tree_live_info_p
1171 calculate_live_ranges (var_map map, bool want_livein)
1173 tree var;
1174 unsigned i;
1175 tree_live_info_p live;
1177 live = new_tree_live_info (map);
1178 for (i = 0; i < num_var_partitions (map); i++)
1180 var = partition_to_var (map, i);
1181 if (var != NULL_TREE)
1182 set_var_live_on_entry (var, live);
1185 live_worklist (live);
1187 if (flag_checking)
1188 verify_live_on_entry (live);
1190 calculate_live_on_exit (live);
1192 if (!want_livein)
1194 bitmap_obstack_release (&live->livein_obstack);
1195 free (live->livein);
1196 live->livein = NULL;
1199 return live;
1203 /* Output partition map MAP to file F. */
1205 void
1206 dump_var_map (FILE *f, var_map map)
1208 int t;
1209 unsigned x, y;
1210 int p;
1212 fprintf (f, "\nPartition map \n\n");
1214 for (x = 0; x < map->num_partitions; x++)
1216 if (map->view_to_partition != NULL)
1217 p = map->view_to_partition[x];
1218 else
1219 p = x;
1221 if (ssa_name (p) == NULL_TREE
1222 || virtual_operand_p (ssa_name (p)))
1223 continue;
1225 t = 0;
1226 for (y = 1; y < num_ssa_names; y++)
1228 p = partition_find (map->var_partition, y);
1229 if (map->partition_to_view)
1230 p = map->partition_to_view[p];
1231 if (p == (int)x)
1233 if (t++ == 0)
1235 fprintf (f, "Partition %d (", x);
1236 print_generic_expr (f, partition_to_var (map, p), TDF_SLIM);
1237 fprintf (f, " - ");
1239 fprintf (f, "%d ", y);
1242 if (t != 0)
1243 fprintf (f, ")\n");
1245 fprintf (f, "\n");
1249 /* Generic dump for the above. */
1251 DEBUG_FUNCTION void
1252 debug (_var_map &ref)
1254 dump_var_map (stderr, &ref);
1257 DEBUG_FUNCTION void
1258 debug (_var_map *ptr)
1260 if (ptr)
1261 debug (*ptr);
1262 else
1263 fprintf (stderr, "<nil>\n");
1267 /* Output live range info LIVE to file F, controlled by FLAG. */
1269 void
1270 dump_live_info (FILE *f, tree_live_info_p live, int flag)
1272 basic_block bb;
1273 unsigned i;
1274 var_map map = live->map;
1275 bitmap_iterator bi;
1277 if ((flag & LIVEDUMP_ENTRY) && live->livein)
1279 FOR_EACH_BB_FN (bb, cfun)
1281 fprintf (f, "\nLive on entry to BB%d : ", bb->index);
1282 EXECUTE_IF_SET_IN_BITMAP (&live->livein[bb->index], 0, i, bi)
1284 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1285 fprintf (f, " ");
1287 fprintf (f, "\n");
1291 if ((flag & LIVEDUMP_EXIT) && live->liveout)
1293 FOR_EACH_BB_FN (bb, cfun)
1295 fprintf (f, "\nLive on exit from BB%d : ", bb->index);
1296 EXECUTE_IF_SET_IN_BITMAP (&live->liveout[bb->index], 0, i, bi)
1298 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1299 fprintf (f, " ");
1301 fprintf (f, "\n");
1307 /* Generic dump for the above. */
1309 DEBUG_FUNCTION void
1310 debug (tree_live_info_d &ref)
1312 dump_live_info (stderr, &ref, 0);
1315 DEBUG_FUNCTION void
1316 debug (tree_live_info_d *ptr)
1318 if (ptr)
1319 debug (*ptr);
1320 else
1321 fprintf (stderr, "<nil>\n");
1325 /* Verify that the info in LIVE matches the current cfg. */
1327 static void
1328 verify_live_on_entry (tree_live_info_p live)
1330 unsigned i;
1331 tree var;
1332 gimple *stmt;
1333 basic_block bb;
1334 edge e;
1335 int num;
1336 edge_iterator ei;
1337 var_map map = live->map;
1339 /* Check for live on entry partitions and report those with a DEF in
1340 the program. This will typically mean an optimization has done
1341 something wrong. */
1342 bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1343 num = 0;
1344 FOR_EACH_EDGE (e, ei, bb->succs)
1346 int entry_block = e->dest->index;
1347 if (!region_contains_p (live->map, e->dest))
1348 continue;
1349 for (i = 0; i < (unsigned)num_var_partitions (map); i++)
1351 basic_block tmp;
1352 tree d = NULL_TREE;
1353 bitmap loe;
1354 var = partition_to_var (map, i);
1355 stmt = SSA_NAME_DEF_STMT (var);
1356 tmp = gimple_bb (stmt);
1357 if (SSA_NAME_VAR (var))
1358 d = ssa_default_def (cfun, SSA_NAME_VAR (var));
1360 loe = live_on_entry (live, e->dest);
1361 if (loe && bitmap_bit_p (loe, i))
1363 if (!gimple_nop_p (stmt))
1365 num++;
1366 print_generic_expr (stderr, var, TDF_SLIM);
1367 fprintf (stderr, " is defined ");
1368 if (tmp)
1369 fprintf (stderr, " in BB%d, ", tmp->index);
1370 fprintf (stderr, "by:\n");
1371 print_gimple_stmt (stderr, stmt, 0, TDF_SLIM);
1372 fprintf (stderr, "\nIt is also live-on-entry to entry BB %d",
1373 entry_block);
1374 fprintf (stderr, " So it appears to have multiple defs.\n");
1376 else
1378 if (d != var)
1380 num++;
1381 print_generic_expr (stderr, var, TDF_SLIM);
1382 fprintf (stderr, " is live-on-entry to BB%d ",
1383 entry_block);
1384 if (d)
1386 fprintf (stderr, " but is not the default def of ");
1387 print_generic_expr (stderr, d, TDF_SLIM);
1388 fprintf (stderr, "\n");
1390 else
1391 fprintf (stderr, " and there is no default def.\n");
1395 else
1396 if (d == var)
1398 /* An undefined local variable does not need to be very
1399 alive. */
1400 if (ssa_undefined_value_p (var, false))
1401 continue;
1403 /* The only way this var shouldn't be marked live on entry is
1404 if it occurs in a PHI argument of the block. */
1405 size_t z;
1406 bool ok = false;
1407 gphi_iterator gsi;
1408 for (gsi = gsi_start_phis (e->dest);
1409 !gsi_end_p (gsi) && !ok;
1410 gsi_next (&gsi))
1412 gphi *phi = gsi.phi ();
1413 if (virtual_operand_p (gimple_phi_result (phi)))
1414 continue;
1415 for (z = 0; z < gimple_phi_num_args (phi); z++)
1416 if (var == gimple_phi_arg_def (phi, z))
1418 ok = true;
1419 break;
1422 if (ok)
1423 continue;
1424 /* Expand adds unused default defs for PARM_DECLs and
1425 RESULT_DECLs. They're ok. */
1426 if (has_zero_uses (var)
1427 && SSA_NAME_VAR (var)
1428 && !VAR_P (SSA_NAME_VAR (var)))
1429 continue;
1430 num++;
1431 print_generic_expr (stderr, var, TDF_SLIM);
1432 fprintf (stderr, " is not marked live-on-entry to entry BB%d ",
1433 entry_block);
1434 fprintf (stderr, "but it is a default def so it should be.\n");
1438 gcc_assert (num <= 0);