OSDN Git Service

2004-05-13 Andrew Pinski <pinskia@physics.uc.edu>
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-live.c
1 /* Liveness for SSA trees.
2    Copyright (C) 2003 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-simple.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"
41
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);
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 its not a user variable, set the
137          root_var to this one.  */
138       if (!root_var || is_gimple_tmp_var (root_var))
139         {
140           other_var = root_var;
141           root_var = var2;
142         }
143       else 
144         other_var = var2;
145     }
146
147   if (p1 == NO_PARTITION || p2 == NO_PARTITION)
148     abort ();
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 x, limit, count, tmp, root, root_i;
189   tree var;
190   root_var_p rv = NULL;
191
192   limit = map->partition_size;
193   used = sbitmap_alloc (limit);
194   sbitmap_zero (used);
195
196   /* Already compressed? Abandon the old one.  */
197   if (map->partition_to_compact)
198     {
199       free (map->partition_to_compact);
200       map->partition_to_compact = NULL;
201     }
202   if (map->compact_to_partition)
203     {
204       free (map->compact_to_partition);
205       map->compact_to_partition = NULL;
206     }
207
208   map->num_partitions = map->partition_size;
209
210   if (flags & VARMAP_NO_SINGLE_DEFS)
211     rv = root_var_init (map);
212
213   map->partition_to_compact = (int *)xmalloc (limit * sizeof (int));
214   memset (map->partition_to_compact, 0xff, (limit * sizeof (int)));
215
216   /* Find out which partitions are actually referenced.  */
217   count = 0;
218   for (x = 0; x < limit; x++)
219     {
220       tmp = partition_find (map->var_partition, x);
221       if (!TEST_BIT (used, tmp) && map->partition_to_var[tmp] != NULL_TREE)
222         {
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)
226             {
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;
232             }
233           SET_BIT (used, tmp);
234           count++;
235         }
236     }
237
238   /* Build a compacted partitioning.  */
239   if (count != limit)
240     {
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,
245         {
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++;
252         });
253     }
254   else
255     {
256       free (map->partition_to_compact);
257       map->partition_to_compact = NULL;
258     }
259
260   map->num_partitions = count;
261
262   if (rv)
263     root_var_delete (rv);
264   sbitmap_free (used);
265 }
266
267
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.  */
271   
272 void 
273 change_partition_var (var_map map, tree var, int part)
274 {
275   var_ann_t ann;
276
277   if (TREE_CODE (var) == SSA_NAME)
278     abort();
279
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;
285 }
286
287
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.  */
291
292 var_map
293 create_ssa_var_map (int flags)
294 {
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   vdef_optype vdefs;
302   use_optype uses;
303   def_optype defs;
304   unsigned x;
305   var_map map;
306 #if defined ENABLE_CHECKING
307   sbitmap used_in_real_ops;
308   sbitmap used_in_virtual_ops;
309 #endif
310
311   map = init_var_map (highest_ssa_version + 1);
312
313 #if defined ENABLE_CHECKING
314   used_in_real_ops = sbitmap_alloc (num_referenced_vars);
315   sbitmap_zero (used_in_real_ops);
316
317   used_in_virtual_ops = sbitmap_alloc (num_referenced_vars);
318   sbitmap_zero (used_in_virtual_ops);
319 #endif
320
321   if (flags & SSA_VAR_MAP_REF_COUNT)
322     {
323       map->ref_count
324         = (int *)xmalloc (((highest_ssa_version + 1) * sizeof (int)));
325       memset (map->ref_count, 0, (highest_ssa_version + 1) * sizeof (int));
326     }
327
328   FOR_EACH_BB (bb)
329     {
330       tree phi, arg;
331       for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
332         {
333           int i;
334           register_ssa_partition (map, PHI_RESULT (phi), false);
335           for (i = 0; i < PHI_NUM_ARGS (phi); i++)
336             {
337               arg = PHI_ARG_DEF (phi, i);
338               if (TREE_CODE (arg) == SSA_NAME)
339                 register_ssa_partition (map, arg, true);
340             }
341         }
342
343       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
344         {
345           stmt = bsi_stmt (bsi);
346           get_stmt_operands (stmt);
347           ann = stmt_ann (stmt);
348
349           /* Register USE and DEF operands in each statement.  */
350           uses = USE_OPS (ann);
351           for (x = 0; x < NUM_USES (uses); x++)
352             {
353               use = USE_OP_PTR (uses, x);
354               register_ssa_partition (map, *use, true);
355
356 #if defined ENABLE_CHECKING
357               SET_BIT (used_in_real_ops, var_ann (SSA_NAME_VAR (*use))->uid);
358 #endif
359             }
360
361           defs = DEF_OPS (ann);
362           for (x = 0; x < NUM_DEFS (defs); x++)
363             {
364               dest = DEF_OP_PTR (defs, x);
365               register_ssa_partition (map, *dest, false);
366
367 #if defined ENABLE_CHECKING
368               SET_BIT (used_in_real_ops, var_ann (SSA_NAME_VAR (*dest))->uid);
369 #endif
370             }
371
372           /* While we do not care about virtual operands for
373              out of SSA, we do need to look at them to make sure
374              we mark all the variables which are used.  */
375           vuses = VUSE_OPS (ann);
376           for (x = 0; x < NUM_VUSES (vuses); x++)
377             {
378               tree var = VUSE_OP (vuses, x);
379               set_is_used (var);
380
381 #if defined ENABLE_CHECKING
382               SET_BIT (used_in_virtual_ops, var_ann (SSA_NAME_VAR (var))->uid);
383 #endif
384             }
385
386           vdefs = VDEF_OPS (ann);
387           for (x = 0; x < NUM_VDEFS (vdefs); x++)
388             {
389               tree var = VDEF_OP (vdefs, x);
390               set_is_used (var);
391
392 #if defined ENABLE_CHECKING
393               SET_BIT (used_in_virtual_ops, var_ann (SSA_NAME_VAR (var))->uid);
394 #endif
395             }
396         }
397     }
398
399 #if defined ENABLE_CHECKING
400   {
401     unsigned i;
402     sbitmap both = sbitmap_alloc (num_referenced_vars);
403     sbitmap_a_and_b (both, used_in_real_ops, used_in_virtual_ops);
404     if (sbitmap_first_set_bit (both) >= 0)
405       {
406         EXECUTE_IF_SET_IN_SBITMAP (both, 0, i,
407           fprintf (stderr, "Variable %s used in real and virtual operands\n",
408                    get_name (referenced_var (i))));
409         abort ();
410       }
411
412     sbitmap_free (used_in_real_ops);
413     sbitmap_free (used_in_virtual_ops);
414     sbitmap_free (both);
415   }
416 #endif
417
418   return map;
419 }
420
421
422 /* Allocate and return a new live range information object base on MAP.  */
423
424 static tree_live_info_p
425 new_tree_live_info (var_map map)
426 {
427   tree_live_info_p live;
428   int x;
429
430   live = (tree_live_info_p) xmalloc (sizeof (struct tree_live_info_d));
431   live->map = map;
432   live->num_blocks = last_basic_block;
433
434   live->global = BITMAP_XMALLOC ();
435
436   live->livein = (bitmap *)xmalloc (num_var_partitions (map) * sizeof (bitmap));
437   for (x = 0; x < num_var_partitions (map); x++)
438     live->livein[x] = BITMAP_XMALLOC ();
439
440   /* liveout is deferred until it is actually requested.  */
441   live->liveout = NULL;
442   return live;
443 }
444
445
446 /* Free storage for live range info object LIVE.  */
447
448 void 
449 delete_tree_live_info (tree_live_info_p live)
450 {
451   int x;
452   if (live->liveout)
453     {
454       for (x = live->num_blocks - 1; x >= 0; x--)
455         BITMAP_XFREE (live->liveout[x]);
456       free (live->liveout);
457     }
458   if (live->livein)
459     {
460       for (x = num_var_partitions (live->map) - 1; x >= 0; x--)
461         BITMAP_XFREE (live->livein[x]);
462       free (live->livein);
463     }
464   if (live->global)
465     BITMAP_XFREE (live->global);
466   
467   free (live);
468 }
469
470
471 /* Using LIVE, fill in all the live-on-entry blocks between the defs and uses 
472    for partition I.  STACK is a varray used for temporary memory which is 
473    passed in rather than being allocated on every call.  */
474
475 static void
476 live_worklist (tree_live_info_p live, varray_type stack, int i)
477 {
478   int b;
479   tree var;
480   basic_block def_bb = NULL;
481   edge e;
482   var_map map = live->map;
483
484   var = partition_to_var (map, i);
485   if (SSA_NAME_DEF_STMT (var))
486     def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (var));
487
488   EXECUTE_IF_SET_IN_BITMAP (live->livein[i], 0, b,
489     {
490       VARRAY_PUSH_INT (stack, b);
491     });
492
493   while (VARRAY_ACTIVE_SIZE (stack) > 0)
494     {
495       b = VARRAY_TOP_INT (stack);
496       VARRAY_POP (stack);
497
498       for (e = BASIC_BLOCK (b)->pred; e; e = e->pred_next)
499         if (e->src != ENTRY_BLOCK_PTR)
500           {
501             /* Its not live on entry to the block its defined in.  */
502             if (e->src == def_bb)
503               continue;
504             if (!bitmap_bit_p (live->livein[i], e->src->index))
505               {
506                 bitmap_set_bit (live->livein[i], e->src->index);
507                 VARRAY_PUSH_INT (stack, e->src->index);
508               }
509           }
510     }
511 }
512
513
514 /* If VAR is in a partition of MAP, set the bit for that partition in VEC.  */
515
516 static inline void
517 set_if_valid (var_map map, bitmap vec, tree var)
518 {
519   int p = var_to_partition (map, var);
520   if (p != NO_PARTITION)
521     bitmap_set_bit (vec, p);
522 }
523
524
525 /* If VAR is in a partition and it isn't defined in DEF_VEC, set the livein and 
526    global bit for it in the LIVE object.  BB is the block being processed.  */
527
528 static inline void
529 add_livein_if_notdef (tree_live_info_p live, bitmap def_vec,
530                       tree var, basic_block bb)
531 {
532   int p = var_to_partition (live->map, var);
533   if (p == NO_PARTITION || bb == ENTRY_BLOCK_PTR)
534     return;
535   if (!bitmap_bit_p (def_vec, p))
536     {
537       bitmap_set_bit (live->livein[p], bb->index);
538       bitmap_set_bit (live->global, p);
539     }
540 }
541
542
543 /* Given partition map MAP, calculate all the live on entry bitmaps for 
544    each basic block.  Return a live info object.  */
545
546 tree_live_info_p 
547 calculate_live_on_entry (var_map map)
548 {
549   tree_live_info_p live;
550   int num, i;
551   basic_block bb;
552   bitmap saw_def;
553   tree phi, var, stmt;
554   tree *vec;
555   edge e;
556   varray_type stack;
557   block_stmt_iterator bsi;
558   vuse_optype vuses;
559   vdef_optype vdefs;
560   use_optype uses;
561   def_optype defs;
562   stmt_ann_t ann;
563
564   saw_def = BITMAP_XMALLOC ();
565
566   live = new_tree_live_info (map);
567
568   FOR_EACH_BB (bb)
569     {
570       bitmap_clear (saw_def);
571
572       for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
573         {
574           for (i = 0; i < PHI_NUM_ARGS (phi); i++)
575             {
576               var = PHI_ARG_DEF (phi, i);
577               if (!phi_ssa_name_p (var))
578                 continue;
579               stmt = SSA_NAME_DEF_STMT (var);
580               e = PHI_ARG_EDGE (phi, i);
581
582               /* Any uses in PHIs which either don't have def's or are not
583                  defined in the block from which the def comes, will be live
584                  on entry to that block.  */
585               if (!stmt || e->src != bb_for_stmt (stmt))
586                 add_livein_if_notdef (live, saw_def, var, e->src);
587             }
588         }
589
590       /* Don't mark PHI results as defined until all the PHI nodes have
591          been processed. If the PHI sequence is:
592             a_3 = PHI <a_1, a_2>
593             b_3 = PHI <b_1, a_3>
594          The a_3 referred to in b_3's PHI node is the one incoming on the
595          edge, *not* the PHI node just seen.  */
596
597       for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
598         {
599           var = PHI_RESULT (phi);
600           set_if_valid (map, saw_def, var);
601         }
602
603       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
604         {
605           stmt = bsi_stmt (bsi);
606           get_stmt_operands (stmt);
607           ann = stmt_ann (stmt);
608
609           uses = USE_OPS (ann);
610           num = NUM_USES (uses);
611           for (i = 0; i < num; i++)
612             {
613               vec = USE_OP_PTR (uses, i);
614               add_livein_if_notdef (live, saw_def, *vec, bb);
615             }
616
617           vuses = VUSE_OPS (ann);
618           num = NUM_VUSES (vuses);
619           for (i = 0; i < num; i++)
620             {
621               var = VUSE_OP (vuses, i);
622               add_livein_if_notdef (live, saw_def, var, bb);
623             }
624
625           vdefs = VDEF_OPS (ann);
626           num = NUM_VDEFS (vdefs);
627           for (i = 0; i < num; i++)
628             {
629               var = VDEF_OP (vdefs, i);
630               add_livein_if_notdef (live, saw_def, var, bb);
631             }
632
633           defs = DEF_OPS (ann);
634           num = NUM_DEFS (defs);
635           for (i = 0; i < num; i++)
636             {
637               vec = DEF_OP_PTR (defs, i);
638               set_if_valid (map, saw_def, *vec);
639             }
640
641           num = NUM_VDEFS (vdefs);
642           for (i = 0; i < num; i++)
643             {
644               var = VDEF_RESULT (vdefs, i);
645               set_if_valid (map, saw_def, var);
646             }
647         }
648     }
649
650   VARRAY_INT_INIT (stack, last_basic_block, "stack");
651   EXECUTE_IF_SET_IN_BITMAP (live->global, 0, i,
652     {
653       live_worklist (live, stack, i);
654     });
655
656 #ifdef ENABLE_CHECKING
657    /* Check for live on entry partitions and report those with a DEF in
658       the program. This will typically mean an optimization has done
659       something wrong.  */
660
661   bb = ENTRY_BLOCK_PTR;
662   num = 0;
663   for (e = bb->succ; e; e = e->succ_next)
664     {
665       int entry_block = e->dest->index;
666       if (e->dest == EXIT_BLOCK_PTR)
667         continue;
668       for (i = 0; i < num_var_partitions (map); i++)
669         {
670           basic_block tmp;
671           tree d;
672           var = partition_to_var (map, i);
673           stmt = SSA_NAME_DEF_STMT (var);
674           tmp = bb_for_stmt (stmt);
675           d = default_def (SSA_NAME_VAR (var));
676
677           if (bitmap_bit_p (live_entry_blocks (live, i), entry_block))
678             {
679               if (!IS_EMPTY_STMT (stmt))
680                 {
681                   num++;
682                   print_generic_expr (stderr, var, TDF_SLIM);
683                   fprintf (stderr, " is defined ");
684                   if (tmp)
685                     fprintf (stderr, " in BB%d, ", tmp->index);
686                   fprintf (stderr, "by:\n");
687                   print_generic_expr (stderr, stmt, TDF_SLIM);
688                   fprintf (stderr, "\nIt is also live-on-entry to entry BB %d", 
689                            entry_block);
690                   fprintf (stderr, " So it appears to have multiple defs.\n");
691                 }
692               else
693                 {
694                   if (d != var)
695                     {
696                       num++;
697                       print_generic_expr (stderr, var, TDF_SLIM);
698                       fprintf (stderr, " is live-on-entry to BB%d ",entry_block);
699                       if (d)
700                         {
701                           fprintf (stderr, " but is not the default def of ");
702                           print_generic_expr (stderr, d, TDF_SLIM);
703                           fprintf (stderr, "\n");
704                         }
705                       else
706                         fprintf (stderr, " and there is no default def.\n");
707                     }
708                 }
709             }
710           else
711             if (d == var)
712               {
713                 /* The only way this var shouldn't be marked live on entry is 
714                    if it occurs in a PHI argument of the block.  */
715                 int z, ok = 0;
716                 for (phi = phi_nodes (e->dest); 
717                      phi && !ok; 
718                      phi = TREE_CHAIN (phi))
719                   {
720                     for (z = 0; z < PHI_NUM_ARGS (phi); z++)
721                       if (var == PHI_ARG_DEF (phi, z))
722                         {
723                           ok = 1;
724                           break;
725                         }
726                   }
727                 if (ok)
728                   continue;
729                 num++;
730                 print_generic_expr (stderr, var, TDF_SLIM);
731                 fprintf (stderr, " is not marked live-on-entry to entry BB%d ", 
732                          entry_block);
733                 fprintf (stderr, "but it is a default def so it should be.\n");
734               }
735         }
736     }
737   if (num > 0)
738     abort ();
739 #endif
740
741   BITMAP_XFREE (saw_def);
742
743   return live;
744 }
745
746
747 /* Calculate the live on exit vectors based on the entry info in LIVEINFO.  */
748
749 void
750 calculate_live_on_exit (tree_live_info_p liveinfo)
751 {
752   unsigned b;
753   int i, x;
754   bitmap *on_exit;
755   basic_block bb;
756   edge e;
757   tree t, phi;
758   bitmap on_entry;
759   var_map map = liveinfo->map;
760
761   on_exit = (bitmap *)xmalloc (last_basic_block * sizeof (bitmap));
762   for (x = 0; x < last_basic_block; x++)
763     on_exit[x] = BITMAP_XMALLOC ();
764
765   /* Set all the live-on-exit bits for uses in PHIs.  */
766   FOR_EACH_BB (bb)
767     {
768       for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
769         for (i = 0; i < PHI_NUM_ARGS (phi); i++)
770           { 
771             t = PHI_ARG_DEF (phi, i);
772             e = PHI_ARG_EDGE (phi, i);
773             if (!phi_ssa_name_p (t) || e->src == ENTRY_BLOCK_PTR)
774               continue;
775             set_if_valid (map, on_exit[e->src->index], t);
776           }
777     }
778
779   /* Set live on exit for all predecessors of live on entry's.  */
780   for (i = 0; i < num_var_partitions (map); i++)
781     {
782       on_entry = live_entry_blocks (liveinfo, i);
783       EXECUTE_IF_SET_IN_BITMAP (on_entry, 0, b,
784         {
785           for (e = BASIC_BLOCK(b)->pred; e; e = e->pred_next)
786             if (e->src != ENTRY_BLOCK_PTR)
787               bitmap_set_bit (on_exit[e->src->index], i);
788         });
789     }
790
791   liveinfo->liveout = on_exit;
792 }
793
794
795 /* Initialize a tree_partition_associator object using MAP.  */
796
797 tpa_p
798 tpa_init (var_map map)
799 {
800   tpa_p tpa;
801   int num_partitions = num_var_partitions (map);
802   int x;
803
804   if (num_partitions == 0)
805     return NULL;
806
807   tpa = (tpa_p) xmalloc (sizeof (struct tree_partition_associator_d));
808   tpa->num_trees = 0;
809   tpa->uncompressed_num = -1;
810   tpa->map = map;
811   tpa->next_partition = (int *)xmalloc (num_partitions * sizeof (int));
812   memset (tpa->next_partition, TPA_NONE, num_partitions * sizeof (int));
813
814   tpa->partition_to_tree_map = (int *)xmalloc (num_partitions * sizeof (int));
815   memset (tpa->partition_to_tree_map, TPA_NONE, num_partitions * sizeof (int));
816
817   x = MAX (40, (num_partitions / 20));
818   VARRAY_TREE_INIT (tpa->trees, x, "trees");
819   VARRAY_INT_INIT (tpa->first_partition, x, "first_partition");
820
821   return tpa;
822
823 }
824
825
826 /* Remove PARTITION_INDEX from TREE_INDEX's list in the tpa structure TPA.  */
827
828 void
829 tpa_remove_partition (tpa_p tpa, int tree_index, int partition_index)
830 {
831   int i;
832
833   i = tpa_first_partition (tpa, tree_index);
834   if (i == partition_index)
835     {
836       VARRAY_INT (tpa->first_partition, tree_index) = tpa->next_partition[i];
837     }
838   else
839     {
840       for ( ; i != TPA_NONE; i = tpa_next_partition (tpa, i))
841         {
842           if (tpa->next_partition[i] == partition_index)
843             {
844               tpa->next_partition[i] = tpa->next_partition[partition_index];
845               break;
846             }
847         }
848     }
849 }
850
851
852 /* Free the memory used by tree_partition_associator object TPA.  */
853
854 void
855 tpa_delete (tpa_p tpa)
856 {
857   if (!tpa)
858     return;
859
860   free (tpa->partition_to_tree_map);
861   free (tpa->next_partition);
862   free (tpa);
863 }
864
865
866 /* This function will remove any tree entires from TPA which have only a single
867    element.  This will help keep the size of the conflict graph down.  The 
868    function returns the number of remaining tree lists.  */
869
870 int 
871 tpa_compact (tpa_p tpa)
872 {
873   int last, x, y, first, swap_i;
874   tree swap_t;
875
876   /* Find the last list which has more than 1 partition.  */
877   for (last = tpa->num_trees - 1; last > 0; last--)
878     {
879       first = tpa_first_partition (tpa, last);
880       if (tpa_next_partition (tpa, first) != NO_PARTITION)
881         break;
882     }
883
884   x = 0;
885   while (x < last)
886     {
887       first = tpa_first_partition (tpa, x);
888
889       /* If there is not more than one partition, swap with the current end
890          of the tree list.  */
891       if (tpa_next_partition (tpa, first) == NO_PARTITION)
892         {
893           swap_t = VARRAY_TREE (tpa->trees, last);
894           swap_i = VARRAY_INT (tpa->first_partition, last);
895
896           /* Update the last entry. Since it is known to only have one
897              partition, there is nothing else to update.  */
898           VARRAY_TREE (tpa->trees, last) = VARRAY_TREE (tpa->trees, x);
899           VARRAY_INT (tpa->first_partition, last) 
900             = VARRAY_INT (tpa->first_partition, x);
901           tpa->partition_to_tree_map[tpa_first_partition (tpa, last)] = last;
902
903           /* Since this list is known to have more than one partition, update
904              the list owner entries.  */
905           VARRAY_TREE (tpa->trees, x) = swap_t;
906           VARRAY_INT (tpa->first_partition, x) = swap_i;
907           for (y = tpa_first_partition (tpa, x); 
908                y != NO_PARTITION; 
909                y = tpa_next_partition (tpa, y))
910             tpa->partition_to_tree_map[y] = x;
911
912           /* Ensure last is a list with more than one partition.  */
913           last--;
914           for (; last > x; last--)
915             {
916               first = tpa_first_partition (tpa, last);
917               if (tpa_next_partition (tpa, first) != NO_PARTITION)
918                 break;
919             }
920         }
921       x++;
922     }
923
924   first = tpa_first_partition (tpa, x);
925   if (tpa_next_partition (tpa, first) != NO_PARTITION)
926     x++;
927   tpa->uncompressed_num = tpa->num_trees;
928   tpa->num_trees = x;
929   return last;
930 }
931
932
933 /* Initialize a root_var object with SSA partitions from MAP which are based 
934    on each root variable.  */
935
936 root_var_p
937 root_var_init (var_map map)
938 {
939   root_var_p rv;
940   int num_partitions = num_var_partitions (map);
941   int x, p;
942   tree t;
943   var_ann_t ann;
944   sbitmap seen;
945
946   rv = tpa_init (map);
947   if (!rv)
948     return NULL;
949
950   seen = sbitmap_alloc (num_partitions);
951   sbitmap_zero (seen);
952
953   /* Start at the end and work towards the front. This will provide a list
954      that is ordered from smallest to largest.  */
955   for (x = num_partitions - 1; x >= 0; x--)
956     {
957       t = partition_to_var (map, x);
958
959       /* The var map may not be compacted yet, so check for NULL.  */
960       if (!t) 
961         continue;
962
963       p = var_to_partition (map, t);
964
965 #ifdef ENABLE_CHECKING
966       if (p == NO_PARTITION)
967         abort ();
968 #endif
969
970       /* Make sure we only put coalesced partitions into the list once.  */
971       if (TEST_BIT (seen, p))
972         continue;
973       SET_BIT (seen, p);
974       if (TREE_CODE (t) == SSA_NAME)
975         t = SSA_NAME_VAR (t);
976       ann = var_ann (t);
977       if (ann->root_var_processed)
978         {
979           rv->next_partition[p] = VARRAY_INT (rv->first_partition, 
980                                               VAR_ANN_ROOT_INDEX (ann));
981           VARRAY_INT (rv->first_partition, VAR_ANN_ROOT_INDEX (ann)) = p;
982         }
983       else
984         {
985           ann->root_var_processed = 1;
986           VAR_ANN_ROOT_INDEX (ann) = rv->num_trees++;
987           VARRAY_PUSH_TREE (rv->trees, t);
988           VARRAY_PUSH_INT (rv->first_partition, p);
989         }
990       rv->partition_to_tree_map[p] = VAR_ANN_ROOT_INDEX (ann);
991     }
992
993   /* Reset the out_of_ssa_tag flag on each variable for later use.  */
994   for (x = 0; x < rv->num_trees; x++)
995     {
996       t = VARRAY_TREE (rv->trees, x);
997       var_ann (t)->root_var_processed = 0;
998     }
999
1000   sbitmap_free (seen);
1001   return rv;
1002 }
1003
1004
1005 /* Initialize a type_var structure which associates all the partitions in MAP 
1006    of the same type to the type node's index.  Volatiles are ignored.  */
1007
1008 type_var_p
1009 type_var_init (var_map map)
1010 {
1011   type_var_p tv;
1012   int x, y, p;
1013   int num_partitions = num_var_partitions (map);
1014   tree t;
1015   sbitmap seen;
1016
1017   seen = sbitmap_alloc (num_partitions);
1018   sbitmap_zero (seen);
1019
1020   tv = tpa_init (map);
1021   if (!tv)
1022     return NULL;
1023
1024   for (x = num_partitions - 1; x >= 0; x--)
1025     {
1026       t = partition_to_var (map, x);
1027
1028       /* Disallow coalescing of these types of variables.  */
1029       if (!t
1030           || TREE_THIS_VOLATILE (t)
1031           || TREE_CODE (t) == RESULT_DECL
1032           || TREE_CODE (t) == PARM_DECL 
1033           || (DECL_P (t)
1034               && (DECL_REGISTER (t)
1035                   || !DECL_ARTIFICIAL (t)
1036                   || DECL_RTL_SET_P (t))))
1037         continue;
1038
1039       p = var_to_partition (map, t);
1040
1041 #ifdef ENABLE_CHECKING
1042       if (p == NO_PARTITION)
1043         abort ();
1044 #endif
1045
1046       /* If partitions have been coalesced, only add the representative 
1047          for the partition to the list once.  */
1048       if (TEST_BIT (seen, p))
1049         continue;
1050       SET_BIT (seen, p);
1051       t = TREE_TYPE (t);
1052
1053       /* Find the list for this type.  */
1054       for (y = 0; y < tv->num_trees; y++)
1055         if (t == VARRAY_TREE (tv->trees, y))
1056           break;
1057       if (y == tv->num_trees)
1058         {
1059           tv->num_trees++;
1060           VARRAY_PUSH_TREE (tv->trees, t);
1061           VARRAY_PUSH_INT (tv->first_partition, p);
1062         }
1063       else
1064         {
1065           tv->next_partition[p] = VARRAY_INT (tv->first_partition, y);
1066           VARRAY_INT (tv->first_partition, y) = p;
1067         }
1068       tv->partition_to_tree_map[p] = y;
1069     }
1070   sbitmap_free (seen);
1071   return tv;
1072 }
1073
1074
1075 /* Create a new coalesce list object from MAP and return it.  */
1076
1077 coalesce_list_p 
1078 create_coalesce_list (var_map map)
1079 {
1080   coalesce_list_p list;
1081
1082   list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
1083
1084   list->map = map;
1085   list->add_mode = true;
1086   list->list = (partition_pair_p *) xcalloc (num_var_partitions (map),
1087                                              sizeof (struct partition_pair_d));
1088   return list;
1089 }
1090
1091
1092 /* Delete coalesce list CL.  */
1093
1094 void 
1095 delete_coalesce_list (coalesce_list_p cl)
1096 {
1097   free (cl->list);
1098   free (cl);
1099 }
1100
1101
1102 /* Find a matching coalesce pair object in CL for partitions P1 and P2.  If 
1103    one isn't found, return NULL if CREATE is false, otherwise create a new 
1104    coalesce pair object and return it.  */
1105
1106 static partition_pair_p
1107 find_partition_pair (coalesce_list_p cl, int p1, int p2, bool create)
1108 {
1109   partition_pair_p node, tmp;
1110   int s;
1111     
1112   /* Normalize so that p1 is the smaller value.  */
1113   if (p2 < p1)
1114     {
1115       s = p1;
1116       p1 = p2;
1117       p2 = s;
1118     }
1119   
1120   tmp = NULL;
1121
1122   /* The list is sorted such that if we find a value greater than p2,
1123      p2 is not in the list.  */
1124   for (node = cl->list[p1]; node; node = node->next)
1125     {
1126       if (node->second_partition == p2)
1127         return node;
1128       else
1129         if (node->second_partition > p2)
1130           break;
1131      tmp = node;
1132     }
1133
1134   if (!create)
1135     return NULL;
1136
1137   node = (partition_pair_p) xmalloc (sizeof (struct partition_pair_d));
1138   node->first_partition = p1;
1139   node->second_partition = p2;
1140   node->cost = 0;
1141     
1142   if (tmp != NULL)
1143     {
1144       node->next = tmp->next;
1145       tmp->next = node;
1146     }
1147   else
1148     {
1149       /* This is now the first node in the list.  */
1150       node->next = cl->list[p1];
1151       cl->list[p1] = node;
1152     }
1153
1154   return node;
1155 }
1156
1157
1158 /* Add a potential coalesce between P1 and P2 in CL with a cost of VALUE.  */
1159
1160 void 
1161 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
1162 {
1163   partition_pair_p node;
1164
1165 #ifdef ENABLE_CHECKING
1166   if (!cl->add_mode)
1167     abort();
1168 #endif
1169
1170   if (p1 == p2)
1171     return;
1172
1173   node = find_partition_pair (cl, p1, p2, true);
1174
1175   node->cost += value;
1176 }
1177
1178
1179 /* Comparison function to allow qsort to sort P1 and P2 in descending order.  */
1180
1181 static
1182 int compare_pairs (const void *p1, const void *p2)
1183 {
1184   return (*(partition_pair_p *)p2)->cost - (*(partition_pair_p *)p1)->cost;
1185 }
1186
1187
1188 /* Prepare CL for removal of preferred pairs.  When finished, list element 
1189    0 has all the coalesce pairs, sorted in order from most important coalesce 
1190    to least important.  */
1191
1192 void
1193 sort_coalesce_list (coalesce_list_p cl)
1194 {
1195   int x, num, count;
1196   partition_pair_p chain, p;
1197   partition_pair_p  *list;
1198
1199   if (!cl->add_mode)
1200     abort();
1201
1202   cl->add_mode = false;
1203
1204   /* Compact the array of lists to a single list, and count the elements.  */
1205   num = 0;
1206   chain = NULL;
1207   for (x = 0; x < num_var_partitions (cl->map); x++)
1208     if (cl->list[x] != NULL)
1209       {
1210         for (p = cl->list[x]; p->next != NULL; p = p->next)
1211           num++;
1212         num++;
1213         p->next = chain;
1214         chain = cl->list[x];
1215         cl->list[x] = NULL;
1216       }
1217
1218   /* Only call qsort if there are more than 2 items.  */
1219   if (num > 2)
1220     {
1221       list = xmalloc (sizeof (partition_pair_p) * num);
1222       count = 0;
1223       for (p = chain; p != NULL; p = p->next)
1224         list[count++] = p;
1225
1226 #ifdef ENABLE_CHECKING
1227   if (count != num)
1228     abort ();
1229 #endif
1230         
1231       qsort (list, count, sizeof (partition_pair_p), compare_pairs);
1232
1233       p = list[0];
1234       for (x = 1; x < num; x++)
1235         {
1236           p->next = list[x];
1237           p = list[x];
1238         }
1239       p->next = NULL;
1240       cl->list[0] = list[0];
1241       free (list);
1242     }
1243   else
1244     {
1245       cl->list[0] = chain;
1246       if (num == 2)
1247         {
1248           /* Simply swap the two elements if they are in the wrong order.  */
1249           if (chain->cost < chain->next->cost)
1250             {
1251               cl->list[0] = chain->next;
1252               cl->list[0]->next = chain;
1253               chain->next = NULL;
1254             }
1255         }
1256     }
1257 }
1258
1259
1260 /* Retrieve the best remaining pair to coalesce from CL.  Returns the 2 
1261    partitions via P1 and P2.  Their calculated cost is returned by the function.
1262    NO_BEST_COALESCE is returned if the coalesce list is empty.  */
1263
1264 int 
1265 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
1266 {
1267   partition_pair_p node;
1268   int ret;
1269
1270   if (cl->add_mode)
1271     abort();
1272
1273   node = cl->list[0];
1274   if (!node)
1275     return NO_BEST_COALESCE;
1276
1277   cl->list[0] = node->next;
1278
1279   *p1 = node->first_partition;
1280   *p2 = node->second_partition;
1281   ret = node->cost;
1282   free (node);
1283
1284   return ret;
1285 }
1286
1287
1288 /* If variable VAR is in a partition in MAP, add a conflict in GRAPH between 
1289    VAR and any other live partitions in VEC which are associated via TPA.  
1290    Reset the live bit in VEC.  */
1291
1292 static inline void 
1293 add_conflicts_if_valid (tpa_p tpa, conflict_graph graph,
1294                         var_map map, bitmap vec, tree var)
1295
1296   int p, y, first;
1297   p = var_to_partition (map, var);
1298   if (p != NO_PARTITION)
1299     { 
1300       bitmap_clear_bit (vec, p);
1301       first = tpa_find_tree (tpa, p);
1302       /* If find returns nothing, this object isn't interesting.  */
1303       if (first == TPA_NONE)
1304         return;
1305       /* Only add interferences between objects in the same list.  */
1306       for (y = tpa_first_partition (tpa, first);
1307            y != TPA_NONE;
1308            y = tpa_next_partition (tpa, y))
1309         {
1310           if (bitmap_bit_p (vec, y))
1311             conflict_graph_add (graph, p, y);
1312         }
1313     }
1314 }
1315
1316
1317 /* Return a conflict graph for the information contained in LIVE_INFO.  Only
1318    conflicts between items in the same TPA list are added.  If optional 
1319    coalesce list CL is passed in, any copies encountered are added.  */
1320
1321 conflict_graph
1322 build_tree_conflict_graph (tree_live_info_p liveinfo, tpa_p tpa, 
1323                            coalesce_list_p cl)
1324 {
1325   conflict_graph graph;
1326   var_map map;
1327   bitmap live;
1328   int num, x, y, i;
1329   basic_block bb;
1330   varray_type partition_link, tpa_to_clear, tpa_nodes;
1331   def_optype defs;
1332   use_optype uses;
1333   unsigned l;
1334
1335   map = live_var_map (liveinfo);
1336   graph = conflict_graph_new (num_var_partitions (map));
1337
1338   if (tpa_num_trees (tpa) == 0)
1339     return graph;
1340
1341   live = BITMAP_XMALLOC ();
1342
1343   VARRAY_INT_INIT (partition_link, num_var_partitions (map) + 1, "part_link");
1344   VARRAY_INT_INIT (tpa_nodes, tpa_num_trees (tpa), "tpa nodes");
1345   VARRAY_INT_INIT (tpa_to_clear, 50, "tpa to clear");
1346
1347   FOR_EACH_BB (bb)
1348     {
1349       block_stmt_iterator bsi;
1350       tree phi;
1351
1352       /* Start with live on exit temporaries.  */
1353       bitmap_copy (live, live_on_exit (liveinfo, bb));
1354
1355       for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_prev (&bsi))
1356         {
1357           bool is_a_copy = false;
1358           tree stmt = bsi_stmt (bsi);
1359           stmt_ann_t ann;
1360
1361           get_stmt_operands (stmt);
1362           ann = stmt_ann (stmt);
1363
1364           /* A copy between 2 partitions does not introduce an interference 
1365              by itself.  If they did, you would never be able to coalesce 
1366              two things which are copied.  If the two variables really do 
1367              conflict, they will conflict elsewhere in the program.  
1368              
1369              This is handled specially here since we may also be interested 
1370              in copies between real variables and SSA_NAME variables.  We may
1371              be interested in trying to coalesce SSA_NAME variables with
1372              root variables in some cases.   */
1373
1374           if (TREE_CODE (stmt) == MODIFY_EXPR)
1375             {
1376               tree lhs = TREE_OPERAND (stmt, 0);
1377               tree rhs = TREE_OPERAND (stmt, 1);
1378               int p1, p2;
1379               int bit;
1380
1381               if (DECL_P (lhs) || TREE_CODE (lhs) == SSA_NAME)
1382                 p1 = var_to_partition (map, lhs);
1383               else 
1384                 p1 = NO_PARTITION;
1385
1386               if (DECL_P (rhs) || TREE_CODE (rhs) == SSA_NAME)
1387                 p2 = var_to_partition (map, rhs);
1388               else 
1389                 p2 = NO_PARTITION;
1390
1391               if (p1 != NO_PARTITION && p2 != NO_PARTITION)
1392                 {
1393                   is_a_copy = true;
1394                   bit = bitmap_bit_p (live, p2);
1395                   /* If the RHS is live, make it not live while we add
1396                      the conflicts, then make it live again.  */
1397                   if (bit)
1398                     bitmap_clear_bit (live, p2);
1399                   add_conflicts_if_valid (tpa, graph, map, live, lhs);
1400                   if (bit)
1401                     bitmap_set_bit (live, p2);
1402                   if (cl)
1403                     add_coalesce (cl, p1, p2, 1);
1404                   set_if_valid (map, live, rhs);
1405                 }
1406             }
1407
1408           if (!is_a_copy)
1409             {
1410               tree *var_p;
1411
1412               defs = DEF_OPS (ann);
1413               num = NUM_DEFS (defs);
1414               for (x = 0; x < num; x++)
1415                 {
1416                   var_p = DEF_OP_PTR (defs, x);
1417                   add_conflicts_if_valid (tpa, graph, map, live, *var_p);
1418                 }
1419
1420               uses = USE_OPS (ann);
1421               num = NUM_USES (uses);
1422               for (x = 0; x < num; x++)
1423                 {
1424                   var_p = USE_OP_PTR (uses, x);
1425                   set_if_valid (map, live, *var_p);
1426                 }
1427             }
1428         }
1429
1430       /* If result of a PHI is unused, then the loops over the statements
1431          will not record any conflicts.  However, since the PHI node is 
1432          going to be translated out of SSA form we must record a conflict
1433          between the result of the PHI and any variables with are live. 
1434          Otherwise the out-of-ssa translation may create incorrect code.  */
1435       for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
1436         {
1437           tree result = PHI_RESULT (phi);
1438           int p = var_to_partition (map, result);
1439
1440           if (p != NO_PARTITION && ! bitmap_bit_p (live, p))
1441             add_conflicts_if_valid (tpa, graph, map, live, result);
1442         }
1443
1444       /* Anything which is still live at this point interferes.  
1445          In order to implement this efficiently, only conflicts between
1446          partitions which have the same TPA root need be added.
1447          TPA roots which have been seen are tracked in 'tpa_nodes'.  A non-zero
1448          entry points to an index into 'partition_link', which then indexes 
1449          into itself forming a linked list of partitions sharing a tpa root 
1450          which have been seen as live up to this point.  Since partitions start
1451          at index zero, all entries in partition_link are (partition + 1).
1452
1453          Conflicts are added between the current partition and any already seen.
1454          tpa_clear contains all the tpa_roots processed, and these are the only
1455          entries which need to be zero'd out for a clean restart.  */
1456
1457       EXECUTE_IF_SET_IN_BITMAP (live, 0, x,
1458         {
1459           i = tpa_find_tree (tpa, x);
1460           if (i != TPA_NONE)
1461             {
1462               int start = VARRAY_INT (tpa_nodes, i);
1463               /* If start is 0, a new root reference list is being started.
1464                  Register it to be cleared.  */
1465               if (!start)
1466                 VARRAY_PUSH_INT (tpa_to_clear, i);
1467
1468               /* Add interferences to other tpa members seen.  */
1469               for (y = start; y != 0; y = VARRAY_INT (partition_link, y))
1470                 conflict_graph_add (graph, x, y - 1);
1471               VARRAY_INT (tpa_nodes, i) = x + 1;
1472               VARRAY_INT (partition_link, x + 1) = start;
1473             }
1474         });
1475
1476         /* Now clear the used tpa root references.  */
1477         for (l = 0; l < VARRAY_ACTIVE_SIZE (tpa_to_clear); l++)
1478           VARRAY_INT (tpa_nodes, VARRAY_INT (tpa_to_clear, l)) = 0;
1479         VARRAY_POP_ALL (tpa_to_clear);
1480     }
1481
1482   BITMAP_XFREE (live);
1483   return graph;
1484 }
1485
1486
1487 /* This routine will attempt to coalesce the elements in TPA subject to the
1488    conflicts found in GRAPH.  If optional coalesce_list CL is provided, 
1489    only coalesces specified within the coalesce list are attempted.  Otherwise 
1490    an attempt is made to coalesce as many partitions within each TPA grouping 
1491    as possible.  If DEBUG is provided, debug output will be sent there.  */
1492
1493 void
1494 coalesce_tpa_members (tpa_p tpa, conflict_graph graph, var_map map, 
1495                       coalesce_list_p cl, FILE *debug)
1496 {
1497   int x, y, z, w;
1498   tree var, tmp;
1499
1500   /* Attempt to coalesce any items in a coalesce list.  */
1501   if (cl)
1502     {
1503       while (pop_best_coalesce (cl, &x, &y) != NO_BEST_COALESCE)
1504         {
1505           if (debug)
1506             {
1507               fprintf (debug, "Coalesce list: (%d)", x);
1508               print_generic_expr (debug, partition_to_var (map, x), TDF_SLIM);
1509               fprintf (debug, " & (%d)", y);
1510               print_generic_expr (debug, partition_to_var (map, y), TDF_SLIM);
1511             }
1512
1513           w = tpa_find_tree (tpa, x);
1514           z = tpa_find_tree (tpa, y);
1515           if (w != z || w == TPA_NONE || z == TPA_NONE)
1516             {
1517               if (debug)
1518                 {
1519                   if (w != z)
1520                     fprintf (debug, ": Fail, Non-matching TPA's\n");
1521                   if (w == TPA_NONE)
1522                     fprintf (debug, ": Fail %d non TPA.\n", x);
1523                   else
1524                     fprintf (debug, ": Fail %d non TPA.\n", y);
1525                 }
1526               continue;
1527             }
1528           var = partition_to_var (map, x);
1529           tmp = partition_to_var (map, y);
1530           x = var_to_partition (map, var);
1531           y = var_to_partition (map, tmp);
1532           if (debug)
1533             fprintf (debug, " [map: %d, %d] ", x, y);
1534           if (x == y)
1535             {
1536               if (debug)
1537                 fprintf (debug, ": Already Coalesced.\n");
1538               continue;
1539             }
1540           if (!conflict_graph_conflict_p (graph, x, y))
1541             {
1542               z = var_union (map, var, tmp);
1543               if (z == NO_PARTITION)
1544                 {
1545                   if (debug)
1546                     fprintf (debug, ": Unable to perform partition union.\n");
1547                   continue;
1548                 }
1549
1550               /* z is the new combined partition. We need to remove the other
1551                  partition from the list. Set x to be that other partition.  */
1552               if (z == x)
1553                 {
1554                   conflict_graph_merge_regs (graph, x, y);
1555                   w = tpa_find_tree (tpa, y);
1556                   tpa_remove_partition (tpa, w, y);
1557                 }
1558               else
1559                 {
1560                   conflict_graph_merge_regs (graph, y, x);
1561                   w = tpa_find_tree (tpa, x);
1562                   tpa_remove_partition (tpa, w, x);
1563                 }
1564
1565               if (debug)
1566                 fprintf (debug, ": Success -> %d\n", z);
1567             }
1568           else
1569             if (debug)
1570               fprintf (debug, ": Fail due to conflict\n");
1571         }
1572       /* If using a coalesce list, don't try to coalesce anything else.  */
1573       return;
1574     }
1575
1576   for (x = 0; x < tpa_num_trees (tpa); x++)
1577     {
1578       while (tpa_first_partition (tpa, x) != TPA_NONE)
1579         {
1580           int p1, p2;
1581           /* Coalesce first partition with anything that doesn't conflict.  */
1582           y = tpa_first_partition (tpa, x);
1583           tpa_remove_partition (tpa, x, y);
1584
1585           var = partition_to_var (map, y);
1586           /* p1 is the partition representative to which y belongs.  */
1587           p1 = var_to_partition (map, var);
1588           
1589           for (z = tpa_next_partition (tpa, y); 
1590                z != TPA_NONE; 
1591                z = tpa_next_partition (tpa, z))
1592             {
1593               tmp = partition_to_var (map, z);
1594               /* p2 is the partition representative to which z belongs.  */
1595               p2 = var_to_partition (map, tmp);
1596               if (debug)
1597                 {
1598                   fprintf (debug, "Coalesce : ");
1599                   print_generic_expr (debug, var, TDF_SLIM);
1600                   fprintf (debug, " &");
1601                   print_generic_expr (debug, tmp, TDF_SLIM);
1602                   fprintf (debug, "  (%d ,%d)", p1, p2);
1603                 }
1604
1605               /* If partitions are already merged, don't check for conflict.  */
1606               if (tmp == var)
1607                 {
1608                   tpa_remove_partition (tpa, x, z);
1609                   if (debug)
1610                     fprintf (debug, ": Already coalesced\n");
1611                 }
1612               else
1613                 if (!conflict_graph_conflict_p (graph, p1, p2))
1614                   {
1615                     int v;
1616                     if (tpa_find_tree (tpa, y) == TPA_NONE 
1617                         || tpa_find_tree (tpa, z) == TPA_NONE)
1618                       {
1619                         if (debug)
1620                           fprintf (debug, ": Fail non-TPA member\n");
1621                         continue;
1622                       }
1623                     if ((v = var_union (map, var, tmp)) == NO_PARTITION)
1624                       {
1625                         if (debug)
1626                           fprintf (debug, ": Fail cannot combine partitions\n");
1627                         continue;
1628                       }
1629
1630                     tpa_remove_partition (tpa, x, z);
1631                     if (v == p1)
1632                       conflict_graph_merge_regs (graph, v, z);
1633                     else
1634                       {
1635                         /* Update the first partition's representative.  */
1636                         conflict_graph_merge_regs (graph, v, y);
1637                         p1 = v;
1638                       }
1639
1640                     /* The root variable of the partition may be changed
1641                        now.  */
1642                     var = partition_to_var (map, p1);
1643
1644                     if (debug)
1645                       fprintf (debug, ": Success -> %d\n", v);
1646                   }
1647                 else
1648                   if (debug)
1649                     fprintf (debug, ": Fail, Conflict\n");
1650             }
1651         }
1652     }
1653 }
1654
1655
1656 /* Send debug info for coalesce list CL to file F.  */
1657
1658 void 
1659 dump_coalesce_list (FILE *f, coalesce_list_p cl)
1660 {
1661   partition_pair_p node;
1662   int x, num;
1663   tree var;
1664
1665   if (cl->add_mode)
1666     {
1667       fprintf (f, "Coalesce List:\n");
1668       num = num_var_partitions (cl->map);
1669       for (x = 0; x < num; x++)
1670         {
1671           node = cl->list[x];
1672           if (node)
1673             {
1674               fprintf (f, "[");
1675               print_generic_expr (f, partition_to_var (cl->map, x), TDF_SLIM);
1676               fprintf (f, "] - ");
1677               for ( ; node; node = node->next)
1678                 {
1679                   var = partition_to_var (cl->map, node->second_partition);
1680                   print_generic_expr (f, var, TDF_SLIM);
1681                   fprintf (f, "(%1d), ", node->cost);
1682                 }
1683               fprintf (f, "\n");
1684             }
1685         }
1686     }
1687   else
1688     {
1689       fprintf (f, "Sorted Coalesce list:\n");
1690       for (node = cl->list[0]; node; node = node->next)
1691         {
1692           fprintf (f, "(%d) ", node->cost);
1693           var = partition_to_var (cl->map, node->first_partition);
1694           print_generic_expr (f, var, TDF_SLIM);
1695           fprintf (f, " : ");
1696           var = partition_to_var (cl->map, node->second_partition);
1697           print_generic_expr (f, var, TDF_SLIM);
1698           fprintf (f, "\n");
1699         }
1700     }
1701 }
1702
1703
1704 /* Output tree_partition_associator object TPA to file F..  */
1705
1706 void
1707 tpa_dump (FILE *f, tpa_p tpa)
1708 {
1709   int x, i;
1710
1711   if (!tpa)
1712     return;
1713
1714   for (x = 0; x < tpa_num_trees (tpa); x++)
1715     {
1716       print_generic_expr (f, tpa_tree (tpa, x), TDF_SLIM);
1717       fprintf (f, " : (");
1718       for (i = tpa_first_partition (tpa, x); 
1719            i != TPA_NONE;
1720            i = tpa_next_partition (tpa, i))
1721         {
1722           fprintf (f, "(%d)",i);
1723           print_generic_expr (f, partition_to_var (tpa->map, i), TDF_SLIM);
1724           fprintf (f, " ");
1725
1726 #ifdef ENABLE_CHECKING
1727           if (tpa_find_tree (tpa, i) != x)
1728             fprintf (f, "**find tree incorrectly set** ");
1729 #endif
1730
1731         }
1732       fprintf (f, ")\n");
1733     }
1734   fflush (f);
1735 }
1736
1737
1738 /* Output partition map MAP to file F.  */
1739
1740 void
1741 dump_var_map (FILE *f, var_map map)
1742 {
1743   int t;
1744   unsigned x, y;
1745   int p;
1746
1747   fprintf (f, "\nPartition map \n\n");
1748
1749   for (x = 0; x < map->num_partitions; x++)
1750     {
1751       if (map->compact_to_partition != NULL)
1752         p = map->compact_to_partition[x];
1753       else
1754         p = x;
1755
1756       if (map->partition_to_var[p] == NULL_TREE)
1757         continue;
1758
1759       t = 0;
1760       for (y = 1; y < highest_ssa_version; y++)
1761         {
1762           p = partition_find (map->var_partition, y);
1763           if (map->partition_to_compact)
1764             p = map->partition_to_compact[p];
1765           if (p == (int)x)
1766             {
1767               if (t++ == 0)
1768                 {
1769                   fprintf(f, "Partition %d (", x);
1770                   print_generic_expr (f, partition_to_var (map, p), TDF_SLIM);
1771                   fprintf (f, " - ");
1772                 }
1773               fprintf (f, "%d ", y);
1774             }
1775         }
1776       if (t != 0)
1777         fprintf (f, ")\n");
1778     }
1779   fprintf (f, "\n");
1780 }
1781
1782
1783 /* Output live range info LIVE to file F, controlled by FLAG.  */
1784
1785 void
1786 dump_live_info (FILE *f, tree_live_info_p live, int flag)
1787 {
1788   basic_block bb;
1789   int i;
1790   var_map map = live->map;
1791
1792   if ((flag & LIVEDUMP_ENTRY) && live->livein)
1793     {
1794       FOR_EACH_BB (bb)
1795         {
1796           fprintf (f, "\nLive on entry to BB%d : ", bb->index);
1797           for (i = 0; i < num_var_partitions (map); i++)
1798             {
1799               if (bitmap_bit_p (live_entry_blocks (live, i), bb->index))
1800                 {
1801                   print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1802                   fprintf (f, "  ");
1803                 }
1804             }
1805           fprintf (f, "\n");
1806         }
1807     }
1808
1809   if ((flag & LIVEDUMP_EXIT) && live->liveout)
1810     {
1811       FOR_EACH_BB (bb)
1812         {
1813           fprintf (f, "\nLive on exit from BB%d : ", bb->index);
1814           EXECUTE_IF_SET_IN_BITMAP (live->liveout[bb->index], 0, i,
1815             {
1816               print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1817               fprintf (f, "  ");
1818             });
1819           fprintf (f, "\n");
1820         }
1821     }
1822 }
1823
1824 /* Register partitions in MAP so that we can take VARS out of SSA form. 
1825    This requires a walk over all the PHI nodes and all the statements.  */
1826
1827 void
1828 register_ssa_partitions_for_vars (bitmap vars, var_map map)
1829 {
1830   basic_block bb;
1831
1832   if (bitmap_first_set_bit (vars) >= 0)
1833     {
1834
1835       /* Find every instance (SSA_NAME) of variables in VARs and
1836          register a new partition for them.  This requires examining
1837          every statement and every PHI node once.  */
1838       FOR_EACH_BB (bb)
1839         {
1840           block_stmt_iterator bsi;
1841           tree next;
1842           tree phi;
1843
1844           /* Register partitions for SSA_NAMEs appearing in the PHI
1845              nodes in this basic block.
1846
1847              Note we delete PHI nodes in this loop if they are 
1848              associated with virtual vars which are going to be
1849              renamed.   */
1850           for (phi = phi_nodes (bb); phi; phi = next)
1851             {
1852               tree result = SSA_NAME_VAR (PHI_RESULT (phi));
1853
1854               next = TREE_CHAIN (phi);
1855               if (bitmap_bit_p (vars, var_ann (result)->uid))
1856                 {
1857                   if (! is_gimple_reg (result))
1858                     remove_phi_node (phi, NULL_TREE, bb);
1859                   else
1860                     {
1861                       int i;
1862
1863                       /* Register a partition for the result.  */
1864                       register_ssa_partition (map, PHI_RESULT (phi), 0);
1865
1866                       /* Register a partition for each argument as needed.  */
1867                       for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1868                         {
1869                           tree arg = PHI_ARG_DEF (phi, i);
1870
1871                           if (TREE_CODE (arg) != SSA_NAME)
1872                             continue;
1873                           if (!bitmap_bit_p (vars, 
1874                                              var_ann (SSA_NAME_VAR (arg))->uid))
1875                             continue;
1876
1877                           register_ssa_partition (map, arg, 1);
1878                         }
1879                     }
1880                 }
1881             }
1882
1883           /* Now register partitions for SSA_NAMEs appearing in each
1884              statement in this block.  */
1885           for (bsi = bsi_start (bb); ! bsi_end_p (bsi); bsi_next (&bsi))
1886             {
1887               stmt_ann_t ann = stmt_ann (bsi_stmt (bsi));
1888               use_optype uses = USE_OPS (ann);
1889               def_optype defs = DEF_OPS (ann);
1890               unsigned int i;
1891
1892               for (i = 0; i < NUM_USES (uses); i++)
1893                 {
1894                   tree op = USE_OP (uses, i);
1895
1896                   if (TREE_CODE (op) == SSA_NAME
1897                       && bitmap_bit_p (vars, var_ann (SSA_NAME_VAR (op))->uid))
1898                     register_ssa_partition (map, op, 1);
1899                 }
1900                     
1901               for (i = 0; i < NUM_DEFS (defs); i++)
1902                 {
1903                   tree op = DEF_OP (defs, i);
1904
1905                   if (TREE_CODE (op) == SSA_NAME
1906                           && bitmap_bit_p (vars,
1907                              var_ann (SSA_NAME_VAR (op))->uid))
1908                     register_ssa_partition (map, op, 0);
1909                 }
1910             }
1911         }
1912     }
1913 }
1914