OSDN Git Service

0113b1b36e7bba19359b741bb4273b2f089a0a3b
[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, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "diagnostic.h"
28 #include "bitmap.h"
29 #include "tree-flow.h"
30 #include "tree-dump.h"
31 #include "tree-ssa-live.h"
32 #include "toplev.h"
33
34 #ifdef ENABLE_CHECKING
35 static void  verify_live_on_entry (tree_live_info_p);
36 #endif
37
38
39 /* VARMAP maintains a mapping from SSA version number to real variables.
40
41    All SSA_NAMES are divided into partitions.  Initially each ssa_name is the
42    only member of it's own partition.  Coalescing will attempt to group any
43    ssa_names which occur in a copy or in a PHI node into the same partition.
44
45    At the end of out-of-ssa, each partition becomes a "real" variable and is
46    rewritten as a compiler variable.
47
48    The var_map datat structure is used to manage these partitions.  It allows
49    partitions to be combined, and determines which partition belongs to what
50    ssa_name or variable, and vice versa.  */
51
52
53 /* This routine will initialize the basevar fields of MAP.  */
54
55 static void
56 var_map_base_init (var_map map)
57 {
58   int x, num_part, num;
59   tree var;
60   var_ann_t ann;
61   
62   num = 0;
63   num_part = num_var_partitions (map);
64
65   /* If a base table already exists, clear it, otherwise create it.  */
66   if (map->partition_to_base_index != NULL)
67     {
68       free (map->partition_to_base_index);
69       VEC_truncate (tree, map->basevars, 0);
70     }
71   else
72     map->basevars = VEC_alloc (tree, heap, MAX (40, (num_part / 10)));
73
74   map->partition_to_base_index = (int *) xmalloc (sizeof (int) * num_part);
75
76   /* Build the base variable list, and point partitions at their bases.  */
77   for (x = 0; x < num_part; x++)
78     {
79       var = partition_to_var (map, x);
80       if (TREE_CODE (var) == SSA_NAME)
81          var = SSA_NAME_VAR (var);
82       ann = var_ann (var);
83       /* If base variable hasn't been seen, set it up.  */
84       if (!ann->base_var_processed)
85         {
86           ann->base_var_processed = 1;
87           VAR_ANN_BASE_INDEX (ann) = num++;
88           VEC_safe_push (tree, heap, map->basevars, var);
89         }
90       map->partition_to_base_index[x] = VAR_ANN_BASE_INDEX (ann);
91     }
92
93   map->num_basevars = num;
94
95   /* Now clear the processed bit.  */
96   for (x = 0; x < num; x++)
97     {
98        var = VEC_index (tree, map->basevars, x);
99        var_ann (var)->base_var_processed = 0;
100     }
101
102 #ifdef ENABLE_CHECKING
103   for (x = 0; x < num_part; x++)
104     {
105       tree var2;
106       var = SSA_NAME_VAR (partition_to_var (map, x));
107       var2 = VEC_index (tree, map->basevars, basevar_index (map, x));
108       gcc_assert (var == var2);
109     }
110 #endif
111 }
112
113
114 /* Remove the base table in MAP.  */
115
116 static void
117 var_map_base_fini (var_map map)
118 {
119   /* Free the basevar info if it is present.  */
120   if (map->partition_to_base_index != NULL)
121     {
122       VEC_free (tree, heap, map->basevars);
123       free (map->partition_to_base_index);
124       map->partition_to_base_index = NULL;
125       map->num_basevars = 0;
126     }
127 }
128 /* Create a variable partition map of SIZE, initialize and return it.  */
129
130 var_map
131 init_var_map (int size)
132 {
133   var_map map;
134
135   map = (var_map) xmalloc (sizeof (struct _var_map));
136   map->var_partition = partition_new (size);
137   map->partition_to_var 
138               = (tree *)xmalloc (size * sizeof (tree));
139   memset (map->partition_to_var, 0, size * sizeof (tree));
140
141   map->partition_to_view = NULL;
142   map->view_to_partition = NULL;
143   map->num_partitions = size;
144   map->partition_size = size;
145   map->num_basevars = 0;
146   map->partition_to_base_index = NULL;
147   map->basevars = NULL;
148   return map;
149 }
150
151
152 /* Free memory associated with MAP.  */
153
154 void
155 delete_var_map (var_map map)
156 {
157   var_map_base_fini (map);
158   free (map->partition_to_var);
159   partition_delete (map->var_partition);
160   if (map->partition_to_view)
161     free (map->partition_to_view);
162   if (map->view_to_partition)
163     free (map->view_to_partition);
164   free (map);
165 }
166
167
168 /* This function will combine the partitions in MAP for VAR1 and VAR2.  It 
169    Returns the partition which represents the new partition.  If the two 
170    partitions cannot be combined, NO_PARTITION is returned.  */
171
172 int
173 var_union (var_map map, tree var1, tree var2)
174 {
175   int p1, p2, p3;
176   tree root_var = NULL_TREE;
177   tree other_var = NULL_TREE;
178
179   /* This is independent of partition_to_view. If partition_to_view is 
180      on, then whichever one of these partitions is absorbed will never have a
181      dereference into the partition_to_view array any more.  */
182
183   if (TREE_CODE (var1) == SSA_NAME)
184     p1 = partition_find (map->var_partition, SSA_NAME_VERSION (var1));
185   else
186     {
187       p1 = var_to_partition (map, var1);
188       if (map->view_to_partition)
189         p1 = map->view_to_partition[p1];
190       root_var = var1;
191     }
192   
193   if (TREE_CODE (var2) == SSA_NAME)
194     p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2));
195   else
196     {
197       p2 = var_to_partition (map, var2);
198       if (map->view_to_partition)
199         p2 = map->view_to_partition[p2];
200
201       /* If there is no root_var set, or it's not a user variable, set the
202          root_var to this one.  */
203       if (!root_var || (DECL_P (root_var) && DECL_IGNORED_P (root_var)))
204         {
205           other_var = root_var;
206           root_var = var2;
207         }
208       else 
209         other_var = var2;
210     }
211
212   gcc_assert (p1 != NO_PARTITION);
213   gcc_assert (p2 != NO_PARTITION);
214
215   if (p1 == p2)
216     p3 = p1;
217   else
218     p3 = partition_union (map->var_partition, p1, p2);
219
220   if (map->partition_to_view)
221     p3 = map->partition_to_view[p3];
222
223   if (root_var)
224     change_partition_var (map, root_var, p3);
225   if (other_var)
226     change_partition_var (map, other_var, p3);
227
228   return p3;
229 }
230
231  
232 /* Compress the partition numbers in MAP such that they fall in the range 
233    0..(num_partitions-1) instead of wherever they turned out during
234    the partitioning exercise.  This removes any references to unused
235    partitions, thereby allowing bitmaps and other vectors to be much
236    denser.  
237
238    This is implemented such that compaction doesn't affect partitioning.
239    Ie., once partitions are created and possibly merged, running one
240    or more different kind of compaction will not affect the partitions
241    themselves.  Their index might change, but all the same variables will
242    still be members of the same partition group.  This allows work on reduced
243    sets, and no loss of information when a larger set is later desired.
244
245    In particular, coalescing can work on partitions which have 2 or more
246    definitions, and then 'recompact' later to include all the single
247    definitions for assignment to program variables.  */
248
249
250 /* Set MAP back to the initial state of having no partition view.  Return a 
251    bitmap which has a bit set for each partition number which is in use in the 
252    varmap.  */
253
254 static bitmap
255 partition_view_init (var_map map)
256 {
257   bitmap used;
258   int tmp;
259   unsigned int x;
260
261   used = BITMAP_ALLOC (NULL);
262
263   /* Already in a view? Abandon the old one.  */
264   if (map->partition_to_view)
265     {
266       free (map->partition_to_view);
267       map->partition_to_view = NULL;
268     }
269   if (map->view_to_partition)
270     {
271       free (map->view_to_partition);
272       map->view_to_partition = NULL;
273     }
274
275   /* Find out which partitions are actually referenced.  */
276   for (x = 0; x < map->partition_size; x++)
277     {
278       tmp = partition_find (map->var_partition, x);
279       if (map->partition_to_var[tmp] != NULL_TREE && !bitmap_bit_p (used, tmp))
280         bitmap_set_bit (used, tmp);
281     }
282
283   map->num_partitions = map->partition_size;
284   return used;
285 }
286
287
288 /* This routine will finalize the view data for MAP based on the partitions
289    set in SELECTED.  This is either the same bitmap returned from 
290    partition_view_init, or a trimmed down version if some of those partitions
291    were not desired in this view.  SELECTED is freed before returning.  */
292
293 static void 
294 partition_view_fini (var_map map, bitmap selected)
295 {
296   bitmap_iterator bi;
297   unsigned count, i, x, limit;
298   tree var;
299
300   gcc_assert (selected);
301
302   count = bitmap_count_bits (selected);
303   limit = map->partition_size;
304
305   /* If its a one-to-one ratio, we don't need any view compaction.  */
306   if (count < limit)
307     {
308       map->partition_to_view = (int *)xmalloc (limit * sizeof (int));
309       memset (map->partition_to_view, 0xff, (limit * sizeof (int)));
310       map->view_to_partition = (int *)xmalloc (count * sizeof (int));
311
312       i = 0;
313       /* Give each selected partition an index.  */
314       EXECUTE_IF_SET_IN_BITMAP (selected, 0, x, bi)
315         {
316           map->partition_to_view[x] = i;
317           map->view_to_partition[i] = x;
318           var = map->partition_to_var[x];
319           /* If any one of the members of a partition is not an SSA_NAME, make
320              sure it is the representative.  */
321           if (TREE_CODE (var) != SSA_NAME)
322             change_partition_var (map, var, i);
323           i++;
324         }
325       gcc_assert (i == count);
326       map->num_partitions = i;
327     }
328
329   BITMAP_FREE (selected);
330 }
331
332
333 /* Create a partition view which includes all the used partitions in MAP.  If 
334    WANT_BASES is true, create the base variable map as well.  */
335
336 extern void
337 partition_view_normal (var_map map, bool want_bases)
338 {
339   bitmap used;
340
341   used = partition_view_init (map);
342   partition_view_fini (map, used);
343
344   if (want_bases)
345     var_map_base_init (map);
346   else
347     var_map_base_fini (map);
348 }
349
350
351 /* Create a partition view in MAP which includes just partitions which occur in 
352    the bitmap ONLY. If WANT_BASES is true, create the base variable map 
353    as well.  */
354
355 extern void
356 partition_view_bitmap (var_map map, bitmap only, bool want_bases)
357 {
358   bitmap used;
359   bitmap new_partitions = BITMAP_ALLOC (NULL);
360   unsigned x, p;
361   bitmap_iterator bi;
362
363   used = partition_view_init (map);
364   EXECUTE_IF_SET_IN_BITMAP (only, 0, x, bi)
365     {
366       p = partition_find (map->var_partition, x);
367       gcc_assert (bitmap_bit_p (used, p));
368       bitmap_set_bit (new_partitions, p);
369     }
370   partition_view_fini (map, new_partitions);
371
372   BITMAP_FREE (used);
373   if (want_bases)
374     var_map_base_init (map);
375   else
376     var_map_base_fini (map);
377 }
378
379
380 /* This function is used to change the representative variable in MAP for VAR's 
381    partition to a regular non-ssa variable.  This allows partitions to be 
382    mapped back to real variables.  */
383   
384 void 
385 change_partition_var (var_map map, tree var, int part)
386 {
387   var_ann_t ann;
388
389   gcc_assert (TREE_CODE (var) != SSA_NAME);
390
391   ann = var_ann (var);
392   ann->out_of_ssa_tag = 1;
393   VAR_ANN_PARTITION (ann) = part;
394   if (map->view_to_partition)
395     map->partition_to_var[map->view_to_partition[part]] = var;
396 }
397
398
399 static inline void mark_all_vars_used (tree *);
400
401 /* Helper function for mark_all_vars_used, called via walk_tree.  */
402
403 static tree
404 mark_all_vars_used_1 (tree *tp, int *walk_subtrees,
405                       void *data ATTRIBUTE_UNUSED)
406 {
407   tree t = *tp;
408
409   if (TREE_CODE (t) == SSA_NAME)
410     t = SSA_NAME_VAR (t);
411
412   /* Ignore TREE_ORIGINAL for TARGET_MEM_REFS, as well as other
413      fields that do not contain vars.  */
414   if (TREE_CODE (t) == TARGET_MEM_REF)
415     {
416       mark_all_vars_used (&TMR_SYMBOL (t));
417       mark_all_vars_used (&TMR_BASE (t));
418       mark_all_vars_used (&TMR_INDEX (t));
419       *walk_subtrees = 0;
420       return NULL;
421     }
422
423   /* Only need to mark VAR_DECLS; parameters and return results are not
424      eliminated as unused.  */
425   if (TREE_CODE (t) == VAR_DECL)
426     set_is_used (t);
427
428   if (IS_TYPE_OR_DECL_P (t))
429     *walk_subtrees = 0;
430
431   return NULL;
432 }
433
434
435 /* Mark all VAR_DECLS under *EXPR_P as used, so that they won't be 
436    eliminated during the tree->rtl conversion process.  */
437
438 static inline void
439 mark_all_vars_used (tree *expr_p)
440 {
441   walk_tree (expr_p, mark_all_vars_used_1, NULL, NULL);
442 }
443
444
445 /* Remove local variables that are not referenced in the IL.  */
446
447 void
448 remove_unused_locals (void)
449 {
450   basic_block bb;
451   tree t, *cell;
452   referenced_var_iterator rvi;
453   var_ann_t ann;
454
455   /* Assume all locals are unused.  */
456   FOR_EACH_REFERENCED_VAR (t, rvi)
457     var_ann (t)->used = false;
458
459   /* Walk the CFG marking all referenced symbols.  */
460   FOR_EACH_BB (bb)
461     {
462       block_stmt_iterator bsi;
463       tree phi, def;
464
465       /* Walk the statements.  */
466       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
467         mark_all_vars_used (bsi_stmt_ptr (bsi));
468
469       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
470         {
471           use_operand_p arg_p;
472           ssa_op_iter i;
473
474           /* No point processing globals.  */
475           if (is_global_var (SSA_NAME_VAR (PHI_RESULT (phi))))
476             continue;
477
478           def = PHI_RESULT (phi);
479           mark_all_vars_used (&def);
480
481           FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_ALL_USES)
482             {
483               tree arg = USE_FROM_PTR (arg_p);
484               mark_all_vars_used (&arg);
485             }
486         }
487     }
488
489   /* Remove unmarked vars and clear used flag.  */
490   for (cell = &cfun->unexpanded_var_list; *cell; )
491     {
492       tree var = TREE_VALUE (*cell);
493
494       if (TREE_CODE (var) != FUNCTION_DECL
495           && (!(ann = var_ann (var))
496               || !ann->used))
497         {
498           *cell = TREE_CHAIN (*cell);
499           continue;
500         }
501
502       cell = &TREE_CHAIN (*cell);
503     }
504
505   /* Remove unused variables from REFERENCED_VARs.  As an special exception
506      keep the variables that are believed to be aliased.  Those can't be
507      easily removed from the alias sets and and operand caches.
508      They will be removed shortly after next may_alias pass is performed.  */
509   FOR_EACH_REFERENCED_VAR (t, rvi)
510     if (!is_global_var (t)
511         && !MTAG_P (t)
512         && TREE_CODE (t) != PARM_DECL
513         && TREE_CODE (t) != RESULT_DECL
514         && !(ann = var_ann (t))->used
515         && !ann->is_aliased && !is_call_clobbered (t) && !ann->symbol_mem_tag)
516         remove_referenced_var (t);
517 }
518
519
520 /* Allocate and return a new live range information object base on MAP.  */
521
522 static tree_live_info_p
523 new_tree_live_info (var_map map)
524 {
525   tree_live_info_p live;
526   unsigned x;
527
528   live = (tree_live_info_p) xmalloc (sizeof (struct tree_live_info_d));
529   live->map = map;
530   live->num_blocks = last_basic_block;
531
532   live->livein = (bitmap *)xmalloc (last_basic_block * sizeof (bitmap));
533   for (x = 0; x < (unsigned)last_basic_block; x++)
534     live->livein[x] = BITMAP_ALLOC (NULL);
535
536   live->liveout = (bitmap *)xmalloc (last_basic_block * sizeof (bitmap));
537   for (x = 0; x < (unsigned)last_basic_block; x++)
538     live->liveout[x] = BITMAP_ALLOC (NULL);
539
540   live->work_stack = XNEWVEC (int, last_basic_block);
541   live->stack_top = live->work_stack;
542
543   live->global = BITMAP_ALLOC (NULL);
544   return live;
545 }
546
547
548 /* Free storage for live range info object LIVE.  */
549
550 void 
551 delete_tree_live_info (tree_live_info_p live)
552 {
553   int x;
554
555   BITMAP_FREE (live->global);
556   free (live->work_stack);
557
558   for (x = live->num_blocks - 1; x >= 0; x--)
559     BITMAP_FREE (live->liveout[x]);
560   free (live->liveout);
561
562   for (x = live->num_blocks - 1; x >= 0; x--)
563     BITMAP_FREE (live->livein[x]);
564   free (live->livein);
565
566   free (live);
567 }
568
569
570 /* Visit basic block BB and propogate any required live on entry bits from 
571    LIVE into the predecessors.  VISITED is the bitmap of visited blocks.  
572    TMP is a temporary work bitmap which is passed in to avoid reallocating
573    it each time.  */
574
575 static void 
576 loe_visit_block (tree_live_info_p live, basic_block bb, sbitmap visited,
577                  bitmap tmp)
578 {
579   edge e;
580   bool change;
581   edge_iterator ei;
582   basic_block pred_bb;
583   bitmap loe;
584   gcc_assert (!TEST_BIT (visited, bb->index));
585
586   SET_BIT (visited, bb->index);
587   loe = live_on_entry (live, bb);
588
589   FOR_EACH_EDGE (e, ei, bb->preds)
590     {
591       pred_bb = e->src;
592       if (pred_bb == ENTRY_BLOCK_PTR)
593         continue;
594       /* TMP is variables live-on-entry from BB that aren't defined in the
595          predecessor block.  This should be the live on entry vars to pred.  
596          Note that liveout is the DEFs in a block while live on entry is
597          being calculated.  */
598       bitmap_and_compl (tmp, loe, live->liveout[pred_bb->index]);
599
600       /* Add these bits to live-on-entry for the pred. if there are any 
601          changes, and pred_bb has been visited already, add it to the
602          revisit stack.  */
603       change = bitmap_ior_into (live_on_entry (live, pred_bb), tmp);
604       if (TEST_BIT (visited, pred_bb->index) && change)
605         {
606           RESET_BIT (visited, pred_bb->index);
607           *(live->stack_top)++ = pred_bb->index;
608         }
609     }
610 }
611
612
613 /* Using LIVE, fill in all the live-on-entry blocks between the defs and uses 
614    of all the variables.  */
615
616 static void
617 live_worklist (tree_live_info_p live)
618 {
619   unsigned b;
620   basic_block bb;
621   sbitmap visited = sbitmap_alloc (last_basic_block + 1);
622   bitmap tmp = BITMAP_ALLOC (NULL);
623
624   sbitmap_zero (visited);
625
626   /* Visit all the blocks in reverse order and propogate live on entry values
627      into the predecessors blocks.  */
628   FOR_EACH_BB_REVERSE (bb)
629     loe_visit_block (live, bb, visited, tmp);
630
631   /* Process any blocks which require further iteration.  */
632   while (live->stack_top != live->work_stack)
633     {
634       b = *--(live->stack_top);
635       loe_visit_block (live, BASIC_BLOCK (b), visited, tmp);
636     }
637
638   BITMAP_FREE (tmp);
639   sbitmap_free (visited);
640 }
641
642
643 /* Calculate the initial live on entry vector for SSA_NAME using immediate_use
644    links.  Set the live on entry fields in LIVE.  Def's are marked temporarily
645    in the liveout vector.  */
646
647 static void
648 set_var_live_on_entry (tree ssa_name, tree_live_info_p live)
649 {
650   int p;
651   tree stmt;
652   use_operand_p use;
653   basic_block def_bb = NULL;
654   imm_use_iterator imm_iter;
655   bool global = false;
656
657   p = var_to_partition (live->map, ssa_name);
658   if (p == NO_PARTITION)
659     return;
660
661   stmt = SSA_NAME_DEF_STMT (ssa_name);
662   if (stmt)
663     {
664       def_bb = bb_for_stmt (stmt);
665       /* Mark defs in liveout bitmap temporarily.  */
666       if (def_bb)
667         bitmap_set_bit (live->liveout[def_bb->index], p);
668     }
669   else
670     def_bb = ENTRY_BLOCK_PTR;
671
672   /* Visit each use of SSA_NAME and if it isn't in the same block as the def,
673      add it to the list of live on entry blocks.  */
674   FOR_EACH_IMM_USE_FAST (use, imm_iter, ssa_name)
675     {
676       tree use_stmt = USE_STMT (use);
677       basic_block add_block = NULL;
678
679       if (TREE_CODE (use_stmt) == PHI_NODE)
680         {
681           /* Uses in PHI's are considered to be live at exit of the SRC block
682              as this is where a copy would be inserted.  Check to see if it is
683              defined in that block, or whether its live on entry.  */
684           int index = PHI_ARG_INDEX_FROM_USE (use);
685           edge e = PHI_ARG_EDGE (use_stmt, index);
686           if (e->src != ENTRY_BLOCK_PTR)
687             {
688               if (e->src != def_bb)
689                 add_block = e->src;
690             }
691         }
692       else
693         {
694           /* If its not defined in this block, its live on entry.  */
695           basic_block use_bb = bb_for_stmt (use_stmt);
696           if (use_bb != def_bb)
697             add_block = use_bb;
698         }  
699
700       /* If there was a live on entry use, set the bit.  */
701       if (add_block)
702         {
703           global = true;
704           bitmap_set_bit (live->livein[add_block->index], p);
705         }
706     }
707
708   /* If SSA_NAME is live on entry to at least one block, fill in all the live
709      on entry blocks between the def and all the uses.  */
710   if (global)
711     bitmap_set_bit (live->global, p);
712 }
713
714
715 /* Calculate the live on exit vectors based on the entry info in LIVEINFO.  */
716
717 void
718 calculate_live_on_exit (tree_live_info_p liveinfo)
719 {
720   unsigned i;
721   int p;
722   tree t, phi;
723   basic_block bb;
724   edge e;
725   edge_iterator ei;
726
727   /* live on entry calculations used liveout vectors for defs, clear them.  */
728   FOR_EACH_BB (bb)
729     bitmap_clear (liveinfo->liveout[bb->index]);
730
731   /* Set all the live-on-exit bits for uses in PHIs.  */
732   FOR_EACH_BB (bb)
733     {
734       /* Mark the PHI arguments which are live on exit to the pred block.  */
735       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
736         for (i = 0; i < (unsigned)PHI_NUM_ARGS (phi); i++)
737           { 
738             t = PHI_ARG_DEF (phi, i);
739             if (TREE_CODE (t) != SSA_NAME)
740               continue;
741             p = var_to_partition (liveinfo->map, t);
742             if (p == NO_PARTITION)
743               continue;
744             e = PHI_ARG_EDGE (phi, i);
745             if (e->src != ENTRY_BLOCK_PTR)
746               bitmap_set_bit (liveinfo->liveout[e->src->index], p);
747           }
748
749       /* Add each successors live on entry to this bock live on exit.  */
750       FOR_EACH_EDGE (e, ei, bb->succs)
751         if (e->dest != EXIT_BLOCK_PTR)
752           bitmap_ior_into (liveinfo->liveout[bb->index],
753                            live_on_entry (liveinfo, e->dest));
754     }
755 }
756
757
758 /* Given partition map MAP, calculate all the live on entry bitmaps for 
759    each partition.  Return a new live info object.  */
760
761 tree_live_info_p 
762 calculate_live_ranges (var_map map)
763 {
764   tree var;
765   unsigned i;
766   tree_live_info_p live;
767
768   live = new_tree_live_info (map);
769   for (i = 0; i < num_var_partitions (map); i++)
770     {
771       var = partition_to_var (map, i);
772       if (var != NULL_TREE)
773         set_var_live_on_entry (var, live);
774     }
775
776   live_worklist (live);
777
778 #ifdef ENABLE_CHECKING
779   verify_live_on_entry (live);
780 #endif
781
782   calculate_live_on_exit (live);
783   return live;
784 }
785
786
787 /* Output partition map MAP to file F.  */
788
789 void
790 dump_var_map (FILE *f, var_map map)
791 {
792   int t;
793   unsigned x, y;
794   int p;
795
796   fprintf (f, "\nPartition map \n\n");
797
798   for (x = 0; x < map->num_partitions; x++)
799     {
800       if (map->view_to_partition != NULL)
801         p = map->view_to_partition[x];
802       else
803         p = x;
804
805       if (map->partition_to_var[p] == NULL_TREE)
806         continue;
807
808       t = 0;
809       for (y = 1; y < num_ssa_names; y++)
810         {
811           p = partition_find (map->var_partition, y);
812           if (map->partition_to_view)
813             p = map->partition_to_view[p];
814           if (p == (int)x)
815             {
816               if (t++ == 0)
817                 {
818                   fprintf(f, "Partition %d (", x);
819                   print_generic_expr (f, partition_to_var (map, p), TDF_SLIM);
820                   fprintf (f, " - ");
821                 }
822               fprintf (f, "%d ", y);
823             }
824         }
825       if (t != 0)
826         fprintf (f, ")\n");
827     }
828   fprintf (f, "\n");
829 }
830
831
832 /* Output live range info LIVE to file F, controlled by FLAG.  */
833
834 void
835 dump_live_info (FILE *f, tree_live_info_p live, int flag)
836 {
837   basic_block bb;
838   unsigned i;
839   var_map map = live->map;
840   bitmap_iterator bi;
841
842   if ((flag & LIVEDUMP_ENTRY) && live->livein)
843     {
844       FOR_EACH_BB (bb)
845         {
846           fprintf (f, "\nLive on entry to BB%d : ", bb->index);
847           EXECUTE_IF_SET_IN_BITMAP (live->livein[bb->index], 0, i, bi)
848             {
849               print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
850               fprintf (f, "  ");
851             }
852           fprintf (f, "\n");
853         }
854     }
855
856   if ((flag & LIVEDUMP_EXIT) && live->liveout)
857     {
858       FOR_EACH_BB (bb)
859         {
860           fprintf (f, "\nLive on exit from BB%d : ", bb->index);
861           EXECUTE_IF_SET_IN_BITMAP (live->liveout[bb->index], 0, i, bi)
862             {
863               print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
864               fprintf (f, "  ");
865             }
866           fprintf (f, "\n");
867         }
868     }
869 }
870
871
872 #ifdef ENABLE_CHECKING
873 /* Verify that SSA_VAR is a non-virtual SSA_NAME.  */
874
875 void
876 register_ssa_partition_check (tree ssa_var)
877 {
878   gcc_assert (TREE_CODE (ssa_var) == SSA_NAME);
879   if (!is_gimple_reg (SSA_NAME_VAR (ssa_var)))
880     {
881       fprintf (stderr, "Illegally registering a virtual SSA name :");
882       print_generic_expr (stderr, ssa_var, TDF_SLIM);
883       fprintf (stderr, " in the SSA->Normal phase.\n");
884       internal_error ("SSA corruption");
885     }
886 }
887
888
889 /* Verify that the info in LIVE matches the current cfg.  */
890
891 static void
892 verify_live_on_entry (tree_live_info_p live)
893 {
894   unsigned i;
895   tree var;
896   tree phi, stmt;
897   basic_block bb;
898   edge e;
899   int num;
900   edge_iterator ei;
901   var_map map = live->map;
902
903    /* Check for live on entry partitions and report those with a DEF in
904       the program. This will typically mean an optimization has done
905       something wrong.  */
906   bb = ENTRY_BLOCK_PTR;
907   num = 0;
908   FOR_EACH_EDGE (e, ei, bb->succs)
909     {
910       int entry_block = e->dest->index;
911       if (e->dest == EXIT_BLOCK_PTR)
912         continue;
913       for (i = 0; i < (unsigned)num_var_partitions (map); i++)
914         {
915           basic_block tmp;
916           tree d;
917           bitmap loe;
918           var = partition_to_var (map, i);
919           stmt = SSA_NAME_DEF_STMT (var);
920           tmp = bb_for_stmt (stmt);
921           d = gimple_default_def (cfun, SSA_NAME_VAR (var));
922
923           loe = live_on_entry (live, e->dest);
924           if (loe && bitmap_bit_p (loe, i))
925             {
926               if (!IS_EMPTY_STMT (stmt))
927                 {
928                   num++;
929                   print_generic_expr (stderr, var, TDF_SLIM);
930                   fprintf (stderr, " is defined ");
931                   if (tmp)
932                     fprintf (stderr, " in BB%d, ", tmp->index);
933                   fprintf (stderr, "by:\n");
934                   print_generic_expr (stderr, stmt, TDF_SLIM);
935                   fprintf (stderr, "\nIt is also live-on-entry to entry BB %d", 
936                            entry_block);
937                   fprintf (stderr, " So it appears to have multiple defs.\n");
938                 }
939               else
940                 {
941                   if (d != var)
942                     {
943                       num++;
944                       print_generic_expr (stderr, var, TDF_SLIM);
945                       fprintf (stderr, " is live-on-entry to BB%d ",entry_block);
946                       if (d)
947                         {
948                           fprintf (stderr, " but is not the default def of ");
949                           print_generic_expr (stderr, d, TDF_SLIM);
950                           fprintf (stderr, "\n");
951                         }
952                       else
953                         fprintf (stderr, " and there is no default def.\n");
954                     }
955                 }
956             }
957           else
958             if (d == var)
959               {
960                 /* The only way this var shouldn't be marked live on entry is 
961                    if it occurs in a PHI argument of the block.  */
962                 int z, ok = 0;
963                 for (phi = phi_nodes (e->dest); 
964                      phi && !ok; 
965                      phi = PHI_CHAIN (phi))
966                   {
967                     for (z = 0; z < PHI_NUM_ARGS (phi); z++)
968                       if (var == PHI_ARG_DEF (phi, z))
969                         {
970                           ok = 1;
971                           break;
972                         }
973                   }
974                 if (ok)
975                   continue;
976                 num++;
977                 print_generic_expr (stderr, var, TDF_SLIM);
978                 fprintf (stderr, " is not marked live-on-entry to entry BB%d ", 
979                          entry_block);
980                 fprintf (stderr, "but it is a default def so it should be.\n");
981               }
982         }
983     }
984   gcc_assert (num <= 0);
985 }
986 #endif