549211d2979e9c42321009cba6341d9892e6cb8d
[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
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 its not a user variable, set the
137 root_var to this one. */
138 if (!root_var || is_gimple_tmp_var (root_var))
139 {
140 other_var = root_var;
141 root_var = var2;
142 }
143 else
144 other_var = var2;
145 }
146
147 if (p1 == NO_PARTITION || p2 == NO_PARTITION)
148 abort ();
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 if (TREE_CODE (var) == SSA_NAME)
278 abort();
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 /* This function looks through the program and uses FLAGS to determine what
289 SSA versioned variables are given entries in a new partition table. This
290 new partition map is returned. */
291
292 var_map
293 create_ssa_var_map (int flags)
294 {
295 block_stmt_iterator bsi;
296 basic_block bb;
297 tree *dest, *use;
298 tree stmt;
299 stmt_ann_t ann;
300 vuse_optype vuses;
301 v_may_def_optype v_may_defs;
302 v_must_def_optype v_must_defs;
303 use_optype uses;
304 def_optype defs;
305 unsigned x;
306 var_map map;
307 #if defined ENABLE_CHECKING
308 sbitmap used_in_real_ops;
309 sbitmap used_in_virtual_ops;
310 #endif
311
312 map = init_var_map (num_ssa_names + 1);
313
314 #if defined ENABLE_CHECKING
315 used_in_real_ops = sbitmap_alloc (num_referenced_vars);
316 sbitmap_zero (used_in_real_ops);
317
318 used_in_virtual_ops = sbitmap_alloc (num_referenced_vars);
319 sbitmap_zero (used_in_virtual_ops);
320 #endif
321
322 if (flags & SSA_VAR_MAP_REF_COUNT)
323 {
324 map->ref_count
325 = (int *)xmalloc (((num_ssa_names + 1) * sizeof (int)));
326 memset (map->ref_count, 0, (num_ssa_names + 1) * sizeof (int));
327 }
328
329 FOR_EACH_BB (bb)
330 {
331 tree phi, arg;
332 for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
333 {
334 int i;
335 register_ssa_partition (map, PHI_RESULT (phi), false);
336 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
337 {
338 arg = PHI_ARG_DEF (phi, i);
339 if (TREE_CODE (arg) == SSA_NAME)
340 register_ssa_partition (map, arg, true);
341 }
342 }
343
344 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
345 {
346 stmt = bsi_stmt (bsi);
347 get_stmt_operands (stmt);
348 ann = stmt_ann (stmt);
349
350 /* Register USE and DEF operands in each statement. */
351 uses = USE_OPS (ann);
352 for (x = 0; x < NUM_USES (uses); x++)
353 {
354 use = USE_OP_PTR (uses, x);
355 register_ssa_partition (map, *use, true);
356
357 #if defined ENABLE_CHECKING
358 SET_BIT (used_in_real_ops, var_ann (SSA_NAME_VAR (*use))->uid);
359 #endif
360 }
361
362 defs = DEF_OPS (ann);
363 for (x = 0; x < NUM_DEFS (defs); x++)
364 {
365 dest = DEF_OP_PTR (defs, x);
366 register_ssa_partition (map, *dest, false);
367
368 #if defined ENABLE_CHECKING
369 SET_BIT (used_in_real_ops, var_ann (SSA_NAME_VAR (*dest))->uid);
370 #endif
371 }
372
373 /* While we do not care about virtual operands for
374 out of SSA, we do need to look at them to make sure
375 we mark all the variables which are used. */
376 vuses = VUSE_OPS (ann);
377 for (x = 0; x < NUM_VUSES (vuses); x++)
378 {
379 tree var = VUSE_OP (vuses, x);
380 set_is_used (var);
381
382 #if defined ENABLE_CHECKING
383 SET_BIT (used_in_virtual_ops, var_ann (SSA_NAME_VAR (var))->uid);
384 #endif
385 }
386
387 v_may_defs = V_MAY_DEF_OPS (ann);
388 for (x = 0; x < NUM_V_MAY_DEFS (v_may_defs); x++)
389 {
390 tree var = V_MAY_DEF_OP (v_may_defs, x);
391 set_is_used (var);
392
393 #if defined ENABLE_CHECKING
394 SET_BIT (used_in_virtual_ops, var_ann (SSA_NAME_VAR (var))->uid);
395 #endif
396 }
397
398 v_must_defs = V_MUST_DEF_OPS (ann);
399 for (x = 0; x < NUM_V_MUST_DEFS (v_must_defs); x++)
400 {
401 tree var = V_MUST_DEF_OP (v_must_defs, x);
402 set_is_used (var);
403 #if defined ENABLE_CHECKING
404 SET_BIT (used_in_virtual_ops, var_ann (SSA_NAME_VAR (var))->uid);
405 #endif
406 }
407 }
408 }
409
410 #if defined ENABLE_CHECKING
411 {
412 unsigned i;
413 sbitmap both = sbitmap_alloc (num_referenced_vars);
414 sbitmap_a_and_b (both, used_in_real_ops, used_in_virtual_ops);
415 if (sbitmap_first_set_bit (both) >= 0)
416 {
417 EXECUTE_IF_SET_IN_SBITMAP (both, 0, i,
418 fprintf (stderr, "Variable %s used in real and virtual operands\n",
419 get_name (referenced_var (i))));
420 abort ();
421 }
422
423 sbitmap_free (used_in_real_ops);
424 sbitmap_free (used_in_virtual_ops);
425 sbitmap_free (both);
426 }
427 #endif
428
429 return map;
430 }
431
432
433 /* Allocate and return a new live range information object base on MAP. */
434
435 static tree_live_info_p
436 new_tree_live_info (var_map map)
437 {
438 tree_live_info_p live;
439 int x;
440
441 live = (tree_live_info_p) xmalloc (sizeof (struct tree_live_info_d));
442 live->map = map;
443 live->num_blocks = last_basic_block;
444
445 live->global = BITMAP_XMALLOC ();
446
447 live->livein = (bitmap *)xmalloc (num_var_partitions (map) * sizeof (bitmap));
448 for (x = 0; x < num_var_partitions (map); x++)
449 live->livein[x] = BITMAP_XMALLOC ();
450
451 /* liveout is deferred until it is actually requested. */
452 live->liveout = NULL;
453 return live;
454 }
455
456
457 /* Free storage for live range info object LIVE. */
458
459 void
460 delete_tree_live_info (tree_live_info_p live)
461 {
462 int x;
463 if (live->liveout)
464 {
465 for (x = live->num_blocks - 1; x >= 0; x--)
466 BITMAP_XFREE (live->liveout[x]);
467 free (live->liveout);
468 }
469 if (live->livein)
470 {
471 for (x = num_var_partitions (live->map) - 1; x >= 0; x--)
472 BITMAP_XFREE (live->livein[x]);
473 free (live->livein);
474 }
475 if (live->global)
476 BITMAP_XFREE (live->global);
477
478 free (live);
479 }
480
481
482 /* Using LIVE, fill in all the live-on-entry blocks between the defs and uses
483 for partition I. STACK is a varray used for temporary memory which is
484 passed in rather than being allocated on every call. */
485
486 static void
487 live_worklist (tree_live_info_p live, varray_type stack, int i)
488 {
489 int b;
490 tree var;
491 basic_block def_bb = NULL;
492 edge e;
493 var_map map = live->map;
494
495 var = partition_to_var (map, i);
496 if (SSA_NAME_DEF_STMT (var))
497 def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (var));
498
499 EXECUTE_IF_SET_IN_BITMAP (live->livein[i], 0, b,
500 {
501 VARRAY_PUSH_INT (stack, b);
502 });
503
504 while (VARRAY_ACTIVE_SIZE (stack) > 0)
505 {
506 b = VARRAY_TOP_INT (stack);
507 VARRAY_POP (stack);
508
509 for (e = BASIC_BLOCK (b)->pred; e; e = e->pred_next)
510 if (e->src != ENTRY_BLOCK_PTR)
511 {
512 /* Its not live on entry to the block its defined in. */
513 if (e->src == def_bb)
514 continue;
515 if (!bitmap_bit_p (live->livein[i], e->src->index))
516 {
517 bitmap_set_bit (live->livein[i], e->src->index);
518 VARRAY_PUSH_INT (stack, e->src->index);
519 }
520 }
521 }
522 }
523
524
525 /* If VAR is in a partition of MAP, set the bit for that partition in VEC. */
526
527 static inline void
528 set_if_valid (var_map map, bitmap vec, tree var)
529 {
530 int p = var_to_partition (map, var);
531 if (p != NO_PARTITION)
532 bitmap_set_bit (vec, p);
533 }
534
535
536 /* If VAR is in a partition and it isn't defined in DEF_VEC, set the livein and
537 global bit for it in the LIVE object. BB is the block being processed. */
538
539 static inline void
540 add_livein_if_notdef (tree_live_info_p live, bitmap def_vec,
541 tree var, basic_block bb)
542 {
543 int p = var_to_partition (live->map, var);
544 if (p == NO_PARTITION || bb == ENTRY_BLOCK_PTR)
545 return;
546 if (!bitmap_bit_p (def_vec, p))
547 {
548 bitmap_set_bit (live->livein[p], bb->index);
549 bitmap_set_bit (live->global, p);
550 }
551 }
552
553
554 /* Given partition map MAP, calculate all the live on entry bitmaps for
555 each basic block. Return a live info object. */
556
557 tree_live_info_p
558 calculate_live_on_entry (var_map map)
559 {
560 tree_live_info_p live;
561 int num, i;
562 basic_block bb;
563 bitmap saw_def;
564 tree phi, var, stmt;
565 tree op;
566 edge e;
567 varray_type stack;
568 block_stmt_iterator bsi;
569 use_optype uses;
570 def_optype defs;
571 stmt_ann_t ann;
572
573 saw_def = BITMAP_XMALLOC ();
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 = TREE_CHAIN (phi))
582 {
583 for (i = 0; i < 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 = PHI_ARG_EDGE (phi, 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 = TREE_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 ann = stmt_ann (stmt);
617
618 uses = USE_OPS (ann);
619 num = NUM_USES (uses);
620 for (i = 0; i < num; i++)
621 {
622 op = USE_OP (uses, i);
623 add_livein_if_notdef (live, saw_def, op, bb);
624 }
625
626 defs = DEF_OPS (ann);
627 num = NUM_DEFS (defs);
628 for (i = 0; i < num; i++)
629 {
630 op = DEF_OP (defs, i);
631 set_if_valid (map, saw_def, op);
632 }
633 }
634 }
635
636 VARRAY_INT_INIT (stack, last_basic_block, "stack");
637 EXECUTE_IF_SET_IN_BITMAP (live->global, 0, i,
638 {
639 live_worklist (live, stack, i);
640 });
641
642 #ifdef ENABLE_CHECKING
643 /* Check for live on entry partitions and report those with a DEF in
644 the program. This will typically mean an optimization has done
645 something wrong. */
646
647 bb = ENTRY_BLOCK_PTR;
648 num = 0;
649 for (e = bb->succ; e; e = e->succ_next)
650 {
651 int entry_block = e->dest->index;
652 if (e->dest == EXIT_BLOCK_PTR)
653 continue;
654 for (i = 0; i < num_var_partitions (map); i++)
655 {
656 basic_block tmp;
657 tree d;
658 var = partition_to_var (map, i);
659 stmt = SSA_NAME_DEF_STMT (var);
660 tmp = bb_for_stmt (stmt);
661 d = default_def (SSA_NAME_VAR (var));
662
663 if (bitmap_bit_p (live_entry_blocks (live, i), entry_block))
664 {
665 if (!IS_EMPTY_STMT (stmt))
666 {
667 num++;
668 print_generic_expr (stderr, var, TDF_SLIM);
669 fprintf (stderr, " is defined ");
670 if (tmp)
671 fprintf (stderr, " in BB%d, ", tmp->index);
672 fprintf (stderr, "by:\n");
673 print_generic_expr (stderr, stmt, TDF_SLIM);
674 fprintf (stderr, "\nIt is also live-on-entry to entry BB %d",
675 entry_block);
676 fprintf (stderr, " So it appears to have multiple defs.\n");
677 }
678 else
679 {
680 if (d != var)
681 {
682 num++;
683 print_generic_expr (stderr, var, TDF_SLIM);
684 fprintf (stderr, " is live-on-entry to BB%d ",entry_block);
685 if (d)
686 {
687 fprintf (stderr, " but is not the default def of ");
688 print_generic_expr (stderr, d, TDF_SLIM);
689 fprintf (stderr, "\n");
690 }
691 else
692 fprintf (stderr, " and there is no default def.\n");
693 }
694 }
695 }
696 else
697 if (d == var)
698 {
699 /* The only way this var shouldn't be marked live on entry is
700 if it occurs in a PHI argument of the block. */
701 int z, ok = 0;
702 for (phi = phi_nodes (e->dest);
703 phi && !ok;
704 phi = TREE_CHAIN (phi))
705 {
706 for (z = 0; z < PHI_NUM_ARGS (phi); z++)
707 if (var == PHI_ARG_DEF (phi, z))
708 {
709 ok = 1;
710 break;
711 }
712 }
713 if (ok)
714 continue;
715 num++;
716 print_generic_expr (stderr, var, TDF_SLIM);
717 fprintf (stderr, " is not marked live-on-entry to entry BB%d ",
718 entry_block);
719 fprintf (stderr, "but it is a default def so it should be.\n");
720 }
721 }
722 }
723 if (num > 0)
724 abort ();
725 #endif
726
727 BITMAP_XFREE (saw_def);
728
729 return live;
730 }
731
732
733 /* Calculate the live on exit vectors based on the entry info in LIVEINFO. */
734
735 void
736 calculate_live_on_exit (tree_live_info_p liveinfo)
737 {
738 unsigned b;
739 int i, x;
740 bitmap *on_exit;
741 basic_block bb;
742 edge e;
743 tree t, phi;
744 bitmap on_entry;
745 var_map map = liveinfo->map;
746
747 on_exit = (bitmap *)xmalloc (last_basic_block * sizeof (bitmap));
748 for (x = 0; x < last_basic_block; x++)
749 on_exit[x] = BITMAP_XMALLOC ();
750
751 /* Set all the live-on-exit bits for uses in PHIs. */
752 FOR_EACH_BB (bb)
753 {
754 for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
755 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
756 {
757 t = PHI_ARG_DEF (phi, i);
758 e = PHI_ARG_EDGE (phi, i);
759 if (!phi_ssa_name_p (t) || e->src == ENTRY_BLOCK_PTR)
760 continue;
761 set_if_valid (map, on_exit[e->src->index], t);
762 }
763 }
764
765 /* Set live on exit for all predecessors of live on entry's. */
766 for (i = 0; i < num_var_partitions (map); i++)
767 {
768 on_entry = live_entry_blocks (liveinfo, i);
769 EXECUTE_IF_SET_IN_BITMAP (on_entry, 0, b,
770 {
771 for (e = BASIC_BLOCK(b)->pred; e; e = e->pred_next)
772 if (e->src != ENTRY_BLOCK_PTR)
773 bitmap_set_bit (on_exit[e->src->index], i);
774 });
775 }
776
777 liveinfo->liveout = on_exit;
778 }
779
780
781 /* Initialize a tree_partition_associator object using MAP. */
782
783 tpa_p
784 tpa_init (var_map map)
785 {
786 tpa_p tpa;
787 int num_partitions = num_var_partitions (map);
788 int x;
789
790 if (num_partitions == 0)
791 return NULL;
792
793 tpa = (tpa_p) xmalloc (sizeof (struct tree_partition_associator_d));
794 tpa->num_trees = 0;
795 tpa->uncompressed_num = -1;
796 tpa->map = map;
797 tpa->next_partition = (int *)xmalloc (num_partitions * sizeof (int));
798 memset (tpa->next_partition, TPA_NONE, num_partitions * sizeof (int));
799
800 tpa->partition_to_tree_map = (int *)xmalloc (num_partitions * sizeof (int));
801 memset (tpa->partition_to_tree_map, TPA_NONE, num_partitions * sizeof (int));
802
803 x = MAX (40, (num_partitions / 20));
804 VARRAY_TREE_INIT (tpa->trees, x, "trees");
805 VARRAY_INT_INIT (tpa->first_partition, x, "first_partition");
806
807 return tpa;
808
809 }
810
811
812 /* Remove PARTITION_INDEX from TREE_INDEX's list in the tpa structure TPA. */
813
814 void
815 tpa_remove_partition (tpa_p tpa, int tree_index, int partition_index)
816 {
817 int i;
818
819 i = tpa_first_partition (tpa, tree_index);
820 if (i == partition_index)
821 {
822 VARRAY_INT (tpa->first_partition, tree_index) = tpa->next_partition[i];
823 }
824 else
825 {
826 for ( ; i != TPA_NONE; i = tpa_next_partition (tpa, i))
827 {
828 if (tpa->next_partition[i] == partition_index)
829 {
830 tpa->next_partition[i] = tpa->next_partition[partition_index];
831 break;
832 }
833 }
834 }
835 }
836
837
838 /* Free the memory used by tree_partition_associator object TPA. */
839
840 void
841 tpa_delete (tpa_p tpa)
842 {
843 if (!tpa)
844 return;
845
846 free (tpa->partition_to_tree_map);
847 free (tpa->next_partition);
848 free (tpa);
849 }
850
851
852 /* This function will remove any tree entries from TPA which have only a single
853 element. This will help keep the size of the conflict graph down. The
854 function returns the number of remaining tree lists. */
855
856 int
857 tpa_compact (tpa_p tpa)
858 {
859 int last, x, y, first, swap_i;
860 tree swap_t;
861
862 /* Find the last list which has more than 1 partition. */
863 for (last = tpa->num_trees - 1; last > 0; last--)
864 {
865 first = tpa_first_partition (tpa, last);
866 if (tpa_next_partition (tpa, first) != NO_PARTITION)
867 break;
868 }
869
870 x = 0;
871 while (x < last)
872 {
873 first = tpa_first_partition (tpa, x);
874
875 /* If there is not more than one partition, swap with the current end
876 of the tree list. */
877 if (tpa_next_partition (tpa, first) == NO_PARTITION)
878 {
879 swap_t = VARRAY_TREE (tpa->trees, last);
880 swap_i = VARRAY_INT (tpa->first_partition, last);
881
882 /* Update the last entry. Since it is known to only have one
883 partition, there is nothing else to update. */
884 VARRAY_TREE (tpa->trees, last) = VARRAY_TREE (tpa->trees, x);
885 VARRAY_INT (tpa->first_partition, last)
886 = VARRAY_INT (tpa->first_partition, x);
887 tpa->partition_to_tree_map[tpa_first_partition (tpa, last)] = last;
888
889 /* Since this list is known to have more than one partition, update
890 the list owner entries. */
891 VARRAY_TREE (tpa->trees, x) = swap_t;
892 VARRAY_INT (tpa->first_partition, x) = swap_i;
893 for (y = tpa_first_partition (tpa, x);
894 y != NO_PARTITION;
895 y = tpa_next_partition (tpa, y))
896 tpa->partition_to_tree_map[y] = x;
897
898 /* Ensure last is a list with more than one partition. */
899 last--;
900 for (; last > x; last--)
901 {
902 first = tpa_first_partition (tpa, last);
903 if (tpa_next_partition (tpa, first) != NO_PARTITION)
904 break;
905 }
906 }
907 x++;
908 }
909
910 first = tpa_first_partition (tpa, x);
911 if (tpa_next_partition (tpa, first) != NO_PARTITION)
912 x++;
913 tpa->uncompressed_num = tpa->num_trees;
914 tpa->num_trees = x;
915 return last;
916 }
917
918
919 /* Initialize a root_var object with SSA partitions from MAP which are based
920 on each root variable. */
921
922 root_var_p
923 root_var_init (var_map map)
924 {
925 root_var_p rv;
926 int num_partitions = num_var_partitions (map);
927 int x, p;
928 tree t;
929 var_ann_t ann;
930 sbitmap seen;
931
932 rv = tpa_init (map);
933 if (!rv)
934 return NULL;
935
936 seen = sbitmap_alloc (num_partitions);
937 sbitmap_zero (seen);
938
939 /* Start at the end and work towards the front. This will provide a list
940 that is ordered from smallest to largest. */
941 for (x = num_partitions - 1; x >= 0; x--)
942 {
943 t = partition_to_var (map, x);
944
945 /* The var map may not be compacted yet, so check for NULL. */
946 if (!t)
947 continue;
948
949 p = var_to_partition (map, t);
950
951 #ifdef ENABLE_CHECKING
952 if (p == NO_PARTITION)
953 abort ();
954 #endif
955
956 /* Make sure we only put coalesced partitions into the list once. */
957 if (TEST_BIT (seen, p))
958 continue;
959 SET_BIT (seen, p);
960 if (TREE_CODE (t) == SSA_NAME)
961 t = SSA_NAME_VAR (t);
962 ann = var_ann (t);
963 if (ann->root_var_processed)
964 {
965 rv->next_partition[p] = VARRAY_INT (rv->first_partition,
966 VAR_ANN_ROOT_INDEX (ann));
967 VARRAY_INT (rv->first_partition, VAR_ANN_ROOT_INDEX (ann)) = p;
968 }
969 else
970 {
971 ann->root_var_processed = 1;
972 VAR_ANN_ROOT_INDEX (ann) = rv->num_trees++;
973 VARRAY_PUSH_TREE (rv->trees, t);
974 VARRAY_PUSH_INT (rv->first_partition, p);
975 }
976 rv->partition_to_tree_map[p] = VAR_ANN_ROOT_INDEX (ann);
977 }
978
979 /* Reset the out_of_ssa_tag flag on each variable for later use. */
980 for (x = 0; x < rv->num_trees; x++)
981 {
982 t = VARRAY_TREE (rv->trees, x);
983 var_ann (t)->root_var_processed = 0;
984 }
985
986 sbitmap_free (seen);
987 return rv;
988 }
989
990
991 /* Initialize a type_var structure which associates all the partitions in MAP
992 of the same type to the type node's index. Volatiles are ignored. */
993
994 type_var_p
995 type_var_init (var_map map)
996 {
997 type_var_p tv;
998 int x, y, p;
999 int num_partitions = num_var_partitions (map);
1000 tree t;
1001 sbitmap seen;
1002
1003 seen = sbitmap_alloc (num_partitions);
1004 sbitmap_zero (seen);
1005
1006 tv = tpa_init (map);
1007 if (!tv)
1008 return NULL;
1009
1010 for (x = num_partitions - 1; x >= 0; x--)
1011 {
1012 t = partition_to_var (map, x);
1013
1014 /* Disallow coalescing of these types of variables. */
1015 if (!t
1016 || TREE_THIS_VOLATILE (t)
1017 || TREE_CODE (t) == RESULT_DECL
1018 || TREE_CODE (t) == PARM_DECL
1019 || (DECL_P (t)
1020 && (DECL_REGISTER (t)
1021 || !DECL_ARTIFICIAL (t)
1022 || DECL_RTL_SET_P (t))))
1023 continue;
1024
1025 p = var_to_partition (map, t);
1026
1027 #ifdef ENABLE_CHECKING
1028 if (p == NO_PARTITION)
1029 abort ();
1030 #endif
1031
1032 /* If partitions have been coalesced, only add the representative
1033 for the partition to the list once. */
1034 if (TEST_BIT (seen, p))
1035 continue;
1036 SET_BIT (seen, p);
1037 t = TREE_TYPE (t);
1038
1039 /* Find the list for this type. */
1040 for (y = 0; y < tv->num_trees; y++)
1041 if (t == VARRAY_TREE (tv->trees, y))
1042 break;
1043 if (y == tv->num_trees)
1044 {
1045 tv->num_trees++;
1046 VARRAY_PUSH_TREE (tv->trees, t);
1047 VARRAY_PUSH_INT (tv->first_partition, p);
1048 }
1049 else
1050 {
1051 tv->next_partition[p] = VARRAY_INT (tv->first_partition, y);
1052 VARRAY_INT (tv->first_partition, y) = p;
1053 }
1054 tv->partition_to_tree_map[p] = y;
1055 }
1056 sbitmap_free (seen);
1057 return tv;
1058 }
1059
1060
1061 /* Create a new coalesce list object from MAP and return it. */
1062
1063 coalesce_list_p
1064 create_coalesce_list (var_map map)
1065 {
1066 coalesce_list_p list;
1067
1068 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
1069
1070 list->map = map;
1071 list->add_mode = true;
1072 list->list = (partition_pair_p *) xcalloc (num_var_partitions (map),
1073 sizeof (struct partition_pair_d));
1074 return list;
1075 }
1076
1077
1078 /* Delete coalesce list CL. */
1079
1080 void
1081 delete_coalesce_list (coalesce_list_p cl)
1082 {
1083 free (cl->list);
1084 free (cl);
1085 }
1086
1087
1088 /* Find a matching coalesce pair object in CL for partitions P1 and P2. If
1089 one isn't found, return NULL if CREATE is false, otherwise create a new
1090 coalesce pair object and return it. */
1091
1092 static partition_pair_p
1093 find_partition_pair (coalesce_list_p cl, int p1, int p2, bool create)
1094 {
1095 partition_pair_p node, tmp;
1096 int s;
1097
1098 /* Normalize so that p1 is the smaller value. */
1099 if (p2 < p1)
1100 {
1101 s = p1;
1102 p1 = p2;
1103 p2 = s;
1104 }
1105
1106 tmp = NULL;
1107
1108 /* The list is sorted such that if we find a value greater than p2,
1109 p2 is not in the list. */
1110 for (node = cl->list[p1]; node; node = node->next)
1111 {
1112 if (node->second_partition == p2)
1113 return node;
1114 else
1115 if (node->second_partition > p2)
1116 break;
1117 tmp = node;
1118 }
1119
1120 if (!create)
1121 return NULL;
1122
1123 node = (partition_pair_p) xmalloc (sizeof (struct partition_pair_d));
1124 node->first_partition = p1;
1125 node->second_partition = p2;
1126 node->cost = 0;
1127
1128 if (tmp != NULL)
1129 {
1130 node->next = tmp->next;
1131 tmp->next = node;
1132 }
1133 else
1134 {
1135 /* This is now the first node in the list. */
1136 node->next = cl->list[p1];
1137 cl->list[p1] = node;
1138 }
1139
1140 return node;
1141 }
1142
1143
1144 /* Add a potential coalesce between P1 and P2 in CL with a cost of VALUE. */
1145
1146 void
1147 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
1148 {
1149 partition_pair_p node;
1150
1151 #ifdef ENABLE_CHECKING
1152 if (!cl->add_mode)
1153 abort();
1154 #endif
1155
1156 if (p1 == p2)
1157 return;
1158
1159 node = find_partition_pair (cl, p1, p2, true);
1160
1161 node->cost += value;
1162 }
1163
1164
1165 /* Comparison function to allow qsort to sort P1 and P2 in descending order. */
1166
1167 static
1168 int compare_pairs (const void *p1, const void *p2)
1169 {
1170 return (*(partition_pair_p *)p2)->cost - (*(partition_pair_p *)p1)->cost;
1171 }
1172
1173
1174 /* Prepare CL for removal of preferred pairs. When finished, list element
1175 0 has all the coalesce pairs, sorted in order from most important coalesce
1176 to least important. */
1177
1178 void
1179 sort_coalesce_list (coalesce_list_p cl)
1180 {
1181 int x, num, count;
1182 partition_pair_p chain, p;
1183 partition_pair_p *list;
1184
1185 if (!cl->add_mode)
1186 abort();
1187
1188 cl->add_mode = false;
1189
1190 /* Compact the array of lists to a single list, and count the elements. */
1191 num = 0;
1192 chain = NULL;
1193 for (x = 0; x < num_var_partitions (cl->map); x++)
1194 if (cl->list[x] != NULL)
1195 {
1196 for (p = cl->list[x]; p->next != NULL; p = p->next)
1197 num++;
1198 num++;
1199 p->next = chain;
1200 chain = cl->list[x];
1201 cl->list[x] = NULL;
1202 }
1203
1204 /* Only call qsort if there are more than 2 items. */
1205 if (num > 2)
1206 {
1207 list = xmalloc (sizeof (partition_pair_p) * num);
1208 count = 0;
1209 for (p = chain; p != NULL; p = p->next)
1210 list[count++] = p;
1211
1212 #ifdef ENABLE_CHECKING
1213 if (count != num)
1214 abort ();
1215 #endif
1216
1217 qsort (list, count, sizeof (partition_pair_p), compare_pairs);
1218
1219 p = list[0];
1220 for (x = 1; x < num; x++)
1221 {
1222 p->next = list[x];
1223 p = list[x];
1224 }
1225 p->next = NULL;
1226 cl->list[0] = list[0];
1227 free (list);
1228 }
1229 else
1230 {
1231 cl->list[0] = chain;
1232 if (num == 2)
1233 {
1234 /* Simply swap the two elements if they are in the wrong order. */
1235 if (chain->cost < chain->next->cost)
1236 {
1237 cl->list[0] = chain->next;
1238 cl->list[0]->next = chain;
1239 chain->next = NULL;
1240 }
1241 }
1242 }
1243 }
1244
1245
1246 /* Retrieve the best remaining pair to coalesce from CL. Returns the 2
1247 partitions via P1 and P2. Their calculated cost is returned by the function.
1248 NO_BEST_COALESCE is returned if the coalesce list is empty. */
1249
1250 int
1251 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
1252 {
1253 partition_pair_p node;
1254 int ret;
1255
1256 if (cl->add_mode)
1257 abort();
1258
1259 node = cl->list[0];
1260 if (!node)
1261 return NO_BEST_COALESCE;
1262
1263 cl->list[0] = node->next;
1264
1265 *p1 = node->first_partition;
1266 *p2 = node->second_partition;
1267 ret = node->cost;
1268 free (node);
1269
1270 return ret;
1271 }
1272
1273
1274 /* If variable VAR is in a partition in MAP, add a conflict in GRAPH between
1275 VAR and any other live partitions in VEC which are associated via TPA.
1276 Reset the live bit in VEC. */
1277
1278 static inline void
1279 add_conflicts_if_valid (tpa_p tpa, conflict_graph graph,
1280 var_map map, bitmap vec, tree var)
1281 {
1282 int p, y, first;
1283 p = var_to_partition (map, var);
1284 if (p != NO_PARTITION)
1285 {
1286 bitmap_clear_bit (vec, p);
1287 first = tpa_find_tree (tpa, p);
1288 /* If find returns nothing, this object isn't interesting. */
1289 if (first == TPA_NONE)
1290 return;
1291 /* Only add interferences between objects in the same list. */
1292 for (y = tpa_first_partition (tpa, first);
1293 y != TPA_NONE;
1294 y = tpa_next_partition (tpa, y))
1295 {
1296 if (bitmap_bit_p (vec, y))
1297 conflict_graph_add (graph, p, y);
1298 }
1299 }
1300 }
1301
1302
1303 /* Return a conflict graph for the information contained in LIVE_INFO. Only
1304 conflicts between items in the same TPA list are added. If optional
1305 coalesce list CL is passed in, any copies encountered are added. */
1306
1307 conflict_graph
1308 build_tree_conflict_graph (tree_live_info_p liveinfo, tpa_p tpa,
1309 coalesce_list_p cl)
1310 {
1311 conflict_graph graph;
1312 var_map map;
1313 bitmap live;
1314 int num, x, y, i;
1315 basic_block bb;
1316 varray_type partition_link, tpa_to_clear, tpa_nodes;
1317 def_optype defs;
1318 use_optype uses;
1319 unsigned l;
1320
1321 map = live_var_map (liveinfo);
1322 graph = conflict_graph_new (num_var_partitions (map));
1323
1324 if (tpa_num_trees (tpa) == 0)
1325 return graph;
1326
1327 live = BITMAP_XMALLOC ();
1328
1329 VARRAY_INT_INIT (partition_link, num_var_partitions (map) + 1, "part_link");
1330 VARRAY_INT_INIT (tpa_nodes, tpa_num_trees (tpa), "tpa nodes");
1331 VARRAY_INT_INIT (tpa_to_clear, 50, "tpa to clear");
1332
1333 FOR_EACH_BB (bb)
1334 {
1335 block_stmt_iterator bsi;
1336 tree phi;
1337
1338 /* Start with live on exit temporaries. */
1339 bitmap_copy (live, live_on_exit (liveinfo, bb));
1340
1341 for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_prev (&bsi))
1342 {
1343 bool is_a_copy = false;
1344 tree stmt = bsi_stmt (bsi);
1345 stmt_ann_t ann;
1346
1347 get_stmt_operands (stmt);
1348 ann = stmt_ann (stmt);
1349
1350 /* A copy between 2 partitions does not introduce an interference
1351 by itself. If they did, you would never be able to coalesce
1352 two things which are copied. If the two variables really do
1353 conflict, they will conflict elsewhere in the program.
1354
1355 This is handled specially here since we may also be interested
1356 in copies between real variables and SSA_NAME variables. We may
1357 be interested in trying to coalesce SSA_NAME variables with
1358 root variables in some cases. */
1359
1360 if (TREE_CODE (stmt) == MODIFY_EXPR)
1361 {
1362 tree lhs = TREE_OPERAND (stmt, 0);
1363 tree rhs = TREE_OPERAND (stmt, 1);
1364 int p1, p2;
1365 int bit;
1366
1367 if (DECL_P (lhs) || TREE_CODE (lhs) == SSA_NAME)
1368 p1 = var_to_partition (map, lhs);
1369 else
1370 p1 = NO_PARTITION;
1371
1372 if (DECL_P (rhs) || TREE_CODE (rhs) == SSA_NAME)
1373 p2 = var_to_partition (map, rhs);
1374 else
1375 p2 = NO_PARTITION;
1376
1377 if (p1 != NO_PARTITION && p2 != NO_PARTITION)
1378 {
1379 is_a_copy = true;
1380 bit = bitmap_bit_p (live, p2);
1381 /* If the RHS is live, make it not live while we add
1382 the conflicts, then make it live again. */
1383 if (bit)
1384 bitmap_clear_bit (live, p2);
1385 add_conflicts_if_valid (tpa, graph, map, live, lhs);
1386 if (bit)
1387 bitmap_set_bit (live, p2);
1388 if (cl)
1389 add_coalesce (cl, p1, p2, 1);
1390 set_if_valid (map, live, rhs);
1391 }
1392 }
1393
1394 if (!is_a_copy)
1395 {
1396 tree *var_p;
1397
1398 defs = DEF_OPS (ann);
1399 num = NUM_DEFS (defs);
1400 for (x = 0; x < num; x++)
1401 {
1402 var_p = DEF_OP_PTR (defs, x);
1403 add_conflicts_if_valid (tpa, graph, map, live, *var_p);
1404 }
1405
1406 uses = USE_OPS (ann);
1407 num = NUM_USES (uses);
1408 for (x = 0; x < num; x++)
1409 {
1410 var_p = USE_OP_PTR (uses, x);
1411 set_if_valid (map, live, *var_p);
1412 }
1413 }
1414 }
1415
1416 /* If result of a PHI is unused, then the loops over the statements
1417 will not record any conflicts. However, since the PHI node is
1418 going to be translated out of SSA form we must record a conflict
1419 between the result of the PHI and any variables with are live.
1420 Otherwise the out-of-ssa translation may create incorrect code. */
1421 for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
1422 {
1423 tree result = PHI_RESULT (phi);
1424 int p = var_to_partition (map, result);
1425
1426 if (p != NO_PARTITION && ! bitmap_bit_p (live, p))
1427 add_conflicts_if_valid (tpa, graph, map, live, result);
1428 }
1429
1430 /* Anything which is still live at this point interferes.
1431 In order to implement this efficiently, only conflicts between
1432 partitions which have the same TPA root need be added.
1433 TPA roots which have been seen are tracked in 'tpa_nodes'. A nonzero
1434 entry points to an index into 'partition_link', which then indexes
1435 into itself forming a linked list of partitions sharing a tpa root
1436 which have been seen as live up to this point. Since partitions start
1437 at index zero, all entries in partition_link are (partition + 1).
1438
1439 Conflicts are added between the current partition and any already seen.
1440 tpa_clear contains all the tpa_roots processed, and these are the only
1441 entries which need to be zero'd out for a clean restart. */
1442
1443 EXECUTE_IF_SET_IN_BITMAP (live, 0, x,
1444 {
1445 i = tpa_find_tree (tpa, x);
1446 if (i != TPA_NONE)
1447 {
1448 int start = VARRAY_INT (tpa_nodes, i);
1449 /* If start is 0, a new root reference list is being started.
1450 Register it to be cleared. */
1451 if (!start)
1452 VARRAY_PUSH_INT (tpa_to_clear, i);
1453
1454 /* Add interferences to other tpa members seen. */
1455 for (y = start; y != 0; y = VARRAY_INT (partition_link, y))
1456 conflict_graph_add (graph, x, y - 1);
1457 VARRAY_INT (tpa_nodes, i) = x + 1;
1458 VARRAY_INT (partition_link, x + 1) = start;
1459 }
1460 });
1461
1462 /* Now clear the used tpa root references. */
1463 for (l = 0; l < VARRAY_ACTIVE_SIZE (tpa_to_clear); l++)
1464 VARRAY_INT (tpa_nodes, VARRAY_INT (tpa_to_clear, l)) = 0;
1465 VARRAY_POP_ALL (tpa_to_clear);
1466 }
1467
1468 BITMAP_XFREE (live);
1469 return graph;
1470 }
1471
1472
1473 /* This routine will attempt to coalesce the elements in TPA subject to the
1474 conflicts found in GRAPH. If optional coalesce_list CL is provided,
1475 only coalesces specified within the coalesce list are attempted. Otherwise
1476 an attempt is made to coalesce as many partitions within each TPA grouping
1477 as possible. If DEBUG is provided, debug output will be sent there. */
1478
1479 void
1480 coalesce_tpa_members (tpa_p tpa, conflict_graph graph, var_map map,
1481 coalesce_list_p cl, FILE *debug)
1482 {
1483 int x, y, z, w;
1484 tree var, tmp;
1485
1486 /* Attempt to coalesce any items in a coalesce list. */
1487 if (cl)
1488 {
1489 while (pop_best_coalesce (cl, &x, &y) != NO_BEST_COALESCE)
1490 {
1491 if (debug)
1492 {
1493 fprintf (debug, "Coalesce list: (%d)", x);
1494 print_generic_expr (debug, partition_to_var (map, x), TDF_SLIM);
1495 fprintf (debug, " & (%d)", y);
1496 print_generic_expr (debug, partition_to_var (map, y), TDF_SLIM);
1497 }
1498
1499 w = tpa_find_tree (tpa, x);
1500 z = tpa_find_tree (tpa, y);
1501 if (w != z || w == TPA_NONE || z == TPA_NONE)
1502 {
1503 if (debug)
1504 {
1505 if (w != z)
1506 fprintf (debug, ": Fail, Non-matching TPA's\n");
1507 if (w == TPA_NONE)
1508 fprintf (debug, ": Fail %d non TPA.\n", x);
1509 else
1510 fprintf (debug, ": Fail %d non TPA.\n", y);
1511 }
1512 continue;
1513 }
1514 var = partition_to_var (map, x);
1515 tmp = partition_to_var (map, y);
1516 x = var_to_partition (map, var);
1517 y = var_to_partition (map, tmp);
1518 if (debug)
1519 fprintf (debug, " [map: %d, %d] ", x, y);
1520 if (x == y)
1521 {
1522 if (debug)
1523 fprintf (debug, ": Already Coalesced.\n");
1524 continue;
1525 }
1526 if (!conflict_graph_conflict_p (graph, x, y))
1527 {
1528 z = var_union (map, var, tmp);
1529 if (z == NO_PARTITION)
1530 {
1531 if (debug)
1532 fprintf (debug, ": Unable to perform partition union.\n");
1533 continue;
1534 }
1535
1536 /* z is the new combined partition. We need to remove the other
1537 partition from the list. Set x to be that other partition. */
1538 if (z == x)
1539 {
1540 conflict_graph_merge_regs (graph, x, y);
1541 w = tpa_find_tree (tpa, y);
1542 tpa_remove_partition (tpa, w, y);
1543 }
1544 else
1545 {
1546 conflict_graph_merge_regs (graph, y, x);
1547 w = tpa_find_tree (tpa, x);
1548 tpa_remove_partition (tpa, w, x);
1549 }
1550
1551 if (debug)
1552 fprintf (debug, ": Success -> %d\n", z);
1553 }
1554 else
1555 if (debug)
1556 fprintf (debug, ": Fail due to conflict\n");
1557 }
1558 /* If using a coalesce list, don't try to coalesce anything else. */
1559 return;
1560 }
1561
1562 for (x = 0; x < tpa_num_trees (tpa); x++)
1563 {
1564 while (tpa_first_partition (tpa, x) != TPA_NONE)
1565 {
1566 int p1, p2;
1567 /* Coalesce first partition with anything that doesn't conflict. */
1568 y = tpa_first_partition (tpa, x);
1569 tpa_remove_partition (tpa, x, y);
1570
1571 var = partition_to_var (map, y);
1572 /* p1 is the partition representative to which y belongs. */
1573 p1 = var_to_partition (map, var);
1574
1575 for (z = tpa_next_partition (tpa, y);
1576 z != TPA_NONE;
1577 z = tpa_next_partition (tpa, z))
1578 {
1579 tmp = partition_to_var (map, z);
1580 /* p2 is the partition representative to which z belongs. */
1581 p2 = var_to_partition (map, tmp);
1582 if (debug)
1583 {
1584 fprintf (debug, "Coalesce : ");
1585 print_generic_expr (debug, var, TDF_SLIM);
1586 fprintf (debug, " &");
1587 print_generic_expr (debug, tmp, TDF_SLIM);
1588 fprintf (debug, " (%d ,%d)", p1, p2);
1589 }
1590
1591 /* If partitions are already merged, don't check for conflict. */
1592 if (tmp == var)
1593 {
1594 tpa_remove_partition (tpa, x, z);
1595 if (debug)
1596 fprintf (debug, ": Already coalesced\n");
1597 }
1598 else
1599 if (!conflict_graph_conflict_p (graph, p1, p2))
1600 {
1601 int v;
1602 if (tpa_find_tree (tpa, y) == TPA_NONE
1603 || tpa_find_tree (tpa, z) == TPA_NONE)
1604 {
1605 if (debug)
1606 fprintf (debug, ": Fail non-TPA member\n");
1607 continue;
1608 }
1609 if ((v = var_union (map, var, tmp)) == NO_PARTITION)
1610 {
1611 if (debug)
1612 fprintf (debug, ": Fail cannot combine partitions\n");
1613 continue;
1614 }
1615
1616 tpa_remove_partition (tpa, x, z);
1617 if (v == p1)
1618 conflict_graph_merge_regs (graph, v, z);
1619 else
1620 {
1621 /* Update the first partition's representative. */
1622 conflict_graph_merge_regs (graph, v, y);
1623 p1 = v;
1624 }
1625
1626 /* The root variable of the partition may be changed
1627 now. */
1628 var = partition_to_var (map, p1);
1629
1630 if (debug)
1631 fprintf (debug, ": Success -> %d\n", v);
1632 }
1633 else
1634 if (debug)
1635 fprintf (debug, ": Fail, Conflict\n");
1636 }
1637 }
1638 }
1639 }
1640
1641
1642 /* Send debug info for coalesce list CL to file F. */
1643
1644 void
1645 dump_coalesce_list (FILE *f, coalesce_list_p cl)
1646 {
1647 partition_pair_p node;
1648 int x, num;
1649 tree var;
1650
1651 if (cl->add_mode)
1652 {
1653 fprintf (f, "Coalesce List:\n");
1654 num = num_var_partitions (cl->map);
1655 for (x = 0; x < num; x++)
1656 {
1657 node = cl->list[x];
1658 if (node)
1659 {
1660 fprintf (f, "[");
1661 print_generic_expr (f, partition_to_var (cl->map, x), TDF_SLIM);
1662 fprintf (f, "] - ");
1663 for ( ; node; node = node->next)
1664 {
1665 var = partition_to_var (cl->map, node->second_partition);
1666 print_generic_expr (f, var, TDF_SLIM);
1667 fprintf (f, "(%1d), ", node->cost);
1668 }
1669 fprintf (f, "\n");
1670 }
1671 }
1672 }
1673 else
1674 {
1675 fprintf (f, "Sorted Coalesce list:\n");
1676 for (node = cl->list[0]; node; node = node->next)
1677 {
1678 fprintf (f, "(%d) ", node->cost);
1679 var = partition_to_var (cl->map, node->first_partition);
1680 print_generic_expr (f, var, TDF_SLIM);
1681 fprintf (f, " : ");
1682 var = partition_to_var (cl->map, node->second_partition);
1683 print_generic_expr (f, var, TDF_SLIM);
1684 fprintf (f, "\n");
1685 }
1686 }
1687 }
1688
1689
1690 /* Output tree_partition_associator object TPA to file F.. */
1691
1692 void
1693 tpa_dump (FILE *f, tpa_p tpa)
1694 {
1695 int x, i;
1696
1697 if (!tpa)
1698 return;
1699
1700 for (x = 0; x < tpa_num_trees (tpa); x++)
1701 {
1702 print_generic_expr (f, tpa_tree (tpa, x), TDF_SLIM);
1703 fprintf (f, " : (");
1704 for (i = tpa_first_partition (tpa, x);
1705 i != TPA_NONE;
1706 i = tpa_next_partition (tpa, i))
1707 {
1708 fprintf (f, "(%d)",i);
1709 print_generic_expr (f, partition_to_var (tpa->map, i), TDF_SLIM);
1710 fprintf (f, " ");
1711
1712 #ifdef ENABLE_CHECKING
1713 if (tpa_find_tree (tpa, i) != x)
1714 fprintf (f, "**find tree incorrectly set** ");
1715 #endif
1716
1717 }
1718 fprintf (f, ")\n");
1719 }
1720 fflush (f);
1721 }
1722
1723
1724 /* Output partition map MAP to file F. */
1725
1726 void
1727 dump_var_map (FILE *f, var_map map)
1728 {
1729 int t;
1730 unsigned x, y;
1731 int p;
1732
1733 fprintf (f, "\nPartition map \n\n");
1734
1735 for (x = 0; x < map->num_partitions; x++)
1736 {
1737 if (map->compact_to_partition != NULL)
1738 p = map->compact_to_partition[x];
1739 else
1740 p = x;
1741
1742 if (map->partition_to_var[p] == NULL_TREE)
1743 continue;
1744
1745 t = 0;
1746 for (y = 1; y < num_ssa_names; y++)
1747 {
1748 p = partition_find (map->var_partition, y);
1749 if (map->partition_to_compact)
1750 p = map->partition_to_compact[p];
1751 if (p == (int)x)
1752 {
1753 if (t++ == 0)
1754 {
1755 fprintf(f, "Partition %d (", x);
1756 print_generic_expr (f, partition_to_var (map, p), TDF_SLIM);
1757 fprintf (f, " - ");
1758 }
1759 fprintf (f, "%d ", y);
1760 }
1761 }
1762 if (t != 0)
1763 fprintf (f, ")\n");
1764 }
1765 fprintf (f, "\n");
1766 }
1767
1768
1769 /* Output live range info LIVE to file F, controlled by FLAG. */
1770
1771 void
1772 dump_live_info (FILE *f, tree_live_info_p live, int flag)
1773 {
1774 basic_block bb;
1775 int i;
1776 var_map map = live->map;
1777
1778 if ((flag & LIVEDUMP_ENTRY) && live->livein)
1779 {
1780 FOR_EACH_BB (bb)
1781 {
1782 fprintf (f, "\nLive on entry to BB%d : ", bb->index);
1783 for (i = 0; i < num_var_partitions (map); i++)
1784 {
1785 if (bitmap_bit_p (live_entry_blocks (live, i), bb->index))
1786 {
1787 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1788 fprintf (f, " ");
1789 }
1790 }
1791 fprintf (f, "\n");
1792 }
1793 }
1794
1795 if ((flag & LIVEDUMP_EXIT) && live->liveout)
1796 {
1797 FOR_EACH_BB (bb)
1798 {
1799 fprintf (f, "\nLive on exit from BB%d : ", bb->index);
1800 EXECUTE_IF_SET_IN_BITMAP (live->liveout[bb->index], 0, i,
1801 {
1802 print_generic_expr (f, partition_to_var (map, i), TDF_SLIM);
1803 fprintf (f, " ");
1804 });
1805 fprintf (f, "\n");
1806 }
1807 }
1808 }
1809
1810 /* Register partitions in MAP so that we can take VARS out of SSA form.
1811 This requires a walk over all the PHI nodes and all the statements. */
1812
1813 void
1814 register_ssa_partitions_for_vars (bitmap vars, var_map map)
1815 {
1816 basic_block bb;
1817
1818 if (bitmap_first_set_bit (vars) >= 0)
1819 {
1820
1821 /* Find every instance (SSA_NAME) of variables in VARs and
1822 register a new partition for them. This requires examining
1823 every statement and every PHI node once. */
1824 FOR_EACH_BB (bb)
1825 {
1826 block_stmt_iterator bsi;
1827 tree next;
1828 tree phi;
1829
1830 /* Register partitions for SSA_NAMEs appearing in the PHI
1831 nodes in this basic block.
1832
1833 Note we delete PHI nodes in this loop if they are
1834 associated with virtual vars which are going to be
1835 renamed. */
1836 for (phi = phi_nodes (bb); phi; phi = next)
1837 {
1838 tree result = SSA_NAME_VAR (PHI_RESULT (phi));
1839
1840 next = TREE_CHAIN (phi);
1841 if (bitmap_bit_p (vars, var_ann (result)->uid))
1842 {
1843 if (! is_gimple_reg (result))
1844 remove_phi_node (phi, NULL_TREE, bb);
1845 else
1846 {
1847 int i;
1848
1849 /* Register a partition for the result. */
1850 register_ssa_partition (map, PHI_RESULT (phi), 0);
1851
1852 /* Register a partition for each argument as needed. */
1853 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1854 {
1855 tree arg = PHI_ARG_DEF (phi, i);
1856
1857 if (TREE_CODE (arg) != SSA_NAME)
1858 continue;
1859 if (!bitmap_bit_p (vars,
1860 var_ann (SSA_NAME_VAR (arg))->uid))
1861 continue;
1862
1863 register_ssa_partition (map, arg, 1);
1864 }
1865 }
1866 }
1867 }
1868
1869 /* Now register partitions for SSA_NAMEs appearing in each
1870 statement in this block. */
1871 for (bsi = bsi_start (bb); ! bsi_end_p (bsi); bsi_next (&bsi))
1872 {
1873 stmt_ann_t ann = stmt_ann (bsi_stmt (bsi));
1874 use_optype uses = USE_OPS (ann);
1875 def_optype defs = DEF_OPS (ann);
1876 unsigned int i;
1877
1878 for (i = 0; i < NUM_USES (uses); i++)
1879 {
1880 tree op = USE_OP (uses, i);
1881
1882 if (TREE_CODE (op) == SSA_NAME
1883 && bitmap_bit_p (vars, var_ann (SSA_NAME_VAR (op))->uid))
1884 register_ssa_partition (map, op, 1);
1885 }
1886
1887 for (i = 0; i < NUM_DEFS (defs); i++)
1888 {
1889 tree op = DEF_OP (defs, i);
1890
1891 if (TREE_CODE (op) == SSA_NAME
1892 && bitmap_bit_p (vars,
1893 var_ann (SSA_NAME_VAR (op))->uid))
1894 register_ssa_partition (map, op, 0);
1895 }
1896 }
1897 }
1898 }
1899 }
1900