2010-07-27 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc/alias-decl.git] / gcc / tree-ssa-coalesce.c
blob5e6854cbaad598458c8f3cb4a91a1411e43cecb3
1 /* Coalesce SSA_NAMES together for the out-of-ssa pass.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, 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 "tree-pretty-print.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 "diagnostic-core.h"
35 #include "toplev.h"
38 /* This set of routines implements a coalesce_list. This is an object which
39 is used to track pairs of ssa_names which are desirable to coalesce
40 together to avoid copies. Costs are associated with each pair, and when
41 all desired information has been collected, the object can be used to
42 order the pairs for processing. */
44 /* This structure defines a pair entry. */
46 typedef struct coalesce_pair
48 int first_element;
49 int second_element;
50 int cost;
51 } * coalesce_pair_p;
52 typedef const struct coalesce_pair *const_coalesce_pair_p;
54 typedef struct cost_one_pair_d
56 int first_element;
57 int second_element;
58 struct cost_one_pair_d *next;
59 } * cost_one_pair_p;
61 /* This structure maintains the list of coalesce pairs. */
63 typedef struct coalesce_list_d
65 htab_t list; /* Hash table. */
66 coalesce_pair_p *sorted; /* List when sorted. */
67 int num_sorted; /* Number in the sorted list. */
68 cost_one_pair_p cost_one_list;/* Single use coalesces with cost 1. */
69 } *coalesce_list_p;
71 #define NO_BEST_COALESCE -1
72 #define MUST_COALESCE_COST INT_MAX
75 /* Return cost of execution of copy instruction with FREQUENCY. */
77 static inline int
78 coalesce_cost (int frequency, bool optimize_for_size)
80 /* Base costs on BB frequencies bounded by 1. */
81 int cost = frequency;
83 if (!cost)
84 cost = 1;
86 if (optimize_for_size)
87 cost = 1;
89 return cost;
93 /* Return the cost of executing a copy instruction in basic block BB. */
95 static inline int
96 coalesce_cost_bb (basic_block bb)
98 return coalesce_cost (bb->frequency, optimize_bb_for_size_p (bb));
102 /* Return the cost of executing a copy instruction on edge E. */
104 static inline int
105 coalesce_cost_edge (edge e)
107 int mult = 1;
109 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
110 if (EDGE_CRITICAL_P (e))
111 mult = 2;
112 if (e->flags & EDGE_ABNORMAL)
113 return MUST_COALESCE_COST;
114 if (e->flags & EDGE_EH)
116 edge e2;
117 edge_iterator ei;
118 FOR_EACH_EDGE (e2, ei, e->dest->preds)
119 if (e2 != e)
121 /* Putting code on EH edge that leads to BB
122 with multiple predecestors imply splitting of
123 edge too. */
124 if (mult < 2)
125 mult = 2;
126 /* If there are multiple EH predecestors, we
127 also copy EH regions and produce separate
128 landing pad. This is expensive. */
129 if (e2->flags & EDGE_EH)
131 mult = 5;
132 break;
137 return coalesce_cost (EDGE_FREQUENCY (e),
138 optimize_edge_for_size_p (e)) * mult;
142 /* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
143 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
144 NO_BEST_COALESCE is returned if there aren't any. */
146 static inline int
147 pop_cost_one_pair (coalesce_list_p cl, int *p1, int *p2)
149 cost_one_pair_p ptr;
151 ptr = cl->cost_one_list;
152 if (!ptr)
153 return NO_BEST_COALESCE;
155 *p1 = ptr->first_element;
156 *p2 = ptr->second_element;
157 cl->cost_one_list = ptr->next;
159 free (ptr);
161 return 1;
164 /* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
165 2 elements via P1 and P2. Their calculated cost is returned by the function.
166 NO_BEST_COALESCE is returned if the coalesce list is empty. */
168 static inline int
169 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
171 coalesce_pair_p node;
172 int ret;
174 if (cl->sorted == NULL)
175 return pop_cost_one_pair (cl, p1, p2);
177 if (cl->num_sorted == 0)
178 return pop_cost_one_pair (cl, p1, p2);
180 node = cl->sorted[--(cl->num_sorted)];
181 *p1 = node->first_element;
182 *p2 = node->second_element;
183 ret = node->cost;
184 free (node);
186 return ret;
190 #define COALESCE_HASH_FN(R1, R2) ((R2) * ((R2) - 1) / 2 + (R1))
192 /* Hash function for coalesce list. Calculate hash for PAIR. */
194 static unsigned int
195 coalesce_pair_map_hash (const void *pair)
197 hashval_t a = (hashval_t)(((const_coalesce_pair_p)pair)->first_element);
198 hashval_t b = (hashval_t)(((const_coalesce_pair_p)pair)->second_element);
200 return COALESCE_HASH_FN (a,b);
204 /* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
205 returning TRUE if the two pairs are equivalent. */
207 static int
208 coalesce_pair_map_eq (const void *pair1, const void *pair2)
210 const_coalesce_pair_p const p1 = (const_coalesce_pair_p) pair1;
211 const_coalesce_pair_p const p2 = (const_coalesce_pair_p) pair2;
213 return (p1->first_element == p2->first_element
214 && p1->second_element == p2->second_element);
218 /* Create a new empty coalesce list object and return it. */
220 static inline coalesce_list_p
221 create_coalesce_list (void)
223 coalesce_list_p list;
224 unsigned size = num_ssa_names * 3;
226 if (size < 40)
227 size = 40;
229 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
230 list->list = htab_create (size, coalesce_pair_map_hash,
231 coalesce_pair_map_eq, NULL);
232 list->sorted = NULL;
233 list->num_sorted = 0;
234 list->cost_one_list = NULL;
235 return list;
239 /* Delete coalesce list CL. */
241 static inline void
242 delete_coalesce_list (coalesce_list_p cl)
244 gcc_assert (cl->cost_one_list == NULL);
245 htab_delete (cl->list);
246 if (cl->sorted)
247 free (cl->sorted);
248 gcc_assert (cl->num_sorted == 0);
249 free (cl);
253 /* Find a matching coalesce pair object in CL for the pair P1 and P2. If
254 one isn't found, return NULL if CREATE is false, otherwise create a new
255 coalesce pair object and return it. */
257 static coalesce_pair_p
258 find_coalesce_pair (coalesce_list_p cl, int p1, int p2, bool create)
260 struct coalesce_pair p;
261 void **slot;
262 unsigned int hash;
264 /* Normalize so that p1 is the smaller value. */
265 if (p2 < p1)
267 p.first_element = p2;
268 p.second_element = p1;
270 else
272 p.first_element = p1;
273 p.second_element = p2;
276 hash = coalesce_pair_map_hash (&p);
277 slot = htab_find_slot_with_hash (cl->list, &p, hash,
278 create ? INSERT : NO_INSERT);
279 if (!slot)
280 return NULL;
282 if (!*slot)
284 struct coalesce_pair * pair = XNEW (struct coalesce_pair);
285 gcc_assert (cl->sorted == NULL);
286 pair->first_element = p.first_element;
287 pair->second_element = p.second_element;
288 pair->cost = 0;
289 *slot = (void *)pair;
292 return (struct coalesce_pair *) *slot;
295 static inline void
296 add_cost_one_coalesce (coalesce_list_p cl, int p1, int p2)
298 cost_one_pair_p pair;
300 pair = XNEW (struct cost_one_pair_d);
301 pair->first_element = p1;
302 pair->second_element = p2;
303 pair->next = cl->cost_one_list;
304 cl->cost_one_list = pair;
308 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
310 static inline void
311 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
313 coalesce_pair_p node;
315 gcc_assert (cl->sorted == NULL);
316 if (p1 == p2)
317 return;
319 node = find_coalesce_pair (cl, p1, p2, true);
321 /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way. */
322 if (node->cost < MUST_COALESCE_COST - 1)
324 if (value < MUST_COALESCE_COST - 1)
325 node->cost += value;
326 else
327 node->cost = value;
332 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
334 static int
335 compare_pairs (const void *p1, const void *p2)
337 const_coalesce_pair_p const *const pp1 = (const_coalesce_pair_p const *) p1;
338 const_coalesce_pair_p const *const pp2 = (const_coalesce_pair_p const *) p2;
339 int result;
341 result = (* pp1)->cost - (* pp2)->cost;
342 /* Since qsort does not guarantee stability we use the elements
343 as a secondary key. This provides us with independence from
344 the host's implementation of the sorting algorithm. */
345 if (result == 0)
347 result = (* pp2)->first_element - (* pp1)->first_element;
348 if (result == 0)
349 result = (* pp2)->second_element - (* pp1)->second_element;
352 return result;
356 /* Return the number of unique coalesce pairs in CL. */
358 static inline int
359 num_coalesce_pairs (coalesce_list_p cl)
361 return htab_elements (cl->list);
365 /* Iterator over hash table pairs. */
366 typedef struct
368 htab_iterator hti;
369 } coalesce_pair_iterator;
372 /* Return first partition pair from list CL, initializing iterator ITER. */
374 static inline coalesce_pair_p
375 first_coalesce_pair (coalesce_list_p cl, coalesce_pair_iterator *iter)
377 coalesce_pair_p pair;
379 pair = (coalesce_pair_p) first_htab_element (&(iter->hti), cl->list);
380 return pair;
384 /* Return TRUE if there are no more partitions in for ITER to process. */
386 static inline bool
387 end_coalesce_pair_p (coalesce_pair_iterator *iter)
389 return end_htab_p (&(iter->hti));
393 /* Return the next partition pair to be visited by ITER. */
395 static inline coalesce_pair_p
396 next_coalesce_pair (coalesce_pair_iterator *iter)
398 coalesce_pair_p pair;
400 pair = (coalesce_pair_p) next_htab_element (&(iter->hti));
401 return pair;
405 /* Iterate over CL using ITER, returning values in PAIR. */
407 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
408 for ((PAIR) = first_coalesce_pair ((CL), &(ITER)); \
409 !end_coalesce_pair_p (&(ITER)); \
410 (PAIR) = next_coalesce_pair (&(ITER)))
413 /* Prepare CL for removal of preferred pairs. When finished they are sorted
414 in order from most important coalesce to least important. */
416 static void
417 sort_coalesce_list (coalesce_list_p cl)
419 unsigned x, num;
420 coalesce_pair_p p;
421 coalesce_pair_iterator ppi;
423 gcc_assert (cl->sorted == NULL);
425 num = num_coalesce_pairs (cl);
426 cl->num_sorted = num;
427 if (num == 0)
428 return;
430 /* Allocate a vector for the pair pointers. */
431 cl->sorted = XNEWVEC (coalesce_pair_p, num);
433 /* Populate the vector with pointers to the pairs. */
434 x = 0;
435 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
436 cl->sorted[x++] = p;
437 gcc_assert (x == num);
439 /* Already sorted. */
440 if (num == 1)
441 return;
443 /* If there are only 2, just pick swap them if the order isn't correct. */
444 if (num == 2)
446 if (cl->sorted[0]->cost > cl->sorted[1]->cost)
448 p = cl->sorted[0];
449 cl->sorted[0] = cl->sorted[1];
450 cl->sorted[1] = p;
452 return;
455 /* Only call qsort if there are more than 2 items. */
456 if (num > 2)
457 qsort (cl->sorted, num, sizeof (coalesce_pair_p), compare_pairs);
461 /* Send debug info for coalesce list CL to file F. */
463 static void
464 dump_coalesce_list (FILE *f, coalesce_list_p cl)
466 coalesce_pair_p node;
467 coalesce_pair_iterator ppi;
468 int x;
469 tree var;
471 if (cl->sorted == NULL)
473 fprintf (f, "Coalesce List:\n");
474 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
476 tree var1 = ssa_name (node->first_element);
477 tree var2 = ssa_name (node->second_element);
478 print_generic_expr (f, var1, TDF_SLIM);
479 fprintf (f, " <-> ");
480 print_generic_expr (f, var2, TDF_SLIM);
481 fprintf (f, " (%1d), ", node->cost);
482 fprintf (f, "\n");
485 else
487 fprintf (f, "Sorted Coalesce list:\n");
488 for (x = cl->num_sorted - 1 ; x >=0; x--)
490 node = cl->sorted[x];
491 fprintf (f, "(%d) ", node->cost);
492 var = ssa_name (node->first_element);
493 print_generic_expr (f, var, TDF_SLIM);
494 fprintf (f, " <-> ");
495 var = ssa_name (node->second_element);
496 print_generic_expr (f, var, TDF_SLIM);
497 fprintf (f, "\n");
503 /* This represents a conflict graph. Implemented as an array of bitmaps.
504 A full matrix is used for conflicts rather than just upper triangular form.
505 this make sit much simpler and faster to perform conflict merges. */
507 typedef struct ssa_conflicts_d
509 unsigned size;
510 bitmap *conflicts;
511 } * ssa_conflicts_p;
514 /* Return an empty new conflict graph for SIZE elements. */
516 static inline ssa_conflicts_p
517 ssa_conflicts_new (unsigned size)
519 ssa_conflicts_p ptr;
521 ptr = XNEW (struct ssa_conflicts_d);
522 ptr->conflicts = XCNEWVEC (bitmap, size);
523 ptr->size = size;
524 return ptr;
528 /* Free storage for conflict graph PTR. */
530 static inline void
531 ssa_conflicts_delete (ssa_conflicts_p ptr)
533 unsigned x;
534 for (x = 0; x < ptr->size; x++)
535 if (ptr->conflicts[x])
536 BITMAP_FREE (ptr->conflicts[x]);
538 free (ptr->conflicts);
539 free (ptr);
543 /* Test if elements X and Y conflict in graph PTR. */
545 static inline bool
546 ssa_conflicts_test_p (ssa_conflicts_p ptr, unsigned x, unsigned y)
548 bitmap b;
550 #ifdef ENABLE_CHECKING
551 gcc_assert (x < ptr->size);
552 gcc_assert (y < ptr->size);
553 gcc_assert (x != y);
554 #endif
556 b = ptr->conflicts[x];
557 if (b)
558 /* Avoid the lookup if Y has no conflicts. */
559 return ptr->conflicts[y] ? bitmap_bit_p (b, y) : false;
560 else
561 return false;
565 /* Add a conflict with Y to the bitmap for X in graph PTR. */
567 static inline void
568 ssa_conflicts_add_one (ssa_conflicts_p ptr, unsigned x, unsigned y)
570 /* If there are no conflicts yet, allocate the bitmap and set bit. */
571 if (!ptr->conflicts[x])
572 ptr->conflicts[x] = BITMAP_ALLOC (NULL);
573 bitmap_set_bit (ptr->conflicts[x], y);
577 /* Add conflicts between X and Y in graph PTR. */
579 static inline void
580 ssa_conflicts_add (ssa_conflicts_p ptr, unsigned x, unsigned y)
582 #ifdef ENABLE_CHECKING
583 gcc_assert (x < ptr->size);
584 gcc_assert (y < ptr->size);
585 gcc_assert (x != y);
586 #endif
587 ssa_conflicts_add_one (ptr, x, y);
588 ssa_conflicts_add_one (ptr, y, x);
592 /* Merge all Y's conflict into X in graph PTR. */
594 static inline void
595 ssa_conflicts_merge (ssa_conflicts_p ptr, unsigned x, unsigned y)
597 unsigned z;
598 bitmap_iterator bi;
600 gcc_assert (x != y);
601 if (!(ptr->conflicts[y]))
602 return;
604 /* Add a conflict between X and every one Y has. If the bitmap doesn't
605 exist, then it has already been coalesced, and we don't need to add a
606 conflict. */
607 EXECUTE_IF_SET_IN_BITMAP (ptr->conflicts[y], 0, z, bi)
608 if (ptr->conflicts[z])
609 bitmap_set_bit (ptr->conflicts[z], x);
611 if (ptr->conflicts[x])
613 /* If X has conflicts, add Y's to X. */
614 bitmap_ior_into (ptr->conflicts[x], ptr->conflicts[y]);
615 BITMAP_FREE (ptr->conflicts[y]);
617 else
619 /* If X has no conflicts, simply use Y's. */
620 ptr->conflicts[x] = ptr->conflicts[y];
621 ptr->conflicts[y] = NULL;
626 /* Dump a conflicts graph. */
628 static void
629 ssa_conflicts_dump (FILE *file, ssa_conflicts_p ptr)
631 unsigned x;
633 fprintf (file, "\nConflict graph:\n");
635 for (x = 0; x < ptr->size; x++)
636 if (ptr->conflicts[x])
638 fprintf (dump_file, "%d: ", x);
639 dump_bitmap (file, ptr->conflicts[x]);
644 /* This structure is used to efficiently record the current status of live
645 SSA_NAMES when building a conflict graph.
646 LIVE_BASE_VAR has a bit set for each base variable which has at least one
647 ssa version live.
648 LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
649 index, and is used to track what partitions of each base variable are
650 live. This makes it easy to add conflicts between just live partitions
651 with the same base variable.
652 The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
653 marked as being live. This delays clearing of these bitmaps until
654 they are actually needed again. */
656 typedef struct live_track_d
658 bitmap live_base_var; /* Indicates if a basevar is live. */
659 bitmap *live_base_partitions; /* Live partitions for each basevar. */
660 var_map map; /* Var_map being used for partition mapping. */
661 } * live_track_p;
664 /* This routine will create a new live track structure based on the partitions
665 in MAP. */
667 static live_track_p
668 new_live_track (var_map map)
670 live_track_p ptr;
671 int lim, x;
673 /* Make sure there is a partition view in place. */
674 gcc_assert (map->partition_to_base_index != NULL);
676 ptr = (live_track_p) xmalloc (sizeof (struct live_track_d));
677 ptr->map = map;
678 lim = num_basevars (map);
679 ptr->live_base_partitions = (bitmap *) xmalloc(sizeof (bitmap *) * lim);
680 ptr->live_base_var = BITMAP_ALLOC (NULL);
681 for (x = 0; x < lim; x++)
682 ptr->live_base_partitions[x] = BITMAP_ALLOC (NULL);
683 return ptr;
687 /* This routine will free the memory associated with PTR. */
689 static void
690 delete_live_track (live_track_p ptr)
692 int x, lim;
694 lim = num_basevars (ptr->map);
695 for (x = 0; x < lim; x++)
696 BITMAP_FREE (ptr->live_base_partitions[x]);
697 BITMAP_FREE (ptr->live_base_var);
698 free (ptr->live_base_partitions);
699 free (ptr);
703 /* This function will remove PARTITION from the live list in PTR. */
705 static inline void
706 live_track_remove_partition (live_track_p ptr, int partition)
708 int root;
710 root = basevar_index (ptr->map, partition);
711 bitmap_clear_bit (ptr->live_base_partitions[root], partition);
712 /* If the element list is empty, make the base variable not live either. */
713 if (bitmap_empty_p (ptr->live_base_partitions[root]))
714 bitmap_clear_bit (ptr->live_base_var, root);
718 /* This function will adds PARTITION to the live list in PTR. */
720 static inline void
721 live_track_add_partition (live_track_p ptr, int partition)
723 int root;
725 root = basevar_index (ptr->map, partition);
726 /* If this base var wasn't live before, it is now. Clear the element list
727 since it was delayed until needed. */
728 if (!bitmap_bit_p (ptr->live_base_var, root))
730 bitmap_set_bit (ptr->live_base_var, root);
731 bitmap_clear (ptr->live_base_partitions[root]);
733 bitmap_set_bit (ptr->live_base_partitions[root], partition);
738 /* Clear the live bit for VAR in PTR. */
740 static inline void
741 live_track_clear_var (live_track_p ptr, tree var)
743 int p;
745 p = var_to_partition (ptr->map, var);
746 if (p != NO_PARTITION)
747 live_track_remove_partition (ptr, p);
751 /* Return TRUE if VAR is live in PTR. */
753 static inline bool
754 live_track_live_p (live_track_p ptr, tree var)
756 int p, root;
758 p = var_to_partition (ptr->map, var);
759 if (p != NO_PARTITION)
761 root = basevar_index (ptr->map, p);
762 if (bitmap_bit_p (ptr->live_base_var, root))
763 return bitmap_bit_p (ptr->live_base_partitions[root], p);
765 return false;
769 /* This routine will add USE to PTR. USE will be marked as live in both the
770 ssa live map and the live bitmap for the root of USE. */
772 static inline void
773 live_track_process_use (live_track_p ptr, tree use)
775 int p;
777 p = var_to_partition (ptr->map, use);
778 if (p == NO_PARTITION)
779 return;
781 /* Mark as live in the appropriate live list. */
782 live_track_add_partition (ptr, p);
786 /* This routine will process a DEF in PTR. DEF will be removed from the live
787 lists, and if there are any other live partitions with the same base
788 variable, conflicts will be added to GRAPH. */
790 static inline void
791 live_track_process_def (live_track_p ptr, tree def, ssa_conflicts_p graph)
793 int p, root;
794 bitmap b;
795 unsigned x;
796 bitmap_iterator bi;
798 p = var_to_partition (ptr->map, def);
799 if (p == NO_PARTITION)
800 return;
802 /* Clear the liveness bit. */
803 live_track_remove_partition (ptr, p);
805 /* If the bitmap isn't empty now, conflicts need to be added. */
806 root = basevar_index (ptr->map, p);
807 if (bitmap_bit_p (ptr->live_base_var, root))
809 b = ptr->live_base_partitions[root];
810 EXECUTE_IF_SET_IN_BITMAP (b, 0, x, bi)
811 ssa_conflicts_add (graph, p, x);
816 /* Initialize PTR with the partitions set in INIT. */
818 static inline void
819 live_track_init (live_track_p ptr, bitmap init)
821 unsigned p;
822 bitmap_iterator bi;
824 /* Mark all live on exit partitions. */
825 EXECUTE_IF_SET_IN_BITMAP (init, 0, p, bi)
826 live_track_add_partition (ptr, p);
830 /* This routine will clear all live partitions in PTR. */
832 static inline void
833 live_track_clear_base_vars (live_track_p ptr)
835 /* Simply clear the live base list. Anything marked as live in the element
836 lists will be cleared later if/when the base variable ever comes alive
837 again. */
838 bitmap_clear (ptr->live_base_var);
842 /* Build a conflict graph based on LIVEINFO. Any partitions which are in the
843 partition view of the var_map liveinfo is based on get entries in the
844 conflict graph. Only conflicts between ssa_name partitions with the same
845 base variable are added. */
847 static ssa_conflicts_p
848 build_ssa_conflict_graph (tree_live_info_p liveinfo)
850 ssa_conflicts_p graph;
851 var_map map;
852 basic_block bb;
853 ssa_op_iter iter;
854 live_track_p live;
856 map = live_var_map (liveinfo);
857 graph = ssa_conflicts_new (num_var_partitions (map));
859 live = new_live_track (map);
861 FOR_EACH_BB (bb)
863 gimple_stmt_iterator gsi;
865 /* Start with live on exit temporaries. */
866 live_track_init (live, live_on_exit (liveinfo, bb));
868 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
870 tree var;
871 gimple stmt = gsi_stmt (gsi);
873 /* A copy between 2 partitions does not introduce an interference
874 by itself. If they did, you would never be able to coalesce
875 two things which are copied. If the two variables really do
876 conflict, they will conflict elsewhere in the program.
878 This is handled by simply removing the SRC of the copy from the
879 live list, and processing the stmt normally. */
880 if (is_gimple_assign (stmt))
882 tree lhs = gimple_assign_lhs (stmt);
883 tree rhs1 = gimple_assign_rhs1 (stmt);
884 if (gimple_assign_copy_p (stmt)
885 && TREE_CODE (lhs) == SSA_NAME
886 && TREE_CODE (rhs1) == SSA_NAME)
887 live_track_clear_var (live, rhs1);
889 else if (is_gimple_debug (stmt))
890 continue;
892 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
893 live_track_process_def (live, var, graph);
895 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
896 live_track_process_use (live, var);
899 /* If result of a PHI is unused, looping over the statements will not
900 record any conflicts since the def was never live. Since the PHI node
901 is going to be translated out of SSA form, it will insert a copy.
902 There must be a conflict recorded between the result of the PHI and
903 any variables that are live. Otherwise the out-of-ssa translation
904 may create incorrect code. */
905 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
907 gimple phi = gsi_stmt (gsi);
908 tree result = PHI_RESULT (phi);
909 if (live_track_live_p (live, result))
910 live_track_process_def (live, result, graph);
913 live_track_clear_base_vars (live);
916 delete_live_track (live);
917 return graph;
921 /* Shortcut routine to print messages to file F of the form:
922 "STR1 EXPR1 STR2 EXPR2 STR3." */
924 static inline void
925 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
926 tree expr2, const char *str3)
928 fprintf (f, "%s", str1);
929 print_generic_expr (f, expr1, TDF_SLIM);
930 fprintf (f, "%s", str2);
931 print_generic_expr (f, expr2, TDF_SLIM);
932 fprintf (f, "%s", str3);
936 /* Called if a coalesce across and abnormal edge cannot be performed. PHI is
937 the phi node at fault, I is the argument index at fault. A message is
938 printed and compilation is then terminated. */
940 static inline void
941 abnormal_corrupt (gimple phi, int i)
943 edge e = gimple_phi_arg_edge (phi, i);
944 tree res = gimple_phi_result (phi);
945 tree arg = gimple_phi_arg_def (phi, i);
947 fprintf (stderr, " Corrupt SSA across abnormal edge BB%d->BB%d\n",
948 e->src->index, e->dest->index);
949 fprintf (stderr, "Argument %d (", i);
950 print_generic_expr (stderr, arg, TDF_SLIM);
951 if (TREE_CODE (arg) != SSA_NAME)
952 fprintf (stderr, ") is not an SSA_NAME.\n");
953 else
955 gcc_assert (SSA_NAME_VAR (res) != SSA_NAME_VAR (arg));
956 fprintf (stderr, ") does not have the same base variable as the result ");
957 print_generic_stmt (stderr, res, TDF_SLIM);
960 internal_error ("SSA corruption");
964 /* Print a failure to coalesce a MUST_COALESCE pair X and Y. */
966 static inline void
967 fail_abnormal_edge_coalesce (int x, int y)
969 fprintf (stderr, "\nUnable to coalesce ssa_names %d and %d",x, y);
970 fprintf (stderr, " which are marked as MUST COALESCE.\n");
971 print_generic_expr (stderr, ssa_name (x), TDF_SLIM);
972 fprintf (stderr, " and ");
973 print_generic_stmt (stderr, ssa_name (y), TDF_SLIM);
975 internal_error ("SSA corruption");
979 /* This function creates a var_map for the current function as well as creating
980 a coalesce list for use later in the out of ssa process. */
982 static var_map
983 create_outofssa_var_map (coalesce_list_p cl, bitmap used_in_copy)
985 gimple_stmt_iterator gsi;
986 basic_block bb;
987 tree var;
988 gimple stmt;
989 tree first;
990 var_map map;
991 ssa_op_iter iter;
992 int v1, v2, cost;
993 unsigned i;
995 #ifdef ENABLE_CHECKING
996 bitmap used_in_real_ops;
997 bitmap used_in_virtual_ops;
999 used_in_real_ops = BITMAP_ALLOC (NULL);
1000 used_in_virtual_ops = BITMAP_ALLOC (NULL);
1001 #endif
1003 map = init_var_map (num_ssa_names);
1005 FOR_EACH_BB (bb)
1007 tree arg;
1009 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1011 gimple phi = gsi_stmt (gsi);
1012 size_t i;
1013 int ver;
1014 tree res;
1015 bool saw_copy = false;
1017 res = gimple_phi_result (phi);
1018 ver = SSA_NAME_VERSION (res);
1019 register_ssa_partition (map, res);
1021 /* Register ssa_names and coalesces between the args and the result
1022 of all PHI. */
1023 for (i = 0; i < gimple_phi_num_args (phi); i++)
1025 edge e = gimple_phi_arg_edge (phi, i);
1026 arg = PHI_ARG_DEF (phi, i);
1027 if (TREE_CODE (arg) == SSA_NAME)
1028 register_ssa_partition (map, arg);
1029 if (TREE_CODE (arg) == SSA_NAME
1030 && SSA_NAME_VAR (arg) == SSA_NAME_VAR (res))
1032 saw_copy = true;
1033 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (arg));
1034 if ((e->flags & EDGE_ABNORMAL) == 0)
1036 int cost = coalesce_cost_edge (e);
1037 if (cost == 1 && has_single_use (arg))
1038 add_cost_one_coalesce (cl, ver, SSA_NAME_VERSION (arg));
1039 else
1040 add_coalesce (cl, ver, SSA_NAME_VERSION (arg), cost);
1043 else
1044 if (e->flags & EDGE_ABNORMAL)
1045 abnormal_corrupt (phi, i);
1047 if (saw_copy)
1048 bitmap_set_bit (used_in_copy, ver);
1051 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1053 stmt = gsi_stmt (gsi);
1055 if (is_gimple_debug (stmt))
1056 continue;
1058 /* Register USE and DEF operands in each statement. */
1059 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
1060 register_ssa_partition (map, var);
1062 /* Check for copy coalesces. */
1063 switch (gimple_code (stmt))
1065 case GIMPLE_ASSIGN:
1067 tree lhs = gimple_assign_lhs (stmt);
1068 tree rhs1 = gimple_assign_rhs1 (stmt);
1070 if (gimple_assign_copy_p (stmt)
1071 && TREE_CODE (lhs) == SSA_NAME
1072 && TREE_CODE (rhs1) == SSA_NAME
1073 && SSA_NAME_VAR (lhs) == SSA_NAME_VAR (rhs1))
1075 v1 = SSA_NAME_VERSION (lhs);
1076 v2 = SSA_NAME_VERSION (rhs1);
1077 cost = coalesce_cost_bb (bb);
1078 add_coalesce (cl, v1, v2, cost);
1079 bitmap_set_bit (used_in_copy, v1);
1080 bitmap_set_bit (used_in_copy, v2);
1083 break;
1085 case GIMPLE_ASM:
1087 unsigned long noutputs, i;
1088 unsigned long ninputs;
1089 tree *outputs, link;
1090 noutputs = gimple_asm_noutputs (stmt);
1091 ninputs = gimple_asm_ninputs (stmt);
1092 outputs = (tree *) alloca (noutputs * sizeof (tree));
1093 for (i = 0; i < noutputs; ++i) {
1094 link = gimple_asm_output_op (stmt, i);
1095 outputs[i] = TREE_VALUE (link);
1098 for (i = 0; i < ninputs; ++i)
1100 const char *constraint;
1101 tree input;
1102 char *end;
1103 unsigned long match;
1105 link = gimple_asm_input_op (stmt, i);
1106 constraint
1107 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1108 input = TREE_VALUE (link);
1110 if (TREE_CODE (input) != SSA_NAME)
1111 continue;
1113 match = strtoul (constraint, &end, 10);
1114 if (match >= noutputs || end == constraint)
1115 continue;
1117 if (TREE_CODE (outputs[match]) != SSA_NAME)
1118 continue;
1120 v1 = SSA_NAME_VERSION (outputs[match]);
1121 v2 = SSA_NAME_VERSION (input);
1123 if (SSA_NAME_VAR (outputs[match]) == SSA_NAME_VAR (input))
1125 cost = coalesce_cost (REG_BR_PROB_BASE,
1126 optimize_bb_for_size_p (bb));
1127 add_coalesce (cl, v1, v2, cost);
1128 bitmap_set_bit (used_in_copy, v1);
1129 bitmap_set_bit (used_in_copy, v2);
1132 break;
1135 default:
1136 break;
1139 #ifdef ENABLE_CHECKING
1140 /* Mark real uses and defs. */
1141 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
1142 bitmap_set_bit (used_in_real_ops, DECL_UID (SSA_NAME_VAR (var)));
1144 /* Validate that virtual ops don't get used in funny ways. */
1145 if (gimple_vuse (stmt))
1146 bitmap_set_bit (used_in_virtual_ops,
1147 DECL_UID (SSA_NAME_VAR (gimple_vuse (stmt))));
1148 #endif /* ENABLE_CHECKING */
1152 /* Now process result decls and live on entry variables for entry into
1153 the coalesce list. */
1154 first = NULL_TREE;
1155 for (i = 1; i < num_ssa_names; i++)
1157 var = ssa_name (i);
1158 if (var != NULL_TREE && is_gimple_reg (var))
1160 /* Add coalesces between all the result decls. */
1161 if (TREE_CODE (SSA_NAME_VAR (var)) == RESULT_DECL)
1163 if (first == NULL_TREE)
1164 first = var;
1165 else
1167 gcc_assert (SSA_NAME_VAR (var) == SSA_NAME_VAR (first));
1168 v1 = SSA_NAME_VERSION (first);
1169 v2 = SSA_NAME_VERSION (var);
1170 bitmap_set_bit (used_in_copy, v1);
1171 bitmap_set_bit (used_in_copy, v2);
1172 cost = coalesce_cost_bb (EXIT_BLOCK_PTR);
1173 add_coalesce (cl, v1, v2, cost);
1176 /* Mark any default_def variables as being in the coalesce list
1177 since they will have to be coalesced with the base variable. If
1178 not marked as present, they won't be in the coalesce view. */
1179 if (gimple_default_def (cfun, SSA_NAME_VAR (var)) == var
1180 && !has_zero_uses (var))
1181 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1185 #if defined ENABLE_CHECKING
1187 unsigned i;
1188 bitmap both = BITMAP_ALLOC (NULL);
1189 bitmap_and (both, used_in_real_ops, used_in_virtual_ops);
1190 if (!bitmap_empty_p (both))
1192 bitmap_iterator bi;
1194 EXECUTE_IF_SET_IN_BITMAP (both, 0, i, bi)
1195 fprintf (stderr, "Variable %s used in real and virtual operands\n",
1196 get_name (referenced_var (i)));
1197 internal_error ("SSA corruption");
1200 BITMAP_FREE (used_in_real_ops);
1201 BITMAP_FREE (used_in_virtual_ops);
1202 BITMAP_FREE (both);
1204 #endif
1206 return map;
1210 /* Attempt to coalesce ssa versions X and Y together using the partition
1211 mapping in MAP and checking conflicts in GRAPH. Output any debug info to
1212 DEBUG, if it is nun-NULL. */
1214 static inline bool
1215 attempt_coalesce (var_map map, ssa_conflicts_p graph, int x, int y,
1216 FILE *debug)
1218 int z;
1219 tree var1, var2;
1220 int p1, p2;
1222 p1 = var_to_partition (map, ssa_name (x));
1223 p2 = var_to_partition (map, ssa_name (y));
1225 if (debug)
1227 fprintf (debug, "(%d)", x);
1228 print_generic_expr (debug, partition_to_var (map, p1), TDF_SLIM);
1229 fprintf (debug, " & (%d)", y);
1230 print_generic_expr (debug, partition_to_var (map, p2), TDF_SLIM);
1233 if (p1 == p2)
1235 if (debug)
1236 fprintf (debug, ": Already Coalesced.\n");
1237 return true;
1240 if (debug)
1241 fprintf (debug, " [map: %d, %d] ", p1, p2);
1244 if (!ssa_conflicts_test_p (graph, p1, p2))
1246 var1 = partition_to_var (map, p1);
1247 var2 = partition_to_var (map, p2);
1248 z = var_union (map, var1, var2);
1249 if (z == NO_PARTITION)
1251 if (debug)
1252 fprintf (debug, ": Unable to perform partition union.\n");
1253 return false;
1256 /* z is the new combined partition. Remove the other partition from
1257 the list, and merge the conflicts. */
1258 if (z == p1)
1259 ssa_conflicts_merge (graph, p1, p2);
1260 else
1261 ssa_conflicts_merge (graph, p2, p1);
1263 if (debug)
1264 fprintf (debug, ": Success -> %d\n", z);
1265 return true;
1268 if (debug)
1269 fprintf (debug, ": Fail due to conflict\n");
1271 return false;
1275 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1276 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1278 static void
1279 coalesce_partitions (var_map map, ssa_conflicts_p graph, coalesce_list_p cl,
1280 FILE *debug)
1282 int x = 0, y = 0;
1283 tree var1, var2;
1284 int cost;
1285 basic_block bb;
1286 edge e;
1287 edge_iterator ei;
1289 /* First, coalesce all the copies across abnormal edges. These are not placed
1290 in the coalesce list because they do not need to be sorted, and simply
1291 consume extra memory/compilation time in large programs. */
1293 FOR_EACH_BB (bb)
1295 FOR_EACH_EDGE (e, ei, bb->preds)
1296 if (e->flags & EDGE_ABNORMAL)
1298 gimple_stmt_iterator gsi;
1299 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1300 gsi_next (&gsi))
1302 gimple phi = gsi_stmt (gsi);
1303 tree res = PHI_RESULT (phi);
1304 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1305 int v1 = SSA_NAME_VERSION (res);
1306 int v2 = SSA_NAME_VERSION (arg);
1308 if (SSA_NAME_VAR (arg) != SSA_NAME_VAR (res))
1309 abnormal_corrupt (phi, e->dest_idx);
1311 if (debug)
1312 fprintf (debug, "Abnormal coalesce: ");
1314 if (!attempt_coalesce (map, graph, v1, v2, debug))
1315 fail_abnormal_edge_coalesce (v1, v2);
1320 /* Now process the items in the coalesce list. */
1322 while ((cost = pop_best_coalesce (cl, &x, &y)) != NO_BEST_COALESCE)
1324 var1 = ssa_name (x);
1325 var2 = ssa_name (y);
1327 /* Assert the coalesces have the same base variable. */
1328 gcc_assert (SSA_NAME_VAR (var1) == SSA_NAME_VAR (var2));
1330 if (debug)
1331 fprintf (debug, "Coalesce list: ");
1332 attempt_coalesce (map, graph, x, y, debug);
1336 /* Returns a hash code for P. */
1338 static hashval_t
1339 hash_ssa_name_by_var (const void *p)
1341 const_tree n = (const_tree) p;
1342 return (hashval_t) htab_hash_pointer (SSA_NAME_VAR (n));
1345 /* Returns nonzero if P1 and P2 are equal. */
1347 static int
1348 eq_ssa_name_by_var (const void *p1, const void *p2)
1350 const_tree n1 = (const_tree) p1;
1351 const_tree n2 = (const_tree) p2;
1352 return SSA_NAME_VAR (n1) == SSA_NAME_VAR (n2);
1355 /* Reduce the number of copies by coalescing variables in the function. Return
1356 a partition map with the resulting coalesces. */
1358 extern var_map
1359 coalesce_ssa_name (void)
1361 tree_live_info_p liveinfo;
1362 ssa_conflicts_p graph;
1363 coalesce_list_p cl;
1364 bitmap used_in_copies = BITMAP_ALLOC (NULL);
1365 var_map map;
1366 unsigned int i;
1367 static htab_t ssa_name_hash;
1369 cl = create_coalesce_list ();
1370 map = create_outofssa_var_map (cl, used_in_copies);
1372 /* We need to coalesce all names originating same SSA_NAME_VAR
1373 so debug info remains undisturbed. */
1374 if (!optimize)
1376 ssa_name_hash = htab_create (10, hash_ssa_name_by_var,
1377 eq_ssa_name_by_var, NULL);
1378 for (i = 1; i < num_ssa_names; i++)
1380 tree a = ssa_name (i);
1382 if (a
1383 && SSA_NAME_VAR (a)
1384 && !DECL_ARTIFICIAL (SSA_NAME_VAR (a))
1385 && (!has_zero_uses (a) || !SSA_NAME_IS_DEFAULT_DEF (a)))
1387 tree *slot = (tree *) htab_find_slot (ssa_name_hash, a, INSERT);
1389 if (!*slot)
1390 *slot = a;
1391 else
1393 add_coalesce (cl, SSA_NAME_VERSION (a), SSA_NAME_VERSION (*slot),
1394 MUST_COALESCE_COST - 1);
1395 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (a));
1396 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (*slot));
1400 htab_delete (ssa_name_hash);
1402 if (dump_file && (dump_flags & TDF_DETAILS))
1403 dump_var_map (dump_file, map);
1405 /* Don't calculate live ranges for variables not in the coalesce list. */
1406 partition_view_bitmap (map, used_in_copies, true);
1407 BITMAP_FREE (used_in_copies);
1409 if (num_var_partitions (map) < 1)
1411 delete_coalesce_list (cl);
1412 return map;
1415 if (dump_file && (dump_flags & TDF_DETAILS))
1416 dump_var_map (dump_file, map);
1418 liveinfo = calculate_live_ranges (map);
1420 if (dump_file && (dump_flags & TDF_DETAILS))
1421 dump_live_info (dump_file, liveinfo, LIVEDUMP_ENTRY);
1423 /* Build a conflict graph. */
1424 graph = build_ssa_conflict_graph (liveinfo);
1425 delete_tree_live_info (liveinfo);
1426 if (dump_file && (dump_flags & TDF_DETAILS))
1427 ssa_conflicts_dump (dump_file, graph);
1429 sort_coalesce_list (cl);
1431 if (dump_file && (dump_flags & TDF_DETAILS))
1433 fprintf (dump_file, "\nAfter sorting:\n");
1434 dump_coalesce_list (dump_file, cl);
1437 /* First, coalesce all live on entry variables to their base variable.
1438 This will ensure the first use is coming from the correct location. */
1440 if (dump_file && (dump_flags & TDF_DETAILS))
1441 dump_var_map (dump_file, map);
1443 /* Now coalesce everything in the list. */
1444 coalesce_partitions (map, graph, cl,
1445 ((dump_flags & TDF_DETAILS) ? dump_file
1446 : NULL));
1448 delete_coalesce_list (cl);
1449 ssa_conflicts_delete (graph);
1451 return map;