tree-ssa-live.c (build_tree_conflict_graph): Remove local variable ann.
[gcc.git] / gcc / tree-ssa-live.c
1 /* Liveness for SSA trees.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "basic-block.h"
29 #include "function.h"
30 #include "diagnostic.h"
31 #include "bitmap.h"
32 #include "tree-flow.h"
33 #include "tree-gimple.h"
34 #include "tree-inline.h"
35 #include "varray.h"
36 #include "timevar.h"
37 #include "hashtab.h"
38 #include "tree-dump.h"
39 #include "tree-ssa-live.h"
40 #include "errors.h"
41
42 static void live_worklist (tree_live_info_p, varray_type, int);
43 static tree_live_info_p new_tree_live_info (var_map);
44 static inline void set_if_valid (var_map, bitmap, tree);
45 static inline void add_livein_if_notdef (tree_live_info_p, bitmap,
46 tree, basic_block);
47 static inline void register_ssa_partition (var_map, tree, bool);
48 static inline void add_conflicts_if_valid (tpa_p, conflict_graph,
49 var_map, bitmap, tree);
50 static partition_pair_p find_partition_pair (coalesce_list_p, int, int, bool);
51
52 /* This is where the mapping from SSA version number to real storage variable
53 is tracked.
54
55 All SSA versions of the same variable may not ultimately be mapped back to
56 the same real variable. In that instance, we need to detect the live
57 range overlap, and give one of the variable new storage. The vector
58 'partition_to_var' tracks which partition maps to which variable.
59
60 Given a VAR, it is sometimes desirable to know which partition that VAR
61 represents. There is an additional field in the variable annotation to
62 track that information. */
63
64 /* Create a variable partition map of SIZE, initialize and return it. */
65
66 var_map
67 init_var_map (int size)
68 {
69 var_map map;
70
71 map = (var_map) xmalloc (sizeof (struct _var_map));
72 map->var_partition = partition_new (size);
73 map->partition_to_var
74 = (tree *)xmalloc (size * sizeof (tree));
75 memset (map->partition_to_var, 0, size * sizeof (tree));
76
77 map->partition_to_compact = NULL;
78 map->compact_to_partition = NULL;
79 map->num_partitions = size;
80 map->partition_size = size;
81 map->ref_count = NULL;
82 return map;
83 }
84
85
86 /* Free memory associated with MAP. */
87
88 void
89 delete_var_map (var_map map)
90 {
91 free (map->partition_to_var);
92 partition_delete (map->var_partition);
93 if (map->partition_to_compact)
94 free (map->partition_to_compact);
95 if (map->compact_to_partition)
96 free (map->compact_to_partition);
97 if (map->ref_count)
98 free (map->ref_count);
99 free (map);
100 }
101
102
103 /* This function will combine the partitions in MAP for VAR1 and VAR2. It
104 Returns the partition which represents the new partition. If the two
105 partitions cannot be combined, NO_PARTITION is returned. */
106
107 int
108 var_union (var_map map, tree var1, tree var2)
109 {
110 int p1, p2, p3;
111 tree root_var = NULL_TREE;
112 tree other_var = NULL_TREE;
113
114 /* This is independent of partition_to_compact. If partition_to_compact is
115 on, then whichever one of these partitions is absorbed will never have a
116 dereference into the partition_to_compact array any more. */
117
118 if (TREE_CODE (var1) == SSA_NAME)
119 p1 = partition_find (map->var_partition, SSA_NAME_VERSION (var1));
120 else
121 {
122 p1 = var_to_partition (map, var1);
123 if (map->compact_to_partition)
124 p1 = map->compact_to_partition[p1];
125 root_var = var1;
126 }
127
128 if (TREE_CODE (var2) == SSA_NAME)
129 p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2));
130 else
131 {
132 p2 = var_to_partition (map, var2);
133 if (map->compact_to_partition)
134 p2 = map->compact_to_partition[p2];
135
136 /* If there is no root_var set, or it's not a user variable, set the
137 root_var to this one. */
138 if (!root_var || (DECL_P (root_var) && DECL_IGNORED_P (root_var)))
139 {
140 other_var = root_var;
141 root_var = var2;
142 }
143 else
144 other_var = var2;
145 }
146
147 gcc_assert (p1 != NO_PARTITION);
148 gcc_assert (p2 != NO_PARTITION);
149
150 if (p1 == p2)
151 p3 = p1;
152 else
153 p3 = partition_union (map->var_partition, p1, p2);
154
155 if (map->partition_to_compact)
156 p3 = map->partition_to_compact[p3];
157
158 if (root_var)
159 change_partition_var (map, root_var, p3);
160 if (other_var)
161 change_partition_var (map, other_var, p3);
162
163 return p3;
164 }
165
166
167 /* Compress the partition numbers in MAP such that they fall in the range
168 0..(num_partitions-1) instead of wherever they turned out during
169 the partitioning exercise. This removes any references to unused
170 partitions, thereby allowing bitmaps and other vectors to be much
171 denser. Compression type is controlled by FLAGS.
172
173 This is implemented such that compaction doesn't affect partitioning.
174 Ie., once partitions are created and possibly merged, running one
175 or more different kind of compaction will not affect the partitions
176 themselves. Their index might change, but all the same variables will
177 still be members of the same partition group. This allows work on reduced
178 sets, and no loss of information when a larger set is later desired.
179
180 In particular, coalescing can work on partitions which have 2 or more
181 definitions, and then 'recompact' later to include all the single
182 definitions for assignment to program variables. */
183
184 void
185 compact_var_map (var_map map, int flags)
186 {
187 sbitmap used;
188 int x, limit, count, tmp, root, root_i;
189 tree var;
190 root_var_p rv = NULL;
191
192 limit = map->partition_size;
193 used = sbitmap_alloc (limit);
194 sbitmap_zero (used);
195
196 /* Already compressed? Abandon the old one. */
197 if (map->partition_to_compact)
198 {
199 free (map->partition_to_compact);
200 map->partition_to_compact = NULL;
201 }
202 if (map->compact_to_partition)
203 {
204 free (map->compact_to_partition);
205 map->compact_to_partition = NULL;
206 }
207
208 map->num_partitions = map->partition_size;
209
210 if (flags & VARMAP_NO_SINGLE_DEFS)
211 rv = root_var_init (map);
212
213 map->partition_to_compact = (int *)xmalloc (limit * sizeof (int));
214 memset (map->partition_to_compact, 0xff, (limit * sizeof (int)));
215
216 /* Find out which partitions are actually referenced. */
217 count = 0;
218 for (x = 0; x < limit; x++)
219 {
220 tmp = partition_find (map->var_partition, x);
221 if (!TEST_BIT (used, tmp) && map->partition_to_var[tmp] != NULL_TREE)
222 {
223 /* It is referenced, check to see if there is more than one version
224 in the root_var table, if one is available. */
225 if (rv)
226 {
227 root = root_var_find (rv, tmp);
228 root_i = root_var_first_partition (rv, root);
229 /* If there is only one, don't include this in the compaction. */
230 if (root_var_next_partition (rv, root_i) == ROOT_VAR_NONE)
231 continue;
232 }
233 SET_BIT (used, tmp);
234 count++;
235 }
236 }
237
238 /* Build a compacted partitioning. */
239 if (count != limit)
240 {
241 map->compact_to_partition = (int *)xmalloc (count * sizeof (int));
242 count = 0;
243 /* SSA renaming begins at 1, so skip 0 when compacting. */
244 EXECUTE_IF_SET_IN_SBITMAP (used, 1, x,
245 {
246 map->partition_to_compact[x] = count;
247 map->compact_to_partition[count] = x;
248 var = map->partition_to_var[x];
249 if (TREE_CODE (var) != SSA_NAME)
250 change_partition_var (map, var, count);
251 count++;
252 });
253 }
254 else
255 {
256 free (map->partition_to_compact);
257 map->partition_to_compact = NULL;
258 }
259
260 map->num_partitions = count;
261
262 if (rv)
263 root_var_delete (rv);
264 sbitmap_free (used);
265 }
266
267
268 /* This function is used to change the representative variable in MAP for VAR's
269 partition from an SSA_NAME variable to a regular variable. This allows
270 partitions to be mapped back to real variables. */
271
272 void
273 change_partition_var (var_map map, tree var, int part)
274 {
275 var_ann_t ann;
276
277 gcc_assert (TREE_CODE (var) != SSA_NAME);
278
279 ann = var_ann (var);
280 ann->out_of_ssa_tag = 1;
281 VAR_ANN_PARTITION (ann) = part;
282 if (map->compact_to_partition)
283 map->partition_to_var[map->compact_to_partition[part]] = var;
284 }
285
286
287 /* Helper function for mark_all_vars_used, called via walk_tree. */
288
289 static tree
290 mark_all_vars_used_1 (tree *tp, int *walk_subtrees,
291 void *data ATTRIBUTE_UNUSED)
292 {
293 tree t = *tp;
294
295 /* Only need to mark VAR_DECLS; parameters and return results are not
296 eliminated as unused. */
297 if (TREE_CODE (t) == VAR_DECL)
298 set_is_used (t);
299
300 if (IS_TYPE_OR_DECL_P (t))
301 *walk_subtrees = 0;
302
303 return NULL;
304 }
305
306 /* Mark all VAR_DECLS under *EXPR_P as used, so that they won't be
307 eliminated during the tree->rtl conversion process. */
308
309 static inline void
310 mark_all_vars_used (tree *expr_p)
311 {
312 walk_tree (expr_p, mark_all_vars_used_1, NULL, NULL);
313 }
314
315 /* This function looks through the program and uses FLAGS to determine what
316 SSA versioned variables are given entries in a new partition table. This
317 new partition map is returned. */
318
319 var_map
320 create_ssa_var_map (int flags)
321 {
322 block_stmt_iterator bsi;
323 basic_block bb;
324 tree dest, use;
325 tree stmt;
326 var_map map;
327 ssa_op_iter iter;
328 #ifdef ENABLE_CHECKING
329 sbitmap used_in_real_ops;
330 sbitmap used_in_virtual_ops;
331 #endif
332
333 map = init_var_map (num_ssa_names + 1);
334
335 #ifdef ENABLE_CHECKING
336 used_in_real_ops = sbitmap_alloc (num_referenced_vars);
337 sbitmap_zero (used_in_real_ops);
338
339 used_in_virtual_ops = sbitmap_alloc (num_referenced_vars);
340 sbitmap_zero (used_in_virtual_ops);
341 #endif
342
343 if (flags & SSA_VAR_MAP_REF_COUNT)
344 {
345 map->ref_count
346 = (int *)xmalloc (((num_ssa_names + 1) * sizeof (int)));
347 memset (map->ref_count, 0, (num_ssa_names + 1) * sizeof (int));
348 }
349
350 FOR_EACH_BB (bb)
351 {
352 tree phi, arg;
353 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
354 {
355 int i;
356 register_ssa_partition (map, PHI_RESULT (phi), false);
357 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
358 {
359 arg = PHI_ARG_DEF (phi, i);
360 if (TREE_CODE (arg) == SSA_NAME)
361 register_ssa_partition (map, arg, true);
362
363 mark_all_vars_used (&PHI_ARG_DEF_TREE (phi, i));
364 }
365 }
366
367 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
368 {
369 stmt = bsi_stmt (bsi);
370 get_stmt_operands (stmt);
371
372 /* Register USE and DEF operands in each statement. */
373 FOR_EACH_SSA_TREE_OPERAND (use , stmt, iter, SSA_OP_USE)
374 {
375 register_ssa_partition (map, use, true);
376
377 #ifdef ENABLE_CHECKING
378 SET_BIT (used_in_real_ops, var_ann (SSA_NAME_VAR (use))->uid);
379 #endif
380 }
381
382 FOR_EACH_SSA_TREE_OPERAND (dest, stmt, iter, SSA_OP_DEF)
383 {
384 register_ssa_partition (map, dest, false);
385
386 #ifdef ENABLE_CHECKING
387 SET_BIT (used_in_real_ops, var_ann (SSA_NAME_VAR (dest))->uid);
388 #endif
389 }
390
391 #ifdef ENABLE_CHECKING
392 /* Validate that virtual ops don't get used in funny ways. */
393 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter,
394 SSA_OP_VIRTUAL_USES | SSA_OP_VMUSTDEF)
395 {
396 SET_BIT (used_in_virtual_ops, var_ann (SSA_NAME_VAR (use))->uid);
397 }
398
399 #endif /* ENABLE_CHECKING */
400
401 mark_all_vars_used (bsi_stmt_ptr (bsi));
402 }
403 }
404
405 #if defined ENABLE_CHECKING
406 {
407 unsigned i;
408 sbitmap both = sbitmap_alloc (num_referenced_vars);
409 sbitmap_a_and_b (both, used_in_real_ops, used_in_virtual_ops);
410 if (sbitmap_first_set_bit (both) >= 0)
411 {
412 EXECUTE_IF_SET_IN_SBITMAP (both, 0, i,
413 fprintf (stderr, "Variable %s used in real and virtual operands\n",
414 get_name (referenced_var (i))));
415 internal_error ("SSA corruption");
416 }
417
418 sbitmap_free (used_in_real_ops);
419 sbitmap_free (used_in_virtual_ops);
420 sbitmap_free (both);
421 }
422 #endif
423
424 return map;
425 }
426
427
428 /* Allocate and return a new live range information object base on MAP. */
429
430 static tree_live_info_p
431 new_tree_live_info (var_map map)
432 {
433 tree_live_info_p live;
434 unsigned x;
435
436 live = (tree_live_info_p) xmalloc (sizeof (struct tree_live_info_d));
437 live->map = map;
438 live->num_blocks = last_basic_block;
439
440 live->global = BITMAP_ALLOC (NULL);
441
442 live->livein = (bitmap *)xmalloc (num_var_partitions (map) * sizeof (bitmap));
443 for (x = 0; x < num_var_partitions (map); x++)
444 live->livein[x] = BITMAP_ALLOC (NULL);
445
446 /* liveout is deferred until it is actually requested. */
447 live->liveout = NULL;
448 return live;
449 }
450
451
452 /* Free storage for live range info object LIVE. */
453
454 void
455 delete_tree_live_info (tree_live_info_p live)
456 {
457 int x;
458 if (live->liveout)
459 {
460 for (x = live->num_blocks - 1; x >= 0; x--)
461 BITMAP_FREE (live->liveout[x]);
462 free (live->liveout);
463 }
464 if (live->livein)
465 {
466 for (x = num_var_partitions (live->map) - 1; x >= 0; x--)
467 BITMAP_FREE (live->livein[x]);
468 free (live->livein);
469 }
470 if (live->global)
471 BITMAP_FREE (live->global);
472
473 free (live);
474 }
475
476
477 /* Using LIVE, fill in all the live-on-entry blocks between the defs and uses
478 for partition I. STACK is a varray used for temporary memory which is
479 passed in rather than being allocated on every call. */
480
481 static void
482 live_worklist (tree_live_info_p live, varray_type stack, int i)
483 {
484 unsigned b;
485 tree var;
486 basic_block def_bb = NULL;
487 edge e;
488 var_map map = live->map;
489 edge_iterator ei;
490 bitmap_iterator bi;
491
492 var = partition_to_var (map, i);
493 if (SSA_NAME_DEF_STMT (var))
494 def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (var));
495
496 EXECUTE_IF_SET_IN_BITMAP (live->livein[i], 0, b, bi)
497 {
498 VARRAY_PUSH_INT (stack, b);
499 }
500
501 while (VARRAY_ACTIVE_SIZE (stack) > 0)
502 {
503 b = VARRAY_TOP_INT (stack);
504 VARRAY_POP (stack);
505
506 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (b)->preds)
507 if (e->src != ENTRY_BLOCK_PTR)
508 {
509 /* Its not live on entry to the block its defined in. */
510 if (e->src == def_bb)
511 continue;
512 if (!bitmap_bit_p (live->livein[i], e->src->index))
513 {
514 bitmap_set_bit (live->livein[i], e->src->index);
515 VARRAY_PUSH_INT (stack, e->src->index);
516 }
517 }
518 }
519 }
520
521
522 /* If VAR is in a partition of MAP, set the bit for that partition in VEC. */
523
524 static inline void
525 set_if_valid (var_map map, bitmap vec, tree var)
526 {
527 int p = var_to_partition (map, var);
528 if (p != NO_PARTITION)
529 bitmap_set_bit (vec, p);
530 }
531
532
533 /* If VAR is in a partition and it isn't defined in DEF_VEC, set the livein and
534 global bit for it in the LIVE object. BB is the block being processed. */
535
536 static inline void
537 add_livein_if_notdef (tree_live_info_p live, bitmap def_vec,
538 tree var, basic_block bb)
539 {
540 int p = var_to_partition (live->map, var);
541 if (p == NO_PARTITION || bb == ENTRY_BLOCK_PTR)
542 return;
543 if (!bitmap_bit_p (def_vec, p))
544 {
545 bitmap_set_bit (live->livein[p], bb->index);
546 bitmap_set_bit (live->global, p);
547 }
548 }
549
550
551 /* Given partition map MAP, calculate all the live on entry bitmaps for
552 each basic block. Return a live info object. */
553
554 tree_live_info_p
555 calculate_live_on_entry (var_map map)
556 {
557 tree_live_info_p live;
558 unsigned i;
559 basic_block bb;
560 bitmap saw_def;
561 tree phi, var, stmt;
562 tree op;
563 edge e;
564 varray_type stack;
565 block_stmt_iterator bsi;
566 ssa_op_iter iter;
567 bitmap_iterator bi;
568 #ifdef ENABLE_CHECKING
569 int num;
570 edge_iterator ei;
571 #endif
572
573 saw_def = BITMAP_ALLOC (NULL);
574
575 live = new_tree_live_info (map);
576
577 FOR_EACH_BB (bb)
578 {
579 bitmap_clear (saw_def);
580
581 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
582 {
583 for (i = 0; i < (unsigned)PHI_NUM_ARGS (phi); i++)
584 {
585 var = PHI_ARG_DEF (phi, i);
586 if (!phi_ssa_name_p (var))
587 continue;
588 stmt = SSA_NAME_DEF_STMT (var);
589 e = EDGE_PRED (bb, i);
590
591 /* Any uses in PHIs which either don't have def's or are not
592 defined in the block from which the def comes, will be live
593 on entry to that block. */
594 if (!stmt || e->src != bb_for_stmt (stmt))
595 add_livein_if_notdef (live, saw_def, var, e->src);
596 }
597 }
598
599 /* Don't mark PHI results as defined until all the PHI nodes have
600 been processed. If the PHI sequence is:
601 a_3 = PHI <a_1, a_2>
602 b_3 = PHI <b_1, a_3>
603 The a_3 referred to in b_3's PHI node is the one incoming on the
604 edge, *not* the PHI node just seen. */
605
606 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
607 {
608 var = PHI_RESULT (phi);
609 set_if_valid (map, saw_def, var);
610 }
611
612 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
613 {
614 stmt = bsi_stmt (bsi);
615 get_stmt_operands (stmt);
616
617 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
618 {
619 add_livein_if_notdef (live, saw_def, op, bb);
620 }
621
622 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
623 {
624 set_if_valid (map, saw_def, op);
625 }
626 }
627 }
628
629 VARRAY_INT_INIT (stack, last_basic_block, "stack");
630 EXECUTE_IF_SET_IN_BITMAP (live->global, 0, i, bi)
631 {
632 live_worklist (live, stack, i);
633 }
634
635 #ifdef ENABLE_CHECKING
636 /* Check for live on entry partitions and report those with a DEF in
637 the program. This will typically mean an optimization has done
638 something wrong. */
639
640 bb = ENTRY_BLOCK_PTR;
641 num = 0;
642 FOR_EACH_EDGE (e, ei, bb->succs)
643 {
644 int entry_block = e->dest->index;
645 if (e->dest == EXIT_BLOCK_PTR)
646 continue;
647 for (i = 0; i < (unsigned)num_var_partitions (map); i++)
648 {
649 basic_block tmp;
650 tree d;
651 var = partition_to_var (map, i);
652 stmt = SSA_NAME_DEF_STMT (var);
653 tmp = bb_for_stmt (stmt);
654 d = default_def (SSA_NAME_VAR (var));
655
656 if (bitmap_bit_p (live_entry_blocks (live, i), entry_block))
657 {
658 if (!IS_EMPTY_STMT (stmt))
659 {
660 num++;
661 print_generic_expr (stderr, var, TDF_SLIM);
662 fprintf (stderr, " is defined ");
663 if (tmp)
664 fprintf (stderr, " in BB%d, ", tmp->index);
665 fprintf (stderr, "by:\n");
666 print_generic_expr (stderr, stmt, TDF_SLIM);
667 fprintf (stderr, "\nIt is also live-on-entry to entry BB %d",
668 entry_block);
669 fprintf (stderr, " So it appears to have multiple defs.\n");
670 }
671 else
672 {
673 if (d != var)
674 {
675 num++;
676 print_generic_expr (stderr, var, TDF_SLIM);
677 fprintf (stderr, " is live-on-entry to BB%d ",entry_block);
678 if (d)
679 {
680 fprintf (stderr, " but is not the default def of ");
681 print_generic_expr (stderr, d, TDF_SLIM);
682 fprintf (stderr, "\n");
683 }
684 else
685 fprintf (stderr, " and there is no default def.\n");
686 }
687 }
688 }
689 else
690 if (d == var)
691 {
692 /* The only way this var shouldn't be marked live on entry is
693 if it occurs in a PHI argument of the block. */
694 int z, ok = 0;
695 for (phi = phi_nodes (e->dest);
696 phi && !ok;
697 phi = PHI_CHAIN (phi))
698 {
699 for (z = 0; z < PHI_NUM_ARGS (phi); z++)
700 if (var == PHI_ARG_DEF (phi, z))
701 {
702 ok = 1;
703 break;
704 }
705 }
706 if (ok)
707 continue;
708 num++;
709 print_generic_expr (stderr, var, TDF_SLIM);
710 fprintf (stderr, " is not marked live-on-entry to entry BB%d ",
711 entry_block);
712 fprintf (stderr, "but it is a default def so it should be.\n");
713 }
714 }
715 }
716 gcc_assert (num <= 0);
717 #endif
718
719 BITMAP_FREE (saw_def);
720
721 return live;
722 }
723
724
725 /* Calculate the live on exit vectors based on the entry info in LIVEINFO. */
726
727 void
728 calculate_live_on_exit (tree_live_info_p liveinfo)
729 {
730 unsigned b;
731 unsigned i, x;
732 bitmap *on_exit;
733 basic_block bb;
734 edge e;
735 tree t, phi;
736 bitmap on_entry;
737 var_map map = liveinfo->map;
738
739 on_exit = (bitmap *)xmalloc (last_basic_block * sizeof (bitmap));
740 for (x = 0; x < (unsigned)last_basic_block; x++)
741 on_exit[x] = BITMAP_ALLOC (NULL);
742
743 /* Set all the live-on-exit bits for uses in PHIs. */
744 FOR_EACH_BB (bb)
745 {
746 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
747 for (i = 0; i < (unsigned)PHI_NUM_ARGS (phi); i++)
748 {
749 t = PHI_ARG_DEF (phi, i);
750 e = PHI_ARG_EDGE (phi, i);
751 if (!phi_ssa_name_p (t) || e->src == ENTRY_BLOCK_PTR)
752 continue;
753 set_if_valid (map, on_exit[e->src->index], t);
754 }
755 }
756
757 /* Set live on exit for all predecessors of live on entry's. */
758 for (i = 0; i < num_var_partitions (map); i++)
759 {
760 bitmap_iterator bi;
761
762 on_entry = live_entry_blocks (liveinfo, i);
763 EXECUTE_IF_SET_IN_BITMAP (on_entry, 0, b, bi)
764 {
765 edge_iterator ei;
766 FOR_EACH_EDGE (e, ei, BASIC_BLOCK (b)->preds)
767 if (e->src != ENTRY_BLOCK_PTR)
768 bitmap_set_bit (on_exit[e->src->index], i);
769 }
770 }
771
772 liveinfo->liveout = on_exit;
773 }
774
775
776 /* Initialize a tree_partition_associator object using MAP. */
777
778 static tpa_p
779 tpa_init (var_map map)
780 {
781 tpa_p tpa;
782 int num_partitions = num_var_partitions (map);
783 int x;
784
785 if (num_partitions == 0)
786 return NULL;
787
788 tpa = (tpa_p) xmalloc (sizeof (struct tree_partition_associator_d));
789 tpa->num_trees = 0;
790 tpa->uncompressed_num = -1;
791 tpa->map = map;
792 tpa->next_partition = (int *)xmalloc (num_partitions * sizeof (int));
793 memset (tpa->next_partition, TPA_NONE, num_partitions * sizeof (int));
794
795 tpa->partition_to_tree_map = (int *)xmalloc (num_partitions * sizeof (int));
796 memset (tpa->partition_to_tree_map, TPA_NONE, num_partitions * sizeof (int));
797
798 x = MAX (40, (num_partitions / 20));
799 VARRAY_TREE_INIT (tpa->trees, x, "trees");
800 VARRAY_INT_INIT (tpa->first_partition, x, "first_partition");
801
802 return tpa;
803
804 }
805
806
807 /* Remove PARTITION_INDEX from TREE_INDEX's list in the tpa structure TPA. */
808
809 void
810 tpa_remove_partition (tpa_p tpa, int tree_index, int partition_index)
811 {
812 int i;
813
814 i = tpa_first_partition (tpa, tree_index);
815 if (i == partition_index)
816 {
817 VARRAY_INT (tpa->first_partition, tree_index) = tpa->next_partition[i];
818 }
819 else
820 {
821 for ( ; i != TPA_NONE; i = tpa_next_partition (tpa, i))
822 {
823 if (tpa->next_partition[i] == partition_index)
824 {
825 tpa->next_partition[i] = tpa->next_partition[partition_index];
826 break;
827 }
828 }
829 }
830 }
831
832
833 /* Free the memory used by tree_partition_associator object TPA. */
834
835 void
836 tpa_delete (tpa_p tpa)
837 {
838 if (!tpa)
839 return;
840
841 free (tpa->partition_to_tree_map);
842 free (tpa->next_partition);
843 free (tpa);
844 }
845
846
847 /* This function will remove any tree entries from TPA which have only a single
848 element. This will help keep the size of the conflict graph down. The
849 function returns the number of remaining tree lists. */
850
851 int
852 tpa_compact (tpa_p tpa)
853 {
854 int last, x, y, first, swap_i;
855 tree swap_t;
856
857 /* Find the last list which has more than 1 partition. */
858 for (last = tpa->num_trees - 1; last > 0; last--)
859 {
860 first = tpa_first_partition (tpa, last);
861 if (tpa_next_partition (tpa, first) != NO_PARTITION)
862 break;
863 }
864
865 x = 0;
866 while (x < last)
867 {
868 first = tpa_first_partition (tpa, x);
869
870 /* If there is not more than one partition, swap with the current end
871 of the tree list. */
872 if (tpa_next_partition (tpa, first) == NO_PARTITION)
873 {
874 swap_t = VARRAY_TREE (tpa->trees, last);
875 swap_i = VARRAY_INT (tpa->first_partition, last);
876
877 /* Update the last entry. Since it is known to only have one
878 partition, there is nothing else to update. */
879 VARRAY_TREE (tpa->trees, last) = VARRAY_TREE (tpa->trees, x);
880 VARRAY_INT (tpa->first_partition, last)
881 = VARRAY_INT (tpa->first_partition, x);
882 tpa->partition_to_tree_map[tpa_first_partition (tpa, last)] = last;
883
884 /* Since this list is known to have more than one partition, update
885 the list owner entries. */
886 VARRAY_TREE (tpa->trees, x) = swap_t;
887 VARRAY_INT (tpa->first_partition, x) = swap_i;
888 for (y = tpa_first_partition (tpa, x);
889 y != NO_PARTITION;
890 y = tpa_next_partition (tpa, y))
891 tpa->partition_to_tree_map[y] = x;
892
893 /* Ensure last is a list with more than one partition. */
894 last--;
895 for (; last > x; last--)
896 {
897 first = tpa_first_partition (tpa, last);
898 if (tpa_next_partition (tpa, first) != NO_PARTITION)
899 break;
900 }
901 }
902 x++;
903 }
904
905 first = tpa_first_partition (tpa, x);
906 if (tpa_next_partition (tpa, first) != NO_PARTITION)
907 x++;
908 tpa->uncompressed_num = tpa->num_trees;
909 tpa->num_trees = x;
910 return last;
911 }
912
913
914 /* Initialize a root_var object with SSA partitions from MAP which are based
915 on each root variable. */
916
917 root_var_p
918 root_var_init (var_map map)
919 {
920 root_var_p rv;
921 int num_partitions = num_var_partitions (map);
922 int x, p;
923 tree t;
924 var_ann_t ann;
925 sbitmap seen;
926
927 rv = tpa_init (map);
928 if (!rv)
929 return NULL;
930
931 seen = sbitmap_alloc (num_partitions);
932 sbitmap_zero (seen);
933
934 /* Start at the end and work towards the front. This will provide a list
935 that is ordered from smallest to largest. */
936 for (x = num_partitions - 1; x >= 0; x--)
937 {
938 t = partition_to_var (map, x);
939
940 /* The var map may not be compacted yet, so check for NULL. */
941 if (!t)
942 continue;
943
944 p = var_to_partition (map, t);
945
946 gcc_assert (p != NO_PARTITION);
947
948 /* Make sure we only put coalesced partitions into the list once. */
949 if (TEST_BIT (seen, p))
950 continue;
951 SET_BIT (seen, p);
952 if (TREE_CODE (t) == SSA_NAME)
953 t = SSA_NAME_VAR (t);
954 ann = var_ann (t);
955 if (ann->root_var_processed)
956 {
957 rv->next_partition[p] = VARRAY_INT (rv->first_partition,
958 VAR_ANN_ROOT_INDEX (ann));
959 VARRAY_INT (rv->first_partition, VAR_ANN_ROOT_INDEX (ann)) = p;
960 }
961 else
962 {
963 ann->root_var_processed = 1;
964 VAR_ANN_ROOT_INDEX (ann) = rv->num_trees++;
965 VARRAY_PUSH_TREE (rv->trees, t);
966 VARRAY_PUSH_INT (rv->first_partition, p);
967 }
968 rv->partition_to_tree_map[p] = VAR_ANN_ROOT_INDEX (ann);
969 }
970
971 /* Reset the out_of_ssa_tag flag on each variable for later use. */
972 for (x = 0; x < rv->num_trees; x++)
973 {
974 t = VARRAY_TREE (rv->trees, x);
975 var_ann (t)->root_var_processed = 0;
976 }
977
978 sbitmap_free (seen);
979 return rv;
980 }
981
982
983 /* Initialize a type_var structure which associates all the partitions in MAP
984 of the same type to the type node's index. Volatiles are ignored. */
985
986 type_var_p
987 type_var_init (var_map map)
988 {
989 type_var_p tv;
990 int x, y, p;
991 int num_partitions = num_var_partitions (map);
992 tree t;
993 sbitmap seen;
994
995 seen = sbitmap_alloc (num_partitions);
996 sbitmap_zero (seen);
997
998 tv = tpa_init (map);
999 if (!tv)
1000 return NULL;
1001
1002 for (x = num_partitions - 1; x >= 0; x--)
1003 {
1004 t = partition_to_var (map, x);
1005
1006 /* Disallow coalescing of these types of variables. */
1007 if (!t
1008 || TREE_THIS_VOLATILE (t)
1009 || TREE_CODE (t) == RESULT_DECL
1010 || TREE_CODE (t) == PARM_DECL
1011 || (DECL_P (t)
1012 && (DECL_REGISTER (t)
1013 || !DECL_IGNORED_P (t)
1014 || DECL_RTL_SET_P (t))))
1015 continue;
1016
1017 p = var_to_partition (map, t);
1018
1019 gcc_assert (p != NO_PARTITION);
1020
1021 /* If partitions have been coalesced, only add the representative
1022 for the partition to the list once. */
1023 if (TEST_BIT (seen, p))
1024 continue;
1025 SET_BIT (seen, p);
1026 t = TREE_TYPE (t);
1027
1028 /* Find the list for this type. */
1029 for (y = 0; y < tv->num_trees; y++)
1030 if (t == VARRAY_TREE (tv->trees, y))
1031 break;
1032 if (y == tv->num_trees)
1033 {
1034 tv->num_trees++;
1035 VARRAY_PUSH_TREE (tv->trees, t);
1036 VARRAY_PUSH_INT (tv->first_partition, p);
1037 }
1038 else
1039 {
1040 tv->next_partition[p] = VARRAY_INT (tv->first_partition, y);
1041 VARRAY_INT (tv->first_partition, y) = p;
1042 }
1043 tv->partition_to_tree_map[p] = y;
1044 }
1045 sbitmap_free (seen);
1046 return tv;
1047 }
1048
1049
1050 /* Create a new coalesce list object from MAP and return it. */
1051
1052 coalesce_list_p
1053 create_coalesce_list (var_map map)
1054 {
1055 coalesce_list_p list;
1056
1057 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
1058
1059 list->map = map;
1060 list->add_mode = true;
1061 list->list = (partition_pair_p *) xcalloc (num_var_partitions (map),
1062 sizeof (struct partition_pair_d));
1063 return list;
1064 }
1065
1066
1067 /* Delete coalesce list CL. */
1068
1069 void
1070 delete_coalesce_list (coalesce_list_p cl)
1071 {
1072 free (cl->list);
1073 free (cl);
1074 }
1075
1076
1077 /* Find a matching coalesce pair object in CL for partitions P1 and P2. If
1078 one isn't found, return NULL if CREATE is false, otherwise create a new
1079 coalesce pair object and return it. */
1080
1081 static partition_pair_p
1082 find_partition_pair (coalesce_list_p cl, int p1, int p2, bool create)
1083 {
1084 partition_pair_p node, tmp;
1085 int s;
1086
1087 /* Normalize so that p1 is the smaller value. */
1088 if (p2 < p1)
1089 {
1090 s = p1;
1091 p1 = p2;
1092 p2 = s;
1093 }
1094
1095 tmp = NULL;
1096
1097 /* The list is sorted such that if we find a value greater than p2,
1098 p2 is not in the list. */
1099 for (node = cl->list[p1]; node; node = node->next)
1100 {
1101 if (node->second_partition == p2)
1102 return node;
1103 else
1104 if (node->second_partition > p2)
1105 break;
1106 tmp = node;
1107 }
1108
1109 if (!create)
1110 return NULL;
1111
1112 node = (partition_pair_p) xmalloc (sizeof (struct partition_pair_d));
1113 node->first_partition = p1;
1114 node->second_partition = p2;
1115 node->cost = 0;
1116
1117 if (tmp != NULL)
1118 {
1119 node->next = tmp->next;
1120 tmp->next = node;
1121 }
1122 else
1123 {
1124 /* This is now the first node in the list. */
1125 node->next = cl->list[p1];
1126 cl->list[p1] = node;
1127 }
1128
1129 return node;
1130 }
1131
1132
1133 /* Add a potential coalesce between P1 and P2 in CL with a cost of VALUE. */
1134
1135 void
1136 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
1137 {
1138 partition_pair_p node;
1139
1140 gcc_assert (cl->add_mode);
1141
1142 if (p1 == p2)
1143 return;
1144
1145 node = find_partition_pair (cl, p1, p2, true);
1146
1147 node->cost += value;
1148 }
1149
1150
1151 /* Comparison function to allow qsort to sort P1 and P2 in descending order. */
1152
1153 static
1154 int compare_pairs (const void *p1, const void *p2)
1155 {
1156 return (*(partition_pair_p *)p2)->cost - (*(partition_pair_p *)p1)->cost;
1157 }
1158
1159
1160 /* Prepare CL for removal of preferred pairs. When finished, list element
1161 0 has all the coalesce pairs, sorted in order from most important coalesce
1162 to least important. */
1163
1164 void
1165 sort_coalesce_list (coalesce_list_p cl)
1166 {
1167 unsigned x, num, count;
1168 partition_pair_p chain, p;
1169 partition_pair_p *list;
1170
1171 gcc_assert (cl->add_mode);
1172
1173 cl->add_mode = false;
1174
1175 /* Compact the array of lists to a single list, and count the elements. */
1176 num = 0;
1177 chain = NULL;
1178 for (x = 0; x < num_var_partitions (cl->map); x++)
1179 if (cl->list[x] != NULL)
1180 {
1181 for (p = cl->list[x]; p->next != NULL; p = p->next)
1182 num++;
1183 num++;
1184 p->next = chain;
1185 chain = cl->list[x];
1186 cl->list[x] = NULL;
1187 }
1188
1189 /* Only call qsort if there are more than 2 items. */
1190 if (num > 2)
1191 {
1192 list = xmalloc (sizeof (partition_pair_p) * num);
1193 count = 0;
1194 for (p = chain; p != NULL; p = p->next)
1195 list[count++] = p;
1196
1197 gcc_assert (count == num);
1198
1199 qsort (list, count, sizeof (partition_pair_p), compare_pairs);
1200
1201 p = list[0];
1202 for (x = 1; x < num; x++)
1203 {
1204 p->next = list[x];
1205 p = list[x];
1206 }
1207 p->next = NULL;
1208 cl->list[0] = list[0];
1209 free (list);
1210 }
1211 else
1212 {
1213 cl->list[0] = chain;
1214 if (num == 2)
1215 {
1216 /* Simply swap the two elements if they are in the wrong order. */
1217 if (chain->cost < chain->next->cost)
1218 {
1219 cl->list[0] = chain->next;
1220 cl->list[0]->next = chain;
1221 chain->next = NULL;
1222 }
1223 }
1224 }
1225 }
1226
1227
1228 /* Retrieve the best remaining pair to coalesce from CL. Returns the 2
1229 partitions via P1 and P2. Their calculated cost is returned by the function.
1230 NO_BEST_COALESCE is returned if the coalesce list is empty. */
1231
1232 static int
1233 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
1234 {
1235 partition_pair_p node;
1236 int ret;
1237
1238 gcc_assert (!cl->add_mode);
1239
1240 node = cl->list[0];
1241 if (!node)
1242 return NO_BEST_COALESCE;
1243
1244 cl->list[0] = node->next;
1245
1246 *p1 = node->first_partition;
1247 *p2 = node->second_partition;
1248 ret = node->cost;
1249 free (node);
1250
1251 return ret;
1252 }
1253
1254
1255 /* If variable VAR is in a partition in MAP, add a conflict in GRAPH between
1256 VAR and any other live partitions in VEC which are associated via TPA.
1257 Reset the live bit in VEC. */
1258
1259 static inline void
1260 add_conflicts_if_valid (tpa_p tpa, conflict_graph graph,
1261 var_map map, bitmap vec, tree var)
1262 {
1263 int p, y, first;
1264 p = var_to_partition (map, var);
1265 if (p != NO_PARTITION)
1266 {
1267 bitmap_clear_bit (vec, p);
1268 first = tpa_find_tree (tpa, p);
1269 /* If find returns nothing, this object isn't interesting. */
1270 if (first == TPA_NONE)
1271 return;
1272 /* Only add interferences between objects in the same list. */
1273 for (y = tpa_first_partition (tpa, first);
1274 y != TPA_NONE;
1275 y = tpa_next_partition (tpa, y))
1276 {
1277 if (bitmap_bit_p (vec, y))
1278 conflict_graph_add (graph, p, y);
1279 }
1280 }
1281 }
1282
1283
1284 /* Return a conflict graph for the information contained in LIVE_INFO. Only
1285 conflicts between items in the same TPA list are added. If optional
1286 coalesce list CL is passed in, any copies encountered are added. */
1287
1288 conflict_graph
1289 build_tree_conflict_graph (tree_live_info_p liveinfo, tpa_p tpa,
1290 coalesce_list_p cl)
1291 {
1292 conflict_graph graph;
1293 var_map map;
1294 bitmap live;
1295 unsigned x, y, i;
1296 basic_block bb;
1297 varray_type partition_link, tpa_to_clear, tpa_nodes;
1298 unsigned l;
1299 ssa_op_iter iter;
1300 bitmap_iterator bi;
1301
1302 map = live_var_map (liveinfo);
1303 graph = conflict_graph_new (num_var_partitions (map));
1304
1305 if (tpa_num_trees (tpa) == 0)
1306 return graph;
1307
1308 live = BITMAP_ALLOC (NULL);
1309
1310 VARRAY_INT_INIT (partition_link, num_var_partitions (map) + 1, "part_link");
1311 VARRAY_INT_INIT (tpa_nodes, tpa_num_trees (tpa), "tpa nodes");
1312 VARRAY_INT_INIT (tpa_to_clear, 50, "tpa to clear");
1313
1314 FOR_EACH_BB (bb)
1315 {
1316 block_stmt_iterator bsi;
1317 tree phi;
1318
1319 /* Start with live on exit temporaries. */
1320 bitmap_copy (live, live_on_exit (liveinfo, bb));
1321
1322 for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_prev (&bsi))
1323 {
1324 bool is_a_copy = false;
1325 tree stmt = bsi_stmt (bsi);
1326
1327 get_stmt_operands (stmt);
1328
1329 /* A copy between 2 partitions does not introduce an interference
1330 by itself. If they did, you would never be able to coalesce
1331 two things which are copied. If the two variables really do
1332 conflict, they will conflict elsewhere in the program.
1333
1334 This is handled specially here since we may also be interested
1335 in copies between real variables and SSA_NAME variables. We may
1336 be interested in trying to coalesce SSA_NAME variables with
1337 root variables in some cases. */
1338
1339 if (TREE_CODE (stmt) == MODIFY_EXPR)
1340 {
1341 tree lhs = TREE_OPERAND (stmt, 0);
1342 tree rhs = TREE_OPERAND (stmt, 1);
1343 int p1, p2;
1344 int bit;
1345
1346 if (DECL_P (lhs) || TREE_CODE (lhs) == SSA_NAME)
1347 p1 = var_to_partition (map, lhs);
1348 else
1349 p1 = NO_PARTITION;
1350
1351 if (DECL_P (rhs) || TREE_CODE (rhs) == SSA_NAME)
1352 p2 = var_to_partition (map, rhs);
1353 else
1354 p2 = NO_PARTITION;
1355
1356 if (p1 != NO_PARTITION && p2 != NO_PARTITION)
1357 {
1358 is_a_copy = true;
1359 bit = bitmap_bit_p (live, p2);
1360 /* If the RHS is live, make it not live while we add
1361 the conflicts, then make it live again. */
1362 if (bit)
1363 bitmap_clear_bit (live, p2);
1364 add_conflicts_if_valid (tpa, graph, map, live, lhs);
1365 if (bit)
1366 bitmap_set_bit (live, p2);
1367 if (cl)
1368 add_coalesce (cl, p1, p2, 1);
1369 set_if_valid (map, live, rhs);
1370 }
1371 }
1372
1373 if (!is_a_copy)
1374 {
1375 tree var;
1376 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
1377 {
1378 add_conflicts_if_valid (tpa, graph, map, live, var);
1379 }
1380
1381 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
1382 {
1383 set_if_valid (map, live, var);
1384 }
1385 }
1386 }
1387
1388 /* If result of a PHI is unused, then the loops over the statements
1389 will not record any conflicts. However, since the PHI node is
1390 going to be translated out of SSA form we must record a conflict
1391 between the result of the PHI and any variables with are live.
1392 Otherwise the out-of-ssa translation may create incorrect code. */
1393 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1394 {
1395 tree result = PHI_RESULT (phi);
1396 int p = var_to_partition (map, result);
1397
1398 if (p != NO_PARTITION && ! bitmap_bit_p (live, p))
1399 add_conflicts_if_valid (tpa, graph, map, live, result);
1400 }
1401
1402 /* Anything which is still live at this point interferes.
1403 In order to implement this efficiently, only conflicts between
1404 partitions which have the same TPA root need be added.
1405 TPA roots which have been seen are tracked in 'tpa_nodes'. A nonzero
1406 entry points to an index into 'partition_link', which then indexes
1407 into itself forming a linked list of partitions sharing a tpa root
1408 which have been seen as live up to this point. Since partitions start
1409 at index zero, all entries in partition_link are (partition + 1).
1410
1411 Conflicts are added between the current partition and any already seen.
1412 tpa_clear contains all the tpa_roots processed, and these are the only
1413 entries which need to be zero'd out for a clean restart. */
1414
1415 EXECUTE_IF_SET_IN_BITMAP (live, 0, x, bi)
1416 {
1417 i = tpa_find_tree (tpa, x);
1418 if (i != (unsigned)TPA_NONE)
1419 {
1420 int start = VARRAY_INT (tpa_nodes, i);
1421 /* If start is 0, a new root reference list is being started.
1422 Register it to be cleared. */
1423 if (!start)
1424 VARRAY_PUSH_INT (tpa_to_clear, i);
1425
1426 /* Add interferences to other tpa members seen. */
1427 for (y = start; y != 0; y = VARRAY_INT (partition_link, y))
1428 conflict_graph_add (graph, x, y - 1);
1429 VARRAY_INT (tpa_nodes, i) = x + 1;
1430 VARRAY_INT (partition_link, x + 1) = start;
1431 }
1432 }
1433
1434 /* Now clear the used tpa root references. */
1435 for (l = 0; l < VARRAY_ACTIVE_SIZE (tpa_to_clear); l++)
1436 VARRAY_INT (tpa_nodes, VARRAY_INT (tpa_to_clear, l)) = 0;
1437 VARRAY_POP_ALL (tpa_to_clear);
1438 }
1439
1440 BITMAP_FREE (live);
1441 return graph;
1442 }
1443
1444
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. */
1450
1451 void
1452 coalesce_tpa_members (tpa_p tpa, conflict_graph graph, var_map map,
1453 coalesce_list_p cl, FILE *debug)
1454 {
1455 int x, y, z, w;
1456 tree var, tmp;
1457
1458 /* Attempt to coalesce any items in a coalesce list. */
1459 if (cl)
1460 {
1461 while (pop_best_coalesce (cl, &x, &y) != NO_BEST_COALESCE)
1462 {
1463 if (debug)
1464 {
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);
1469 }
1470
1471 w = tpa_find_tree (tpa, x);
1472 z = tpa_find_tree (tpa, y);
1473 if (w != z || w == TPA_NONE || z == TPA_NONE)
1474 {
1475 if (debug)
1476 {
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);
1483 }
1484 continue;
1485 }
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)
1493 {
1494 if (debug)
1495 fprintf (debug, ": Already Coalesced.\n");
1496 continue;
1497 }
1498 if (!conflict_graph_conflict_p (graph, x, y))
1499 {
1500 z = var_union (map, var, tmp);
1501 if (z == NO_PARTITION)
1502 {
1503 if (debug)
1504 fprintf (debug, ": Unable to perform partition union.\n");
1505 continue;
1506 }
1507
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)
1511 {
1512 conflict_graph_merge_regs (graph, x, y);
1513 w = tpa_find_tree (tpa, y);
1514 tpa_remove_partition (tpa, w, y);
1515 }
1516 else
1517 {
1518 conflict_graph_merge_regs (graph, y, x);
1519 w = tpa_find_tree (tpa, x);
1520 tpa_remove_partition (tpa, w, x);
1521 }
1522
1523 if (debug)
1524 fprintf (debug, ": Success -> %d\n", z);
1525 }
1526 else
1527 if (debug)
1528 fprintf (debug, ": Fail due to conflict\n");
1529 }
1530 /* If using a coalesce list, don't try to coalesce anything else. */
1531 return;
1532 }
1533
1534 for (x = 0; x < tpa_num_trees (tpa); x++)
1535 {
1536 while (tpa_first_partition (tpa, x) != TPA_NONE)
1537 {
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);
1542
1543 var = partition_to_var (map, y);
1544 /* p1 is the partition representative to which y belongs. */
1545 p1 = var_to_partition (map, var);
1546
1547 for (z = tpa_next_partition (tpa, y);
1548 z != TPA_NONE;
1549 z = tpa_next_partition (tpa, z))
1550 {
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)
1555 {
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);
1561 }
1562
1563 /* If partitions are already merged, don't check for conflict. */
1564 if (tmp == var)
1565 {
1566 tpa_remove_partition (tpa, x, z);
1567 if (debug)
1568 fprintf (debug, ": Already coalesced\n");
1569 }
1570 else
1571 if (!conflict_graph_conflict_p (graph, p1, p2))
1572 {
1573 int v;
1574 if (tpa_find_tree (tpa, y) == TPA_NONE
1575 || tpa_find_tree (tpa, z) == TPA_NONE)
1576 {
1577 if (debug)
1578 fprintf (debug, ": Fail non-TPA member\n");
1579 continue;
1580 }
1581 if ((v = var_union (map, var, tmp)) == NO_PARTITION)
1582 {
1583 if (debug)
1584 fprintf (debug, ": Fail cannot combine partitions\n");
1585 continue;
1586 }
1587
1588 tpa_remove_partition (tpa, x, z);
1589 if (v == p1)
1590 conflict_graph_merge_regs (graph, v, z);
1591 else
1592 {
1593 /* Update the first partition's representative. */
1594 conflict_graph_merge_regs (graph, v, y);
1595 p1 = v;
1596 }
1597
1598 /* The root variable of the partition may be changed
1599 now. */
1600 var = partition_to_var (map, p1);
1601
1602 if (debug)
1603 fprintf (debug, ": Success -> %d\n", v);
1604 }
1605 else
1606 if (debug)
1607 fprintf (debug, ": Fail, Conflict\n");
1608 }
1609 }
1610 }
1611 }
1612
1613
1614 /* Send debug info for coalesce list CL to file F. */
1615
1616 void
1617 dump_coalesce_list (FILE *f, coalesce_list_p cl)
1618 {
1619 partition_pair_p node;
1620 int x, num;
1621 tree var;
1622
1623 if (cl->add_mode)
1624 {
1625 fprintf (f, "Coalesce List:\n");
1626 num = num_var_partitions (cl->map);
1627 for (x = 0; x < num; x++)
1628 {
1629 node = cl->list[x];
1630 if (node)
1631 {
1632 fprintf (f, "[");
1633 print_generic_expr (f, partition_to_var (cl->map, x), TDF_SLIM);
1634 fprintf (f, "] - ");
1635 for ( ; node; node = node->next)
1636 {
1637 var = partition_to_var (cl->map, node->second_partition);
1638 print_generic_expr (f, var, TDF_SLIM);
1639 fprintf (f, "(%1d), ", node->cost);
1640 }
1641 fprintf (f, "\n");
1642 }
1643 }
1644 }
1645 else
1646 {
1647 fprintf (f, "Sorted Coalesce list:\n");
1648 for (node = cl->list[0]; node; node = node->next)
1649 {
1650 fprintf (f, "(%d) ", node->cost);
1651 var = partition_to_var (cl->map, node->first_partition);
1652 print_generic_expr (f, var, TDF_SLIM);
1653 fprintf (f, " : ");
1654 var = partition_to_var (cl->map, node->second_partition);
1655 print_generic_expr (f, var, TDF_SLIM);
1656 fprintf (f, "\n");
1657 }
1658 }
1659 }
1660
1661
1662 /* Output tree_partition_associator object TPA to file F.. */
1663
1664 void
1665 tpa_dump (FILE *f, tpa_p tpa)
1666 {
1667 int x, i;
1668
1669 if (!tpa)
1670 return;
1671
1672 for (x = 0; x < tpa_num_trees (tpa); x++)
1673 {
1674 print_generic_expr (f, tpa_tree (tpa, x), TDF_SLIM);
1675 fprintf (f, " : (");
1676 for (i = tpa_first_partition (tpa, x);
1677 i != TPA_NONE;
1678 i = tpa_next_partition (tpa, i))
1679 {
1680 fprintf (f, "(%d)",i);
1681 print_generic_expr (f, partition_to_var (tpa->map, i), TDF_SLIM);
1682 fprintf (f, " ");
1683
1684 #ifdef ENABLE_CHECKING
1685 if (tpa_find_tree (tpa, i) != x)
1686 fprintf (f, "**find tree incorrectly set** ");
1687 #endif
1688
1689 }
1690 fprintf (f, ")\n");
1691 }
1692 fflush (f);
1693 }
1694
1695
1696 /* Output partition map MAP to file F. */
1697
1698 void
1699 dump_var_map (FILE *f, var_map map)
1700 {
1701 int t;
1702 unsigned x, y;
1703 int p;
1704
1705 fprintf (f, "\nPartition map \n\n");
1706
1707 for (x = 0; x < map->num_partitions; x++)
1708 {
1709 if (map->compact_to_partition != NULL)
1710 p = map->compact_to_partition[x];
1711 else
1712 p = x;
1713
1714 if (map->partition_to_var[p] == NULL_TREE)
1715 continue;
1716
1717 t = 0;
1718 for (y = 1; y < num_ssa_names; y++)
1719 {
1720 p = partition_find (map->var_partition, y);
1721 if (map->partition_to_compact)
1722 p = map->partition_to_compact[p];
1723 if (p == (int)x)
1724 {
1725 if (t++ == 0)
1726 {
1727 fprintf(f, "Partition %d (", x);
1728 print_generic_expr (f, partition_to_var (map, p), TDF_SLIM);
1729 fprintf (f, " - ");
1730 }
1731 fprintf (f, "%d ", y);
1732 }
1733 }
1734 if (t != 0)
1735 fprintf (f, ")\n");
1736 }
1737 fprintf (f, "\n");
1738 }
1739
1740
1741 /* Output live range info LIVE to file F, controlled by FLAG. */
1742
1743 void
1744 dump_live_info (FILE *f, tree_live_info_p live, int flag)
1745 {
1746 basic_block bb;
1747 unsigned i;
1748 var_map map = live->map;
1749 bitmap_iterator bi;
1750
1751 if ((flag & LIVEDUMP_ENTRY) && live->livein)
1752 {
1753 FOR_EACH_BB (bb)
1754 {
1755 fprintf (f, "\nLive on entry to BB%d : ", bb->index);
1756 for (i = 0; i < num_var_partitions (map); i++)
1757 {
1758 if (bitmap_bit_p (live_entry_blocks (live, i), bb->index))
1759 {
1760 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1761 fprintf (f, " ");
1762 }
1763 }
1764 fprintf (f, "\n");
1765 }
1766 }
1767
1768 if ((flag & LIVEDUMP_EXIT) && live->liveout)
1769 {
1770 FOR_EACH_BB (bb)
1771 {
1772 fprintf (f, "\nLive on exit from BB%d : ", bb->index);
1773 EXECUTE_IF_SET_IN_BITMAP (live->liveout[bb->index], 0, i, bi)
1774 {
1775 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1776 fprintf (f, " ");
1777 }
1778 fprintf (f, "\n");
1779 }
1780 }
1781 }
1782
1783 #ifdef ENABLE_CHECKING
1784 void
1785 register_ssa_partition_check (tree ssa_var)
1786 {
1787 gcc_assert (TREE_CODE (ssa_var) == SSA_NAME);
1788 if (!is_gimple_reg (SSA_NAME_VAR (ssa_var)))
1789 {
1790 fprintf (stderr, "Illegally registering a virtual SSA name :");
1791 print_generic_expr (stderr, ssa_var, TDF_SLIM);
1792 fprintf (stderr, " in the SSA->Normal phase.\n");
1793 internal_error ("SSA corruption");
1794 }
1795 }
1796 #endif