* defaults.h (FRAME_GROWS_DOWNWARD): Define to 0 if not defined.
[official-gcc.git] / gcc / tree-ssa-live.c
blobcbd36f1e1aaed19537565e3f3503732135226b8e
1 /* Liveness for SSA trees.
2 Copyright (C) 2003, 2004, 2005 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)
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 COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "basic-block.h"
29 #include "function.h"
30 #include "diagnostic.h"
31 #include "bitmap.h"
32 #include "tree-flow.h"
33 #include "tree-gimple.h"
34 #include "tree-inline.h"
35 #include "varray.h"
36 #include "timevar.h"
37 #include "hashtab.h"
38 #include "tree-dump.h"
39 #include "tree-ssa-live.h"
40 #include "toplev.h"
42 static void live_worklist (tree_live_info_p, int *, 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,
46 tree, basic_block);
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
53 is tracked.
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. */
66 var_map
67 init_var_map (int size)
69 var_map map;
71 map = (var_map) xmalloc (sizeof (struct _var_map));
72 map->var_partition = partition_new (size);
73 map->partition_to_var
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;
82 return map;
86 /* Free memory associated with MAP. */
88 void
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);
97 if (map->ref_count)
98 free (map->ref_count);
99 free (map);
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)
110 int p1, p2, p3;
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));
120 else
122 p1 = var_to_partition (map, var1);
123 if (map->compact_to_partition)
124 p1 = map->compact_to_partition[p1];
125 root_var = var1;
128 if (TREE_CODE (var2) == SSA_NAME)
129 p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2));
130 else
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 it's not a user variable, set the
137 root_var to this one. */
138 if (!root_var || (DECL_P (root_var) && DECL_IGNORED_P (root_var)))
140 other_var = root_var;
141 root_var = var2;
143 else
144 other_var = var2;
147 gcc_assert (p1 != NO_PARTITION);
148 gcc_assert (p2 != NO_PARTITION);
150 if (p1 == p2)
151 p3 = p1;
152 else
153 p3 = partition_union (map->var_partition, p1, p2);
155 if (map->partition_to_compact)
156 p3 = map->partition_to_compact[p3];
158 if (root_var)
159 change_partition_var (map, root_var, p3);
160 if (other_var)
161 change_partition_var (map, other_var, p3);
163 return p3;
167 /* Compress the partition numbers in MAP such that they fall in the range
168 0..(num_partitions-1) instead of wherever they turned out during
169 the partitioning exercise. This removes any references to unused
170 partitions, thereby allowing bitmaps and other vectors to be much
171 denser. 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. */
184 void
185 compact_var_map (var_map map, int flags)
187 sbitmap used;
188 int tmp, root, root_i;
189 unsigned int x, limit, count;
190 tree var;
191 root_var_p rv = NULL;
193 limit = map->partition_size;
194 used = sbitmap_alloc (limit);
195 sbitmap_zero (used);
197 /* Already compressed? Abandon the old one. */
198 if (map->partition_to_compact)
200 free (map->partition_to_compact);
201 map->partition_to_compact = NULL;
203 if (map->compact_to_partition)
205 free (map->compact_to_partition);
206 map->compact_to_partition = NULL;
209 map->num_partitions = map->partition_size;
211 if (flags & VARMAP_NO_SINGLE_DEFS)
212 rv = root_var_init (map);
214 map->partition_to_compact = (int *)xmalloc (limit * sizeof (int));
215 memset (map->partition_to_compact, 0xff, (limit * sizeof (int)));
217 /* Find out which partitions are actually referenced. */
218 count = 0;
219 for (x = 0; x < limit; x++)
221 tmp = partition_find (map->var_partition, x);
222 if (!TEST_BIT (used, tmp) && map->partition_to_var[tmp] != NULL_TREE)
224 /* It is referenced, check to see if there is more than one version
225 in the root_var table, if one is available. */
226 if (rv)
228 root = root_var_find (rv, tmp);
229 root_i = root_var_first_partition (rv, root);
230 /* If there is only one, don't include this in the compaction. */
231 if (root_var_next_partition (rv, root_i) == ROOT_VAR_NONE)
232 continue;
234 SET_BIT (used, tmp);
235 count++;
239 /* Build a compacted partitioning. */
240 if (count != limit)
242 sbitmap_iterator sbi;
244 map->compact_to_partition = (int *)xmalloc (count * sizeof (int));
245 count = 0;
246 /* SSA renaming begins at 1, so skip 0 when compacting. */
247 EXECUTE_IF_SET_IN_SBITMAP (used, 1, x, sbi)
249 map->partition_to_compact[x] = count;
250 map->compact_to_partition[count] = x;
251 var = map->partition_to_var[x];
252 if (TREE_CODE (var) != SSA_NAME)
253 change_partition_var (map, var, count);
254 count++;
257 else
259 free (map->partition_to_compact);
260 map->partition_to_compact = NULL;
263 map->num_partitions = count;
265 if (rv)
266 root_var_delete (rv);
267 sbitmap_free (used);
271 /* This function is used to change the representative variable in MAP for VAR's
272 partition from an SSA_NAME variable to a regular variable. This allows
273 partitions to be mapped back to real variables. */
275 void
276 change_partition_var (var_map map, tree var, int part)
278 var_ann_t ann;
280 gcc_assert (TREE_CODE (var) != SSA_NAME);
282 ann = var_ann (var);
283 ann->out_of_ssa_tag = 1;
284 VAR_ANN_PARTITION (ann) = part;
285 if (map->compact_to_partition)
286 map->partition_to_var[map->compact_to_partition[part]] = var;
290 /* Helper function for mark_all_vars_used, called via walk_tree. */
292 static tree
293 mark_all_vars_used_1 (tree *tp, int *walk_subtrees,
294 void *data ATTRIBUTE_UNUSED)
296 tree t = *tp;
298 /* Only need to mark VAR_DECLS; parameters and return results are not
299 eliminated as unused. */
300 if (TREE_CODE (t) == VAR_DECL)
301 set_is_used (t);
303 if (IS_TYPE_OR_DECL_P (t))
304 *walk_subtrees = 0;
306 return NULL;
309 /* Mark all VAR_DECLS under *EXPR_P as used, so that they won't be
310 eliminated during the tree->rtl conversion process. */
312 static inline void
313 mark_all_vars_used (tree *expr_p)
315 walk_tree (expr_p, mark_all_vars_used_1, NULL, NULL);
318 /* This function looks through the program and uses FLAGS to determine what
319 SSA versioned variables are given entries in a new partition table. This
320 new partition map is returned. */
322 var_map
323 create_ssa_var_map (int flags)
325 block_stmt_iterator bsi;
326 basic_block bb;
327 tree dest, use;
328 tree stmt;
329 var_map map;
330 ssa_op_iter iter;
331 #ifdef ENABLE_CHECKING
332 sbitmap used_in_real_ops;
333 sbitmap used_in_virtual_ops;
334 #endif
336 map = init_var_map (num_ssa_names + 1);
338 #ifdef ENABLE_CHECKING
339 used_in_real_ops = sbitmap_alloc (num_referenced_vars);
340 sbitmap_zero (used_in_real_ops);
342 used_in_virtual_ops = sbitmap_alloc (num_referenced_vars);
343 sbitmap_zero (used_in_virtual_ops);
344 #endif
346 if (flags & SSA_VAR_MAP_REF_COUNT)
348 map->ref_count
349 = (int *)xmalloc (((num_ssa_names + 1) * sizeof (int)));
350 memset (map->ref_count, 0, (num_ssa_names + 1) * sizeof (int));
353 FOR_EACH_BB (bb)
355 tree phi, arg;
356 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
358 int i;
359 register_ssa_partition (map, PHI_RESULT (phi), false);
360 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
362 arg = PHI_ARG_DEF (phi, i);
363 if (TREE_CODE (arg) == SSA_NAME)
364 register_ssa_partition (map, arg, true);
366 mark_all_vars_used (&PHI_ARG_DEF_TREE (phi, i));
370 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
372 stmt = bsi_stmt (bsi);
374 /* Register USE and DEF operands in each statement. */
375 FOR_EACH_SSA_TREE_OPERAND (use , stmt, iter, SSA_OP_USE)
377 register_ssa_partition (map, use, true);
379 #ifdef ENABLE_CHECKING
380 SET_BIT (used_in_real_ops, var_ann (SSA_NAME_VAR (use))->uid);
381 #endif
384 FOR_EACH_SSA_TREE_OPERAND (dest, stmt, iter, SSA_OP_DEF)
386 register_ssa_partition (map, dest, false);
388 #ifdef ENABLE_CHECKING
389 SET_BIT (used_in_real_ops, var_ann (SSA_NAME_VAR (dest))->uid);
390 #endif
393 #ifdef ENABLE_CHECKING
394 /* Validate that virtual ops don't get used in funny ways. */
395 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter,
396 SSA_OP_VIRTUAL_USES | SSA_OP_VMUSTDEF)
398 SET_BIT (used_in_virtual_ops, var_ann (SSA_NAME_VAR (use))->uid);
401 #endif /* ENABLE_CHECKING */
403 mark_all_vars_used (bsi_stmt_ptr (bsi));
407 #if defined ENABLE_CHECKING
409 unsigned i;
410 sbitmap both = sbitmap_alloc (num_referenced_vars);
411 sbitmap_a_and_b (both, used_in_real_ops, used_in_virtual_ops);
412 if (sbitmap_first_set_bit (both) >= 0)
414 sbitmap_iterator sbi;
416 EXECUTE_IF_SET_IN_SBITMAP (both, 0, i, sbi)
417 fprintf (stderr, "Variable %s used in real and virtual operands\n",
418 get_name (referenced_var (i)));
419 internal_error ("SSA corruption");
422 sbitmap_free (used_in_real_ops);
423 sbitmap_free (used_in_virtual_ops);
424 sbitmap_free (both);
426 #endif
428 return map;
432 /* Allocate and return a new live range information object base on MAP. */
434 static tree_live_info_p
435 new_tree_live_info (var_map map)
437 tree_live_info_p live;
438 unsigned x;
440 live = (tree_live_info_p) xmalloc (sizeof (struct tree_live_info_d));
441 live->map = map;
442 live->num_blocks = last_basic_block;
444 live->global = BITMAP_ALLOC (NULL);
446 live->livein = (bitmap *)xmalloc (num_var_partitions (map) * sizeof (bitmap));
447 for (x = 0; x < num_var_partitions (map); x++)
448 live->livein[x] = BITMAP_ALLOC (NULL);
450 /* liveout is deferred until it is actually requested. */
451 live->liveout = NULL;
452 return live;
456 /* Free storage for live range info object LIVE. */
458 void
459 delete_tree_live_info (tree_live_info_p live)
461 int x;
462 if (live->liveout)
464 for (x = live->num_blocks - 1; x >= 0; x--)
465 BITMAP_FREE (live->liveout[x]);
466 free (live->liveout);
468 if (live->livein)
470 for (x = num_var_partitions (live->map) - 1; x >= 0; x--)
471 BITMAP_FREE (live->livein[x]);
472 free (live->livein);
474 if (live->global)
475 BITMAP_FREE (live->global);
477 free (live);
481 /* Using LIVE, fill in all the live-on-entry blocks between the defs and uses
482 for partition I. STACK is a varray used for temporary memory which is
483 passed in rather than being allocated on every call. */
485 static void
486 live_worklist (tree_live_info_p live, int *stack, int i)
488 unsigned b;
489 tree var;
490 basic_block def_bb = NULL;
491 edge e;
492 var_map map = live->map;
493 edge_iterator ei;
494 bitmap_iterator bi;
495 int *tos = stack;
497 var = partition_to_var (map, i);
498 if (SSA_NAME_DEF_STMT (var))
499 def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (var));
501 EXECUTE_IF_SET_IN_BITMAP (live->livein[i], 0, b, bi)
503 *tos++ = b;
506 while (tos != stack)
508 b = *--tos;
510 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (b)->preds)
511 if (e->src != ENTRY_BLOCK_PTR)
513 /* Its not live on entry to the block its defined in. */
514 if (e->src == def_bb)
515 continue;
516 if (!bitmap_bit_p (live->livein[i], e->src->index))
518 bitmap_set_bit (live->livein[i], e->src->index);
519 *tos++ = e->src->index;
526 /* If VAR is in a partition of MAP, set the bit for that partition in VEC. */
528 static inline void
529 set_if_valid (var_map map, bitmap vec, tree var)
531 int p = var_to_partition (map, var);
532 if (p != NO_PARTITION)
533 bitmap_set_bit (vec, p);
537 /* If VAR is in a partition and it isn't defined in DEF_VEC, set the livein and
538 global bit for it in the LIVE object. BB is the block being processed. */
540 static inline void
541 add_livein_if_notdef (tree_live_info_p live, bitmap def_vec,
542 tree var, basic_block bb)
544 int p = var_to_partition (live->map, var);
545 if (p == NO_PARTITION || bb == ENTRY_BLOCK_PTR)
546 return;
547 if (!bitmap_bit_p (def_vec, p))
549 bitmap_set_bit (live->livein[p], bb->index);
550 bitmap_set_bit (live->global, p);
555 /* Given partition map MAP, calculate all the live on entry bitmaps for
556 each basic block. Return a live info object. */
558 tree_live_info_p
559 calculate_live_on_entry (var_map map)
561 tree_live_info_p live;
562 unsigned i;
563 basic_block bb;
564 bitmap saw_def;
565 tree phi, var, stmt;
566 tree op;
567 edge e;
568 int *stack;
569 block_stmt_iterator bsi;
570 ssa_op_iter iter;
571 bitmap_iterator bi;
572 #ifdef ENABLE_CHECKING
573 int num;
574 edge_iterator ei;
575 #endif
577 saw_def = BITMAP_ALLOC (NULL);
579 live = new_tree_live_info (map);
581 FOR_EACH_BB (bb)
583 bitmap_clear (saw_def);
585 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
587 for (i = 0; i < (unsigned)PHI_NUM_ARGS (phi); i++)
589 var = PHI_ARG_DEF (phi, i);
590 if (!phi_ssa_name_p (var))
591 continue;
592 stmt = SSA_NAME_DEF_STMT (var);
593 e = EDGE_PRED (bb, i);
595 /* Any uses in PHIs which either don't have def's or are not
596 defined in the block from which the def comes, will be live
597 on entry to that block. */
598 if (!stmt || e->src != bb_for_stmt (stmt))
599 add_livein_if_notdef (live, saw_def, var, e->src);
603 /* Don't mark PHI results as defined until all the PHI nodes have
604 been processed. If the PHI sequence is:
605 a_3 = PHI <a_1, a_2>
606 b_3 = PHI <b_1, a_3>
607 The a_3 referred to in b_3's PHI node is the one incoming on the
608 edge, *not* the PHI node just seen. */
610 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
612 var = PHI_RESULT (phi);
613 set_if_valid (map, saw_def, var);
616 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
618 stmt = bsi_stmt (bsi);
620 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
622 add_livein_if_notdef (live, saw_def, op, bb);
625 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
627 set_if_valid (map, saw_def, op);
632 stack = xmalloc (sizeof (int) * last_basic_block);
633 EXECUTE_IF_SET_IN_BITMAP (live->global, 0, i, bi)
635 live_worklist (live, stack, i);
637 free (stack);
639 #ifdef ENABLE_CHECKING
640 /* Check for live on entry partitions and report those with a DEF in
641 the program. This will typically mean an optimization has done
642 something wrong. */
644 bb = ENTRY_BLOCK_PTR;
645 num = 0;
646 FOR_EACH_EDGE (e, ei, bb->succs)
648 int entry_block = e->dest->index;
649 if (e->dest == EXIT_BLOCK_PTR)
650 continue;
651 for (i = 0; i < (unsigned)num_var_partitions (map); i++)
653 basic_block tmp;
654 tree d;
655 var = partition_to_var (map, i);
656 stmt = SSA_NAME_DEF_STMT (var);
657 tmp = bb_for_stmt (stmt);
658 d = default_def (SSA_NAME_VAR (var));
660 if (bitmap_bit_p (live_entry_blocks (live, i), entry_block))
662 if (!IS_EMPTY_STMT (stmt))
664 num++;
665 print_generic_expr (stderr, var, TDF_SLIM);
666 fprintf (stderr, " is defined ");
667 if (tmp)
668 fprintf (stderr, " in BB%d, ", tmp->index);
669 fprintf (stderr, "by:\n");
670 print_generic_expr (stderr, stmt, TDF_SLIM);
671 fprintf (stderr, "\nIt is also live-on-entry to entry BB %d",
672 entry_block);
673 fprintf (stderr, " So it appears to have multiple defs.\n");
675 else
677 if (d != var)
679 num++;
680 print_generic_expr (stderr, var, TDF_SLIM);
681 fprintf (stderr, " is live-on-entry to BB%d ",entry_block);
682 if (d)
684 fprintf (stderr, " but is not the default def of ");
685 print_generic_expr (stderr, d, TDF_SLIM);
686 fprintf (stderr, "\n");
688 else
689 fprintf (stderr, " and there is no default def.\n");
693 else
694 if (d == var)
696 /* The only way this var shouldn't be marked live on entry is
697 if it occurs in a PHI argument of the block. */
698 int z, ok = 0;
699 for (phi = phi_nodes (e->dest);
700 phi && !ok;
701 phi = PHI_CHAIN (phi))
703 for (z = 0; z < PHI_NUM_ARGS (phi); z++)
704 if (var == PHI_ARG_DEF (phi, z))
706 ok = 1;
707 break;
710 if (ok)
711 continue;
712 num++;
713 print_generic_expr (stderr, var, TDF_SLIM);
714 fprintf (stderr, " is not marked live-on-entry to entry BB%d ",
715 entry_block);
716 fprintf (stderr, "but it is a default def so it should be.\n");
720 gcc_assert (num <= 0);
721 #endif
723 BITMAP_FREE (saw_def);
725 return live;
729 /* Calculate the live on exit vectors based on the entry info in LIVEINFO. */
731 void
732 calculate_live_on_exit (tree_live_info_p liveinfo)
734 unsigned b;
735 unsigned i, x;
736 bitmap *on_exit;
737 basic_block bb;
738 edge e;
739 tree t, phi;
740 bitmap on_entry;
741 var_map map = liveinfo->map;
743 on_exit = (bitmap *)xmalloc (last_basic_block * sizeof (bitmap));
744 for (x = 0; x < (unsigned)last_basic_block; x++)
745 on_exit[x] = BITMAP_ALLOC (NULL);
747 /* Set all the live-on-exit bits for uses in PHIs. */
748 FOR_EACH_BB (bb)
750 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
751 for (i = 0; i < (unsigned)PHI_NUM_ARGS (phi); i++)
753 t = PHI_ARG_DEF (phi, i);
754 e = PHI_ARG_EDGE (phi, i);
755 if (!phi_ssa_name_p (t) || e->src == ENTRY_BLOCK_PTR)
756 continue;
757 set_if_valid (map, on_exit[e->src->index], t);
761 /* Set live on exit for all predecessors of live on entry's. */
762 for (i = 0; i < num_var_partitions (map); i++)
764 bitmap_iterator bi;
766 on_entry = live_entry_blocks (liveinfo, i);
767 EXECUTE_IF_SET_IN_BITMAP (on_entry, 0, b, bi)
769 edge_iterator ei;
770 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (b)->preds)
771 if (e->src != ENTRY_BLOCK_PTR)
772 bitmap_set_bit (on_exit[e->src->index], i);
776 liveinfo->liveout = on_exit;
780 /* Initialize a tree_partition_associator object using MAP. */
782 static tpa_p
783 tpa_init (var_map map)
785 tpa_p tpa;
786 int num_partitions = num_var_partitions (map);
787 int x;
789 if (num_partitions == 0)
790 return NULL;
792 tpa = (tpa_p) xmalloc (sizeof (struct tree_partition_associator_d));
793 tpa->num_trees = 0;
794 tpa->uncompressed_num = -1;
795 tpa->map = map;
796 tpa->next_partition = (int *)xmalloc (num_partitions * sizeof (int));
797 memset (tpa->next_partition, TPA_NONE, num_partitions * sizeof (int));
799 tpa->partition_to_tree_map = (int *)xmalloc (num_partitions * sizeof (int));
800 memset (tpa->partition_to_tree_map, TPA_NONE, num_partitions * sizeof (int));
802 x = MAX (40, (num_partitions / 20));
803 tpa->trees = VEC_alloc (tree, heap, x);
804 VARRAY_INT_INIT (tpa->first_partition, x, "first_partition");
806 return tpa;
811 /* Remove PARTITION_INDEX from TREE_INDEX's list in the tpa structure TPA. */
813 void
814 tpa_remove_partition (tpa_p tpa, int tree_index, int partition_index)
816 int i;
818 i = tpa_first_partition (tpa, tree_index);
819 if (i == partition_index)
821 VARRAY_INT (tpa->first_partition, tree_index) = tpa->next_partition[i];
823 else
825 for ( ; i != TPA_NONE; i = tpa_next_partition (tpa, i))
827 if (tpa->next_partition[i] == partition_index)
829 tpa->next_partition[i] = tpa->next_partition[partition_index];
830 break;
837 /* Free the memory used by tree_partition_associator object TPA. */
839 void
840 tpa_delete (tpa_p tpa)
842 if (!tpa)
843 return;
845 VEC_free (tree, heap, tpa->trees);
846 free (tpa->partition_to_tree_map);
847 free (tpa->next_partition);
848 free (tpa);
852 /* This function will remove any tree entries from TPA which have only a single
853 element. This will help keep the size of the conflict graph down. The
854 function returns the number of remaining tree lists. */
856 int
857 tpa_compact (tpa_p tpa)
859 int last, x, y, first, swap_i;
860 tree swap_t;
862 /* Find the last list which has more than 1 partition. */
863 for (last = tpa->num_trees - 1; last > 0; last--)
865 first = tpa_first_partition (tpa, last);
866 if (tpa_next_partition (tpa, first) != NO_PARTITION)
867 break;
870 x = 0;
871 while (x < last)
873 first = tpa_first_partition (tpa, x);
875 /* If there is not more than one partition, swap with the current end
876 of the tree list. */
877 if (tpa_next_partition (tpa, first) == NO_PARTITION)
879 swap_t = VEC_index (tree, tpa->trees, last);
880 swap_i = VARRAY_INT (tpa->first_partition, last);
882 /* Update the last entry. Since it is known to only have one
883 partition, there is nothing else to update. */
884 VEC_replace (tree, tpa->trees, last,
885 VEC_index (tree, tpa->trees, x));
886 VARRAY_INT (tpa->first_partition, last)
887 = VARRAY_INT (tpa->first_partition, x);
888 tpa->partition_to_tree_map[tpa_first_partition (tpa, last)] = last;
890 /* Since this list is known to have more than one partition, update
891 the list owner entries. */
892 VEC_replace (tree, tpa->trees, x, swap_t);
893 VARRAY_INT (tpa->first_partition, x) = swap_i;
894 for (y = tpa_first_partition (tpa, x);
895 y != NO_PARTITION;
896 y = tpa_next_partition (tpa, y))
897 tpa->partition_to_tree_map[y] = x;
899 /* Ensure last is a list with more than one partition. */
900 last--;
901 for (; last > x; last--)
903 first = tpa_first_partition (tpa, last);
904 if (tpa_next_partition (tpa, first) != NO_PARTITION)
905 break;
908 x++;
911 first = tpa_first_partition (tpa, x);
912 if (tpa_next_partition (tpa, first) != NO_PARTITION)
913 x++;
914 tpa->uncompressed_num = tpa->num_trees;
915 tpa->num_trees = x;
916 return last;
920 /* Initialize a root_var object with SSA partitions from MAP which are based
921 on each root variable. */
923 root_var_p
924 root_var_init (var_map map)
926 root_var_p rv;
927 int num_partitions = num_var_partitions (map);
928 int x, p;
929 tree t;
930 var_ann_t ann;
931 sbitmap seen;
933 rv = tpa_init (map);
934 if (!rv)
935 return NULL;
937 seen = sbitmap_alloc (num_partitions);
938 sbitmap_zero (seen);
940 /* Start at the end and work towards the front. This will provide a list
941 that is ordered from smallest to largest. */
942 for (x = num_partitions - 1; x >= 0; x--)
944 t = partition_to_var (map, x);
946 /* The var map may not be compacted yet, so check for NULL. */
947 if (!t)
948 continue;
950 p = var_to_partition (map, t);
952 gcc_assert (p != NO_PARTITION);
954 /* Make sure we only put coalesced partitions into the list once. */
955 if (TEST_BIT (seen, p))
956 continue;
957 SET_BIT (seen, p);
958 if (TREE_CODE (t) == SSA_NAME)
959 t = SSA_NAME_VAR (t);
960 ann = var_ann (t);
961 if (ann->root_var_processed)
963 rv->next_partition[p] = VARRAY_INT (rv->first_partition,
964 VAR_ANN_ROOT_INDEX (ann));
965 VARRAY_INT (rv->first_partition, VAR_ANN_ROOT_INDEX (ann)) = p;
967 else
969 ann->root_var_processed = 1;
970 VAR_ANN_ROOT_INDEX (ann) = rv->num_trees++;
971 VEC_safe_push (tree, heap, rv->trees, t);
972 VARRAY_PUSH_INT (rv->first_partition, p);
974 rv->partition_to_tree_map[p] = VAR_ANN_ROOT_INDEX (ann);
977 /* Reset the out_of_ssa_tag flag on each variable for later use. */
978 for (x = 0; x < rv->num_trees; x++)
980 t = VEC_index (tree, rv->trees, x);
981 var_ann (t)->root_var_processed = 0;
984 sbitmap_free (seen);
985 return rv;
989 /* Initialize a type_var structure which associates all the partitions in MAP
990 of the same type to the type node's index. Volatiles are ignored. */
992 type_var_p
993 type_var_init (var_map map)
995 type_var_p tv;
996 int x, y, p;
997 int num_partitions = num_var_partitions (map);
998 tree t;
999 sbitmap seen;
1001 seen = sbitmap_alloc (num_partitions);
1002 sbitmap_zero (seen);
1004 tv = tpa_init (map);
1005 if (!tv)
1006 return NULL;
1008 for (x = num_partitions - 1; x >= 0; x--)
1010 t = partition_to_var (map, x);
1012 /* Disallow coalescing of these types of variables. */
1013 if (!t
1014 || TREE_THIS_VOLATILE (t)
1015 || TREE_CODE (t) == RESULT_DECL
1016 || TREE_CODE (t) == PARM_DECL
1017 || (DECL_P (t)
1018 && (DECL_REGISTER (t)
1019 || !DECL_IGNORED_P (t)
1020 || DECL_RTL_SET_P (t))))
1021 continue;
1023 p = var_to_partition (map, t);
1025 gcc_assert (p != NO_PARTITION);
1027 /* If partitions have been coalesced, only add the representative
1028 for the partition to the list once. */
1029 if (TEST_BIT (seen, p))
1030 continue;
1031 SET_BIT (seen, p);
1032 t = TREE_TYPE (t);
1034 /* Find the list for this type. */
1035 for (y = 0; y < tv->num_trees; y++)
1036 if (t == VEC_index (tree, tv->trees, y))
1037 break;
1038 if (y == tv->num_trees)
1040 tv->num_trees++;
1041 VEC_safe_push (tree, heap, tv->trees, t);
1042 VARRAY_PUSH_INT (tv->first_partition, p);
1044 else
1046 tv->next_partition[p] = VARRAY_INT (tv->first_partition, y);
1047 VARRAY_INT (tv->first_partition, y) = p;
1049 tv->partition_to_tree_map[p] = y;
1051 sbitmap_free (seen);
1052 return tv;
1056 /* Create a new coalesce list object from MAP and return it. */
1058 coalesce_list_p
1059 create_coalesce_list (var_map map)
1061 coalesce_list_p list;
1063 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
1065 list->map = map;
1066 list->add_mode = true;
1067 list->list = (partition_pair_p *) xcalloc (num_var_partitions (map),
1068 sizeof (struct partition_pair_d));
1069 return list;
1073 /* Delete coalesce list CL. */
1075 void
1076 delete_coalesce_list (coalesce_list_p cl)
1078 free (cl->list);
1079 free (cl);
1083 /* Find a matching coalesce pair object in CL for partitions P1 and P2. If
1084 one isn't found, return NULL if CREATE is false, otherwise create a new
1085 coalesce pair object and return it. */
1087 static partition_pair_p
1088 find_partition_pair (coalesce_list_p cl, int p1, int p2, bool create)
1090 partition_pair_p node, tmp;
1091 int s;
1093 /* Normalize so that p1 is the smaller value. */
1094 if (p2 < p1)
1096 s = p1;
1097 p1 = p2;
1098 p2 = s;
1101 tmp = NULL;
1103 /* The list is sorted such that if we find a value greater than p2,
1104 p2 is not in the list. */
1105 for (node = cl->list[p1]; node; node = node->next)
1107 if (node->second_partition == p2)
1108 return node;
1109 else
1110 if (node->second_partition > p2)
1111 break;
1112 tmp = node;
1115 if (!create)
1116 return NULL;
1118 node = (partition_pair_p) xmalloc (sizeof (struct partition_pair_d));
1119 node->first_partition = p1;
1120 node->second_partition = p2;
1121 node->cost = 0;
1123 if (tmp != NULL)
1125 node->next = tmp->next;
1126 tmp->next = node;
1128 else
1130 /* This is now the first node in the list. */
1131 node->next = cl->list[p1];
1132 cl->list[p1] = node;
1135 return node;
1139 /* Add a potential coalesce between P1 and P2 in CL with a cost of VALUE. */
1141 void
1142 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
1144 partition_pair_p node;
1146 gcc_assert (cl->add_mode);
1148 if (p1 == p2)
1149 return;
1151 node = find_partition_pair (cl, p1, p2, true);
1153 node->cost += value;
1157 /* Comparison function to allow qsort to sort P1 and P2 in descending order. */
1159 static
1160 int compare_pairs (const void *p1, const void *p2)
1162 return (*(partition_pair_p *)p2)->cost - (*(partition_pair_p *)p1)->cost;
1166 /* Prepare CL for removal of preferred pairs. When finished, list element
1167 0 has all the coalesce pairs, sorted in order from most important coalesce
1168 to least important. */
1170 void
1171 sort_coalesce_list (coalesce_list_p cl)
1173 unsigned x, num, count;
1174 partition_pair_p chain, p;
1175 partition_pair_p *list;
1177 gcc_assert (cl->add_mode);
1179 cl->add_mode = false;
1181 /* Compact the array of lists to a single list, and count the elements. */
1182 num = 0;
1183 chain = NULL;
1184 for (x = 0; x < num_var_partitions (cl->map); x++)
1185 if (cl->list[x] != NULL)
1187 for (p = cl->list[x]; p->next != NULL; p = p->next)
1188 num++;
1189 num++;
1190 p->next = chain;
1191 chain = cl->list[x];
1192 cl->list[x] = NULL;
1195 /* Only call qsort if there are more than 2 items. */
1196 if (num > 2)
1198 list = xmalloc (sizeof (partition_pair_p) * num);
1199 count = 0;
1200 for (p = chain; p != NULL; p = p->next)
1201 list[count++] = p;
1203 gcc_assert (count == num);
1205 qsort (list, count, sizeof (partition_pair_p), compare_pairs);
1207 p = list[0];
1208 for (x = 1; x < num; x++)
1210 p->next = list[x];
1211 p = list[x];
1213 p->next = NULL;
1214 cl->list[0] = list[0];
1215 free (list);
1217 else
1219 cl->list[0] = chain;
1220 if (num == 2)
1222 /* Simply swap the two elements if they are in the wrong order. */
1223 if (chain->cost < chain->next->cost)
1225 cl->list[0] = chain->next;
1226 cl->list[0]->next = chain;
1227 chain->next = NULL;
1234 /* Retrieve the best remaining pair to coalesce from CL. Returns the 2
1235 partitions via P1 and P2. Their calculated cost is returned by the function.
1236 NO_BEST_COALESCE is returned if the coalesce list is empty. */
1238 static int
1239 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
1241 partition_pair_p node;
1242 int ret;
1244 gcc_assert (!cl->add_mode);
1246 node = cl->list[0];
1247 if (!node)
1248 return NO_BEST_COALESCE;
1250 cl->list[0] = node->next;
1252 *p1 = node->first_partition;
1253 *p2 = node->second_partition;
1254 ret = node->cost;
1255 free (node);
1257 return ret;
1261 /* If variable VAR is in a partition in MAP, add a conflict in GRAPH between
1262 VAR and any other live partitions in VEC which are associated via TPA.
1263 Reset the live bit in VEC. */
1265 static inline void
1266 add_conflicts_if_valid (tpa_p tpa, conflict_graph graph,
1267 var_map map, bitmap vec, tree var)
1269 int p, y, first;
1270 p = var_to_partition (map, var);
1271 if (p != NO_PARTITION)
1273 bitmap_clear_bit (vec, p);
1274 first = tpa_find_tree (tpa, p);
1275 /* If find returns nothing, this object isn't interesting. */
1276 if (first == TPA_NONE)
1277 return;
1278 /* Only add interferences between objects in the same list. */
1279 for (y = tpa_first_partition (tpa, first);
1280 y != TPA_NONE;
1281 y = tpa_next_partition (tpa, y))
1283 if (bitmap_bit_p (vec, y))
1284 conflict_graph_add (graph, p, y);
1289 DEF_VEC_I(int);
1290 DEF_VEC_ALLOC_I(int,heap);
1292 /* Return a conflict graph for the information contained in LIVE_INFO. Only
1293 conflicts between items in the same TPA list are added. If optional
1294 coalesce list CL is passed in, any copies encountered are added. */
1296 conflict_graph
1297 build_tree_conflict_graph (tree_live_info_p liveinfo, tpa_p tpa,
1298 coalesce_list_p cl)
1300 conflict_graph graph;
1301 var_map map;
1302 bitmap live;
1303 unsigned x, y, i;
1304 basic_block bb;
1305 int *partition_link, *tpa_nodes;
1306 VEC(int,heap) *tpa_to_clear;
1307 unsigned l;
1308 ssa_op_iter iter;
1309 bitmap_iterator bi;
1311 map = live_var_map (liveinfo);
1312 graph = conflict_graph_new (num_var_partitions (map));
1314 if (tpa_num_trees (tpa) == 0)
1315 return graph;
1317 live = BITMAP_ALLOC (NULL);
1319 partition_link = xcalloc (num_var_partitions (map) + 1, sizeof (int));
1320 tpa_nodes = xcalloc (tpa_num_trees (tpa), sizeof (int));
1321 tpa_to_clear = VEC_alloc (int, heap, 50);
1323 FOR_EACH_BB (bb)
1325 block_stmt_iterator bsi;
1326 tree phi;
1327 int idx;
1329 /* Start with live on exit temporaries. */
1330 bitmap_copy (live, live_on_exit (liveinfo, bb));
1332 for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_prev (&bsi))
1334 bool is_a_copy = false;
1335 tree stmt = bsi_stmt (bsi);
1337 /* A copy between 2 partitions does not introduce an interference
1338 by itself. If they did, you would never be able to coalesce
1339 two things which are copied. If the two variables really do
1340 conflict, they will conflict elsewhere in the program.
1342 This is handled specially here since we may also be interested
1343 in copies between real variables and SSA_NAME variables. We may
1344 be interested in trying to coalesce SSA_NAME variables with
1345 root variables in some cases. */
1347 if (TREE_CODE (stmt) == MODIFY_EXPR)
1349 tree lhs = TREE_OPERAND (stmt, 0);
1350 tree rhs = TREE_OPERAND (stmt, 1);
1351 int p1, p2;
1352 int bit;
1354 if (DECL_P (lhs) || TREE_CODE (lhs) == SSA_NAME)
1355 p1 = var_to_partition (map, lhs);
1356 else
1357 p1 = NO_PARTITION;
1359 if (DECL_P (rhs) || TREE_CODE (rhs) == SSA_NAME)
1360 p2 = var_to_partition (map, rhs);
1361 else
1362 p2 = NO_PARTITION;
1364 if (p1 != NO_PARTITION && p2 != NO_PARTITION)
1366 is_a_copy = true;
1367 bit = bitmap_bit_p (live, p2);
1368 /* If the RHS is live, make it not live while we add
1369 the conflicts, then make it live again. */
1370 if (bit)
1371 bitmap_clear_bit (live, p2);
1372 add_conflicts_if_valid (tpa, graph, map, live, lhs);
1373 if (bit)
1374 bitmap_set_bit (live, p2);
1375 if (cl)
1376 add_coalesce (cl, p1, p2, 1);
1377 set_if_valid (map, live, rhs);
1381 if (!is_a_copy)
1383 tree var;
1384 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
1386 add_conflicts_if_valid (tpa, graph, map, live, var);
1389 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
1391 set_if_valid (map, live, var);
1396 /* If result of a PHI is unused, then the loops over the statements
1397 will not record any conflicts. However, since the PHI node is
1398 going to be translated out of SSA form we must record a conflict
1399 between the result of the PHI and any variables with are live.
1400 Otherwise the out-of-ssa translation may create incorrect code. */
1401 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1403 tree result = PHI_RESULT (phi);
1404 int p = var_to_partition (map, result);
1406 if (p != NO_PARTITION && ! bitmap_bit_p (live, p))
1407 add_conflicts_if_valid (tpa, graph, map, live, result);
1410 /* Anything which is still live at this point interferes.
1411 In order to implement this efficiently, only conflicts between
1412 partitions which have the same TPA root need be added.
1413 TPA roots which have been seen are tracked in 'tpa_nodes'. A nonzero
1414 entry points to an index into 'partition_link', which then indexes
1415 into itself forming a linked list of partitions sharing a tpa root
1416 which have been seen as live up to this point. Since partitions start
1417 at index zero, all entries in partition_link are (partition + 1).
1419 Conflicts are added between the current partition and any already seen.
1420 tpa_clear contains all the tpa_roots processed, and these are the only
1421 entries which need to be zero'd out for a clean restart. */
1423 EXECUTE_IF_SET_IN_BITMAP (live, 0, x, bi)
1425 i = tpa_find_tree (tpa, x);
1426 if (i != (unsigned)TPA_NONE)
1428 int start = tpa_nodes[i];
1429 /* If start is 0, a new root reference list is being started.
1430 Register it to be cleared. */
1431 if (!start)
1432 VEC_safe_push (int, heap, tpa_to_clear, i);
1434 /* Add interferences to other tpa members seen. */
1435 for (y = start; y != 0; y = partition_link[y])
1436 conflict_graph_add (graph, x, y - 1);
1437 tpa_nodes[i] = x + 1;
1438 partition_link[x + 1] = start;
1442 /* Now clear the used tpa root references. */
1443 for (l = 0; VEC_iterate (int, tpa_to_clear, l, idx); l++)
1444 tpa_nodes[idx] = 0;
1445 VEC_truncate (int, tpa_to_clear, 0);
1448 free (tpa_nodes);
1449 free (partition_link);
1450 VEC_free (int, heap, tpa_to_clear);
1451 BITMAP_FREE (live);
1452 return graph;
1456 /* This routine will attempt to coalesce the elements in TPA subject to the
1457 conflicts found in GRAPH. If optional coalesce_list CL is provided,
1458 only coalesces specified within the coalesce list are attempted. Otherwise
1459 an attempt is made to coalesce as many partitions within each TPA grouping
1460 as possible. If DEBUG is provided, debug output will be sent there. */
1462 void
1463 coalesce_tpa_members (tpa_p tpa, conflict_graph graph, var_map map,
1464 coalesce_list_p cl, FILE *debug)
1466 int x, y, z, w;
1467 tree var, tmp;
1469 /* Attempt to coalesce any items in a coalesce list. */
1470 if (cl)
1472 while (pop_best_coalesce (cl, &x, &y) != NO_BEST_COALESCE)
1474 if (debug)
1476 fprintf (debug, "Coalesce list: (%d)", x);
1477 print_generic_expr (debug, partition_to_var (map, x), TDF_SLIM);
1478 fprintf (debug, " & (%d)", y);
1479 print_generic_expr (debug, partition_to_var (map, y), TDF_SLIM);
1482 w = tpa_find_tree (tpa, x);
1483 z = tpa_find_tree (tpa, y);
1484 if (w != z || w == TPA_NONE || z == TPA_NONE)
1486 if (debug)
1488 if (w != z)
1489 fprintf (debug, ": Fail, Non-matching TPA's\n");
1490 if (w == TPA_NONE)
1491 fprintf (debug, ": Fail %d non TPA.\n", x);
1492 else
1493 fprintf (debug, ": Fail %d non TPA.\n", y);
1495 continue;
1497 var = partition_to_var (map, x);
1498 tmp = partition_to_var (map, y);
1499 x = var_to_partition (map, var);
1500 y = var_to_partition (map, tmp);
1501 if (debug)
1502 fprintf (debug, " [map: %d, %d] ", x, y);
1503 if (x == y)
1505 if (debug)
1506 fprintf (debug, ": Already Coalesced.\n");
1507 continue;
1509 if (!conflict_graph_conflict_p (graph, x, y))
1511 z = var_union (map, var, tmp);
1512 if (z == NO_PARTITION)
1514 if (debug)
1515 fprintf (debug, ": Unable to perform partition union.\n");
1516 continue;
1519 /* z is the new combined partition. We need to remove the other
1520 partition from the list. Set x to be that other partition. */
1521 if (z == x)
1523 conflict_graph_merge_regs (graph, x, y);
1524 w = tpa_find_tree (tpa, y);
1525 tpa_remove_partition (tpa, w, y);
1527 else
1529 conflict_graph_merge_regs (graph, y, x);
1530 w = tpa_find_tree (tpa, x);
1531 tpa_remove_partition (tpa, w, x);
1534 if (debug)
1535 fprintf (debug, ": Success -> %d\n", z);
1537 else
1538 if (debug)
1539 fprintf (debug, ": Fail due to conflict\n");
1541 /* If using a coalesce list, don't try to coalesce anything else. */
1542 return;
1545 for (x = 0; x < tpa_num_trees (tpa); x++)
1547 while (tpa_first_partition (tpa, x) != TPA_NONE)
1549 int p1, p2;
1550 /* Coalesce first partition with anything that doesn't conflict. */
1551 y = tpa_first_partition (tpa, x);
1552 tpa_remove_partition (tpa, x, y);
1554 var = partition_to_var (map, y);
1555 /* p1 is the partition representative to which y belongs. */
1556 p1 = var_to_partition (map, var);
1558 for (z = tpa_next_partition (tpa, y);
1559 z != TPA_NONE;
1560 z = tpa_next_partition (tpa, z))
1562 tmp = partition_to_var (map, z);
1563 /* p2 is the partition representative to which z belongs. */
1564 p2 = var_to_partition (map, tmp);
1565 if (debug)
1567 fprintf (debug, "Coalesce : ");
1568 print_generic_expr (debug, var, TDF_SLIM);
1569 fprintf (debug, " &");
1570 print_generic_expr (debug, tmp, TDF_SLIM);
1571 fprintf (debug, " (%d ,%d)", p1, p2);
1574 /* If partitions are already merged, don't check for conflict. */
1575 if (tmp == var)
1577 tpa_remove_partition (tpa, x, z);
1578 if (debug)
1579 fprintf (debug, ": Already coalesced\n");
1581 else
1582 if (!conflict_graph_conflict_p (graph, p1, p2))
1584 int v;
1585 if (tpa_find_tree (tpa, y) == TPA_NONE
1586 || tpa_find_tree (tpa, z) == TPA_NONE)
1588 if (debug)
1589 fprintf (debug, ": Fail non-TPA member\n");
1590 continue;
1592 if ((v = var_union (map, var, tmp)) == NO_PARTITION)
1594 if (debug)
1595 fprintf (debug, ": Fail cannot combine partitions\n");
1596 continue;
1599 tpa_remove_partition (tpa, x, z);
1600 if (v == p1)
1601 conflict_graph_merge_regs (graph, v, z);
1602 else
1604 /* Update the first partition's representative. */
1605 conflict_graph_merge_regs (graph, v, y);
1606 p1 = v;
1609 /* The root variable of the partition may be changed
1610 now. */
1611 var = partition_to_var (map, p1);
1613 if (debug)
1614 fprintf (debug, ": Success -> %d\n", v);
1616 else
1617 if (debug)
1618 fprintf (debug, ": Fail, Conflict\n");
1625 /* Send debug info for coalesce list CL to file F. */
1627 void
1628 dump_coalesce_list (FILE *f, coalesce_list_p cl)
1630 partition_pair_p node;
1631 int x, num;
1632 tree var;
1634 if (cl->add_mode)
1636 fprintf (f, "Coalesce List:\n");
1637 num = num_var_partitions (cl->map);
1638 for (x = 0; x < num; x++)
1640 node = cl->list[x];
1641 if (node)
1643 fprintf (f, "[");
1644 print_generic_expr (f, partition_to_var (cl->map, x), TDF_SLIM);
1645 fprintf (f, "] - ");
1646 for ( ; node; node = node->next)
1648 var = partition_to_var (cl->map, node->second_partition);
1649 print_generic_expr (f, var, TDF_SLIM);
1650 fprintf (f, "(%1d), ", node->cost);
1652 fprintf (f, "\n");
1656 else
1658 fprintf (f, "Sorted Coalesce list:\n");
1659 for (node = cl->list[0]; node; node = node->next)
1661 fprintf (f, "(%d) ", node->cost);
1662 var = partition_to_var (cl->map, node->first_partition);
1663 print_generic_expr (f, var, TDF_SLIM);
1664 fprintf (f, " : ");
1665 var = partition_to_var (cl->map, node->second_partition);
1666 print_generic_expr (f, var, TDF_SLIM);
1667 fprintf (f, "\n");
1673 /* Output tree_partition_associator object TPA to file F.. */
1675 void
1676 tpa_dump (FILE *f, tpa_p tpa)
1678 int x, i;
1680 if (!tpa)
1681 return;
1683 for (x = 0; x < tpa_num_trees (tpa); x++)
1685 print_generic_expr (f, tpa_tree (tpa, x), TDF_SLIM);
1686 fprintf (f, " : (");
1687 for (i = tpa_first_partition (tpa, x);
1688 i != TPA_NONE;
1689 i = tpa_next_partition (tpa, i))
1691 fprintf (f, "(%d)",i);
1692 print_generic_expr (f, partition_to_var (tpa->map, i), TDF_SLIM);
1693 fprintf (f, " ");
1695 #ifdef ENABLE_CHECKING
1696 if (tpa_find_tree (tpa, i) != x)
1697 fprintf (f, "**find tree incorrectly set** ");
1698 #endif
1701 fprintf (f, ")\n");
1703 fflush (f);
1707 /* Output partition map MAP to file F. */
1709 void
1710 dump_var_map (FILE *f, var_map map)
1712 int t;
1713 unsigned x, y;
1714 int p;
1716 fprintf (f, "\nPartition map \n\n");
1718 for (x = 0; x < map->num_partitions; x++)
1720 if (map->compact_to_partition != NULL)
1721 p = map->compact_to_partition[x];
1722 else
1723 p = x;
1725 if (map->partition_to_var[p] == NULL_TREE)
1726 continue;
1728 t = 0;
1729 for (y = 1; y < num_ssa_names; y++)
1731 p = partition_find (map->var_partition, y);
1732 if (map->partition_to_compact)
1733 p = map->partition_to_compact[p];
1734 if (p == (int)x)
1736 if (t++ == 0)
1738 fprintf(f, "Partition %d (", x);
1739 print_generic_expr (f, partition_to_var (map, p), TDF_SLIM);
1740 fprintf (f, " - ");
1742 fprintf (f, "%d ", y);
1745 if (t != 0)
1746 fprintf (f, ")\n");
1748 fprintf (f, "\n");
1752 /* Output live range info LIVE to file F, controlled by FLAG. */
1754 void
1755 dump_live_info (FILE *f, tree_live_info_p live, int flag)
1757 basic_block bb;
1758 unsigned i;
1759 var_map map = live->map;
1760 bitmap_iterator bi;
1762 if ((flag & LIVEDUMP_ENTRY) && live->livein)
1764 FOR_EACH_BB (bb)
1766 fprintf (f, "\nLive on entry to BB%d : ", bb->index);
1767 for (i = 0; i < num_var_partitions (map); i++)
1769 if (bitmap_bit_p (live_entry_blocks (live, i), bb->index))
1771 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1772 fprintf (f, " ");
1775 fprintf (f, "\n");
1779 if ((flag & LIVEDUMP_EXIT) && live->liveout)
1781 FOR_EACH_BB (bb)
1783 fprintf (f, "\nLive on exit from BB%d : ", bb->index);
1784 EXECUTE_IF_SET_IN_BITMAP (live->liveout[bb->index], 0, i, bi)
1786 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1787 fprintf (f, " ");
1789 fprintf (f, "\n");
1794 #ifdef ENABLE_CHECKING
1795 void
1796 register_ssa_partition_check (tree ssa_var)
1798 gcc_assert (TREE_CODE (ssa_var) == SSA_NAME);
1799 if (!is_gimple_reg (SSA_NAME_VAR (ssa_var)))
1801 fprintf (stderr, "Illegally registering a virtual SSA name :");
1802 print_generic_expr (stderr, ssa_var, TDF_SLIM);
1803 fprintf (stderr, " in the SSA->Normal phase.\n");
1804 internal_error ("SSA corruption");
1807 #endif