OSDN Git Service

2005-06-15 Andrew Pinski <pinskia@physics.uc.edu>
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-live.c
1 /* Liveness for SSA trees.
2    Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3    Contributed by Andrew MacLeod <amacleod@redhat.com>
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
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"
41
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);
51
52 /* This is where the mapping from SSA version number to real storage variable
53    is tracked.  
54
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.
59
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.  */
63
64 /* Create a variable partition map of SIZE, initialize and return it.  */
65
66 var_map
67 init_var_map (int size)
68 {
69   var_map map;
70
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));
76
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;
83 }
84
85
86 /* Free memory associated with MAP.  */
87
88 void
89 delete_var_map (var_map map)
90 {
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);
100 }
101
102
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.  */
106
107 int
108 var_union (var_map map, tree var1, tree var2)
109 {
110   int p1, p2, p3;
111   tree root_var = NULL_TREE;
112   tree other_var = NULL_TREE;
113
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.  */
117
118   if (TREE_CODE (var1) == SSA_NAME)
119     p1 = partition_find (map->var_partition, SSA_NAME_VERSION (var1));
120   else
121     {
122       p1 = var_to_partition (map, var1);
123       if (map->compact_to_partition)
124         p1 = map->compact_to_partition[p1];
125       root_var = var1;
126     }
127   
128   if (TREE_CODE (var2) == SSA_NAME)
129     p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2));
130   else
131     {
132       p2 = var_to_partition (map, var2);
133       if (map->compact_to_partition)
134         p2 = map->compact_to_partition[p2];
135
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)))
139         {
140           other_var = root_var;
141           root_var = var2;
142         }
143       else 
144         other_var = var2;
145     }
146
147   gcc_assert (p1 != NO_PARTITION);
148   gcc_assert (p2 != NO_PARTITION);
149
150   if (p1 == p2)
151     p3 = p1;
152   else
153     p3 = partition_union (map->var_partition, p1, p2);
154
155   if (map->partition_to_compact)
156     p3 = map->partition_to_compact[p3];
157
158   if (root_var)
159     change_partition_var (map, root_var, p3);
160   if (other_var)
161     change_partition_var (map, other_var, p3);
162
163   return p3;
164 }
165
166
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.
172
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.
179
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.  */
183
184 void 
185 compact_var_map (var_map map, int flags)
186 {
187   sbitmap used;
188   int tmp, root, root_i;
189   unsigned int x, limit, count;
190   tree var;
191   root_var_p rv = NULL;
192
193   limit = map->partition_size;
194   used = sbitmap_alloc (limit);
195   sbitmap_zero (used);
196
197   /* Already compressed? Abandon the old one.  */
198   if (map->partition_to_compact)
199     {
200       free (map->partition_to_compact);
201       map->partition_to_compact = NULL;
202     }
203   if (map->compact_to_partition)
204     {
205       free (map->compact_to_partition);
206       map->compact_to_partition = NULL;
207     }
208
209   map->num_partitions = map->partition_size;
210
211   if (flags & VARMAP_NO_SINGLE_DEFS)
212     rv = root_var_init (map);
213
214   map->partition_to_compact = (int *)xmalloc (limit * sizeof (int));
215   memset (map->partition_to_compact, 0xff, (limit * sizeof (int)));
216
217   /* Find out which partitions are actually referenced.  */
218   count = 0;
219   for (x = 0; x < limit; x++)
220     {
221       tmp = partition_find (map->var_partition, x);
222       if (!TEST_BIT (used, tmp) && map->partition_to_var[tmp] != NULL_TREE)
223         {
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)
227             {
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;
233             }
234           SET_BIT (used, tmp);
235           count++;
236         }
237     }
238
239   /* Build a compacted partitioning.  */
240   if (count != limit)
241     {
242       sbitmap_iterator sbi;
243
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)
248         {
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++;
255         }
256     }
257   else
258     {
259       free (map->partition_to_compact);
260       map->partition_to_compact = NULL;
261     }
262
263   map->num_partitions = count;
264
265   if (rv)
266     root_var_delete (rv);
267   sbitmap_free (used);
268 }
269
270
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.  */
274   
275 void 
276 change_partition_var (var_map map, tree var, int part)
277 {
278   var_ann_t ann;
279
280   gcc_assert (TREE_CODE (var) != SSA_NAME);
281
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;
287 }
288
289
290 /* Helper function for mark_all_vars_used, called via walk_tree.  */
291
292 static tree
293 mark_all_vars_used_1 (tree *tp, int *walk_subtrees,
294                       void *data ATTRIBUTE_UNUSED)
295 {
296   tree t = *tp;
297
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);
302
303   if (IS_TYPE_OR_DECL_P (t))
304     *walk_subtrees = 0;
305
306   return NULL;
307 }
308
309 /* Mark all VAR_DECLS under *EXPR_P as used, so that they won't be 
310    eliminated during the tree->rtl conversion process.  */
311
312 static inline void
313 mark_all_vars_used (tree *expr_p)
314 {
315   walk_tree (expr_p, mark_all_vars_used_1, NULL, NULL);
316 }
317
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.  */
321
322 var_map
323 create_ssa_var_map (int flags)
324 {
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
335
336   map = init_var_map (num_ssa_names + 1);
337
338 #ifdef ENABLE_CHECKING
339   used_in_real_ops = sbitmap_alloc (num_referenced_vars);
340   sbitmap_zero (used_in_real_ops);
341
342   used_in_virtual_ops = sbitmap_alloc (num_referenced_vars);
343   sbitmap_zero (used_in_virtual_ops);
344 #endif
345
346   if (flags & SSA_VAR_MAP_REF_COUNT)
347     {
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));
351     }
352
353   FOR_EACH_BB (bb)
354     {
355       tree phi, arg;
356       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
357         {
358           int i;
359           register_ssa_partition (map, PHI_RESULT (phi), false);
360           for (i = 0; i < PHI_NUM_ARGS (phi); i++)
361             {
362               arg = PHI_ARG_DEF (phi, i);
363               if (TREE_CODE (arg) == SSA_NAME)
364                 register_ssa_partition (map, arg, true);
365
366               mark_all_vars_used (&PHI_ARG_DEF_TREE (phi, i));
367             }
368         }
369
370       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
371         {
372           stmt = bsi_stmt (bsi);
373
374           /* Register USE and DEF operands in each statement.  */
375           FOR_EACH_SSA_TREE_OPERAND (use , stmt, iter, SSA_OP_USE)
376             {
377               register_ssa_partition (map, use, true);
378
379 #ifdef ENABLE_CHECKING
380               SET_BIT (used_in_real_ops, var_ann (SSA_NAME_VAR (use))->uid);
381 #endif
382             }
383
384           FOR_EACH_SSA_TREE_OPERAND (dest, stmt, iter, SSA_OP_DEF)
385             {
386               register_ssa_partition (map, dest, false);
387
388 #ifdef ENABLE_CHECKING
389               SET_BIT (used_in_real_ops, var_ann (SSA_NAME_VAR (dest))->uid);
390 #endif
391             }
392
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)
397             {
398               SET_BIT (used_in_virtual_ops, var_ann (SSA_NAME_VAR (use))->uid);
399             }
400
401 #endif /* ENABLE_CHECKING */
402
403           mark_all_vars_used (bsi_stmt_ptr (bsi));
404         }
405     }
406
407 #if defined ENABLE_CHECKING
408   {
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)
413       {
414         sbitmap_iterator sbi;
415
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");
420       }
421
422     sbitmap_free (used_in_real_ops);
423     sbitmap_free (used_in_virtual_ops);
424     sbitmap_free (both);
425   }
426 #endif
427
428   return map;
429 }
430
431
432 /* Allocate and return a new live range information object base on MAP.  */
433
434 static tree_live_info_p
435 new_tree_live_info (var_map map)
436 {
437   tree_live_info_p live;
438   unsigned x;
439
440   live = (tree_live_info_p) xmalloc (sizeof (struct tree_live_info_d));
441   live->map = map;
442   live->num_blocks = last_basic_block;
443
444   live->global = BITMAP_ALLOC (NULL);
445
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);
449
450   /* liveout is deferred until it is actually requested.  */
451   live->liveout = NULL;
452   return live;
453 }
454
455
456 /* Free storage for live range info object LIVE.  */
457
458 void 
459 delete_tree_live_info (tree_live_info_p live)
460 {
461   int x;
462   if (live->liveout)
463     {
464       for (x = live->num_blocks - 1; x >= 0; x--)
465         BITMAP_FREE (live->liveout[x]);
466       free (live->liveout);
467     }
468   if (live->livein)
469     {
470       for (x = num_var_partitions (live->map) - 1; x >= 0; x--)
471         BITMAP_FREE (live->livein[x]);
472       free (live->livein);
473     }
474   if (live->global)
475     BITMAP_FREE (live->global);
476   
477   free (live);
478 }
479
480
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.  */
484
485 static void
486 live_worklist (tree_live_info_p live, int *stack, int i)
487 {
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;
496
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));
500
501   EXECUTE_IF_SET_IN_BITMAP (live->livein[i], 0, b, bi)
502     {
503       *tos++ = b;
504     }
505
506   while (tos != stack)
507     {
508       b = *--tos;
509
510       FOR_EACH_EDGE (e, ei, BASIC_BLOCK (b)->preds)
511         if (e->src != ENTRY_BLOCK_PTR)
512           {
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))
517               {
518                 bitmap_set_bit (live->livein[i], e->src->index);
519                 *tos++ = e->src->index;
520               }
521           }
522     }
523 }
524
525
526 /* If VAR is in a partition of MAP, set the bit for that partition in VEC.  */
527
528 static inline void
529 set_if_valid (var_map map, bitmap vec, tree var)
530 {
531   int p = var_to_partition (map, var);
532   if (p != NO_PARTITION)
533     bitmap_set_bit (vec, p);
534 }
535
536
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.  */
539
540 static inline void
541 add_livein_if_notdef (tree_live_info_p live, bitmap def_vec,
542                       tree var, basic_block bb)
543 {
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))
548     {
549       bitmap_set_bit (live->livein[p], bb->index);
550       bitmap_set_bit (live->global, p);
551     }
552 }
553
554
555 /* Given partition map MAP, calculate all the live on entry bitmaps for 
556    each basic block.  Return a live info object.  */
557
558 tree_live_info_p 
559 calculate_live_on_entry (var_map map)
560 {
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
576
577   saw_def = BITMAP_ALLOC (NULL);
578
579   live = new_tree_live_info (map);
580
581   FOR_EACH_BB (bb)
582     {
583       bitmap_clear (saw_def);
584
585       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
586         {
587           for (i = 0; i < (unsigned)PHI_NUM_ARGS (phi); i++)
588             {
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);
594
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);
600             }
601         }
602
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.  */
609
610       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
611         {
612           var = PHI_RESULT (phi);
613           set_if_valid (map, saw_def, var);
614         }
615
616       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
617         {
618           stmt = bsi_stmt (bsi);
619
620           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
621             {
622               add_livein_if_notdef (live, saw_def, op, bb);
623             }
624
625           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
626             {
627               set_if_valid (map, saw_def, op);
628             }
629         }
630     }
631
632   stack = xmalloc (sizeof (int) * last_basic_block);
633   EXECUTE_IF_SET_IN_BITMAP (live->global, 0, i, bi)
634     {
635       live_worklist (live, stack, i);
636     }
637   free (stack);
638
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.  */
643
644   bb = ENTRY_BLOCK_PTR;
645   num = 0;
646   FOR_EACH_EDGE (e, ei, bb->succs)
647     {
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++)
652         {
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));
659
660           if (bitmap_bit_p (live_entry_blocks (live, i), entry_block))
661             {
662               if (!IS_EMPTY_STMT (stmt))
663                 {
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");
674                 }
675               else
676                 {
677                   if (d != var)
678                     {
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)
683                         {
684                           fprintf (stderr, " but is not the default def of ");
685                           print_generic_expr (stderr, d, TDF_SLIM);
686                           fprintf (stderr, "\n");
687                         }
688                       else
689                         fprintf (stderr, " and there is no default def.\n");
690                     }
691                 }
692             }
693           else
694             if (d == var)
695               {
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))
702                   {
703                     for (z = 0; z < PHI_NUM_ARGS (phi); z++)
704                       if (var == PHI_ARG_DEF (phi, z))
705                         {
706                           ok = 1;
707                           break;
708                         }
709                   }
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");
717               }
718         }
719     }
720   gcc_assert (num <= 0);
721 #endif
722
723   BITMAP_FREE (saw_def);
724
725   return live;
726 }
727
728
729 /* Calculate the live on exit vectors based on the entry info in LIVEINFO.  */
730
731 void
732 calculate_live_on_exit (tree_live_info_p liveinfo)
733 {
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;
742
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);
746
747   /* Set all the live-on-exit bits for uses in PHIs.  */
748   FOR_EACH_BB (bb)
749     {
750       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
751         for (i = 0; i < (unsigned)PHI_NUM_ARGS (phi); i++)
752           { 
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);
758           }
759     }
760
761   /* Set live on exit for all predecessors of live on entry's.  */
762   for (i = 0; i < num_var_partitions (map); i++)
763     {
764       bitmap_iterator bi;
765
766       on_entry = live_entry_blocks (liveinfo, i);
767       EXECUTE_IF_SET_IN_BITMAP (on_entry, 0, b, bi)
768         {
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);
773         }
774     }
775
776   liveinfo->liveout = on_exit;
777 }
778
779
780 /* Initialize a tree_partition_associator object using MAP.  */
781
782 static tpa_p
783 tpa_init (var_map map)
784 {
785   tpa_p tpa;
786   int num_partitions = num_var_partitions (map);
787   int x;
788
789   if (num_partitions == 0)
790     return NULL;
791
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));
798
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));
801
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");
805
806   return tpa;
807
808 }
809
810
811 /* Remove PARTITION_INDEX from TREE_INDEX's list in the tpa structure TPA.  */
812
813 void
814 tpa_remove_partition (tpa_p tpa, int tree_index, int partition_index)
815 {
816   int i;
817
818   i = tpa_first_partition (tpa, tree_index);
819   if (i == partition_index)
820     {
821       VARRAY_INT (tpa->first_partition, tree_index) = tpa->next_partition[i];
822     }
823   else
824     {
825       for ( ; i != TPA_NONE; i = tpa_next_partition (tpa, i))
826         {
827           if (tpa->next_partition[i] == partition_index)
828             {
829               tpa->next_partition[i] = tpa->next_partition[partition_index];
830               break;
831             }
832         }
833     }
834 }
835
836
837 /* Free the memory used by tree_partition_associator object TPA.  */
838
839 void
840 tpa_delete (tpa_p tpa)
841 {
842   if (!tpa)
843     return;
844
845   VEC_free (tree, heap, tpa->trees);
846   free (tpa->partition_to_tree_map);
847   free (tpa->next_partition);
848   free (tpa);
849 }
850
851
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.  */
855
856 int 
857 tpa_compact (tpa_p tpa)
858 {
859   int last, x, y, first, swap_i;
860   tree swap_t;
861
862   /* Find the last list which has more than 1 partition.  */
863   for (last = tpa->num_trees - 1; last > 0; last--)
864     {
865       first = tpa_first_partition (tpa, last);
866       if (tpa_next_partition (tpa, first) != NO_PARTITION)
867         break;
868     }
869
870   x = 0;
871   while (x < last)
872     {
873       first = tpa_first_partition (tpa, x);
874
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)
878         {
879           swap_t = VEC_index (tree, tpa->trees, last);
880           swap_i = VARRAY_INT (tpa->first_partition, last);
881
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;
889
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;
898
899           /* Ensure last is a list with more than one partition.  */
900           last--;
901           for (; last > x; last--)
902             {
903               first = tpa_first_partition (tpa, last);
904               if (tpa_next_partition (tpa, first) != NO_PARTITION)
905                 break;
906             }
907         }
908       x++;
909     }
910
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;
917 }
918
919
920 /* Initialize a root_var object with SSA partitions from MAP which are based 
921    on each root variable.  */
922
923 root_var_p
924 root_var_init (var_map map)
925 {
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;
932
933   rv = tpa_init (map);
934   if (!rv)
935     return NULL;
936
937   seen = sbitmap_alloc (num_partitions);
938   sbitmap_zero (seen);
939
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--)
943     {
944       t = partition_to_var (map, x);
945
946       /* The var map may not be compacted yet, so check for NULL.  */
947       if (!t) 
948         continue;
949
950       p = var_to_partition (map, t);
951
952       gcc_assert (p != NO_PARTITION);
953
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)
962         {
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;
966         }
967       else
968         {
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);
973         }
974       rv->partition_to_tree_map[p] = VAR_ANN_ROOT_INDEX (ann);
975     }
976
977   /* Reset the out_of_ssa_tag flag on each variable for later use.  */
978   for (x = 0; x < rv->num_trees; x++)
979     {
980       t = VEC_index (tree, rv->trees, x);
981       var_ann (t)->root_var_processed = 0;
982     }
983
984   sbitmap_free (seen);
985   return rv;
986 }
987
988
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.  */
991
992 type_var_p
993 type_var_init (var_map map)
994 {
995   type_var_p tv;
996   int x, y, p;
997   int num_partitions = num_var_partitions (map);
998   tree t;
999   sbitmap seen;
1000
1001   seen = sbitmap_alloc (num_partitions);
1002   sbitmap_zero (seen);
1003
1004   tv = tpa_init (map);
1005   if (!tv)
1006     return NULL;
1007
1008   for (x = num_partitions - 1; x >= 0; x--)
1009     {
1010       t = partition_to_var (map, x);
1011
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;
1022
1023       p = var_to_partition (map, t);
1024
1025       gcc_assert (p != NO_PARTITION);
1026
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);
1033
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)
1039         {
1040           tv->num_trees++;
1041           VEC_safe_push (tree, heap, tv->trees, t);
1042           VARRAY_PUSH_INT (tv->first_partition, p);
1043         }
1044       else
1045         {
1046           tv->next_partition[p] = VARRAY_INT (tv->first_partition, y);
1047           VARRAY_INT (tv->first_partition, y) = p;
1048         }
1049       tv->partition_to_tree_map[p] = y;
1050     }
1051   sbitmap_free (seen);
1052   return tv;
1053 }
1054
1055
1056 /* Create a new coalesce list object from MAP and return it.  */
1057
1058 coalesce_list_p 
1059 create_coalesce_list (var_map map)
1060 {
1061   coalesce_list_p list;
1062
1063   list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
1064
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;
1070 }
1071
1072
1073 /* Delete coalesce list CL.  */
1074
1075 void 
1076 delete_coalesce_list (coalesce_list_p cl)
1077 {
1078   free (cl->list);
1079   free (cl);
1080 }
1081
1082
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.  */
1086
1087 static partition_pair_p
1088 find_partition_pair (coalesce_list_p cl, int p1, int p2, bool create)
1089 {
1090   partition_pair_p node, tmp;
1091   int s;
1092     
1093   /* Normalize so that p1 is the smaller value.  */
1094   if (p2 < p1)
1095     {
1096       s = p1;
1097       p1 = p2;
1098       p2 = s;
1099     }
1100   
1101   tmp = NULL;
1102
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)
1106     {
1107       if (node->second_partition == p2)
1108         return node;
1109       else
1110         if (node->second_partition > p2)
1111           break;
1112      tmp = node;
1113     }
1114
1115   if (!create)
1116     return NULL;
1117
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;
1122     
1123   if (tmp != NULL)
1124     {
1125       node->next = tmp->next;
1126       tmp->next = node;
1127     }
1128   else
1129     {
1130       /* This is now the first node in the list.  */
1131       node->next = cl->list[p1];
1132       cl->list[p1] = node;
1133     }
1134
1135   return node;
1136 }
1137
1138
1139 /* Add a potential coalesce between P1 and P2 in CL with a cost of VALUE.  */
1140
1141 void 
1142 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
1143 {
1144   partition_pair_p node;
1145
1146   gcc_assert (cl->add_mode);
1147
1148   if (p1 == p2)
1149     return;
1150
1151   node = find_partition_pair (cl, p1, p2, true);
1152
1153   node->cost += value;
1154 }
1155
1156
1157 /* Comparison function to allow qsort to sort P1 and P2 in descending order.  */
1158
1159 static
1160 int compare_pairs (const void *p1, const void *p2)
1161 {
1162   return (*(partition_pair_p *)p2)->cost - (*(partition_pair_p *)p1)->cost;
1163 }
1164
1165
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.  */
1169
1170 void
1171 sort_coalesce_list (coalesce_list_p cl)
1172 {
1173   unsigned x, num, count;
1174   partition_pair_p chain, p;
1175   partition_pair_p  *list;
1176
1177   gcc_assert (cl->add_mode);
1178
1179   cl->add_mode = false;
1180
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)
1186       {
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;
1193       }
1194
1195   /* Only call qsort if there are more than 2 items.  */
1196   if (num > 2)
1197     {
1198       list = xmalloc (sizeof (partition_pair_p) * num);
1199       count = 0;
1200       for (p = chain; p != NULL; p = p->next)
1201         list[count++] = p;
1202
1203       gcc_assert (count == num);
1204         
1205       qsort (list, count, sizeof (partition_pair_p), compare_pairs);
1206
1207       p = list[0];
1208       for (x = 1; x < num; x++)
1209         {
1210           p->next = list[x];
1211           p = list[x];
1212         }
1213       p->next = NULL;
1214       cl->list[0] = list[0];
1215       free (list);
1216     }
1217   else
1218     {
1219       cl->list[0] = chain;
1220       if (num == 2)
1221         {
1222           /* Simply swap the two elements if they are in the wrong order.  */
1223           if (chain->cost < chain->next->cost)
1224             {
1225               cl->list[0] = chain->next;
1226               cl->list[0]->next = chain;
1227               chain->next = NULL;
1228             }
1229         }
1230     }
1231 }
1232
1233
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.  */
1237
1238 static int
1239 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
1240 {
1241   partition_pair_p node;
1242   int ret;
1243
1244   gcc_assert (!cl->add_mode);
1245
1246   node = cl->list[0];
1247   if (!node)
1248     return NO_BEST_COALESCE;
1249
1250   cl->list[0] = node->next;
1251
1252   *p1 = node->first_partition;
1253   *p2 = node->second_partition;
1254   ret = node->cost;
1255   free (node);
1256
1257   return ret;
1258 }
1259
1260
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.  */
1264
1265 static inline void 
1266 add_conflicts_if_valid (tpa_p tpa, conflict_graph graph,
1267                         var_map map, bitmap vec, tree var)
1268
1269   int p, y, first;
1270   p = var_to_partition (map, var);
1271   if (p != NO_PARTITION)
1272     { 
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))
1282         {
1283           if (bitmap_bit_p (vec, y))
1284             conflict_graph_add (graph, p, y);
1285         }
1286     }
1287 }
1288
1289 DEF_VEC_I(int);
1290 DEF_VEC_ALLOC_I(int,heap);
1291
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.  */
1295
1296 conflict_graph
1297 build_tree_conflict_graph (tree_live_info_p liveinfo, tpa_p tpa, 
1298                            coalesce_list_p cl)
1299 {
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;
1310
1311   map = live_var_map (liveinfo);
1312   graph = conflict_graph_new (num_var_partitions (map));
1313
1314   if (tpa_num_trees (tpa) == 0)
1315     return graph;
1316
1317   live = BITMAP_ALLOC (NULL);
1318
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);
1322
1323   FOR_EACH_BB (bb)
1324     {
1325       block_stmt_iterator bsi;
1326       tree phi;
1327       int idx;
1328
1329       /* Start with live on exit temporaries.  */
1330       bitmap_copy (live, live_on_exit (liveinfo, bb));
1331
1332       for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_prev (&bsi))
1333         {
1334           bool is_a_copy = false;
1335           tree stmt = bsi_stmt (bsi);
1336
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.  
1341              
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.  */
1346
1347           if (TREE_CODE (stmt) == MODIFY_EXPR)
1348             {
1349               tree lhs = TREE_OPERAND (stmt, 0);
1350               tree rhs = TREE_OPERAND (stmt, 1);
1351               int p1, p2;
1352               int bit;
1353
1354               if (DECL_P (lhs) || TREE_CODE (lhs) == SSA_NAME)
1355                 p1 = var_to_partition (map, lhs);
1356               else 
1357                 p1 = NO_PARTITION;
1358
1359               if (DECL_P (rhs) || TREE_CODE (rhs) == SSA_NAME)
1360                 p2 = var_to_partition (map, rhs);
1361               else 
1362                 p2 = NO_PARTITION;
1363
1364               if (p1 != NO_PARTITION && p2 != NO_PARTITION)
1365                 {
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);
1378                 }
1379             }
1380
1381           if (!is_a_copy)
1382             {
1383               tree var;
1384               FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
1385                 {
1386                   add_conflicts_if_valid (tpa, graph, map, live, var);
1387                 }
1388
1389               FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
1390                 {
1391                   set_if_valid (map, live, var);
1392                 }
1393             }
1394         }
1395
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))
1402         {
1403           tree result = PHI_RESULT (phi);
1404           int p = var_to_partition (map, result);
1405
1406           if (p != NO_PARTITION && ! bitmap_bit_p (live, p))
1407             add_conflicts_if_valid (tpa, graph, map, live, result);
1408         }
1409
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).
1418
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.  */
1422
1423       EXECUTE_IF_SET_IN_BITMAP (live, 0, x, bi)
1424         {
1425           i = tpa_find_tree (tpa, x);
1426           if (i != (unsigned)TPA_NONE)
1427             {
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);
1433
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;
1439             }
1440         }
1441
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);
1446     }
1447
1448   free (tpa_nodes);
1449   free (partition_link);
1450   VEC_free (int, heap, tpa_to_clear);
1451   BITMAP_FREE (live);
1452   return graph;
1453 }
1454
1455
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.  */
1461
1462 void
1463 coalesce_tpa_members (tpa_p tpa, conflict_graph graph, var_map map, 
1464                       coalesce_list_p cl, FILE *debug)
1465 {
1466   int x, y, z, w;
1467   tree var, tmp;
1468
1469   /* Attempt to coalesce any items in a coalesce list.  */
1470   if (cl)
1471     {
1472       while (pop_best_coalesce (cl, &x, &y) != NO_BEST_COALESCE)
1473         {
1474           if (debug)
1475             {
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);
1480             }
1481
1482           w = tpa_find_tree (tpa, x);
1483           z = tpa_find_tree (tpa, y);
1484           if (w != z || w == TPA_NONE || z == TPA_NONE)
1485             {
1486               if (debug)
1487                 {
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);
1494                 }
1495               continue;
1496             }
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)
1504             {
1505               if (debug)
1506                 fprintf (debug, ": Already Coalesced.\n");
1507               continue;
1508             }
1509           if (!conflict_graph_conflict_p (graph, x, y))
1510             {
1511               z = var_union (map, var, tmp);
1512               if (z == NO_PARTITION)
1513                 {
1514                   if (debug)
1515                     fprintf (debug, ": Unable to perform partition union.\n");
1516                   continue;
1517                 }
1518
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)
1522                 {
1523                   conflict_graph_merge_regs (graph, x, y);
1524                   w = tpa_find_tree (tpa, y);
1525                   tpa_remove_partition (tpa, w, y);
1526                 }
1527               else
1528                 {
1529                   conflict_graph_merge_regs (graph, y, x);
1530                   w = tpa_find_tree (tpa, x);
1531                   tpa_remove_partition (tpa, w, x);
1532                 }
1533
1534               if (debug)
1535                 fprintf (debug, ": Success -> %d\n", z);
1536             }
1537           else
1538             if (debug)
1539               fprintf (debug, ": Fail due to conflict\n");
1540         }
1541       /* If using a coalesce list, don't try to coalesce anything else.  */
1542       return;
1543     }
1544
1545   for (x = 0; x < tpa_num_trees (tpa); x++)
1546     {
1547       while (tpa_first_partition (tpa, x) != TPA_NONE)
1548         {
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);
1553
1554           var = partition_to_var (map, y);
1555           /* p1 is the partition representative to which y belongs.  */
1556           p1 = var_to_partition (map, var);
1557           
1558           for (z = tpa_next_partition (tpa, y); 
1559                z != TPA_NONE; 
1560                z = tpa_next_partition (tpa, z))
1561             {
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)
1566                 {
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);
1572                 }
1573
1574               /* If partitions are already merged, don't check for conflict.  */
1575               if (tmp == var)
1576                 {
1577                   tpa_remove_partition (tpa, x, z);
1578                   if (debug)
1579                     fprintf (debug, ": Already coalesced\n");
1580                 }
1581               else
1582                 if (!conflict_graph_conflict_p (graph, p1, p2))
1583                   {
1584                     int v;
1585                     if (tpa_find_tree (tpa, y) == TPA_NONE 
1586                         || tpa_find_tree (tpa, z) == TPA_NONE)
1587                       {
1588                         if (debug)
1589                           fprintf (debug, ": Fail non-TPA member\n");
1590                         continue;
1591                       }
1592                     if ((v = var_union (map, var, tmp)) == NO_PARTITION)
1593                       {
1594                         if (debug)
1595                           fprintf (debug, ": Fail cannot combine partitions\n");
1596                         continue;
1597                       }
1598
1599                     tpa_remove_partition (tpa, x, z);
1600                     if (v == p1)
1601                       conflict_graph_merge_regs (graph, v, z);
1602                     else
1603                       {
1604                         /* Update the first partition's representative.  */
1605                         conflict_graph_merge_regs (graph, v, y);
1606                         p1 = v;
1607                       }
1608
1609                     /* The root variable of the partition may be changed
1610                        now.  */
1611                     var = partition_to_var (map, p1);
1612
1613                     if (debug)
1614                       fprintf (debug, ": Success -> %d\n", v);
1615                   }
1616                 else
1617                   if (debug)
1618                     fprintf (debug, ": Fail, Conflict\n");
1619             }
1620         }
1621     }
1622 }
1623
1624
1625 /* Send debug info for coalesce list CL to file F.  */
1626
1627 void 
1628 dump_coalesce_list (FILE *f, coalesce_list_p cl)
1629 {
1630   partition_pair_p node;
1631   int x, num;
1632   tree var;
1633
1634   if (cl->add_mode)
1635     {
1636       fprintf (f, "Coalesce List:\n");
1637       num = num_var_partitions (cl->map);
1638       for (x = 0; x < num; x++)
1639         {
1640           node = cl->list[x];
1641           if (node)
1642             {
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)
1647                 {
1648                   var = partition_to_var (cl->map, node->second_partition);
1649                   print_generic_expr (f, var, TDF_SLIM);
1650                   fprintf (f, "(%1d), ", node->cost);
1651                 }
1652               fprintf (f, "\n");
1653             }
1654         }
1655     }
1656   else
1657     {
1658       fprintf (f, "Sorted Coalesce list:\n");
1659       for (node = cl->list[0]; node; node = node->next)
1660         {
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");
1668         }
1669     }
1670 }
1671
1672
1673 /* Output tree_partition_associator object TPA to file F..  */
1674
1675 void
1676 tpa_dump (FILE *f, tpa_p tpa)
1677 {
1678   int x, i;
1679
1680   if (!tpa)
1681     return;
1682
1683   for (x = 0; x < tpa_num_trees (tpa); x++)
1684     {
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))
1690         {
1691           fprintf (f, "(%d)",i);
1692           print_generic_expr (f, partition_to_var (tpa->map, i), TDF_SLIM);
1693           fprintf (f, " ");
1694
1695 #ifdef ENABLE_CHECKING
1696           if (tpa_find_tree (tpa, i) != x)
1697             fprintf (f, "**find tree incorrectly set** ");
1698 #endif
1699
1700         }
1701       fprintf (f, ")\n");
1702     }
1703   fflush (f);
1704 }
1705
1706
1707 /* Output partition map MAP to file F.  */
1708
1709 void
1710 dump_var_map (FILE *f, var_map map)
1711 {
1712   int t;
1713   unsigned x, y;
1714   int p;
1715
1716   fprintf (f, "\nPartition map \n\n");
1717
1718   for (x = 0; x < map->num_partitions; x++)
1719     {
1720       if (map->compact_to_partition != NULL)
1721         p = map->compact_to_partition[x];
1722       else
1723         p = x;
1724
1725       if (map->partition_to_var[p] == NULL_TREE)
1726         continue;
1727
1728       t = 0;
1729       for (y = 1; y < num_ssa_names; y++)
1730         {
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)
1735             {
1736               if (t++ == 0)
1737                 {
1738                   fprintf(f, "Partition %d (", x);
1739                   print_generic_expr (f, partition_to_var (map, p), TDF_SLIM);
1740                   fprintf (f, " - ");
1741                 }
1742               fprintf (f, "%d ", y);
1743             }
1744         }
1745       if (t != 0)
1746         fprintf (f, ")\n");
1747     }
1748   fprintf (f, "\n");
1749 }
1750
1751
1752 /* Output live range info LIVE to file F, controlled by FLAG.  */
1753
1754 void
1755 dump_live_info (FILE *f, tree_live_info_p live, int flag)
1756 {
1757   basic_block bb;
1758   unsigned i;
1759   var_map map = live->map;
1760   bitmap_iterator bi;
1761
1762   if ((flag & LIVEDUMP_ENTRY) && live->livein)
1763     {
1764       FOR_EACH_BB (bb)
1765         {
1766           fprintf (f, "\nLive on entry to BB%d : ", bb->index);
1767           for (i = 0; i < num_var_partitions (map); i++)
1768             {
1769               if (bitmap_bit_p (live_entry_blocks (live, i), bb->index))
1770                 {
1771                   print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1772                   fprintf (f, "  ");
1773                 }
1774             }
1775           fprintf (f, "\n");
1776         }
1777     }
1778
1779   if ((flag & LIVEDUMP_EXIT) && live->liveout)
1780     {
1781       FOR_EACH_BB (bb)
1782         {
1783           fprintf (f, "\nLive on exit from BB%d : ", bb->index);
1784           EXECUTE_IF_SET_IN_BITMAP (live->liveout[bb->index], 0, i, bi)
1785             {
1786               print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1787               fprintf (f, "  ");
1788             }
1789           fprintf (f, "\n");
1790         }
1791     }
1792 }
1793
1794 #ifdef ENABLE_CHECKING
1795 void
1796 register_ssa_partition_check (tree ssa_var)
1797 {
1798   gcc_assert (TREE_CODE (ssa_var) == SSA_NAME);
1799   if (!is_gimple_reg (SSA_NAME_VAR (ssa_var)))
1800     {
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");
1805     }
1806 }
1807 #endif