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