* cp-objcp-common.c (cp_expr_size): Return NULL in the case
[official-gcc.git] / gcc / tree-ssa-live.c
bloba632600a0206f8c633f70a01e76558d751435bb7
1 /* Liveness for SSA trees.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "basic-block.h"
29 #include "function.h"
30 #include "diagnostic.h"
31 #include "bitmap.h"
32 #include "tree-flow.h"
33 #include "tree-gimple.h"
34 #include "tree-inline.h"
35 #include "varray.h"
36 #include "timevar.h"
37 #include "hashtab.h"
38 #include "tree-dump.h"
39 #include "tree-ssa-live.h"
40 #include "toplev.h"
41 #include "vecprim.h"
43 static void live_worklist (tree_live_info_p);
44 static tree_live_info_p new_tree_live_info (var_map);
45 static inline void set_if_valid (var_map, bitmap, tree);
46 static inline void add_conflicts_if_valid (tpa_p, conflict_graph,
47 var_map, bitmap, tree);
48 static partition_pair_p find_partition_pair (coalesce_list_p, int, int, bool);
49 #ifdef ENABLE_CHECKING
50 static void verify_live_on_entry (tree_live_info_p);
51 #endif
53 /* This is where the mapping from SSA version number to real storage variable
54 is tracked.
56 All SSA versions of the same variable may not ultimately be mapped back to
57 the same real variable. In that instance, we need to detect the live
58 range overlap, and give one of the variable new storage. The vector
59 'partition_to_var' tracks which partition maps to which variable.
61 Given a VAR, it is sometimes desirable to know which partition that VAR
62 represents. There is an additional field in the variable annotation to
63 track that information. */
65 /* Create a variable partition map of SIZE, initialize and return it. */
67 var_map
68 init_var_map (int size)
70 var_map map;
72 map = (var_map) xmalloc (sizeof (struct _var_map));
73 map->var_partition = partition_new (size);
74 map->partition_to_var
75 = (tree *)xmalloc (size * sizeof (tree));
76 memset (map->partition_to_var, 0, size * sizeof (tree));
78 map->partition_to_compact = NULL;
79 map->compact_to_partition = NULL;
80 map->num_partitions = size;
81 map->partition_size = size;
82 return map;
86 /* Free memory associated with MAP. */
88 void
89 delete_var_map (var_map map)
91 free (map->partition_to_var);
92 partition_delete (map->var_partition);
93 if (map->partition_to_compact)
94 free (map->partition_to_compact);
95 if (map->compact_to_partition)
96 free (map->compact_to_partition);
97 free (map);
101 /* This function will combine the partitions in MAP for VAR1 and VAR2. It
102 Returns the partition which represents the new partition. If the two
103 partitions cannot be combined, NO_PARTITION is returned. */
106 var_union (var_map map, tree var1, tree var2)
108 int p1, p2, p3;
109 tree root_var = NULL_TREE;
110 tree other_var = NULL_TREE;
112 /* This is independent of partition_to_compact. If partition_to_compact is
113 on, then whichever one of these partitions is absorbed will never have a
114 dereference into the partition_to_compact array any more. */
116 if (TREE_CODE (var1) == SSA_NAME)
117 p1 = partition_find (map->var_partition, SSA_NAME_VERSION (var1));
118 else
120 p1 = var_to_partition (map, var1);
121 if (map->compact_to_partition)
122 p1 = map->compact_to_partition[p1];
123 root_var = var1;
126 if (TREE_CODE (var2) == SSA_NAME)
127 p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2));
128 else
130 p2 = var_to_partition (map, var2);
131 if (map->compact_to_partition)
132 p2 = map->compact_to_partition[p2];
134 /* If there is no root_var set, or it's not a user variable, set the
135 root_var to this one. */
136 if (!root_var || (DECL_P (root_var) && DECL_IGNORED_P (root_var)))
138 other_var = root_var;
139 root_var = var2;
141 else
142 other_var = var2;
145 gcc_assert (p1 != NO_PARTITION);
146 gcc_assert (p2 != NO_PARTITION);
148 if (p1 == p2)
149 p3 = p1;
150 else
151 p3 = partition_union (map->var_partition, p1, p2);
153 if (map->partition_to_compact)
154 p3 = map->partition_to_compact[p3];
156 if (root_var)
157 change_partition_var (map, root_var, p3);
158 if (other_var)
159 change_partition_var (map, other_var, p3);
161 return p3;
165 /* Compress the partition numbers in MAP such that they fall in the range
166 0..(num_partitions-1) instead of wherever they turned out during
167 the partitioning exercise. This removes any references to unused
168 partitions, thereby allowing bitmaps and other vectors to be much
169 denser. Compression type is controlled by FLAGS.
171 This is implemented such that compaction doesn't affect partitioning.
172 Ie., once partitions are created and possibly merged, running one
173 or more different kind of compaction will not affect the partitions
174 themselves. Their index might change, but all the same variables will
175 still be members of the same partition group. This allows work on reduced
176 sets, and no loss of information when a larger set is later desired.
178 In particular, coalescing can work on partitions which have 2 or more
179 definitions, and then 'recompact' later to include all the single
180 definitions for assignment to program variables. */
182 void
183 compact_var_map (var_map map, int flags)
185 sbitmap used;
186 int tmp, root, root_i;
187 unsigned int x, limit, count;
188 tree var;
189 root_var_p rv = NULL;
191 limit = map->partition_size;
192 used = sbitmap_alloc (limit);
193 sbitmap_zero (used);
195 /* Already compressed? Abandon the old one. */
196 if (map->partition_to_compact)
198 free (map->partition_to_compact);
199 map->partition_to_compact = NULL;
201 if (map->compact_to_partition)
203 free (map->compact_to_partition);
204 map->compact_to_partition = NULL;
207 map->num_partitions = map->partition_size;
209 if (flags & VARMAP_NO_SINGLE_DEFS)
210 rv = root_var_init (map);
212 map->partition_to_compact = (int *)xmalloc (limit * sizeof (int));
213 memset (map->partition_to_compact, 0xff, (limit * sizeof (int)));
215 /* Find out which partitions are actually referenced. */
216 count = 0;
217 for (x = 0; x < limit; x++)
219 tmp = partition_find (map->var_partition, x);
220 if (!TEST_BIT (used, tmp) && map->partition_to_var[tmp] != NULL_TREE)
222 /* It is referenced, check to see if there is more than one version
223 in the root_var table, if one is available. */
224 if (rv)
226 root = root_var_find (rv, tmp);
227 root_i = root_var_first_partition (rv, root);
228 /* If there is only one, don't include this in the compaction. */
229 if (root_var_next_partition (rv, root_i) == ROOT_VAR_NONE)
230 continue;
232 SET_BIT (used, tmp);
233 count++;
237 /* Build a compacted partitioning. */
238 if (count != limit)
240 sbitmap_iterator sbi;
242 map->compact_to_partition = (int *)xmalloc (count * sizeof (int));
243 count = 0;
244 /* SSA renaming begins at 1, so skip 0 when compacting. */
245 EXECUTE_IF_SET_IN_SBITMAP (used, 1, x, sbi)
247 map->partition_to_compact[x] = count;
248 map->compact_to_partition[count] = x;
249 var = map->partition_to_var[x];
250 if (TREE_CODE (var) != SSA_NAME)
251 change_partition_var (map, var, count);
252 count++;
255 else
257 free (map->partition_to_compact);
258 map->partition_to_compact = NULL;
261 map->num_partitions = count;
263 if (rv)
264 root_var_delete (rv);
265 sbitmap_free (used);
269 /* This function is used to change the representative variable in MAP for VAR's
270 partition from an SSA_NAME variable to a regular variable. This allows
271 partitions to be mapped back to real variables. */
273 void
274 change_partition_var (var_map map, tree var, int part)
276 var_ann_t ann;
278 gcc_assert (TREE_CODE (var) != SSA_NAME);
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;
287 static inline void mark_all_vars_used (tree *);
289 /* Helper function for mark_all_vars_used, called via walk_tree. */
291 static tree
292 mark_all_vars_used_1 (tree *tp, int *walk_subtrees,
293 void *data ATTRIBUTE_UNUSED)
295 tree t = *tp;
297 if (TREE_CODE (t) == SSA_NAME)
298 t = SSA_NAME_VAR (t);
300 /* Ignore TREE_ORIGINAL for TARGET_MEM_REFS, as well as other
301 fields that do not contain vars. */
302 if (TREE_CODE (t) == TARGET_MEM_REF)
304 mark_all_vars_used (&TMR_SYMBOL (t));
305 mark_all_vars_used (&TMR_BASE (t));
306 mark_all_vars_used (&TMR_INDEX (t));
307 *walk_subtrees = 0;
308 return NULL;
311 /* Only need to mark VAR_DECLS; parameters and return results are not
312 eliminated as unused. */
313 if (TREE_CODE (t) == VAR_DECL)
314 set_is_used (t);
316 if (IS_TYPE_OR_DECL_P (t))
317 *walk_subtrees = 0;
319 return NULL;
322 /* Mark all VAR_DECLS under *EXPR_P as used, so that they won't be
323 eliminated during the tree->rtl conversion process. */
325 static inline void
326 mark_all_vars_used (tree *expr_p)
328 walk_tree (expr_p, mark_all_vars_used_1, NULL, NULL);
332 /* Remove local variables that are not referenced in the IL. */
334 void
335 remove_unused_locals (void)
337 basic_block bb;
338 tree t, *cell;
340 /* Assume all locals are unused. */
341 for (t = cfun->unexpanded_var_list; t; t = TREE_CHAIN (t))
343 tree var = TREE_VALUE (t);
344 if (TREE_CODE (var) != FUNCTION_DECL
345 && var_ann (var))
346 var_ann (var)->used = false;
349 /* Walk the CFG marking all referenced symbols. */
350 FOR_EACH_BB (bb)
352 block_stmt_iterator bsi;
353 tree phi, def;
355 /* Walk the statements. */
356 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
357 mark_all_vars_used (bsi_stmt_ptr (bsi));
359 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
361 use_operand_p arg_p;
362 ssa_op_iter i;
364 /* No point processing globals. */
365 if (is_global_var (SSA_NAME_VAR (PHI_RESULT (phi))))
366 continue;
368 def = PHI_RESULT (phi);
369 mark_all_vars_used (&def);
371 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_ALL_USES)
373 tree arg = USE_FROM_PTR (arg_p);
374 mark_all_vars_used (&arg);
379 /* Remove unmarked vars and clear used flag. */
380 for (cell = &cfun->unexpanded_var_list; *cell; )
382 tree var = TREE_VALUE (*cell);
383 var_ann_t ann;
385 if (TREE_CODE (var) != FUNCTION_DECL
386 && (!(ann = var_ann (var))
387 || !ann->used))
389 *cell = TREE_CHAIN (*cell);
390 continue;
393 cell = &TREE_CHAIN (*cell);
397 /* This function looks through the program and uses FLAGS to determine what
398 SSA versioned variables are given entries in a new partition table. This
399 new partition map is returned. */
401 var_map
402 create_ssa_var_map (void)
404 block_stmt_iterator bsi;
405 basic_block bb;
406 tree var;
407 tree stmt;
408 var_map map;
409 ssa_op_iter iter;
410 #ifdef ENABLE_CHECKING
411 bitmap used_in_real_ops;
412 bitmap used_in_virtual_ops;
413 #endif
415 map = init_var_map (num_ssa_names + 1);
417 #ifdef ENABLE_CHECKING
418 used_in_real_ops = BITMAP_ALLOC (NULL);
419 used_in_virtual_ops = BITMAP_ALLOC (NULL);
420 #endif
422 FOR_EACH_BB (bb)
424 tree phi, arg;
426 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
428 int i;
429 register_ssa_partition (map, PHI_RESULT (phi));
430 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
432 arg = PHI_ARG_DEF (phi, i);
433 if (TREE_CODE (arg) == SSA_NAME)
434 register_ssa_partition (map, arg);
436 mark_all_vars_used (&PHI_ARG_DEF_TREE (phi, i));
440 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
442 stmt = bsi_stmt (bsi);
444 /* Register USE and DEF operands in each statement. */
445 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
447 register_ssa_partition (map, var);
449 #ifdef ENABLE_CHECKING
450 bitmap_set_bit (used_in_real_ops, DECL_UID (SSA_NAME_VAR (var)));
451 #endif
454 #ifdef ENABLE_CHECKING
455 /* Validate that virtual ops don't get used in funny ways. */
456 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter,
457 SSA_OP_VIRTUAL_USES | SSA_OP_VMUSTDEF)
459 bitmap_set_bit (used_in_virtual_ops,
460 DECL_UID (SSA_NAME_VAR (var)));
463 #endif /* ENABLE_CHECKING */
465 mark_all_vars_used (bsi_stmt_ptr (bsi));
469 #if defined ENABLE_CHECKING
471 unsigned i;
472 bitmap both = BITMAP_ALLOC (NULL);
473 bitmap_and (both, used_in_real_ops, used_in_virtual_ops);
474 if (!bitmap_empty_p (both))
476 bitmap_iterator bi;
478 EXECUTE_IF_SET_IN_BITMAP (both, 0, i, bi)
479 fprintf (stderr, "Variable %s used in real and virtual operands\n",
480 get_name (referenced_var (i)));
481 internal_error ("SSA corruption");
484 BITMAP_FREE (used_in_real_ops);
485 BITMAP_FREE (used_in_virtual_ops);
486 BITMAP_FREE (both);
488 #endif
490 return map;
494 /* Allocate and return a new live range information object base on MAP. */
496 static tree_live_info_p
497 new_tree_live_info (var_map map)
499 tree_live_info_p live;
500 unsigned x;
502 live = (tree_live_info_p) xmalloc (sizeof (struct tree_live_info_d));
503 live->map = map;
504 live->num_blocks = last_basic_block;
506 live->livein = (bitmap *)xmalloc (last_basic_block * sizeof (bitmap));
507 for (x = 0; x < (unsigned)last_basic_block; x++)
508 live->livein[x] = BITMAP_ALLOC (NULL);
510 live->liveout = (bitmap *)xmalloc (last_basic_block * sizeof (bitmap));
511 for (x = 0; x < (unsigned)last_basic_block; x++)
512 live->liveout[x] = BITMAP_ALLOC (NULL);
514 live->work_stack = XNEWVEC (int, last_basic_block);
515 live->stack_top = live->work_stack;
517 live->global = BITMAP_ALLOC (NULL);
518 return live;
522 /* Free storage for live range info object LIVE. */
524 void
525 delete_tree_live_info (tree_live_info_p live)
527 int x;
529 BITMAP_FREE (live->global);
530 free (live->work_stack);
532 for (x = live->num_blocks - 1; x >= 0; x--)
533 BITMAP_FREE (live->liveout[x]);
534 free (live->liveout);
536 for (x = live->num_blocks - 1; x >= 0; x--)
537 BITMAP_FREE (live->livein[x]);
538 free (live->livein);
540 free (live);
544 /* Visit basic block BB, and propogate any required live on entry bits from
545 LIVE into the predecessors. VISITED is the bitmap of visited blocks.
546 TMP is a temporary work bitmap which is passed in to avoid reallocting
547 it each time. */
549 static void
550 loe_visit_block (tree_live_info_p live, basic_block bb, sbitmap visited,
551 bitmap tmp)
553 edge e;
554 bool change;
555 edge_iterator ei;
556 basic_block pred_bb;
557 bitmap loe;
558 gcc_assert (!TEST_BIT (visited, bb->index));
560 SET_BIT (visited, bb->index);
561 loe = live_on_entry (live, bb);
563 FOR_EACH_EDGE (e, ei, bb->preds)
565 pred_bb = e->src;
566 if (pred_bb == ENTRY_BLOCK_PTR)
567 continue;
568 /* tmp is vars live-=on-entry from BB that aren't defined in the
569 pred. block. This should be the live on entry vars to pred.
570 Note that liveout is the DEFs in a block while live on entry is
571 being calculated. */
572 bitmap_and_compl (tmp, loe, live->liveout[pred_bb->index]);
574 /* Add these bits to live-on-entry for the pred. if there are any
575 changes, and pred_bb has been visited already, add it to the
576 revisit stack. */
577 change = bitmap_ior_into (live_on_entry (live, pred_bb), tmp);
578 if (TEST_BIT (visited, pred_bb->index) && change)
580 RESET_BIT (visited, pred_bb->index);
581 *(live->stack_top)++ = pred_bb->index;
587 /* Using LIVE, fill in all the live-on-entry blocks between the defs and uses
588 of all the vairables. */
590 static void
591 live_worklist (tree_live_info_p live)
593 unsigned b;
594 basic_block bb;
595 sbitmap visited = sbitmap_alloc (last_basic_block + 1);
596 bitmap tmp = BITMAP_ALLOC (NULL);
598 sbitmap_zero (visited);
600 /* Visit all the blocks in reverse order and propogate live on entry values
601 into the predecessors blocks. */
602 FOR_EACH_BB_REVERSE (bb)
603 loe_visit_block (live, bb, visited, tmp);
605 /* Process any blocks which require further iteration. */
606 while (live->stack_top != live->work_stack)
608 b = *--(live->stack_top);
609 loe_visit_block (live, BASIC_BLOCK (b), visited, tmp);
612 BITMAP_FREE (tmp);
613 sbitmap_free (visited);
617 /* Calulate the initial live on entry vector for SSA_NAME using immediate_use
618 links. Set the live on entry fields in LIVE. Def's are marked temporarily
619 in the liveout vector. */
621 static void
622 set_var_live_on_entry (tree ssa_name, tree_live_info_p live)
624 int p;
625 tree stmt;
626 use_operand_p use;
627 basic_block def_bb = NULL;
628 imm_use_iterator imm_iter;
629 bool global = false;
631 p = var_to_partition (live->map, ssa_name);
632 if (p == NO_PARTITION)
633 return;
635 stmt = SSA_NAME_DEF_STMT (ssa_name);
636 if (stmt)
638 def_bb = bb_for_stmt (stmt);
639 /* Mark defs in liveout bitmap for now. */
640 if (def_bb)
641 bitmap_set_bit (live->liveout[def_bb->index], p);
643 else
644 def_bb = ENTRY_BLOCK_PTR;
646 /* Visit each use of SSA_NAME and if it isn't in the same block as the def,
647 add it to the list of live on entry blocks. */
648 FOR_EACH_IMM_USE_FAST (use, imm_iter, ssa_name)
650 tree use_stmt = USE_STMT (use);
651 basic_block add_block = NULL;
653 if (TREE_CODE (use_stmt) == PHI_NODE)
655 /* Uses in PHI's are considered to be live at exit of the SRC block
656 as this is where a copy would be inserted. Check to see if it is
657 defined in that block, or whether its live on entry. */
658 int index = PHI_ARG_INDEX_FROM_USE (use);
659 edge e = PHI_ARG_EDGE (use_stmt, index);
660 if (e->src != ENTRY_BLOCK_PTR)
662 if (e->src != def_bb)
663 add_block = e->src;
666 else
668 /* If its not defined in this block, its live on entry. */
669 basic_block use_bb = bb_for_stmt (use_stmt);
670 if (use_bb != def_bb)
671 add_block = use_bb;
674 /* If there was a live on entry use, set the bit. */
675 if (add_block)
677 global = true;
678 bitmap_set_bit (live->livein[add_block->index], p);
682 /* If SSA_NAME is live on entry to at least one block, fill in all the live
683 on entry blocks between the def and all the uses. */
684 if (global)
685 bitmap_set_bit (live->global, p);
689 /* Calculate the live on exit vectors based on the entry info in LIVEINFO. */
691 void
692 calculate_live_on_exit (tree_live_info_p liveinfo)
694 unsigned i;
695 int p;
696 tree t, phi;
697 basic_block bb;
698 edge e;
699 edge_iterator ei;
701 /* live on entry calculations used the liveouit vector for defs. */
702 FOR_EACH_BB (bb)
703 bitmap_clear (liveinfo->liveout[bb->index]);
705 /* Set all the live-on-exit bits for uses in PHIs. */
706 FOR_EACH_BB (bb)
708 /* Mark the PHI arguments which are live on exit to the pred block. */
709 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
710 for (i = 0; i < (unsigned)PHI_NUM_ARGS (phi); i++)
712 t = PHI_ARG_DEF (phi, i);
713 if (TREE_CODE (t) != SSA_NAME)
714 continue;
715 p = var_to_partition (liveinfo->map, t);
716 if (p == NO_PARTITION)
717 continue;
718 e = PHI_ARG_EDGE (phi, i);
719 if (e->src != ENTRY_BLOCK_PTR)
720 bitmap_set_bit (liveinfo->liveout[e->src->index], p);
723 /* add each successors live on entry to this bock live on exit. */
724 FOR_EACH_EDGE (e, ei, bb->succs)
725 if (e->dest != EXIT_BLOCK_PTR)
726 bitmap_ior_into (liveinfo->liveout[bb->index],
727 live_on_entry (liveinfo, e->dest));
731 /* Given partition map MAP, calculate all the live on entry bitmaps for
732 each partition. Return a new live info object. */
734 tree_live_info_p
735 calculate_live_ranges (var_map map)
737 tree var;
738 unsigned i;
739 tree_live_info_p live;
741 live = new_tree_live_info (map);
742 for (i = 0; i < num_var_partitions (map); i++)
744 var = partition_to_var (map, i);
745 if (var != NULL_TREE)
746 set_var_live_on_entry (var, live);
749 live_worklist (live);
751 #ifdef ENABLE_CHECKING
752 verify_live_on_entry (live);
753 #endif
755 calculate_live_on_exit (live);
756 return live;
760 /* Initialize a tree_partition_associator object using MAP. */
762 static tpa_p
763 tpa_init (var_map map)
765 tpa_p tpa;
766 int num_partitions = num_var_partitions (map);
767 int x;
769 if (num_partitions == 0)
770 return NULL;
772 tpa = (tpa_p) xmalloc (sizeof (struct tree_partition_associator_d));
773 tpa->num_trees = 0;
774 tpa->uncompressed_num = -1;
775 tpa->map = map;
776 tpa->next_partition = (int *)xmalloc (num_partitions * sizeof (int));
777 memset (tpa->next_partition, TPA_NONE, num_partitions * sizeof (int));
779 tpa->partition_to_tree_map = (int *)xmalloc (num_partitions * sizeof (int));
780 memset (tpa->partition_to_tree_map, TPA_NONE, num_partitions * sizeof (int));
782 x = MAX (40, (num_partitions / 20));
783 tpa->trees = VEC_alloc (tree, heap, x);
784 tpa->first_partition = VEC_alloc (int, heap, x);
786 return tpa;
791 /* Remove PARTITION_INDEX from TREE_INDEX's list in the tpa structure TPA. */
793 void
794 tpa_remove_partition (tpa_p tpa, int tree_index, int partition_index)
796 int i;
798 i = tpa_first_partition (tpa, tree_index);
799 if (i == partition_index)
801 VEC_replace (int, tpa->first_partition, tree_index,
802 tpa->next_partition[i]);
804 else
806 for ( ; i != TPA_NONE; i = tpa_next_partition (tpa, i))
808 if (tpa->next_partition[i] == partition_index)
810 tpa->next_partition[i] = tpa->next_partition[partition_index];
811 break;
818 /* Free the memory used by tree_partition_associator object TPA. */
820 void
821 tpa_delete (tpa_p tpa)
823 if (!tpa)
824 return;
826 VEC_free (tree, heap, tpa->trees);
827 VEC_free (int, heap, tpa->first_partition);
828 free (tpa->partition_to_tree_map);
829 free (tpa->next_partition);
830 free (tpa);
834 /* This function will remove any tree entries from TPA which have only a single
835 element. This will help keep the size of the conflict graph down. The
836 function returns the number of remaining tree lists. */
838 int
839 tpa_compact (tpa_p tpa)
841 int last, x, y, first, swap_i;
842 tree swap_t;
844 /* Find the last list which has more than 1 partition. */
845 for (last = tpa->num_trees - 1; last > 0; last--)
847 first = tpa_first_partition (tpa, last);
848 if (tpa_next_partition (tpa, first) != NO_PARTITION)
849 break;
852 x = 0;
853 while (x < last)
855 first = tpa_first_partition (tpa, x);
857 /* If there is not more than one partition, swap with the current end
858 of the tree list. */
859 if (tpa_next_partition (tpa, first) == NO_PARTITION)
861 swap_t = VEC_index (tree, tpa->trees, last);
862 swap_i = VEC_index (int, tpa->first_partition, last);
864 /* Update the last entry. Since it is known to only have one
865 partition, there is nothing else to update. */
866 VEC_replace (tree, tpa->trees, last,
867 VEC_index (tree, tpa->trees, x));
868 VEC_replace (int, tpa->first_partition, last,
869 VEC_index (int, tpa->first_partition, x));
870 tpa->partition_to_tree_map[tpa_first_partition (tpa, last)] = last;
872 /* Since this list is known to have more than one partition, update
873 the list owner entries. */
874 VEC_replace (tree, tpa->trees, x, swap_t);
875 VEC_replace (int, tpa->first_partition, x, swap_i);
876 for (y = tpa_first_partition (tpa, x);
877 y != NO_PARTITION;
878 y = tpa_next_partition (tpa, y))
879 tpa->partition_to_tree_map[y] = x;
881 /* Ensure last is a list with more than one partition. */
882 last--;
883 for (; last > x; last--)
885 first = tpa_first_partition (tpa, last);
886 if (tpa_next_partition (tpa, first) != NO_PARTITION)
887 break;
890 x++;
893 first = tpa_first_partition (tpa, x);
894 if (tpa_next_partition (tpa, first) != NO_PARTITION)
895 x++;
896 tpa->uncompressed_num = tpa->num_trees;
897 tpa->num_trees = x;
898 return last;
902 /* Initialize a root_var object with SSA partitions from MAP which are based
903 on each root variable. */
905 root_var_p
906 root_var_init (var_map map)
908 root_var_p rv;
909 int num_partitions = num_var_partitions (map);
910 int x, p;
911 tree t;
912 var_ann_t ann;
913 sbitmap seen;
915 rv = tpa_init (map);
916 if (!rv)
917 return NULL;
919 seen = sbitmap_alloc (num_partitions);
920 sbitmap_zero (seen);
922 /* Start at the end and work towards the front. This will provide a list
923 that is ordered from smallest to largest. */
924 for (x = num_partitions - 1; x >= 0; x--)
926 t = partition_to_var (map, x);
928 /* The var map may not be compacted yet, so check for NULL. */
929 if (!t)
930 continue;
932 p = var_to_partition (map, t);
934 gcc_assert (p != NO_PARTITION);
936 /* Make sure we only put coalesced partitions into the list once. */
937 if (TEST_BIT (seen, p))
938 continue;
939 SET_BIT (seen, p);
940 if (TREE_CODE (t) == SSA_NAME)
941 t = SSA_NAME_VAR (t);
942 ann = var_ann (t);
943 if (ann->root_var_processed)
945 rv->next_partition[p] = VEC_index (int, rv->first_partition,
946 VAR_ANN_ROOT_INDEX (ann));
947 VEC_replace (int, rv->first_partition, VAR_ANN_ROOT_INDEX (ann), p);
949 else
951 ann->root_var_processed = 1;
952 VAR_ANN_ROOT_INDEX (ann) = rv->num_trees++;
953 VEC_safe_push (tree, heap, rv->trees, t);
954 VEC_safe_push (int, heap, rv->first_partition, p);
956 rv->partition_to_tree_map[p] = VAR_ANN_ROOT_INDEX (ann);
959 /* Reset the out_of_ssa_tag flag on each variable for later use. */
960 for (x = 0; x < rv->num_trees; x++)
962 t = VEC_index (tree, rv->trees, x);
963 var_ann (t)->root_var_processed = 0;
966 sbitmap_free (seen);
967 return rv;
971 /* Hash function for 2 integer coalesce pairs. */
972 #define COALESCE_HASH_FN(R1, R2) ((R2) * ((R2) - 1) / 2 + (R1))
975 /* Return hash value for partition pair PAIR. */
977 unsigned int
978 partition_pair_map_hash (const void *pair)
980 hashval_t a = (hashval_t)(((partition_pair_p)pair)->first_partition);
981 hashval_t b = (hashval_t)(((partition_pair_p)pair)->second_partition);
983 return COALESCE_HASH_FN (a,b);
987 /* Return TRUE if PAIR1 is equivilent to PAIR2. */
989 int
990 partition_pair_map_eq (const void *pair1, const void *pair2)
992 partition_pair_p p1 = (partition_pair_p) pair1;
993 partition_pair_p p2 = (partition_pair_p) pair2;
995 return (p1->first_partition == p2->first_partition
996 && p1->second_partition == p2->second_partition);
1000 /* Create a new coalesce list object from MAP and return it. */
1002 coalesce_list_p
1003 create_coalesce_list (var_map map)
1005 coalesce_list_p list;
1006 unsigned size = num_ssa_names * 3;
1008 if (size < 40)
1009 size = 40;
1011 list = xmalloc (sizeof (struct coalesce_list_d));
1012 list->list = htab_create (size, partition_pair_map_hash,
1013 partition_pair_map_eq, NULL);
1015 list->map = map;
1016 list->sorted = NULL;
1017 list->add_mode = true;
1018 list->num_sorted = 0;
1019 return list;
1023 /* Delete coalesce list CL. */
1025 void
1026 delete_coalesce_list (coalesce_list_p cl)
1028 htab_delete (cl->list);
1029 if (cl->sorted)
1030 free (cl->sorted);
1031 gcc_assert (cl->num_sorted == 0);
1032 free (cl);
1036 /* Find a matching coalesce pair object in CL for partitions P1 and P2. If
1037 one isn't found, return NULL if CREATE is false, otherwise create a new
1038 coalesce pair object and return it. */
1040 static partition_pair_p
1041 find_partition_pair (coalesce_list_p cl, int p1, int p2, bool create)
1043 struct partition_pair p, *pair;
1044 void **slot;
1045 unsigned int hash;
1047 /* normalize so that p1 is the smaller value. */
1048 if (p2 < p1)
1050 p.first_partition = p2;
1051 p.second_partition = p1;
1053 else
1055 p.first_partition = p1;
1056 p.second_partition = p2;
1060 hash = partition_pair_map_hash (&p);
1061 pair = (struct partition_pair *) htab_find_with_hash (cl->list, &p, hash);
1063 if (create && !pair)
1065 gcc_assert (cl->add_mode);
1066 pair = xmalloc (sizeof (struct partition_pair));
1067 pair->first_partition = p.first_partition;
1068 pair->second_partition = p.second_partition;
1069 pair->cost = 0;
1070 slot = htab_find_slot_with_hash (cl->list, pair, hash, INSERT);
1071 *(struct partition_pair **)slot = pair;
1074 return pair;
1077 /* Return cost of execution of copy instruction with FREQUENCY
1078 possibly on CRITICAL edge and in HOT basic block. */
1080 coalesce_cost (int frequency, bool hot, bool critical)
1082 /* Base costs on BB frequencies bounded by 1. */
1083 int cost = frequency;
1085 if (!cost)
1086 cost = 1;
1087 if (optimize_size || hot)
1088 cost = 1;
1089 /* Inserting copy on critical edge costs more
1090 than inserting it elsewhere. */
1091 if (critical)
1092 cost *= 2;
1093 return cost;
1096 /* Add a potential coalesce between P1 and P2 in CL with a cost of VALUE. */
1098 void
1099 add_coalesce (coalesce_list_p cl, int p1, int p2,
1100 int value)
1102 partition_pair_p node;
1104 gcc_assert (cl->add_mode);
1106 if (p1 == p2)
1107 return;
1109 node = find_partition_pair (cl, p1, p2, true);
1111 node->cost += value;
1115 /* Comparison function to allow qsort to sort P1 and P2 in Ascendiong order. */
1117 static
1118 int compare_pairs (const void *p1, const void *p2)
1120 return (*(partition_pair_p *)p1)->cost - (*(partition_pair_p *)p2)->cost;
1124 static inline int
1125 num_coalesce_pairs (coalesce_list_p cl)
1127 return htab_elements (cl->list);
1130 typedef struct
1132 htab_iterator hti;
1133 } partition_pair_iterator;
1135 static inline partition_pair_p
1136 first_partition_pair (coalesce_list_p cl, partition_pair_iterator *iter)
1138 partition_pair_p pair;
1140 pair = (partition_pair_p) first_htab_element (&(iter->hti), cl->list);
1141 return pair;
1144 static inline bool
1145 end_partition_pair_p (partition_pair_iterator *iter)
1147 return end_htab_p (&(iter->hti));
1150 static inline partition_pair_p
1151 next_partition_pair (partition_pair_iterator *iter)
1153 partition_pair_p pair;
1155 pair = (partition_pair_p) next_htab_element (&(iter->hti));
1156 return pair;
1159 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
1160 for ((PAIR) = first_partition_pair ((CL), &(ITER)); \
1161 !end_partition_pair_p (&(ITER)); \
1162 (PAIR) = next_partition_pair (&(ITER)))
1165 /* Prepare CL for removal of preferred pairs. When finished, list element
1166 0 has all the coalesce pairs, sorted in order from most important coalesce
1167 to least important. */
1169 void
1170 sort_coalesce_list (coalesce_list_p cl)
1172 unsigned x, num;
1173 partition_pair_p p;
1174 partition_pair_iterator ppi;
1176 gcc_assert (cl->add_mode);
1178 cl->add_mode = false;
1180 /* allocate a vector for the pair pointers. */
1181 num = num_coalesce_pairs (cl);
1182 cl->num_sorted = num;
1183 if (num == 0)
1184 return;
1185 cl->sorted = XNEWVEC (partition_pair_p, num);
1187 /* Populate the vector with pointers to the partition pairs. */
1189 x = 0;
1190 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
1191 cl->sorted[x++] = p;
1192 gcc_assert (x == num);
1194 if (num == 1)
1195 return;
1197 if (num == 2)
1199 if (cl->sorted[0]->cost > cl->sorted[1]->cost)
1201 p = cl->sorted[0];
1202 cl->sorted[0] = cl->sorted[1];
1203 cl->sorted[1] = p;
1205 return;
1208 /* Only call qsort if there are more than 2 items. */
1209 if (num > 2)
1210 qsort (cl->sorted, num, sizeof (partition_pair_p), compare_pairs);
1214 /* Retrieve the best remaining pair to coalesce from CL. Returns the 2
1215 partitions via P1 and P2. Their calculated cost is returned by the function.
1216 NO_BEST_COALESCE is returned if the coalesce list is empty. */
1218 static int
1219 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
1221 partition_pair_p node;
1222 int ret;
1224 gcc_assert (!cl->add_mode);
1226 if (cl->num_sorted == 0)
1227 return NO_BEST_COALESCE;
1229 node = cl->sorted[--(cl->num_sorted)];
1231 *p1 = node->first_partition;
1232 *p2 = node->second_partition;
1233 ret = node->cost;
1234 free (node);
1236 return ret;
1240 /* If variable VAR is in a partition in MAP, add a conflict in GRAPH between
1241 VAR and any other live partitions in VEC which are associated via TPA.
1242 Reset the live bit in VEC. */
1244 static inline void
1245 add_conflicts_if_valid (tpa_p tpa, conflict_graph graph,
1246 var_map map, bitmap vec, tree var)
1248 int p, y, first;
1249 p = var_to_partition (map, var);
1250 if (p != NO_PARTITION)
1252 bitmap_clear_bit (vec, p);
1253 first = tpa_find_tree (tpa, p);
1254 /* If find returns nothing, this object isn't interesting. */
1255 if (first == TPA_NONE)
1256 return;
1257 /* Only add interferences between objects in the same list. */
1258 for (y = tpa_first_partition (tpa, first);
1259 y != TPA_NONE;
1260 y = tpa_next_partition (tpa, y))
1262 if (bitmap_bit_p (vec, y))
1263 conflict_graph_add (graph, p, y);
1269 /* If VAR is in a partition of MAP, set the bit for that partition in VEC. */
1271 static inline void
1272 set_if_valid (var_map map, bitmap vec, tree var)
1274 int p = var_to_partition (map, var);
1275 if (p != NO_PARTITION)
1276 bitmap_set_bit (vec, p);
1279 /* Return a conflict graph for the information contained in LIVE_INFO. Only
1280 conflicts between items in the same TPA list are added. If optional
1281 coalesce list CL is passed in, any copies encountered are added. */
1283 conflict_graph
1284 build_tree_conflict_graph (tree_live_info_p liveinfo, tpa_p tpa,
1285 coalesce_list_p cl)
1287 conflict_graph graph;
1288 var_map map;
1289 bitmap live;
1290 unsigned x, y, i;
1291 basic_block bb;
1292 int *partition_link, *tpa_nodes;
1293 VEC(int,heap) *tpa_to_clear;
1294 unsigned l;
1295 ssa_op_iter iter;
1296 bitmap_iterator bi;
1298 map = live_var_map (liveinfo);
1299 graph = conflict_graph_new (num_var_partitions (map));
1301 if (tpa_num_trees (tpa) == 0)
1302 return graph;
1304 live = BITMAP_ALLOC (NULL);
1306 partition_link = XCNEWVEC (int, num_var_partitions (map) + 1);
1307 tpa_nodes = XCNEWVEC (int, tpa_num_trees (tpa));
1308 tpa_to_clear = VEC_alloc (int, heap, 50);
1310 FOR_EACH_BB (bb)
1312 block_stmt_iterator bsi;
1313 tree phi;
1314 int idx;
1316 /* Start with live on exit temporaries. */
1317 bitmap_copy (live, live_on_exit (liveinfo, bb));
1319 for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_prev (&bsi))
1321 bool is_a_copy = false;
1322 tree stmt = bsi_stmt (bsi);
1324 /* A copy between 2 partitions does not introduce an interference
1325 by itself. If they did, you would never be able to coalesce
1326 two things which are copied. If the two variables really do
1327 conflict, they will conflict elsewhere in the program.
1329 This is handled specially here since we may also be interested
1330 in copies between real variables and SSA_NAME variables. We may
1331 be interested in trying to coalesce SSA_NAME variables with
1332 root variables in some cases. */
1334 if (TREE_CODE (stmt) == MODIFY_EXPR)
1336 tree lhs = TREE_OPERAND (stmt, 0);
1337 tree rhs = TREE_OPERAND (stmt, 1);
1338 int p1, p2;
1339 int bit;
1341 if (DECL_P (lhs) || TREE_CODE (lhs) == SSA_NAME)
1342 p1 = var_to_partition (map, lhs);
1343 else
1344 p1 = NO_PARTITION;
1346 if (DECL_P (rhs) || TREE_CODE (rhs) == SSA_NAME)
1347 p2 = var_to_partition (map, rhs);
1348 else
1349 p2 = NO_PARTITION;
1351 if (p1 != NO_PARTITION && p2 != NO_PARTITION)
1353 is_a_copy = true;
1354 bit = bitmap_bit_p (live, p2);
1355 /* If the RHS is live, make it not live while we add
1356 the conflicts, then make it live again. */
1357 if (bit)
1358 bitmap_clear_bit (live, p2);
1359 add_conflicts_if_valid (tpa, graph, map, live, lhs);
1360 if (bit)
1361 bitmap_set_bit (live, p2);
1362 if (cl)
1363 add_coalesce (cl, p1, p2,
1364 coalesce_cost (bb->frequency,
1365 maybe_hot_bb_p (bb), false));
1366 set_if_valid (map, live, rhs);
1370 if (!is_a_copy)
1372 tree var;
1373 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
1375 add_conflicts_if_valid (tpa, graph, map, live, var);
1378 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
1380 set_if_valid (map, live, var);
1385 /* If result of a PHI is unused, then the loops over the statements
1386 will not record any conflicts. However, since the PHI node is
1387 going to be translated out of SSA form we must record a conflict
1388 between the result of the PHI and any variables with are live.
1389 Otherwise the out-of-ssa translation may create incorrect code. */
1390 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1392 tree result = PHI_RESULT (phi);
1393 int p = var_to_partition (map, result);
1395 if (p != NO_PARTITION && ! bitmap_bit_p (live, p))
1396 add_conflicts_if_valid (tpa, graph, map, live, result);
1399 /* Anything which is still live at this point interferes.
1400 In order to implement this efficiently, only conflicts between
1401 partitions which have the same TPA root need be added.
1402 TPA roots which have been seen are tracked in 'tpa_nodes'. A nonzero
1403 entry points to an index into 'partition_link', which then indexes
1404 into itself forming a linked list of partitions sharing a tpa root
1405 which have been seen as live up to this point. Since partitions start
1406 at index zero, all entries in partition_link are (partition + 1).
1408 Conflicts are added between the current partition and any already seen.
1409 tpa_clear contains all the tpa_roots processed, and these are the only
1410 entries which need to be zero'd out for a clean restart. */
1412 EXECUTE_IF_SET_IN_BITMAP (live, 0, x, bi)
1414 i = tpa_find_tree (tpa, x);
1415 if (i != (unsigned)TPA_NONE)
1417 int start = tpa_nodes[i];
1418 /* If start is 0, a new root reference list is being started.
1419 Register it to be cleared. */
1420 if (!start)
1421 VEC_safe_push (int, heap, tpa_to_clear, i);
1423 /* Add interferences to other tpa members seen. */
1424 for (y = start; y != 0; y = partition_link[y])
1425 conflict_graph_add (graph, x, y - 1);
1426 tpa_nodes[i] = x + 1;
1427 partition_link[x + 1] = start;
1431 /* Now clear the used tpa root references. */
1432 for (l = 0; VEC_iterate (int, tpa_to_clear, l, idx); l++)
1433 tpa_nodes[idx] = 0;
1434 VEC_truncate (int, tpa_to_clear, 0);
1437 free (tpa_nodes);
1438 free (partition_link);
1439 VEC_free (int, heap, tpa_to_clear);
1440 BITMAP_FREE (live);
1441 return graph;
1445 /* This routine will attempt to coalesce the elements in TPA subject to the
1446 conflicts found in GRAPH. If optional coalesce_list CL is provided,
1447 only coalesces specified within the coalesce list are attempted. Otherwise
1448 an attempt is made to coalesce as many partitions within each TPA grouping
1449 as possible. If DEBUG is provided, debug output will be sent there. */
1451 void
1452 coalesce_tpa_members (tpa_p tpa, conflict_graph graph, var_map map,
1453 coalesce_list_p cl, FILE *debug)
1455 int x, y, z, w;
1456 tree var, tmp;
1458 /* Attempt to coalesce any items in a coalesce list. */
1459 if (cl)
1461 while (pop_best_coalesce (cl, &x, &y) != NO_BEST_COALESCE)
1463 if (debug)
1465 fprintf (debug, "Coalesce list: (%d)", x);
1466 print_generic_expr (debug, partition_to_var (map, x), TDF_SLIM);
1467 fprintf (debug, " & (%d)", y);
1468 print_generic_expr (debug, partition_to_var (map, y), TDF_SLIM);
1471 w = tpa_find_tree (tpa, x);
1472 z = tpa_find_tree (tpa, y);
1473 if (w != z || w == TPA_NONE || z == TPA_NONE)
1475 if (debug)
1477 if (w != z)
1478 fprintf (debug, ": Fail, Non-matching TPA's\n");
1479 if (w == TPA_NONE)
1480 fprintf (debug, ": Fail %d non TPA.\n", x);
1481 else
1482 fprintf (debug, ": Fail %d non TPA.\n", y);
1484 continue;
1486 var = partition_to_var (map, x);
1487 tmp = partition_to_var (map, y);
1488 x = var_to_partition (map, var);
1489 y = var_to_partition (map, tmp);
1490 if (debug)
1491 fprintf (debug, " [map: %d, %d] ", x, y);
1492 if (x == y)
1494 if (debug)
1495 fprintf (debug, ": Already Coalesced.\n");
1496 continue;
1498 if (!conflict_graph_conflict_p (graph, x, y))
1500 z = var_union (map, var, tmp);
1501 if (z == NO_PARTITION)
1503 if (debug)
1504 fprintf (debug, ": Unable to perform partition union.\n");
1505 continue;
1508 /* z is the new combined partition. We need to remove the other
1509 partition from the list. Set x to be that other partition. */
1510 if (z == x)
1512 conflict_graph_merge_regs (graph, x, y);
1513 w = tpa_find_tree (tpa, y);
1514 tpa_remove_partition (tpa, w, y);
1516 else
1518 conflict_graph_merge_regs (graph, y, x);
1519 w = tpa_find_tree (tpa, x);
1520 tpa_remove_partition (tpa, w, x);
1523 if (debug)
1524 fprintf (debug, ": Success -> %d\n", z);
1526 else
1527 if (debug)
1528 fprintf (debug, ": Fail due to conflict\n");
1530 /* If using a coalesce list, don't try to coalesce anything else. */
1531 return;
1534 for (x = 0; x < tpa_num_trees (tpa); x++)
1536 while (tpa_first_partition (tpa, x) != TPA_NONE)
1538 int p1, p2;
1539 /* Coalesce first partition with anything that doesn't conflict. */
1540 y = tpa_first_partition (tpa, x);
1541 tpa_remove_partition (tpa, x, y);
1543 var = partition_to_var (map, y);
1544 /* p1 is the partition representative to which y belongs. */
1545 p1 = var_to_partition (map, var);
1547 for (z = tpa_next_partition (tpa, y);
1548 z != TPA_NONE;
1549 z = tpa_next_partition (tpa, z))
1551 tmp = partition_to_var (map, z);
1552 /* p2 is the partition representative to which z belongs. */
1553 p2 = var_to_partition (map, tmp);
1554 if (debug)
1556 fprintf (debug, "Coalesce : ");
1557 print_generic_expr (debug, var, TDF_SLIM);
1558 fprintf (debug, " &");
1559 print_generic_expr (debug, tmp, TDF_SLIM);
1560 fprintf (debug, " (%d ,%d)", p1, p2);
1563 /* If partitions are already merged, don't check for conflict. */
1564 if (tmp == var)
1566 tpa_remove_partition (tpa, x, z);
1567 if (debug)
1568 fprintf (debug, ": Already coalesced\n");
1570 else
1571 if (!conflict_graph_conflict_p (graph, p1, p2))
1573 int v;
1574 if (tpa_find_tree (tpa, y) == TPA_NONE
1575 || tpa_find_tree (tpa, z) == TPA_NONE)
1577 if (debug)
1578 fprintf (debug, ": Fail non-TPA member\n");
1579 continue;
1581 if ((v = var_union (map, var, tmp)) == NO_PARTITION)
1583 if (debug)
1584 fprintf (debug, ": Fail cannot combine partitions\n");
1585 continue;
1588 tpa_remove_partition (tpa, x, z);
1589 if (v == p1)
1590 conflict_graph_merge_regs (graph, v, z);
1591 else
1593 /* Update the first partition's representative. */
1594 conflict_graph_merge_regs (graph, v, y);
1595 p1 = v;
1598 /* The root variable of the partition may be changed
1599 now. */
1600 var = partition_to_var (map, p1);
1602 if (debug)
1603 fprintf (debug, ": Success -> %d\n", v);
1605 else
1606 if (debug)
1607 fprintf (debug, ": Fail, Conflict\n");
1614 /* Send debug info for coalesce list CL to file F. */
1616 void
1617 dump_coalesce_list (FILE *f, coalesce_list_p cl)
1619 partition_pair_p node;
1620 partition_pair_iterator ppi;
1621 int x;
1622 tree var;
1624 if (cl->add_mode)
1626 fprintf (f, "Coalesce List:\n");
1627 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
1629 tree var1 = partition_to_var (cl->map, node->first_partition);
1630 tree var2 = partition_to_var (cl->map, node->second_partition);
1631 print_generic_expr (f, var1, TDF_SLIM);
1632 fprintf (f, " <-> ");
1633 print_generic_expr (f, var2, TDF_SLIM);
1634 fprintf (f, " (%1d), ", node->cost);
1635 fprintf (f, "\n");
1638 else
1640 fprintf (f, "Sorted Coalesce list:\n");
1641 for (x = cl->num_sorted - 1 ; x >=0; x--)
1643 node = cl->sorted[x];
1644 fprintf (f, "(%d) ", node->cost);
1645 var = partition_to_var (cl->map, node->first_partition);
1646 print_generic_expr (f, var, TDF_SLIM);
1647 fprintf (f, " <-> ");
1648 var = partition_to_var (cl->map, node->second_partition);
1649 print_generic_expr (f, var, TDF_SLIM);
1650 fprintf (f, "\n");
1656 /* Output tree_partition_associator object TPA to file F.. */
1658 void
1659 tpa_dump (FILE *f, tpa_p tpa)
1661 int x, i;
1663 if (!tpa)
1664 return;
1666 for (x = 0; x < tpa_num_trees (tpa); x++)
1668 print_generic_expr (f, tpa_tree (tpa, x), TDF_SLIM);
1669 fprintf (f, " : (");
1670 for (i = tpa_first_partition (tpa, x);
1671 i != TPA_NONE;
1672 i = tpa_next_partition (tpa, i))
1674 fprintf (f, "(%d)",i);
1675 print_generic_expr (f, partition_to_var (tpa->map, i), TDF_SLIM);
1676 fprintf (f, " ");
1678 #ifdef ENABLE_CHECKING
1679 if (tpa_find_tree (tpa, i) != x)
1680 fprintf (f, "**find tree incorrectly set** ");
1681 #endif
1684 fprintf (f, ")\n");
1686 fflush (f);
1690 /* Output partition map MAP to file F. */
1692 void
1693 dump_var_map (FILE *f, var_map map)
1695 int t;
1696 unsigned x, y;
1697 int p;
1699 fprintf (f, "\nPartition map \n\n");
1701 for (x = 0; x < map->num_partitions; x++)
1703 if (map->compact_to_partition != NULL)
1704 p = map->compact_to_partition[x];
1705 else
1706 p = x;
1708 if (map->partition_to_var[p] == NULL_TREE)
1709 continue;
1711 t = 0;
1712 for (y = 1; y < num_ssa_names; y++)
1714 p = partition_find (map->var_partition, y);
1715 if (map->partition_to_compact)
1716 p = map->partition_to_compact[p];
1717 if (p == (int)x)
1719 if (t++ == 0)
1721 fprintf(f, "Partition %d (", x);
1722 print_generic_expr (f, partition_to_var (map, p), TDF_SLIM);
1723 fprintf (f, " - ");
1725 fprintf (f, "%d ", y);
1728 if (t != 0)
1729 fprintf (f, ")\n");
1731 fprintf (f, "\n");
1735 /* Output live range info LIVE to file F, controlled by FLAG. */
1737 void
1738 dump_live_info (FILE *f, tree_live_info_p live, int flag)
1740 basic_block bb;
1741 unsigned i;
1742 var_map map = live->map;
1743 bitmap_iterator bi;
1745 if ((flag & LIVEDUMP_ENTRY) && live->livein)
1747 FOR_EACH_BB (bb)
1749 fprintf (f, "\nLive on entry to BB%d : ", bb->index);
1750 EXECUTE_IF_SET_IN_BITMAP (live->livein[bb->index], 0, i, bi)
1752 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1753 fprintf (f, " ");
1755 fprintf (f, "\n");
1759 if ((flag & LIVEDUMP_EXIT) && live->liveout)
1761 FOR_EACH_BB (bb)
1763 fprintf (f, "\nLive on exit from BB%d : ", bb->index);
1764 EXECUTE_IF_SET_IN_BITMAP (live->liveout[bb->index], 0, i, bi)
1766 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1767 fprintf (f, " ");
1769 fprintf (f, "\n");
1774 #ifdef ENABLE_CHECKING
1775 void
1776 register_ssa_partition_check (tree ssa_var)
1778 gcc_assert (TREE_CODE (ssa_var) == SSA_NAME);
1779 if (!is_gimple_reg (SSA_NAME_VAR (ssa_var)))
1781 fprintf (stderr, "Illegally registering a virtual SSA name :");
1782 print_generic_expr (stderr, ssa_var, TDF_SLIM);
1783 fprintf (stderr, " in the SSA->Normal phase.\n");
1784 internal_error ("SSA corruption");
1789 /* Verify that the info in LIVE matches the current cfg. */
1790 static void
1791 verify_live_on_entry (tree_live_info_p live)
1793 unsigned i;
1794 tree var;
1795 tree phi, stmt;
1796 basic_block bb;
1797 edge e;
1798 int num;
1799 edge_iterator ei;
1800 var_map map = live->map;
1802 /* Check for live on entry partitions and report those with a DEF in
1803 the program. This will typically mean an optimization has done
1804 something wrong. */
1806 bb = ENTRY_BLOCK_PTR;
1807 num = 0;
1808 FOR_EACH_EDGE (e, ei, bb->succs)
1810 int entry_block = e->dest->index;
1811 if (e->dest == EXIT_BLOCK_PTR)
1812 continue;
1813 for (i = 0; i < (unsigned)num_var_partitions (map); i++)
1815 basic_block tmp;
1816 tree d;
1817 bitmap loe;
1818 var = partition_to_var (map, i);
1819 stmt = SSA_NAME_DEF_STMT (var);
1820 tmp = bb_for_stmt (stmt);
1821 d = gimple_default_def (cfun, SSA_NAME_VAR (var));
1823 loe = live_on_entry (live, e->dest);
1824 if (loe && bitmap_bit_p (loe, i))
1826 if (!IS_EMPTY_STMT (stmt))
1828 num++;
1829 print_generic_expr (stderr, var, TDF_SLIM);
1830 fprintf (stderr, " is defined ");
1831 if (tmp)
1832 fprintf (stderr, " in BB%d, ", tmp->index);
1833 fprintf (stderr, "by:\n");
1834 print_generic_expr (stderr, stmt, TDF_SLIM);
1835 fprintf (stderr, "\nIt is also live-on-entry to entry BB %d",
1836 entry_block);
1837 fprintf (stderr, " So it appears to have multiple defs.\n");
1839 else
1841 if (d != var)
1843 num++;
1844 print_generic_expr (stderr, var, TDF_SLIM);
1845 fprintf (stderr, " is live-on-entry to BB%d ",entry_block);
1846 if (d)
1848 fprintf (stderr, " but is not the default def of ");
1849 print_generic_expr (stderr, d, TDF_SLIM);
1850 fprintf (stderr, "\n");
1852 else
1853 fprintf (stderr, " and there is no default def.\n");
1857 else
1858 if (d == var)
1860 /* The only way this var shouldn't be marked live on entry is
1861 if it occurs in a PHI argument of the block. */
1862 int z, ok = 0;
1863 for (phi = phi_nodes (e->dest);
1864 phi && !ok;
1865 phi = PHI_CHAIN (phi))
1867 for (z = 0; z < PHI_NUM_ARGS (phi); z++)
1868 if (var == PHI_ARG_DEF (phi, z))
1870 ok = 1;
1871 break;
1874 if (ok)
1875 continue;
1876 num++;
1877 print_generic_expr (stderr, var, TDF_SLIM);
1878 fprintf (stderr, " is not marked live-on-entry to entry BB%d ",
1879 entry_block);
1880 fprintf (stderr, "but it is a default def so it should be.\n");
1884 gcc_assert (num <= 0);
1886 #endif