Current state of work.
[official-gcc.git] / gcc / tree-ssa-live.c
blob530d41135649cfbe9934561bf18ea85ea34872ca
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)
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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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 "tree-alias-common.h"
38 #include "hashtab.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,
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 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;
141 root_var = var2;
143 else
144 other_var = var2;
147 if (p1 == NO_PARTITION || p2 == NO_PARTITION)
148 abort ();
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 x, limit, count, tmp, root, root_i;
189 tree var;
190 root_var_p rv = NULL;
192 limit = map->partition_size;
193 used = sbitmap_alloc (limit);
194 sbitmap_zero (used);
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. */
217 count = 0;
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. */
225 if (rv)
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)
231 continue;
233 SET_BIT (used, tmp);
234 count++;
238 /* Build a compacted partitioning. */
239 if (count != limit)
241 map->compact_to_partition = (int *)xmalloc (count * sizeof (int));
242 count = 0;
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);
251 count++;
254 else
256 free (map->partition_to_compact);
257 map->partition_to_compact = NULL;
260 map->num_partitions = count;
262 if (rv)
263 root_var_delete (rv);
264 sbitmap_free (used);
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. */
272 void
273 change_partition_var (var_map map, tree var, int part)
275 var_ann_t ann;
277 if (TREE_CODE (var) == SSA_NAME)
278 abort();
280 ann = var_ann (var);
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. */
292 var_map
293 create_ssa_var_map (int flags)
295 block_stmt_iterator bsi;
296 basic_block bb;
297 tree dest, use;
298 tree stmt;
299 stmt_ann_t ann;
300 vuse_optype vuses;
301 v_may_def_optype v_may_defs;
302 v_must_def_optype v_must_defs;
303 use_optype uses;
304 def_optype defs;
305 unsigned x;
306 var_map map;
307 #if defined ENABLE_CHECKING
308 sbitmap used_in_real_ops;
309 sbitmap used_in_virtual_ops;
310 #endif
312 map = init_var_map (num_ssa_names + 1);
314 #if defined ENABLE_CHECKING
315 used_in_real_ops = sbitmap_alloc (num_referenced_vars);
316 sbitmap_zero (used_in_real_ops);
318 used_in_virtual_ops = sbitmap_alloc (num_referenced_vars);
319 sbitmap_zero (used_in_virtual_ops);
320 #endif
322 if (flags & SSA_VAR_MAP_REF_COUNT)
324 map->ref_count
325 = (int *)xmalloc (((num_ssa_names + 1) * sizeof (int)));
326 memset (map->ref_count, 0, (num_ssa_names + 1) * sizeof (int));
329 FOR_EACH_BB (bb)
331 tree phi, arg;
332 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
334 int i;
335 register_ssa_partition (map, PHI_RESULT (phi), false);
336 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
338 arg = PHI_ARG_DEF (phi, i);
339 if (TREE_CODE (arg) == SSA_NAME)
340 register_ssa_partition (map, arg, true);
344 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
346 stmt = bsi_stmt (bsi);
347 get_stmt_operands (stmt);
348 ann = stmt_ann (stmt);
350 /* Register USE and DEF operands in each statement. */
351 uses = USE_OPS (ann);
352 for (x = 0; x < NUM_USES (uses); x++)
354 use = USE_OP (uses, x);
355 register_ssa_partition (map, use, true);
357 #if defined ENABLE_CHECKING
358 SET_BIT (used_in_real_ops, var_ann (SSA_NAME_VAR (use))->uid);
359 #endif
362 defs = DEF_OPS (ann);
363 for (x = 0; x < NUM_DEFS (defs); x++)
365 dest = DEF_OP (defs, x);
366 register_ssa_partition (map, dest, false);
368 #if defined ENABLE_CHECKING
369 SET_BIT (used_in_real_ops, var_ann (SSA_NAME_VAR (dest))->uid);
370 #endif
373 /* While we do not care about virtual operands for
374 out of SSA, we do need to look at them to make sure
375 we mark all the variables which are used. */
376 vuses = VUSE_OPS (ann);
377 for (x = 0; x < NUM_VUSES (vuses); x++)
379 tree var = VUSE_OP (vuses, x);
380 set_is_used (var);
382 #if defined ENABLE_CHECKING
383 SET_BIT (used_in_virtual_ops, var_ann (SSA_NAME_VAR (var))->uid);
384 #endif
387 v_may_defs = V_MAY_DEF_OPS (ann);
388 for (x = 0; x < NUM_V_MAY_DEFS (v_may_defs); x++)
390 tree var = V_MAY_DEF_OP (v_may_defs, x);
391 set_is_used (var);
393 #if defined ENABLE_CHECKING
394 SET_BIT (used_in_virtual_ops, var_ann (SSA_NAME_VAR (var))->uid);
395 #endif
398 v_must_defs = V_MUST_DEF_OPS (ann);
399 for (x = 0; x < NUM_V_MUST_DEFS (v_must_defs); x++)
401 tree var = V_MUST_DEF_OP (v_must_defs, x);
402 set_is_used (var);
403 #if defined ENABLE_CHECKING
404 SET_BIT (used_in_virtual_ops, var_ann (SSA_NAME_VAR (var))->uid);
405 #endif
410 #if defined ENABLE_CHECKING
412 unsigned i;
413 sbitmap both = sbitmap_alloc (num_referenced_vars);
414 sbitmap_a_and_b (both, used_in_real_ops, used_in_virtual_ops);
415 if (sbitmap_first_set_bit (both) >= 0)
417 EXECUTE_IF_SET_IN_SBITMAP (both, 0, i,
418 fprintf (stderr, "Variable %s used in real and virtual operands\n",
419 get_name (referenced_var (i))));
420 abort ();
423 sbitmap_free (used_in_real_ops);
424 sbitmap_free (used_in_virtual_ops);
425 sbitmap_free (both);
427 #endif
429 return map;
433 /* Allocate and return a new live range information object base on MAP. */
435 static tree_live_info_p
436 new_tree_live_info (var_map map)
438 tree_live_info_p live;
439 int x;
441 live = (tree_live_info_p) xmalloc (sizeof (struct tree_live_info_d));
442 live->map = map;
443 live->num_blocks = last_basic_block;
445 live->global = BITMAP_XMALLOC ();
447 live->livein = (bitmap *)xmalloc (num_var_partitions (map) * sizeof (bitmap));
448 for (x = 0; x < num_var_partitions (map); x++)
449 live->livein[x] = BITMAP_XMALLOC ();
451 /* liveout is deferred until it is actually requested. */
452 live->liveout = NULL;
453 return live;
457 /* Free storage for live range info object LIVE. */
459 void
460 delete_tree_live_info (tree_live_info_p live)
462 int x;
463 if (live->liveout)
465 for (x = live->num_blocks - 1; x >= 0; x--)
466 BITMAP_XFREE (live->liveout[x]);
467 free (live->liveout);
469 if (live->livein)
471 for (x = num_var_partitions (live->map) - 1; x >= 0; x--)
472 BITMAP_XFREE (live->livein[x]);
473 free (live->livein);
475 if (live->global)
476 BITMAP_XFREE (live->global);
478 free (live);
482 /* Using LIVE, fill in all the live-on-entry blocks between the defs and uses
483 for partition I. STACK is a varray used for temporary memory which is
484 passed in rather than being allocated on every call. */
486 static void
487 live_worklist (tree_live_info_p live, varray_type stack, int i)
489 int b;
490 tree var;
491 basic_block def_bb = NULL;
492 edge e;
493 unsigned ix;
494 var_map map = live->map;
496 var = partition_to_var (map, i);
497 if (SSA_NAME_DEF_STMT (var))
498 def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (var));
500 EXECUTE_IF_SET_IN_BITMAP (live->livein[i], 0, b,
502 VARRAY_PUSH_INT (stack, b);
505 while (VARRAY_ACTIVE_SIZE (stack) > 0)
507 b = VARRAY_TOP_INT (stack);
508 VARRAY_POP (stack);
510 FOR_EACH_EDGE (e, BASIC_BLOCK (b)->pred, ix)
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 VARRAY_PUSH_INT (stack, 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 int num, i;
563 basic_block bb;
564 bitmap saw_def;
565 tree phi, var, stmt;
566 tree op;
567 edge e;
568 unsigned ix;
569 varray_type stack;
570 block_stmt_iterator bsi;
571 use_optype uses;
572 def_optype defs;
573 stmt_ann_t ann;
575 saw_def = BITMAP_XMALLOC ();
577 live = new_tree_live_info (map);
579 FOR_EACH_BB (bb)
581 bitmap_clear (saw_def);
583 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
585 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
587 var = PHI_ARG_DEF (phi, i);
588 if (!phi_ssa_name_p (var))
589 continue;
590 stmt = SSA_NAME_DEF_STMT (var);
591 e = PHI_ARG_EDGE (phi, i);
593 /* Any uses in PHIs which either don't have def's or are not
594 defined in the block from which the def comes, will be live
595 on entry to that block. */
596 if (!stmt || e->src != bb_for_stmt (stmt))
597 add_livein_if_notdef (live, saw_def, var, e->src);
601 /* Don't mark PHI results as defined until all the PHI nodes have
602 been processed. If the PHI sequence is:
603 a_3 = PHI <a_1, a_2>
604 b_3 = PHI <b_1, a_3>
605 The a_3 referred to in b_3's PHI node is the one incoming on the
606 edge, *not* the PHI node just seen. */
608 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
610 var = PHI_RESULT (phi);
611 set_if_valid (map, saw_def, var);
614 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
616 stmt = bsi_stmt (bsi);
617 get_stmt_operands (stmt);
618 ann = stmt_ann (stmt);
620 uses = USE_OPS (ann);
621 num = NUM_USES (uses);
622 for (i = 0; i < num; i++)
624 op = USE_OP (uses, i);
625 add_livein_if_notdef (live, saw_def, op, bb);
628 defs = DEF_OPS (ann);
629 num = NUM_DEFS (defs);
630 for (i = 0; i < num; i++)
632 op = DEF_OP (defs, i);
633 set_if_valid (map, saw_def, op);
638 VARRAY_INT_INIT (stack, last_basic_block, "stack");
639 EXECUTE_IF_SET_IN_BITMAP (live->global, 0, i,
641 live_worklist (live, stack, i);
644 #ifdef ENABLE_CHECKING
645 /* Check for live on entry partitions and report those with a DEF in
646 the program. This will typically mean an optimization has done
647 something wrong. */
649 bb = ENTRY_BLOCK_PTR;
650 num = 0;
651 FOR_EACH_EDGE (e, bb->succ, ix)
653 int entry_block = e->dest->index;
654 if (e->dest == EXIT_BLOCK_PTR)
655 continue;
656 for (i = 0; i < num_var_partitions (map); i++)
658 basic_block tmp;
659 tree d;
660 var = partition_to_var (map, i);
661 stmt = SSA_NAME_DEF_STMT (var);
662 tmp = bb_for_stmt (stmt);
663 d = default_def (SSA_NAME_VAR (var));
665 if (bitmap_bit_p (live_entry_blocks (live, i), entry_block))
667 if (!IS_EMPTY_STMT (stmt))
669 num++;
670 print_generic_expr (stderr, var, TDF_SLIM);
671 fprintf (stderr, " is defined ");
672 if (tmp)
673 fprintf (stderr, " in BB%d, ", tmp->index);
674 fprintf (stderr, "by:\n");
675 print_generic_expr (stderr, stmt, TDF_SLIM);
676 fprintf (stderr, "\nIt is also live-on-entry to entry BB %d",
677 entry_block);
678 fprintf (stderr, " So it appears to have multiple defs.\n");
680 else
682 if (d != var)
684 num++;
685 print_generic_expr (stderr, var, TDF_SLIM);
686 fprintf (stderr, " is live-on-entry to BB%d ",entry_block);
687 if (d)
689 fprintf (stderr, " but is not the default def of ");
690 print_generic_expr (stderr, d, TDF_SLIM);
691 fprintf (stderr, "\n");
693 else
694 fprintf (stderr, " and there is no default def.\n");
698 else
699 if (d == var)
701 /* The only way this var shouldn't be marked live on entry is
702 if it occurs in a PHI argument of the block. */
703 int z, ok = 0;
704 for (phi = phi_nodes (e->dest);
705 phi && !ok;
706 phi = PHI_CHAIN (phi))
708 for (z = 0; z < PHI_NUM_ARGS (phi); z++)
709 if (var == PHI_ARG_DEF (phi, z))
711 ok = 1;
712 break;
715 if (ok)
716 continue;
717 num++;
718 print_generic_expr (stderr, var, TDF_SLIM);
719 fprintf (stderr, " is not marked live-on-entry to entry BB%d ",
720 entry_block);
721 fprintf (stderr, "but it is a default def so it should be.\n");
725 if (num > 0)
726 abort ();
727 #endif
729 BITMAP_XFREE (saw_def);
731 return live;
735 /* Calculate the live on exit vectors based on the entry info in LIVEINFO. */
737 void
738 calculate_live_on_exit (tree_live_info_p liveinfo)
740 unsigned b;
741 int i, x;
742 bitmap *on_exit;
743 basic_block bb;
744 edge e;
745 unsigned ix;
746 tree t, phi;
747 bitmap on_entry;
748 var_map map = liveinfo->map;
750 on_exit = (bitmap *)xmalloc (last_basic_block * sizeof (bitmap));
751 for (x = 0; x < last_basic_block; x++)
752 on_exit[x] = BITMAP_XMALLOC ();
754 /* Set all the live-on-exit bits for uses in PHIs. */
755 FOR_EACH_BB (bb)
757 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
758 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
760 t = PHI_ARG_DEF (phi, i);
761 e = PHI_ARG_EDGE (phi, i);
762 if (!phi_ssa_name_p (t) || e->src == ENTRY_BLOCK_PTR)
763 continue;
764 set_if_valid (map, on_exit[e->src->index], t);
768 /* Set live on exit for all predecessors of live on entry's. */
769 for (i = 0; i < num_var_partitions (map); i++)
771 on_entry = live_entry_blocks (liveinfo, i);
772 EXECUTE_IF_SET_IN_BITMAP (on_entry, 0, b,
774 FOR_EACH_EDGE (e, BASIC_BLOCK (b)->pred, ix)
775 if (e->src != ENTRY_BLOCK_PTR)
776 bitmap_set_bit (on_exit[e->src->index], i);
780 liveinfo->liveout = on_exit;
784 /* Initialize a tree_partition_associator object using MAP. */
786 tpa_p
787 tpa_init (var_map map)
789 tpa_p tpa;
790 int num_partitions = num_var_partitions (map);
791 int x;
793 if (num_partitions == 0)
794 return NULL;
796 tpa = (tpa_p) xmalloc (sizeof (struct tree_partition_associator_d));
797 tpa->num_trees = 0;
798 tpa->uncompressed_num = -1;
799 tpa->map = map;
800 tpa->next_partition = (int *)xmalloc (num_partitions * sizeof (int));
801 memset (tpa->next_partition, TPA_NONE, num_partitions * sizeof (int));
803 tpa->partition_to_tree_map = (int *)xmalloc (num_partitions * sizeof (int));
804 memset (tpa->partition_to_tree_map, TPA_NONE, num_partitions * sizeof (int));
806 x = MAX (40, (num_partitions / 20));
807 VARRAY_TREE_INIT (tpa->trees, x, "trees");
808 VARRAY_INT_INIT (tpa->first_partition, x, "first_partition");
810 return tpa;
815 /* Remove PARTITION_INDEX from TREE_INDEX's list in the tpa structure TPA. */
817 void
818 tpa_remove_partition (tpa_p tpa, int tree_index, int partition_index)
820 int i;
822 i = tpa_first_partition (tpa, tree_index);
823 if (i == partition_index)
825 VARRAY_INT (tpa->first_partition, tree_index) = tpa->next_partition[i];
827 else
829 for ( ; i != TPA_NONE; i = tpa_next_partition (tpa, i))
831 if (tpa->next_partition[i] == partition_index)
833 tpa->next_partition[i] = tpa->next_partition[partition_index];
834 break;
841 /* Free the memory used by tree_partition_associator object TPA. */
843 void
844 tpa_delete (tpa_p tpa)
846 if (!tpa)
847 return;
849 free (tpa->partition_to_tree_map);
850 free (tpa->next_partition);
851 free (tpa);
855 /* This function will remove any tree entries from TPA which have only a single
856 element. This will help keep the size of the conflict graph down. The
857 function returns the number of remaining tree lists. */
859 int
860 tpa_compact (tpa_p tpa)
862 int last, x, y, first, swap_i;
863 tree swap_t;
865 /* Find the last list which has more than 1 partition. */
866 for (last = tpa->num_trees - 1; last > 0; last--)
868 first = tpa_first_partition (tpa, last);
869 if (tpa_next_partition (tpa, first) != NO_PARTITION)
870 break;
873 x = 0;
874 while (x < last)
876 first = tpa_first_partition (tpa, x);
878 /* If there is not more than one partition, swap with the current end
879 of the tree list. */
880 if (tpa_next_partition (tpa, first) == NO_PARTITION)
882 swap_t = VARRAY_TREE (tpa->trees, last);
883 swap_i = VARRAY_INT (tpa->first_partition, last);
885 /* Update the last entry. Since it is known to only have one
886 partition, there is nothing else to update. */
887 VARRAY_TREE (tpa->trees, last) = VARRAY_TREE (tpa->trees, x);
888 VARRAY_INT (tpa->first_partition, last)
889 = VARRAY_INT (tpa->first_partition, x);
890 tpa->partition_to_tree_map[tpa_first_partition (tpa, last)] = last;
892 /* Since this list is known to have more than one partition, update
893 the list owner entries. */
894 VARRAY_TREE (tpa->trees, x) = swap_t;
895 VARRAY_INT (tpa->first_partition, x) = swap_i;
896 for (y = tpa_first_partition (tpa, x);
897 y != NO_PARTITION;
898 y = tpa_next_partition (tpa, y))
899 tpa->partition_to_tree_map[y] = x;
901 /* Ensure last is a list with more than one partition. */
902 last--;
903 for (; last > x; last--)
905 first = tpa_first_partition (tpa, last);
906 if (tpa_next_partition (tpa, first) != NO_PARTITION)
907 break;
910 x++;
913 first = tpa_first_partition (tpa, x);
914 if (tpa_next_partition (tpa, first) != NO_PARTITION)
915 x++;
916 tpa->uncompressed_num = tpa->num_trees;
917 tpa->num_trees = x;
918 return last;
922 /* Initialize a root_var object with SSA partitions from MAP which are based
923 on each root variable. */
925 root_var_p
926 root_var_init (var_map map)
928 root_var_p rv;
929 int num_partitions = num_var_partitions (map);
930 int x, p;
931 tree t;
932 var_ann_t ann;
933 sbitmap seen;
935 rv = tpa_init (map);
936 if (!rv)
937 return NULL;
939 seen = sbitmap_alloc (num_partitions);
940 sbitmap_zero (seen);
942 /* Start at the end and work towards the front. This will provide a list
943 that is ordered from smallest to largest. */
944 for (x = num_partitions - 1; x >= 0; x--)
946 t = partition_to_var (map, x);
948 /* The var map may not be compacted yet, so check for NULL. */
949 if (!t)
950 continue;
952 p = var_to_partition (map, t);
954 #ifdef ENABLE_CHECKING
955 if (p == NO_PARTITION)
956 abort ();
957 #endif
959 /* Make sure we only put coalesced partitions into the list once. */
960 if (TEST_BIT (seen, p))
961 continue;
962 SET_BIT (seen, p);
963 if (TREE_CODE (t) == SSA_NAME)
964 t = SSA_NAME_VAR (t);
965 ann = var_ann (t);
966 if (ann->root_var_processed)
968 rv->next_partition[p] = VARRAY_INT (rv->first_partition,
969 VAR_ANN_ROOT_INDEX (ann));
970 VARRAY_INT (rv->first_partition, VAR_ANN_ROOT_INDEX (ann)) = p;
972 else
974 ann->root_var_processed = 1;
975 VAR_ANN_ROOT_INDEX (ann) = rv->num_trees++;
976 VARRAY_PUSH_TREE (rv->trees, t);
977 VARRAY_PUSH_INT (rv->first_partition, p);
979 rv->partition_to_tree_map[p] = VAR_ANN_ROOT_INDEX (ann);
982 /* Reset the out_of_ssa_tag flag on each variable for later use. */
983 for (x = 0; x < rv->num_trees; x++)
985 t = VARRAY_TREE (rv->trees, x);
986 var_ann (t)->root_var_processed = 0;
989 sbitmap_free (seen);
990 return rv;
994 /* Initialize a type_var structure which associates all the partitions in MAP
995 of the same type to the type node's index. Volatiles are ignored. */
997 type_var_p
998 type_var_init (var_map map)
1000 type_var_p tv;
1001 int x, y, p;
1002 int num_partitions = num_var_partitions (map);
1003 tree t;
1004 sbitmap seen;
1006 seen = sbitmap_alloc (num_partitions);
1007 sbitmap_zero (seen);
1009 tv = tpa_init (map);
1010 if (!tv)
1011 return NULL;
1013 for (x = num_partitions - 1; x >= 0; x--)
1015 t = partition_to_var (map, x);
1017 /* Disallow coalescing of these types of variables. */
1018 if (!t
1019 || TREE_THIS_VOLATILE (t)
1020 || TREE_CODE (t) == RESULT_DECL
1021 || TREE_CODE (t) == PARM_DECL
1022 || (DECL_P (t)
1023 && (DECL_REGISTER (t)
1024 || !DECL_ARTIFICIAL (t)
1025 || DECL_RTL_SET_P (t))))
1026 continue;
1028 p = var_to_partition (map, t);
1030 #ifdef ENABLE_CHECKING
1031 if (p == NO_PARTITION)
1032 abort ();
1033 #endif
1035 /* If partitions have been coalesced, only add the representative
1036 for the partition to the list once. */
1037 if (TEST_BIT (seen, p))
1038 continue;
1039 SET_BIT (seen, p);
1040 t = TREE_TYPE (t);
1042 /* Find the list for this type. */
1043 for (y = 0; y < tv->num_trees; y++)
1044 if (t == VARRAY_TREE (tv->trees, y))
1045 break;
1046 if (y == tv->num_trees)
1048 tv->num_trees++;
1049 VARRAY_PUSH_TREE (tv->trees, t);
1050 VARRAY_PUSH_INT (tv->first_partition, p);
1052 else
1054 tv->next_partition[p] = VARRAY_INT (tv->first_partition, y);
1055 VARRAY_INT (tv->first_partition, y) = p;
1057 tv->partition_to_tree_map[p] = y;
1059 sbitmap_free (seen);
1060 return tv;
1064 /* Create a new coalesce list object from MAP and return it. */
1066 coalesce_list_p
1067 create_coalesce_list (var_map map)
1069 coalesce_list_p list;
1071 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
1073 list->map = map;
1074 list->add_mode = true;
1075 list->list = (partition_pair_p *) xcalloc (num_var_partitions (map),
1076 sizeof (struct partition_pair_d));
1077 return list;
1081 /* Delete coalesce list CL. */
1083 void
1084 delete_coalesce_list (coalesce_list_p cl)
1086 free (cl->list);
1087 free (cl);
1091 /* Find a matching coalesce pair object in CL for partitions P1 and P2. If
1092 one isn't found, return NULL if CREATE is false, otherwise create a new
1093 coalesce pair object and return it. */
1095 static partition_pair_p
1096 find_partition_pair (coalesce_list_p cl, int p1, int p2, bool create)
1098 partition_pair_p node, tmp;
1099 int s;
1101 /* Normalize so that p1 is the smaller value. */
1102 if (p2 < p1)
1104 s = p1;
1105 p1 = p2;
1106 p2 = s;
1109 tmp = NULL;
1111 /* The list is sorted such that if we find a value greater than p2,
1112 p2 is not in the list. */
1113 for (node = cl->list[p1]; node; node = node->next)
1115 if (node->second_partition == p2)
1116 return node;
1117 else
1118 if (node->second_partition > p2)
1119 break;
1120 tmp = node;
1123 if (!create)
1124 return NULL;
1126 node = (partition_pair_p) xmalloc (sizeof (struct partition_pair_d));
1127 node->first_partition = p1;
1128 node->second_partition = p2;
1129 node->cost = 0;
1131 if (tmp != NULL)
1133 node->next = tmp->next;
1134 tmp->next = node;
1136 else
1138 /* This is now the first node in the list. */
1139 node->next = cl->list[p1];
1140 cl->list[p1] = node;
1143 return node;
1147 /* Add a potential coalesce between P1 and P2 in CL with a cost of VALUE. */
1149 void
1150 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
1152 partition_pair_p node;
1154 #ifdef ENABLE_CHECKING
1155 if (!cl->add_mode)
1156 abort();
1157 #endif
1159 if (p1 == p2)
1160 return;
1162 node = find_partition_pair (cl, p1, p2, true);
1164 node->cost += value;
1168 /* Comparison function to allow qsort to sort P1 and P2 in descending order. */
1170 static
1171 int compare_pairs (const void *p1, const void *p2)
1173 return (*(partition_pair_p *)p2)->cost - (*(partition_pair_p *)p1)->cost;
1177 /* Prepare CL for removal of preferred pairs. When finished, list element
1178 0 has all the coalesce pairs, sorted in order from most important coalesce
1179 to least important. */
1181 void
1182 sort_coalesce_list (coalesce_list_p cl)
1184 int x, num, count;
1185 partition_pair_p chain, p;
1186 partition_pair_p *list;
1188 if (!cl->add_mode)
1189 abort();
1191 cl->add_mode = false;
1193 /* Compact the array of lists to a single list, and count the elements. */
1194 num = 0;
1195 chain = NULL;
1196 for (x = 0; x < num_var_partitions (cl->map); x++)
1197 if (cl->list[x] != NULL)
1199 for (p = cl->list[x]; p->next != NULL; p = p->next)
1200 num++;
1201 num++;
1202 p->next = chain;
1203 chain = cl->list[x];
1204 cl->list[x] = NULL;
1207 /* Only call qsort if there are more than 2 items. */
1208 if (num > 2)
1210 list = xmalloc (sizeof (partition_pair_p) * num);
1211 count = 0;
1212 for (p = chain; p != NULL; p = p->next)
1213 list[count++] = p;
1215 #ifdef ENABLE_CHECKING
1216 if (count != num)
1217 abort ();
1218 #endif
1220 qsort (list, count, sizeof (partition_pair_p), compare_pairs);
1222 p = list[0];
1223 for (x = 1; x < num; x++)
1225 p->next = list[x];
1226 p = list[x];
1228 p->next = NULL;
1229 cl->list[0] = list[0];
1230 free (list);
1232 else
1234 cl->list[0] = chain;
1235 if (num == 2)
1237 /* Simply swap the two elements if they are in the wrong order. */
1238 if (chain->cost < chain->next->cost)
1240 cl->list[0] = chain->next;
1241 cl->list[0]->next = chain;
1242 chain->next = NULL;
1249 /* Retrieve the best remaining pair to coalesce from CL. Returns the 2
1250 partitions via P1 and P2. Their calculated cost is returned by the function.
1251 NO_BEST_COALESCE is returned if the coalesce list is empty. */
1253 int
1254 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
1256 partition_pair_p node;
1257 int ret;
1259 if (cl->add_mode)
1260 abort();
1262 node = cl->list[0];
1263 if (!node)
1264 return NO_BEST_COALESCE;
1266 cl->list[0] = node->next;
1268 *p1 = node->first_partition;
1269 *p2 = node->second_partition;
1270 ret = node->cost;
1271 free (node);
1273 return ret;
1277 /* If variable VAR is in a partition in MAP, add a conflict in GRAPH between
1278 VAR and any other live partitions in VEC which are associated via TPA.
1279 Reset the live bit in VEC. */
1281 static inline void
1282 add_conflicts_if_valid (tpa_p tpa, conflict_graph graph,
1283 var_map map, bitmap vec, tree var)
1285 int p, y, first;
1286 p = var_to_partition (map, var);
1287 if (p != NO_PARTITION)
1289 bitmap_clear_bit (vec, p);
1290 first = tpa_find_tree (tpa, p);
1291 /* If find returns nothing, this object isn't interesting. */
1292 if (first == TPA_NONE)
1293 return;
1294 /* Only add interferences between objects in the same list. */
1295 for (y = tpa_first_partition (tpa, first);
1296 y != TPA_NONE;
1297 y = tpa_next_partition (tpa, y))
1299 if (bitmap_bit_p (vec, y))
1300 conflict_graph_add (graph, p, y);
1306 /* Return a conflict graph for the information contained in LIVE_INFO. Only
1307 conflicts between items in the same TPA list are added. If optional
1308 coalesce list CL is passed in, any copies encountered are added. */
1310 conflict_graph
1311 build_tree_conflict_graph (tree_live_info_p liveinfo, tpa_p tpa,
1312 coalesce_list_p cl)
1314 conflict_graph graph;
1315 var_map map;
1316 bitmap live;
1317 int num, x, y, i;
1318 basic_block bb;
1319 varray_type partition_link, tpa_to_clear, tpa_nodes;
1320 def_optype defs;
1321 use_optype uses;
1322 unsigned l;
1324 map = live_var_map (liveinfo);
1325 graph = conflict_graph_new (num_var_partitions (map));
1327 if (tpa_num_trees (tpa) == 0)
1328 return graph;
1330 live = BITMAP_XMALLOC ();
1332 VARRAY_INT_INIT (partition_link, num_var_partitions (map) + 1, "part_link");
1333 VARRAY_INT_INIT (tpa_nodes, tpa_num_trees (tpa), "tpa nodes");
1334 VARRAY_INT_INIT (tpa_to_clear, 50, "tpa to clear");
1336 FOR_EACH_BB (bb)
1338 block_stmt_iterator bsi;
1339 tree phi;
1341 /* Start with live on exit temporaries. */
1342 bitmap_copy (live, live_on_exit (liveinfo, bb));
1344 for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_prev (&bsi))
1346 bool is_a_copy = false;
1347 tree stmt = bsi_stmt (bsi);
1348 stmt_ann_t ann;
1350 get_stmt_operands (stmt);
1351 ann = stmt_ann (stmt);
1353 /* A copy between 2 partitions does not introduce an interference
1354 by itself. If they did, you would never be able to coalesce
1355 two things which are copied. If the two variables really do
1356 conflict, they will conflict elsewhere in the program.
1358 This is handled specially here since we may also be interested
1359 in copies between real variables and SSA_NAME variables. We may
1360 be interested in trying to coalesce SSA_NAME variables with
1361 root variables in some cases. */
1363 if (TREE_CODE (stmt) == MODIFY_EXPR)
1365 tree lhs = TREE_OPERAND (stmt, 0);
1366 tree rhs = TREE_OPERAND (stmt, 1);
1367 int p1, p2;
1368 int bit;
1370 if (DECL_P (lhs) || TREE_CODE (lhs) == SSA_NAME)
1371 p1 = var_to_partition (map, lhs);
1372 else
1373 p1 = NO_PARTITION;
1375 if (DECL_P (rhs) || TREE_CODE (rhs) == SSA_NAME)
1376 p2 = var_to_partition (map, rhs);
1377 else
1378 p2 = NO_PARTITION;
1380 if (p1 != NO_PARTITION && p2 != NO_PARTITION)
1382 is_a_copy = true;
1383 bit = bitmap_bit_p (live, p2);
1384 /* If the RHS is live, make it not live while we add
1385 the conflicts, then make it live again. */
1386 if (bit)
1387 bitmap_clear_bit (live, p2);
1388 add_conflicts_if_valid (tpa, graph, map, live, lhs);
1389 if (bit)
1390 bitmap_set_bit (live, p2);
1391 if (cl)
1392 add_coalesce (cl, p1, p2, 1);
1393 set_if_valid (map, live, rhs);
1397 if (!is_a_copy)
1399 tree var;
1401 defs = DEF_OPS (ann);
1402 num = NUM_DEFS (defs);
1403 for (x = 0; x < num; x++)
1405 var = DEF_OP (defs, x);
1406 add_conflicts_if_valid (tpa, graph, map, live, var);
1409 uses = USE_OPS (ann);
1410 num = NUM_USES (uses);
1411 for (x = 0; x < num; x++)
1413 var = USE_OP (uses, x);
1414 set_if_valid (map, live, var);
1419 /* If result of a PHI is unused, then the loops over the statements
1420 will not record any conflicts. However, since the PHI node is
1421 going to be translated out of SSA form we must record a conflict
1422 between the result of the PHI and any variables with are live.
1423 Otherwise the out-of-ssa translation may create incorrect code. */
1424 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1426 tree result = PHI_RESULT (phi);
1427 int p = var_to_partition (map, result);
1429 if (p != NO_PARTITION && ! bitmap_bit_p (live, p))
1430 add_conflicts_if_valid (tpa, graph, map, live, result);
1433 /* Anything which is still live at this point interferes.
1434 In order to implement this efficiently, only conflicts between
1435 partitions which have the same TPA root need be added.
1436 TPA roots which have been seen are tracked in 'tpa_nodes'. A nonzero
1437 entry points to an index into 'partition_link', which then indexes
1438 into itself forming a linked list of partitions sharing a tpa root
1439 which have been seen as live up to this point. Since partitions start
1440 at index zero, all entries in partition_link are (partition + 1).
1442 Conflicts are added between the current partition and any already seen.
1443 tpa_clear contains all the tpa_roots processed, and these are the only
1444 entries which need to be zero'd out for a clean restart. */
1446 EXECUTE_IF_SET_IN_BITMAP (live, 0, x,
1448 i = tpa_find_tree (tpa, x);
1449 if (i != TPA_NONE)
1451 int start = VARRAY_INT (tpa_nodes, i);
1452 /* If start is 0, a new root reference list is being started.
1453 Register it to be cleared. */
1454 if (!start)
1455 VARRAY_PUSH_INT (tpa_to_clear, i);
1457 /* Add interferences to other tpa members seen. */
1458 for (y = start; y != 0; y = VARRAY_INT (partition_link, y))
1459 conflict_graph_add (graph, x, y - 1);
1460 VARRAY_INT (tpa_nodes, i) = x + 1;
1461 VARRAY_INT (partition_link, x + 1) = start;
1465 /* Now clear the used tpa root references. */
1466 for (l = 0; l < VARRAY_ACTIVE_SIZE (tpa_to_clear); l++)
1467 VARRAY_INT (tpa_nodes, VARRAY_INT (tpa_to_clear, l)) = 0;
1468 VARRAY_POP_ALL (tpa_to_clear);
1471 BITMAP_XFREE (live);
1472 return graph;
1476 /* This routine will attempt to coalesce the elements in TPA subject to the
1477 conflicts found in GRAPH. If optional coalesce_list CL is provided,
1478 only coalesces specified within the coalesce list are attempted. Otherwise
1479 an attempt is made to coalesce as many partitions within each TPA grouping
1480 as possible. If DEBUG is provided, debug output will be sent there. */
1482 void
1483 coalesce_tpa_members (tpa_p tpa, conflict_graph graph, var_map map,
1484 coalesce_list_p cl, FILE *debug)
1486 int x, y, z, w;
1487 tree var, tmp;
1489 /* Attempt to coalesce any items in a coalesce list. */
1490 if (cl)
1492 while (pop_best_coalesce (cl, &x, &y) != NO_BEST_COALESCE)
1494 if (debug)
1496 fprintf (debug, "Coalesce list: (%d)", x);
1497 print_generic_expr (debug, partition_to_var (map, x), TDF_SLIM);
1498 fprintf (debug, " & (%d)", y);
1499 print_generic_expr (debug, partition_to_var (map, y), TDF_SLIM);
1502 w = tpa_find_tree (tpa, x);
1503 z = tpa_find_tree (tpa, y);
1504 if (w != z || w == TPA_NONE || z == TPA_NONE)
1506 if (debug)
1508 if (w != z)
1509 fprintf (debug, ": Fail, Non-matching TPA's\n");
1510 if (w == TPA_NONE)
1511 fprintf (debug, ": Fail %d non TPA.\n", x);
1512 else
1513 fprintf (debug, ": Fail %d non TPA.\n", y);
1515 continue;
1517 var = partition_to_var (map, x);
1518 tmp = partition_to_var (map, y);
1519 x = var_to_partition (map, var);
1520 y = var_to_partition (map, tmp);
1521 if (debug)
1522 fprintf (debug, " [map: %d, %d] ", x, y);
1523 if (x == y)
1525 if (debug)
1526 fprintf (debug, ": Already Coalesced.\n");
1527 continue;
1529 if (!conflict_graph_conflict_p (graph, x, y))
1531 z = var_union (map, var, tmp);
1532 if (z == NO_PARTITION)
1534 if (debug)
1535 fprintf (debug, ": Unable to perform partition union.\n");
1536 continue;
1539 /* z is the new combined partition. We need to remove the other
1540 partition from the list. Set x to be that other partition. */
1541 if (z == x)
1543 conflict_graph_merge_regs (graph, x, y);
1544 w = tpa_find_tree (tpa, y);
1545 tpa_remove_partition (tpa, w, y);
1547 else
1549 conflict_graph_merge_regs (graph, y, x);
1550 w = tpa_find_tree (tpa, x);
1551 tpa_remove_partition (tpa, w, x);
1554 if (debug)
1555 fprintf (debug, ": Success -> %d\n", z);
1557 else
1558 if (debug)
1559 fprintf (debug, ": Fail due to conflict\n");
1561 /* If using a coalesce list, don't try to coalesce anything else. */
1562 return;
1565 for (x = 0; x < tpa_num_trees (tpa); x++)
1567 while (tpa_first_partition (tpa, x) != TPA_NONE)
1569 int p1, p2;
1570 /* Coalesce first partition with anything that doesn't conflict. */
1571 y = tpa_first_partition (tpa, x);
1572 tpa_remove_partition (tpa, x, y);
1574 var = partition_to_var (map, y);
1575 /* p1 is the partition representative to which y belongs. */
1576 p1 = var_to_partition (map, var);
1578 for (z = tpa_next_partition (tpa, y);
1579 z != TPA_NONE;
1580 z = tpa_next_partition (tpa, z))
1582 tmp = partition_to_var (map, z);
1583 /* p2 is the partition representative to which z belongs. */
1584 p2 = var_to_partition (map, tmp);
1585 if (debug)
1587 fprintf (debug, "Coalesce : ");
1588 print_generic_expr (debug, var, TDF_SLIM);
1589 fprintf (debug, " &");
1590 print_generic_expr (debug, tmp, TDF_SLIM);
1591 fprintf (debug, " (%d ,%d)", p1, p2);
1594 /* If partitions are already merged, don't check for conflict. */
1595 if (tmp == var)
1597 tpa_remove_partition (tpa, x, z);
1598 if (debug)
1599 fprintf (debug, ": Already coalesced\n");
1601 else
1602 if (!conflict_graph_conflict_p (graph, p1, p2))
1604 int v;
1605 if (tpa_find_tree (tpa, y) == TPA_NONE
1606 || tpa_find_tree (tpa, z) == TPA_NONE)
1608 if (debug)
1609 fprintf (debug, ": Fail non-TPA member\n");
1610 continue;
1612 if ((v = var_union (map, var, tmp)) == NO_PARTITION)
1614 if (debug)
1615 fprintf (debug, ": Fail cannot combine partitions\n");
1616 continue;
1619 tpa_remove_partition (tpa, x, z);
1620 if (v == p1)
1621 conflict_graph_merge_regs (graph, v, z);
1622 else
1624 /* Update the first partition's representative. */
1625 conflict_graph_merge_regs (graph, v, y);
1626 p1 = v;
1629 /* The root variable of the partition may be changed
1630 now. */
1631 var = partition_to_var (map, p1);
1633 if (debug)
1634 fprintf (debug, ": Success -> %d\n", v);
1636 else
1637 if (debug)
1638 fprintf (debug, ": Fail, Conflict\n");
1645 /* Send debug info for coalesce list CL to file F. */
1647 void
1648 dump_coalesce_list (FILE *f, coalesce_list_p cl)
1650 partition_pair_p node;
1651 int x, num;
1652 tree var;
1654 if (cl->add_mode)
1656 fprintf (f, "Coalesce List:\n");
1657 num = num_var_partitions (cl->map);
1658 for (x = 0; x < num; x++)
1660 node = cl->list[x];
1661 if (node)
1663 fprintf (f, "[");
1664 print_generic_expr (f, partition_to_var (cl->map, x), TDF_SLIM);
1665 fprintf (f, "] - ");
1666 for ( ; node; node = node->next)
1668 var = partition_to_var (cl->map, node->second_partition);
1669 print_generic_expr (f, var, TDF_SLIM);
1670 fprintf (f, "(%1d), ", node->cost);
1672 fprintf (f, "\n");
1676 else
1678 fprintf (f, "Sorted Coalesce list:\n");
1679 for (node = cl->list[0]; node; node = node->next)
1681 fprintf (f, "(%d) ", node->cost);
1682 var = partition_to_var (cl->map, node->first_partition);
1683 print_generic_expr (f, var, TDF_SLIM);
1684 fprintf (f, " : ");
1685 var = partition_to_var (cl->map, node->second_partition);
1686 print_generic_expr (f, var, TDF_SLIM);
1687 fprintf (f, "\n");
1693 /* Output tree_partition_associator object TPA to file F.. */
1695 void
1696 tpa_dump (FILE *f, tpa_p tpa)
1698 int x, i;
1700 if (!tpa)
1701 return;
1703 for (x = 0; x < tpa_num_trees (tpa); x++)
1705 print_generic_expr (f, tpa_tree (tpa, x), TDF_SLIM);
1706 fprintf (f, " : (");
1707 for (i = tpa_first_partition (tpa, x);
1708 i != TPA_NONE;
1709 i = tpa_next_partition (tpa, i))
1711 fprintf (f, "(%d)",i);
1712 print_generic_expr (f, partition_to_var (tpa->map, i), TDF_SLIM);
1713 fprintf (f, " ");
1715 #ifdef ENABLE_CHECKING
1716 if (tpa_find_tree (tpa, i) != x)
1717 fprintf (f, "**find tree incorrectly set** ");
1718 #endif
1721 fprintf (f, ")\n");
1723 fflush (f);
1727 /* Output partition map MAP to file F. */
1729 void
1730 dump_var_map (FILE *f, var_map map)
1732 int t;
1733 unsigned x, y;
1734 int p;
1736 fprintf (f, "\nPartition map \n\n");
1738 for (x = 0; x < map->num_partitions; x++)
1740 if (map->compact_to_partition != NULL)
1741 p = map->compact_to_partition[x];
1742 else
1743 p = x;
1745 if (map->partition_to_var[p] == NULL_TREE)
1746 continue;
1748 t = 0;
1749 for (y = 1; y < num_ssa_names; y++)
1751 p = partition_find (map->var_partition, y);
1752 if (map->partition_to_compact)
1753 p = map->partition_to_compact[p];
1754 if (p == (int)x)
1756 if (t++ == 0)
1758 fprintf(f, "Partition %d (", x);
1759 print_generic_expr (f, partition_to_var (map, p), TDF_SLIM);
1760 fprintf (f, " - ");
1762 fprintf (f, "%d ", y);
1765 if (t != 0)
1766 fprintf (f, ")\n");
1768 fprintf (f, "\n");
1772 /* Output live range info LIVE to file F, controlled by FLAG. */
1774 void
1775 dump_live_info (FILE *f, tree_live_info_p live, int flag)
1777 basic_block bb;
1778 int i;
1779 var_map map = live->map;
1781 if ((flag & LIVEDUMP_ENTRY) && live->livein)
1783 FOR_EACH_BB (bb)
1785 fprintf (f, "\nLive on entry to BB%d : ", bb->index);
1786 for (i = 0; i < num_var_partitions (map); i++)
1788 if (bitmap_bit_p (live_entry_blocks (live, i), bb->index))
1790 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1791 fprintf (f, " ");
1794 fprintf (f, "\n");
1798 if ((flag & LIVEDUMP_EXIT) && live->liveout)
1800 FOR_EACH_BB (bb)
1802 fprintf (f, "\nLive on exit from BB%d : ", bb->index);
1803 EXECUTE_IF_SET_IN_BITMAP (live->liveout[bb->index], 0, i,
1805 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1806 fprintf (f, " ");
1808 fprintf (f, "\n");
1813 /* Register partitions in MAP so that we can take VARS out of SSA form.
1814 This requires a walk over all the PHI nodes and all the statements. */
1816 void
1817 register_ssa_partitions_for_vars (bitmap vars, var_map map)
1819 basic_block bb;
1821 if (bitmap_first_set_bit (vars) >= 0)
1824 /* Find every instance (SSA_NAME) of variables in VARs and
1825 register a new partition for them. This requires examining
1826 every statement and every PHI node once. */
1827 FOR_EACH_BB (bb)
1829 block_stmt_iterator bsi;
1830 tree next;
1831 tree phi;
1833 /* Register partitions for SSA_NAMEs appearing in the PHI
1834 nodes in this basic block.
1836 Note we delete PHI nodes in this loop if they are
1837 associated with virtual vars which are going to be
1838 renamed. */
1839 for (phi = phi_nodes (bb); phi; phi = next)
1841 tree result = SSA_NAME_VAR (PHI_RESULT (phi));
1843 next = PHI_CHAIN (phi);
1844 if (bitmap_bit_p (vars, var_ann (result)->uid))
1846 if (! is_gimple_reg (result))
1847 remove_phi_node (phi, NULL_TREE, bb);
1848 else
1850 int i;
1852 /* Register a partition for the result. */
1853 register_ssa_partition (map, PHI_RESULT (phi), 0);
1855 /* Register a partition for each argument as needed. */
1856 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1858 tree arg = PHI_ARG_DEF (phi, i);
1860 if (TREE_CODE (arg) != SSA_NAME)
1861 continue;
1862 if (!bitmap_bit_p (vars,
1863 var_ann (SSA_NAME_VAR (arg))->uid))
1864 continue;
1866 register_ssa_partition (map, arg, 1);
1872 /* Now register partitions for SSA_NAMEs appearing in each
1873 statement in this block. */
1874 for (bsi = bsi_start (bb); ! bsi_end_p (bsi); bsi_next (&bsi))
1876 stmt_ann_t ann = stmt_ann (bsi_stmt (bsi));
1877 use_optype uses = USE_OPS (ann);
1878 def_optype defs = DEF_OPS (ann);
1879 unsigned int i;
1881 for (i = 0; i < NUM_USES (uses); i++)
1883 tree op = USE_OP (uses, i);
1885 if (TREE_CODE (op) == SSA_NAME
1886 && bitmap_bit_p (vars, var_ann (SSA_NAME_VAR (op))->uid))
1887 register_ssa_partition (map, op, 1);
1890 for (i = 0; i < NUM_DEFS (defs); i++)
1892 tree op = DEF_OP (defs, i);
1894 if (TREE_CODE (op) == SSA_NAME
1895 && bitmap_bit_p (vars,
1896 var_ann (SSA_NAME_VAR (op))->uid))
1897 register_ssa_partition (map, op, 0);