* include/bits/regex_compiler.h (__detail::__has_contiguous_iter):
[official-gcc.git] / gcc / tree-ssa-live.c
blob17849a21159445a10e90255154d71b07c1c9850e
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 "sbitmap.h"
30 #include "gimple.h"
31 #include "gimple-ssa.h"
32 #include "tree-phinodes.h"
33 #include "ssa-iterators.h"
34 #include "tree-ssanames.h"
35 #include "tree-dfa.h"
36 #include "timevar.h"
37 #include "dumpfile.h"
38 #include "tree-ssa-live.h"
39 #include "diagnostic-core.h"
40 #include "debug.h"
41 #include "flags.h"
43 #ifdef ENABLE_CHECKING
44 static void verify_live_on_entry (tree_live_info_p);
45 #endif
48 /* VARMAP maintains a mapping from SSA version number to real variables.
50 All SSA_NAMES are divided into partitions. Initially each ssa_name is the
51 only member of it's own partition. Coalescing will attempt to group any
52 ssa_names which occur in a copy or in a PHI node into the same partition.
54 At the end of out-of-ssa, each partition becomes a "real" variable and is
55 rewritten as a compiler variable.
57 The var_map data structure is used to manage these partitions. It allows
58 partitions to be combined, and determines which partition belongs to what
59 ssa_name or variable, and vice versa. */
62 /* Hashtable helpers. */
64 struct tree_int_map_hasher : typed_noop_remove <tree_int_map>
66 typedef tree_int_map value_type;
67 typedef tree_int_map compare_type;
68 static inline hashval_t hash (const value_type *);
69 static inline bool equal (const value_type *, const compare_type *);
72 inline hashval_t
73 tree_int_map_hasher::hash (const value_type *v)
75 return tree_map_base_hash (v);
78 inline bool
79 tree_int_map_hasher::equal (const value_type *v, const compare_type *c)
81 return tree_int_map_eq (v, c);
85 /* This routine will initialize the basevar fields of MAP. */
87 static void
88 var_map_base_init (var_map map)
90 int x, num_part;
91 tree var;
92 hash_table <tree_int_map_hasher> tree_to_index;
93 struct tree_int_map *m, *mapstorage;
95 num_part = num_var_partitions (map);
96 tree_to_index.create (num_part);
97 /* We can have at most num_part entries in the hash tables, so it's
98 enough to allocate so many map elements once, saving some malloc
99 calls. */
100 mapstorage = m = XNEWVEC (struct tree_int_map, num_part);
102 /* If a base table already exists, clear it, otherwise create it. */
103 free (map->partition_to_base_index);
104 map->partition_to_base_index = (int *) xmalloc (sizeof (int) * num_part);
106 /* Build the base variable list, and point partitions at their bases. */
107 for (x = 0; x < num_part; x++)
109 struct tree_int_map **slot;
110 unsigned baseindex;
111 var = partition_to_var (map, x);
112 if (SSA_NAME_VAR (var)
113 && (!VAR_P (SSA_NAME_VAR (var))
114 || !DECL_IGNORED_P (SSA_NAME_VAR (var))))
115 m->base.from = SSA_NAME_VAR (var);
116 else
117 /* This restricts what anonymous SSA names we can coalesce
118 as it restricts the sets we compute conflicts for.
119 Using TREE_TYPE to generate sets is the easies as
120 type equivalency also holds for SSA names with the same
121 underlying decl.
123 Check gimple_can_coalesce_p when changing this code. */
124 m->base.from = (TYPE_CANONICAL (TREE_TYPE (var))
125 ? TYPE_CANONICAL (TREE_TYPE (var))
126 : TREE_TYPE (var));
127 /* If base variable hasn't been seen, set it up. */
128 slot = tree_to_index.find_slot (m, INSERT);
129 if (!*slot)
131 baseindex = m - mapstorage;
132 m->to = baseindex;
133 *slot = m;
134 m++;
136 else
137 baseindex = (*slot)->to;
138 map->partition_to_base_index[x] = baseindex;
141 map->num_basevars = m - mapstorage;
143 free (mapstorage);
144 tree_to_index. dispose ();
148 /* Remove the base table in MAP. */
150 static void
151 var_map_base_fini (var_map map)
153 /* Free the basevar info if it is present. */
154 if (map->partition_to_base_index != NULL)
156 free (map->partition_to_base_index);
157 map->partition_to_base_index = NULL;
158 map->num_basevars = 0;
161 /* Create a variable partition map of SIZE, initialize and return it. */
163 var_map
164 init_var_map (int size)
166 var_map map;
168 map = (var_map) xmalloc (sizeof (struct _var_map));
169 map->var_partition = partition_new (size);
171 map->partition_to_view = NULL;
172 map->view_to_partition = NULL;
173 map->num_partitions = size;
174 map->partition_size = size;
175 map->num_basevars = 0;
176 map->partition_to_base_index = NULL;
177 return map;
181 /* Free memory associated with MAP. */
183 void
184 delete_var_map (var_map map)
186 var_map_base_fini (map);
187 partition_delete (map->var_partition);
188 free (map->partition_to_view);
189 free (map->view_to_partition);
190 free (map);
194 /* This function will combine the partitions in MAP for VAR1 and VAR2. It
195 Returns the partition which represents the new partition. If the two
196 partitions cannot be combined, NO_PARTITION is returned. */
199 var_union (var_map map, tree var1, tree var2)
201 int p1, p2, p3;
203 gcc_assert (TREE_CODE (var1) == SSA_NAME);
204 gcc_assert (TREE_CODE (var2) == SSA_NAME);
206 /* This is independent of partition_to_view. If partition_to_view is
207 on, then whichever one of these partitions is absorbed will never have a
208 dereference into the partition_to_view array any more. */
210 p1 = partition_find (map->var_partition, SSA_NAME_VERSION (var1));
211 p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2));
213 gcc_assert (p1 != NO_PARTITION);
214 gcc_assert (p2 != NO_PARTITION);
216 if (p1 == p2)
217 p3 = p1;
218 else
219 p3 = partition_union (map->var_partition, p1, p2);
221 if (map->partition_to_view)
222 p3 = map->partition_to_view[p3];
224 return p3;
228 /* Compress the partition numbers in MAP such that they fall in the range
229 0..(num_partitions-1) instead of wherever they turned out during
230 the partitioning exercise. This removes any references to unused
231 partitions, thereby allowing bitmaps and other vectors to be much
232 denser.
234 This is implemented such that compaction doesn't affect partitioning.
235 Ie., once partitions are created and possibly merged, running one
236 or more different kind of compaction will not affect the partitions
237 themselves. Their index might change, but all the same variables will
238 still be members of the same partition group. This allows work on reduced
239 sets, and no loss of information when a larger set is later desired.
241 In particular, coalescing can work on partitions which have 2 or more
242 definitions, and then 'recompact' later to include all the single
243 definitions for assignment to program variables. */
246 /* Set MAP back to the initial state of having no partition view. Return a
247 bitmap which has a bit set for each partition number which is in use in the
248 varmap. */
250 static bitmap
251 partition_view_init (var_map map)
253 bitmap used;
254 int tmp;
255 unsigned int x;
257 used = BITMAP_ALLOC (NULL);
259 /* Already in a view? Abandon the old one. */
260 if (map->partition_to_view)
262 free (map->partition_to_view);
263 map->partition_to_view = NULL;
265 if (map->view_to_partition)
267 free (map->view_to_partition);
268 map->view_to_partition = NULL;
271 /* Find out which partitions are actually referenced. */
272 for (x = 0; x < map->partition_size; x++)
274 tmp = partition_find (map->var_partition, x);
275 if (ssa_name (tmp) != NULL_TREE && !virtual_operand_p (ssa_name (tmp))
276 && (!has_zero_uses (ssa_name (tmp))
277 || !SSA_NAME_IS_DEFAULT_DEF (ssa_name (tmp))))
278 bitmap_set_bit (used, tmp);
281 map->num_partitions = map->partition_size;
282 return used;
286 /* This routine will finalize the view data for MAP based on the partitions
287 set in SELECTED. This is either the same bitmap returned from
288 partition_view_init, or a trimmed down version if some of those partitions
289 were not desired in this view. SELECTED is freed before returning. */
291 static void
292 partition_view_fini (var_map map, bitmap selected)
294 bitmap_iterator bi;
295 unsigned count, i, x, limit;
297 gcc_assert (selected);
299 count = bitmap_count_bits (selected);
300 limit = map->partition_size;
302 /* If its a one-to-one ratio, we don't need any view compaction. */
303 if (count < limit)
305 map->partition_to_view = (int *)xmalloc (limit * sizeof (int));
306 memset (map->partition_to_view, 0xff, (limit * sizeof (int)));
307 map->view_to_partition = (int *)xmalloc (count * sizeof (int));
309 i = 0;
310 /* Give each selected partition an index. */
311 EXECUTE_IF_SET_IN_BITMAP (selected, 0, x, bi)
313 map->partition_to_view[x] = i;
314 map->view_to_partition[i] = x;
315 i++;
317 gcc_assert (i == count);
318 map->num_partitions = i;
321 BITMAP_FREE (selected);
325 /* Create a partition view which includes all the used partitions in MAP. If
326 WANT_BASES is true, create the base variable map as well. */
328 void
329 partition_view_normal (var_map map, bool want_bases)
331 bitmap used;
333 used = partition_view_init (map);
334 partition_view_fini (map, used);
336 if (want_bases)
337 var_map_base_init (map);
338 else
339 var_map_base_fini (map);
343 /* Create a partition view in MAP which includes just partitions which occur in
344 the bitmap ONLY. If WANT_BASES is true, create the base variable map
345 as well. */
347 void
348 partition_view_bitmap (var_map map, bitmap only, bool want_bases)
350 bitmap used;
351 bitmap new_partitions = BITMAP_ALLOC (NULL);
352 unsigned x, p;
353 bitmap_iterator bi;
355 used = partition_view_init (map);
356 EXECUTE_IF_SET_IN_BITMAP (only, 0, x, bi)
358 p = partition_find (map->var_partition, x);
359 gcc_assert (bitmap_bit_p (used, p));
360 bitmap_set_bit (new_partitions, p);
362 partition_view_fini (map, new_partitions);
364 if (want_bases)
365 var_map_base_init (map);
366 else
367 var_map_base_fini (map);
371 static bitmap usedvars;
373 /* Mark VAR as used, so that it'll be preserved during rtl expansion.
374 Returns true if VAR wasn't marked before. */
376 static inline bool
377 set_is_used (tree var)
379 return bitmap_set_bit (usedvars, DECL_UID (var));
382 /* Return true if VAR is marked as used. */
384 static inline bool
385 is_used_p (tree var)
387 return bitmap_bit_p (usedvars, DECL_UID (var));
390 static inline void mark_all_vars_used (tree *);
392 /* Helper function for mark_all_vars_used, called via walk_tree. */
394 static tree
395 mark_all_vars_used_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
397 tree t = *tp;
398 enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
399 tree b;
401 if (TREE_CODE (t) == SSA_NAME)
403 *walk_subtrees = 0;
404 t = SSA_NAME_VAR (t);
405 if (!t)
406 return NULL;
409 if (IS_EXPR_CODE_CLASS (c)
410 && (b = TREE_BLOCK (t)) != NULL)
411 TREE_USED (b) = true;
413 /* Ignore TMR_OFFSET and TMR_STEP for TARGET_MEM_REFS, as those
414 fields do not contain vars. */
415 if (TREE_CODE (t) == TARGET_MEM_REF)
417 mark_all_vars_used (&TMR_BASE (t));
418 mark_all_vars_used (&TMR_INDEX (t));
419 mark_all_vars_used (&TMR_INDEX2 (t));
420 *walk_subtrees = 0;
421 return NULL;
424 /* Only need to mark VAR_DECLS; parameters and return results are not
425 eliminated as unused. */
426 if (TREE_CODE (t) == VAR_DECL)
428 /* When a global var becomes used for the first time also walk its
429 initializer (non global ones don't have any). */
430 if (set_is_used (t) && is_global_var (t))
431 mark_all_vars_used (&DECL_INITIAL (t));
433 /* remove_unused_scope_block_p requires information about labels
434 which are not DECL_IGNORED_P to tell if they might be used in the IL. */
435 else if (TREE_CODE (t) == LABEL_DECL)
436 /* Although the TREE_USED values that the frontend uses would be
437 acceptable (albeit slightly over-conservative) for our purposes,
438 init_vars_expansion clears TREE_USED for LABEL_DECLs too, so we
439 must re-compute it here. */
440 TREE_USED (t) = 1;
442 if (IS_TYPE_OR_DECL_P (t))
443 *walk_subtrees = 0;
445 return NULL;
448 /* Mark the scope block SCOPE and its subblocks unused when they can be
449 possibly eliminated if dead. */
451 static void
452 mark_scope_block_unused (tree scope)
454 tree t;
455 TREE_USED (scope) = false;
456 if (!(*debug_hooks->ignore_block) (scope))
457 TREE_USED (scope) = true;
458 for (t = BLOCK_SUBBLOCKS (scope); t ; t = BLOCK_CHAIN (t))
459 mark_scope_block_unused (t);
462 /* Look if the block is dead (by possibly eliminating its dead subblocks)
463 and return true if so.
464 Block is declared dead if:
465 1) No statements are associated with it.
466 2) Declares no live variables
467 3) All subblocks are dead
468 or there is precisely one subblocks and the block
469 has same abstract origin as outer block and declares
470 no variables, so it is pure wrapper.
471 When we are not outputting full debug info, we also eliminate dead variables
472 out of scope blocks to let them to be recycled by GGC and to save copying work
473 done by the inliner. */
475 static bool
476 remove_unused_scope_block_p (tree scope)
478 tree *t, *next;
479 bool unused = !TREE_USED (scope);
480 int nsubblocks = 0;
482 for (t = &BLOCK_VARS (scope); *t; t = next)
484 next = &DECL_CHAIN (*t);
486 /* Debug info of nested function refers to the block of the
487 function. We might stil call it even if all statements
488 of function it was nested into was elliminated.
490 TODO: We can actually look into cgraph to see if function
491 will be output to file. */
492 if (TREE_CODE (*t) == FUNCTION_DECL)
493 unused = false;
495 /* If a decl has a value expr, we need to instantiate it
496 regardless of debug info generation, to avoid codegen
497 differences in memory overlap tests. update_equiv_regs() may
498 indirectly call validate_equiv_mem() to test whether a
499 SET_DEST overlaps with others, and if the value expr changes
500 by virtual register instantiation, we may get end up with
501 different results. */
502 else if (TREE_CODE (*t) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (*t))
503 unused = false;
505 /* Remove everything we don't generate debug info for. */
506 else if (DECL_IGNORED_P (*t))
508 *t = DECL_CHAIN (*t);
509 next = t;
512 /* When we are outputting debug info, we usually want to output
513 info about optimized-out variables in the scope blocks.
514 Exception are the scope blocks not containing any instructions
515 at all so user can't get into the scopes at first place. */
516 else if (is_used_p (*t))
517 unused = false;
518 else if (TREE_CODE (*t) == LABEL_DECL && TREE_USED (*t))
519 /* For labels that are still used in the IL, the decision to
520 preserve them must not depend DEBUG_INFO_LEVEL, otherwise we
521 risk having different ordering in debug vs. non-debug builds
522 during inlining or versioning.
523 A label appearing here (we have already checked DECL_IGNORED_P)
524 should not be used in the IL unless it has been explicitly used
525 before, so we use TREE_USED as an approximation. */
526 /* In principle, we should do the same here as for the debug case
527 below, however, when debugging, there might be additional nested
528 levels that keep an upper level with a label live, so we have to
529 force this block to be considered used, too. */
530 unused = false;
532 /* When we are not doing full debug info, we however can keep around
533 only the used variables for cfgexpand's memory packing saving quite
534 a lot of memory.
536 For sake of -g3, we keep around those vars but we don't count this as
537 use of block, so innermost block with no used vars and no instructions
538 can be considered dead. We only want to keep around blocks user can
539 breakpoint into and ask about value of optimized out variables.
541 Similarly we need to keep around types at least until all
542 variables of all nested blocks are gone. We track no
543 information on whether given type is used or not, so we have
544 to keep them even when not emitting debug information,
545 otherwise we may end up remapping variables and their (local)
546 types in different orders depending on whether debug
547 information is being generated. */
549 else if (TREE_CODE (*t) == TYPE_DECL
550 || debug_info_level == DINFO_LEVEL_NORMAL
551 || debug_info_level == DINFO_LEVEL_VERBOSE)
553 else
555 *t = DECL_CHAIN (*t);
556 next = t;
560 for (t = &BLOCK_SUBBLOCKS (scope); *t ;)
561 if (remove_unused_scope_block_p (*t))
563 if (BLOCK_SUBBLOCKS (*t))
565 tree next = BLOCK_CHAIN (*t);
566 tree supercontext = BLOCK_SUPERCONTEXT (*t);
568 *t = BLOCK_SUBBLOCKS (*t);
569 while (BLOCK_CHAIN (*t))
571 BLOCK_SUPERCONTEXT (*t) = supercontext;
572 t = &BLOCK_CHAIN (*t);
574 BLOCK_CHAIN (*t) = next;
575 BLOCK_SUPERCONTEXT (*t) = supercontext;
576 t = &BLOCK_CHAIN (*t);
577 nsubblocks ++;
579 else
580 *t = BLOCK_CHAIN (*t);
582 else
584 t = &BLOCK_CHAIN (*t);
585 nsubblocks ++;
589 if (!unused)
591 /* Outer scope is always used. */
592 else if (!BLOCK_SUPERCONTEXT (scope)
593 || TREE_CODE (BLOCK_SUPERCONTEXT (scope)) == FUNCTION_DECL)
594 unused = false;
595 /* Innermost blocks with no live variables nor statements can be always
596 eliminated. */
597 else if (!nsubblocks)
599 /* For terse debug info we can eliminate info on unused variables. */
600 else if (debug_info_level == DINFO_LEVEL_NONE
601 || debug_info_level == DINFO_LEVEL_TERSE)
603 /* Even for -g0/-g1 don't prune outer scopes from artificial
604 functions, otherwise diagnostics using tree_nonartificial_location
605 will not be emitted properly. */
606 if (inlined_function_outer_scope_p (scope))
608 tree ao = scope;
610 while (ao
611 && TREE_CODE (ao) == BLOCK
612 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
613 ao = BLOCK_ABSTRACT_ORIGIN (ao);
614 if (ao
615 && TREE_CODE (ao) == FUNCTION_DECL
616 && DECL_DECLARED_INLINE_P (ao)
617 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
618 unused = false;
621 else if (BLOCK_VARS (scope) || BLOCK_NUM_NONLOCALIZED_VARS (scope))
622 unused = false;
623 /* See if this block is important for representation of inlined function.
624 Inlined functions are always represented by block with
625 block_ultimate_origin being set to FUNCTION_DECL and DECL_SOURCE_LOCATION
626 set... */
627 else if (inlined_function_outer_scope_p (scope))
628 unused = false;
629 else
630 /* Verfify that only blocks with source location set
631 are entry points to the inlined functions. */
632 gcc_assert (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (scope))
633 == UNKNOWN_LOCATION);
635 TREE_USED (scope) = !unused;
636 return unused;
639 /* Mark all VAR_DECLS under *EXPR_P as used, so that they won't be
640 eliminated during the tree->rtl conversion process. */
642 static inline void
643 mark_all_vars_used (tree *expr_p)
645 walk_tree (expr_p, mark_all_vars_used_1, NULL, NULL);
648 /* Helper function for clear_unused_block_pointer, called via walk_tree. */
650 static tree
651 clear_unused_block_pointer_1 (tree *tp, int *, void *)
653 if (EXPR_P (*tp) && TREE_BLOCK (*tp)
654 && !TREE_USED (TREE_BLOCK (*tp)))
655 TREE_SET_BLOCK (*tp, NULL);
656 return NULL_TREE;
659 /* Set all block pointer in debug or clobber stmt to NULL if the block
660 is unused, so that they will not be streamed out. */
662 static void
663 clear_unused_block_pointer (void)
665 basic_block bb;
666 gimple_stmt_iterator gsi;
668 FOR_EACH_BB (bb)
669 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
671 unsigned i;
672 tree b;
673 gimple stmt = gsi_stmt (gsi);
675 if (!is_gimple_debug (stmt) && !gimple_clobber_p (stmt))
676 continue;
677 b = gimple_block (stmt);
678 if (b && !TREE_USED (b))
679 gimple_set_block (stmt, NULL);
680 for (i = 0; i < gimple_num_ops (stmt); i++)
681 walk_tree (gimple_op_ptr (stmt, i), clear_unused_block_pointer_1,
682 NULL, NULL);
686 /* Dump scope blocks starting at SCOPE to FILE. INDENT is the
687 indentation level and FLAGS is as in print_generic_expr. */
689 static void
690 dump_scope_block (FILE *file, int indent, tree scope, int flags)
692 tree var, t;
693 unsigned int i;
695 fprintf (file, "\n%*s{ Scope block #%i%s%s",indent, "" , BLOCK_NUMBER (scope),
696 TREE_USED (scope) ? "" : " (unused)",
697 BLOCK_ABSTRACT (scope) ? " (abstract)": "");
698 if (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (scope)) != UNKNOWN_LOCATION)
700 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (scope));
701 fprintf (file, " %s:%i", s.file, s.line);
703 if (BLOCK_ABSTRACT_ORIGIN (scope))
705 tree origin = block_ultimate_origin (scope);
706 if (origin)
708 fprintf (file, " Originating from :");
709 if (DECL_P (origin))
710 print_generic_decl (file, origin, flags);
711 else
712 fprintf (file, "#%i", BLOCK_NUMBER (origin));
715 fprintf (file, " \n");
716 for (var = BLOCK_VARS (scope); var; var = DECL_CHAIN (var))
718 fprintf (file, "%*s", indent, "");
719 print_generic_decl (file, var, flags);
720 fprintf (file, "\n");
722 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (scope); i++)
724 fprintf (file, "%*s",indent, "");
725 print_generic_decl (file, BLOCK_NONLOCALIZED_VAR (scope, i),
726 flags);
727 fprintf (file, " (nonlocalized)\n");
729 for (t = BLOCK_SUBBLOCKS (scope); t ; t = BLOCK_CHAIN (t))
730 dump_scope_block (file, indent + 2, t, flags);
731 fprintf (file, "\n%*s}\n",indent, "");
734 /* Dump the tree of lexical scopes starting at SCOPE to stderr. FLAGS
735 is as in print_generic_expr. */
737 DEBUG_FUNCTION void
738 debug_scope_block (tree scope, int flags)
740 dump_scope_block (stderr, 0, scope, flags);
744 /* Dump the tree of lexical scopes of current_function_decl to FILE.
745 FLAGS is as in print_generic_expr. */
747 void
748 dump_scope_blocks (FILE *file, int flags)
750 dump_scope_block (file, 0, DECL_INITIAL (current_function_decl), flags);
754 /* Dump the tree of lexical scopes of current_function_decl to stderr.
755 FLAGS is as in print_generic_expr. */
757 DEBUG_FUNCTION void
758 debug_scope_blocks (int flags)
760 dump_scope_blocks (stderr, flags);
763 /* Remove local variables that are not referenced in the IL. */
765 void
766 remove_unused_locals (void)
768 basic_block bb;
769 tree var;
770 unsigned srcidx, dstidx, num;
771 bool have_local_clobbers = false;
773 /* Removing declarations from lexical blocks when not optimizing is
774 not only a waste of time, it actually causes differences in stack
775 layout. */
776 if (!optimize)
777 return;
779 timevar_push (TV_REMOVE_UNUSED);
781 mark_scope_block_unused (DECL_INITIAL (current_function_decl));
783 usedvars = BITMAP_ALLOC (NULL);
785 /* Walk the CFG marking all referenced symbols. */
786 FOR_EACH_BB (bb)
788 gimple_stmt_iterator gsi;
789 size_t i;
790 edge_iterator ei;
791 edge e;
793 /* Walk the statements. */
794 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
796 gimple stmt = gsi_stmt (gsi);
797 tree b = gimple_block (stmt);
799 if (is_gimple_debug (stmt))
800 continue;
802 if (gimple_clobber_p (stmt))
804 have_local_clobbers = true;
805 continue;
808 if (b)
809 TREE_USED (b) = true;
811 for (i = 0; i < gimple_num_ops (stmt); i++)
812 mark_all_vars_used (gimple_op_ptr (gsi_stmt (gsi), i));
815 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
817 use_operand_p arg_p;
818 ssa_op_iter i;
819 tree def;
820 gimple phi = gsi_stmt (gsi);
822 if (virtual_operand_p (gimple_phi_result (phi)))
823 continue;
825 def = gimple_phi_result (phi);
826 mark_all_vars_used (&def);
828 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_ALL_USES)
830 tree arg = USE_FROM_PTR (arg_p);
831 int index = PHI_ARG_INDEX_FROM_USE (arg_p);
832 tree block =
833 LOCATION_BLOCK (gimple_phi_arg_location (phi, index));
834 if (block != NULL)
835 TREE_USED (block) = true;
836 mark_all_vars_used (&arg);
840 FOR_EACH_EDGE (e, ei, bb->succs)
841 if (LOCATION_BLOCK (e->goto_locus) != NULL)
842 TREE_USED (LOCATION_BLOCK (e->goto_locus)) = true;
845 /* We do a two-pass approach about the out-of-scope clobbers. We want
846 to remove them if they are the only references to a local variable,
847 but we want to retain them when there's any other. So the first pass
848 ignores them, and the second pass (if there were any) tries to remove
849 them. */
850 if (have_local_clobbers)
851 FOR_EACH_BB (bb)
853 gimple_stmt_iterator gsi;
855 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
857 gimple stmt = gsi_stmt (gsi);
858 tree b = gimple_block (stmt);
860 if (gimple_clobber_p (stmt))
862 tree lhs = gimple_assign_lhs (stmt);
863 tree base = get_base_address (lhs);
864 /* Remove clobbers referencing unused vars, or clobbers
865 with MEM_REF lhs referencing uninitialized pointers. */
866 if ((TREE_CODE (base) == VAR_DECL && !is_used_p (base))
867 || (TREE_CODE (lhs) == MEM_REF
868 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME
869 && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (lhs, 0))
870 && (TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (lhs, 0)))
871 != PARM_DECL)))
873 unlink_stmt_vdef (stmt);
874 gsi_remove (&gsi, true);
875 release_defs (stmt);
876 continue;
878 if (b)
879 TREE_USED (b) = true;
881 gsi_next (&gsi);
885 cfun->has_local_explicit_reg_vars = false;
887 /* Remove unmarked local and global vars from local_decls. */
888 num = vec_safe_length (cfun->local_decls);
889 for (srcidx = 0, dstidx = 0; srcidx < num; srcidx++)
891 var = (*cfun->local_decls)[srcidx];
892 if (TREE_CODE (var) == VAR_DECL)
894 if (!is_used_p (var))
896 tree def;
897 if (cfun->nonlocal_goto_save_area
898 && TREE_OPERAND (cfun->nonlocal_goto_save_area, 0) == var)
899 cfun->nonlocal_goto_save_area = NULL;
900 /* Release any default def associated with var. */
901 if ((def = ssa_default_def (cfun, var)) != NULL_TREE)
903 set_ssa_default_def (cfun, var, NULL_TREE);
904 release_ssa_name (def);
906 continue;
909 if (TREE_CODE (var) == VAR_DECL
910 && DECL_HARD_REGISTER (var)
911 && !is_global_var (var))
912 cfun->has_local_explicit_reg_vars = true;
914 if (srcidx != dstidx)
915 (*cfun->local_decls)[dstidx] = var;
916 dstidx++;
918 if (dstidx != num)
920 statistics_counter_event (cfun, "unused VAR_DECLs removed", num - dstidx);
921 cfun->local_decls->truncate (dstidx);
924 remove_unused_scope_block_p (DECL_INITIAL (current_function_decl));
925 clear_unused_block_pointer ();
927 BITMAP_FREE (usedvars);
929 if (dump_file && (dump_flags & TDF_DETAILS))
931 fprintf (dump_file, "Scope blocks after cleanups:\n");
932 dump_scope_blocks (dump_file, dump_flags);
935 timevar_pop (TV_REMOVE_UNUSED);
938 /* Obstack for globale liveness info bitmaps. We don't want to put these
939 on the default obstack because these bitmaps can grow quite large and
940 we'll hold on to all that memory until the end of the compiler run.
941 As a bonus, delete_tree_live_info can destroy all the bitmaps by just
942 releasing the whole obstack. */
943 static bitmap_obstack liveness_bitmap_obstack;
945 /* Allocate and return a new live range information object base on MAP. */
947 static tree_live_info_p
948 new_tree_live_info (var_map map)
950 tree_live_info_p live;
951 basic_block bb;
953 live = XNEW (struct tree_live_info_d);
954 live->map = map;
955 live->num_blocks = last_basic_block;
957 live->livein = XNEWVEC (bitmap_head, last_basic_block);
958 FOR_EACH_BB (bb)
959 bitmap_initialize (&live->livein[bb->index], &liveness_bitmap_obstack);
961 live->liveout = XNEWVEC (bitmap_head, last_basic_block);
962 FOR_EACH_BB (bb)
963 bitmap_initialize (&live->liveout[bb->index], &liveness_bitmap_obstack);
965 live->work_stack = XNEWVEC (int, last_basic_block);
966 live->stack_top = live->work_stack;
968 live->global = BITMAP_ALLOC (&liveness_bitmap_obstack);
969 return live;
973 /* Free storage for live range info object LIVE. */
975 void
976 delete_tree_live_info (tree_live_info_p live)
978 bitmap_obstack_release (&liveness_bitmap_obstack);
979 free (live->work_stack);
980 free (live->liveout);
981 free (live->livein);
982 free (live);
986 /* Visit basic block BB and propagate any required live on entry bits from
987 LIVE into the predecessors. VISITED is the bitmap of visited blocks.
988 TMP is a temporary work bitmap which is passed in to avoid reallocating
989 it each time. */
991 static void
992 loe_visit_block (tree_live_info_p live, basic_block bb, sbitmap visited,
993 bitmap tmp)
995 edge e;
996 bool change;
997 edge_iterator ei;
998 basic_block pred_bb;
999 bitmap loe;
1001 gcc_checking_assert (!bitmap_bit_p (visited, bb->index));
1002 bitmap_set_bit (visited, bb->index);
1004 loe = live_on_entry (live, bb);
1006 FOR_EACH_EDGE (e, ei, bb->preds)
1008 pred_bb = e->src;
1009 if (pred_bb == ENTRY_BLOCK_PTR)
1010 continue;
1011 /* TMP is variables live-on-entry from BB that aren't defined in the
1012 predecessor block. This should be the live on entry vars to pred.
1013 Note that liveout is the DEFs in a block while live on entry is
1014 being calculated. */
1015 bitmap_and_compl (tmp, loe, &live->liveout[pred_bb->index]);
1017 /* Add these bits to live-on-entry for the pred. if there are any
1018 changes, and pred_bb has been visited already, add it to the
1019 revisit stack. */
1020 change = bitmap_ior_into (live_on_entry (live, pred_bb), tmp);
1021 if (bitmap_bit_p (visited, pred_bb->index) && change)
1023 bitmap_clear_bit (visited, pred_bb->index);
1024 *(live->stack_top)++ = pred_bb->index;
1030 /* Using LIVE, fill in all the live-on-entry blocks between the defs and uses
1031 of all the variables. */
1033 static void
1034 live_worklist (tree_live_info_p live)
1036 unsigned b;
1037 basic_block bb;
1038 sbitmap visited = sbitmap_alloc (last_basic_block + 1);
1039 bitmap tmp = BITMAP_ALLOC (&liveness_bitmap_obstack);
1041 bitmap_clear (visited);
1043 /* Visit all the blocks in reverse order and propagate live on entry values
1044 into the predecessors blocks. */
1045 FOR_EACH_BB_REVERSE (bb)
1046 loe_visit_block (live, bb, visited, tmp);
1048 /* Process any blocks which require further iteration. */
1049 while (live->stack_top != live->work_stack)
1051 b = *--(live->stack_top);
1052 loe_visit_block (live, BASIC_BLOCK (b), visited, tmp);
1055 BITMAP_FREE (tmp);
1056 sbitmap_free (visited);
1060 /* Calculate the initial live on entry vector for SSA_NAME using immediate_use
1061 links. Set the live on entry fields in LIVE. Def's are marked temporarily
1062 in the liveout vector. */
1064 static void
1065 set_var_live_on_entry (tree ssa_name, tree_live_info_p live)
1067 int p;
1068 gimple stmt;
1069 use_operand_p use;
1070 basic_block def_bb = NULL;
1071 imm_use_iterator imm_iter;
1072 bool global = false;
1074 p = var_to_partition (live->map, ssa_name);
1075 if (p == NO_PARTITION)
1076 return;
1078 stmt = SSA_NAME_DEF_STMT (ssa_name);
1079 if (stmt)
1081 def_bb = gimple_bb (stmt);
1082 /* Mark defs in liveout bitmap temporarily. */
1083 if (def_bb)
1084 bitmap_set_bit (&live->liveout[def_bb->index], p);
1086 else
1087 def_bb = ENTRY_BLOCK_PTR;
1089 /* Visit each use of SSA_NAME and if it isn't in the same block as the def,
1090 add it to the list of live on entry blocks. */
1091 FOR_EACH_IMM_USE_FAST (use, imm_iter, ssa_name)
1093 gimple use_stmt = USE_STMT (use);
1094 basic_block add_block = NULL;
1096 if (gimple_code (use_stmt) == GIMPLE_PHI)
1098 /* Uses in PHI's are considered to be live at exit of the SRC block
1099 as this is where a copy would be inserted. Check to see if it is
1100 defined in that block, or whether its live on entry. */
1101 int index = PHI_ARG_INDEX_FROM_USE (use);
1102 edge e = gimple_phi_arg_edge (use_stmt, index);
1103 if (e->src != ENTRY_BLOCK_PTR)
1105 if (e->src != def_bb)
1106 add_block = e->src;
1109 else if (is_gimple_debug (use_stmt))
1110 continue;
1111 else
1113 /* If its not defined in this block, its live on entry. */
1114 basic_block use_bb = gimple_bb (use_stmt);
1115 if (use_bb != def_bb)
1116 add_block = use_bb;
1119 /* If there was a live on entry use, set the bit. */
1120 if (add_block)
1122 global = true;
1123 bitmap_set_bit (&live->livein[add_block->index], p);
1127 /* If SSA_NAME is live on entry to at least one block, fill in all the live
1128 on entry blocks between the def and all the uses. */
1129 if (global)
1130 bitmap_set_bit (live->global, p);
1134 /* Calculate the live on exit vectors based on the entry info in LIVEINFO. */
1136 void
1137 calculate_live_on_exit (tree_live_info_p liveinfo)
1139 basic_block bb;
1140 edge e;
1141 edge_iterator ei;
1143 /* live on entry calculations used liveout vectors for defs, clear them. */
1144 FOR_EACH_BB (bb)
1145 bitmap_clear (&liveinfo->liveout[bb->index]);
1147 /* Set all the live-on-exit bits for uses in PHIs. */
1148 FOR_EACH_BB (bb)
1150 gimple_stmt_iterator gsi;
1151 size_t i;
1153 /* Mark the PHI arguments which are live on exit to the pred block. */
1154 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1156 gimple phi = gsi_stmt (gsi);
1157 for (i = 0; i < gimple_phi_num_args (phi); i++)
1159 tree t = PHI_ARG_DEF (phi, i);
1160 int p;
1162 if (TREE_CODE (t) != SSA_NAME)
1163 continue;
1165 p = var_to_partition (liveinfo->map, t);
1166 if (p == NO_PARTITION)
1167 continue;
1168 e = gimple_phi_arg_edge (phi, i);
1169 if (e->src != ENTRY_BLOCK_PTR)
1170 bitmap_set_bit (&liveinfo->liveout[e->src->index], p);
1174 /* Add each successors live on entry to this bock live on exit. */
1175 FOR_EACH_EDGE (e, ei, bb->succs)
1176 if (e->dest != EXIT_BLOCK_PTR)
1177 bitmap_ior_into (&liveinfo->liveout[bb->index],
1178 live_on_entry (liveinfo, e->dest));
1183 /* Given partition map MAP, calculate all the live on entry bitmaps for
1184 each partition. Return a new live info object. */
1186 tree_live_info_p
1187 calculate_live_ranges (var_map map)
1189 tree var;
1190 unsigned i;
1191 tree_live_info_p live;
1193 bitmap_obstack_initialize (&liveness_bitmap_obstack);
1194 live = new_tree_live_info (map);
1195 for (i = 0; i < num_var_partitions (map); i++)
1197 var = partition_to_var (map, i);
1198 if (var != NULL_TREE)
1199 set_var_live_on_entry (var, live);
1202 live_worklist (live);
1204 #ifdef ENABLE_CHECKING
1205 verify_live_on_entry (live);
1206 #endif
1208 calculate_live_on_exit (live);
1209 return live;
1213 /* Output partition map MAP to file F. */
1215 void
1216 dump_var_map (FILE *f, var_map map)
1218 int t;
1219 unsigned x, y;
1220 int p;
1222 fprintf (f, "\nPartition map \n\n");
1224 for (x = 0; x < map->num_partitions; x++)
1226 if (map->view_to_partition != NULL)
1227 p = map->view_to_partition[x];
1228 else
1229 p = x;
1231 if (ssa_name (p) == NULL_TREE
1232 || virtual_operand_p (ssa_name (p)))
1233 continue;
1235 t = 0;
1236 for (y = 1; y < num_ssa_names; y++)
1238 p = partition_find (map->var_partition, y);
1239 if (map->partition_to_view)
1240 p = map->partition_to_view[p];
1241 if (p == (int)x)
1243 if (t++ == 0)
1245 fprintf (f, "Partition %d (", x);
1246 print_generic_expr (f, partition_to_var (map, p), TDF_SLIM);
1247 fprintf (f, " - ");
1249 fprintf (f, "%d ", y);
1252 if (t != 0)
1253 fprintf (f, ")\n");
1255 fprintf (f, "\n");
1259 /* Generic dump for the above. */
1261 DEBUG_FUNCTION void
1262 debug (_var_map &ref)
1264 dump_var_map (stderr, &ref);
1267 DEBUG_FUNCTION void
1268 debug (_var_map *ptr)
1270 if (ptr)
1271 debug (*ptr);
1272 else
1273 fprintf (stderr, "<nil>\n");
1277 /* Output live range info LIVE to file F, controlled by FLAG. */
1279 void
1280 dump_live_info (FILE *f, tree_live_info_p live, int flag)
1282 basic_block bb;
1283 unsigned i;
1284 var_map map = live->map;
1285 bitmap_iterator bi;
1287 if ((flag & LIVEDUMP_ENTRY) && live->livein)
1289 FOR_EACH_BB (bb)
1291 fprintf (f, "\nLive on entry to BB%d : ", bb->index);
1292 EXECUTE_IF_SET_IN_BITMAP (&live->livein[bb->index], 0, i, bi)
1294 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1295 fprintf (f, " ");
1297 fprintf (f, "\n");
1301 if ((flag & LIVEDUMP_EXIT) && live->liveout)
1303 FOR_EACH_BB (bb)
1305 fprintf (f, "\nLive on exit from BB%d : ", bb->index);
1306 EXECUTE_IF_SET_IN_BITMAP (&live->liveout[bb->index], 0, i, bi)
1308 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1309 fprintf (f, " ");
1311 fprintf (f, "\n");
1317 /* Generic dump for the above. */
1319 DEBUG_FUNCTION void
1320 debug (tree_live_info_d &ref)
1322 dump_live_info (stderr, &ref, 0);
1325 DEBUG_FUNCTION void
1326 debug (tree_live_info_d *ptr)
1328 if (ptr)
1329 debug (*ptr);
1330 else
1331 fprintf (stderr, "<nil>\n");
1335 #ifdef ENABLE_CHECKING
1336 /* Verify that SSA_VAR is a non-virtual SSA_NAME. */
1338 void
1339 register_ssa_partition_check (tree ssa_var)
1341 gcc_assert (TREE_CODE (ssa_var) == SSA_NAME);
1342 if (virtual_operand_p (ssa_var))
1344 fprintf (stderr, "Illegally registering a virtual SSA name :");
1345 print_generic_expr (stderr, ssa_var, TDF_SLIM);
1346 fprintf (stderr, " in the SSA->Normal phase.\n");
1347 internal_error ("SSA corruption");
1352 /* Verify that the info in LIVE matches the current cfg. */
1354 static void
1355 verify_live_on_entry (tree_live_info_p live)
1357 unsigned i;
1358 tree var;
1359 gimple stmt;
1360 basic_block bb;
1361 edge e;
1362 int num;
1363 edge_iterator ei;
1364 var_map map = live->map;
1366 /* Check for live on entry partitions and report those with a DEF in
1367 the program. This will typically mean an optimization has done
1368 something wrong. */
1369 bb = ENTRY_BLOCK_PTR;
1370 num = 0;
1371 FOR_EACH_EDGE (e, ei, bb->succs)
1373 int entry_block = e->dest->index;
1374 if (e->dest == EXIT_BLOCK_PTR)
1375 continue;
1376 for (i = 0; i < (unsigned)num_var_partitions (map); i++)
1378 basic_block tmp;
1379 tree d = NULL_TREE;
1380 bitmap loe;
1381 var = partition_to_var (map, i);
1382 stmt = SSA_NAME_DEF_STMT (var);
1383 tmp = gimple_bb (stmt);
1384 if (SSA_NAME_VAR (var))
1385 d = ssa_default_def (cfun, SSA_NAME_VAR (var));
1387 loe = live_on_entry (live, e->dest);
1388 if (loe && bitmap_bit_p (loe, i))
1390 if (!gimple_nop_p (stmt))
1392 num++;
1393 print_generic_expr (stderr, var, TDF_SLIM);
1394 fprintf (stderr, " is defined ");
1395 if (tmp)
1396 fprintf (stderr, " in BB%d, ", tmp->index);
1397 fprintf (stderr, "by:\n");
1398 print_gimple_stmt (stderr, stmt, 0, TDF_SLIM);
1399 fprintf (stderr, "\nIt is also live-on-entry to entry BB %d",
1400 entry_block);
1401 fprintf (stderr, " So it appears to have multiple defs.\n");
1403 else
1405 if (d != var)
1407 num++;
1408 print_generic_expr (stderr, var, TDF_SLIM);
1409 fprintf (stderr, " is live-on-entry to BB%d ",
1410 entry_block);
1411 if (d)
1413 fprintf (stderr, " but is not the default def of ");
1414 print_generic_expr (stderr, d, TDF_SLIM);
1415 fprintf (stderr, "\n");
1417 else
1418 fprintf (stderr, " and there is no default def.\n");
1422 else
1423 if (d == var)
1425 /* The only way this var shouldn't be marked live on entry is
1426 if it occurs in a PHI argument of the block. */
1427 size_t z;
1428 bool ok = false;
1429 gimple_stmt_iterator gsi;
1430 for (gsi = gsi_start_phis (e->dest);
1431 !gsi_end_p (gsi) && !ok;
1432 gsi_next (&gsi))
1434 gimple phi = gsi_stmt (gsi);
1435 for (z = 0; z < gimple_phi_num_args (phi); z++)
1436 if (var == gimple_phi_arg_def (phi, z))
1438 ok = true;
1439 break;
1442 if (ok)
1443 continue;
1444 num++;
1445 print_generic_expr (stderr, var, TDF_SLIM);
1446 fprintf (stderr, " is not marked live-on-entry to entry BB%d ",
1447 entry_block);
1448 fprintf (stderr, "but it is a default def so it should be.\n");
1452 gcc_assert (num <= 0);
1454 #endif