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