[obvious] Fix typos above expand_cond_expr_using_cmove
[official-gcc.git] / gcc / tree-ssa-live.c
blob5b00f58c06e604f7af24229bb817a1b8a63f850b
1 /* Liveness for SSA trees.
2 Copyright (C) 2003-2015 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 "tree.h"
26 #include "gimple.h"
27 #include "rtl.h"
28 #include "ssa.h"
29 #include "alias.h"
30 #include "fold-const.h"
31 #include "gimple-pretty-print.h"
32 #include "internal-fn.h"
33 #include "gimple-iterator.h"
34 #include "flags.h"
35 #include "insn-config.h"
36 #include "expmed.h"
37 #include "dojump.h"
38 #include "explow.h"
39 #include "calls.h"
40 #include "emit-rtl.h"
41 #include "varasm.h"
42 #include "stmt.h"
43 #include "expr.h"
44 #include "tree-dfa.h"
45 #include "timevar.h"
46 #include "dumpfile.h"
47 #include "tree-ssa-live.h"
48 #include "diagnostic-core.h"
49 #include "debug.h"
50 #include "tree-ssa.h"
51 #include "cgraph.h"
52 #include "ipa-utils.h"
54 #ifdef ENABLE_CHECKING
55 static void verify_live_on_entry (tree_live_info_p);
56 #endif
59 /* VARMAP maintains a mapping from SSA version number to real variables.
61 All SSA_NAMES are divided into partitions. Initially each ssa_name is the
62 only member of it's own partition. Coalescing will attempt to group any
63 ssa_names which occur in a copy or in a PHI node into the same partition.
65 At the end of out-of-ssa, each partition becomes a "real" variable and is
66 rewritten as a compiler variable.
68 The var_map data structure is used to manage these partitions. It allows
69 partitions to be combined, and determines which partition belongs to what
70 ssa_name or variable, and vice versa. */
73 /* Hashtable helpers. */
75 struct tree_int_map_hasher : nofree_ptr_hash <tree_int_map>
77 static inline hashval_t hash (const tree_int_map *);
78 static inline bool equal (const tree_int_map *, const tree_int_map *);
81 inline hashval_t
82 tree_int_map_hasher::hash (const tree_int_map *v)
84 return tree_map_base_hash (v);
87 inline bool
88 tree_int_map_hasher::equal (const tree_int_map *v, const tree_int_map *c)
90 return tree_int_map_eq (v, c);
94 /* This routine will initialize the basevar fields of MAP. */
96 static void
97 var_map_base_init (var_map map)
99 int x, num_part;
100 tree var;
101 struct tree_int_map *m, *mapstorage;
103 num_part = num_var_partitions (map);
104 hash_table<tree_int_map_hasher> tree_to_index (num_part);
105 /* We can have at most num_part entries in the hash tables, so it's
106 enough to allocate so many map elements once, saving some malloc
107 calls. */
108 mapstorage = m = XNEWVEC (struct tree_int_map, num_part);
110 /* If a base table already exists, clear it, otherwise create it. */
111 free (map->partition_to_base_index);
112 map->partition_to_base_index = (int *) xmalloc (sizeof (int) * num_part);
114 /* Build the base variable list, and point partitions at their bases. */
115 for (x = 0; x < num_part; x++)
117 struct tree_int_map **slot;
118 unsigned baseindex;
119 var = partition_to_var (map, x);
120 if (SSA_NAME_VAR (var)
121 && (!VAR_P (SSA_NAME_VAR (var))
122 || !DECL_IGNORED_P (SSA_NAME_VAR (var))))
123 m->base.from = SSA_NAME_VAR (var);
124 else
125 /* This restricts what anonymous SSA names we can coalesce
126 as it restricts the sets we compute conflicts for.
127 Using TREE_TYPE to generate sets is the easies as
128 type equivalency also holds for SSA names with the same
129 underlying decl.
131 Check gimple_can_coalesce_p when changing this code. */
132 m->base.from = (TYPE_CANONICAL (TREE_TYPE (var))
133 ? TYPE_CANONICAL (TREE_TYPE (var))
134 : TREE_TYPE (var));
135 /* If base variable hasn't been seen, set it up. */
136 slot = tree_to_index.find_slot (m, INSERT);
137 if (!*slot)
139 baseindex = m - mapstorage;
140 m->to = baseindex;
141 *slot = m;
142 m++;
144 else
145 baseindex = (*slot)->to;
146 map->partition_to_base_index[x] = baseindex;
149 map->num_basevars = m - mapstorage;
151 free (mapstorage);
155 /* Remove the base table in MAP. */
157 static void
158 var_map_base_fini (var_map map)
160 /* Free the basevar info if it is present. */
161 if (map->partition_to_base_index != NULL)
163 free (map->partition_to_base_index);
164 map->partition_to_base_index = NULL;
165 map->num_basevars = 0;
168 /* Create a variable partition map of SIZE, initialize and return it. */
170 var_map
171 init_var_map (int size)
173 var_map map;
175 map = (var_map) xmalloc (sizeof (struct _var_map));
176 map->var_partition = partition_new (size);
178 map->partition_to_view = NULL;
179 map->view_to_partition = NULL;
180 map->num_partitions = size;
181 map->partition_size = size;
182 map->num_basevars = 0;
183 map->partition_to_base_index = NULL;
184 return map;
188 /* Free memory associated with MAP. */
190 void
191 delete_var_map (var_map map)
193 var_map_base_fini (map);
194 partition_delete (map->var_partition);
195 free (map->partition_to_view);
196 free (map->view_to_partition);
197 free (map);
201 /* This function will combine the partitions in MAP for VAR1 and VAR2. It
202 Returns the partition which represents the new partition. If the two
203 partitions cannot be combined, NO_PARTITION is returned. */
206 var_union (var_map map, tree var1, tree var2)
208 int p1, p2, p3;
210 gcc_assert (TREE_CODE (var1) == SSA_NAME);
211 gcc_assert (TREE_CODE (var2) == SSA_NAME);
213 /* This is independent of partition_to_view. If partition_to_view is
214 on, then whichever one of these partitions is absorbed will never have a
215 dereference into the partition_to_view array any more. */
217 p1 = partition_find (map->var_partition, SSA_NAME_VERSION (var1));
218 p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2));
220 gcc_assert (p1 != NO_PARTITION);
221 gcc_assert (p2 != NO_PARTITION);
223 if (p1 == p2)
224 p3 = p1;
225 else
226 p3 = partition_union (map->var_partition, p1, p2);
228 if (map->partition_to_view)
229 p3 = map->partition_to_view[p3];
231 return p3;
235 /* Compress the partition numbers in MAP such that they fall in the range
236 0..(num_partitions-1) instead of wherever they turned out during
237 the partitioning exercise. This removes any references to unused
238 partitions, thereby allowing bitmaps and other vectors to be much
239 denser.
241 This is implemented such that compaction doesn't affect partitioning.
242 Ie., once partitions are created and possibly merged, running one
243 or more different kind of compaction will not affect the partitions
244 themselves. Their index might change, but all the same variables will
245 still be members of the same partition group. This allows work on reduced
246 sets, and no loss of information when a larger set is later desired.
248 In particular, coalescing can work on partitions which have 2 or more
249 definitions, and then 'recompact' later to include all the single
250 definitions for assignment to program variables. */
253 /* Set MAP back to the initial state of having no partition view. Return a
254 bitmap which has a bit set for each partition number which is in use in the
255 varmap. */
257 static bitmap
258 partition_view_init (var_map map)
260 bitmap used;
261 int tmp;
262 unsigned int x;
264 used = BITMAP_ALLOC (NULL);
266 /* Already in a view? Abandon the old one. */
267 if (map->partition_to_view)
269 free (map->partition_to_view);
270 map->partition_to_view = NULL;
272 if (map->view_to_partition)
274 free (map->view_to_partition);
275 map->view_to_partition = NULL;
278 /* Find out which partitions are actually referenced. */
279 for (x = 0; x < map->partition_size; x++)
281 tmp = partition_find (map->var_partition, x);
282 if (ssa_name (tmp) != NULL_TREE && !virtual_operand_p (ssa_name (tmp))
283 && (!has_zero_uses (ssa_name (tmp))
284 || !SSA_NAME_IS_DEFAULT_DEF (ssa_name (tmp))))
285 bitmap_set_bit (used, tmp);
288 map->num_partitions = map->partition_size;
289 return used;
293 /* This routine will finalize the view data for MAP based on the partitions
294 set in SELECTED. This is either the same bitmap returned from
295 partition_view_init, or a trimmed down version if some of those partitions
296 were not desired in this view. SELECTED is freed before returning. */
298 static void
299 partition_view_fini (var_map map, bitmap selected)
301 bitmap_iterator bi;
302 unsigned count, i, x, limit;
304 gcc_assert (selected);
306 count = bitmap_count_bits (selected);
307 limit = map->partition_size;
309 /* If its a one-to-one ratio, we don't need any view compaction. */
310 if (count < limit)
312 map->partition_to_view = (int *)xmalloc (limit * sizeof (int));
313 memset (map->partition_to_view, 0xff, (limit * sizeof (int)));
314 map->view_to_partition = (int *)xmalloc (count * sizeof (int));
316 i = 0;
317 /* Give each selected partition an index. */
318 EXECUTE_IF_SET_IN_BITMAP (selected, 0, x, bi)
320 map->partition_to_view[x] = i;
321 map->view_to_partition[i] = x;
322 i++;
324 gcc_assert (i == count);
325 map->num_partitions = i;
328 BITMAP_FREE (selected);
332 /* Create a partition view which includes all the used partitions in MAP. If
333 WANT_BASES is true, create the base variable map as well. */
335 void
336 partition_view_normal (var_map map, bool want_bases)
338 bitmap used;
340 used = partition_view_init (map);
341 partition_view_fini (map, used);
343 if (want_bases)
344 var_map_base_init (map);
345 else
346 var_map_base_fini (map);
350 /* Create a partition view in MAP which includes just partitions which occur in
351 the bitmap ONLY. If WANT_BASES is true, create the base variable map
352 as well. */
354 void
355 partition_view_bitmap (var_map map, bitmap only, bool want_bases)
357 bitmap used;
358 bitmap new_partitions = BITMAP_ALLOC (NULL);
359 unsigned x, p;
360 bitmap_iterator bi;
362 used = partition_view_init (map);
363 EXECUTE_IF_SET_IN_BITMAP (only, 0, x, bi)
365 p = partition_find (map->var_partition, x);
366 gcc_assert (bitmap_bit_p (used, p));
367 bitmap_set_bit (new_partitions, p);
369 partition_view_fini (map, new_partitions);
371 if (want_bases)
372 var_map_base_init (map);
373 else
374 var_map_base_fini (map);
378 static bitmap usedvars;
380 /* Mark VAR as used, so that it'll be preserved during rtl expansion.
381 Returns true if VAR wasn't marked before. */
383 static inline bool
384 set_is_used (tree var)
386 return bitmap_set_bit (usedvars, DECL_UID (var));
389 /* Return true if VAR is marked as used. */
391 static inline bool
392 is_used_p (tree var)
394 return bitmap_bit_p (usedvars, DECL_UID (var));
397 static inline void mark_all_vars_used (tree *);
399 /* Helper function for mark_all_vars_used, called via walk_tree. */
401 static tree
402 mark_all_vars_used_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
404 tree t = *tp;
405 enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
406 tree b;
408 if (TREE_CODE (t) == SSA_NAME)
410 *walk_subtrees = 0;
411 t = SSA_NAME_VAR (t);
412 if (!t)
413 return NULL;
416 if (IS_EXPR_CODE_CLASS (c)
417 && (b = TREE_BLOCK (t)) != NULL)
418 TREE_USED (b) = true;
420 /* Ignore TMR_OFFSET and TMR_STEP for TARGET_MEM_REFS, as those
421 fields do not contain vars. */
422 if (TREE_CODE (t) == TARGET_MEM_REF)
424 mark_all_vars_used (&TMR_BASE (t));
425 mark_all_vars_used (&TMR_INDEX (t));
426 mark_all_vars_used (&TMR_INDEX2 (t));
427 *walk_subtrees = 0;
428 return NULL;
431 /* Only need to mark VAR_DECLS; parameters and return results are not
432 eliminated as unused. */
433 if (TREE_CODE (t) == VAR_DECL)
435 /* When a global var becomes used for the first time also walk its
436 initializer (non global ones don't have any). */
437 if (set_is_used (t) && is_global_var (t)
438 && DECL_CONTEXT (t) == current_function_decl)
439 mark_all_vars_used (&DECL_INITIAL (t));
441 /* remove_unused_scope_block_p requires information about labels
442 which are not DECL_IGNORED_P to tell if they might be used in the IL. */
443 else if (TREE_CODE (t) == LABEL_DECL)
444 /* Although the TREE_USED values that the frontend uses would be
445 acceptable (albeit slightly over-conservative) for our purposes,
446 init_vars_expansion clears TREE_USED for LABEL_DECLs too, so we
447 must re-compute it here. */
448 TREE_USED (t) = 1;
450 if (IS_TYPE_OR_DECL_P (t))
451 *walk_subtrees = 0;
453 return NULL;
456 /* Mark the scope block SCOPE and its subblocks unused when they can be
457 possibly eliminated if dead. */
459 static void
460 mark_scope_block_unused (tree scope)
462 tree t;
463 TREE_USED (scope) = false;
464 if (!(*debug_hooks->ignore_block) (scope))
465 TREE_USED (scope) = true;
466 for (t = BLOCK_SUBBLOCKS (scope); t ; t = BLOCK_CHAIN (t))
467 mark_scope_block_unused (t);
470 /* Look if the block is dead (by possibly eliminating its dead subblocks)
471 and return true if so.
472 Block is declared dead if:
473 1) No statements are associated with it.
474 2) Declares no live variables
475 3) All subblocks are dead
476 or there is precisely one subblocks and the block
477 has same abstract origin as outer block and declares
478 no variables, so it is pure wrapper.
479 When we are not outputting full debug info, we also eliminate dead variables
480 out of scope blocks to let them to be recycled by GGC and to save copying work
481 done by the inliner. */
483 static bool
484 remove_unused_scope_block_p (tree scope, bool in_ctor_dtor_block)
486 tree *t, *next;
487 bool unused = !TREE_USED (scope);
488 int nsubblocks = 0;
490 /* For ipa-polymorphic-call.c purposes, preserve blocks:
491 1) with BLOCK_ABSTRACT_ORIGIN of a ctor/dtor or their clones */
492 if (inlined_polymorphic_ctor_dtor_block_p (scope, true))
494 in_ctor_dtor_block = true;
495 unused = false;
497 /* 2) inside such blocks, the outermost block with BLOCK_ABSTRACT_ORIGIN
498 being a FUNCTION_DECL. */
499 else if (in_ctor_dtor_block
500 && BLOCK_ABSTRACT_ORIGIN (scope)
501 && TREE_CODE (BLOCK_ABSTRACT_ORIGIN (scope)) == FUNCTION_DECL)
503 in_ctor_dtor_block = false;
504 unused = false;
507 for (t = &BLOCK_VARS (scope); *t; t = next)
509 next = &DECL_CHAIN (*t);
511 /* Debug info of nested function refers to the block of the
512 function. We might stil call it even if all statements
513 of function it was nested into was elliminated.
515 TODO: We can actually look into cgraph to see if function
516 will be output to file. */
517 if (TREE_CODE (*t) == FUNCTION_DECL)
518 unused = false;
520 /* If a decl has a value expr, we need to instantiate it
521 regardless of debug info generation, to avoid codegen
522 differences in memory overlap tests. update_equiv_regs() may
523 indirectly call validate_equiv_mem() to test whether a
524 SET_DEST overlaps with others, and if the value expr changes
525 by virtual register instantiation, we may get end up with
526 different results. */
527 else if (TREE_CODE (*t) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (*t))
528 unused = false;
530 /* Remove everything we don't generate debug info for. */
531 else if (DECL_IGNORED_P (*t))
533 *t = DECL_CHAIN (*t);
534 next = t;
537 /* When we are outputting debug info, we usually want to output
538 info about optimized-out variables in the scope blocks.
539 Exception are the scope blocks not containing any instructions
540 at all so user can't get into the scopes at first place. */
541 else if (is_used_p (*t))
542 unused = false;
543 else if (TREE_CODE (*t) == LABEL_DECL && TREE_USED (*t))
544 /* For labels that are still used in the IL, the decision to
545 preserve them must not depend DEBUG_INFO_LEVEL, otherwise we
546 risk having different ordering in debug vs. non-debug builds
547 during inlining or versioning.
548 A label appearing here (we have already checked DECL_IGNORED_P)
549 should not be used in the IL unless it has been explicitly used
550 before, so we use TREE_USED as an approximation. */
551 /* In principle, we should do the same here as for the debug case
552 below, however, when debugging, there might be additional nested
553 levels that keep an upper level with a label live, so we have to
554 force this block to be considered used, too. */
555 unused = false;
557 /* When we are not doing full debug info, we however can keep around
558 only the used variables for cfgexpand's memory packing saving quite
559 a lot of memory.
561 For sake of -g3, we keep around those vars but we don't count this as
562 use of block, so innermost block with no used vars and no instructions
563 can be considered dead. We only want to keep around blocks user can
564 breakpoint into and ask about value of optimized out variables.
566 Similarly we need to keep around types at least until all
567 variables of all nested blocks are gone. We track no
568 information on whether given type is used or not, so we have
569 to keep them even when not emitting debug information,
570 otherwise we may end up remapping variables and their (local)
571 types in different orders depending on whether debug
572 information is being generated. */
574 else if (TREE_CODE (*t) == TYPE_DECL
575 || debug_info_level == DINFO_LEVEL_NORMAL
576 || debug_info_level == DINFO_LEVEL_VERBOSE)
578 else
580 *t = DECL_CHAIN (*t);
581 next = t;
585 for (t = &BLOCK_SUBBLOCKS (scope); *t ;)
586 if (remove_unused_scope_block_p (*t, in_ctor_dtor_block))
588 if (BLOCK_SUBBLOCKS (*t))
590 tree next = BLOCK_CHAIN (*t);
591 tree supercontext = BLOCK_SUPERCONTEXT (*t);
593 *t = BLOCK_SUBBLOCKS (*t);
594 while (BLOCK_CHAIN (*t))
596 BLOCK_SUPERCONTEXT (*t) = supercontext;
597 t = &BLOCK_CHAIN (*t);
599 BLOCK_CHAIN (*t) = next;
600 BLOCK_SUPERCONTEXT (*t) = supercontext;
601 t = &BLOCK_CHAIN (*t);
602 nsubblocks ++;
604 else
605 *t = BLOCK_CHAIN (*t);
607 else
609 t = &BLOCK_CHAIN (*t);
610 nsubblocks ++;
614 if (!unused)
616 /* Outer scope is always used. */
617 else if (!BLOCK_SUPERCONTEXT (scope)
618 || TREE_CODE (BLOCK_SUPERCONTEXT (scope)) == FUNCTION_DECL)
619 unused = false;
620 /* Innermost blocks with no live variables nor statements can be always
621 eliminated. */
622 else if (!nsubblocks)
624 /* When not generating debug info we can eliminate info on unused
625 variables. */
626 else if (!flag_auto_profile && debug_info_level == DINFO_LEVEL_NONE)
628 /* Even for -g0 don't prune outer scopes from artificial
629 functions, otherwise diagnostics using tree_nonartificial_location
630 will not be emitted properly. */
631 if (inlined_function_outer_scope_p (scope))
633 tree ao = scope;
635 while (ao
636 && TREE_CODE (ao) == BLOCK
637 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
638 ao = BLOCK_ABSTRACT_ORIGIN (ao);
639 if (ao
640 && TREE_CODE (ao) == FUNCTION_DECL
641 && DECL_DECLARED_INLINE_P (ao)
642 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
643 unused = false;
646 else if (BLOCK_VARS (scope) || BLOCK_NUM_NONLOCALIZED_VARS (scope))
647 unused = false;
648 /* See if this block is important for representation of inlined function.
649 Inlined functions are always represented by block with
650 block_ultimate_origin being set to FUNCTION_DECL and DECL_SOURCE_LOCATION
651 set... */
652 else if (inlined_function_outer_scope_p (scope))
653 unused = false;
654 else
655 /* Verfify that only blocks with source location set
656 are entry points to the inlined functions. */
657 gcc_assert (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (scope))
658 == UNKNOWN_LOCATION);
660 TREE_USED (scope) = !unused;
661 return unused;
664 /* Mark all VAR_DECLS under *EXPR_P as used, so that they won't be
665 eliminated during the tree->rtl conversion process. */
667 static inline void
668 mark_all_vars_used (tree *expr_p)
670 walk_tree (expr_p, mark_all_vars_used_1, NULL, NULL);
673 /* Helper function for clear_unused_block_pointer, called via walk_tree. */
675 static tree
676 clear_unused_block_pointer_1 (tree *tp, int *, void *)
678 if (EXPR_P (*tp) && TREE_BLOCK (*tp)
679 && !TREE_USED (TREE_BLOCK (*tp)))
680 TREE_SET_BLOCK (*tp, NULL);
681 return NULL_TREE;
684 /* Set all block pointer in debug or clobber stmt to NULL if the block
685 is unused, so that they will not be streamed out. */
687 static void
688 clear_unused_block_pointer (void)
690 basic_block bb;
691 gimple_stmt_iterator gsi;
693 FOR_EACH_BB_FN (bb, cfun)
694 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
696 unsigned i;
697 tree b;
698 gimple stmt = gsi_stmt (gsi);
700 if (!is_gimple_debug (stmt) && !gimple_clobber_p (stmt))
701 continue;
702 b = gimple_block (stmt);
703 if (b && !TREE_USED (b))
704 gimple_set_block (stmt, NULL);
705 for (i = 0; i < gimple_num_ops (stmt); i++)
706 walk_tree (gimple_op_ptr (stmt, i), clear_unused_block_pointer_1,
707 NULL, NULL);
711 /* Dump scope blocks starting at SCOPE to FILE. INDENT is the
712 indentation level and FLAGS is as in print_generic_expr. */
714 static void
715 dump_scope_block (FILE *file, int indent, tree scope, int flags)
717 tree var, t;
718 unsigned int i;
720 fprintf (file, "\n%*s{ Scope block #%i%s%s",indent, "" , BLOCK_NUMBER (scope),
721 TREE_USED (scope) ? "" : " (unused)",
722 BLOCK_ABSTRACT (scope) ? " (abstract)": "");
723 if (LOCATION_LOCUS (BLOCK_SOURCE_LOCATION (scope)) != UNKNOWN_LOCATION)
725 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (scope));
726 fprintf (file, " %s:%i", s.file, s.line);
728 if (BLOCK_ABSTRACT_ORIGIN (scope))
730 tree origin = block_ultimate_origin (scope);
731 if (origin)
733 fprintf (file, " Originating from :");
734 if (DECL_P (origin))
735 print_generic_decl (file, origin, flags);
736 else
737 fprintf (file, "#%i", BLOCK_NUMBER (origin));
740 fprintf (file, " \n");
741 for (var = BLOCK_VARS (scope); var; var = DECL_CHAIN (var))
743 fprintf (file, "%*s", indent, "");
744 print_generic_decl (file, var, flags);
745 fprintf (file, "\n");
747 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (scope); i++)
749 fprintf (file, "%*s",indent, "");
750 print_generic_decl (file, BLOCK_NONLOCALIZED_VAR (scope, i),
751 flags);
752 fprintf (file, " (nonlocalized)\n");
754 for (t = BLOCK_SUBBLOCKS (scope); t ; t = BLOCK_CHAIN (t))
755 dump_scope_block (file, indent + 2, t, flags);
756 fprintf (file, "\n%*s}\n",indent, "");
759 /* Dump the tree of lexical scopes starting at SCOPE to stderr. FLAGS
760 is as in print_generic_expr. */
762 DEBUG_FUNCTION void
763 debug_scope_block (tree scope, int flags)
765 dump_scope_block (stderr, 0, scope, flags);
769 /* Dump the tree of lexical scopes of current_function_decl to FILE.
770 FLAGS is as in print_generic_expr. */
772 void
773 dump_scope_blocks (FILE *file, int flags)
775 dump_scope_block (file, 0, DECL_INITIAL (current_function_decl), flags);
779 /* Dump the tree of lexical scopes of current_function_decl to stderr.
780 FLAGS is as in print_generic_expr. */
782 DEBUG_FUNCTION void
783 debug_scope_blocks (int flags)
785 dump_scope_blocks (stderr, flags);
788 /* Remove local variables that are not referenced in the IL. */
790 void
791 remove_unused_locals (void)
793 basic_block bb;
794 tree var;
795 unsigned srcidx, dstidx, num;
796 bool have_local_clobbers = false;
798 /* Removing declarations from lexical blocks when not optimizing is
799 not only a waste of time, it actually causes differences in stack
800 layout. */
801 if (!optimize)
802 return;
804 timevar_push (TV_REMOVE_UNUSED);
806 mark_scope_block_unused (DECL_INITIAL (current_function_decl));
808 usedvars = BITMAP_ALLOC (NULL);
810 /* Walk the CFG marking all referenced symbols. */
811 FOR_EACH_BB_FN (bb, cfun)
813 gimple_stmt_iterator gsi;
814 size_t i;
815 edge_iterator ei;
816 edge e;
818 /* Walk the statements. */
819 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
821 gimple stmt = gsi_stmt (gsi);
822 tree b = gimple_block (stmt);
824 if (is_gimple_debug (stmt))
825 continue;
827 if (gimple_clobber_p (stmt))
829 have_local_clobbers = true;
830 continue;
833 if (b)
834 TREE_USED (b) = true;
836 for (i = 0; i < gimple_num_ops (stmt); i++)
837 mark_all_vars_used (gimple_op_ptr (gsi_stmt (gsi), i));
840 for (gphi_iterator gpi = gsi_start_phis (bb);
841 !gsi_end_p (gpi);
842 gsi_next (&gpi))
844 use_operand_p arg_p;
845 ssa_op_iter i;
846 tree def;
847 gphi *phi = gpi.phi ();
849 if (virtual_operand_p (gimple_phi_result (phi)))
850 continue;
852 def = gimple_phi_result (phi);
853 mark_all_vars_used (&def);
855 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_ALL_USES)
857 tree arg = USE_FROM_PTR (arg_p);
858 int index = PHI_ARG_INDEX_FROM_USE (arg_p);
859 tree block =
860 LOCATION_BLOCK (gimple_phi_arg_location (phi, index));
861 if (block != NULL)
862 TREE_USED (block) = true;
863 mark_all_vars_used (&arg);
867 FOR_EACH_EDGE (e, ei, bb->succs)
868 if (LOCATION_BLOCK (e->goto_locus) != NULL)
869 TREE_USED (LOCATION_BLOCK (e->goto_locus)) = true;
872 /* We do a two-pass approach about the out-of-scope clobbers. We want
873 to remove them if they are the only references to a local variable,
874 but we want to retain them when there's any other. So the first pass
875 ignores them, and the second pass (if there were any) tries to remove
876 them. */
877 if (have_local_clobbers)
878 FOR_EACH_BB_FN (bb, cfun)
880 gimple_stmt_iterator gsi;
882 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
884 gimple stmt = gsi_stmt (gsi);
885 tree b = gimple_block (stmt);
887 if (gimple_clobber_p (stmt))
889 tree lhs = gimple_assign_lhs (stmt);
890 tree base = get_base_address (lhs);
891 /* Remove clobbers referencing unused vars, or clobbers
892 with MEM_REF lhs referencing uninitialized pointers. */
893 if ((TREE_CODE (base) == VAR_DECL && !is_used_p (base))
894 || (TREE_CODE (lhs) == MEM_REF
895 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME
896 && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (lhs, 0))
897 && (TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (lhs, 0)))
898 != PARM_DECL)))
900 unlink_stmt_vdef (stmt);
901 gsi_remove (&gsi, true);
902 release_defs (stmt);
903 continue;
905 if (b)
906 TREE_USED (b) = true;
908 gsi_next (&gsi);
912 cfun->has_local_explicit_reg_vars = false;
914 /* Remove unmarked local and global vars from local_decls. */
915 num = vec_safe_length (cfun->local_decls);
916 for (srcidx = 0, dstidx = 0; srcidx < num; srcidx++)
918 var = (*cfun->local_decls)[srcidx];
919 if (TREE_CODE (var) == VAR_DECL)
921 if (!is_used_p (var))
923 tree def;
924 if (cfun->nonlocal_goto_save_area
925 && TREE_OPERAND (cfun->nonlocal_goto_save_area, 0) == var)
926 cfun->nonlocal_goto_save_area = NULL;
927 /* Release any default def associated with var. */
928 if ((def = ssa_default_def (cfun, var)) != NULL_TREE)
930 set_ssa_default_def (cfun, var, NULL_TREE);
931 release_ssa_name (def);
933 continue;
936 if (TREE_CODE (var) == VAR_DECL
937 && DECL_HARD_REGISTER (var)
938 && !is_global_var (var))
939 cfun->has_local_explicit_reg_vars = true;
941 if (srcidx != dstidx)
942 (*cfun->local_decls)[dstidx] = var;
943 dstidx++;
945 if (dstidx != num)
947 statistics_counter_event (cfun, "unused VAR_DECLs removed", num - dstidx);
948 cfun->local_decls->truncate (dstidx);
951 remove_unused_scope_block_p (DECL_INITIAL (current_function_decl), false);
952 clear_unused_block_pointer ();
954 BITMAP_FREE (usedvars);
956 if (dump_file && (dump_flags & TDF_DETAILS))
958 fprintf (dump_file, "Scope blocks after cleanups:\n");
959 dump_scope_blocks (dump_file, dump_flags);
962 timevar_pop (TV_REMOVE_UNUSED);
965 /* Allocate and return a new live range information object base on MAP. */
967 static tree_live_info_p
968 new_tree_live_info (var_map map)
970 tree_live_info_p live;
971 basic_block bb;
973 live = XNEW (struct tree_live_info_d);
974 live->map = map;
975 live->num_blocks = last_basic_block_for_fn (cfun);
977 bitmap_obstack_initialize (&live->livein_obstack);
978 bitmap_obstack_initialize (&live->liveout_obstack);
979 live->livein = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
980 FOR_EACH_BB_FN (bb, cfun)
981 bitmap_initialize (&live->livein[bb->index], &live->livein_obstack);
983 live->liveout = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
984 FOR_EACH_BB_FN (bb, cfun)
985 bitmap_initialize (&live->liveout[bb->index], &live->liveout_obstack);
987 live->work_stack = XNEWVEC (int, last_basic_block_for_fn (cfun));
988 live->stack_top = live->work_stack;
990 live->global = BITMAP_ALLOC (NULL);
991 return live;
995 /* Free storage for live range info object LIVE. */
997 void
998 delete_tree_live_info (tree_live_info_p live)
1000 if (live->livein)
1002 bitmap_obstack_release (&live->livein_obstack);
1003 free (live->livein);
1005 if (live->liveout)
1007 bitmap_obstack_release (&live->liveout_obstack);
1008 free (live->liveout);
1010 BITMAP_FREE (live->global);
1011 free (live->work_stack);
1012 free (live);
1016 /* Visit basic block BB and propagate any required live on entry bits from
1017 LIVE into the predecessors. VISITED is the bitmap of visited blocks.
1018 TMP is a temporary work bitmap which is passed in to avoid reallocating
1019 it each time. */
1021 static void
1022 loe_visit_block (tree_live_info_p live, basic_block bb, sbitmap visited)
1024 edge e;
1025 bool change;
1026 edge_iterator ei;
1027 basic_block pred_bb;
1028 bitmap loe;
1030 gcc_checking_assert (!bitmap_bit_p (visited, bb->index));
1031 bitmap_set_bit (visited, bb->index);
1033 loe = live_on_entry (live, bb);
1035 FOR_EACH_EDGE (e, ei, bb->preds)
1037 pred_bb = e->src;
1038 if (pred_bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1039 continue;
1040 /* Variables live-on-entry from BB that aren't defined in the
1041 predecessor block. This should be the live on entry vars to pred.
1042 Note that liveout is the DEFs in a block while live on entry is
1043 being calculated.
1044 Add these bits to live-on-entry for the pred. if there are any
1045 changes, and pred_bb has been visited already, add it to the
1046 revisit stack. */
1047 change = bitmap_ior_and_compl_into (live_on_entry (live, pred_bb),
1048 loe, &live->liveout[pred_bb->index]);
1049 if (change
1050 && bitmap_bit_p (visited, pred_bb->index))
1052 bitmap_clear_bit (visited, pred_bb->index);
1053 *(live->stack_top)++ = pred_bb->index;
1059 /* Using LIVE, fill in all the live-on-entry blocks between the defs and uses
1060 of all the variables. */
1062 static void
1063 live_worklist (tree_live_info_p live)
1065 unsigned b;
1066 basic_block bb;
1067 sbitmap visited = sbitmap_alloc (last_basic_block_for_fn (cfun) + 1);
1069 bitmap_clear (visited);
1071 /* Visit all the blocks in reverse order and propagate live on entry values
1072 into the predecessors blocks. */
1073 FOR_EACH_BB_REVERSE_FN (bb, cfun)
1074 loe_visit_block (live, bb, visited);
1076 /* Process any blocks which require further iteration. */
1077 while (live->stack_top != live->work_stack)
1079 b = *--(live->stack_top);
1080 loe_visit_block (live, BASIC_BLOCK_FOR_FN (cfun, b), visited);
1083 sbitmap_free (visited);
1087 /* Calculate the initial live on entry vector for SSA_NAME using immediate_use
1088 links. Set the live on entry fields in LIVE. Def's are marked temporarily
1089 in the liveout vector. */
1091 static void
1092 set_var_live_on_entry (tree ssa_name, tree_live_info_p live)
1094 int p;
1095 gimple stmt;
1096 use_operand_p use;
1097 basic_block def_bb = NULL;
1098 imm_use_iterator imm_iter;
1099 bool global = false;
1101 p = var_to_partition (live->map, ssa_name);
1102 if (p == NO_PARTITION)
1103 return;
1105 stmt = SSA_NAME_DEF_STMT (ssa_name);
1106 if (stmt)
1108 def_bb = gimple_bb (stmt);
1109 /* Mark defs in liveout bitmap temporarily. */
1110 if (def_bb)
1111 bitmap_set_bit (&live->liveout[def_bb->index], p);
1113 else
1114 def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1116 /* An undefined local variable does not need to be very alive. */
1117 if (ssa_undefined_value_p (ssa_name, false))
1118 return;
1120 /* Visit each use of SSA_NAME and if it isn't in the same block as the def,
1121 add it to the list of live on entry blocks. */
1122 FOR_EACH_IMM_USE_FAST (use, imm_iter, ssa_name)
1124 gimple use_stmt = USE_STMT (use);
1125 basic_block add_block = NULL;
1127 if (gimple_code (use_stmt) == GIMPLE_PHI)
1129 /* Uses in PHI's are considered to be live at exit of the SRC block
1130 as this is where a copy would be inserted. Check to see if it is
1131 defined in that block, or whether its live on entry. */
1132 int index = PHI_ARG_INDEX_FROM_USE (use);
1133 edge e = gimple_phi_arg_edge (as_a <gphi *> (use_stmt), index);
1134 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1136 if (e->src != def_bb)
1137 add_block = e->src;
1140 else if (is_gimple_debug (use_stmt))
1141 continue;
1142 else
1144 /* If its not defined in this block, its live on entry. */
1145 basic_block use_bb = gimple_bb (use_stmt);
1146 if (use_bb != def_bb)
1147 add_block = use_bb;
1150 /* If there was a live on entry use, set the bit. */
1151 if (add_block)
1153 global = true;
1154 bitmap_set_bit (&live->livein[add_block->index], p);
1158 /* If SSA_NAME is live on entry to at least one block, fill in all the live
1159 on entry blocks between the def and all the uses. */
1160 if (global)
1161 bitmap_set_bit (live->global, p);
1165 /* Calculate the live on exit vectors based on the entry info in LIVEINFO. */
1167 static void
1168 calculate_live_on_exit (tree_live_info_p liveinfo)
1170 basic_block bb;
1171 edge e;
1172 edge_iterator ei;
1174 /* live on entry calculations used liveout vectors for defs, clear them. */
1175 FOR_EACH_BB_FN (bb, cfun)
1176 bitmap_clear (&liveinfo->liveout[bb->index]);
1178 /* Set all the live-on-exit bits for uses in PHIs. */
1179 FOR_EACH_BB_FN (bb, cfun)
1181 gphi_iterator gsi;
1182 size_t i;
1184 /* Mark the PHI arguments which are live on exit to the pred block. */
1185 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1187 gphi *phi = gsi.phi ();
1188 for (i = 0; i < gimple_phi_num_args (phi); i++)
1190 tree t = PHI_ARG_DEF (phi, i);
1191 int p;
1193 if (TREE_CODE (t) != SSA_NAME)
1194 continue;
1196 p = var_to_partition (liveinfo->map, t);
1197 if (p == NO_PARTITION)
1198 continue;
1199 e = gimple_phi_arg_edge (phi, i);
1200 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1201 bitmap_set_bit (&liveinfo->liveout[e->src->index], p);
1205 /* Add each successors live on entry to this bock live on exit. */
1206 FOR_EACH_EDGE (e, ei, bb->succs)
1207 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1208 bitmap_ior_into (&liveinfo->liveout[bb->index],
1209 live_on_entry (liveinfo, e->dest));
1214 /* Given partition map MAP, calculate all the live on entry bitmaps for
1215 each partition. Return a new live info object. */
1217 tree_live_info_p
1218 calculate_live_ranges (var_map map, bool want_livein)
1220 tree var;
1221 unsigned i;
1222 tree_live_info_p live;
1224 live = new_tree_live_info (map);
1225 for (i = 0; i < num_var_partitions (map); i++)
1227 var = partition_to_var (map, i);
1228 if (var != NULL_TREE)
1229 set_var_live_on_entry (var, live);
1232 live_worklist (live);
1234 #ifdef ENABLE_CHECKING
1235 verify_live_on_entry (live);
1236 #endif
1238 calculate_live_on_exit (live);
1240 if (!want_livein)
1242 bitmap_obstack_release (&live->livein_obstack);
1243 free (live->livein);
1244 live->livein = NULL;
1247 return live;
1251 /* Output partition map MAP to file F. */
1253 void
1254 dump_var_map (FILE *f, var_map map)
1256 int t;
1257 unsigned x, y;
1258 int p;
1260 fprintf (f, "\nPartition map \n\n");
1262 for (x = 0; x < map->num_partitions; x++)
1264 if (map->view_to_partition != NULL)
1265 p = map->view_to_partition[x];
1266 else
1267 p = x;
1269 if (ssa_name (p) == NULL_TREE
1270 || virtual_operand_p (ssa_name (p)))
1271 continue;
1273 t = 0;
1274 for (y = 1; y < num_ssa_names; y++)
1276 p = partition_find (map->var_partition, y);
1277 if (map->partition_to_view)
1278 p = map->partition_to_view[p];
1279 if (p == (int)x)
1281 if (t++ == 0)
1283 fprintf (f, "Partition %d (", x);
1284 print_generic_expr (f, partition_to_var (map, p), TDF_SLIM);
1285 fprintf (f, " - ");
1287 fprintf (f, "%d ", y);
1290 if (t != 0)
1291 fprintf (f, ")\n");
1293 fprintf (f, "\n");
1297 /* Generic dump for the above. */
1299 DEBUG_FUNCTION void
1300 debug (_var_map &ref)
1302 dump_var_map (stderr, &ref);
1305 DEBUG_FUNCTION void
1306 debug (_var_map *ptr)
1308 if (ptr)
1309 debug (*ptr);
1310 else
1311 fprintf (stderr, "<nil>\n");
1315 /* Output live range info LIVE to file F, controlled by FLAG. */
1317 void
1318 dump_live_info (FILE *f, tree_live_info_p live, int flag)
1320 basic_block bb;
1321 unsigned i;
1322 var_map map = live->map;
1323 bitmap_iterator bi;
1325 if ((flag & LIVEDUMP_ENTRY) && live->livein)
1327 FOR_EACH_BB_FN (bb, cfun)
1329 fprintf (f, "\nLive on entry to BB%d : ", bb->index);
1330 EXECUTE_IF_SET_IN_BITMAP (&live->livein[bb->index], 0, i, bi)
1332 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1333 fprintf (f, " ");
1335 fprintf (f, "\n");
1339 if ((flag & LIVEDUMP_EXIT) && live->liveout)
1341 FOR_EACH_BB_FN (bb, cfun)
1343 fprintf (f, "\nLive on exit from BB%d : ", bb->index);
1344 EXECUTE_IF_SET_IN_BITMAP (&live->liveout[bb->index], 0, i, bi)
1346 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1347 fprintf (f, " ");
1349 fprintf (f, "\n");
1355 /* Generic dump for the above. */
1357 DEBUG_FUNCTION void
1358 debug (tree_live_info_d &ref)
1360 dump_live_info (stderr, &ref, 0);
1363 DEBUG_FUNCTION void
1364 debug (tree_live_info_d *ptr)
1366 if (ptr)
1367 debug (*ptr);
1368 else
1369 fprintf (stderr, "<nil>\n");
1373 #ifdef ENABLE_CHECKING
1374 /* Verify that SSA_VAR is a non-virtual SSA_NAME. */
1376 void
1377 register_ssa_partition_check (tree ssa_var)
1379 gcc_assert (TREE_CODE (ssa_var) == SSA_NAME);
1380 if (virtual_operand_p (ssa_var))
1382 fprintf (stderr, "Illegally registering a virtual SSA name :");
1383 print_generic_expr (stderr, ssa_var, TDF_SLIM);
1384 fprintf (stderr, " in the SSA->Normal phase.\n");
1385 internal_error ("SSA corruption");
1390 /* Verify that the info in LIVE matches the current cfg. */
1392 static void
1393 verify_live_on_entry (tree_live_info_p live)
1395 unsigned i;
1396 tree var;
1397 gimple stmt;
1398 basic_block bb;
1399 edge e;
1400 int num;
1401 edge_iterator ei;
1402 var_map map = live->map;
1404 /* Check for live on entry partitions and report those with a DEF in
1405 the program. This will typically mean an optimization has done
1406 something wrong. */
1407 bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1408 num = 0;
1409 FOR_EACH_EDGE (e, ei, bb->succs)
1411 int entry_block = e->dest->index;
1412 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1413 continue;
1414 for (i = 0; i < (unsigned)num_var_partitions (map); i++)
1416 basic_block tmp;
1417 tree d = NULL_TREE;
1418 bitmap loe;
1419 var = partition_to_var (map, i);
1420 stmt = SSA_NAME_DEF_STMT (var);
1421 tmp = gimple_bb (stmt);
1422 if (SSA_NAME_VAR (var))
1423 d = ssa_default_def (cfun, SSA_NAME_VAR (var));
1425 loe = live_on_entry (live, e->dest);
1426 if (loe && bitmap_bit_p (loe, i))
1428 if (!gimple_nop_p (stmt))
1430 num++;
1431 print_generic_expr (stderr, var, TDF_SLIM);
1432 fprintf (stderr, " is defined ");
1433 if (tmp)
1434 fprintf (stderr, " in BB%d, ", tmp->index);
1435 fprintf (stderr, "by:\n");
1436 print_gimple_stmt (stderr, stmt, 0, TDF_SLIM);
1437 fprintf (stderr, "\nIt is also live-on-entry to entry BB %d",
1438 entry_block);
1439 fprintf (stderr, " So it appears to have multiple defs.\n");
1441 else
1443 if (d != var)
1445 num++;
1446 print_generic_expr (stderr, var, TDF_SLIM);
1447 fprintf (stderr, " is live-on-entry to BB%d ",
1448 entry_block);
1449 if (d)
1451 fprintf (stderr, " but is not the default def of ");
1452 print_generic_expr (stderr, d, TDF_SLIM);
1453 fprintf (stderr, "\n");
1455 else
1456 fprintf (stderr, " and there is no default def.\n");
1460 else
1461 if (d == var)
1463 /* An undefined local variable does not need to be very
1464 alive. */
1465 if (ssa_undefined_value_p (var, false))
1466 continue;
1468 /* The only way this var shouldn't be marked live on entry is
1469 if it occurs in a PHI argument of the block. */
1470 size_t z;
1471 bool ok = false;
1472 gphi_iterator gsi;
1473 for (gsi = gsi_start_phis (e->dest);
1474 !gsi_end_p (gsi) && !ok;
1475 gsi_next (&gsi))
1477 gphi *phi = gsi.phi ();
1478 for (z = 0; z < gimple_phi_num_args (phi); z++)
1479 if (var == gimple_phi_arg_def (phi, z))
1481 ok = true;
1482 break;
1485 if (ok)
1486 continue;
1487 num++;
1488 print_generic_expr (stderr, var, TDF_SLIM);
1489 fprintf (stderr, " is not marked live-on-entry to entry BB%d ",
1490 entry_block);
1491 fprintf (stderr, "but it is a default def so it should be.\n");
1495 gcc_assert (num <= 0);
1497 #endif