Implement coalesce list with hash table instead of linked list.
[official-gcc.git] / gcc / tree-ssa-live.c
blobe1525c1d0037453a88532bbd9759834d8becd8ba
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, int *, int);
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_livein_if_notdef (tree_live_info_p, bitmap,
47 tree, basic_block);
48 static inline void register_ssa_partition (var_map, tree, bool);
49 static inline void add_conflicts_if_valid (tpa_p, conflict_graph,
50 var_map, bitmap, tree);
51 static partition_pair_p find_partition_pair (coalesce_list_p, int, int, bool);
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 map->ref_count = NULL;
83 return map;
87 /* Free memory associated with MAP. */
89 void
90 delete_var_map (var_map map)
92 free (map->partition_to_var);
93 partition_delete (map->var_partition);
94 if (map->partition_to_compact)
95 free (map->partition_to_compact);
96 if (map->compact_to_partition)
97 free (map->compact_to_partition);
98 if (map->ref_count)
99 free (map->ref_count);
100 free (map);
104 /* This function will combine the partitions in MAP for VAR1 and VAR2. It
105 Returns the partition which represents the new partition. If the two
106 partitions cannot be combined, NO_PARTITION is returned. */
109 var_union (var_map map, tree var1, tree var2)
111 int p1, p2, p3;
112 tree root_var = NULL_TREE;
113 tree other_var = NULL_TREE;
115 /* This is independent of partition_to_compact. If partition_to_compact is
116 on, then whichever one of these partitions is absorbed will never have a
117 dereference into the partition_to_compact array any more. */
119 if (TREE_CODE (var1) == SSA_NAME)
120 p1 = partition_find (map->var_partition, SSA_NAME_VERSION (var1));
121 else
123 p1 = var_to_partition (map, var1);
124 if (map->compact_to_partition)
125 p1 = map->compact_to_partition[p1];
126 root_var = var1;
129 if (TREE_CODE (var2) == SSA_NAME)
130 p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2));
131 else
133 p2 = var_to_partition (map, var2);
134 if (map->compact_to_partition)
135 p2 = map->compact_to_partition[p2];
137 /* If there is no root_var set, or it's not a user variable, set the
138 root_var to this one. */
139 if (!root_var || (DECL_P (root_var) && DECL_IGNORED_P (root_var)))
141 other_var = root_var;
142 root_var = var2;
144 else
145 other_var = var2;
148 gcc_assert (p1 != NO_PARTITION);
149 gcc_assert (p2 != NO_PARTITION);
151 if (p1 == p2)
152 p3 = p1;
153 else
154 p3 = partition_union (map->var_partition, p1, p2);
156 if (map->partition_to_compact)
157 p3 = map->partition_to_compact[p3];
159 if (root_var)
160 change_partition_var (map, root_var, p3);
161 if (other_var)
162 change_partition_var (map, other_var, p3);
164 return p3;
168 /* Compress the partition numbers in MAP such that they fall in the range
169 0..(num_partitions-1) instead of wherever they turned out during
170 the partitioning exercise. This removes any references to unused
171 partitions, thereby allowing bitmaps and other vectors to be much
172 denser. Compression type is controlled by FLAGS.
174 This is implemented such that compaction doesn't affect partitioning.
175 Ie., once partitions are created and possibly merged, running one
176 or more different kind of compaction will not affect the partitions
177 themselves. Their index might change, but all the same variables will
178 still be members of the same partition group. This allows work on reduced
179 sets, and no loss of information when a larger set is later desired.
181 In particular, coalescing can work on partitions which have 2 or more
182 definitions, and then 'recompact' later to include all the single
183 definitions for assignment to program variables. */
185 void
186 compact_var_map (var_map map, int flags)
188 sbitmap used;
189 int tmp, root, root_i;
190 unsigned int x, limit, count;
191 tree var;
192 root_var_p rv = NULL;
194 limit = map->partition_size;
195 used = sbitmap_alloc (limit);
196 sbitmap_zero (used);
198 /* Already compressed? Abandon the old one. */
199 if (map->partition_to_compact)
201 free (map->partition_to_compact);
202 map->partition_to_compact = NULL;
204 if (map->compact_to_partition)
206 free (map->compact_to_partition);
207 map->compact_to_partition = NULL;
210 map->num_partitions = map->partition_size;
212 if (flags & VARMAP_NO_SINGLE_DEFS)
213 rv = root_var_init (map);
215 map->partition_to_compact = (int *)xmalloc (limit * sizeof (int));
216 memset (map->partition_to_compact, 0xff, (limit * sizeof (int)));
218 /* Find out which partitions are actually referenced. */
219 count = 0;
220 for (x = 0; x < limit; x++)
222 tmp = partition_find (map->var_partition, x);
223 if (!TEST_BIT (used, tmp) && map->partition_to_var[tmp] != NULL_TREE)
225 /* It is referenced, check to see if there is more than one version
226 in the root_var table, if one is available. */
227 if (rv)
229 root = root_var_find (rv, tmp);
230 root_i = root_var_first_partition (rv, root);
231 /* If there is only one, don't include this in the compaction. */
232 if (root_var_next_partition (rv, root_i) == ROOT_VAR_NONE)
233 continue;
235 SET_BIT (used, tmp);
236 count++;
240 /* Build a compacted partitioning. */
241 if (count != limit)
243 sbitmap_iterator sbi;
245 map->compact_to_partition = (int *)xmalloc (count * sizeof (int));
246 count = 0;
247 /* SSA renaming begins at 1, so skip 0 when compacting. */
248 EXECUTE_IF_SET_IN_SBITMAP (used, 1, x, sbi)
250 map->partition_to_compact[x] = count;
251 map->compact_to_partition[count] = x;
252 var = map->partition_to_var[x];
253 if (TREE_CODE (var) != SSA_NAME)
254 change_partition_var (map, var, count);
255 count++;
258 else
260 free (map->partition_to_compact);
261 map->partition_to_compact = NULL;
264 map->num_partitions = count;
266 if (rv)
267 root_var_delete (rv);
268 sbitmap_free (used);
272 /* This function is used to change the representative variable in MAP for VAR's
273 partition from an SSA_NAME variable to a regular variable. This allows
274 partitions to be mapped back to real variables. */
276 void
277 change_partition_var (var_map map, tree var, int part)
279 var_ann_t ann;
281 gcc_assert (TREE_CODE (var) != SSA_NAME);
283 ann = var_ann (var);
284 ann->out_of_ssa_tag = 1;
285 VAR_ANN_PARTITION (ann) = part;
286 if (map->compact_to_partition)
287 map->partition_to_var[map->compact_to_partition[part]] = var;
290 static inline void mark_all_vars_used (tree *);
292 /* Helper function for mark_all_vars_used, called via walk_tree. */
294 static tree
295 mark_all_vars_used_1 (tree *tp, int *walk_subtrees,
296 void *data ATTRIBUTE_UNUSED)
298 tree t = *tp;
300 if (TREE_CODE (t) == SSA_NAME)
301 t = SSA_NAME_VAR (t);
303 /* Ignore TREE_ORIGINAL for TARGET_MEM_REFS, as well as other
304 fields that do not contain vars. */
305 if (TREE_CODE (t) == TARGET_MEM_REF)
307 mark_all_vars_used (&TMR_SYMBOL (t));
308 mark_all_vars_used (&TMR_BASE (t));
309 mark_all_vars_used (&TMR_INDEX (t));
310 *walk_subtrees = 0;
311 return NULL;
314 /* Only need to mark VAR_DECLS; parameters and return results are not
315 eliminated as unused. */
316 if (TREE_CODE (t) == VAR_DECL)
317 set_is_used (t);
319 if (IS_TYPE_OR_DECL_P (t))
320 *walk_subtrees = 0;
322 return NULL;
325 /* Mark all VAR_DECLS under *EXPR_P as used, so that they won't be
326 eliminated during the tree->rtl conversion process. */
328 static inline void
329 mark_all_vars_used (tree *expr_p)
331 walk_tree (expr_p, mark_all_vars_used_1, NULL, NULL);
335 /* Remove local variables that are not referenced in the IL. */
337 void
338 remove_unused_locals (void)
340 basic_block bb;
341 tree t, *cell;
343 /* Assume all locals are unused. */
344 for (t = cfun->unexpanded_var_list; t; t = TREE_CHAIN (t))
346 tree var = TREE_VALUE (t);
347 if (TREE_CODE (var) != FUNCTION_DECL
348 && var_ann (var))
349 var_ann (var)->used = false;
352 /* Walk the CFG marking all referenced symbols. */
353 FOR_EACH_BB (bb)
355 block_stmt_iterator bsi;
356 tree phi, def;
358 /* Walk the statements. */
359 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
360 mark_all_vars_used (bsi_stmt_ptr (bsi));
362 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
364 use_operand_p arg_p;
365 ssa_op_iter i;
367 /* No point processing globals. */
368 if (is_global_var (SSA_NAME_VAR (PHI_RESULT (phi))))
369 continue;
371 def = PHI_RESULT (phi);
372 mark_all_vars_used (&def);
374 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_ALL_USES)
376 tree arg = USE_FROM_PTR (arg_p);
377 mark_all_vars_used (&arg);
382 /* Remove unmarked vars and clear used flag. */
383 for (cell = &cfun->unexpanded_var_list; *cell; )
385 tree var = TREE_VALUE (*cell);
386 var_ann_t ann;
388 if (TREE_CODE (var) != FUNCTION_DECL
389 && (!(ann = var_ann (var))
390 || !ann->used))
392 *cell = TREE_CHAIN (*cell);
393 continue;
396 cell = &TREE_CHAIN (*cell);
400 /* This function looks through the program and uses FLAGS to determine what
401 SSA versioned variables are given entries in a new partition table. This
402 new partition map is returned. */
404 var_map
405 create_ssa_var_map (int flags)
407 block_stmt_iterator bsi;
408 basic_block bb;
409 tree dest, use;
410 tree stmt;
411 var_map map;
412 ssa_op_iter iter;
413 #ifdef ENABLE_CHECKING
414 bitmap used_in_real_ops;
415 bitmap used_in_virtual_ops;
416 #endif
418 map = init_var_map (num_ssa_names + 1);
420 #ifdef ENABLE_CHECKING
421 used_in_real_ops = BITMAP_ALLOC (NULL);
422 used_in_virtual_ops = BITMAP_ALLOC (NULL);
423 #endif
425 if (flags & SSA_VAR_MAP_REF_COUNT)
427 map->ref_count
428 = (int *)xmalloc (((num_ssa_names + 1) * sizeof (int)));
429 memset (map->ref_count, 0, (num_ssa_names + 1) * sizeof (int));
432 FOR_EACH_BB (bb)
434 tree phi, arg;
436 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
438 int i;
439 register_ssa_partition (map, PHI_RESULT (phi), false);
440 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
442 arg = PHI_ARG_DEF (phi, i);
443 if (TREE_CODE (arg) == SSA_NAME)
444 register_ssa_partition (map, arg, true);
446 mark_all_vars_used (&PHI_ARG_DEF_TREE (phi, i));
450 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
452 stmt = bsi_stmt (bsi);
454 /* Register USE and DEF operands in each statement. */
455 FOR_EACH_SSA_TREE_OPERAND (use , stmt, iter, SSA_OP_USE)
457 register_ssa_partition (map, use, true);
459 #ifdef ENABLE_CHECKING
460 bitmap_set_bit (used_in_real_ops, DECL_UID (SSA_NAME_VAR (use)));
461 #endif
464 FOR_EACH_SSA_TREE_OPERAND (dest, stmt, iter, SSA_OP_DEF)
466 register_ssa_partition (map, dest, false);
468 #ifdef ENABLE_CHECKING
469 bitmap_set_bit (used_in_real_ops, DECL_UID (SSA_NAME_VAR (dest)));
470 #endif
473 #ifdef ENABLE_CHECKING
474 /* Validate that virtual ops don't get used in funny ways. */
475 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter,
476 SSA_OP_VIRTUAL_USES | SSA_OP_VMUSTDEF)
478 bitmap_set_bit (used_in_virtual_ops,
479 DECL_UID (SSA_NAME_VAR (use)));
482 #endif /* ENABLE_CHECKING */
484 mark_all_vars_used (bsi_stmt_ptr (bsi));
488 #if defined ENABLE_CHECKING
490 unsigned i;
491 bitmap both = BITMAP_ALLOC (NULL);
492 bitmap_and (both, used_in_real_ops, used_in_virtual_ops);
493 if (!bitmap_empty_p (both))
495 bitmap_iterator bi;
497 EXECUTE_IF_SET_IN_BITMAP (both, 0, i, bi)
498 fprintf (stderr, "Variable %s used in real and virtual operands\n",
499 get_name (referenced_var (i)));
500 internal_error ("SSA corruption");
503 BITMAP_FREE (used_in_real_ops);
504 BITMAP_FREE (used_in_virtual_ops);
505 BITMAP_FREE (both);
507 #endif
509 return map;
513 /* Allocate and return a new live range information object base on MAP. */
515 static tree_live_info_p
516 new_tree_live_info (var_map map)
518 tree_live_info_p live;
519 unsigned x;
521 live = (tree_live_info_p) xmalloc (sizeof (struct tree_live_info_d));
522 live->map = map;
523 live->num_blocks = last_basic_block;
525 live->global = BITMAP_ALLOC (NULL);
527 live->livein = (bitmap *)xmalloc (num_var_partitions (map) * sizeof (bitmap));
528 for (x = 0; x < num_var_partitions (map); x++)
529 live->livein[x] = BITMAP_ALLOC (NULL);
531 /* liveout is deferred until it is actually requested. */
532 live->liveout = NULL;
533 return live;
537 /* Free storage for live range info object LIVE. */
539 void
540 delete_tree_live_info (tree_live_info_p live)
542 int x;
543 if (live->liveout)
545 for (x = live->num_blocks - 1; x >= 0; x--)
546 BITMAP_FREE (live->liveout[x]);
547 free (live->liveout);
549 if (live->livein)
551 for (x = num_var_partitions (live->map) - 1; x >= 0; x--)
552 BITMAP_FREE (live->livein[x]);
553 free (live->livein);
555 if (live->global)
556 BITMAP_FREE (live->global);
558 free (live);
562 /* Using LIVE, fill in all the live-on-entry blocks between the defs and uses
563 for partition I. STACK is a varray used for temporary memory which is
564 passed in rather than being allocated on every call. */
566 static void
567 live_worklist (tree_live_info_p live, int *stack, int i)
569 unsigned b;
570 tree var;
571 basic_block def_bb = NULL;
572 edge e;
573 var_map map = live->map;
574 edge_iterator ei;
575 bitmap_iterator bi;
576 int *tos = stack;
578 var = partition_to_var (map, i);
579 if (SSA_NAME_DEF_STMT (var))
580 def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (var));
582 EXECUTE_IF_SET_IN_BITMAP (live->livein[i], 0, b, bi)
584 *tos++ = b;
587 while (tos != stack)
589 b = *--tos;
591 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (b)->preds)
592 if (e->src != ENTRY_BLOCK_PTR)
594 /* Its not live on entry to the block its defined in. */
595 if (e->src == def_bb)
596 continue;
597 if (!bitmap_bit_p (live->livein[i], e->src->index))
599 bitmap_set_bit (live->livein[i], e->src->index);
600 *tos++ = e->src->index;
607 /* If VAR is in a partition of MAP, set the bit for that partition in VEC. */
609 static inline void
610 set_if_valid (var_map map, bitmap vec, tree var)
612 int p = var_to_partition (map, var);
613 if (p != NO_PARTITION)
614 bitmap_set_bit (vec, p);
618 /* If VAR is in a partition and it isn't defined in DEF_VEC, set the livein and
619 global bit for it in the LIVE object. BB is the block being processed. */
621 static inline void
622 add_livein_if_notdef (tree_live_info_p live, bitmap def_vec,
623 tree var, basic_block bb)
625 int p = var_to_partition (live->map, var);
626 if (p == NO_PARTITION || bb == ENTRY_BLOCK_PTR)
627 return;
628 if (!bitmap_bit_p (def_vec, p))
630 bitmap_set_bit (live->livein[p], bb->index);
631 bitmap_set_bit (live->global, p);
636 /* Given partition map MAP, calculate all the live on entry bitmaps for
637 each basic block. Return a live info object. */
639 tree_live_info_p
640 calculate_live_on_entry (var_map map)
642 tree_live_info_p live;
643 unsigned i;
644 basic_block bb;
645 bitmap saw_def;
646 tree phi, var, stmt;
647 tree op;
648 edge e;
649 int *stack;
650 block_stmt_iterator bsi;
651 ssa_op_iter iter;
652 bitmap_iterator bi;
653 #ifdef ENABLE_CHECKING
654 int num;
655 edge_iterator ei;
656 #endif
658 saw_def = BITMAP_ALLOC (NULL);
660 live = new_tree_live_info (map);
662 FOR_EACH_BB (bb)
664 bitmap_clear (saw_def);
666 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
668 for (i = 0; i < (unsigned)PHI_NUM_ARGS (phi); i++)
670 var = PHI_ARG_DEF (phi, i);
671 if (!phi_ssa_name_p (var))
672 continue;
673 stmt = SSA_NAME_DEF_STMT (var);
674 e = EDGE_PRED (bb, i);
676 /* Any uses in PHIs which either don't have def's or are not
677 defined in the block from which the def comes, will be live
678 on entry to that block. */
679 if (!stmt || e->src != bb_for_stmt (stmt))
680 add_livein_if_notdef (live, saw_def, var, e->src);
684 /* Don't mark PHI results as defined until all the PHI nodes have
685 been processed. If the PHI sequence is:
686 a_3 = PHI <a_1, a_2>
687 b_3 = PHI <b_1, a_3>
688 The a_3 referred to in b_3's PHI node is the one incoming on the
689 edge, *not* the PHI node just seen. */
691 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
693 var = PHI_RESULT (phi);
694 set_if_valid (map, saw_def, var);
697 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
699 stmt = bsi_stmt (bsi);
701 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
703 add_livein_if_notdef (live, saw_def, op, bb);
706 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
708 set_if_valid (map, saw_def, op);
713 stack = XNEWVEC (int, last_basic_block);
714 EXECUTE_IF_SET_IN_BITMAP (live->global, 0, i, bi)
716 live_worklist (live, stack, i);
718 free (stack);
720 #ifdef ENABLE_CHECKING
721 /* Check for live on entry partitions and report those with a DEF in
722 the program. This will typically mean an optimization has done
723 something wrong. */
725 bb = ENTRY_BLOCK_PTR;
726 num = 0;
727 FOR_EACH_EDGE (e, ei, bb->succs)
729 int entry_block = e->dest->index;
730 if (e->dest == EXIT_BLOCK_PTR)
731 continue;
732 for (i = 0; i < (unsigned)num_var_partitions (map); i++)
734 basic_block tmp;
735 tree d;
736 var = partition_to_var (map, i);
737 stmt = SSA_NAME_DEF_STMT (var);
738 tmp = bb_for_stmt (stmt);
739 d = gimple_default_def (cfun, SSA_NAME_VAR (var));
741 if (bitmap_bit_p (live_entry_blocks (live, i), entry_block))
743 if (!IS_EMPTY_STMT (stmt))
745 num++;
746 print_generic_expr (stderr, var, TDF_SLIM);
747 fprintf (stderr, " is defined ");
748 if (tmp)
749 fprintf (stderr, " in BB%d, ", tmp->index);
750 fprintf (stderr, "by:\n");
751 print_generic_expr (stderr, stmt, TDF_SLIM);
752 fprintf (stderr, "\nIt is also live-on-entry to entry BB %d",
753 entry_block);
754 fprintf (stderr, " So it appears to have multiple defs.\n");
756 else
758 if (d != var)
760 num++;
761 print_generic_expr (stderr, var, TDF_SLIM);
762 fprintf (stderr, " is live-on-entry to BB%d ",entry_block);
763 if (d)
765 fprintf (stderr, " but is not the default def of ");
766 print_generic_expr (stderr, d, TDF_SLIM);
767 fprintf (stderr, "\n");
769 else
770 fprintf (stderr, " and there is no default def.\n");
774 else
775 if (d == var)
777 /* The only way this var shouldn't be marked live on entry is
778 if it occurs in a PHI argument of the block. */
779 int z, ok = 0;
780 for (phi = phi_nodes (e->dest);
781 phi && !ok;
782 phi = PHI_CHAIN (phi))
784 for (z = 0; z < PHI_NUM_ARGS (phi); z++)
785 if (var == PHI_ARG_DEF (phi, z))
787 ok = 1;
788 break;
791 if (ok)
792 continue;
793 num++;
794 print_generic_expr (stderr, var, TDF_SLIM);
795 fprintf (stderr, " is not marked live-on-entry to entry BB%d ",
796 entry_block);
797 fprintf (stderr, "but it is a default def so it should be.\n");
801 gcc_assert (num <= 0);
802 #endif
804 BITMAP_FREE (saw_def);
806 return live;
810 /* Calculate the live on exit vectors based on the entry info in LIVEINFO. */
812 void
813 calculate_live_on_exit (tree_live_info_p liveinfo)
815 unsigned b;
816 unsigned i, x;
817 bitmap *on_exit;
818 basic_block bb;
819 edge e;
820 tree t, phi;
821 bitmap on_entry;
822 var_map map = liveinfo->map;
824 on_exit = (bitmap *)xmalloc (last_basic_block * sizeof (bitmap));
825 for (x = 0; x < (unsigned)last_basic_block; x++)
826 on_exit[x] = BITMAP_ALLOC (NULL);
828 /* Set all the live-on-exit bits for uses in PHIs. */
829 FOR_EACH_BB (bb)
831 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
832 for (i = 0; i < (unsigned)PHI_NUM_ARGS (phi); i++)
834 t = PHI_ARG_DEF (phi, i);
835 e = PHI_ARG_EDGE (phi, i);
836 if (!phi_ssa_name_p (t) || e->src == ENTRY_BLOCK_PTR)
837 continue;
838 set_if_valid (map, on_exit[e->src->index], t);
842 /* Set live on exit for all predecessors of live on entry's. */
843 for (i = 0; i < num_var_partitions (map); i++)
845 bitmap_iterator bi;
847 on_entry = live_entry_blocks (liveinfo, i);
848 EXECUTE_IF_SET_IN_BITMAP (on_entry, 0, b, bi)
850 edge_iterator ei;
851 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (b)->preds)
852 if (e->src != ENTRY_BLOCK_PTR)
853 bitmap_set_bit (on_exit[e->src->index], i);
857 liveinfo->liveout = on_exit;
861 /* Initialize a tree_partition_associator object using MAP. */
863 static tpa_p
864 tpa_init (var_map map)
866 tpa_p tpa;
867 int num_partitions = num_var_partitions (map);
868 int x;
870 if (num_partitions == 0)
871 return NULL;
873 tpa = (tpa_p) xmalloc (sizeof (struct tree_partition_associator_d));
874 tpa->num_trees = 0;
875 tpa->uncompressed_num = -1;
876 tpa->map = map;
877 tpa->next_partition = (int *)xmalloc (num_partitions * sizeof (int));
878 memset (tpa->next_partition, TPA_NONE, num_partitions * sizeof (int));
880 tpa->partition_to_tree_map = (int *)xmalloc (num_partitions * sizeof (int));
881 memset (tpa->partition_to_tree_map, TPA_NONE, num_partitions * sizeof (int));
883 x = MAX (40, (num_partitions / 20));
884 tpa->trees = VEC_alloc (tree, heap, x);
885 tpa->first_partition = VEC_alloc (int, heap, x);
887 return tpa;
892 /* Remove PARTITION_INDEX from TREE_INDEX's list in the tpa structure TPA. */
894 void
895 tpa_remove_partition (tpa_p tpa, int tree_index, int partition_index)
897 int i;
899 i = tpa_first_partition (tpa, tree_index);
900 if (i == partition_index)
902 VEC_replace (int, tpa->first_partition, tree_index,
903 tpa->next_partition[i]);
905 else
907 for ( ; i != TPA_NONE; i = tpa_next_partition (tpa, i))
909 if (tpa->next_partition[i] == partition_index)
911 tpa->next_partition[i] = tpa->next_partition[partition_index];
912 break;
919 /* Free the memory used by tree_partition_associator object TPA. */
921 void
922 tpa_delete (tpa_p tpa)
924 if (!tpa)
925 return;
927 VEC_free (tree, heap, tpa->trees);
928 VEC_free (int, heap, tpa->first_partition);
929 free (tpa->partition_to_tree_map);
930 free (tpa->next_partition);
931 free (tpa);
935 /* This function will remove any tree entries from TPA which have only a single
936 element. This will help keep the size of the conflict graph down. The
937 function returns the number of remaining tree lists. */
939 int
940 tpa_compact (tpa_p tpa)
942 int last, x, y, first, swap_i;
943 tree swap_t;
945 /* Find the last list which has more than 1 partition. */
946 for (last = tpa->num_trees - 1; last > 0; last--)
948 first = tpa_first_partition (tpa, last);
949 if (tpa_next_partition (tpa, first) != NO_PARTITION)
950 break;
953 x = 0;
954 while (x < last)
956 first = tpa_first_partition (tpa, x);
958 /* If there is not more than one partition, swap with the current end
959 of the tree list. */
960 if (tpa_next_partition (tpa, first) == NO_PARTITION)
962 swap_t = VEC_index (tree, tpa->trees, last);
963 swap_i = VEC_index (int, tpa->first_partition, last);
965 /* Update the last entry. Since it is known to only have one
966 partition, there is nothing else to update. */
967 VEC_replace (tree, tpa->trees, last,
968 VEC_index (tree, tpa->trees, x));
969 VEC_replace (int, tpa->first_partition, last,
970 VEC_index (int, tpa->first_partition, x));
971 tpa->partition_to_tree_map[tpa_first_partition (tpa, last)] = last;
973 /* Since this list is known to have more than one partition, update
974 the list owner entries. */
975 VEC_replace (tree, tpa->trees, x, swap_t);
976 VEC_replace (int, tpa->first_partition, x, swap_i);
977 for (y = tpa_first_partition (tpa, x);
978 y != NO_PARTITION;
979 y = tpa_next_partition (tpa, y))
980 tpa->partition_to_tree_map[y] = x;
982 /* Ensure last is a list with more than one partition. */
983 last--;
984 for (; last > x; last--)
986 first = tpa_first_partition (tpa, last);
987 if (tpa_next_partition (tpa, first) != NO_PARTITION)
988 break;
991 x++;
994 first = tpa_first_partition (tpa, x);
995 if (tpa_next_partition (tpa, first) != NO_PARTITION)
996 x++;
997 tpa->uncompressed_num = tpa->num_trees;
998 tpa->num_trees = x;
999 return last;
1003 /* Initialize a root_var object with SSA partitions from MAP which are based
1004 on each root variable. */
1006 root_var_p
1007 root_var_init (var_map map)
1009 root_var_p rv;
1010 int num_partitions = num_var_partitions (map);
1011 int x, p;
1012 tree t;
1013 var_ann_t ann;
1014 sbitmap seen;
1016 rv = tpa_init (map);
1017 if (!rv)
1018 return NULL;
1020 seen = sbitmap_alloc (num_partitions);
1021 sbitmap_zero (seen);
1023 /* Start at the end and work towards the front. This will provide a list
1024 that is ordered from smallest to largest. */
1025 for (x = num_partitions - 1; x >= 0; x--)
1027 t = partition_to_var (map, x);
1029 /* The var map may not be compacted yet, so check for NULL. */
1030 if (!t)
1031 continue;
1033 p = var_to_partition (map, t);
1035 gcc_assert (p != NO_PARTITION);
1037 /* Make sure we only put coalesced partitions into the list once. */
1038 if (TEST_BIT (seen, p))
1039 continue;
1040 SET_BIT (seen, p);
1041 if (TREE_CODE (t) == SSA_NAME)
1042 t = SSA_NAME_VAR (t);
1043 ann = var_ann (t);
1044 if (ann->root_var_processed)
1046 rv->next_partition[p] = VEC_index (int, rv->first_partition,
1047 VAR_ANN_ROOT_INDEX (ann));
1048 VEC_replace (int, rv->first_partition, VAR_ANN_ROOT_INDEX (ann), p);
1050 else
1052 ann->root_var_processed = 1;
1053 VAR_ANN_ROOT_INDEX (ann) = rv->num_trees++;
1054 VEC_safe_push (tree, heap, rv->trees, t);
1055 VEC_safe_push (int, heap, rv->first_partition, p);
1057 rv->partition_to_tree_map[p] = VAR_ANN_ROOT_INDEX (ann);
1060 /* Reset the out_of_ssa_tag flag on each variable for later use. */
1061 for (x = 0; x < rv->num_trees; x++)
1063 t = VEC_index (tree, rv->trees, x);
1064 var_ann (t)->root_var_processed = 0;
1067 sbitmap_free (seen);
1068 return rv;
1072 /* Initialize a type_var structure which associates all the partitions in MAP
1073 of the same type to the type node's index. Volatiles are ignored. */
1075 type_var_p
1076 type_var_init (var_map map)
1078 type_var_p tv;
1079 int x, y, p;
1080 int num_partitions = num_var_partitions (map);
1081 tree t;
1082 sbitmap seen;
1084 tv = tpa_init (map);
1085 if (!tv)
1086 return NULL;
1088 seen = sbitmap_alloc (num_partitions);
1089 sbitmap_zero (seen);
1091 for (x = num_partitions - 1; x >= 0; x--)
1093 t = partition_to_var (map, x);
1095 /* Disallow coalescing of these types of variables. */
1096 if (!t
1097 || TREE_THIS_VOLATILE (t)
1098 || TREE_CODE (t) == RESULT_DECL
1099 || TREE_CODE (t) == PARM_DECL
1100 || (DECL_P (t)
1101 && (DECL_REGISTER (t)
1102 || !DECL_IGNORED_P (t)
1103 || DECL_RTL_SET_P (t))))
1104 continue;
1106 p = var_to_partition (map, t);
1108 gcc_assert (p != NO_PARTITION);
1110 /* If partitions have been coalesced, only add the representative
1111 for the partition to the list once. */
1112 if (TEST_BIT (seen, p))
1113 continue;
1114 SET_BIT (seen, p);
1115 t = TREE_TYPE (t);
1117 /* Find the list for this type. */
1118 for (y = 0; y < tv->num_trees; y++)
1119 if (t == VEC_index (tree, tv->trees, y))
1120 break;
1121 if (y == tv->num_trees)
1123 tv->num_trees++;
1124 VEC_safe_push (tree, heap, tv->trees, t);
1125 VEC_safe_push (int, heap, tv->first_partition, p);
1127 else
1129 tv->next_partition[p] = VEC_index (int, tv->first_partition, y);
1130 VEC_replace (int, tv->first_partition, y, p);
1132 tv->partition_to_tree_map[p] = y;
1134 sbitmap_free (seen);
1135 return tv;
1139 /* Hash function for 2 integer coalesce pairs. */
1140 #define COALESCE_HASH_FN(R1, R2) ((R2) * ((R2) - 1) / 2 + (R1))
1143 /* Return hash value for partition pair PAIR. */
1145 unsigned int
1146 partition_pair_map_hash (const void *pair)
1148 hashval_t a = (hashval_t)(((partition_pair_p)pair)->first_partition);
1149 hashval_t b = (hashval_t)(((partition_pair_p)pair)->second_partition);
1151 return COALESCE_HASH_FN (a,b);
1155 /* Return TRUE if PAIR1 is equivilent to PAIR2. */
1157 int
1158 partition_pair_map_eq (const void *pair1, const void *pair2)
1160 partition_pair_p p1 = (partition_pair_p) pair1;
1161 partition_pair_p p2 = (partition_pair_p) pair2;
1163 return (p1->first_partition == p2->first_partition
1164 && p1->second_partition == p2->second_partition);
1168 /* Create a new coalesce list object from MAP and return it. */
1170 coalesce_list_p
1171 create_coalesce_list (var_map map)
1173 coalesce_list_p list;
1174 unsigned size = num_ssa_names * 3;
1176 if (size < 40)
1177 size = 40;
1179 list = xmalloc (sizeof (struct coalesce_list_d));
1180 list->list = htab_create (size, partition_pair_map_hash,
1181 partition_pair_map_eq, NULL);
1183 list->map = map;
1184 list->sorted = NULL;
1185 list->add_mode = true;
1186 list->num_sorted = 0;
1187 return list;
1191 /* Delete coalesce list CL. */
1193 void
1194 delete_coalesce_list (coalesce_list_p cl)
1196 htab_delete (cl->list);
1197 if (cl->sorted)
1198 free (cl->sorted);
1199 gcc_assert (cl->num_sorted == 0);
1200 free (cl);
1204 /* Find a matching coalesce pair object in CL for partitions P1 and P2. If
1205 one isn't found, return NULL if CREATE is false, otherwise create a new
1206 coalesce pair object and return it. */
1208 static partition_pair_p
1209 find_partition_pair (coalesce_list_p cl, int p1, int p2, bool create)
1211 struct partition_pair p, *pair;
1212 void **slot;
1213 unsigned int hash;
1215 /* normalize so that p1 is the smaller value. */
1216 if (p2 < p1)
1218 p.first_partition = p2;
1219 p.second_partition = p1;
1221 else
1223 p.first_partition = p1;
1224 p.second_partition = p2;
1228 hash = partition_pair_map_hash (&p);
1229 pair = (struct partition_pair *) htab_find_with_hash (cl->list, &p, hash);
1231 if (create && !pair)
1233 gcc_assert (cl->add_mode);
1234 pair = xmalloc (sizeof (struct partition_pair));
1235 pair->first_partition = p.first_partition;
1236 pair->second_partition = p.second_partition;
1237 pair->cost = 0;
1238 slot = htab_find_slot_with_hash (cl->list, pair, hash, INSERT);
1239 *(struct partition_pair **)slot = pair;
1242 return pair;
1245 /* Return cost of execution of copy instruction with FREQUENCY
1246 possibly on CRITICAL edge and in HOT basic block. */
1248 coalesce_cost (int frequency, bool hot, bool critical)
1250 /* Base costs on BB frequencies bounded by 1. */
1251 int cost = frequency;
1253 if (!cost)
1254 cost = 1;
1255 if (optimize_size || hot)
1256 cost = 1;
1257 /* Inserting copy on critical edge costs more
1258 than inserting it elsewhere. */
1259 if (critical)
1260 cost *= 2;
1261 return cost;
1264 /* Add a potential coalesce between P1 and P2 in CL with a cost of VALUE. */
1266 void
1267 add_coalesce (coalesce_list_p cl, int p1, int p2,
1268 int value)
1270 partition_pair_p node;
1272 gcc_assert (cl->add_mode);
1274 if (p1 == p2)
1275 return;
1277 node = find_partition_pair (cl, p1, p2, true);
1279 node->cost += value;
1283 /* Comparison function to allow qsort to sort P1 and P2 in Ascendiong order. */
1285 static
1286 int compare_pairs (const void *p1, const void *p2)
1288 return (*(partition_pair_p *)p1)->cost - (*(partition_pair_p *)p2)->cost;
1292 static inline int
1293 num_coalesce_pairs (coalesce_list_p cl)
1295 return htab_elements (cl->list);
1298 typedef struct
1300 htab_iterator hti;
1301 } partition_pair_iterator;
1303 static inline partition_pair_p
1304 first_partition_pair (coalesce_list_p cl, partition_pair_iterator *iter)
1306 partition_pair_p pair;
1308 pair = (partition_pair_p) first_htab_element (&(iter->hti), cl->list);
1309 return pair;
1312 static inline bool
1313 end_partition_pair_p (partition_pair_iterator *iter)
1315 return end_htab_p (&(iter->hti));
1318 static inline partition_pair_p
1319 next_partition_pair (partition_pair_iterator *iter)
1321 partition_pair_p pair;
1323 pair = (partition_pair_p) next_htab_element (&(iter->hti));
1324 return pair;
1327 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
1328 for ((PAIR) = first_partition_pair ((CL), &(ITER)); \
1329 !end_partition_pair_p (&(ITER)); \
1330 (PAIR) = next_partition_pair (&(ITER)))
1333 /* Prepare CL for removal of preferred pairs. When finished, list element
1334 0 has all the coalesce pairs, sorted in order from most important coalesce
1335 to least important. */
1337 void
1338 sort_coalesce_list (coalesce_list_p cl)
1340 unsigned x, num;
1341 partition_pair_p p;
1342 partition_pair_iterator ppi;
1344 gcc_assert (cl->add_mode);
1346 cl->add_mode = false;
1348 /* allocate a vector for the pair pointers. */
1349 num = num_coalesce_pairs (cl);
1350 cl->num_sorted = num;
1351 if (num == 0)
1352 return;
1353 cl->sorted = XNEWVEC (partition_pair_p, num);
1355 /* Populate the vector with pointers to the partition pairs. */
1357 x = 0;
1358 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
1359 cl->sorted[x++] = p;
1360 gcc_assert (x == num);
1362 if (num == 1)
1363 return;
1365 if (num == 2)
1367 if (cl->sorted[0]->cost > cl->sorted[1]->cost)
1369 p = cl->sorted[0];
1370 cl->sorted[0] = cl->sorted[1];
1371 cl->sorted[1] = p;
1373 return;
1376 /* Only call qsort if there are more than 2 items. */
1377 if (num > 2)
1378 qsort (cl->sorted, num, sizeof (partition_pair_p), compare_pairs);
1382 /* Retrieve the best remaining pair to coalesce from CL. Returns the 2
1383 partitions via P1 and P2. Their calculated cost is returned by the function.
1384 NO_BEST_COALESCE is returned if the coalesce list is empty. */
1386 static int
1387 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
1389 partition_pair_p node;
1390 int ret;
1392 gcc_assert (!cl->add_mode);
1394 if (cl->num_sorted == 0)
1395 return NO_BEST_COALESCE;
1397 node = cl->sorted[--(cl->num_sorted)];
1399 *p1 = node->first_partition;
1400 *p2 = node->second_partition;
1401 ret = node->cost;
1402 free (node);
1404 return ret;
1408 /* If variable VAR is in a partition in MAP, add a conflict in GRAPH between
1409 VAR and any other live partitions in VEC which are associated via TPA.
1410 Reset the live bit in VEC. */
1412 static inline void
1413 add_conflicts_if_valid (tpa_p tpa, conflict_graph graph,
1414 var_map map, bitmap vec, tree var)
1416 int p, y, first;
1417 p = var_to_partition (map, var);
1418 if (p != NO_PARTITION)
1420 bitmap_clear_bit (vec, p);
1421 first = tpa_find_tree (tpa, p);
1422 /* If find returns nothing, this object isn't interesting. */
1423 if (first == TPA_NONE)
1424 return;
1425 /* Only add interferences between objects in the same list. */
1426 for (y = tpa_first_partition (tpa, first);
1427 y != TPA_NONE;
1428 y = tpa_next_partition (tpa, y))
1430 if (bitmap_bit_p (vec, y))
1431 conflict_graph_add (graph, p, y);
1436 /* Return a conflict graph for the information contained in LIVE_INFO. Only
1437 conflicts between items in the same TPA list are added. If optional
1438 coalesce list CL is passed in, any copies encountered are added. */
1440 conflict_graph
1441 build_tree_conflict_graph (tree_live_info_p liveinfo, tpa_p tpa,
1442 coalesce_list_p cl)
1444 conflict_graph graph;
1445 var_map map;
1446 bitmap live;
1447 unsigned x, y, i;
1448 basic_block bb;
1449 int *partition_link, *tpa_nodes;
1450 VEC(int,heap) *tpa_to_clear;
1451 unsigned l;
1452 ssa_op_iter iter;
1453 bitmap_iterator bi;
1455 map = live_var_map (liveinfo);
1456 graph = conflict_graph_new (num_var_partitions (map));
1458 if (tpa_num_trees (tpa) == 0)
1459 return graph;
1461 live = BITMAP_ALLOC (NULL);
1463 partition_link = XCNEWVEC (int, num_var_partitions (map) + 1);
1464 tpa_nodes = XCNEWVEC (int, tpa_num_trees (tpa));
1465 tpa_to_clear = VEC_alloc (int, heap, 50);
1467 FOR_EACH_BB (bb)
1469 block_stmt_iterator bsi;
1470 tree phi;
1471 int idx;
1473 /* Start with live on exit temporaries. */
1474 bitmap_copy (live, live_on_exit (liveinfo, bb));
1476 for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_prev (&bsi))
1478 bool is_a_copy = false;
1479 tree stmt = bsi_stmt (bsi);
1481 /* A copy between 2 partitions does not introduce an interference
1482 by itself. If they did, you would never be able to coalesce
1483 two things which are copied. If the two variables really do
1484 conflict, they will conflict elsewhere in the program.
1486 This is handled specially here since we may also be interested
1487 in copies between real variables and SSA_NAME variables. We may
1488 be interested in trying to coalesce SSA_NAME variables with
1489 root variables in some cases. */
1491 if (TREE_CODE (stmt) == MODIFY_EXPR)
1493 tree lhs = TREE_OPERAND (stmt, 0);
1494 tree rhs = TREE_OPERAND (stmt, 1);
1495 int p1, p2;
1496 int bit;
1498 if (DECL_P (lhs) || TREE_CODE (lhs) == SSA_NAME)
1499 p1 = var_to_partition (map, lhs);
1500 else
1501 p1 = NO_PARTITION;
1503 if (DECL_P (rhs) || TREE_CODE (rhs) == SSA_NAME)
1504 p2 = var_to_partition (map, rhs);
1505 else
1506 p2 = NO_PARTITION;
1508 if (p1 != NO_PARTITION && p2 != NO_PARTITION)
1510 is_a_copy = true;
1511 bit = bitmap_bit_p (live, p2);
1512 /* If the RHS is live, make it not live while we add
1513 the conflicts, then make it live again. */
1514 if (bit)
1515 bitmap_clear_bit (live, p2);
1516 add_conflicts_if_valid (tpa, graph, map, live, lhs);
1517 if (bit)
1518 bitmap_set_bit (live, p2);
1519 if (cl)
1520 add_coalesce (cl, p1, p2,
1521 coalesce_cost (bb->frequency,
1522 maybe_hot_bb_p (bb), false));
1523 set_if_valid (map, live, rhs);
1527 if (!is_a_copy)
1529 tree var;
1530 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
1532 add_conflicts_if_valid (tpa, graph, map, live, var);
1535 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
1537 set_if_valid (map, live, var);
1542 /* If result of a PHI is unused, then the loops over the statements
1543 will not record any conflicts. However, since the PHI node is
1544 going to be translated out of SSA form we must record a conflict
1545 between the result of the PHI and any variables with are live.
1546 Otherwise the out-of-ssa translation may create incorrect code. */
1547 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1549 tree result = PHI_RESULT (phi);
1550 int p = var_to_partition (map, result);
1552 if (p != NO_PARTITION && ! bitmap_bit_p (live, p))
1553 add_conflicts_if_valid (tpa, graph, map, live, result);
1556 /* Anything which is still live at this point interferes.
1557 In order to implement this efficiently, only conflicts between
1558 partitions which have the same TPA root need be added.
1559 TPA roots which have been seen are tracked in 'tpa_nodes'. A nonzero
1560 entry points to an index into 'partition_link', which then indexes
1561 into itself forming a linked list of partitions sharing a tpa root
1562 which have been seen as live up to this point. Since partitions start
1563 at index zero, all entries in partition_link are (partition + 1).
1565 Conflicts are added between the current partition and any already seen.
1566 tpa_clear contains all the tpa_roots processed, and these are the only
1567 entries which need to be zero'd out for a clean restart. */
1569 EXECUTE_IF_SET_IN_BITMAP (live, 0, x, bi)
1571 i = tpa_find_tree (tpa, x);
1572 if (i != (unsigned)TPA_NONE)
1574 int start = tpa_nodes[i];
1575 /* If start is 0, a new root reference list is being started.
1576 Register it to be cleared. */
1577 if (!start)
1578 VEC_safe_push (int, heap, tpa_to_clear, i);
1580 /* Add interferences to other tpa members seen. */
1581 for (y = start; y != 0; y = partition_link[y])
1582 conflict_graph_add (graph, x, y - 1);
1583 tpa_nodes[i] = x + 1;
1584 partition_link[x + 1] = start;
1588 /* Now clear the used tpa root references. */
1589 for (l = 0; VEC_iterate (int, tpa_to_clear, l, idx); l++)
1590 tpa_nodes[idx] = 0;
1591 VEC_truncate (int, tpa_to_clear, 0);
1594 free (tpa_nodes);
1595 free (partition_link);
1596 VEC_free (int, heap, tpa_to_clear);
1597 BITMAP_FREE (live);
1598 return graph;
1602 /* This routine will attempt to coalesce the elements in TPA subject to the
1603 conflicts found in GRAPH. If optional coalesce_list CL is provided,
1604 only coalesces specified within the coalesce list are attempted. Otherwise
1605 an attempt is made to coalesce as many partitions within each TPA grouping
1606 as possible. If DEBUG is provided, debug output will be sent there. */
1608 void
1609 coalesce_tpa_members (tpa_p tpa, conflict_graph graph, var_map map,
1610 coalesce_list_p cl, FILE *debug)
1612 int x, y, z, w;
1613 tree var, tmp;
1615 /* Attempt to coalesce any items in a coalesce list. */
1616 if (cl)
1618 while (pop_best_coalesce (cl, &x, &y) != NO_BEST_COALESCE)
1620 if (debug)
1622 fprintf (debug, "Coalesce list: (%d)", x);
1623 print_generic_expr (debug, partition_to_var (map, x), TDF_SLIM);
1624 fprintf (debug, " & (%d)", y);
1625 print_generic_expr (debug, partition_to_var (map, y), TDF_SLIM);
1628 w = tpa_find_tree (tpa, x);
1629 z = tpa_find_tree (tpa, y);
1630 if (w != z || w == TPA_NONE || z == TPA_NONE)
1632 if (debug)
1634 if (w != z)
1635 fprintf (debug, ": Fail, Non-matching TPA's\n");
1636 if (w == TPA_NONE)
1637 fprintf (debug, ": Fail %d non TPA.\n", x);
1638 else
1639 fprintf (debug, ": Fail %d non TPA.\n", y);
1641 continue;
1643 var = partition_to_var (map, x);
1644 tmp = partition_to_var (map, y);
1645 x = var_to_partition (map, var);
1646 y = var_to_partition (map, tmp);
1647 if (debug)
1648 fprintf (debug, " [map: %d, %d] ", x, y);
1649 if (x == y)
1651 if (debug)
1652 fprintf (debug, ": Already Coalesced.\n");
1653 continue;
1655 if (!conflict_graph_conflict_p (graph, x, y))
1657 z = var_union (map, var, tmp);
1658 if (z == NO_PARTITION)
1660 if (debug)
1661 fprintf (debug, ": Unable to perform partition union.\n");
1662 continue;
1665 /* z is the new combined partition. We need to remove the other
1666 partition from the list. Set x to be that other partition. */
1667 if (z == x)
1669 conflict_graph_merge_regs (graph, x, y);
1670 w = tpa_find_tree (tpa, y);
1671 tpa_remove_partition (tpa, w, y);
1673 else
1675 conflict_graph_merge_regs (graph, y, x);
1676 w = tpa_find_tree (tpa, x);
1677 tpa_remove_partition (tpa, w, x);
1680 if (debug)
1681 fprintf (debug, ": Success -> %d\n", z);
1683 else
1684 if (debug)
1685 fprintf (debug, ": Fail due to conflict\n");
1687 /* If using a coalesce list, don't try to coalesce anything else. */
1688 return;
1691 for (x = 0; x < tpa_num_trees (tpa); x++)
1693 while (tpa_first_partition (tpa, x) != TPA_NONE)
1695 int p1, p2;
1696 /* Coalesce first partition with anything that doesn't conflict. */
1697 y = tpa_first_partition (tpa, x);
1698 tpa_remove_partition (tpa, x, y);
1700 var = partition_to_var (map, y);
1701 /* p1 is the partition representative to which y belongs. */
1702 p1 = var_to_partition (map, var);
1704 for (z = tpa_next_partition (tpa, y);
1705 z != TPA_NONE;
1706 z = tpa_next_partition (tpa, z))
1708 tmp = partition_to_var (map, z);
1709 /* p2 is the partition representative to which z belongs. */
1710 p2 = var_to_partition (map, tmp);
1711 if (debug)
1713 fprintf (debug, "Coalesce : ");
1714 print_generic_expr (debug, var, TDF_SLIM);
1715 fprintf (debug, " &");
1716 print_generic_expr (debug, tmp, TDF_SLIM);
1717 fprintf (debug, " (%d ,%d)", p1, p2);
1720 /* If partitions are already merged, don't check for conflict. */
1721 if (tmp == var)
1723 tpa_remove_partition (tpa, x, z);
1724 if (debug)
1725 fprintf (debug, ": Already coalesced\n");
1727 else
1728 if (!conflict_graph_conflict_p (graph, p1, p2))
1730 int v;
1731 if (tpa_find_tree (tpa, y) == TPA_NONE
1732 || tpa_find_tree (tpa, z) == TPA_NONE)
1734 if (debug)
1735 fprintf (debug, ": Fail non-TPA member\n");
1736 continue;
1738 if ((v = var_union (map, var, tmp)) == NO_PARTITION)
1740 if (debug)
1741 fprintf (debug, ": Fail cannot combine partitions\n");
1742 continue;
1745 tpa_remove_partition (tpa, x, z);
1746 if (v == p1)
1747 conflict_graph_merge_regs (graph, v, z);
1748 else
1750 /* Update the first partition's representative. */
1751 conflict_graph_merge_regs (graph, v, y);
1752 p1 = v;
1755 /* The root variable of the partition may be changed
1756 now. */
1757 var = partition_to_var (map, p1);
1759 if (debug)
1760 fprintf (debug, ": Success -> %d\n", v);
1762 else
1763 if (debug)
1764 fprintf (debug, ": Fail, Conflict\n");
1771 /* Send debug info for coalesce list CL to file F. */
1773 void
1774 dump_coalesce_list (FILE *f, coalesce_list_p cl)
1776 partition_pair_p node;
1777 partition_pair_iterator ppi;
1778 int x;
1779 tree var;
1781 if (cl->add_mode)
1783 fprintf (f, "Coalesce List:\n");
1784 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
1786 tree var1 = partition_to_var (cl->map, node->first_partition);
1787 tree var2 = partition_to_var (cl->map, node->second_partition);
1788 print_generic_expr (f, var1, TDF_SLIM);
1789 fprintf (f, " <-> ");
1790 print_generic_expr (f, var2, TDF_SLIM);
1791 fprintf (f, " (%1d), ", node->cost);
1792 fprintf (f, "\n");
1795 else
1797 fprintf (f, "Sorted Coalesce list:\n");
1798 for (x = cl->num_sorted - 1 ; x >=0; x--)
1800 node = cl->sorted[x];
1801 fprintf (f, "(%d) ", node->cost);
1802 var = partition_to_var (cl->map, node->first_partition);
1803 print_generic_expr (f, var, TDF_SLIM);
1804 fprintf (f, " <-> ");
1805 var = partition_to_var (cl->map, node->second_partition);
1806 print_generic_expr (f, var, TDF_SLIM);
1807 fprintf (f, "\n");
1813 /* Output tree_partition_associator object TPA to file F.. */
1815 void
1816 tpa_dump (FILE *f, tpa_p tpa)
1818 int x, i;
1820 if (!tpa)
1821 return;
1823 for (x = 0; x < tpa_num_trees (tpa); x++)
1825 print_generic_expr (f, tpa_tree (tpa, x), TDF_SLIM);
1826 fprintf (f, " : (");
1827 for (i = tpa_first_partition (tpa, x);
1828 i != TPA_NONE;
1829 i = tpa_next_partition (tpa, i))
1831 fprintf (f, "(%d)",i);
1832 print_generic_expr (f, partition_to_var (tpa->map, i), TDF_SLIM);
1833 fprintf (f, " ");
1835 #ifdef ENABLE_CHECKING
1836 if (tpa_find_tree (tpa, i) != x)
1837 fprintf (f, "**find tree incorrectly set** ");
1838 #endif
1841 fprintf (f, ")\n");
1843 fflush (f);
1847 /* Output partition map MAP to file F. */
1849 void
1850 dump_var_map (FILE *f, var_map map)
1852 int t;
1853 unsigned x, y;
1854 int p;
1856 fprintf (f, "\nPartition map \n\n");
1858 for (x = 0; x < map->num_partitions; x++)
1860 if (map->compact_to_partition != NULL)
1861 p = map->compact_to_partition[x];
1862 else
1863 p = x;
1865 if (map->partition_to_var[p] == NULL_TREE)
1866 continue;
1868 t = 0;
1869 for (y = 1; y < num_ssa_names; y++)
1871 p = partition_find (map->var_partition, y);
1872 if (map->partition_to_compact)
1873 p = map->partition_to_compact[p];
1874 if (p == (int)x)
1876 if (t++ == 0)
1878 fprintf(f, "Partition %d (", x);
1879 print_generic_expr (f, partition_to_var (map, p), TDF_SLIM);
1880 fprintf (f, " - ");
1882 fprintf (f, "%d ", y);
1885 if (t != 0)
1886 fprintf (f, ")\n");
1888 fprintf (f, "\n");
1892 /* Output live range info LIVE to file F, controlled by FLAG. */
1894 void
1895 dump_live_info (FILE *f, tree_live_info_p live, int flag)
1897 basic_block bb;
1898 unsigned i;
1899 var_map map = live->map;
1900 bitmap_iterator bi;
1902 if ((flag & LIVEDUMP_ENTRY) && live->livein)
1904 FOR_EACH_BB (bb)
1906 fprintf (f, "\nLive on entry to BB%d : ", bb->index);
1907 for (i = 0; i < num_var_partitions (map); i++)
1909 if (bitmap_bit_p (live_entry_blocks (live, i), bb->index))
1911 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1912 fprintf (f, " ");
1915 fprintf (f, "\n");
1919 if ((flag & LIVEDUMP_EXIT) && live->liveout)
1921 FOR_EACH_BB (bb)
1923 fprintf (f, "\nLive on exit from BB%d : ", bb->index);
1924 EXECUTE_IF_SET_IN_BITMAP (live->liveout[bb->index], 0, i, bi)
1926 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1927 fprintf (f, " ");
1929 fprintf (f, "\n");
1934 #ifdef ENABLE_CHECKING
1935 void
1936 register_ssa_partition_check (tree ssa_var)
1938 gcc_assert (TREE_CODE (ssa_var) == SSA_NAME);
1939 if (!is_gimple_reg (SSA_NAME_VAR (ssa_var)))
1941 fprintf (stderr, "Illegally registering a virtual SSA name :");
1942 print_generic_expr (stderr, ssa_var, TDF_SLIM);
1943 fprintf (stderr, " in the SSA->Normal phase.\n");
1944 internal_error ("SSA corruption");
1947 #endif