2013-04-26 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / tree-ssa-coalesce.c
blob354b5f182a8bb28cef73d8dffe025fb4ebfb37ae
1 /* Coalesce SSA_NAMES together for the out-of-ssa pass.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "tree-pretty-print.h"
28 #include "bitmap.h"
29 #include "dumpfile.h"
30 #include "tree-flow.h"
31 #include "hash-table.h"
32 #include "tree-ssa-live.h"
33 #include "diagnostic-core.h"
36 /* This set of routines implements a coalesce_list. This is an object which
37 is used to track pairs of ssa_names which are desirable to coalesce
38 together to avoid copies. Costs are associated with each pair, and when
39 all desired information has been collected, the object can be used to
40 order the pairs for processing. */
42 /* This structure defines a pair entry. */
44 typedef struct coalesce_pair
46 int first_element;
47 int second_element;
48 int cost;
49 } * coalesce_pair_p;
50 typedef const struct coalesce_pair *const_coalesce_pair_p;
52 /* Coalesce pair hashtable helpers. */
54 struct coalesce_pair_hasher : typed_noop_remove <coalesce_pair>
56 typedef coalesce_pair value_type;
57 typedef coalesce_pair compare_type;
58 static inline hashval_t hash (const value_type *);
59 static inline bool equal (const value_type *, const compare_type *);
62 /* Hash function for coalesce list. Calculate hash for PAIR. */
64 inline hashval_t
65 coalesce_pair_hasher::hash (const value_type *pair)
67 hashval_t a = (hashval_t)(pair->first_element);
68 hashval_t b = (hashval_t)(pair->second_element);
70 return b * (b - 1) / 2 + a;
73 /* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
74 returning TRUE if the two pairs are equivalent. */
76 inline bool
77 coalesce_pair_hasher::equal (const value_type *p1, const compare_type *p2)
79 return (p1->first_element == p2->first_element
80 && p1->second_element == p2->second_element);
83 typedef hash_table <coalesce_pair_hasher> coalesce_table_type;
84 typedef coalesce_table_type::iterator coalesce_iterator_type;
87 typedef struct cost_one_pair_d
89 int first_element;
90 int second_element;
91 struct cost_one_pair_d *next;
92 } * cost_one_pair_p;
94 /* This structure maintains the list of coalesce pairs. */
96 typedef struct coalesce_list_d
98 coalesce_table_type list; /* Hash table. */
99 coalesce_pair_p *sorted; /* List when sorted. */
100 int num_sorted; /* Number in the sorted list. */
101 cost_one_pair_p cost_one_list;/* Single use coalesces with cost 1. */
102 } *coalesce_list_p;
104 #define NO_BEST_COALESCE -1
105 #define MUST_COALESCE_COST INT_MAX
108 /* Return cost of execution of copy instruction with FREQUENCY. */
110 static inline int
111 coalesce_cost (int frequency, bool optimize_for_size)
113 /* Base costs on BB frequencies bounded by 1. */
114 int cost = frequency;
116 if (!cost)
117 cost = 1;
119 if (optimize_for_size)
120 cost = 1;
122 return cost;
126 /* Return the cost of executing a copy instruction in basic block BB. */
128 static inline int
129 coalesce_cost_bb (basic_block bb)
131 return coalesce_cost (bb->frequency, optimize_bb_for_size_p (bb));
135 /* Return the cost of executing a copy instruction on edge E. */
137 static inline int
138 coalesce_cost_edge (edge e)
140 int mult = 1;
142 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
143 if (EDGE_CRITICAL_P (e))
144 mult = 2;
145 if (e->flags & EDGE_ABNORMAL)
146 return MUST_COALESCE_COST;
147 if (e->flags & EDGE_EH)
149 edge e2;
150 edge_iterator ei;
151 FOR_EACH_EDGE (e2, ei, e->dest->preds)
152 if (e2 != e)
154 /* Putting code on EH edge that leads to BB
155 with multiple predecestors imply splitting of
156 edge too. */
157 if (mult < 2)
158 mult = 2;
159 /* If there are multiple EH predecestors, we
160 also copy EH regions and produce separate
161 landing pad. This is expensive. */
162 if (e2->flags & EDGE_EH)
164 mult = 5;
165 break;
170 return coalesce_cost (EDGE_FREQUENCY (e),
171 optimize_edge_for_size_p (e)) * mult;
175 /* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
176 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
177 NO_BEST_COALESCE is returned if there aren't any. */
179 static inline int
180 pop_cost_one_pair (coalesce_list_p cl, int *p1, int *p2)
182 cost_one_pair_p ptr;
184 ptr = cl->cost_one_list;
185 if (!ptr)
186 return NO_BEST_COALESCE;
188 *p1 = ptr->first_element;
189 *p2 = ptr->second_element;
190 cl->cost_one_list = ptr->next;
192 free (ptr);
194 return 1;
197 /* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
198 2 elements via P1 and P2. Their calculated cost is returned by the function.
199 NO_BEST_COALESCE is returned if the coalesce list is empty. */
201 static inline int
202 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
204 coalesce_pair_p node;
205 int ret;
207 if (cl->sorted == NULL)
208 return pop_cost_one_pair (cl, p1, p2);
210 if (cl->num_sorted == 0)
211 return pop_cost_one_pair (cl, p1, p2);
213 node = cl->sorted[--(cl->num_sorted)];
214 *p1 = node->first_element;
215 *p2 = node->second_element;
216 ret = node->cost;
217 free (node);
219 return ret;
223 /* Create a new empty coalesce list object and return it. */
225 static inline coalesce_list_p
226 create_coalesce_list (void)
228 coalesce_list_p list;
229 unsigned size = num_ssa_names * 3;
231 if (size < 40)
232 size = 40;
234 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
235 list->list.create (size);
236 list->sorted = NULL;
237 list->num_sorted = 0;
238 list->cost_one_list = NULL;
239 return list;
243 /* Delete coalesce list CL. */
245 static inline void
246 delete_coalesce_list (coalesce_list_p cl)
248 gcc_assert (cl->cost_one_list == NULL);
249 cl->list.dispose ();
250 free (cl->sorted);
251 gcc_assert (cl->num_sorted == 0);
252 free (cl);
256 /* Find a matching coalesce pair object in CL for the pair P1 and P2. If
257 one isn't found, return NULL if CREATE is false, otherwise create a new
258 coalesce pair object and return it. */
260 static coalesce_pair_p
261 find_coalesce_pair (coalesce_list_p cl, int p1, int p2, bool create)
263 struct coalesce_pair p;
264 coalesce_pair **slot;
265 unsigned int hash;
267 /* Normalize so that p1 is the smaller value. */
268 if (p2 < p1)
270 p.first_element = p2;
271 p.second_element = p1;
273 else
275 p.first_element = p1;
276 p.second_element = p2;
279 hash = coalesce_pair_hasher::hash (&p);
280 slot = cl->list.find_slot_with_hash (&p, hash, create ? INSERT : NO_INSERT);
281 if (!slot)
282 return NULL;
284 if (!*slot)
286 struct coalesce_pair * pair = XNEW (struct coalesce_pair);
287 gcc_assert (cl->sorted == NULL);
288 pair->first_element = p.first_element;
289 pair->second_element = p.second_element;
290 pair->cost = 0;
291 *slot = pair;
294 return (struct coalesce_pair *) *slot;
297 static inline void
298 add_cost_one_coalesce (coalesce_list_p cl, int p1, int p2)
300 cost_one_pair_p pair;
302 pair = XNEW (struct cost_one_pair_d);
303 pair->first_element = p1;
304 pair->second_element = p2;
305 pair->next = cl->cost_one_list;
306 cl->cost_one_list = pair;
310 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
312 static inline void
313 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
315 coalesce_pair_p node;
317 gcc_assert (cl->sorted == NULL);
318 if (p1 == p2)
319 return;
321 node = find_coalesce_pair (cl, p1, p2, true);
323 /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way. */
324 if (node->cost < MUST_COALESCE_COST - 1)
326 if (value < MUST_COALESCE_COST - 1)
327 node->cost += value;
328 else
329 node->cost = value;
334 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
336 static int
337 compare_pairs (const void *p1, const void *p2)
339 const_coalesce_pair_p const *const pp1 = (const_coalesce_pair_p const *) p1;
340 const_coalesce_pair_p const *const pp2 = (const_coalesce_pair_p const *) p2;
341 int result;
343 result = (* pp1)->cost - (* pp2)->cost;
344 /* Since qsort does not guarantee stability we use the elements
345 as a secondary key. This provides us with independence from
346 the host's implementation of the sorting algorithm. */
347 if (result == 0)
349 result = (* pp2)->first_element - (* pp1)->first_element;
350 if (result == 0)
351 result = (* pp2)->second_element - (* pp1)->second_element;
354 return result;
358 /* Return the number of unique coalesce pairs in CL. */
360 static inline int
361 num_coalesce_pairs (coalesce_list_p cl)
363 return cl->list.elements ();
367 /* Iterate over CL using ITER, returning values in PAIR. */
369 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
370 FOR_EACH_HASH_TABLE_ELEMENT ((CL)->list, (PAIR), coalesce_pair_p, (ITER))
373 /* Prepare CL for removal of preferred pairs. When finished they are sorted
374 in order from most important coalesce to least important. */
376 static void
377 sort_coalesce_list (coalesce_list_p cl)
379 unsigned x, num;
380 coalesce_pair_p p;
381 coalesce_iterator_type ppi;
383 gcc_assert (cl->sorted == NULL);
385 num = num_coalesce_pairs (cl);
386 cl->num_sorted = num;
387 if (num == 0)
388 return;
390 /* Allocate a vector for the pair pointers. */
391 cl->sorted = XNEWVEC (coalesce_pair_p, num);
393 /* Populate the vector with pointers to the pairs. */
394 x = 0;
395 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
396 cl->sorted[x++] = p;
397 gcc_assert (x == num);
399 /* Already sorted. */
400 if (num == 1)
401 return;
403 /* If there are only 2, just pick swap them if the order isn't correct. */
404 if (num == 2)
406 if (cl->sorted[0]->cost > cl->sorted[1]->cost)
408 p = cl->sorted[0];
409 cl->sorted[0] = cl->sorted[1];
410 cl->sorted[1] = p;
412 return;
415 /* Only call qsort if there are more than 2 items. */
416 if (num > 2)
417 qsort (cl->sorted, num, sizeof (coalesce_pair_p), compare_pairs);
421 /* Send debug info for coalesce list CL to file F. */
423 static void
424 dump_coalesce_list (FILE *f, coalesce_list_p cl)
426 coalesce_pair_p node;
427 coalesce_iterator_type ppi;
429 int x;
430 tree var;
432 if (cl->sorted == NULL)
434 fprintf (f, "Coalesce List:\n");
435 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
437 tree var1 = ssa_name (node->first_element);
438 tree var2 = ssa_name (node->second_element);
439 print_generic_expr (f, var1, TDF_SLIM);
440 fprintf (f, " <-> ");
441 print_generic_expr (f, var2, TDF_SLIM);
442 fprintf (f, " (%1d), ", node->cost);
443 fprintf (f, "\n");
446 else
448 fprintf (f, "Sorted Coalesce list:\n");
449 for (x = cl->num_sorted - 1 ; x >=0; x--)
451 node = cl->sorted[x];
452 fprintf (f, "(%d) ", node->cost);
453 var = ssa_name (node->first_element);
454 print_generic_expr (f, var, TDF_SLIM);
455 fprintf (f, " <-> ");
456 var = ssa_name (node->second_element);
457 print_generic_expr (f, var, TDF_SLIM);
458 fprintf (f, "\n");
464 /* This represents a conflict graph. Implemented as an array of bitmaps.
465 A full matrix is used for conflicts rather than just upper triangular form.
466 this make sit much simpler and faster to perform conflict merges. */
468 typedef struct ssa_conflicts_d
470 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
471 vec<bitmap> conflicts;
472 } * ssa_conflicts_p;
474 /* Return an empty new conflict graph for SIZE elements. */
476 static inline ssa_conflicts_p
477 ssa_conflicts_new (unsigned size)
479 ssa_conflicts_p ptr;
481 ptr = XNEW (struct ssa_conflicts_d);
482 bitmap_obstack_initialize (&ptr->obstack);
483 ptr->conflicts.create (size);
484 ptr->conflicts.safe_grow_cleared (size);
485 return ptr;
489 /* Free storage for conflict graph PTR. */
491 static inline void
492 ssa_conflicts_delete (ssa_conflicts_p ptr)
494 bitmap_obstack_release (&ptr->obstack);
495 ptr->conflicts.release ();
496 free (ptr);
500 /* Test if elements X and Y conflict in graph PTR. */
502 static inline bool
503 ssa_conflicts_test_p (ssa_conflicts_p ptr, unsigned x, unsigned y)
505 bitmap bx = ptr->conflicts[x];
506 bitmap by = ptr->conflicts[y];
508 gcc_checking_assert (x != y);
510 if (bx)
511 /* Avoid the lookup if Y has no conflicts. */
512 return by ? bitmap_bit_p (bx, y) : false;
513 else
514 return false;
518 /* Add a conflict with Y to the bitmap for X in graph PTR. */
520 static inline void
521 ssa_conflicts_add_one (ssa_conflicts_p ptr, unsigned x, unsigned y)
523 bitmap bx = ptr->conflicts[x];
524 /* If there are no conflicts yet, allocate the bitmap and set bit. */
525 if (! bx)
526 bx = ptr->conflicts[x] = BITMAP_ALLOC (&ptr->obstack);
527 bitmap_set_bit (bx, y);
531 /* Add conflicts between X and Y in graph PTR. */
533 static inline void
534 ssa_conflicts_add (ssa_conflicts_p ptr, unsigned x, unsigned y)
536 gcc_checking_assert (x != y);
537 ssa_conflicts_add_one (ptr, x, y);
538 ssa_conflicts_add_one (ptr, y, x);
542 /* Merge all Y's conflict into X in graph PTR. */
544 static inline void
545 ssa_conflicts_merge (ssa_conflicts_p ptr, unsigned x, unsigned y)
547 unsigned z;
548 bitmap_iterator bi;
549 bitmap bx = ptr->conflicts[x];
550 bitmap by = ptr->conflicts[y];
552 gcc_checking_assert (x != y);
553 if (! by)
554 return;
556 /* Add a conflict between X and every one Y has. If the bitmap doesn't
557 exist, then it has already been coalesced, and we don't need to add a
558 conflict. */
559 EXECUTE_IF_SET_IN_BITMAP (by, 0, z, bi)
561 bitmap bz = ptr->conflicts[z];
562 if (bz)
563 bitmap_set_bit (bz, x);
566 if (bx)
568 /* If X has conflicts, add Y's to X. */
569 bitmap_ior_into (bx, by);
570 BITMAP_FREE (by);
571 ptr->conflicts[y] = NULL;
573 else
575 /* If X has no conflicts, simply use Y's. */
576 ptr->conflicts[x] = by;
577 ptr->conflicts[y] = NULL;
582 /* Dump a conflicts graph. */
584 static void
585 ssa_conflicts_dump (FILE *file, ssa_conflicts_p ptr)
587 unsigned x;
588 bitmap b;
590 fprintf (file, "\nConflict graph:\n");
592 FOR_EACH_VEC_ELT (ptr->conflicts, x, b)
593 if (b)
595 fprintf (file, "%d: ", x);
596 dump_bitmap (file, b);
601 /* This structure is used to efficiently record the current status of live
602 SSA_NAMES when building a conflict graph.
603 LIVE_BASE_VAR has a bit set for each base variable which has at least one
604 ssa version live.
605 LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
606 index, and is used to track what partitions of each base variable are
607 live. This makes it easy to add conflicts between just live partitions
608 with the same base variable.
609 The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
610 marked as being live. This delays clearing of these bitmaps until
611 they are actually needed again. */
613 typedef struct live_track_d
615 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
616 bitmap live_base_var; /* Indicates if a basevar is live. */
617 bitmap *live_base_partitions; /* Live partitions for each basevar. */
618 var_map map; /* Var_map being used for partition mapping. */
619 } * live_track_p;
622 /* This routine will create a new live track structure based on the partitions
623 in MAP. */
625 static live_track_p
626 new_live_track (var_map map)
628 live_track_p ptr;
629 int lim, x;
631 /* Make sure there is a partition view in place. */
632 gcc_assert (map->partition_to_base_index != NULL);
634 ptr = (live_track_p) xmalloc (sizeof (struct live_track_d));
635 ptr->map = map;
636 lim = num_basevars (map);
637 bitmap_obstack_initialize (&ptr->obstack);
638 ptr->live_base_partitions = (bitmap *) xmalloc(sizeof (bitmap *) * lim);
639 ptr->live_base_var = BITMAP_ALLOC (&ptr->obstack);
640 for (x = 0; x < lim; x++)
641 ptr->live_base_partitions[x] = BITMAP_ALLOC (&ptr->obstack);
642 return ptr;
646 /* This routine will free the memory associated with PTR. */
648 static void
649 delete_live_track (live_track_p ptr)
651 bitmap_obstack_release (&ptr->obstack);
652 free (ptr->live_base_partitions);
653 free (ptr);
657 /* This function will remove PARTITION from the live list in PTR. */
659 static inline void
660 live_track_remove_partition (live_track_p ptr, int partition)
662 int root;
664 root = basevar_index (ptr->map, partition);
665 bitmap_clear_bit (ptr->live_base_partitions[root], partition);
666 /* If the element list is empty, make the base variable not live either. */
667 if (bitmap_empty_p (ptr->live_base_partitions[root]))
668 bitmap_clear_bit (ptr->live_base_var, root);
672 /* This function will adds PARTITION to the live list in PTR. */
674 static inline void
675 live_track_add_partition (live_track_p ptr, int partition)
677 int root;
679 root = basevar_index (ptr->map, partition);
680 /* If this base var wasn't live before, it is now. Clear the element list
681 since it was delayed until needed. */
682 if (bitmap_set_bit (ptr->live_base_var, root))
683 bitmap_clear (ptr->live_base_partitions[root]);
684 bitmap_set_bit (ptr->live_base_partitions[root], partition);
689 /* Clear the live bit for VAR in PTR. */
691 static inline void
692 live_track_clear_var (live_track_p ptr, tree var)
694 int p;
696 p = var_to_partition (ptr->map, var);
697 if (p != NO_PARTITION)
698 live_track_remove_partition (ptr, p);
702 /* Return TRUE if VAR is live in PTR. */
704 static inline bool
705 live_track_live_p (live_track_p ptr, tree var)
707 int p, root;
709 p = var_to_partition (ptr->map, var);
710 if (p != NO_PARTITION)
712 root = basevar_index (ptr->map, p);
713 if (bitmap_bit_p (ptr->live_base_var, root))
714 return bitmap_bit_p (ptr->live_base_partitions[root], p);
716 return false;
720 /* This routine will add USE to PTR. USE will be marked as live in both the
721 ssa live map and the live bitmap for the root of USE. */
723 static inline void
724 live_track_process_use (live_track_p ptr, tree use)
726 int p;
728 p = var_to_partition (ptr->map, use);
729 if (p == NO_PARTITION)
730 return;
732 /* Mark as live in the appropriate live list. */
733 live_track_add_partition (ptr, p);
737 /* This routine will process a DEF in PTR. DEF will be removed from the live
738 lists, and if there are any other live partitions with the same base
739 variable, conflicts will be added to GRAPH. */
741 static inline void
742 live_track_process_def (live_track_p ptr, tree def, ssa_conflicts_p graph)
744 int p, root;
745 bitmap b;
746 unsigned x;
747 bitmap_iterator bi;
749 p = var_to_partition (ptr->map, def);
750 if (p == NO_PARTITION)
751 return;
753 /* Clear the liveness bit. */
754 live_track_remove_partition (ptr, p);
756 /* If the bitmap isn't empty now, conflicts need to be added. */
757 root = basevar_index (ptr->map, p);
758 if (bitmap_bit_p (ptr->live_base_var, root))
760 b = ptr->live_base_partitions[root];
761 EXECUTE_IF_SET_IN_BITMAP (b, 0, x, bi)
762 ssa_conflicts_add (graph, p, x);
767 /* Initialize PTR with the partitions set in INIT. */
769 static inline void
770 live_track_init (live_track_p ptr, bitmap init)
772 unsigned p;
773 bitmap_iterator bi;
775 /* Mark all live on exit partitions. */
776 EXECUTE_IF_SET_IN_BITMAP (init, 0, p, bi)
777 live_track_add_partition (ptr, p);
781 /* This routine will clear all live partitions in PTR. */
783 static inline void
784 live_track_clear_base_vars (live_track_p ptr)
786 /* Simply clear the live base list. Anything marked as live in the element
787 lists will be cleared later if/when the base variable ever comes alive
788 again. */
789 bitmap_clear (ptr->live_base_var);
793 /* Build a conflict graph based on LIVEINFO. Any partitions which are in the
794 partition view of the var_map liveinfo is based on get entries in the
795 conflict graph. Only conflicts between ssa_name partitions with the same
796 base variable are added. */
798 static ssa_conflicts_p
799 build_ssa_conflict_graph (tree_live_info_p liveinfo)
801 ssa_conflicts_p graph;
802 var_map map;
803 basic_block bb;
804 ssa_op_iter iter;
805 live_track_p live;
807 map = live_var_map (liveinfo);
808 graph = ssa_conflicts_new (num_var_partitions (map));
810 live = new_live_track (map);
812 FOR_EACH_BB (bb)
814 gimple_stmt_iterator gsi;
816 /* Start with live on exit temporaries. */
817 live_track_init (live, live_on_exit (liveinfo, bb));
819 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
821 tree var;
822 gimple stmt = gsi_stmt (gsi);
824 /* A copy between 2 partitions does not introduce an interference
825 by itself. If they did, you would never be able to coalesce
826 two things which are copied. If the two variables really do
827 conflict, they will conflict elsewhere in the program.
829 This is handled by simply removing the SRC of the copy from the
830 live list, and processing the stmt normally. */
831 if (is_gimple_assign (stmt))
833 tree lhs = gimple_assign_lhs (stmt);
834 tree rhs1 = gimple_assign_rhs1 (stmt);
835 if (gimple_assign_copy_p (stmt)
836 && TREE_CODE (lhs) == SSA_NAME
837 && TREE_CODE (rhs1) == SSA_NAME)
838 live_track_clear_var (live, rhs1);
840 else if (is_gimple_debug (stmt))
841 continue;
843 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
844 live_track_process_def (live, var, graph);
846 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
847 live_track_process_use (live, var);
850 /* If result of a PHI is unused, looping over the statements will not
851 record any conflicts since the def was never live. Since the PHI node
852 is going to be translated out of SSA form, it will insert a copy.
853 There must be a conflict recorded between the result of the PHI and
854 any variables that are live. Otherwise the out-of-ssa translation
855 may create incorrect code. */
856 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
858 gimple phi = gsi_stmt (gsi);
859 tree result = PHI_RESULT (phi);
860 if (live_track_live_p (live, result))
861 live_track_process_def (live, result, graph);
864 live_track_clear_base_vars (live);
867 delete_live_track (live);
868 return graph;
872 /* Shortcut routine to print messages to file F of the form:
873 "STR1 EXPR1 STR2 EXPR2 STR3." */
875 static inline void
876 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
877 tree expr2, const char *str3)
879 fprintf (f, "%s", str1);
880 print_generic_expr (f, expr1, TDF_SLIM);
881 fprintf (f, "%s", str2);
882 print_generic_expr (f, expr2, TDF_SLIM);
883 fprintf (f, "%s", str3);
887 /* Print a failure to coalesce a MUST_COALESCE pair X and Y. */
889 static inline void
890 fail_abnormal_edge_coalesce (int x, int y)
892 fprintf (stderr, "\nUnable to coalesce ssa_names %d and %d",x, y);
893 fprintf (stderr, " which are marked as MUST COALESCE.\n");
894 print_generic_expr (stderr, ssa_name (x), TDF_SLIM);
895 fprintf (stderr, " and ");
896 print_generic_stmt (stderr, ssa_name (y), TDF_SLIM);
898 internal_error ("SSA corruption");
902 /* This function creates a var_map for the current function as well as creating
903 a coalesce list for use later in the out of ssa process. */
905 static var_map
906 create_outofssa_var_map (coalesce_list_p cl, bitmap used_in_copy)
908 gimple_stmt_iterator gsi;
909 basic_block bb;
910 tree var;
911 gimple stmt;
912 tree first;
913 var_map map;
914 ssa_op_iter iter;
915 int v1, v2, cost;
916 unsigned i;
918 map = init_var_map (num_ssa_names);
920 FOR_EACH_BB (bb)
922 tree arg;
924 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
926 gimple phi = gsi_stmt (gsi);
927 size_t i;
928 int ver;
929 tree res;
930 bool saw_copy = false;
932 res = gimple_phi_result (phi);
933 ver = SSA_NAME_VERSION (res);
934 register_ssa_partition (map, res);
936 /* Register ssa_names and coalesces between the args and the result
937 of all PHI. */
938 for (i = 0; i < gimple_phi_num_args (phi); i++)
940 edge e = gimple_phi_arg_edge (phi, i);
941 arg = PHI_ARG_DEF (phi, i);
942 if (TREE_CODE (arg) != SSA_NAME)
943 continue;
945 register_ssa_partition (map, arg);
946 if ((SSA_NAME_VAR (arg) == SSA_NAME_VAR (res)
947 && TREE_TYPE (arg) == TREE_TYPE (res))
948 || (e->flags & EDGE_ABNORMAL))
950 saw_copy = true;
951 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (arg));
952 if ((e->flags & EDGE_ABNORMAL) == 0)
954 int cost = coalesce_cost_edge (e);
955 if (cost == 1 && has_single_use (arg))
956 add_cost_one_coalesce (cl, ver, SSA_NAME_VERSION (arg));
957 else
958 add_coalesce (cl, ver, SSA_NAME_VERSION (arg), cost);
962 if (saw_copy)
963 bitmap_set_bit (used_in_copy, ver);
966 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
968 stmt = gsi_stmt (gsi);
970 if (is_gimple_debug (stmt))
971 continue;
973 /* Register USE and DEF operands in each statement. */
974 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
975 register_ssa_partition (map, var);
977 /* Check for copy coalesces. */
978 switch (gimple_code (stmt))
980 case GIMPLE_ASSIGN:
982 tree lhs = gimple_assign_lhs (stmt);
983 tree rhs1 = gimple_assign_rhs1 (stmt);
985 if (gimple_assign_copy_p (stmt)
986 && TREE_CODE (lhs) == SSA_NAME
987 && TREE_CODE (rhs1) == SSA_NAME
988 && SSA_NAME_VAR (lhs) == SSA_NAME_VAR (rhs1)
989 && TREE_TYPE (lhs) == TREE_TYPE (rhs1))
991 v1 = SSA_NAME_VERSION (lhs);
992 v2 = SSA_NAME_VERSION (rhs1);
993 cost = coalesce_cost_bb (bb);
994 add_coalesce (cl, v1, v2, cost);
995 bitmap_set_bit (used_in_copy, v1);
996 bitmap_set_bit (used_in_copy, v2);
999 break;
1001 case GIMPLE_ASM:
1003 unsigned long noutputs, i;
1004 unsigned long ninputs;
1005 tree *outputs, link;
1006 noutputs = gimple_asm_noutputs (stmt);
1007 ninputs = gimple_asm_ninputs (stmt);
1008 outputs = (tree *) alloca (noutputs * sizeof (tree));
1009 for (i = 0; i < noutputs; ++i)
1011 link = gimple_asm_output_op (stmt, i);
1012 outputs[i] = TREE_VALUE (link);
1015 for (i = 0; i < ninputs; ++i)
1017 const char *constraint;
1018 tree input;
1019 char *end;
1020 unsigned long match;
1022 link = gimple_asm_input_op (stmt, i);
1023 constraint
1024 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1025 input = TREE_VALUE (link);
1027 if (TREE_CODE (input) != SSA_NAME)
1028 continue;
1030 match = strtoul (constraint, &end, 10);
1031 if (match >= noutputs || end == constraint)
1032 continue;
1034 if (TREE_CODE (outputs[match]) != SSA_NAME)
1035 continue;
1037 v1 = SSA_NAME_VERSION (outputs[match]);
1038 v2 = SSA_NAME_VERSION (input);
1040 if (SSA_NAME_VAR (outputs[match]) == SSA_NAME_VAR (input)
1041 && TREE_TYPE (outputs[match]) == TREE_TYPE (input))
1043 cost = coalesce_cost (REG_BR_PROB_BASE,
1044 optimize_bb_for_size_p (bb));
1045 add_coalesce (cl, v1, v2, cost);
1046 bitmap_set_bit (used_in_copy, v1);
1047 bitmap_set_bit (used_in_copy, v2);
1050 break;
1053 default:
1054 break;
1059 /* Now process result decls and live on entry variables for entry into
1060 the coalesce list. */
1061 first = NULL_TREE;
1062 for (i = 1; i < num_ssa_names; i++)
1064 var = ssa_name (i);
1065 if (var != NULL_TREE && !virtual_operand_p (var))
1067 /* Add coalesces between all the result decls. */
1068 if (SSA_NAME_VAR (var)
1069 && TREE_CODE (SSA_NAME_VAR (var)) == RESULT_DECL)
1071 if (first == NULL_TREE)
1072 first = var;
1073 else
1075 gcc_assert (SSA_NAME_VAR (var) == SSA_NAME_VAR (first)
1076 && TREE_TYPE (var) == TREE_TYPE (first));
1077 v1 = SSA_NAME_VERSION (first);
1078 v2 = SSA_NAME_VERSION (var);
1079 bitmap_set_bit (used_in_copy, v1);
1080 bitmap_set_bit (used_in_copy, v2);
1081 cost = coalesce_cost_bb (EXIT_BLOCK_PTR);
1082 add_coalesce (cl, v1, v2, cost);
1085 /* Mark any default_def variables as being in the coalesce list
1086 since they will have to be coalesced with the base variable. If
1087 not marked as present, they won't be in the coalesce view. */
1088 if (SSA_NAME_IS_DEFAULT_DEF (var)
1089 && !has_zero_uses (var))
1090 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1094 return map;
1098 /* Attempt to coalesce ssa versions X and Y together using the partition
1099 mapping in MAP and checking conflicts in GRAPH. Output any debug info to
1100 DEBUG, if it is nun-NULL. */
1102 static inline bool
1103 attempt_coalesce (var_map map, ssa_conflicts_p graph, int x, int y,
1104 FILE *debug)
1106 int z;
1107 tree var1, var2;
1108 int p1, p2;
1110 p1 = var_to_partition (map, ssa_name (x));
1111 p2 = var_to_partition (map, ssa_name (y));
1113 if (debug)
1115 fprintf (debug, "(%d)", x);
1116 print_generic_expr (debug, partition_to_var (map, p1), TDF_SLIM);
1117 fprintf (debug, " & (%d)", y);
1118 print_generic_expr (debug, partition_to_var (map, p2), TDF_SLIM);
1121 if (p1 == p2)
1123 if (debug)
1124 fprintf (debug, ": Already Coalesced.\n");
1125 return true;
1128 if (debug)
1129 fprintf (debug, " [map: %d, %d] ", p1, p2);
1132 if (!ssa_conflicts_test_p (graph, p1, p2))
1134 var1 = partition_to_var (map, p1);
1135 var2 = partition_to_var (map, p2);
1136 z = var_union (map, var1, var2);
1137 if (z == NO_PARTITION)
1139 if (debug)
1140 fprintf (debug, ": Unable to perform partition union.\n");
1141 return false;
1144 /* z is the new combined partition. Remove the other partition from
1145 the list, and merge the conflicts. */
1146 if (z == p1)
1147 ssa_conflicts_merge (graph, p1, p2);
1148 else
1149 ssa_conflicts_merge (graph, p2, p1);
1151 if (debug)
1152 fprintf (debug, ": Success -> %d\n", z);
1153 return true;
1156 if (debug)
1157 fprintf (debug, ": Fail due to conflict\n");
1159 return false;
1163 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1164 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1166 static void
1167 coalesce_partitions (var_map map, ssa_conflicts_p graph, coalesce_list_p cl,
1168 FILE *debug)
1170 int x = 0, y = 0;
1171 tree var1, var2;
1172 int cost;
1173 basic_block bb;
1174 edge e;
1175 edge_iterator ei;
1177 /* First, coalesce all the copies across abnormal edges. These are not placed
1178 in the coalesce list because they do not need to be sorted, and simply
1179 consume extra memory/compilation time in large programs. */
1181 FOR_EACH_BB (bb)
1183 FOR_EACH_EDGE (e, ei, bb->preds)
1184 if (e->flags & EDGE_ABNORMAL)
1186 gimple_stmt_iterator gsi;
1187 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1188 gsi_next (&gsi))
1190 gimple phi = gsi_stmt (gsi);
1191 tree res = PHI_RESULT (phi);
1192 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1193 int v1 = SSA_NAME_VERSION (res);
1194 int v2 = SSA_NAME_VERSION (arg);
1196 if (debug)
1197 fprintf (debug, "Abnormal coalesce: ");
1199 if (!attempt_coalesce (map, graph, v1, v2, debug))
1200 fail_abnormal_edge_coalesce (v1, v2);
1205 /* Now process the items in the coalesce list. */
1207 while ((cost = pop_best_coalesce (cl, &x, &y)) != NO_BEST_COALESCE)
1209 var1 = ssa_name (x);
1210 var2 = ssa_name (y);
1212 /* Assert the coalesces have the same base variable. */
1213 gcc_assert (SSA_NAME_VAR (var1) == SSA_NAME_VAR (var2)
1214 && TREE_TYPE (var1) == TREE_TYPE (var2));
1216 if (debug)
1217 fprintf (debug, "Coalesce list: ");
1218 attempt_coalesce (map, graph, x, y, debug);
1223 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1225 struct ssa_name_var_hash : typed_noop_remove <tree_node>
1227 typedef union tree_node value_type;
1228 typedef union tree_node compare_type;
1229 static inline hashval_t hash (const value_type *);
1230 static inline int equal (const value_type *, const compare_type *);
1233 inline hashval_t
1234 ssa_name_var_hash::hash (const_tree n)
1236 return DECL_UID (SSA_NAME_VAR (n));
1239 inline int
1240 ssa_name_var_hash::equal (const value_type *n1, const compare_type *n2)
1242 return SSA_NAME_VAR (n1) == SSA_NAME_VAR (n2);
1246 /* Reduce the number of copies by coalescing variables in the function. Return
1247 a partition map with the resulting coalesces. */
1249 extern var_map
1250 coalesce_ssa_name (void)
1252 tree_live_info_p liveinfo;
1253 ssa_conflicts_p graph;
1254 coalesce_list_p cl;
1255 bitmap used_in_copies = BITMAP_ALLOC (NULL);
1256 var_map map;
1257 unsigned int i;
1259 cl = create_coalesce_list ();
1260 map = create_outofssa_var_map (cl, used_in_copies);
1262 /* We need to coalesce all names originating same SSA_NAME_VAR
1263 so debug info remains undisturbed. */
1264 if (!optimize)
1266 hash_table <ssa_name_var_hash> ssa_name_hash;
1268 ssa_name_hash.create (10);
1269 for (i = 1; i < num_ssa_names; i++)
1271 tree a = ssa_name (i);
1273 if (a
1274 && SSA_NAME_VAR (a)
1275 && !DECL_IGNORED_P (SSA_NAME_VAR (a))
1276 && (!has_zero_uses (a) || !SSA_NAME_IS_DEFAULT_DEF (a)))
1278 tree *slot = ssa_name_hash.find_slot (a, INSERT);
1280 if (!*slot)
1281 *slot = a;
1282 else
1284 add_coalesce (cl, SSA_NAME_VERSION (a), SSA_NAME_VERSION (*slot),
1285 MUST_COALESCE_COST - 1);
1286 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (a));
1287 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (*slot));
1291 ssa_name_hash.dispose ();
1293 if (dump_file && (dump_flags & TDF_DETAILS))
1294 dump_var_map (dump_file, map);
1296 /* Don't calculate live ranges for variables not in the coalesce list. */
1297 partition_view_bitmap (map, used_in_copies, true);
1298 BITMAP_FREE (used_in_copies);
1300 if (num_var_partitions (map) < 1)
1302 delete_coalesce_list (cl);
1303 return map;
1306 if (dump_file && (dump_flags & TDF_DETAILS))
1307 dump_var_map (dump_file, map);
1309 liveinfo = calculate_live_ranges (map);
1311 if (dump_file && (dump_flags & TDF_DETAILS))
1312 dump_live_info (dump_file, liveinfo, LIVEDUMP_ENTRY);
1314 /* Build a conflict graph. */
1315 graph = build_ssa_conflict_graph (liveinfo);
1316 delete_tree_live_info (liveinfo);
1317 if (dump_file && (dump_flags & TDF_DETAILS))
1318 ssa_conflicts_dump (dump_file, graph);
1320 sort_coalesce_list (cl);
1322 if (dump_file && (dump_flags & TDF_DETAILS))
1324 fprintf (dump_file, "\nAfter sorting:\n");
1325 dump_coalesce_list (dump_file, cl);
1328 /* First, coalesce all live on entry variables to their base variable.
1329 This will ensure the first use is coming from the correct location. */
1331 if (dump_file && (dump_flags & TDF_DETAILS))
1332 dump_var_map (dump_file, map);
1334 /* Now coalesce everything in the list. */
1335 coalesce_partitions (map, graph, cl,
1336 ((dump_flags & TDF_DETAILS) ? dump_file
1337 : NULL));
1339 delete_coalesce_list (cl);
1340 ssa_conflicts_delete (graph);
1342 return map;