targhooks.c (default_unwind_emit, [...]): Use gcc_assert, gcc_unreachable & internal_...
[gcc.git] / gcc / tree-ssa-live.c
1 /* Liveness for SSA trees.
2 Copyright (C) 2003 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "basic-block.h"
29 #include "function.h"
30 #include "diagnostic.h"
31 #include "bitmap.h"
32 #include "tree-flow.h"
33 #include "tree-gimple.h"
34 #include "tree-inline.h"
35 #include "varray.h"
36 #include "timevar.h"
37 #include "tree-alias-common.h"
38 #include "hashtab.h"
39 #include "tree-dump.h"
40 #include "tree-ssa-live.h"
41 #include "errors.h"
42
43 static void live_worklist (tree_live_info_p, varray_type, 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);
52
53 /* This is where the mapping from SSA version number to real storage variable
54 is tracked.
55
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.
60
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. */
64
65 /* Create a variable partition map of SIZE, initialize and return it. */
66
67 var_map
68 init_var_map (int size)
69 {
70 var_map map;
71
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));
77
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;
84 }
85
86
87 /* Free memory associated with MAP. */
88
89 void
90 delete_var_map (var_map map)
91 {
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);
101 }
102
103
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. */
107
108 int
109 var_union (var_map map, tree var1, tree var2)
110 {
111 int p1, p2, p3;
112 tree root_var = NULL_TREE;
113 tree other_var = NULL_TREE;
114
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. */
118
119 if (TREE_CODE (var1) == SSA_NAME)
120 p1 = partition_find (map->var_partition, SSA_NAME_VERSION (var1));
121 else
122 {
123 p1 = var_to_partition (map, var1);
124 if (map->compact_to_partition)
125 p1 = map->compact_to_partition[p1];
126 root_var = var1;
127 }
128
129 if (TREE_CODE (var2) == SSA_NAME)
130 p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2));
131 else
132 {
133 p2 = var_to_partition (map, var2);
134 if (map->compact_to_partition)
135 p2 = map->compact_to_partition[p2];
136
137 /* If there is no root_var set, or its 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)))
140 {
141 other_var = root_var;
142 root_var = var2;
143 }
144 else
145 other_var = var2;
146 }
147
148 gcc_assert (p1 != NO_PARTITION);
149 gcc_assert (p2 != NO_PARTITION);
150
151 if (p1 == p2)
152 p3 = p1;
153 else
154 p3 = partition_union (map->var_partition, p1, p2);
155
156 if (map->partition_to_compact)
157 p3 = map->partition_to_compact[p3];
158
159 if (root_var)
160 change_partition_var (map, root_var, p3);
161 if (other_var)
162 change_partition_var (map, other_var, p3);
163
164 return p3;
165 }
166
167
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.
173
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.
180
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. */
184
185 void
186 compact_var_map (var_map map, int flags)
187 {
188 sbitmap used;
189 int x, limit, count, tmp, root, root_i;
190 tree var;
191 root_var_p rv = NULL;
192
193 limit = map->partition_size;
194 used = sbitmap_alloc (limit);
195 sbitmap_zero (used);
196
197 /* Already compressed? Abandon the old one. */
198 if (map->partition_to_compact)
199 {
200 free (map->partition_to_compact);
201 map->partition_to_compact = NULL;
202 }
203 if (map->compact_to_partition)
204 {
205 free (map->compact_to_partition);
206 map->compact_to_partition = NULL;
207 }
208
209 map->num_partitions = map->partition_size;
210
211 if (flags & VARMAP_NO_SINGLE_DEFS)
212 rv = root_var_init (map);
213
214 map->partition_to_compact = (int *)xmalloc (limit * sizeof (int));
215 memset (map->partition_to_compact, 0xff, (limit * sizeof (int)));
216
217 /* Find out which partitions are actually referenced. */
218 count = 0;
219 for (x = 0; x < limit; x++)
220 {
221 tmp = partition_find (map->var_partition, x);
222 if (!TEST_BIT (used, tmp) && map->partition_to_var[tmp] != NULL_TREE)
223 {
224 /* It is referenced, check to see if there is more than one version
225 in the root_var table, if one is available. */
226 if (rv)
227 {
228 root = root_var_find (rv, tmp);
229 root_i = root_var_first_partition (rv, root);
230 /* If there is only one, don't include this in the compaction. */
231 if (root_var_next_partition (rv, root_i) == ROOT_VAR_NONE)
232 continue;
233 }
234 SET_BIT (used, tmp);
235 count++;
236 }
237 }
238
239 /* Build a compacted partitioning. */
240 if (count != limit)
241 {
242 map->compact_to_partition = (int *)xmalloc (count * sizeof (int));
243 count = 0;
244 /* SSA renaming begins at 1, so skip 0 when compacting. */
245 EXECUTE_IF_SET_IN_SBITMAP (used, 1, x,
246 {
247 map->partition_to_compact[x] = count;
248 map->compact_to_partition[count] = x;
249 var = map->partition_to_var[x];
250 if (TREE_CODE (var) != SSA_NAME)
251 change_partition_var (map, var, count);
252 count++;
253 });
254 }
255 else
256 {
257 free (map->partition_to_compact);
258 map->partition_to_compact = NULL;
259 }
260
261 map->num_partitions = count;
262
263 if (rv)
264 root_var_delete (rv);
265 sbitmap_free (used);
266 }
267
268
269 /* This function is used to change the representative variable in MAP for VAR's
270 partition from an SSA_NAME variable to a regular variable. This allows
271 partitions to be mapped back to real variables. */
272
273 void
274 change_partition_var (var_map map, tree var, int part)
275 {
276 var_ann_t ann;
277
278 gcc_assert (TREE_CODE (var) != SSA_NAME);
279
280 ann = var_ann (var);
281 ann->out_of_ssa_tag = 1;
282 VAR_ANN_PARTITION (ann) = part;
283 if (map->compact_to_partition)
284 map->partition_to_var[map->compact_to_partition[part]] = var;
285 }
286
287
288 /* Helper function for mark_all_vars_used, called via walk_tree. */
289
290 static tree
291 mark_all_vars_used_1 (tree *tp, int *walk_subtrees,
292 void *data ATTRIBUTE_UNUSED)
293 {
294 tree t = *tp;
295
296 /* Only need to mark VAR_DECLS; parameters and return results are not
297 eliminated as unused. */
298 if (TREE_CODE (t) == VAR_DECL)
299 set_is_used (t);
300
301 if (DECL_P (t) || TYPE_P (t))
302 *walk_subtrees = 0;
303
304 return NULL;
305 }
306
307 /* Mark all VAR_DECLS under *EXPR_P as used, so that they won't be
308 eliminated during the tree->rtl conversion process. */
309
310 static inline void
311 mark_all_vars_used (tree *expr_p)
312 {
313 walk_tree (expr_p, mark_all_vars_used_1, NULL, NULL);
314 }
315
316 /* This function looks through the program and uses FLAGS to determine what
317 SSA versioned variables are given entries in a new partition table. This
318 new partition map is returned. */
319
320 var_map
321 create_ssa_var_map (int flags)
322 {
323 block_stmt_iterator bsi;
324 basic_block bb;
325 tree dest, use;
326 tree stmt;
327 stmt_ann_t ann;
328 var_map map;
329 ssa_op_iter iter;
330 #ifdef ENABLE_CHECKING
331 sbitmap used_in_real_ops;
332 sbitmap used_in_virtual_ops;
333 #endif
334
335 map = init_var_map (num_ssa_names + 1);
336
337 #ifdef ENABLE_CHECKING
338 used_in_real_ops = sbitmap_alloc (num_referenced_vars);
339 sbitmap_zero (used_in_real_ops);
340
341 used_in_virtual_ops = sbitmap_alloc (num_referenced_vars);
342 sbitmap_zero (used_in_virtual_ops);
343 #endif
344
345 if (flags & SSA_VAR_MAP_REF_COUNT)
346 {
347 map->ref_count
348 = (int *)xmalloc (((num_ssa_names + 1) * sizeof (int)));
349 memset (map->ref_count, 0, (num_ssa_names + 1) * sizeof (int));
350 }
351
352 FOR_EACH_BB (bb)
353 {
354 tree phi, arg;
355 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
356 {
357 int i;
358 register_ssa_partition (map, PHI_RESULT (phi), false);
359 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
360 {
361 arg = PHI_ARG_DEF (phi, i);
362 if (TREE_CODE (arg) == SSA_NAME)
363 register_ssa_partition (map, arg, true);
364
365 mark_all_vars_used (&PHI_ARG_DEF_TREE (phi, i));
366 }
367 }
368
369 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
370 {
371 stmt = bsi_stmt (bsi);
372 get_stmt_operands (stmt);
373 ann = stmt_ann (stmt);
374
375 /* Register USE and DEF operands in each statement. */
376 FOR_EACH_SSA_TREE_OPERAND (use , stmt, iter, SSA_OP_USE)
377 {
378 register_ssa_partition (map, use, true);
379
380 #ifdef ENABLE_CHECKING
381 SET_BIT (used_in_real_ops, var_ann (SSA_NAME_VAR (use))->uid);
382 #endif
383 }
384
385 FOR_EACH_SSA_TREE_OPERAND (dest, stmt, iter, SSA_OP_DEF)
386 {
387 register_ssa_partition (map, dest, false);
388
389 #ifdef ENABLE_CHECKING
390 SET_BIT (used_in_real_ops, var_ann (SSA_NAME_VAR (dest))->uid);
391 #endif
392 }
393
394 #ifdef ENABLE_CHECKING
395 /* Validate that virtual ops don't get used in funny ways. */
396 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter,
397 SSA_OP_VIRTUAL_USES | SSA_OP_VMUSTDEF)
398 {
399 SET_BIT (used_in_virtual_ops, var_ann (SSA_NAME_VAR (use))->uid);
400 }
401
402 #endif /* ENABLE_CHECKING */
403
404 mark_all_vars_used (bsi_stmt_ptr (bsi));
405 }
406 }
407
408 #if defined ENABLE_CHECKING
409 {
410 unsigned i;
411 sbitmap both = sbitmap_alloc (num_referenced_vars);
412 sbitmap_a_and_b (both, used_in_real_ops, used_in_virtual_ops);
413 if (sbitmap_first_set_bit (both) >= 0)
414 {
415 EXECUTE_IF_SET_IN_SBITMAP (both, 0, i,
416 fprintf (stderr, "Variable %s used in real and virtual operands\n",
417 get_name (referenced_var (i))));
418 internal_error ("SSA corruption");
419 }
420
421 sbitmap_free (used_in_real_ops);
422 sbitmap_free (used_in_virtual_ops);
423 sbitmap_free (both);
424 }
425 #endif
426
427 return map;
428 }
429
430
431 /* Allocate and return a new live range information object base on MAP. */
432
433 static tree_live_info_p
434 new_tree_live_info (var_map map)
435 {
436 tree_live_info_p live;
437 int x;
438
439 live = (tree_live_info_p) xmalloc (sizeof (struct tree_live_info_d));
440 live->map = map;
441 live->num_blocks = last_basic_block;
442
443 live->global = BITMAP_XMALLOC ();
444
445 live->livein = (bitmap *)xmalloc (num_var_partitions (map) * sizeof (bitmap));
446 for (x = 0; x < num_var_partitions (map); x++)
447 live->livein[x] = BITMAP_XMALLOC ();
448
449 /* liveout is deferred until it is actually requested. */
450 live->liveout = NULL;
451 return live;
452 }
453
454
455 /* Free storage for live range info object LIVE. */
456
457 void
458 delete_tree_live_info (tree_live_info_p live)
459 {
460 int x;
461 if (live->liveout)
462 {
463 for (x = live->num_blocks - 1; x >= 0; x--)
464 BITMAP_XFREE (live->liveout[x]);
465 free (live->liveout);
466 }
467 if (live->livein)
468 {
469 for (x = num_var_partitions (live->map) - 1; x >= 0; x--)
470 BITMAP_XFREE (live->livein[x]);
471 free (live->livein);
472 }
473 if (live->global)
474 BITMAP_XFREE (live->global);
475
476 free (live);
477 }
478
479
480 /* Using LIVE, fill in all the live-on-entry blocks between the defs and uses
481 for partition I. STACK is a varray used for temporary memory which is
482 passed in rather than being allocated on every call. */
483
484 static void
485 live_worklist (tree_live_info_p live, varray_type stack, int i)
486 {
487 int b;
488 tree var;
489 basic_block def_bb = NULL;
490 edge e;
491 var_map map = live->map;
492
493 var = partition_to_var (map, i);
494 if (SSA_NAME_DEF_STMT (var))
495 def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (var));
496
497 EXECUTE_IF_SET_IN_BITMAP (live->livein[i], 0, b,
498 {
499 VARRAY_PUSH_INT (stack, b);
500 });
501
502 while (VARRAY_ACTIVE_SIZE (stack) > 0)
503 {
504 b = VARRAY_TOP_INT (stack);
505 VARRAY_POP (stack);
506
507 for (e = BASIC_BLOCK (b)->pred; e; e = e->pred_next)
508 if (e->src != ENTRY_BLOCK_PTR)
509 {
510 /* Its not live on entry to the block its defined in. */
511 if (e->src == def_bb)
512 continue;
513 if (!bitmap_bit_p (live->livein[i], e->src->index))
514 {
515 bitmap_set_bit (live->livein[i], e->src->index);
516 VARRAY_PUSH_INT (stack, e->src->index);
517 }
518 }
519 }
520 }
521
522
523 /* If VAR is in a partition of MAP, set the bit for that partition in VEC. */
524
525 static inline void
526 set_if_valid (var_map map, bitmap vec, tree var)
527 {
528 int p = var_to_partition (map, var);
529 if (p != NO_PARTITION)
530 bitmap_set_bit (vec, p);
531 }
532
533
534 /* If VAR is in a partition and it isn't defined in DEF_VEC, set the livein and
535 global bit for it in the LIVE object. BB is the block being processed. */
536
537 static inline void
538 add_livein_if_notdef (tree_live_info_p live, bitmap def_vec,
539 tree var, basic_block bb)
540 {
541 int p = var_to_partition (live->map, var);
542 if (p == NO_PARTITION || bb == ENTRY_BLOCK_PTR)
543 return;
544 if (!bitmap_bit_p (def_vec, p))
545 {
546 bitmap_set_bit (live->livein[p], bb->index);
547 bitmap_set_bit (live->global, p);
548 }
549 }
550
551
552 /* Given partition map MAP, calculate all the live on entry bitmaps for
553 each basic block. Return a live info object. */
554
555 tree_live_info_p
556 calculate_live_on_entry (var_map map)
557 {
558 tree_live_info_p live;
559 int i;
560 basic_block bb;
561 bitmap saw_def;
562 tree phi, var, stmt;
563 tree op;
564 edge e;
565 varray_type stack;
566 block_stmt_iterator bsi;
567 stmt_ann_t ann;
568 ssa_op_iter iter;
569 #ifdef ENABLE_CHECKING
570 int num;
571 #endif
572
573
574 saw_def = BITMAP_XMALLOC ();
575
576 live = new_tree_live_info (map);
577
578 FOR_EACH_BB (bb)
579 {
580 bitmap_clear (saw_def);
581
582 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
583 {
584 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
585 {
586 var = PHI_ARG_DEF (phi, i);
587 if (!phi_ssa_name_p (var))
588 continue;
589 stmt = SSA_NAME_DEF_STMT (var);
590 e = PHI_ARG_EDGE (phi, i);
591
592 /* Any uses in PHIs which either don't have def's or are not
593 defined in the block from which the def comes, will be live
594 on entry to that block. */
595 if (!stmt || e->src != bb_for_stmt (stmt))
596 add_livein_if_notdef (live, saw_def, var, e->src);
597 }
598 }
599
600 /* Don't mark PHI results as defined until all the PHI nodes have
601 been processed. If the PHI sequence is:
602 a_3 = PHI <a_1, a_2>
603 b_3 = PHI <b_1, a_3>
604 The a_3 referred to in b_3's PHI node is the one incoming on the
605 edge, *not* the PHI node just seen. */
606
607 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
608 {
609 var = PHI_RESULT (phi);
610 set_if_valid (map, saw_def, var);
611 }
612
613 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
614 {
615 stmt = bsi_stmt (bsi);
616 get_stmt_operands (stmt);
617 ann = stmt_ann (stmt);
618
619 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
620 {
621 add_livein_if_notdef (live, saw_def, op, bb);
622 }
623
624 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
625 {
626 set_if_valid (map, saw_def, op);
627 }
628 }
629 }
630
631 VARRAY_INT_INIT (stack, last_basic_block, "stack");
632 EXECUTE_IF_SET_IN_BITMAP (live->global, 0, i,
633 {
634 live_worklist (live, stack, i);
635 });
636
637 #ifdef ENABLE_CHECKING
638 /* Check for live on entry partitions and report those with a DEF in
639 the program. This will typically mean an optimization has done
640 something wrong. */
641
642 bb = ENTRY_BLOCK_PTR;
643 num = 0;
644 for (e = bb->succ; e; e = e->succ_next)
645 {
646 int entry_block = e->dest->index;
647 if (e->dest == EXIT_BLOCK_PTR)
648 continue;
649 for (i = 0; i < num_var_partitions (map); i++)
650 {
651 basic_block tmp;
652 tree d;
653 var = partition_to_var (map, i);
654 stmt = SSA_NAME_DEF_STMT (var);
655 tmp = bb_for_stmt (stmt);
656 d = default_def (SSA_NAME_VAR (var));
657
658 if (bitmap_bit_p (live_entry_blocks (live, i), entry_block))
659 {
660 if (!IS_EMPTY_STMT (stmt))
661 {
662 num++;
663 print_generic_expr (stderr, var, TDF_SLIM);
664 fprintf (stderr, " is defined ");
665 if (tmp)
666 fprintf (stderr, " in BB%d, ", tmp->index);
667 fprintf (stderr, "by:\n");
668 print_generic_expr (stderr, stmt, TDF_SLIM);
669 fprintf (stderr, "\nIt is also live-on-entry to entry BB %d",
670 entry_block);
671 fprintf (stderr, " So it appears to have multiple defs.\n");
672 }
673 else
674 {
675 if (d != var)
676 {
677 num++;
678 print_generic_expr (stderr, var, TDF_SLIM);
679 fprintf (stderr, " is live-on-entry to BB%d ",entry_block);
680 if (d)
681 {
682 fprintf (stderr, " but is not the default def of ");
683 print_generic_expr (stderr, d, TDF_SLIM);
684 fprintf (stderr, "\n");
685 }
686 else
687 fprintf (stderr, " and there is no default def.\n");
688 }
689 }
690 }
691 else
692 if (d == var)
693 {
694 /* The only way this var shouldn't be marked live on entry is
695 if it occurs in a PHI argument of the block. */
696 int z, ok = 0;
697 for (phi = phi_nodes (e->dest);
698 phi && !ok;
699 phi = PHI_CHAIN (phi))
700 {
701 for (z = 0; z < PHI_NUM_ARGS (phi); z++)
702 if (var == PHI_ARG_DEF (phi, z))
703 {
704 ok = 1;
705 break;
706 }
707 }
708 if (ok)
709 continue;
710 num++;
711 print_generic_expr (stderr, var, TDF_SLIM);
712 fprintf (stderr, " is not marked live-on-entry to entry BB%d ",
713 entry_block);
714 fprintf (stderr, "but it is a default def so it should be.\n");
715 }
716 }
717 }
718 gcc_assert (num <= 0);
719 #endif
720
721 BITMAP_XFREE (saw_def);
722
723 return live;
724 }
725
726
727 /* Calculate the live on exit vectors based on the entry info in LIVEINFO. */
728
729 void
730 calculate_live_on_exit (tree_live_info_p liveinfo)
731 {
732 unsigned b;
733 int i, x;
734 bitmap *on_exit;
735 basic_block bb;
736 edge e;
737 tree t, phi;
738 bitmap on_entry;
739 var_map map = liveinfo->map;
740
741 on_exit = (bitmap *)xmalloc (last_basic_block * sizeof (bitmap));
742 for (x = 0; x < last_basic_block; x++)
743 on_exit[x] = BITMAP_XMALLOC ();
744
745 /* Set all the live-on-exit bits for uses in PHIs. */
746 FOR_EACH_BB (bb)
747 {
748 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
749 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
750 {
751 t = PHI_ARG_DEF (phi, i);
752 e = PHI_ARG_EDGE (phi, i);
753 if (!phi_ssa_name_p (t) || e->src == ENTRY_BLOCK_PTR)
754 continue;
755 set_if_valid (map, on_exit[e->src->index], t);
756 }
757 }
758
759 /* Set live on exit for all predecessors of live on entry's. */
760 for (i = 0; i < num_var_partitions (map); i++)
761 {
762 on_entry = live_entry_blocks (liveinfo, i);
763 EXECUTE_IF_SET_IN_BITMAP (on_entry, 0, b,
764 {
765 for (e = BASIC_BLOCK(b)->pred; e; e = e->pred_next)
766 if (e->src != ENTRY_BLOCK_PTR)
767 bitmap_set_bit (on_exit[e->src->index], i);
768 });
769 }
770
771 liveinfo->liveout = on_exit;
772 }
773
774
775 /* Initialize a tree_partition_associator object using MAP. */
776
777 tpa_p
778 tpa_init (var_map map)
779 {
780 tpa_p tpa;
781 int num_partitions = num_var_partitions (map);
782 int x;
783
784 if (num_partitions == 0)
785 return NULL;
786
787 tpa = (tpa_p) xmalloc (sizeof (struct tree_partition_associator_d));
788 tpa->num_trees = 0;
789 tpa->uncompressed_num = -1;
790 tpa->map = map;
791 tpa->next_partition = (int *)xmalloc (num_partitions * sizeof (int));
792 memset (tpa->next_partition, TPA_NONE, num_partitions * sizeof (int));
793
794 tpa->partition_to_tree_map = (int *)xmalloc (num_partitions * sizeof (int));
795 memset (tpa->partition_to_tree_map, TPA_NONE, num_partitions * sizeof (int));
796
797 x = MAX (40, (num_partitions / 20));
798 VARRAY_TREE_INIT (tpa->trees, x, "trees");
799 VARRAY_INT_INIT (tpa->first_partition, x, "first_partition");
800
801 return tpa;
802
803 }
804
805
806 /* Remove PARTITION_INDEX from TREE_INDEX's list in the tpa structure TPA. */
807
808 void
809 tpa_remove_partition (tpa_p tpa, int tree_index, int partition_index)
810 {
811 int i;
812
813 i = tpa_first_partition (tpa, tree_index);
814 if (i == partition_index)
815 {
816 VARRAY_INT (tpa->first_partition, tree_index) = tpa->next_partition[i];
817 }
818 else
819 {
820 for ( ; i != TPA_NONE; i = tpa_next_partition (tpa, i))
821 {
822 if (tpa->next_partition[i] == partition_index)
823 {
824 tpa->next_partition[i] = tpa->next_partition[partition_index];
825 break;
826 }
827 }
828 }
829 }
830
831
832 /* Free the memory used by tree_partition_associator object TPA. */
833
834 void
835 tpa_delete (tpa_p tpa)
836 {
837 if (!tpa)
838 return;
839
840 free (tpa->partition_to_tree_map);
841 free (tpa->next_partition);
842 free (tpa);
843 }
844
845
846 /* This function will remove any tree entries from TPA which have only a single
847 element. This will help keep the size of the conflict graph down. The
848 function returns the number of remaining tree lists. */
849
850 int
851 tpa_compact (tpa_p tpa)
852 {
853 int last, x, y, first, swap_i;
854 tree swap_t;
855
856 /* Find the last list which has more than 1 partition. */
857 for (last = tpa->num_trees - 1; last > 0; last--)
858 {
859 first = tpa_first_partition (tpa, last);
860 if (tpa_next_partition (tpa, first) != NO_PARTITION)
861 break;
862 }
863
864 x = 0;
865 while (x < last)
866 {
867 first = tpa_first_partition (tpa, x);
868
869 /* If there is not more than one partition, swap with the current end
870 of the tree list. */
871 if (tpa_next_partition (tpa, first) == NO_PARTITION)
872 {
873 swap_t = VARRAY_TREE (tpa->trees, last);
874 swap_i = VARRAY_INT (tpa->first_partition, last);
875
876 /* Update the last entry. Since it is known to only have one
877 partition, there is nothing else to update. */
878 VARRAY_TREE (tpa->trees, last) = VARRAY_TREE (tpa->trees, x);
879 VARRAY_INT (tpa->first_partition, last)
880 = VARRAY_INT (tpa->first_partition, x);
881 tpa->partition_to_tree_map[tpa_first_partition (tpa, last)] = last;
882
883 /* Since this list is known to have more than one partition, update
884 the list owner entries. */
885 VARRAY_TREE (tpa->trees, x) = swap_t;
886 VARRAY_INT (tpa->first_partition, x) = swap_i;
887 for (y = tpa_first_partition (tpa, x);
888 y != NO_PARTITION;
889 y = tpa_next_partition (tpa, y))
890 tpa->partition_to_tree_map[y] = x;
891
892 /* Ensure last is a list with more than one partition. */
893 last--;
894 for (; last > x; last--)
895 {
896 first = tpa_first_partition (tpa, last);
897 if (tpa_next_partition (tpa, first) != NO_PARTITION)
898 break;
899 }
900 }
901 x++;
902 }
903
904 first = tpa_first_partition (tpa, x);
905 if (tpa_next_partition (tpa, first) != NO_PARTITION)
906 x++;
907 tpa->uncompressed_num = tpa->num_trees;
908 tpa->num_trees = x;
909 return last;
910 }
911
912
913 /* Initialize a root_var object with SSA partitions from MAP which are based
914 on each root variable. */
915
916 root_var_p
917 root_var_init (var_map map)
918 {
919 root_var_p rv;
920 int num_partitions = num_var_partitions (map);
921 int x, p;
922 tree t;
923 var_ann_t ann;
924 sbitmap seen;
925
926 rv = tpa_init (map);
927 if (!rv)
928 return NULL;
929
930 seen = sbitmap_alloc (num_partitions);
931 sbitmap_zero (seen);
932
933 /* Start at the end and work towards the front. This will provide a list
934 that is ordered from smallest to largest. */
935 for (x = num_partitions - 1; x >= 0; x--)
936 {
937 t = partition_to_var (map, x);
938
939 /* The var map may not be compacted yet, so check for NULL. */
940 if (!t)
941 continue;
942
943 p = var_to_partition (map, t);
944
945 gcc_assert (p != NO_PARTITION);
946
947 /* Make sure we only put coalesced partitions into the list once. */
948 if (TEST_BIT (seen, p))
949 continue;
950 SET_BIT (seen, p);
951 if (TREE_CODE (t) == SSA_NAME)
952 t = SSA_NAME_VAR (t);
953 ann = var_ann (t);
954 if (ann->root_var_processed)
955 {
956 rv->next_partition[p] = VARRAY_INT (rv->first_partition,
957 VAR_ANN_ROOT_INDEX (ann));
958 VARRAY_INT (rv->first_partition, VAR_ANN_ROOT_INDEX (ann)) = p;
959 }
960 else
961 {
962 ann->root_var_processed = 1;
963 VAR_ANN_ROOT_INDEX (ann) = rv->num_trees++;
964 VARRAY_PUSH_TREE (rv->trees, t);
965 VARRAY_PUSH_INT (rv->first_partition, p);
966 }
967 rv->partition_to_tree_map[p] = VAR_ANN_ROOT_INDEX (ann);
968 }
969
970 /* Reset the out_of_ssa_tag flag on each variable for later use. */
971 for (x = 0; x < rv->num_trees; x++)
972 {
973 t = VARRAY_TREE (rv->trees, x);
974 var_ann (t)->root_var_processed = 0;
975 }
976
977 sbitmap_free (seen);
978 return rv;
979 }
980
981
982 /* Initialize a type_var structure which associates all the partitions in MAP
983 of the same type to the type node's index. Volatiles are ignored. */
984
985 type_var_p
986 type_var_init (var_map map)
987 {
988 type_var_p tv;
989 int x, y, p;
990 int num_partitions = num_var_partitions (map);
991 tree t;
992 sbitmap seen;
993
994 seen = sbitmap_alloc (num_partitions);
995 sbitmap_zero (seen);
996
997 tv = tpa_init (map);
998 if (!tv)
999 return NULL;
1000
1001 for (x = num_partitions - 1; x >= 0; x--)
1002 {
1003 t = partition_to_var (map, x);
1004
1005 /* Disallow coalescing of these types of variables. */
1006 if (!t
1007 || TREE_THIS_VOLATILE (t)
1008 || TREE_CODE (t) == RESULT_DECL
1009 || TREE_CODE (t) == PARM_DECL
1010 || (DECL_P (t)
1011 && (DECL_REGISTER (t)
1012 || !DECL_IGNORED_P (t)
1013 || DECL_RTL_SET_P (t))))
1014 continue;
1015
1016 p = var_to_partition (map, t);
1017
1018 gcc_assert (p != NO_PARTITION);
1019
1020 /* If partitions have been coalesced, only add the representative
1021 for the partition to the list once. */
1022 if (TEST_BIT (seen, p))
1023 continue;
1024 SET_BIT (seen, p);
1025 t = TREE_TYPE (t);
1026
1027 /* Find the list for this type. */
1028 for (y = 0; y < tv->num_trees; y++)
1029 if (t == VARRAY_TREE (tv->trees, y))
1030 break;
1031 if (y == tv->num_trees)
1032 {
1033 tv->num_trees++;
1034 VARRAY_PUSH_TREE (tv->trees, t);
1035 VARRAY_PUSH_INT (tv->first_partition, p);
1036 }
1037 else
1038 {
1039 tv->next_partition[p] = VARRAY_INT (tv->first_partition, y);
1040 VARRAY_INT (tv->first_partition, y) = p;
1041 }
1042 tv->partition_to_tree_map[p] = y;
1043 }
1044 sbitmap_free (seen);
1045 return tv;
1046 }
1047
1048
1049 /* Create a new coalesce list object from MAP and return it. */
1050
1051 coalesce_list_p
1052 create_coalesce_list (var_map map)
1053 {
1054 coalesce_list_p list;
1055
1056 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
1057
1058 list->map = map;
1059 list->add_mode = true;
1060 list->list = (partition_pair_p *) xcalloc (num_var_partitions (map),
1061 sizeof (struct partition_pair_d));
1062 return list;
1063 }
1064
1065
1066 /* Delete coalesce list CL. */
1067
1068 void
1069 delete_coalesce_list (coalesce_list_p cl)
1070 {
1071 free (cl->list);
1072 free (cl);
1073 }
1074
1075
1076 /* Find a matching coalesce pair object in CL for partitions P1 and P2. If
1077 one isn't found, return NULL if CREATE is false, otherwise create a new
1078 coalesce pair object and return it. */
1079
1080 static partition_pair_p
1081 find_partition_pair (coalesce_list_p cl, int p1, int p2, bool create)
1082 {
1083 partition_pair_p node, tmp;
1084 int s;
1085
1086 /* Normalize so that p1 is the smaller value. */
1087 if (p2 < p1)
1088 {
1089 s = p1;
1090 p1 = p2;
1091 p2 = s;
1092 }
1093
1094 tmp = NULL;
1095
1096 /* The list is sorted such that if we find a value greater than p2,
1097 p2 is not in the list. */
1098 for (node = cl->list[p1]; node; node = node->next)
1099 {
1100 if (node->second_partition == p2)
1101 return node;
1102 else
1103 if (node->second_partition > p2)
1104 break;
1105 tmp = node;
1106 }
1107
1108 if (!create)
1109 return NULL;
1110
1111 node = (partition_pair_p) xmalloc (sizeof (struct partition_pair_d));
1112 node->first_partition = p1;
1113 node->second_partition = p2;
1114 node->cost = 0;
1115
1116 if (tmp != NULL)
1117 {
1118 node->next = tmp->next;
1119 tmp->next = node;
1120 }
1121 else
1122 {
1123 /* This is now the first node in the list. */
1124 node->next = cl->list[p1];
1125 cl->list[p1] = node;
1126 }
1127
1128 return node;
1129 }
1130
1131
1132 /* Add a potential coalesce between P1 and P2 in CL with a cost of VALUE. */
1133
1134 void
1135 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
1136 {
1137 partition_pair_p node;
1138
1139 gcc_assert (cl->add_mode);
1140
1141 if (p1 == p2)
1142 return;
1143
1144 node = find_partition_pair (cl, p1, p2, true);
1145
1146 node->cost += value;
1147 }
1148
1149
1150 /* Comparison function to allow qsort to sort P1 and P2 in descending order. */
1151
1152 static
1153 int compare_pairs (const void *p1, const void *p2)
1154 {
1155 return (*(partition_pair_p *)p2)->cost - (*(partition_pair_p *)p1)->cost;
1156 }
1157
1158
1159 /* Prepare CL for removal of preferred pairs. When finished, list element
1160 0 has all the coalesce pairs, sorted in order from most important coalesce
1161 to least important. */
1162
1163 void
1164 sort_coalesce_list (coalesce_list_p cl)
1165 {
1166 int x, num, count;
1167 partition_pair_p chain, p;
1168 partition_pair_p *list;
1169
1170 gcc_assert (cl->add_mode);
1171
1172 cl->add_mode = false;
1173
1174 /* Compact the array of lists to a single list, and count the elements. */
1175 num = 0;
1176 chain = NULL;
1177 for (x = 0; x < num_var_partitions (cl->map); x++)
1178 if (cl->list[x] != NULL)
1179 {
1180 for (p = cl->list[x]; p->next != NULL; p = p->next)
1181 num++;
1182 num++;
1183 p->next = chain;
1184 chain = cl->list[x];
1185 cl->list[x] = NULL;
1186 }
1187
1188 /* Only call qsort if there are more than 2 items. */
1189 if (num > 2)
1190 {
1191 list = xmalloc (sizeof (partition_pair_p) * num);
1192 count = 0;
1193 for (p = chain; p != NULL; p = p->next)
1194 list[count++] = p;
1195
1196 gcc_assert (count == num);
1197
1198 qsort (list, count, sizeof (partition_pair_p), compare_pairs);
1199
1200 p = list[0];
1201 for (x = 1; x < num; x++)
1202 {
1203 p->next = list[x];
1204 p = list[x];
1205 }
1206 p->next = NULL;
1207 cl->list[0] = list[0];
1208 free (list);
1209 }
1210 else
1211 {
1212 cl->list[0] = chain;
1213 if (num == 2)
1214 {
1215 /* Simply swap the two elements if they are in the wrong order. */
1216 if (chain->cost < chain->next->cost)
1217 {
1218 cl->list[0] = chain->next;
1219 cl->list[0]->next = chain;
1220 chain->next = NULL;
1221 }
1222 }
1223 }
1224 }
1225
1226
1227 /* Retrieve the best remaining pair to coalesce from CL. Returns the 2
1228 partitions via P1 and P2. Their calculated cost is returned by the function.
1229 NO_BEST_COALESCE is returned if the coalesce list is empty. */
1230
1231 int
1232 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
1233 {
1234 partition_pair_p node;
1235 int ret;
1236
1237 gcc_assert (!cl->add_mode);
1238
1239 node = cl->list[0];
1240 if (!node)
1241 return NO_BEST_COALESCE;
1242
1243 cl->list[0] = node->next;
1244
1245 *p1 = node->first_partition;
1246 *p2 = node->second_partition;
1247 ret = node->cost;
1248 free (node);
1249
1250 return ret;
1251 }
1252
1253
1254 /* If variable VAR is in a partition in MAP, add a conflict in GRAPH between
1255 VAR and any other live partitions in VEC which are associated via TPA.
1256 Reset the live bit in VEC. */
1257
1258 static inline void
1259 add_conflicts_if_valid (tpa_p tpa, conflict_graph graph,
1260 var_map map, bitmap vec, tree var)
1261 {
1262 int p, y, first;
1263 p = var_to_partition (map, var);
1264 if (p != NO_PARTITION)
1265 {
1266 bitmap_clear_bit (vec, p);
1267 first = tpa_find_tree (tpa, p);
1268 /* If find returns nothing, this object isn't interesting. */
1269 if (first == TPA_NONE)
1270 return;
1271 /* Only add interferences between objects in the same list. */
1272 for (y = tpa_first_partition (tpa, first);
1273 y != TPA_NONE;
1274 y = tpa_next_partition (tpa, y))
1275 {
1276 if (bitmap_bit_p (vec, y))
1277 conflict_graph_add (graph, p, y);
1278 }
1279 }
1280 }
1281
1282
1283 /* Return a conflict graph for the information contained in LIVE_INFO. Only
1284 conflicts between items in the same TPA list are added. If optional
1285 coalesce list CL is passed in, any copies encountered are added. */
1286
1287 conflict_graph
1288 build_tree_conflict_graph (tree_live_info_p liveinfo, tpa_p tpa,
1289 coalesce_list_p cl)
1290 {
1291 conflict_graph graph;
1292 var_map map;
1293 bitmap live;
1294 int x, y, i;
1295 basic_block bb;
1296 varray_type partition_link, tpa_to_clear, tpa_nodes;
1297 unsigned l;
1298 ssa_op_iter iter;
1299
1300 map = live_var_map (liveinfo);
1301 graph = conflict_graph_new (num_var_partitions (map));
1302
1303 if (tpa_num_trees (tpa) == 0)
1304 return graph;
1305
1306 live = BITMAP_XMALLOC ();
1307
1308 VARRAY_INT_INIT (partition_link, num_var_partitions (map) + 1, "part_link");
1309 VARRAY_INT_INIT (tpa_nodes, tpa_num_trees (tpa), "tpa nodes");
1310 VARRAY_INT_INIT (tpa_to_clear, 50, "tpa to clear");
1311
1312 FOR_EACH_BB (bb)
1313 {
1314 block_stmt_iterator bsi;
1315 tree phi;
1316
1317 /* Start with live on exit temporaries. */
1318 bitmap_copy (live, live_on_exit (liveinfo, bb));
1319
1320 for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_prev (&bsi))
1321 {
1322 bool is_a_copy = false;
1323 tree stmt = bsi_stmt (bsi);
1324 stmt_ann_t ann;
1325
1326 get_stmt_operands (stmt);
1327 ann = stmt_ann (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,
1416 {
1417 i = tpa_find_tree (tpa, x);
1418 if (i != 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_XFREE (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 int i;
1748 var_map map = live->map;
1749
1750 if ((flag & LIVEDUMP_ENTRY) && live->livein)
1751 {
1752 FOR_EACH_BB (bb)
1753 {
1754 fprintf (f, "\nLive on entry to BB%d : ", bb->index);
1755 for (i = 0; i < num_var_partitions (map); i++)
1756 {
1757 if (bitmap_bit_p (live_entry_blocks (live, i), bb->index))
1758 {
1759 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1760 fprintf (f, " ");
1761 }
1762 }
1763 fprintf (f, "\n");
1764 }
1765 }
1766
1767 if ((flag & LIVEDUMP_EXIT) && live->liveout)
1768 {
1769 FOR_EACH_BB (bb)
1770 {
1771 fprintf (f, "\nLive on exit from BB%d : ", bb->index);
1772 EXECUTE_IF_SET_IN_BITMAP (live->liveout[bb->index], 0, i,
1773 {
1774 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1775 fprintf (f, " ");
1776 });
1777 fprintf (f, "\n");
1778 }
1779 }
1780 }
1781
1782 #ifdef ENABLE_CHECKING
1783 void
1784 register_ssa_partition_check (tree ssa_var)
1785 {
1786 gcc_assert (TREE_CODE (ssa_var) == SSA_NAME);
1787 if (!is_gimple_reg (SSA_NAME_VAR (ssa_var)))
1788 {
1789 fprintf (stderr, "Illegally registering a virtual SSA name :");
1790 print_generic_expr (stderr, ssa_var, TDF_SLIM);
1791 fprintf (stderr, " in the SSA->Normal phase.\n");
1792 internal_error ("SSA corruption");
1793 }
1794 }
1795 #endif