1 /* Liveness for SSA trees.
2 Copyright (C) 2003 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 2, or (at your option)
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 COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "coretypes.h"
28 #include "basic-block.h"
30 #include "diagnostic.h"
32 #include "tree-flow.h"
33 #include "tree-simple.h"
34 #include "tree-inline.h"
37 #include "tree-alias-common.h"
39 #include "tree-dump.h"
40 #include "tree-ssa-live.h"
42 static void live_worklist (tree_live_info_p, varray_type, int);
43 static tree_live_info_p new_tree_live_info (var_map);
44 static inline void set_if_valid (var_map, bitmap, tree);
45 static inline void add_livein_if_notdef (tree_live_info_p, bitmap,
47 static inline void register_ssa_partition (var_map, tree, bool);
48 static inline void add_conflicts_if_valid (tpa_p, conflict_graph,
49 var_map, bitmap, tree);
50 static partition_pair_p find_partition_pair (coalesce_list_p, int, int, bool);
52 /* This is where the mapping from SSA version number to real storage variable
55 All SSA versions of the same variable may not ultimately be mapped back to
56 the same real variable. In that instance, we need to detect the live
57 range overlap, and give one of the variable new storage. The vector
58 'partition_to_var' tracks which partition maps to which variable.
60 Given a VAR, it is sometimes desirable to know which partition that VAR
61 represents. There is an additional field in the variable annotation to
62 track that information. */
64 /* Create a variable partition map of SIZE, initialize and return it. */
67 init_var_map (int size)
71 map = (var_map) xmalloc (sizeof (struct _var_map));
72 map->var_partition = partition_new (size);
74 = (tree *)xmalloc (size * sizeof (tree));
75 memset (map->partition_to_var, 0, size * sizeof (tree));
77 map->partition_to_compact = NULL;
78 map->compact_to_partition = NULL;
79 map->num_partitions = size;
80 map->partition_size = size;
81 map->ref_count = NULL;
86 /* Free memory associated with MAP. */
89 delete_var_map (var_map map)
91 free (map->partition_to_var);
92 partition_delete (map->var_partition);
93 if (map->partition_to_compact)
94 free (map->partition_to_compact);
95 if (map->compact_to_partition)
96 free (map->compact_to_partition);
98 free (map->ref_count);
103 /* This function will combine the partitions in MAP for VAR1 and VAR2. It
104 Returns the partition which represents the new partition. If the two
105 partitions cannot be combined, NO_PARTITION is returned. */
108 var_union (var_map map, tree var1, tree var2)
111 tree root_var = NULL_TREE;
112 tree other_var = NULL_TREE;
114 /* This is independent of partition_to_compact. If partition_to_compact is
115 on, then whichever one of these partitions is absorbed will never have a
116 dereference into the partition_to_compact array any more. */
118 if (TREE_CODE (var1) == SSA_NAME)
119 p1 = partition_find (map->var_partition, SSA_NAME_VERSION (var1));
122 p1 = var_to_partition (map, var1);
123 if (map->compact_to_partition)
124 p1 = map->compact_to_partition[p1];
128 if (TREE_CODE (var2) == SSA_NAME)
129 p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2));
132 p2 = var_to_partition (map, var2);
133 if (map->compact_to_partition)
134 p2 = map->compact_to_partition[p2];
136 /* If there is no root_var set, or its not a user variable, set the
137 root_var to this one. */
138 if (!root_var || is_gimple_tmp_var (root_var))
140 other_var = root_var;
147 if (p1 == NO_PARTITION || p2 == NO_PARTITION)
153 p3 = partition_union (map->var_partition, p1, p2);
155 if (map->partition_to_compact)
156 p3 = map->partition_to_compact[p3];
159 change_partition_var (map, root_var, p3);
161 change_partition_var (map, other_var, p3);
167 /* Compress the partition numbers in MAP such that they fall in the range
168 0..(num_partitions-1) instead of wherever they turned out during
169 the partitioning exercise. This removes any references to unused
170 partitions, thereby allowing bitmaps and other vectors to be much
171 denser. Compression type is controlled by FLAGS.
173 This is implemented such that compaction doesn't affect partitioning.
174 Ie., once partitions are created and possibly merged, running one
175 or more different kind of compaction will not affect the partitions
176 themselves. Their index might change, but all the same variables will
177 still be members of the same partition group. This allows work on reduced
178 sets, and no loss of information when a larger set is later desired.
180 In particular, coalescing can work on partitions which have 2 or more
181 definitions, and then 'recompact' later to include all the single
182 definitions for assignment to program variables. */
185 compact_var_map (var_map map, int flags)
188 int x, limit, count, tmp, root, root_i;
190 root_var_p rv = NULL;
192 limit = map->partition_size;
193 used = sbitmap_alloc (limit);
196 /* Already compressed? Abandon the old one. */
197 if (map->partition_to_compact)
199 free (map->partition_to_compact);
200 map->partition_to_compact = NULL;
202 if (map->compact_to_partition)
204 free (map->compact_to_partition);
205 map->compact_to_partition = NULL;
208 map->num_partitions = map->partition_size;
210 if (flags & VARMAP_NO_SINGLE_DEFS)
211 rv = root_var_init (map);
213 map->partition_to_compact = (int *)xmalloc (limit * sizeof (int));
214 memset (map->partition_to_compact, 0xff, (limit * sizeof (int)));
216 /* Find out which partitions are actually referenced. */
218 for (x = 0; x < limit; x++)
220 tmp = partition_find (map->var_partition, x);
221 if (!TEST_BIT (used, tmp) && map->partition_to_var[tmp] != NULL_TREE)
223 /* It is referenced, check to see if there is more than one version
224 in the root_var table, if one is available. */
227 root = root_var_find (rv, tmp);
228 root_i = root_var_first_partition (rv, root);
229 /* If there is only one, don't include this in the compaction. */
230 if (root_var_next_partition (rv, root_i) == ROOT_VAR_NONE)
238 /* Build a compacted partitioning. */
241 map->compact_to_partition = (int *)xmalloc (count * sizeof (int));
243 /* SSA renaming begins at 1, so skip 0 when compacting. */
244 EXECUTE_IF_SET_IN_SBITMAP (used, 1, x,
246 map->partition_to_compact[x] = count;
247 map->compact_to_partition[count] = x;
248 var = map->partition_to_var[x];
249 if (TREE_CODE (var) != SSA_NAME)
250 change_partition_var (map, var, count);
256 free (map->partition_to_compact);
257 map->partition_to_compact = NULL;
260 map->num_partitions = count;
263 root_var_delete (rv);
268 /* This function is used to change the representative variable in MAP for VAR's
269 partition from an SSA_NAME variable to a regular variable. This allows
270 partitions to be mapped back to real variables. */
273 change_partition_var (var_map map, tree var, int part)
277 if (TREE_CODE (var) == SSA_NAME)
281 ann->out_of_ssa_tag = 1;
282 VAR_ANN_PARTITION (ann) = part;
283 if (map->compact_to_partition)
284 map->partition_to_var[map->compact_to_partition[part]] = var;
288 /* This function looks through the program and uses FLAGS to determine what
289 SSA versioned variables are given entries in a new partition table. This
290 new partition map is returned. */
293 create_ssa_var_map (int flags)
295 block_stmt_iterator bsi;
306 #if defined ENABLE_CHECKING
307 sbitmap used_in_real_ops;
308 sbitmap used_in_virtual_ops;
311 map = init_var_map (highest_ssa_version + 1);
313 #if defined ENABLE_CHECKING
314 used_in_real_ops = sbitmap_alloc (num_referenced_vars);
315 sbitmap_zero (used_in_real_ops);
317 used_in_virtual_ops = sbitmap_alloc (num_referenced_vars);
318 sbitmap_zero (used_in_virtual_ops);
321 if (flags & SSA_VAR_MAP_REF_COUNT)
324 = (int *)xmalloc (((highest_ssa_version + 1) * sizeof (int)));
325 memset (map->ref_count, 0, (highest_ssa_version + 1) * sizeof (int));
331 for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
334 register_ssa_partition (map, PHI_RESULT (phi), false);
335 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
337 arg = PHI_ARG_DEF (phi, i);
338 if (TREE_CODE (arg) == SSA_NAME)
339 register_ssa_partition (map, arg, true);
343 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
345 stmt = bsi_stmt (bsi);
346 get_stmt_operands (stmt);
347 ann = stmt_ann (stmt);
349 /* Register USE and DEF operands in each statement. */
350 uses = USE_OPS (ann);
351 for (x = 0; x < NUM_USES (uses); x++)
353 use = USE_OP_PTR (uses, x);
354 register_ssa_partition (map, *use, true);
356 #if defined ENABLE_CHECKING
357 SET_BIT (used_in_real_ops, var_ann (SSA_NAME_VAR (*use))->uid);
361 defs = DEF_OPS (ann);
362 for (x = 0; x < NUM_DEFS (defs); x++)
364 dest = DEF_OP_PTR (defs, x);
365 register_ssa_partition (map, *dest, false);
367 #if defined ENABLE_CHECKING
368 SET_BIT (used_in_real_ops, var_ann (SSA_NAME_VAR (*dest))->uid);
372 /* While we do not care about virtual operands for
373 out of SSA, we do need to look at them to make sure
374 we mark all the variables which are used. */
375 vuses = VUSE_OPS (ann);
376 for (x = 0; x < NUM_VUSES (vuses); x++)
378 tree var = VUSE_OP (vuses, x);
381 #if defined ENABLE_CHECKING
382 SET_BIT (used_in_virtual_ops, var_ann (SSA_NAME_VAR (var))->uid);
386 vdefs = VDEF_OPS (ann);
387 for (x = 0; x < NUM_VDEFS (vdefs); x++)
389 tree var = VDEF_OP (vdefs, x);
392 #if defined ENABLE_CHECKING
393 SET_BIT (used_in_virtual_ops, var_ann (SSA_NAME_VAR (var))->uid);
399 #if defined ENABLE_CHECKING
402 sbitmap both = sbitmap_alloc (num_referenced_vars);
403 sbitmap_a_and_b (both, used_in_real_ops, used_in_virtual_ops);
404 if (sbitmap_first_set_bit (both) >= 0)
406 EXECUTE_IF_SET_IN_SBITMAP (both, 0, i,
407 fprintf (stderr, "Variable %s used in real and virtual operands\n",
408 get_name (referenced_var (i))));
412 sbitmap_free (used_in_real_ops);
413 sbitmap_free (used_in_virtual_ops);
422 /* Allocate and return a new live range information object base on MAP. */
424 static tree_live_info_p
425 new_tree_live_info (var_map map)
427 tree_live_info_p live;
430 live = (tree_live_info_p) xmalloc (sizeof (struct tree_live_info_d));
432 live->num_blocks = last_basic_block;
434 live->global = BITMAP_XMALLOC ();
436 live->livein = (bitmap *)xmalloc (num_var_partitions (map) * sizeof (bitmap));
437 for (x = 0; x < num_var_partitions (map); x++)
438 live->livein[x] = BITMAP_XMALLOC ();
440 /* liveout is deferred until it is actually requested. */
441 live->liveout = NULL;
446 /* Free storage for live range info object LIVE. */
449 delete_tree_live_info (tree_live_info_p live)
454 for (x = live->num_blocks - 1; x >= 0; x--)
455 BITMAP_XFREE (live->liveout[x]);
456 free (live->liveout);
460 for (x = num_var_partitions (live->map) - 1; x >= 0; x--)
461 BITMAP_XFREE (live->livein[x]);
465 BITMAP_XFREE (live->global);
471 /* Using LIVE, fill in all the live-on-entry blocks between the defs and uses
472 for partition I. STACK is a varray used for temporary memory which is
473 passed in rather than being allocated on every call. */
476 live_worklist (tree_live_info_p live, varray_type stack, int i)
480 basic_block def_bb = NULL;
482 var_map map = live->map;
484 var = partition_to_var (map, i);
485 if (SSA_NAME_DEF_STMT (var))
486 def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (var));
488 EXECUTE_IF_SET_IN_BITMAP (live->livein[i], 0, b,
490 VARRAY_PUSH_INT (stack, b);
493 while (VARRAY_ACTIVE_SIZE (stack) > 0)
495 b = VARRAY_TOP_INT (stack);
498 for (e = BASIC_BLOCK (b)->pred; e; e = e->pred_next)
499 if (e->src != ENTRY_BLOCK_PTR)
501 /* Its not live on entry to the block its defined in. */
502 if (e->src == def_bb)
504 if (!bitmap_bit_p (live->livein[i], e->src->index))
506 bitmap_set_bit (live->livein[i], e->src->index);
507 VARRAY_PUSH_INT (stack, e->src->index);
514 /* If VAR is in a partition of MAP, set the bit for that partition in VEC. */
517 set_if_valid (var_map map, bitmap vec, tree var)
519 int p = var_to_partition (map, var);
520 if (p != NO_PARTITION)
521 bitmap_set_bit (vec, p);
525 /* If VAR is in a partition and it isn't defined in DEF_VEC, set the livein and
526 global bit for it in the LIVE object. BB is the block being processed. */
529 add_livein_if_notdef (tree_live_info_p live, bitmap def_vec,
530 tree var, basic_block bb)
532 int p = var_to_partition (live->map, var);
533 if (p == NO_PARTITION || bb == ENTRY_BLOCK_PTR)
535 if (!bitmap_bit_p (def_vec, p))
537 bitmap_set_bit (live->livein[p], bb->index);
538 bitmap_set_bit (live->global, p);
543 /* Given partition map MAP, calculate all the live on entry bitmaps for
544 each basic block. Return a live info object. */
547 calculate_live_on_entry (var_map map)
549 tree_live_info_p live;
557 block_stmt_iterator bsi;
564 saw_def = BITMAP_XMALLOC ();
566 live = new_tree_live_info (map);
570 bitmap_clear (saw_def);
572 for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
574 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
576 var = PHI_ARG_DEF (phi, i);
577 if (!phi_ssa_name_p (var))
579 stmt = SSA_NAME_DEF_STMT (var);
580 e = PHI_ARG_EDGE (phi, i);
582 /* Any uses in PHIs which either don't have def's or are not
583 defined in the block from which the def comes, will be live
584 on entry to that block. */
585 if (!stmt || e->src != bb_for_stmt (stmt))
586 add_livein_if_notdef (live, saw_def, var, e->src);
590 /* Don't mark PHI results as defined until all the PHI nodes have
591 been processed. If the PHI sequence is:
594 The a_3 referred to in b_3's PHI node is the one incoming on the
595 edge, *not* the PHI node just seen. */
597 for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
599 var = PHI_RESULT (phi);
600 set_if_valid (map, saw_def, var);
603 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
605 stmt = bsi_stmt (bsi);
606 get_stmt_operands (stmt);
607 ann = stmt_ann (stmt);
609 uses = USE_OPS (ann);
610 num = NUM_USES (uses);
611 for (i = 0; i < num; i++)
613 vec = USE_OP_PTR (uses, i);
614 add_livein_if_notdef (live, saw_def, *vec, bb);
617 vuses = VUSE_OPS (ann);
618 num = NUM_VUSES (vuses);
619 for (i = 0; i < num; i++)
621 var = VUSE_OP (vuses, i);
622 add_livein_if_notdef (live, saw_def, var, bb);
625 vdefs = VDEF_OPS (ann);
626 num = NUM_VDEFS (vdefs);
627 for (i = 0; i < num; i++)
629 var = VDEF_OP (vdefs, i);
630 add_livein_if_notdef (live, saw_def, var, bb);
633 defs = DEF_OPS (ann);
634 num = NUM_DEFS (defs);
635 for (i = 0; i < num; i++)
637 vec = DEF_OP_PTR (defs, i);
638 set_if_valid (map, saw_def, *vec);
641 num = NUM_VDEFS (vdefs);
642 for (i = 0; i < num; i++)
644 var = VDEF_RESULT (vdefs, i);
645 set_if_valid (map, saw_def, var);
650 VARRAY_INT_INIT (stack, last_basic_block, "stack");
651 EXECUTE_IF_SET_IN_BITMAP (live->global, 0, i,
653 live_worklist (live, stack, i);
656 #ifdef ENABLE_CHECKING
657 /* Check for live on entry partitions and report those with a DEF in
658 the program. This will typically mean an optimization has done
661 bb = ENTRY_BLOCK_PTR;
663 for (e = bb->succ; e; e = e->succ_next)
665 int entry_block = e->dest->index;
666 if (e->dest == EXIT_BLOCK_PTR)
668 for (i = 0; i < num_var_partitions (map); i++)
672 var = partition_to_var (map, i);
673 stmt = SSA_NAME_DEF_STMT (var);
674 tmp = bb_for_stmt (stmt);
675 d = default_def (SSA_NAME_VAR (var));
677 if (bitmap_bit_p (live_entry_blocks (live, i), entry_block))
679 if (!IS_EMPTY_STMT (stmt))
682 print_generic_expr (stderr, var, TDF_SLIM);
683 fprintf (stderr, " is defined ");
685 fprintf (stderr, " in BB%d, ", tmp->index);
686 fprintf (stderr, "by:\n");
687 print_generic_expr (stderr, stmt, TDF_SLIM);
688 fprintf (stderr, "\nIt is also live-on-entry to entry BB %d",
690 fprintf (stderr, " So it appears to have multiple defs.\n");
697 print_generic_expr (stderr, var, TDF_SLIM);
698 fprintf (stderr, " is live-on-entry to BB%d ",entry_block);
701 fprintf (stderr, " but is not the default def of ");
702 print_generic_expr (stderr, d, TDF_SLIM);
703 fprintf (stderr, "\n");
706 fprintf (stderr, " and there is no default def.\n");
713 /* The only way this var shouldn't be marked live on entry is
714 if it occurs in a PHI argument of the block. */
716 for (phi = phi_nodes (e->dest);
718 phi = TREE_CHAIN (phi))
720 for (z = 0; z < PHI_NUM_ARGS (phi); z++)
721 if (var == PHI_ARG_DEF (phi, z))
730 print_generic_expr (stderr, var, TDF_SLIM);
731 fprintf (stderr, " is not marked live-on-entry to entry BB%d ",
733 fprintf (stderr, "but it is a default def so it should be.\n");
745 /* Calculate the live on exit vectors based on the entry info in LIVEINFO. */
748 calculate_live_on_exit (tree_live_info_p liveinfo)
757 var_map map = liveinfo->map;
759 on_exit = (bitmap *)xmalloc (last_basic_block * sizeof (bitmap));
760 for (x = 0; x < last_basic_block; x++)
761 on_exit[x] = BITMAP_XMALLOC ();
763 /* Set all the live-on-exit bits for uses in PHIs. */
766 for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
767 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
769 t = PHI_ARG_DEF (phi, i);
770 e = PHI_ARG_EDGE (phi, i);
771 if (!phi_ssa_name_p (t) || e->src == ENTRY_BLOCK_PTR)
773 set_if_valid (map, on_exit[e->src->index], t);
777 /* Set live on exit for all predecessors of live on entry's. */
778 for (i = 0; i < num_var_partitions (map); i++)
780 on_entry = live_entry_blocks (liveinfo, i);
781 EXECUTE_IF_SET_IN_BITMAP (on_entry, 0, b,
783 for (e = BASIC_BLOCK(b)->pred; e; e = e->pred_next)
784 if (e->src != ENTRY_BLOCK_PTR)
785 bitmap_set_bit (on_exit[e->src->index], i);
789 liveinfo->liveout = on_exit;
793 /* Initialize a tree_partition_associator object using MAP. */
796 tpa_init (var_map map)
799 int num_partitions = num_var_partitions (map);
802 if (num_partitions == 0)
805 tpa = (tpa_p) xmalloc (sizeof (struct tree_partition_associator_d));
807 tpa->uncompressed_num = -1;
809 tpa->next_partition = (int *)xmalloc (num_partitions * sizeof (int));
810 memset (tpa->next_partition, TPA_NONE, num_partitions * sizeof (int));
812 tpa->partition_to_tree_map = (int *)xmalloc (num_partitions * sizeof (int));
813 memset (tpa->partition_to_tree_map, TPA_NONE, num_partitions * sizeof (int));
815 x = MAX (40, (num_partitions / 20));
816 VARRAY_TREE_INIT (tpa->trees, x, "trees");
817 VARRAY_INT_INIT (tpa->first_partition, x, "first_partition");
824 /* Remove PARTITION_INDEX from TREE_INDEX's list in the tpa structure TPA. */
827 tpa_remove_partition (tpa_p tpa, int tree_index, int partition_index)
831 i = tpa_first_partition (tpa, tree_index);
832 if (i == partition_index)
834 VARRAY_INT (tpa->first_partition, tree_index) = tpa->next_partition[i];
838 for ( ; i != TPA_NONE; i = tpa_next_partition (tpa, i))
840 if (tpa->next_partition[i] == partition_index)
842 tpa->next_partition[i] = tpa->next_partition[partition_index];
850 /* Free the memory used by tree_partition_associator object TPA. */
853 tpa_delete (tpa_p tpa)
858 free (tpa->partition_to_tree_map);
859 free (tpa->next_partition);
864 /* This function will remove any tree entires from TPA which have only a single
865 element. This will help keep the size of the conflict graph down. The
866 function returns the number of remaining tree lists. */
869 tpa_compact (tpa_p tpa)
871 int last, x, y, first, swap_i;
874 /* Find the last list which has more than 1 partition. */
875 for (last = tpa->num_trees - 1; last > 0; last--)
877 first = tpa_first_partition (tpa, last);
878 if (tpa_next_partition (tpa, first) != NO_PARTITION)
885 first = tpa_first_partition (tpa, x);
887 /* If there is not more than one partition, swap with the current end
889 if (tpa_next_partition (tpa, first) == NO_PARTITION)
891 swap_t = VARRAY_TREE (tpa->trees, last);
892 swap_i = VARRAY_INT (tpa->first_partition, last);
894 /* Update the last entry. Since it is known to only have one
895 partition, there is nothing else to update. */
896 VARRAY_TREE (tpa->trees, last) = VARRAY_TREE (tpa->trees, x);
897 VARRAY_INT (tpa->first_partition, last)
898 = VARRAY_INT (tpa->first_partition, x);
899 tpa->partition_to_tree_map[tpa_first_partition (tpa, last)] = last;
901 /* Since this list is known to have more than one partition, update
902 the list owner entries. */
903 VARRAY_TREE (tpa->trees, x) = swap_t;
904 VARRAY_INT (tpa->first_partition, x) = swap_i;
905 for (y = tpa_first_partition (tpa, x);
907 y = tpa_next_partition (tpa, y))
908 tpa->partition_to_tree_map[y] = x;
910 /* Ensure last is a list with more than one partition. */
912 for (; last > x; last--)
914 first = tpa_first_partition (tpa, last);
915 if (tpa_next_partition (tpa, first) != NO_PARTITION)
922 first = tpa_first_partition (tpa, x);
923 if (tpa_next_partition (tpa, first) != NO_PARTITION)
925 tpa->uncompressed_num = tpa->num_trees;
931 /* Initialize a root_var object with SSA partitions from MAP which are based
932 on each root variable. */
935 root_var_init (var_map map)
938 int num_partitions = num_var_partitions (map);
948 seen = sbitmap_alloc (num_partitions);
951 /* Start at the end and work towards the front. This will provide a list
952 that is ordered from smallest to largest. */
953 for (x = num_partitions - 1; x >= 0; x--)
955 t = partition_to_var (map, x);
957 /* The var map may not be compacted yet, so check for NULL. */
961 p = var_to_partition (map, t);
963 #ifdef ENABLE_CHECKING
964 if (p == NO_PARTITION)
968 /* Make sure we only put coalesced partitions into the list once. */
969 if (TEST_BIT (seen, p))
972 if (TREE_CODE (t) == SSA_NAME)
973 t = SSA_NAME_VAR (t);
975 if (ann->root_var_processed)
977 rv->next_partition[p] = VARRAY_INT (rv->first_partition,
978 VAR_ANN_ROOT_INDEX (ann));
979 VARRAY_INT (rv->first_partition, VAR_ANN_ROOT_INDEX (ann)) = p;
983 ann->root_var_processed = 1;
984 VAR_ANN_ROOT_INDEX (ann) = rv->num_trees++;
985 VARRAY_PUSH_TREE (rv->trees, t);
986 VARRAY_PUSH_INT (rv->first_partition, p);
988 rv->partition_to_tree_map[p] = VAR_ANN_ROOT_INDEX (ann);
991 /* Reset the out_of_ssa_tag flag on each variable for later use. */
992 for (x = 0; x < rv->num_trees; x++)
994 t = VARRAY_TREE (rv->trees, x);
995 var_ann (t)->root_var_processed = 0;
1003 /* Initialize a type_var structure which associates all the partitions in MAP
1004 of the same type to the type node's index. Volatiles are ignored. */
1007 type_var_init (var_map map)
1011 int num_partitions = num_var_partitions (map);
1015 seen = sbitmap_alloc (num_partitions);
1016 sbitmap_zero (seen);
1018 tv = tpa_init (map);
1022 for (x = num_partitions - 1; x >= 0; x--)
1024 t = partition_to_var (map, x);
1026 /* Disallow coalescing of these types of variables. */
1028 || TREE_THIS_VOLATILE (t)
1029 || TREE_CODE (t) == RESULT_DECL
1030 || TREE_CODE (t) == PARM_DECL
1032 && (DECL_REGISTER (t)
1033 || !DECL_ARTIFICIAL (t)
1034 || DECL_RTL_SET_P (t))))
1037 p = var_to_partition (map, t);
1039 #ifdef ENABLE_CHECKING
1040 if (p == NO_PARTITION)
1044 /* If partitions have been coalesced, only add the representative
1045 for the partition to the list once. */
1046 if (TEST_BIT (seen, p))
1051 /* Find the list for this type. */
1052 for (y = 0; y < tv->num_trees; y++)
1053 if (t == VARRAY_TREE (tv->trees, y))
1055 if (y == tv->num_trees)
1058 VARRAY_PUSH_TREE (tv->trees, t);
1059 VARRAY_PUSH_INT (tv->first_partition, p);
1063 tv->next_partition[p] = VARRAY_INT (tv->first_partition, y);
1064 VARRAY_INT (tv->first_partition, y) = p;
1066 tv->partition_to_tree_map[p] = y;
1068 sbitmap_free (seen);
1073 /* Create a new coalesce list object from MAP and return it. */
1076 create_coalesce_list (var_map map)
1078 coalesce_list_p list;
1080 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
1083 list->add_mode = true;
1084 list->list = (partition_pair_p *) xcalloc (num_var_partitions (map),
1085 sizeof (struct partition_pair_d));
1090 /* Delete coalesce list CL. */
1093 delete_coalesce_list (coalesce_list_p cl)
1100 /* Find a matching coalesce pair object in CL for partitions P1 and P2. If
1101 one isn't found, return NULL if CREATE is false, otherwise create a new
1102 coalesce pair object and return it. */
1104 static partition_pair_p
1105 find_partition_pair (coalesce_list_p cl, int p1, int p2, bool create)
1107 partition_pair_p node, tmp;
1110 /* Normalize so that p1 is the smaller value. */
1120 /* The list is sorted such that if we find a value greater than p2,
1121 p2 is not in the list. */
1122 for (node = cl->list[p1]; node; node = node->next)
1124 if (node->second_partition == p2)
1127 if (node->second_partition > p2)
1135 node = (partition_pair_p) xmalloc (sizeof (struct partition_pair_d));
1136 node->first_partition = p1;
1137 node->second_partition = p2;
1142 node->next = tmp->next;
1147 /* This is now the first node in the list. */
1148 node->next = cl->list[p1];
1149 cl->list[p1] = node;
1156 /* Add a potential coalesce between P1 and P2 in CL with a cost of VALUE. */
1159 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
1161 partition_pair_p node;
1163 #ifdef ENABLE_CHECKING
1171 node = find_partition_pair (cl, p1, p2, true);
1173 node->cost += value;
1177 /* Comparison function to allow qsort to sort P1 and P2 in descending order. */
1180 int compare_pairs (const void *p1, const void *p2)
1182 return (*(partition_pair_p *)p2)->cost - (*(partition_pair_p *)p1)->cost;
1186 /* Prepare CL for removal of preferred pairs. When finished, list element
1187 0 has all the coalesce pairs, sorted in order from most important coalesce
1188 to least important. */
1191 sort_coalesce_list (coalesce_list_p cl)
1194 partition_pair_p chain, p;
1195 partition_pair_p *list;
1200 cl->add_mode = false;
1202 /* Compact the array of lists to a single list, and count the elements. */
1205 for (x = 0; x < num_var_partitions (cl->map); x++)
1206 if (cl->list[x] != NULL)
1208 for (p = cl->list[x]; p->next != NULL; p = p->next)
1212 chain = cl->list[x];
1216 /* Only call qsort if there are more than 2 items. */
1219 list = xmalloc (sizeof (partition_pair_p) * num);
1221 for (p = chain; p != NULL; p = p->next)
1224 #ifdef ENABLE_CHECKING
1229 qsort (list, count, sizeof (partition_pair_p), compare_pairs);
1232 for (x = 1; x < num; x++)
1238 cl->list[0] = list[0];
1243 cl->list[0] = chain;
1246 /* Simply swap the two elements if they are in the wrong order. */
1247 if (chain->cost < chain->next->cost)
1249 cl->list[0] = chain->next;
1250 cl->list[0]->next = chain;
1258 /* Retrieve the best remaining pair to coalesce from CL. Returns the 2
1259 partitions via P1 and P2. Their calculated cost is returned by the function.
1260 NO_BEST_COALESCE is returned if the coalesce list is empty. */
1263 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
1265 partition_pair_p node;
1273 return NO_BEST_COALESCE;
1275 cl->list[0] = node->next;
1277 *p1 = node->first_partition;
1278 *p2 = node->second_partition;
1286 /* If variable VAR is in a partition in MAP, add a conflict in GRAPH between
1287 VAR and any other live partitions in VEC which are associated via TPA.
1288 Reset the live bit in VEC. */
1291 add_conflicts_if_valid (tpa_p tpa, conflict_graph graph,
1292 var_map map, bitmap vec, tree var)
1295 p = var_to_partition (map, var);
1296 if (p != NO_PARTITION)
1298 bitmap_clear_bit (vec, p);
1299 first = tpa_find_tree (tpa, p);
1300 /* If find returns nothing, this object isn't interesting. */
1301 if (first == TPA_NONE)
1303 /* Only add interferences between objects in the same list. */
1304 for (y = tpa_first_partition (tpa, first);
1306 y = tpa_next_partition (tpa, y))
1308 if (bitmap_bit_p (vec, y))
1309 conflict_graph_add (graph, p, y);
1315 /* Return a conflict graph for the information contained in LIVE_INFO. Only
1316 conflicts between items in the same TPA list are added. If optional
1317 coalesce list CL is passed in, any copies encountered are added. */
1320 build_tree_conflict_graph (tree_live_info_p liveinfo, tpa_p tpa,
1323 conflict_graph graph;
1328 varray_type partition_link, tpa_to_clear, tpa_nodes;
1333 map = live_var_map (liveinfo);
1334 graph = conflict_graph_new (num_var_partitions (map));
1336 if (tpa_num_trees (tpa) == 0)
1339 live = BITMAP_XMALLOC ();
1341 VARRAY_INT_INIT (partition_link, num_var_partitions (map) + 1, "part_link");
1342 VARRAY_INT_INIT (tpa_nodes, tpa_num_trees (tpa), "tpa nodes");
1343 VARRAY_INT_INIT (tpa_to_clear, 50, "tpa to clear");
1347 block_stmt_iterator bsi;
1350 /* Start with live on exit temporaries. */
1351 bitmap_copy (live, live_on_exit (liveinfo, bb));
1353 for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_prev (&bsi))
1355 bool is_a_copy = false;
1356 tree stmt = bsi_stmt (bsi);
1359 get_stmt_operands (stmt);
1360 ann = stmt_ann (stmt);
1362 /* A copy between 2 partitions does not introduce an interference
1363 by itself. If they did, you would never be able to coalesce
1364 two things which are copied. If the two variables really do
1365 conflict, they will conflict elsewhere in the program.
1367 This is handled specially here since we may also be interested
1368 in copies between real variables and SSA_NAME variables. We may
1369 be interested in trying to coalesce SSA_NAME variables with
1370 root variables in some cases. */
1372 if (TREE_CODE (stmt) == MODIFY_EXPR)
1374 tree lhs = TREE_OPERAND (stmt, 0);
1375 tree rhs = TREE_OPERAND (stmt, 1);
1379 if (DECL_P (lhs) || TREE_CODE (lhs) == SSA_NAME)
1380 p1 = var_to_partition (map, lhs);
1384 if (DECL_P (rhs) || TREE_CODE (rhs) == SSA_NAME)
1385 p2 = var_to_partition (map, rhs);
1389 if (p1 != NO_PARTITION && p2 != NO_PARTITION)
1392 bit = bitmap_bit_p (live, p2);
1393 /* If the RHS is live, make it not live while we add
1394 the conflicts, then make it live again. */
1396 bitmap_clear_bit (live, p2);
1397 add_conflicts_if_valid (tpa, graph, map, live, lhs);
1399 bitmap_set_bit (live, p2);
1401 add_coalesce (cl, p1, p2, 1);
1402 set_if_valid (map, live, rhs);
1410 defs = DEF_OPS (ann);
1411 num = NUM_DEFS (defs);
1412 for (x = 0; x < num; x++)
1414 var_p = DEF_OP_PTR (defs, x);
1415 add_conflicts_if_valid (tpa, graph, map, live, *var_p);
1418 uses = USE_OPS (ann);
1419 num = NUM_USES (uses);
1420 for (x = 0; x < num; x++)
1422 var_p = USE_OP_PTR (uses, x);
1423 set_if_valid (map, live, *var_p);
1428 /* If result of a PHI is unused, then the loops over the statements
1429 will not record any conflicts. However, since the PHI node is
1430 going to be translated out of SSA form we must record a conflict
1431 between the result of the PHI and any variables with are live.
1432 Otherwise the out-of-ssa translation may create incorrect code. */
1433 for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
1435 tree result = PHI_RESULT (phi);
1436 int p = var_to_partition (map, result);
1438 if (p != NO_PARTITION && ! bitmap_bit_p (live, p))
1439 add_conflicts_if_valid (tpa, graph, map, live, result);
1442 /* Anything which is still live at this point interferes.
1443 In order to implement this efficiently, only conflicts between
1444 partitions which have the same TPA root need be added.
1445 TPA roots which have been seen are tracked in 'tpa_nodes'. A non-zero
1446 entry points to an index into 'partition_link', which then indexes
1447 into itself forming a linked list of partitions sharing a tpa root
1448 which have been seen as live up to this point. Since partitions start
1449 at index zero, all entries in partition_link are (partition + 1).
1451 Conflicts are added between the current partition and any already seen.
1452 tpa_clear contains all the tpa_roots processed, and these are the only
1453 entries which need to be zero'd out for a clean restart. */
1455 EXECUTE_IF_SET_IN_BITMAP (live, 0, x,
1457 i = tpa_find_tree (tpa, x);
1460 int start = VARRAY_INT (tpa_nodes, i);
1461 /* If start is 0, a new root reference list is being started.
1462 Register it to be cleared. */
1464 VARRAY_PUSH_INT (tpa_to_clear, i);
1466 /* Add interferences to other tpa members seen. */
1467 for (y = start; y != 0; y = VARRAY_INT (partition_link, y))
1468 conflict_graph_add (graph, x, y - 1);
1469 VARRAY_INT (tpa_nodes, i) = x + 1;
1470 VARRAY_INT (partition_link, x + 1) = start;
1474 /* Now clear the used tpa root references. */
1475 for (l = 0; l < VARRAY_ACTIVE_SIZE (tpa_to_clear); l++)
1476 VARRAY_INT (tpa_nodes, VARRAY_INT (tpa_to_clear, l)) = 0;
1477 VARRAY_POP_ALL (tpa_to_clear);
1480 BITMAP_XFREE (live);
1485 /* This routine will attempt to coalesce the elements in TPA subject to the
1486 conflicts found in GRAPH. If optional coalesce_list CL is provided,
1487 only coalesces specified within the coalesce list are attempted. Otherwise
1488 an attempt is made to coalesce as many partitions within each TPA grouping
1489 as possible. If DEBUG is provided, debug output will be sent there. */
1492 coalesce_tpa_members (tpa_p tpa, conflict_graph graph, var_map map,
1493 coalesce_list_p cl, FILE *debug)
1498 /* Attempt to coalesce any items in a coalesce list. */
1501 while (pop_best_coalesce (cl, &x, &y) != NO_BEST_COALESCE)
1505 fprintf (debug, "Coalesce list: (%d)", x);
1506 print_generic_expr (debug, partition_to_var (map, x), TDF_SLIM);
1507 fprintf (debug, " & (%d)", y);
1508 print_generic_expr (debug, partition_to_var (map, y), TDF_SLIM);
1511 w = tpa_find_tree (tpa, x);
1512 z = tpa_find_tree (tpa, y);
1513 if (w != z || w == TPA_NONE || z == TPA_NONE)
1518 fprintf (debug, ": Fail, Non-matching TPA's\n");
1520 fprintf (debug, ": Fail %d non TPA.\n", x);
1522 fprintf (debug, ": Fail %d non TPA.\n", y);
1526 var = partition_to_var (map, x);
1527 tmp = partition_to_var (map, y);
1528 x = var_to_partition (map, var);
1529 y = var_to_partition (map, tmp);
1531 fprintf (debug, " [map: %d, %d] ", x, y);
1535 fprintf (debug, ": Already Coalesced.\n");
1538 if (!conflict_graph_conflict_p (graph, x, y))
1540 z = var_union (map, var, tmp);
1541 if (z == NO_PARTITION)
1544 fprintf (debug, ": Unable to perform partition union.\n");
1548 /* z is the new combined partition. We need to remove the other
1549 partition from the list. Set x to be that other partition. */
1552 conflict_graph_merge_regs (graph, x, y);
1553 w = tpa_find_tree (tpa, y);
1554 tpa_remove_partition (tpa, w, y);
1558 conflict_graph_merge_regs (graph, y, x);
1559 w = tpa_find_tree (tpa, x);
1560 tpa_remove_partition (tpa, w, x);
1564 fprintf (debug, ": Success -> %d\n", z);
1568 fprintf (debug, ": Fail due to conflict\n");
1570 /* If using a coalesce list, don't try to coalesce anything else. */
1574 for (x = 0; x < tpa_num_trees (tpa); x++)
1576 while (tpa_first_partition (tpa, x) != TPA_NONE)
1579 /* Coalesce first partition with anything that doesn't conflict. */
1580 y = tpa_first_partition (tpa, x);
1581 tpa_remove_partition (tpa, x, y);
1583 var = partition_to_var (map, y);
1584 /* p1 is the partition representative to which y belongs. */
1585 p1 = var_to_partition (map, var);
1587 for (z = tpa_next_partition (tpa, y);
1589 z = tpa_next_partition (tpa, z))
1591 tmp = partition_to_var (map, z);
1592 /* p2 is the partition representative to which z belongs. */
1593 p2 = var_to_partition (map, tmp);
1596 fprintf (debug, "Coalesce : ");
1597 print_generic_expr (debug, var, TDF_SLIM);
1598 fprintf (debug, " &");
1599 print_generic_expr (debug, tmp, TDF_SLIM);
1600 fprintf (debug, " (%d ,%d)", p1, p2);
1603 /* If partitions are already merged, don't check for conflict. */
1606 tpa_remove_partition (tpa, x, z);
1608 fprintf (debug, ": Already coalesced\n");
1611 if (!conflict_graph_conflict_p (graph, p1, p2))
1614 if (tpa_find_tree (tpa, y) == TPA_NONE
1615 || tpa_find_tree (tpa, z) == TPA_NONE)
1618 fprintf (debug, ": Fail non-TPA member\n");
1621 if ((v = var_union (map, var, tmp)) == NO_PARTITION)
1624 fprintf (debug, ": Fail cannot combine partitions\n");
1628 tpa_remove_partition (tpa, x, z);
1630 conflict_graph_merge_regs (graph, v, z);
1633 /* Update the first partition's representative. */
1634 conflict_graph_merge_regs (graph, v, y);
1638 /* The root variable of the partition may be changed
1640 var = partition_to_var (map, p1);
1643 fprintf (debug, ": Success -> %d\n", v);
1647 fprintf (debug, ": Fail, Conflict\n");
1654 /* Send debug info for coalesce list CL to file F. */
1657 dump_coalesce_list (FILE *f, coalesce_list_p cl)
1659 partition_pair_p node;
1665 fprintf (f, "Coalesce List:\n");
1666 num = num_var_partitions (cl->map);
1667 for (x = 0; x < num; x++)
1673 print_generic_expr (f, partition_to_var (cl->map, x), TDF_SLIM);
1674 fprintf (f, "] - ");
1675 for ( ; node; node = node->next)
1677 var = partition_to_var (cl->map, node->second_partition);
1678 print_generic_expr (f, var, TDF_SLIM);
1679 fprintf (f, "(%1d), ", node->cost);
1687 fprintf (f, "Sorted Coalesce list:\n");
1688 for (node = cl->list[0]; node; node = node->next)
1690 fprintf (f, "(%d) ", node->cost);
1691 var = partition_to_var (cl->map, node->first_partition);
1692 print_generic_expr (f, var, TDF_SLIM);
1694 var = partition_to_var (cl->map, node->second_partition);
1695 print_generic_expr (f, var, TDF_SLIM);
1702 /* Output tree_partition_associator object TPA to file F.. */
1705 tpa_dump (FILE *f, tpa_p tpa)
1712 for (x = 0; x < tpa_num_trees (tpa); x++)
1714 print_generic_expr (f, tpa_tree (tpa, x), TDF_SLIM);
1715 fprintf (f, " : (");
1716 for (i = tpa_first_partition (tpa, x);
1718 i = tpa_next_partition (tpa, i))
1720 fprintf (f, "(%d)",i);
1721 print_generic_expr (f, partition_to_var (tpa->map, i), TDF_SLIM);
1724 #ifdef ENABLE_CHECKING
1725 if (tpa_find_tree (tpa, i) != x)
1726 fprintf (f, "**find tree incorrectly set** ");
1736 /* Output partition map MAP to file F. */
1739 dump_var_map (FILE *f, var_map map)
1745 fprintf (f, "\nPartition map \n\n");
1747 for (x = 0; x < map->num_partitions; x++)
1749 if (map->compact_to_partition != NULL)
1750 p = map->compact_to_partition[x];
1754 if (map->partition_to_var[p] == NULL_TREE)
1758 for (y = 1; y < highest_ssa_version; y++)
1760 p = partition_find (map->var_partition, y);
1761 if (map->partition_to_compact)
1762 p = map->partition_to_compact[p];
1767 fprintf(f, "Partition %d (", x);
1768 print_generic_expr (f, partition_to_var (map, p), TDF_SLIM);
1771 fprintf (f, "%d ", y);
1781 /* Output live range info LIVE to file F, controlled by FLAG. */
1784 dump_live_info (FILE *f, tree_live_info_p live, int flag)
1788 var_map map = live->map;
1790 if ((flag & LIVEDUMP_ENTRY) && live->livein)
1794 fprintf (f, "\nLive on entry to BB%d : ", bb->index);
1795 for (i = 0; i < num_var_partitions (map); i++)
1797 if (bitmap_bit_p (live_entry_blocks (live, i), bb->index))
1799 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1807 if ((flag & LIVEDUMP_EXIT) && live->liveout)
1811 fprintf (f, "\nLive on exit from BB%d : ", bb->index);
1812 EXECUTE_IF_SET_IN_BITMAP (live->liveout[bb->index], 0, i,
1814 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1822 /* Register partitions in MAP so that we can take VARS out of SSA form.
1823 This requires a walk over all the PHI nodes and all the statements. */
1826 register_ssa_partitions_for_vars (bitmap vars, var_map map)
1830 if (bitmap_first_set_bit (vars) >= 0)
1833 /* Find every instance (SSA_NAME) of variables in VARs and
1834 register a new partition for them. This requires examining
1835 every statement and every PHI node once. */
1838 block_stmt_iterator bsi;
1842 /* Register partitions for SSA_NAMEs appearing in the PHI
1843 nodes in this basic block.
1845 Note we delete PHI nodes in this loop if they are
1846 associated with virtual vars which are going to be
1848 for (phi = phi_nodes (bb); phi; phi = next)
1850 tree result = SSA_NAME_VAR (PHI_RESULT (phi));
1852 next = TREE_CHAIN (phi);
1853 if (bitmap_bit_p (vars, var_ann (result)->uid))
1855 if (! is_gimple_reg (result))
1856 remove_phi_node (phi, NULL_TREE, bb);
1861 /* Register a partition for the result. */
1862 register_ssa_partition (map, PHI_RESULT (phi), 0);
1864 /* Register a partition for each argument as needed. */
1865 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1867 tree arg = PHI_ARG_DEF (phi, i);
1869 if (TREE_CODE (arg) != SSA_NAME)
1871 if (!bitmap_bit_p (vars,
1872 var_ann (SSA_NAME_VAR (arg))->uid))
1875 register_ssa_partition (map, arg, 1);
1881 /* Now register partitions for SSA_NAMEs appearing in each
1882 statement in this block. */
1883 for (bsi = bsi_start (bb); ! bsi_end_p (bsi); bsi_next (&bsi))
1885 stmt_ann_t ann = stmt_ann (bsi_stmt (bsi));
1886 use_optype uses = USE_OPS (ann);
1887 def_optype defs = DEF_OPS (ann);
1890 for (i = 0; i < NUM_USES (uses); i++)
1892 tree op = USE_OP (uses, i);
1894 if (TREE_CODE (op) == SSA_NAME
1895 && bitmap_bit_p (vars, var_ann (SSA_NAME_VAR (op))->uid))
1896 register_ssa_partition (map, op, 1);
1899 for (i = 0; i < NUM_DEFS (defs); i++)
1901 tree op = DEF_OP (defs, i);
1903 if (TREE_CODE (op) == SSA_NAME
1904 && bitmap_bit_p (vars,
1905 var_ann (SSA_NAME_VAR (op))->uid))
1906 register_ssa_partition (map, op, 0);