2013-09-30 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tree-ssa-live.c
blob7705f1312b86c3b32431eda85b2ef8fc1d30b9ba
1 /* Liveness for SSA trees.
2 Copyright (C) 2003-2013 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 "hash-table.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "gimple-pretty-print.h"
28 #include "bitmap.h"
29 #include "tree-ssa.h"
30 #include "timevar.h"
31 #include "dumpfile.h"
32 #include "tree-ssa-live.h"
33 #include "diagnostic-core.h"
34 #include "debug.h"
35 #include "flags.h"
36 #include "gimple.h"
38 #ifdef ENABLE_CHECKING
39 static void verify_live_on_entry (tree_live_info_p);
40 #endif
43 /* VARMAP maintains a mapping from SSA version number to real variables.
45 All SSA_NAMES are divided into partitions. Initially each ssa_name is the
46 only member of it's own partition. Coalescing will attempt to group any
47 ssa_names which occur in a copy or in a PHI node into the same partition.
49 At the end of out-of-ssa, each partition becomes a "real" variable and is
50 rewritten as a compiler variable.
52 The var_map data structure is used to manage these partitions. It allows
53 partitions to be combined, and determines which partition belongs to what
54 ssa_name or variable, and vice versa. */
57 /* Hashtable helpers. */
59 struct tree_int_map_hasher : typed_noop_remove <tree_int_map>
61 typedef tree_int_map value_type;
62 typedef tree_int_map compare_type;
63 static inline hashval_t hash (const value_type *);
64 static inline bool equal (const value_type *, const compare_type *);
67 inline hashval_t
68 tree_int_map_hasher::hash (const value_type *v)
70 return tree_map_base_hash (v);
73 inline bool
74 tree_int_map_hasher::equal (const value_type *v, const compare_type *c)
76 return tree_int_map_eq (v, c);
80 /* This routine will initialize the basevar fields of MAP. */
82 static void
83 var_map_base_init (var_map map)
85 int x, num_part;
86 tree var;
87 hash_table <tree_int_map_hasher> tree_to_index;
88 struct tree_int_map *m, *mapstorage;
90 num_part = num_var_partitions (map);
91 tree_to_index.create (num_part);
92 /* We can have at most num_part entries in the hash tables, so it's
93 enough to allocate so many map elements once, saving some malloc
94 calls. */
95 mapstorage = m = XNEWVEC (struct tree_int_map, num_part);
97 /* If a base table already exists, clear it, otherwise create it. */
98 free (map->partition_to_base_index);
99 map->partition_to_base_index = (int *) xmalloc (sizeof (int) * num_part);
101 /* Build the base variable list, and point partitions at their bases. */
102 for (x = 0; x < num_part; x++)
104 struct tree_int_map **slot;
105 unsigned baseindex;
106 var = partition_to_var (map, x);
107 if (SSA_NAME_VAR (var)
108 && (!VAR_P (SSA_NAME_VAR (var))
109 || !DECL_IGNORED_P (SSA_NAME_VAR (var))))
110 m->base.from = SSA_NAME_VAR (var);
111 else
112 /* This restricts what anonymous SSA names we can coalesce
113 as it restricts the sets we compute conflicts for.
114 Using TREE_TYPE to generate sets is the easies as
115 type equivalency also holds for SSA names with the same
116 underlying decl.
118 Check gimple_can_coalesce_p when changing this code. */
119 m->base.from = (TYPE_CANONICAL (TREE_TYPE (var))
120 ? TYPE_CANONICAL (TREE_TYPE (var))
121 : TREE_TYPE (var));
122 /* If base variable hasn't been seen, set it up. */
123 slot = tree_to_index.find_slot (m, INSERT);
124 if (!*slot)
126 baseindex = m - mapstorage;
127 m->to = baseindex;
128 *slot = m;
129 m++;
131 else
132 baseindex = (*slot)->to;
133 map->partition_to_base_index[x] = baseindex;
136 map->num_basevars = m - mapstorage;
138 free (mapstorage);
139 tree_to_index. dispose ();
143 /* Remove the base table in MAP. */
145 static void
146 var_map_base_fini (var_map map)
148 /* Free the basevar info if it is present. */
149 if (map->partition_to_base_index != NULL)
151 free (map->partition_to_base_index);
152 map->partition_to_base_index = NULL;
153 map->num_basevars = 0;
156 /* Create a variable partition map of SIZE, initialize and return it. */
158 var_map
159 init_var_map (int size)
161 var_map map;
163 map = (var_map) xmalloc (sizeof (struct _var_map));
164 map->var_partition = partition_new (size);
166 map->partition_to_view = NULL;
167 map->view_to_partition = NULL;
168 map->num_partitions = size;
169 map->partition_size = size;
170 map->num_basevars = 0;
171 map->partition_to_base_index = NULL;
172 return map;
176 /* Free memory associated with MAP. */
178 void
179 delete_var_map (var_map map)
181 var_map_base_fini (map);
182 partition_delete (map->var_partition);
183 free (map->partition_to_view);
184 free (map->view_to_partition);
185 free (map);
189 /* This function will combine the partitions in MAP for VAR1 and VAR2. It
190 Returns the partition which represents the new partition. If the two
191 partitions cannot be combined, NO_PARTITION is returned. */
194 var_union (var_map map, tree var1, tree var2)
196 int p1, p2, p3;
198 gcc_assert (TREE_CODE (var1) == SSA_NAME);
199 gcc_assert (TREE_CODE (var2) == SSA_NAME);
201 /* This is independent of partition_to_view. If partition_to_view is
202 on, then whichever one of these partitions is absorbed will never have a
203 dereference into the partition_to_view array any more. */
205 p1 = partition_find (map->var_partition, SSA_NAME_VERSION (var1));
206 p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2));
208 gcc_assert (p1 != NO_PARTITION);
209 gcc_assert (p2 != NO_PARTITION);
211 if (p1 == p2)
212 p3 = p1;
213 else
214 p3 = partition_union (map->var_partition, p1, p2);
216 if (map->partition_to_view)
217 p3 = map->partition_to_view[p3];
219 return p3;
223 /* Compress the partition numbers in MAP such that they fall in the range
224 0..(num_partitions-1) instead of wherever they turned out during
225 the partitioning exercise. This removes any references to unused
226 partitions, thereby allowing bitmaps and other vectors to be much
227 denser.
229 This is implemented such that compaction doesn't affect partitioning.
230 Ie., once partitions are created and possibly merged, running one
231 or more different kind of compaction will not affect the partitions
232 themselves. Their index might change, but all the same variables will
233 still be members of the same partition group. This allows work on reduced
234 sets, and no loss of information when a larger set is later desired.
236 In particular, coalescing can work on partitions which have 2 or more
237 definitions, and then 'recompact' later to include all the single
238 definitions for assignment to program variables. */
241 /* Set MAP back to the initial state of having no partition view. Return a
242 bitmap which has a bit set for each partition number which is in use in the
243 varmap. */
245 static bitmap
246 partition_view_init (var_map map)
248 bitmap used;
249 int tmp;
250 unsigned int x;
252 used = BITMAP_ALLOC (NULL);
254 /* Already in a view? Abandon the old one. */
255 if (map->partition_to_view)
257 free (map->partition_to_view);
258 map->partition_to_view = NULL;
260 if (map->view_to_partition)
262 free (map->view_to_partition);
263 map->view_to_partition = NULL;
266 /* Find out which partitions are actually referenced. */
267 for (x = 0; x < map->partition_size; x++)
269 tmp = partition_find (map->var_partition, x);
270 if (ssa_name (tmp) != NULL_TREE && !virtual_operand_p (ssa_name (tmp))
271 && (!has_zero_uses (ssa_name (tmp))
272 || !SSA_NAME_IS_DEFAULT_DEF (ssa_name (tmp))))
273 bitmap_set_bit (used, tmp);
276 map->num_partitions = map->partition_size;
277 return used;
281 /* This routine will finalize the view data for MAP based on the partitions
282 set in SELECTED. This is either the same bitmap returned from
283 partition_view_init, or a trimmed down version if some of those partitions
284 were not desired in this view. SELECTED is freed before returning. */
286 static void
287 partition_view_fini (var_map map, bitmap selected)
289 bitmap_iterator bi;
290 unsigned count, i, x, limit;
292 gcc_assert (selected);
294 count = bitmap_count_bits (selected);
295 limit = map->partition_size;
297 /* If its a one-to-one ratio, we don't need any view compaction. */
298 if (count < limit)
300 map->partition_to_view = (int *)xmalloc (limit * sizeof (int));
301 memset (map->partition_to_view, 0xff, (limit * sizeof (int)));
302 map->view_to_partition = (int *)xmalloc (count * sizeof (int));
304 i = 0;
305 /* Give each selected partition an index. */
306 EXECUTE_IF_SET_IN_BITMAP (selected, 0, x, bi)
308 map->partition_to_view[x] = i;
309 map->view_to_partition[i] = x;
310 i++;
312 gcc_assert (i == count);
313 map->num_partitions = i;
316 BITMAP_FREE (selected);
320 /* Create a partition view which includes all the used partitions in MAP. If
321 WANT_BASES is true, create the base variable map as well. */
323 void
324 partition_view_normal (var_map map, bool want_bases)
326 bitmap used;
328 used = partition_view_init (map);
329 partition_view_fini (map, used);
331 if (want_bases)
332 var_map_base_init (map);
333 else
334 var_map_base_fini (map);
338 /* Create a partition view in MAP which includes just partitions which occur in
339 the bitmap ONLY. If WANT_BASES is true, create the base variable map
340 as well. */
342 void
343 partition_view_bitmap (var_map map, bitmap only, bool want_bases)
345 bitmap used;
346 bitmap new_partitions = BITMAP_ALLOC (NULL);
347 unsigned x, p;
348 bitmap_iterator bi;
350 used = partition_view_init (map);
351 EXECUTE_IF_SET_IN_BITMAP (only, 0, x, bi)
353 p = partition_find (map->var_partition, x);
354 gcc_assert (bitmap_bit_p (used, p));
355 bitmap_set_bit (new_partitions, p);
357 partition_view_fini (map, new_partitions);
359 if (want_bases)
360 var_map_base_init (map);
361 else
362 var_map_base_fini (map);
366 static bitmap usedvars;
368 /* Mark VAR as used, so that it'll be preserved during rtl expansion.
369 Returns true if VAR wasn't marked before. */
371 static inline bool
372 set_is_used (tree var)
374 return bitmap_set_bit (usedvars, DECL_UID (var));
377 /* Return true if VAR is marked as used. */
379 static inline bool
380 is_used_p (tree var)
382 return bitmap_bit_p (usedvars, DECL_UID (var));
385 static inline void mark_all_vars_used (tree *);
387 /* Helper function for mark_all_vars_used, called via walk_tree. */
389 static tree
390 mark_all_vars_used_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
392 tree t = *tp;
393 enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
394 tree b;
396 if (TREE_CODE (t) == SSA_NAME)
398 *walk_subtrees = 0;
399 t = SSA_NAME_VAR (t);
400 if (!t)
401 return NULL;
404 if (IS_EXPR_CODE_CLASS (c)
405 && (b = TREE_BLOCK (t)) != NULL)
406 TREE_USED (b) = true;
408 /* Ignore TMR_OFFSET and TMR_STEP for TARGET_MEM_REFS, as those
409 fields do not contain vars. */
410 if (TREE_CODE (t) == TARGET_MEM_REF)
412 mark_all_vars_used (&TMR_BASE (t));
413 mark_all_vars_used (&TMR_INDEX (t));
414 mark_all_vars_used (&TMR_INDEX2 (t));
415 *walk_subtrees = 0;
416 return NULL;
419 /* Only need to mark VAR_DECLS; parameters and return results are not
420 eliminated as unused. */
421 if (TREE_CODE (t) == VAR_DECL)
423 /* When a global var becomes used for the first time also walk its
424 initializer (non global ones don't have any). */
425 if (set_is_used (t) && is_global_var (t))
426 mark_all_vars_used (&DECL_INITIAL (t));
428 /* remove_unused_scope_block_p requires information about labels
429 which are not DECL_IGNORED_P to tell if they might be used in the IL. */
430 else if (TREE_CODE (t) == LABEL_DECL)
431 /* Although the TREE_USED values that the frontend uses would be
432 acceptable (albeit slightly over-conservative) for our purposes,
433 init_vars_expansion clears TREE_USED for LABEL_DECLs too, so we
434 must re-compute it here. */
435 TREE_USED (t) = 1;
437 if (IS_TYPE_OR_DECL_P (t))
438 *walk_subtrees = 0;
440 return NULL;
443 /* Mark the scope block SCOPE and its subblocks unused when they can be
444 possibly eliminated if dead. */
446 static void
447 mark_scope_block_unused (tree scope)
449 tree t;
450 TREE_USED (scope) = false;
451 if (!(*debug_hooks->ignore_block) (scope))
452 TREE_USED (scope) = true;
453 for (t = BLOCK_SUBBLOCKS (scope); t ; t = BLOCK_CHAIN (t))
454 mark_scope_block_unused (t);
457 /* Look if the block is dead (by possibly eliminating its dead subblocks)
458 and return true if so.
459 Block is declared dead if:
460 1) No statements are associated with it.
461 2) Declares no live variables
462 3) All subblocks are dead
463 or there is precisely one subblocks and the block
464 has same abstract origin as outer block and declares
465 no variables, so it is pure wrapper.
466 When we are not outputting full debug info, we also eliminate dead variables
467 out of scope blocks to let them to be recycled by GGC and to save copying work
468 done by the inliner. */
470 static bool
471 remove_unused_scope_block_p (tree scope)
473 tree *t, *next;
474 bool unused = !TREE_USED (scope);
475 int nsubblocks = 0;
477 for (t = &BLOCK_VARS (scope); *t; t = next)
479 next = &DECL_CHAIN (*t);
481 /* Debug info of nested function refers to the block of the
482 function. We might stil call it even if all statements
483 of function it was nested into was elliminated.
485 TODO: We can actually look into cgraph to see if function
486 will be output to file. */
487 if (TREE_CODE (*t) == FUNCTION_DECL)
488 unused = false;
490 /* If a decl has a value expr, we need to instantiate it
491 regardless of debug info generation, to avoid codegen
492 differences in memory overlap tests. update_equiv_regs() may
493 indirectly call validate_equiv_mem() to test whether a
494 SET_DEST overlaps with others, and if the value expr changes
495 by virtual register instantiation, we may get end up with
496 different results. */
497 else if (TREE_CODE (*t) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (*t))
498 unused = false;
500 /* Remove everything we don't generate debug info for. */
501 else if (DECL_IGNORED_P (*t))
503 *t = DECL_CHAIN (*t);
504 next = t;
507 /* When we are outputting debug info, we usually want to output
508 info about optimized-out variables in the scope blocks.
509 Exception are the scope blocks not containing any instructions
510 at all so user can't get into the scopes at first place. */
511 else if (is_used_p (*t))
512 unused = false;
513 else if (TREE_CODE (*t) == LABEL_DECL && TREE_USED (*t))
514 /* For labels that are still used in the IL, the decision to
515 preserve them must not depend DEBUG_INFO_LEVEL, otherwise we
516 risk having different ordering in debug vs. non-debug builds
517 during inlining or versioning.
518 A label appearing here (we have already checked DECL_IGNORED_P)
519 should not be used in the IL unless it has been explicitly used
520 before, so we use TREE_USED as an approximation. */
521 /* In principle, we should do the same here as for the debug case
522 below, however, when debugging, there might be additional nested
523 levels that keep an upper level with a label live, so we have to
524 force this block to be considered used, too. */
525 unused = false;
527 /* When we are not doing full debug info, we however can keep around
528 only the used variables for cfgexpand's memory packing saving quite
529 a lot of memory.
531 For sake of -g3, we keep around those vars but we don't count this as
532 use of block, so innermost block with no used vars and no instructions
533 can be considered dead. We only want to keep around blocks user can
534 breakpoint into and ask about value of optimized out variables.
536 Similarly we need to keep around types at least until all
537 variables of all nested blocks are gone. We track no
538 information on whether given type is used or not, so we have
539 to keep them even when not emitting debug information,
540 otherwise we may end up remapping variables and their (local)
541 types in different orders depending on whether debug
542 information is being generated. */
544 else if (TREE_CODE (*t) == TYPE_DECL
545 || debug_info_level == DINFO_LEVEL_NORMAL
546 || debug_info_level == DINFO_LEVEL_VERBOSE)
548 else
550 *t = DECL_CHAIN (*t);
551 next = t;
555 for (t = &BLOCK_SUBBLOCKS (scope); *t ;)
556 if (remove_unused_scope_block_p (*t))
558 if (BLOCK_SUBBLOCKS (*t))
560 tree next = BLOCK_CHAIN (*t);
561 tree supercontext = BLOCK_SUPERCONTEXT (*t);
563 *t = BLOCK_SUBBLOCKS (*t);
564 while (BLOCK_CHAIN (*t))
566 BLOCK_SUPERCONTEXT (*t) = supercontext;
567 t = &BLOCK_CHAIN (*t);
569 BLOCK_CHAIN (*t) = next;
570 BLOCK_SUPERCONTEXT (*t) = supercontext;
571 t = &BLOCK_CHAIN (*t);
572 nsubblocks ++;
574 else
575 *t = BLOCK_CHAIN (*t);
577 else
579 t = &BLOCK_CHAIN (*t);
580 nsubblocks ++;
584 if (!unused)
586 /* Outer scope is always used. */
587 else if (!BLOCK_SUPERCONTEXT (scope)
588 || TREE_CODE (BLOCK_SUPERCONTEXT (scope)) == FUNCTION_DECL)
589 unused = false;
590 /* Innermost blocks with no live variables nor statements can be always
591 eliminated. */
592 else if (!nsubblocks)
594 /* For terse debug info we can eliminate info on unused variables. */
595 else if (debug_info_level == DINFO_LEVEL_NONE
596 || debug_info_level == DINFO_LEVEL_TERSE)
598 /* Even for -g0/-g1 don't prune outer scopes from artificial
599 functions, otherwise diagnostics using tree_nonartificial_location
600 will not be emitted properly. */
601 if (inlined_function_outer_scope_p (scope))
603 tree ao = scope;
605 while (ao
606 && TREE_CODE (ao) == BLOCK
607 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
608 ao = BLOCK_ABSTRACT_ORIGIN (ao);
609 if (ao
610 && TREE_CODE (ao) == FUNCTION_DECL
611 && DECL_DECLARED_INLINE_P (ao)
612 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
613 unused = false;
616 else if (BLOCK_VARS (scope) || BLOCK_NUM_NONLOCALIZED_VARS (scope))
617 unused = false;
618 /* See if this block is important for representation of inlined function.
619 Inlined functions are always represented by block with
620 block_ultimate_origin being set to FUNCTION_DECL and DECL_SOURCE_LOCATION
621 set... */
622 else if (inlined_function_outer_scope_p (scope))
623 unused = false;
624 else
625 /* Verfify that only blocks with source location set
626 are entry points to the inlined functions. */
627 gcc_assert (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (scope))
628 == UNKNOWN_LOCATION);
630 TREE_USED (scope) = !unused;
631 return unused;
634 /* Mark all VAR_DECLS under *EXPR_P as used, so that they won't be
635 eliminated during the tree->rtl conversion process. */
637 static inline void
638 mark_all_vars_used (tree *expr_p)
640 walk_tree (expr_p, mark_all_vars_used_1, NULL, NULL);
643 /* Helper function for clear_unused_block_pointer, called via walk_tree. */
645 static tree
646 clear_unused_block_pointer_1 (tree *tp, int *, void *)
648 if (EXPR_P (*tp) && TREE_BLOCK (*tp)
649 && !TREE_USED (TREE_BLOCK (*tp)))
650 TREE_SET_BLOCK (*tp, NULL);
651 return NULL_TREE;
654 /* Set all block pointer in debug or clobber stmt to NULL if the block
655 is unused, so that they will not be streamed out. */
657 static void
658 clear_unused_block_pointer (void)
660 basic_block bb;
661 gimple_stmt_iterator gsi;
663 FOR_EACH_BB (bb)
664 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
666 unsigned i;
667 tree b;
668 gimple stmt = gsi_stmt (gsi);
670 if (!is_gimple_debug (stmt) && !gimple_clobber_p (stmt))
671 continue;
672 b = gimple_block (stmt);
673 if (b && !TREE_USED (b))
674 gimple_set_block (stmt, NULL);
675 for (i = 0; i < gimple_num_ops (stmt); i++)
676 walk_tree (gimple_op_ptr (stmt, i), clear_unused_block_pointer_1,
677 NULL, NULL);
681 /* Dump scope blocks starting at SCOPE to FILE. INDENT is the
682 indentation level and FLAGS is as in print_generic_expr. */
684 static void
685 dump_scope_block (FILE *file, int indent, tree scope, int flags)
687 tree var, t;
688 unsigned int i;
690 fprintf (file, "\n%*s{ Scope block #%i%s%s",indent, "" , BLOCK_NUMBER (scope),
691 TREE_USED (scope) ? "" : " (unused)",
692 BLOCK_ABSTRACT (scope) ? " (abstract)": "");
693 if (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (scope)) != UNKNOWN_LOCATION)
695 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (scope));
696 fprintf (file, " %s:%i", s.file, s.line);
698 if (BLOCK_ABSTRACT_ORIGIN (scope))
700 tree origin = block_ultimate_origin (scope);
701 if (origin)
703 fprintf (file, " Originating from :");
704 if (DECL_P (origin))
705 print_generic_decl (file, origin, flags);
706 else
707 fprintf (file, "#%i", BLOCK_NUMBER (origin));
710 fprintf (file, " \n");
711 for (var = BLOCK_VARS (scope); var; var = DECL_CHAIN (var))
713 fprintf (file, "%*s", indent, "");
714 print_generic_decl (file, var, flags);
715 fprintf (file, "\n");
717 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (scope); i++)
719 fprintf (file, "%*s",indent, "");
720 print_generic_decl (file, BLOCK_NONLOCALIZED_VAR (scope, i),
721 flags);
722 fprintf (file, " (nonlocalized)\n");
724 for (t = BLOCK_SUBBLOCKS (scope); t ; t = BLOCK_CHAIN (t))
725 dump_scope_block (file, indent + 2, t, flags);
726 fprintf (file, "\n%*s}\n",indent, "");
729 /* Dump the tree of lexical scopes starting at SCOPE to stderr. FLAGS
730 is as in print_generic_expr. */
732 DEBUG_FUNCTION void
733 debug_scope_block (tree scope, int flags)
735 dump_scope_block (stderr, 0, scope, flags);
739 /* Dump the tree of lexical scopes of current_function_decl to FILE.
740 FLAGS is as in print_generic_expr. */
742 void
743 dump_scope_blocks (FILE *file, int flags)
745 dump_scope_block (file, 0, DECL_INITIAL (current_function_decl), flags);
749 /* Dump the tree of lexical scopes of current_function_decl to stderr.
750 FLAGS is as in print_generic_expr. */
752 DEBUG_FUNCTION void
753 debug_scope_blocks (int flags)
755 dump_scope_blocks (stderr, flags);
758 /* Remove local variables that are not referenced in the IL. */
760 void
761 remove_unused_locals (void)
763 basic_block bb;
764 tree var;
765 unsigned srcidx, dstidx, num;
766 bool have_local_clobbers = false;
768 /* Removing declarations from lexical blocks when not optimizing is
769 not only a waste of time, it actually causes differences in stack
770 layout. */
771 if (!optimize)
772 return;
774 timevar_push (TV_REMOVE_UNUSED);
776 mark_scope_block_unused (DECL_INITIAL (current_function_decl));
778 usedvars = BITMAP_ALLOC (NULL);
780 /* Walk the CFG marking all referenced symbols. */
781 FOR_EACH_BB (bb)
783 gimple_stmt_iterator gsi;
784 size_t i;
785 edge_iterator ei;
786 edge e;
788 /* Walk the statements. */
789 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
791 gimple stmt = gsi_stmt (gsi);
792 tree b = gimple_block (stmt);
794 if (is_gimple_debug (stmt))
795 continue;
797 if (gimple_clobber_p (stmt))
799 have_local_clobbers = true;
800 continue;
803 if (b)
804 TREE_USED (b) = true;
806 for (i = 0; i < gimple_num_ops (stmt); i++)
807 mark_all_vars_used (gimple_op_ptr (gsi_stmt (gsi), i));
810 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
812 use_operand_p arg_p;
813 ssa_op_iter i;
814 tree def;
815 gimple phi = gsi_stmt (gsi);
817 if (virtual_operand_p (gimple_phi_result (phi)))
818 continue;
820 def = gimple_phi_result (phi);
821 mark_all_vars_used (&def);
823 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_ALL_USES)
825 tree arg = USE_FROM_PTR (arg_p);
826 int index = PHI_ARG_INDEX_FROM_USE (arg_p);
827 tree block =
828 LOCATION_BLOCK (gimple_phi_arg_location (phi, index));
829 if (block != NULL)
830 TREE_USED (block) = true;
831 mark_all_vars_used (&arg);
835 FOR_EACH_EDGE (e, ei, bb->succs)
836 if (LOCATION_BLOCK (e->goto_locus) != NULL)
837 TREE_USED (LOCATION_BLOCK (e->goto_locus)) = true;
840 /* We do a two-pass approach about the out-of-scope clobbers. We want
841 to remove them if they are the only references to a local variable,
842 but we want to retain them when there's any other. So the first pass
843 ignores them, and the second pass (if there were any) tries to remove
844 them. */
845 if (have_local_clobbers)
846 FOR_EACH_BB (bb)
848 gimple_stmt_iterator gsi;
850 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
852 gimple stmt = gsi_stmt (gsi);
853 tree b = gimple_block (stmt);
855 if (gimple_clobber_p (stmt))
857 tree lhs = gimple_assign_lhs (stmt);
858 tree base = get_base_address (lhs);
859 /* Remove clobbers referencing unused vars, or clobbers
860 with MEM_REF lhs referencing uninitialized pointers. */
861 if ((TREE_CODE (base) == VAR_DECL && !is_used_p (base))
862 || (TREE_CODE (lhs) == MEM_REF
863 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME
864 && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (lhs, 0))
865 && (TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (lhs, 0)))
866 != PARM_DECL)))
868 unlink_stmt_vdef (stmt);
869 gsi_remove (&gsi, true);
870 release_defs (stmt);
871 continue;
873 if (b)
874 TREE_USED (b) = true;
876 gsi_next (&gsi);
880 cfun->has_local_explicit_reg_vars = false;
882 /* Remove unmarked local and global vars from local_decls. */
883 num = vec_safe_length (cfun->local_decls);
884 for (srcidx = 0, dstidx = 0; srcidx < num; srcidx++)
886 var = (*cfun->local_decls)[srcidx];
887 if (TREE_CODE (var) == VAR_DECL)
889 if (!is_used_p (var))
891 tree def;
892 if (cfun->nonlocal_goto_save_area
893 && TREE_OPERAND (cfun->nonlocal_goto_save_area, 0) == var)
894 cfun->nonlocal_goto_save_area = NULL;
895 /* Release any default def associated with var. */
896 if ((def = ssa_default_def (cfun, var)) != NULL_TREE)
898 set_ssa_default_def (cfun, var, NULL_TREE);
899 release_ssa_name (def);
901 continue;
904 if (TREE_CODE (var) == VAR_DECL
905 && DECL_HARD_REGISTER (var)
906 && !is_global_var (var))
907 cfun->has_local_explicit_reg_vars = true;
909 if (srcidx != dstidx)
910 (*cfun->local_decls)[dstidx] = var;
911 dstidx++;
913 if (dstidx != num)
915 statistics_counter_event (cfun, "unused VAR_DECLs removed", num - dstidx);
916 cfun->local_decls->truncate (dstidx);
919 remove_unused_scope_block_p (DECL_INITIAL (current_function_decl));
920 clear_unused_block_pointer ();
922 BITMAP_FREE (usedvars);
924 if (dump_file && (dump_flags & TDF_DETAILS))
926 fprintf (dump_file, "Scope blocks after cleanups:\n");
927 dump_scope_blocks (dump_file, dump_flags);
930 timevar_pop (TV_REMOVE_UNUSED);
933 /* Obstack for globale liveness info bitmaps. We don't want to put these
934 on the default obstack because these bitmaps can grow quite large and
935 we'll hold on to all that memory until the end of the compiler run.
936 As a bonus, delete_tree_live_info can destroy all the bitmaps by just
937 releasing the whole obstack. */
938 static bitmap_obstack liveness_bitmap_obstack;
940 /* Allocate and return a new live range information object base on MAP. */
942 static tree_live_info_p
943 new_tree_live_info (var_map map)
945 tree_live_info_p live;
946 basic_block bb;
948 live = XNEW (struct tree_live_info_d);
949 live->map = map;
950 live->num_blocks = last_basic_block;
952 live->livein = XNEWVEC (bitmap_head, last_basic_block);
953 FOR_EACH_BB (bb)
954 bitmap_initialize (&live->livein[bb->index], &liveness_bitmap_obstack);
956 live->liveout = XNEWVEC (bitmap_head, last_basic_block);
957 FOR_EACH_BB (bb)
958 bitmap_initialize (&live->liveout[bb->index], &liveness_bitmap_obstack);
960 live->work_stack = XNEWVEC (int, last_basic_block);
961 live->stack_top = live->work_stack;
963 live->global = BITMAP_ALLOC (&liveness_bitmap_obstack);
964 return live;
968 /* Free storage for live range info object LIVE. */
970 void
971 delete_tree_live_info (tree_live_info_p live)
973 bitmap_obstack_release (&liveness_bitmap_obstack);
974 free (live->work_stack);
975 free (live->liveout);
976 free (live->livein);
977 free (live);
981 /* Visit basic block BB and propagate any required live on entry bits from
982 LIVE into the predecessors. VISITED is the bitmap of visited blocks.
983 TMP is a temporary work bitmap which is passed in to avoid reallocating
984 it each time. */
986 static void
987 loe_visit_block (tree_live_info_p live, basic_block bb, sbitmap visited,
988 bitmap tmp)
990 edge e;
991 bool change;
992 edge_iterator ei;
993 basic_block pred_bb;
994 bitmap loe;
996 gcc_checking_assert (!bitmap_bit_p (visited, bb->index));
997 bitmap_set_bit (visited, bb->index);
999 loe = live_on_entry (live, bb);
1001 FOR_EACH_EDGE (e, ei, bb->preds)
1003 pred_bb = e->src;
1004 if (pred_bb == ENTRY_BLOCK_PTR)
1005 continue;
1006 /* TMP is variables live-on-entry from BB that aren't defined in the
1007 predecessor block. This should be the live on entry vars to pred.
1008 Note that liveout is the DEFs in a block while live on entry is
1009 being calculated. */
1010 bitmap_and_compl (tmp, loe, &live->liveout[pred_bb->index]);
1012 /* Add these bits to live-on-entry for the pred. if there are any
1013 changes, and pred_bb has been visited already, add it to the
1014 revisit stack. */
1015 change = bitmap_ior_into (live_on_entry (live, pred_bb), tmp);
1016 if (bitmap_bit_p (visited, pred_bb->index) && change)
1018 bitmap_clear_bit (visited, pred_bb->index);
1019 *(live->stack_top)++ = pred_bb->index;
1025 /* Using LIVE, fill in all the live-on-entry blocks between the defs and uses
1026 of all the variables. */
1028 static void
1029 live_worklist (tree_live_info_p live)
1031 unsigned b;
1032 basic_block bb;
1033 sbitmap visited = sbitmap_alloc (last_basic_block + 1);
1034 bitmap tmp = BITMAP_ALLOC (&liveness_bitmap_obstack);
1036 bitmap_clear (visited);
1038 /* Visit all the blocks in reverse order and propagate live on entry values
1039 into the predecessors blocks. */
1040 FOR_EACH_BB_REVERSE (bb)
1041 loe_visit_block (live, bb, visited, tmp);
1043 /* Process any blocks which require further iteration. */
1044 while (live->stack_top != live->work_stack)
1046 b = *--(live->stack_top);
1047 loe_visit_block (live, BASIC_BLOCK (b), visited, tmp);
1050 BITMAP_FREE (tmp);
1051 sbitmap_free (visited);
1055 /* Calculate the initial live on entry vector for SSA_NAME using immediate_use
1056 links. Set the live on entry fields in LIVE. Def's are marked temporarily
1057 in the liveout vector. */
1059 static void
1060 set_var_live_on_entry (tree ssa_name, tree_live_info_p live)
1062 int p;
1063 gimple stmt;
1064 use_operand_p use;
1065 basic_block def_bb = NULL;
1066 imm_use_iterator imm_iter;
1067 bool global = false;
1069 p = var_to_partition (live->map, ssa_name);
1070 if (p == NO_PARTITION)
1071 return;
1073 stmt = SSA_NAME_DEF_STMT (ssa_name);
1074 if (stmt)
1076 def_bb = gimple_bb (stmt);
1077 /* Mark defs in liveout bitmap temporarily. */
1078 if (def_bb)
1079 bitmap_set_bit (&live->liveout[def_bb->index], p);
1081 else
1082 def_bb = ENTRY_BLOCK_PTR;
1084 /* Visit each use of SSA_NAME and if it isn't in the same block as the def,
1085 add it to the list of live on entry blocks. */
1086 FOR_EACH_IMM_USE_FAST (use, imm_iter, ssa_name)
1088 gimple use_stmt = USE_STMT (use);
1089 basic_block add_block = NULL;
1091 if (gimple_code (use_stmt) == GIMPLE_PHI)
1093 /* Uses in PHI's are considered to be live at exit of the SRC block
1094 as this is where a copy would be inserted. Check to see if it is
1095 defined in that block, or whether its live on entry. */
1096 int index = PHI_ARG_INDEX_FROM_USE (use);
1097 edge e = gimple_phi_arg_edge (use_stmt, index);
1098 if (e->src != ENTRY_BLOCK_PTR)
1100 if (e->src != def_bb)
1101 add_block = e->src;
1104 else if (is_gimple_debug (use_stmt))
1105 continue;
1106 else
1108 /* If its not defined in this block, its live on entry. */
1109 basic_block use_bb = gimple_bb (use_stmt);
1110 if (use_bb != def_bb)
1111 add_block = use_bb;
1114 /* If there was a live on entry use, set the bit. */
1115 if (add_block)
1117 global = true;
1118 bitmap_set_bit (&live->livein[add_block->index], p);
1122 /* If SSA_NAME is live on entry to at least one block, fill in all the live
1123 on entry blocks between the def and all the uses. */
1124 if (global)
1125 bitmap_set_bit (live->global, p);
1129 /* Calculate the live on exit vectors based on the entry info in LIVEINFO. */
1131 void
1132 calculate_live_on_exit (tree_live_info_p liveinfo)
1134 basic_block bb;
1135 edge e;
1136 edge_iterator ei;
1138 /* live on entry calculations used liveout vectors for defs, clear them. */
1139 FOR_EACH_BB (bb)
1140 bitmap_clear (&liveinfo->liveout[bb->index]);
1142 /* Set all the live-on-exit bits for uses in PHIs. */
1143 FOR_EACH_BB (bb)
1145 gimple_stmt_iterator gsi;
1146 size_t i;
1148 /* Mark the PHI arguments which are live on exit to the pred block. */
1149 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1151 gimple phi = gsi_stmt (gsi);
1152 for (i = 0; i < gimple_phi_num_args (phi); i++)
1154 tree t = PHI_ARG_DEF (phi, i);
1155 int p;
1157 if (TREE_CODE (t) != SSA_NAME)
1158 continue;
1160 p = var_to_partition (liveinfo->map, t);
1161 if (p == NO_PARTITION)
1162 continue;
1163 e = gimple_phi_arg_edge (phi, i);
1164 if (e->src != ENTRY_BLOCK_PTR)
1165 bitmap_set_bit (&liveinfo->liveout[e->src->index], p);
1169 /* Add each successors live on entry to this bock live on exit. */
1170 FOR_EACH_EDGE (e, ei, bb->succs)
1171 if (e->dest != EXIT_BLOCK_PTR)
1172 bitmap_ior_into (&liveinfo->liveout[bb->index],
1173 live_on_entry (liveinfo, e->dest));
1178 /* Given partition map MAP, calculate all the live on entry bitmaps for
1179 each partition. Return a new live info object. */
1181 tree_live_info_p
1182 calculate_live_ranges (var_map map)
1184 tree var;
1185 unsigned i;
1186 tree_live_info_p live;
1188 bitmap_obstack_initialize (&liveness_bitmap_obstack);
1189 live = new_tree_live_info (map);
1190 for (i = 0; i < num_var_partitions (map); i++)
1192 var = partition_to_var (map, i);
1193 if (var != NULL_TREE)
1194 set_var_live_on_entry (var, live);
1197 live_worklist (live);
1199 #ifdef ENABLE_CHECKING
1200 verify_live_on_entry (live);
1201 #endif
1203 calculate_live_on_exit (live);
1204 return live;
1208 /* Output partition map MAP to file F. */
1210 void
1211 dump_var_map (FILE *f, var_map map)
1213 int t;
1214 unsigned x, y;
1215 int p;
1217 fprintf (f, "\nPartition map \n\n");
1219 for (x = 0; x < map->num_partitions; x++)
1221 if (map->view_to_partition != NULL)
1222 p = map->view_to_partition[x];
1223 else
1224 p = x;
1226 if (ssa_name (p) == NULL_TREE
1227 || virtual_operand_p (ssa_name (p)))
1228 continue;
1230 t = 0;
1231 for (y = 1; y < num_ssa_names; y++)
1233 p = partition_find (map->var_partition, y);
1234 if (map->partition_to_view)
1235 p = map->partition_to_view[p];
1236 if (p == (int)x)
1238 if (t++ == 0)
1240 fprintf (f, "Partition %d (", x);
1241 print_generic_expr (f, partition_to_var (map, p), TDF_SLIM);
1242 fprintf (f, " - ");
1244 fprintf (f, "%d ", y);
1247 if (t != 0)
1248 fprintf (f, ")\n");
1250 fprintf (f, "\n");
1254 /* Generic dump for the above. */
1256 DEBUG_FUNCTION void
1257 debug (_var_map &ref)
1259 dump_var_map (stderr, &ref);
1262 DEBUG_FUNCTION void
1263 debug (_var_map *ptr)
1265 if (ptr)
1266 debug (*ptr);
1267 else
1268 fprintf (stderr, "<nil>\n");
1272 /* Output live range info LIVE to file F, controlled by FLAG. */
1274 void
1275 dump_live_info (FILE *f, tree_live_info_p live, int flag)
1277 basic_block bb;
1278 unsigned i;
1279 var_map map = live->map;
1280 bitmap_iterator bi;
1282 if ((flag & LIVEDUMP_ENTRY) && live->livein)
1284 FOR_EACH_BB (bb)
1286 fprintf (f, "\nLive on entry to BB%d : ", bb->index);
1287 EXECUTE_IF_SET_IN_BITMAP (&live->livein[bb->index], 0, i, bi)
1289 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1290 fprintf (f, " ");
1292 fprintf (f, "\n");
1296 if ((flag & LIVEDUMP_EXIT) && live->liveout)
1298 FOR_EACH_BB (bb)
1300 fprintf (f, "\nLive on exit from BB%d : ", bb->index);
1301 EXECUTE_IF_SET_IN_BITMAP (&live->liveout[bb->index], 0, i, bi)
1303 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1304 fprintf (f, " ");
1306 fprintf (f, "\n");
1312 /* Generic dump for the above. */
1314 DEBUG_FUNCTION void
1315 debug (tree_live_info_d &ref)
1317 dump_live_info (stderr, &ref, 0);
1320 DEBUG_FUNCTION void
1321 debug (tree_live_info_d *ptr)
1323 if (ptr)
1324 debug (*ptr);
1325 else
1326 fprintf (stderr, "<nil>\n");
1330 #ifdef ENABLE_CHECKING
1331 /* Verify that SSA_VAR is a non-virtual SSA_NAME. */
1333 void
1334 register_ssa_partition_check (tree ssa_var)
1336 gcc_assert (TREE_CODE (ssa_var) == SSA_NAME);
1337 if (virtual_operand_p (ssa_var))
1339 fprintf (stderr, "Illegally registering a virtual SSA name :");
1340 print_generic_expr (stderr, ssa_var, TDF_SLIM);
1341 fprintf (stderr, " in the SSA->Normal phase.\n");
1342 internal_error ("SSA corruption");
1347 /* Verify that the info in LIVE matches the current cfg. */
1349 static void
1350 verify_live_on_entry (tree_live_info_p live)
1352 unsigned i;
1353 tree var;
1354 gimple stmt;
1355 basic_block bb;
1356 edge e;
1357 int num;
1358 edge_iterator ei;
1359 var_map map = live->map;
1361 /* Check for live on entry partitions and report those with a DEF in
1362 the program. This will typically mean an optimization has done
1363 something wrong. */
1364 bb = ENTRY_BLOCK_PTR;
1365 num = 0;
1366 FOR_EACH_EDGE (e, ei, bb->succs)
1368 int entry_block = e->dest->index;
1369 if (e->dest == EXIT_BLOCK_PTR)
1370 continue;
1371 for (i = 0; i < (unsigned)num_var_partitions (map); i++)
1373 basic_block tmp;
1374 tree d = NULL_TREE;
1375 bitmap loe;
1376 var = partition_to_var (map, i);
1377 stmt = SSA_NAME_DEF_STMT (var);
1378 tmp = gimple_bb (stmt);
1379 if (SSA_NAME_VAR (var))
1380 d = ssa_default_def (cfun, SSA_NAME_VAR (var));
1382 loe = live_on_entry (live, e->dest);
1383 if (loe && bitmap_bit_p (loe, i))
1385 if (!gimple_nop_p (stmt))
1387 num++;
1388 print_generic_expr (stderr, var, TDF_SLIM);
1389 fprintf (stderr, " is defined ");
1390 if (tmp)
1391 fprintf (stderr, " in BB%d, ", tmp->index);
1392 fprintf (stderr, "by:\n");
1393 print_gimple_stmt (stderr, stmt, 0, TDF_SLIM);
1394 fprintf (stderr, "\nIt is also live-on-entry to entry BB %d",
1395 entry_block);
1396 fprintf (stderr, " So it appears to have multiple defs.\n");
1398 else
1400 if (d != var)
1402 num++;
1403 print_generic_expr (stderr, var, TDF_SLIM);
1404 fprintf (stderr, " is live-on-entry to BB%d ",
1405 entry_block);
1406 if (d)
1408 fprintf (stderr, " but is not the default def of ");
1409 print_generic_expr (stderr, d, TDF_SLIM);
1410 fprintf (stderr, "\n");
1412 else
1413 fprintf (stderr, " and there is no default def.\n");
1417 else
1418 if (d == var)
1420 /* The only way this var shouldn't be marked live on entry is
1421 if it occurs in a PHI argument of the block. */
1422 size_t z;
1423 bool ok = false;
1424 gimple_stmt_iterator gsi;
1425 for (gsi = gsi_start_phis (e->dest);
1426 !gsi_end_p (gsi) && !ok;
1427 gsi_next (&gsi))
1429 gimple phi = gsi_stmt (gsi);
1430 for (z = 0; z < gimple_phi_num_args (phi); z++)
1431 if (var == gimple_phi_arg_def (phi, z))
1433 ok = true;
1434 break;
1437 if (ok)
1438 continue;
1439 num++;
1440 print_generic_expr (stderr, var, TDF_SLIM);
1441 fprintf (stderr, " is not marked live-on-entry to entry BB%d ",
1442 entry_block);
1443 fprintf (stderr, "but it is a default def so it should be.\n");
1447 gcc_assert (num <= 0);
1449 #endif