Fix PR#.
[official-gcc.git] / gcc / tree-ssa-coalesce.c
blobd5e5f8702ff69fa5359ffe64632447e2987ed00f
1 /* Coalesce SSA_NAMES together for the out-of-ssa pass.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation,
3 Inc.
4 Contributed by Andrew MacLeod <amacleod@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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 "diagnostic.h"
29 #include "bitmap.h"
30 #include "tree-flow.h"
31 #include "hashtab.h"
32 #include "tree-dump.h"
33 #include "tree-ssa-live.h"
34 #include "toplev.h"
37 /* This set of routines implements a coalesce_list. This is an object which
38 is used to track pairs of ssa_names which are desirable to coalesce
39 together to avoid copies. Costs are associated with each pair, and when
40 all desired information has been collected, the object can be used to
41 order the pairs for processing. */
43 /* This structure defines a pair entry. */
45 typedef struct coalesce_pair
47 int first_element;
48 int second_element;
49 int cost;
50 } * coalesce_pair_p;
51 typedef const struct coalesce_pair *const_coalesce_pair_p;
53 typedef struct cost_one_pair_d
55 int first_element;
56 int second_element;
57 struct cost_one_pair_d *next;
58 } * cost_one_pair_p;
60 /* This structure maintains the list of coalesce pairs. */
62 typedef struct coalesce_list_d
64 htab_t list; /* Hash table. */
65 coalesce_pair_p *sorted; /* List when sorted. */
66 int num_sorted; /* Number in the sorted list. */
67 cost_one_pair_p cost_one_list;/* Single use coalesces with cost 1. */
68 } *coalesce_list_p;
70 #define NO_BEST_COALESCE -1
71 #define MUST_COALESCE_COST INT_MAX
74 /* Return cost of execution of copy instruction with FREQUENCY
75 possibly on CRITICAL edge and in HOT basic block. */
77 static inline int
78 coalesce_cost (int frequency, bool hot, bool critical)
80 /* Base costs on BB frequencies bounded by 1. */
81 int cost = frequency;
83 if (!cost)
84 cost = 1;
86 if (optimize_size)
87 cost = 1;
88 else
89 /* It is more important to coalesce in HOT blocks. */
90 if (hot)
91 cost *= 2;
93 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
94 if (critical)
95 cost *= 2;
96 return cost;
100 /* Return the cost of executing a copy instruction in basic block BB. */
102 static inline int
103 coalesce_cost_bb (basic_block bb)
105 return coalesce_cost (bb->frequency, maybe_hot_bb_p (bb), false);
109 /* Return the cost of executing a copy instruction on edge E. */
111 static inline int
112 coalesce_cost_edge (edge e)
114 if (e->flags & EDGE_ABNORMAL)
115 return MUST_COALESCE_COST;
117 return coalesce_cost (EDGE_FREQUENCY (e),
118 maybe_hot_edge_p (e),
119 EDGE_CRITICAL_P (e));
123 /* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
124 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
125 NO_BEST_COALESCE is returned if there aren't any. */
127 static inline int
128 pop_cost_one_pair (coalesce_list_p cl, int *p1, int *p2)
130 cost_one_pair_p ptr;
132 ptr = cl->cost_one_list;
133 if (!ptr)
134 return NO_BEST_COALESCE;
136 *p1 = ptr->first_element;
137 *p2 = ptr->second_element;
138 cl->cost_one_list = ptr->next;
140 free (ptr);
142 return 1;
145 /* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
146 2 elements via P1 and P2. Their calculated cost is returned by the function.
147 NO_BEST_COALESCE is returned if the coalesce list is empty. */
149 static inline int
150 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
152 coalesce_pair_p node;
153 int ret;
155 if (cl->sorted == NULL)
156 return pop_cost_one_pair (cl, p1, p2);
158 if (cl->num_sorted == 0)
159 return pop_cost_one_pair (cl, p1, p2);
161 node = cl->sorted[--(cl->num_sorted)];
162 *p1 = node->first_element;
163 *p2 = node->second_element;
164 ret = node->cost;
165 free (node);
167 return ret;
171 #define COALESCE_HASH_FN(R1, R2) ((R2) * ((R2) - 1) / 2 + (R1))
173 /* Hash function for coalesce list. Calculate hash for PAIR. */
175 static unsigned int
176 coalesce_pair_map_hash (const void *pair)
178 hashval_t a = (hashval_t)(((const_coalesce_pair_p)pair)->first_element);
179 hashval_t b = (hashval_t)(((const_coalesce_pair_p)pair)->second_element);
181 return COALESCE_HASH_FN (a,b);
185 /* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
186 returning TRUE if the two pairs are equivalent. */
188 static int
189 coalesce_pair_map_eq (const void *pair1, const void *pair2)
191 const_coalesce_pair_p const p1 = (const_coalesce_pair_p) pair1;
192 const_coalesce_pair_p const p2 = (const_coalesce_pair_p) pair2;
194 return (p1->first_element == p2->first_element
195 && p1->second_element == p2->second_element);
199 /* Create a new empty coalesce list object and return it. */
201 static inline coalesce_list_p
202 create_coalesce_list (void)
204 coalesce_list_p list;
205 unsigned size = num_ssa_names * 3;
207 if (size < 40)
208 size = 40;
210 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
211 list->list = htab_create (size, coalesce_pair_map_hash,
212 coalesce_pair_map_eq, NULL);
213 list->sorted = NULL;
214 list->num_sorted = 0;
215 list->cost_one_list = NULL;
216 return list;
220 /* Delete coalesce list CL. */
222 static inline void
223 delete_coalesce_list (coalesce_list_p cl)
225 gcc_assert (cl->cost_one_list == NULL);
226 htab_delete (cl->list);
227 if (cl->sorted)
228 free (cl->sorted);
229 gcc_assert (cl->num_sorted == 0);
230 free (cl);
234 /* Find a matching coalesce pair object in CL for the pair P1 and P2. If
235 one isn't found, return NULL if CREATE is false, otherwise create a new
236 coalesce pair object and return it. */
238 static coalesce_pair_p
239 find_coalesce_pair (coalesce_list_p cl, int p1, int p2, bool create)
241 struct coalesce_pair p, *pair;
242 void **slot;
243 unsigned int hash;
245 /* Normalize so that p1 is the smaller value. */
246 if (p2 < p1)
248 p.first_element = p2;
249 p.second_element = p1;
251 else
253 p.first_element = p1;
254 p.second_element = p2;
258 hash = coalesce_pair_map_hash (&p);
259 pair = (struct coalesce_pair *) htab_find_with_hash (cl->list, &p, hash);
261 if (create && !pair)
263 gcc_assert (cl->sorted == NULL);
264 pair = XNEW (struct coalesce_pair);
265 pair->first_element = p.first_element;
266 pair->second_element = p.second_element;
267 pair->cost = 0;
268 slot = htab_find_slot_with_hash (cl->list, pair, hash, INSERT);
269 *(struct coalesce_pair **)slot = pair;
272 return pair;
275 static inline void
276 add_cost_one_coalesce (coalesce_list_p cl, int p1, int p2)
278 cost_one_pair_p pair;
280 pair = XNEW (struct cost_one_pair_d);
281 pair->first_element = p1;
282 pair->second_element = p2;
283 pair->next = cl->cost_one_list;
284 cl->cost_one_list = pair;
288 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
290 static inline void
291 add_coalesce (coalesce_list_p cl, int p1, int p2,
292 int value)
294 coalesce_pair_p node;
296 gcc_assert (cl->sorted == NULL);
297 if (p1 == p2)
298 return;
300 node = find_coalesce_pair (cl, p1, p2, true);
302 /* Once the value is MUST_COALESCE_COST, leave it that way. */
303 if (node->cost != MUST_COALESCE_COST)
305 if (value == MUST_COALESCE_COST)
306 node->cost = value;
307 else
308 node->cost += value;
313 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
315 static int
316 compare_pairs (const void *p1, const void *p2)
318 const_coalesce_pair_p const *const pp1 = (const_coalesce_pair_p const *) p1;
319 const_coalesce_pair_p const *const pp2 = (const_coalesce_pair_p const *) p2;
320 int result;
322 result = (* pp2)->cost - (* pp1)->cost;
323 /* Since qsort does not guarantee stability we use the elements
324 as a secondary key. This provides us with independence from
325 the host's implementation of the sorting algorithm. */
326 if (result == 0)
328 result = (* pp2)->first_element - (* pp1)->first_element;
329 if (result == 0)
330 result = (* pp2)->second_element - (* pp1)->second_element;
333 return result;
337 /* Return the number of unique coalesce pairs in CL. */
339 static inline int
340 num_coalesce_pairs (coalesce_list_p cl)
342 return htab_elements (cl->list);
346 /* Iterator over hash table pairs. */
347 typedef struct
349 htab_iterator hti;
350 } coalesce_pair_iterator;
353 /* Return first partition pair from list CL, initializing iterator ITER. */
355 static inline coalesce_pair_p
356 first_coalesce_pair (coalesce_list_p cl, coalesce_pair_iterator *iter)
358 coalesce_pair_p pair;
360 pair = (coalesce_pair_p) first_htab_element (&(iter->hti), cl->list);
361 return pair;
365 /* Return TRUE if there are no more partitions in for ITER to process. */
367 static inline bool
368 end_coalesce_pair_p (coalesce_pair_iterator *iter)
370 return end_htab_p (&(iter->hti));
374 /* Return the next partition pair to be visited by ITER. */
376 static inline coalesce_pair_p
377 next_coalesce_pair (coalesce_pair_iterator *iter)
379 coalesce_pair_p pair;
381 pair = (coalesce_pair_p) next_htab_element (&(iter->hti));
382 return pair;
386 /* Iterate over CL using ITER, returning values in PAIR. */
388 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
389 for ((PAIR) = first_coalesce_pair ((CL), &(ITER)); \
390 !end_coalesce_pair_p (&(ITER)); \
391 (PAIR) = next_coalesce_pair (&(ITER)))
394 /* Prepare CL for removal of preferred pairs. When finished they are sorted
395 in order from most important coalesce to least important. */
397 static void
398 sort_coalesce_list (coalesce_list_p cl)
400 unsigned x, num;
401 coalesce_pair_p p;
402 coalesce_pair_iterator ppi;
404 gcc_assert (cl->sorted == NULL);
406 num = num_coalesce_pairs (cl);
407 cl->num_sorted = num;
408 if (num == 0)
409 return;
411 /* Allocate a vector for the pair pointers. */
412 cl->sorted = XNEWVEC (coalesce_pair_p, num);
414 /* Populate the vector with pointers to the pairs. */
415 x = 0;
416 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
417 cl->sorted[x++] = p;
418 gcc_assert (x == num);
420 /* Already sorted. */
421 if (num == 1)
422 return;
424 /* If there are only 2, just pick swap them if the order isn't correct. */
425 if (num == 2)
427 if (cl->sorted[0]->cost > cl->sorted[1]->cost)
429 p = cl->sorted[0];
430 cl->sorted[0] = cl->sorted[1];
431 cl->sorted[1] = p;
433 return;
436 /* Only call qsort if there are more than 2 items. */
437 if (num > 2)
438 qsort (cl->sorted, num, sizeof (coalesce_pair_p), compare_pairs);
442 /* Send debug info for coalesce list CL to file F. */
444 static void
445 dump_coalesce_list (FILE *f, coalesce_list_p cl)
447 coalesce_pair_p node;
448 coalesce_pair_iterator ppi;
449 int x;
450 tree var;
452 if (cl->sorted == NULL)
454 fprintf (f, "Coalesce List:\n");
455 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
457 tree var1 = ssa_name (node->first_element);
458 tree var2 = ssa_name (node->second_element);
459 print_generic_expr (f, var1, TDF_SLIM);
460 fprintf (f, " <-> ");
461 print_generic_expr (f, var2, TDF_SLIM);
462 fprintf (f, " (%1d), ", node->cost);
463 fprintf (f, "\n");
466 else
468 fprintf (f, "Sorted Coalesce list:\n");
469 for (x = cl->num_sorted - 1 ; x >=0; x--)
471 node = cl->sorted[x];
472 fprintf (f, "(%d) ", node->cost);
473 var = ssa_name (node->first_element);
474 print_generic_expr (f, var, TDF_SLIM);
475 fprintf (f, " <-> ");
476 var = ssa_name (node->second_element);
477 print_generic_expr (f, var, TDF_SLIM);
478 fprintf (f, "\n");
484 /* This represents a conflict graph. Implemented as an array of bitmaps.
485 A full matrix is used for conflicts rather than just upper triangular form.
486 this make sit much simpler and faster to perform conflict merges. */
488 typedef struct ssa_conflicts_d
490 unsigned size;
491 bitmap *conflicts;
492 } * ssa_conflicts_p;
495 /* Return an empty new conflict graph for SIZE elements. */
497 static inline ssa_conflicts_p
498 ssa_conflicts_new (unsigned size)
500 ssa_conflicts_p ptr;
502 ptr = XNEW (struct ssa_conflicts_d);
503 ptr->conflicts = XCNEWVEC (bitmap, size);
504 ptr->size = size;
505 return ptr;
509 /* Free storage for conflict graph PTR. */
511 static inline void
512 ssa_conflicts_delete (ssa_conflicts_p ptr)
514 unsigned x;
515 for (x = 0; x < ptr->size; x++)
516 if (ptr->conflicts[x])
517 BITMAP_FREE (ptr->conflicts[x]);
519 free (ptr->conflicts);
520 free (ptr);
524 /* Test if elements X and Y conflict in graph PTR. */
526 static inline bool
527 ssa_conflicts_test_p (ssa_conflicts_p ptr, unsigned x, unsigned y)
529 bitmap b;
531 #ifdef ENABLE_CHECKING
532 gcc_assert (x < ptr->size);
533 gcc_assert (y < ptr->size);
534 gcc_assert (x != y);
535 #endif
537 b = ptr->conflicts[x];
538 if (b)
539 /* Avoid the lookup if Y has no conflicts. */
540 return ptr->conflicts[y] ? bitmap_bit_p (b, y) : false;
541 else
542 return false;
546 /* Add a conflict with Y to the bitmap for X in graph PTR. */
548 static inline void
549 ssa_conflicts_add_one (ssa_conflicts_p ptr, unsigned x, unsigned y)
551 /* If there are no conflicts yet, allocate the bitmap and set bit. */
552 if (!ptr->conflicts[x])
553 ptr->conflicts[x] = BITMAP_ALLOC (NULL);
554 bitmap_set_bit (ptr->conflicts[x], y);
558 /* Add conflicts between X and Y in graph PTR. */
560 static inline void
561 ssa_conflicts_add (ssa_conflicts_p ptr, unsigned x, unsigned y)
563 #ifdef ENABLE_CHECKING
564 gcc_assert (x < ptr->size);
565 gcc_assert (y < ptr->size);
566 gcc_assert (x != y);
567 #endif
568 ssa_conflicts_add_one (ptr, x, y);
569 ssa_conflicts_add_one (ptr, y, x);
573 /* Merge all Y's conflict into X in graph PTR. */
575 static inline void
576 ssa_conflicts_merge (ssa_conflicts_p ptr, unsigned x, unsigned y)
578 unsigned z;
579 bitmap_iterator bi;
581 gcc_assert (x != y);
582 if (!(ptr->conflicts[y]))
583 return;
585 /* Add a conflict between X and every one Y has. If the bitmap doesn't
586 exist, then it has already been coalesced, and we don't need to add a
587 conflict. */
588 EXECUTE_IF_SET_IN_BITMAP (ptr->conflicts[y], 0, z, bi)
589 if (ptr->conflicts[z])
590 bitmap_set_bit (ptr->conflicts[z], x);
592 if (ptr->conflicts[x])
594 /* If X has conflicts, add Y's to X. */
595 bitmap_ior_into (ptr->conflicts[x], ptr->conflicts[y]);
596 BITMAP_FREE (ptr->conflicts[y]);
598 else
600 /* If X has no conflicts, simply use Y's. */
601 ptr->conflicts[x] = ptr->conflicts[y];
602 ptr->conflicts[y] = NULL;
607 /* Dump a conflicts graph. */
609 static void
610 ssa_conflicts_dump (FILE *file, ssa_conflicts_p ptr)
612 unsigned x;
614 fprintf (file, "\nConflict graph:\n");
616 for (x = 0; x < ptr->size; x++)
617 if (ptr->conflicts[x])
619 fprintf (dump_file, "%d: ", x);
620 dump_bitmap (file, ptr->conflicts[x]);
625 /* This structure is used to efficiently record the current status of live
626 SSA_NAMES when building a conflict graph.
627 LIVE_BASE_VAR has a bit set for each base variable which has at least one
628 ssa version live.
629 LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
630 index, and is used to track what partitions of each base variable are
631 live. This makes it easy to add conflicts between just live partitions
632 with the same base variable.
633 The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
634 marked as being live. This delays clearing of these bitmaps until
635 they are actually needed again. */
637 typedef struct live_track_d
639 bitmap live_base_var; /* Indicates if a basevar is live. */
640 bitmap *live_base_partitions; /* Live partitions for each basevar. */
641 var_map map; /* Var_map being used for partition mapping. */
642 } * live_track_p;
645 /* This routine will create a new live track structure based on the partitions
646 in MAP. */
648 static live_track_p
649 new_live_track (var_map map)
651 live_track_p ptr;
652 int lim, x;
654 /* Make sure there is a partition view in place. */
655 gcc_assert (map->partition_to_base_index != NULL);
657 ptr = (live_track_p) xmalloc (sizeof (struct live_track_d));
658 ptr->map = map;
659 lim = num_basevars (map);
660 ptr->live_base_partitions = (bitmap *) xmalloc(sizeof (bitmap *) * lim);
661 ptr->live_base_var = BITMAP_ALLOC (NULL);
662 for (x = 0; x < lim; x++)
663 ptr->live_base_partitions[x] = BITMAP_ALLOC (NULL);
664 return ptr;
668 /* This routine will free the memory associated with PTR. */
670 static void
671 delete_live_track (live_track_p ptr)
673 int x, lim;
675 lim = num_basevars (ptr->map);
676 for (x = 0; x < lim; x++)
677 BITMAP_FREE (ptr->live_base_partitions[x]);
678 BITMAP_FREE (ptr->live_base_var);
679 free (ptr->live_base_partitions);
680 free (ptr);
684 /* This function will remove PARTITION from the live list in PTR. */
686 static inline void
687 live_track_remove_partition (live_track_p ptr, int partition)
689 int root;
691 root = basevar_index (ptr->map, partition);
692 bitmap_clear_bit (ptr->live_base_partitions[root], partition);
693 /* If the element list is empty, make the base variable not live either. */
694 if (bitmap_empty_p (ptr->live_base_partitions[root]))
695 bitmap_clear_bit (ptr->live_base_var, root);
699 /* This function will adds PARTITION to the live list in PTR. */
701 static inline void
702 live_track_add_partition (live_track_p ptr, int partition)
704 int root;
706 root = basevar_index (ptr->map, partition);
707 /* If this base var wasn't live before, it is now. Clear the element list
708 since it was delayed until needed. */
709 if (!bitmap_bit_p (ptr->live_base_var, root))
711 bitmap_set_bit (ptr->live_base_var, root);
712 bitmap_clear (ptr->live_base_partitions[root]);
714 bitmap_set_bit (ptr->live_base_partitions[root], partition);
719 /* Clear the live bit for VAR in PTR. */
721 static inline void
722 live_track_clear_var (live_track_p ptr, tree var)
724 int p;
726 p = var_to_partition (ptr->map, var);
727 if (p != NO_PARTITION)
728 live_track_remove_partition (ptr, p);
732 /* Return TRUE if VAR is live in PTR. */
734 static inline bool
735 live_track_live_p (live_track_p ptr, tree var)
737 int p, root;
739 p = var_to_partition (ptr->map, var);
740 if (p != NO_PARTITION)
742 root = basevar_index (ptr->map, p);
743 if (bitmap_bit_p (ptr->live_base_var, root))
744 return bitmap_bit_p (ptr->live_base_partitions[root], p);
746 return false;
750 /* This routine will add USE to PTR. USE will be marked as live in both the
751 ssa live map and the live bitmap for the root of USE. */
753 static inline void
754 live_track_process_use (live_track_p ptr, tree use)
756 int p;
758 p = var_to_partition (ptr->map, use);
759 if (p == NO_PARTITION)
760 return;
762 /* Mark as live in the appropriate live list. */
763 live_track_add_partition (ptr, p);
767 /* This routine will process a DEF in PTR. DEF will be removed from the live
768 lists, and if there are any other live partitions with the same base
769 variable, conflicts will be added to GRAPH. */
771 static inline void
772 live_track_process_def (live_track_p ptr, tree def, ssa_conflicts_p graph)
774 int p, root;
775 bitmap b;
776 unsigned x;
777 bitmap_iterator bi;
779 p = var_to_partition (ptr->map, def);
780 if (p == NO_PARTITION)
781 return;
783 /* Clear the liveness bit. */
784 live_track_remove_partition (ptr, p);
786 /* If the bitmap isn't empty now, conflicts need to be added. */
787 root = basevar_index (ptr->map, p);
788 if (bitmap_bit_p (ptr->live_base_var, root))
790 b = ptr->live_base_partitions[root];
791 EXECUTE_IF_SET_IN_BITMAP (b, 0, x, bi)
792 ssa_conflicts_add (graph, p, x);
797 /* Initialize PTR with the partitions set in INIT. */
799 static inline void
800 live_track_init (live_track_p ptr, bitmap init)
802 unsigned p;
803 bitmap_iterator bi;
805 /* Mark all live on exit partitions. */
806 EXECUTE_IF_SET_IN_BITMAP (init, 0, p, bi)
807 live_track_add_partition (ptr, p);
811 /* This routine will clear all live partitions in PTR. */
813 static inline void
814 live_track_clear_base_vars (live_track_p ptr)
816 /* Simply clear the live base list. Anything marked as live in the element
817 lists will be cleared later if/when the base variable ever comes alive
818 again. */
819 bitmap_clear (ptr->live_base_var);
823 /* Build a conflict graph based on LIVEINFO. Any partitions which are in the
824 partition view of the var_map liveinfo is based on get entries in the
825 conflict graph. Only conflicts between ssa_name partitions with the same
826 base variable are added. */
828 static ssa_conflicts_p
829 build_ssa_conflict_graph (tree_live_info_p liveinfo)
831 ssa_conflicts_p graph;
832 var_map map;
833 basic_block bb;
834 ssa_op_iter iter;
835 live_track_p live;
837 map = live_var_map (liveinfo);
838 graph = ssa_conflicts_new (num_var_partitions (map));
840 live = new_live_track (map);
842 FOR_EACH_BB (bb)
844 gimple_stmt_iterator gsi;
846 /* Start with live on exit temporaries. */
847 live_track_init (live, live_on_exit (liveinfo, bb));
849 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
851 tree var;
852 gimple stmt = gsi_stmt (gsi);
854 /* A copy between 2 partitions does not introduce an interference
855 by itself. If they did, you would never be able to coalesce
856 two things which are copied. If the two variables really do
857 conflict, they will conflict elsewhere in the program.
859 This is handled by simply removing the SRC of the copy from the
860 live list, and processing the stmt normally. */
861 if (is_gimple_assign (stmt))
863 tree lhs = gimple_assign_lhs (stmt);
864 tree rhs1 = gimple_assign_rhs1 (stmt);
865 if (gimple_assign_copy_p (stmt)
866 && TREE_CODE (lhs) == SSA_NAME
867 && TREE_CODE (rhs1) == SSA_NAME)
868 live_track_clear_var (live, rhs1);
871 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
872 live_track_process_def (live, var, graph);
874 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
875 live_track_process_use (live, var);
878 /* If result of a PHI is unused, looping over the statements will not
879 record any conflicts since the def was never live. Since the PHI node
880 is going to be translated out of SSA form, it will insert a copy.
881 There must be a conflict recorded between the result of the PHI and
882 any variables that are live. Otherwise the out-of-ssa translation
883 may create incorrect code. */
884 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
886 gimple phi = gsi_stmt (gsi);
887 tree result = PHI_RESULT (phi);
888 if (live_track_live_p (live, result))
889 live_track_process_def (live, result, graph);
892 live_track_clear_base_vars (live);
895 delete_live_track (live);
896 return graph;
900 /* Shortcut routine to print messages to file F of the form:
901 "STR1 EXPR1 STR2 EXPR2 STR3." */
903 static inline void
904 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
905 tree expr2, const char *str3)
907 fprintf (f, "%s", str1);
908 print_generic_expr (f, expr1, TDF_SLIM);
909 fprintf (f, "%s", str2);
910 print_generic_expr (f, expr2, TDF_SLIM);
911 fprintf (f, "%s", str3);
915 /* Called if a coalesce across and abnormal edge cannot be performed. PHI is
916 the phi node at fault, I is the argument index at fault. A message is
917 printed and compilation is then terminated. */
919 static inline void
920 abnormal_corrupt (gimple phi, int i)
922 edge e = gimple_phi_arg_edge (phi, i);
923 tree res = gimple_phi_result (phi);
924 tree arg = gimple_phi_arg_def (phi, i);
926 fprintf (stderr, " Corrupt SSA across abnormal edge BB%d->BB%d\n",
927 e->src->index, e->dest->index);
928 fprintf (stderr, "Argument %d (", i);
929 print_generic_expr (stderr, arg, TDF_SLIM);
930 if (TREE_CODE (arg) != SSA_NAME)
931 fprintf (stderr, ") is not an SSA_NAME.\n");
932 else
934 gcc_assert (SSA_NAME_VAR (res) != SSA_NAME_VAR (arg));
935 fprintf (stderr, ") does not have the same base variable as the result ");
936 print_generic_stmt (stderr, res, TDF_SLIM);
939 internal_error ("SSA corruption");
943 /* Print a failure to coalesce a MUST_COALESCE pair X and Y. */
945 static inline void
946 fail_abnormal_edge_coalesce (int x, int y)
948 fprintf (stderr, "\nUnable to coalesce ssa_names %d and %d",x, y);
949 fprintf (stderr, " which are marked as MUST COALESCE.\n");
950 print_generic_expr (stderr, ssa_name (x), TDF_SLIM);
951 fprintf (stderr, " and ");
952 print_generic_stmt (stderr, ssa_name (y), TDF_SLIM);
954 internal_error ("SSA corruption");
958 /* This function creates a var_map for the current function as well as creating
959 a coalesce list for use later in the out of ssa process. */
961 static var_map
962 create_outofssa_var_map (coalesce_list_p cl, bitmap used_in_copy)
964 gimple_stmt_iterator gsi;
965 basic_block bb;
966 tree var;
967 gimple stmt;
968 tree first;
969 var_map map;
970 ssa_op_iter iter;
971 int v1, v2, cost;
972 unsigned i;
974 #ifdef ENABLE_CHECKING
975 bitmap used_in_real_ops;
976 bitmap used_in_virtual_ops;
978 used_in_real_ops = BITMAP_ALLOC (NULL);
979 used_in_virtual_ops = BITMAP_ALLOC (NULL);
980 #endif
982 map = init_var_map (num_ssa_names + 1);
984 FOR_EACH_BB (bb)
986 tree arg;
988 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
990 gimple phi = gsi_stmt (gsi);
991 size_t i;
992 int ver;
993 tree res;
994 bool saw_copy = false;
996 res = gimple_phi_result (phi);
997 ver = SSA_NAME_VERSION (res);
998 register_ssa_partition (map, res);
1000 /* Register ssa_names and coalesces between the args and the result
1001 of all PHI. */
1002 for (i = 0; i < gimple_phi_num_args (phi); i++)
1004 edge e = gimple_phi_arg_edge (phi, i);
1005 arg = PHI_ARG_DEF (phi, i);
1006 if (TREE_CODE (arg) == SSA_NAME)
1007 register_ssa_partition (map, arg);
1008 if (TREE_CODE (arg) == SSA_NAME
1009 && SSA_NAME_VAR (arg) == SSA_NAME_VAR (res))
1011 saw_copy = true;
1012 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (arg));
1013 if ((e->flags & EDGE_ABNORMAL) == 0)
1015 int cost = coalesce_cost_edge (e);
1016 if (cost == 1 && has_single_use (arg))
1017 add_cost_one_coalesce (cl, ver, SSA_NAME_VERSION (arg));
1018 else
1019 add_coalesce (cl, ver, SSA_NAME_VERSION (arg), cost);
1022 else
1023 if (e->flags & EDGE_ABNORMAL)
1024 abnormal_corrupt (phi, i);
1026 if (saw_copy)
1027 bitmap_set_bit (used_in_copy, ver);
1030 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1032 stmt = gsi_stmt (gsi);
1034 /* Register USE and DEF operands in each statement. */
1035 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
1036 register_ssa_partition (map, var);
1038 /* Check for copy coalesces. */
1039 switch (gimple_code (stmt))
1041 case GIMPLE_ASSIGN:
1043 tree lhs = gimple_assign_lhs (stmt);
1044 tree rhs1 = gimple_assign_rhs1 (stmt);
1046 if (gimple_assign_copy_p (stmt)
1047 && TREE_CODE (lhs) == SSA_NAME
1048 && TREE_CODE (rhs1) == SSA_NAME
1049 && SSA_NAME_VAR (lhs) == SSA_NAME_VAR (rhs1))
1051 v1 = SSA_NAME_VERSION (lhs);
1052 v2 = SSA_NAME_VERSION (rhs1);
1053 cost = coalesce_cost_bb (bb);
1054 add_coalesce (cl, v1, v2, cost);
1055 bitmap_set_bit (used_in_copy, v1);
1056 bitmap_set_bit (used_in_copy, v2);
1059 break;
1061 case GIMPLE_ASM:
1063 unsigned long noutputs, i;
1064 unsigned long ninputs;
1065 tree *outputs, link;
1066 noutputs = gimple_asm_noutputs (stmt);
1067 ninputs = gimple_asm_ninputs (stmt);
1068 outputs = (tree *) alloca (noutputs * sizeof (tree));
1069 for (i = 0; i < noutputs; ++i) {
1070 link = gimple_asm_output_op (stmt, i);
1071 outputs[i] = TREE_VALUE (link);
1074 for (i = 0; i < ninputs; ++i)
1076 const char *constraint;
1077 tree input;
1078 char *end;
1079 unsigned long match;
1081 link = gimple_asm_input_op (stmt, i);
1082 constraint
1083 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1084 input = TREE_VALUE (link);
1086 if (TREE_CODE (input) != SSA_NAME)
1087 continue;
1089 match = strtoul (constraint, &end, 10);
1090 if (match >= noutputs || end == constraint)
1091 continue;
1093 if (TREE_CODE (outputs[match]) != SSA_NAME)
1094 continue;
1096 v1 = SSA_NAME_VERSION (outputs[match]);
1097 v2 = SSA_NAME_VERSION (input);
1099 if (SSA_NAME_VAR (outputs[match]) == SSA_NAME_VAR (input))
1101 cost = coalesce_cost (REG_BR_PROB_BASE,
1102 maybe_hot_bb_p (bb),
1103 false);
1104 add_coalesce (cl, v1, v2, cost);
1105 bitmap_set_bit (used_in_copy, v1);
1106 bitmap_set_bit (used_in_copy, v2);
1109 break;
1112 default:
1113 break;
1116 #ifdef ENABLE_CHECKING
1117 /* Mark real uses and defs. */
1118 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
1119 bitmap_set_bit (used_in_real_ops, DECL_UID (SSA_NAME_VAR (var)));
1121 /* Validate that virtual ops don't get used in funny ways. */
1122 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_VIRTUALS)
1124 bitmap_set_bit (used_in_virtual_ops,
1125 DECL_UID (SSA_NAME_VAR (var)));
1128 #endif /* ENABLE_CHECKING */
1132 /* Now process result decls and live on entry variables for entry into
1133 the coalesce list. */
1134 first = NULL_TREE;
1135 for (i = 1; i < num_ssa_names; i++)
1137 var = map->partition_to_var[i];
1138 if (var != NULL_TREE)
1140 /* Add coalesces between all the result decls. */
1141 if (TREE_CODE (SSA_NAME_VAR (var)) == RESULT_DECL)
1143 if (first == NULL_TREE)
1144 first = var;
1145 else
1147 gcc_assert (SSA_NAME_VAR (var) == SSA_NAME_VAR (first));
1148 v1 = SSA_NAME_VERSION (first);
1149 v2 = SSA_NAME_VERSION (var);
1150 bitmap_set_bit (used_in_copy, v1);
1151 bitmap_set_bit (used_in_copy, v2);
1152 cost = coalesce_cost_bb (EXIT_BLOCK_PTR);
1153 add_coalesce (cl, v1, v2, cost);
1156 /* Mark any default_def variables as being in the coalesce list
1157 since they will have to be coalesced with the base variable. If
1158 not marked as present, they won't be in the coalesce view. */
1159 if (gimple_default_def (cfun, SSA_NAME_VAR (var)) == var)
1160 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1164 #if defined ENABLE_CHECKING
1166 unsigned i;
1167 bitmap both = BITMAP_ALLOC (NULL);
1168 bitmap_and (both, used_in_real_ops, used_in_virtual_ops);
1169 if (!bitmap_empty_p (both))
1171 bitmap_iterator bi;
1173 EXECUTE_IF_SET_IN_BITMAP (both, 0, i, bi)
1174 fprintf (stderr, "Variable %s used in real and virtual operands\n",
1175 get_name (referenced_var (i)));
1176 internal_error ("SSA corruption");
1179 BITMAP_FREE (used_in_real_ops);
1180 BITMAP_FREE (used_in_virtual_ops);
1181 BITMAP_FREE (both);
1183 #endif
1185 return map;
1189 /* Attempt to coalesce ssa versions X and Y together using the partition
1190 mapping in MAP and checking conflicts in GRAPH. Output any debug info to
1191 DEBUG, if it is nun-NULL. */
1193 static inline bool
1194 attempt_coalesce (var_map map, ssa_conflicts_p graph, int x, int y,
1195 FILE *debug)
1197 int z;
1198 tree var1, var2;
1199 int p1, p2;
1201 p1 = var_to_partition (map, ssa_name (x));
1202 p2 = var_to_partition (map, ssa_name (y));
1204 if (debug)
1206 fprintf (debug, "(%d)", x);
1207 print_generic_expr (debug, partition_to_var (map, p1), TDF_SLIM);
1208 fprintf (debug, " & (%d)", y);
1209 print_generic_expr (debug, partition_to_var (map, p2), TDF_SLIM);
1212 if (p1 == p2)
1214 if (debug)
1215 fprintf (debug, ": Already Coalesced.\n");
1216 return true;
1219 if (debug)
1220 fprintf (debug, " [map: %d, %d] ", p1, p2);
1223 if (!ssa_conflicts_test_p (graph, p1, p2))
1225 var1 = partition_to_var (map, p1);
1226 var2 = partition_to_var (map, p2);
1227 z = var_union (map, var1, var2);
1228 if (z == NO_PARTITION)
1230 if (debug)
1231 fprintf (debug, ": Unable to perform partition union.\n");
1232 return false;
1235 /* z is the new combined partition. Remove the other partition from
1236 the list, and merge the conflicts. */
1237 if (z == p1)
1238 ssa_conflicts_merge (graph, p1, p2);
1239 else
1240 ssa_conflicts_merge (graph, p2, p1);
1242 if (debug)
1243 fprintf (debug, ": Success -> %d\n", z);
1244 return true;
1247 if (debug)
1248 fprintf (debug, ": Fail due to conflict\n");
1250 return false;
1254 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1255 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1257 static void
1258 coalesce_partitions (var_map map, ssa_conflicts_p graph, coalesce_list_p cl,
1259 FILE *debug)
1261 int x = 0, y = 0;
1262 tree var1, var2;
1263 int cost;
1264 basic_block bb;
1265 edge e;
1266 edge_iterator ei;
1268 /* First, coalesce all the copies across abnormal edges. These are not placed
1269 in the coalesce list because they do not need to be sorted, and simply
1270 consume extra memory/compilation time in large programs. */
1272 FOR_EACH_BB (bb)
1274 FOR_EACH_EDGE (e, ei, bb->preds)
1275 if (e->flags & EDGE_ABNORMAL)
1277 gimple_stmt_iterator gsi;
1278 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1279 gsi_next (&gsi))
1281 gimple phi = gsi_stmt (gsi);
1282 tree res = PHI_RESULT (phi);
1283 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1284 int v1 = SSA_NAME_VERSION (res);
1285 int v2 = SSA_NAME_VERSION (arg);
1287 if (SSA_NAME_VAR (arg) != SSA_NAME_VAR (res))
1288 abnormal_corrupt (phi, e->dest_idx);
1290 if (debug)
1291 fprintf (debug, "Abnormal coalesce: ");
1293 if (!attempt_coalesce (map, graph, v1, v2, debug))
1294 fail_abnormal_edge_coalesce (v1, v2);
1299 /* Now process the items in the coalesce list. */
1301 while ((cost = pop_best_coalesce (cl, &x, &y)) != NO_BEST_COALESCE)
1303 var1 = ssa_name (x);
1304 var2 = ssa_name (y);
1306 /* Assert the coalesces have the same base variable. */
1307 gcc_assert (SSA_NAME_VAR (var1) == SSA_NAME_VAR (var2));
1309 if (debug)
1310 fprintf (debug, "Coalesce list: ");
1311 attempt_coalesce (map, graph, x, y, debug);
1315 /* Returns a hash code for P. */
1317 static hashval_t
1318 hash_ssa_name_by_var (const void *p)
1320 const_tree n = (const_tree) p;
1321 return (hashval_t) htab_hash_pointer (SSA_NAME_VAR (n));
1324 /* Returns nonzero if P1 and P2 are equal. */
1326 static int
1327 eq_ssa_name_by_var (const void *p1, const void *p2)
1329 const_tree n1 = (const_tree) p1;
1330 const_tree n2 = (const_tree) p2;
1331 return SSA_NAME_VAR (n1) == SSA_NAME_VAR (n2);
1334 /* Reduce the number of copies by coalescing variables in the function. Return
1335 a partition map with the resulting coalesces. */
1337 extern var_map
1338 coalesce_ssa_name (void)
1340 unsigned num, x;
1341 tree_live_info_p liveinfo;
1342 ssa_conflicts_p graph;
1343 coalesce_list_p cl;
1344 bitmap used_in_copies = BITMAP_ALLOC (NULL);
1345 var_map map;
1346 unsigned int i;
1347 static htab_t ssa_name_hash;
1349 cl = create_coalesce_list ();
1350 map = create_outofssa_var_map (cl, used_in_copies);
1352 /* We need to coalesce all names originating same SSA_NAME_VAR
1353 so debug info remains undisturbed. */
1354 if (!optimize)
1356 ssa_name_hash = htab_create (10, hash_ssa_name_by_var,
1357 eq_ssa_name_by_var, NULL);
1358 for (i = 1; i < num_ssa_names; i++)
1360 tree a = ssa_name (i);
1362 if (a && SSA_NAME_VAR (a) && !DECL_ARTIFICIAL (SSA_NAME_VAR (a)))
1364 tree *slot = (tree *) htab_find_slot (ssa_name_hash, a, INSERT);
1366 if (!*slot)
1367 *slot = a;
1368 else
1370 add_coalesce (cl, SSA_NAME_VERSION (a), SSA_NAME_VERSION (*slot),
1371 MUST_COALESCE_COST - 1);
1372 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (a));
1373 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (*slot));
1377 htab_delete (ssa_name_hash);
1379 if (dump_file && (dump_flags & TDF_DETAILS))
1380 dump_var_map (dump_file, map);
1382 /* Don't calculate live ranges for variables not in the coalesce list. */
1383 partition_view_bitmap (map, used_in_copies, true);
1384 BITMAP_FREE (used_in_copies);
1386 if (num_var_partitions (map) < 1)
1388 delete_coalesce_list (cl);
1389 return map;
1392 if (dump_file && (dump_flags & TDF_DETAILS))
1393 dump_var_map (dump_file, map);
1395 liveinfo = calculate_live_ranges (map);
1397 if (dump_file && (dump_flags & TDF_DETAILS))
1398 dump_live_info (dump_file, liveinfo, LIVEDUMP_ENTRY);
1400 /* Build a conflict graph. */
1401 graph = build_ssa_conflict_graph (liveinfo);
1402 delete_tree_live_info (liveinfo);
1403 if (dump_file && (dump_flags & TDF_DETAILS))
1404 ssa_conflicts_dump (dump_file, graph);
1406 sort_coalesce_list (cl);
1408 if (dump_file && (dump_flags & TDF_DETAILS))
1410 fprintf (dump_file, "\nAfter sorting:\n");
1411 dump_coalesce_list (dump_file, cl);
1414 /* First, coalesce all live on entry variables to their base variable.
1415 This will ensure the first use is coming from the correct location. */
1417 num = num_var_partitions (map);
1418 for (x = 0 ; x < num; x++)
1420 tree var = partition_to_var (map, x);
1421 tree root;
1423 if (TREE_CODE (var) != SSA_NAME)
1424 continue;
1426 root = SSA_NAME_VAR (var);
1427 if (gimple_default_def (cfun, root) == var)
1429 /* This root variable should have not already been assigned
1430 to another partition which is not coalesced with this one. */
1431 gcc_assert (!var_ann (root)->out_of_ssa_tag);
1433 if (dump_file && (dump_flags & TDF_DETAILS))
1435 print_exprs (dump_file, "Must coalesce ", var,
1436 " with the root variable ", root, ".\n");
1438 change_partition_var (map, root, x);
1442 if (dump_file && (dump_flags & TDF_DETAILS))
1443 dump_var_map (dump_file, map);
1445 /* Now coalesce everything in the list. */
1446 coalesce_partitions (map, graph, cl,
1447 ((dump_flags & TDF_DETAILS) ? dump_file
1448 : NULL));
1450 delete_coalesce_list (cl);
1451 ssa_conflicts_delete (graph);
1453 return map;