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