2013-11-13 Jan-Benedict Glaw <jbglaw@lug-owl.de>
[official-gcc.git] / gcc / tree-ssa-coalesce.c
blobc445c78626e2247f0659cbc957bb675c2ae81eeb
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 "gimple.h"
31 #include "gimple-ssa.h"
32 #include "tree-phinodes.h"
33 #include "ssa-iterators.h"
34 #include "tree-ssanames.h"
35 #include "hash-table.h"
36 #include "tree-ssa-live.h"
37 #include "tree-ssa-coalesce.h"
38 #include "diagnostic-core.h"
41 /* This set of routines implements a coalesce_list. This is an object which
42 is used to track pairs of ssa_names which are desirable to coalesce
43 together to avoid copies. Costs are associated with each pair, and when
44 all desired information has been collected, the object can be used to
45 order the pairs for processing. */
47 /* This structure defines a pair entry. */
49 typedef struct coalesce_pair
51 int first_element;
52 int second_element;
53 int cost;
54 } * coalesce_pair_p;
55 typedef const struct coalesce_pair *const_coalesce_pair_p;
57 /* Coalesce pair hashtable helpers. */
59 struct coalesce_pair_hasher : typed_noop_remove <coalesce_pair>
61 typedef coalesce_pair value_type;
62 typedef coalesce_pair compare_type;
63 static inline hashval_t hash (const value_type *);
64 static inline bool equal (const value_type *, const compare_type *);
67 /* Hash function for coalesce list. Calculate hash for PAIR. */
69 inline hashval_t
70 coalesce_pair_hasher::hash (const value_type *pair)
72 hashval_t a = (hashval_t)(pair->first_element);
73 hashval_t b = (hashval_t)(pair->second_element);
75 return b * (b - 1) / 2 + a;
78 /* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
79 returning TRUE if the two pairs are equivalent. */
81 inline bool
82 coalesce_pair_hasher::equal (const value_type *p1, const compare_type *p2)
84 return (p1->first_element == p2->first_element
85 && p1->second_element == p2->second_element);
88 typedef hash_table <coalesce_pair_hasher> coalesce_table_type;
89 typedef coalesce_table_type::iterator coalesce_iterator_type;
92 typedef struct cost_one_pair_d
94 int first_element;
95 int second_element;
96 struct cost_one_pair_d *next;
97 } * cost_one_pair_p;
99 /* This structure maintains the list of coalesce pairs. */
101 typedef struct coalesce_list_d
103 coalesce_table_type list; /* Hash table. */
104 coalesce_pair_p *sorted; /* List when sorted. */
105 int num_sorted; /* Number in the sorted list. */
106 cost_one_pair_p cost_one_list;/* Single use coalesces with cost 1. */
107 } *coalesce_list_p;
109 #define NO_BEST_COALESCE -1
110 #define MUST_COALESCE_COST INT_MAX
113 /* Return cost of execution of copy instruction with FREQUENCY. */
115 static inline int
116 coalesce_cost (int frequency, bool optimize_for_size)
118 /* Base costs on BB frequencies bounded by 1. */
119 int cost = frequency;
121 if (!cost)
122 cost = 1;
124 if (optimize_for_size)
125 cost = 1;
127 return cost;
131 /* Return the cost of executing a copy instruction in basic block BB. */
133 static inline int
134 coalesce_cost_bb (basic_block bb)
136 return coalesce_cost (bb->frequency, optimize_bb_for_size_p (bb));
140 /* Return the cost of executing a copy instruction on edge E. */
142 static inline int
143 coalesce_cost_edge (edge e)
145 int mult = 1;
147 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
148 if (EDGE_CRITICAL_P (e))
149 mult = 2;
150 if (e->flags & EDGE_ABNORMAL)
151 return MUST_COALESCE_COST;
152 if (e->flags & EDGE_EH)
154 edge e2;
155 edge_iterator ei;
156 FOR_EACH_EDGE (e2, ei, e->dest->preds)
157 if (e2 != e)
159 /* Putting code on EH edge that leads to BB
160 with multiple predecestors imply splitting of
161 edge too. */
162 if (mult < 2)
163 mult = 2;
164 /* If there are multiple EH predecestors, we
165 also copy EH regions and produce separate
166 landing pad. This is expensive. */
167 if (e2->flags & EDGE_EH)
169 mult = 5;
170 break;
175 return coalesce_cost (EDGE_FREQUENCY (e),
176 optimize_edge_for_size_p (e)) * mult;
180 /* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
181 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
182 NO_BEST_COALESCE is returned if there aren't any. */
184 static inline int
185 pop_cost_one_pair (coalesce_list_p cl, int *p1, int *p2)
187 cost_one_pair_p ptr;
189 ptr = cl->cost_one_list;
190 if (!ptr)
191 return NO_BEST_COALESCE;
193 *p1 = ptr->first_element;
194 *p2 = ptr->second_element;
195 cl->cost_one_list = ptr->next;
197 free (ptr);
199 return 1;
202 /* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
203 2 elements via P1 and P2. Their calculated cost is returned by the function.
204 NO_BEST_COALESCE is returned if the coalesce list is empty. */
206 static inline int
207 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
209 coalesce_pair_p node;
210 int ret;
212 if (cl->sorted == NULL)
213 return pop_cost_one_pair (cl, p1, p2);
215 if (cl->num_sorted == 0)
216 return pop_cost_one_pair (cl, p1, p2);
218 node = cl->sorted[--(cl->num_sorted)];
219 *p1 = node->first_element;
220 *p2 = node->second_element;
221 ret = node->cost;
222 free (node);
224 return ret;
228 /* Create a new empty coalesce list object and return it. */
230 static inline coalesce_list_p
231 create_coalesce_list (void)
233 coalesce_list_p list;
234 unsigned size = num_ssa_names * 3;
236 if (size < 40)
237 size = 40;
239 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
240 list->list.create (size);
241 list->sorted = NULL;
242 list->num_sorted = 0;
243 list->cost_one_list = NULL;
244 return list;
248 /* Delete coalesce list CL. */
250 static inline void
251 delete_coalesce_list (coalesce_list_p cl)
253 gcc_assert (cl->cost_one_list == NULL);
254 cl->list.dispose ();
255 free (cl->sorted);
256 gcc_assert (cl->num_sorted == 0);
257 free (cl);
261 /* Find a matching coalesce pair object in CL for the pair P1 and P2. If
262 one isn't found, return NULL if CREATE is false, otherwise create a new
263 coalesce pair object and return it. */
265 static coalesce_pair_p
266 find_coalesce_pair (coalesce_list_p cl, int p1, int p2, bool create)
268 struct coalesce_pair p;
269 coalesce_pair **slot;
270 unsigned int hash;
272 /* Normalize so that p1 is the smaller value. */
273 if (p2 < p1)
275 p.first_element = p2;
276 p.second_element = p1;
278 else
280 p.first_element = p1;
281 p.second_element = p2;
284 hash = coalesce_pair_hasher::hash (&p);
285 slot = cl->list.find_slot_with_hash (&p, hash, create ? INSERT : NO_INSERT);
286 if (!slot)
287 return NULL;
289 if (!*slot)
291 struct coalesce_pair * pair = XNEW (struct coalesce_pair);
292 gcc_assert (cl->sorted == NULL);
293 pair->first_element = p.first_element;
294 pair->second_element = p.second_element;
295 pair->cost = 0;
296 *slot = pair;
299 return (struct coalesce_pair *) *slot;
302 static inline void
303 add_cost_one_coalesce (coalesce_list_p cl, int p1, int p2)
305 cost_one_pair_p pair;
307 pair = XNEW (struct cost_one_pair_d);
308 pair->first_element = p1;
309 pair->second_element = p2;
310 pair->next = cl->cost_one_list;
311 cl->cost_one_list = pair;
315 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
317 static inline void
318 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
320 coalesce_pair_p node;
322 gcc_assert (cl->sorted == NULL);
323 if (p1 == p2)
324 return;
326 node = find_coalesce_pair (cl, p1, p2, true);
328 /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way. */
329 if (node->cost < MUST_COALESCE_COST - 1)
331 if (value < MUST_COALESCE_COST - 1)
332 node->cost += value;
333 else
334 node->cost = value;
339 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
341 static int
342 compare_pairs (const void *p1, const void *p2)
344 const_coalesce_pair_p const *const pp1 = (const_coalesce_pair_p const *) p1;
345 const_coalesce_pair_p const *const pp2 = (const_coalesce_pair_p const *) p2;
346 int result;
348 result = (* pp1)->cost - (* pp2)->cost;
349 /* Since qsort does not guarantee stability we use the elements
350 as a secondary key. This provides us with independence from
351 the host's implementation of the sorting algorithm. */
352 if (result == 0)
354 result = (* pp2)->first_element - (* pp1)->first_element;
355 if (result == 0)
356 result = (* pp2)->second_element - (* pp1)->second_element;
359 return result;
363 /* Return the number of unique coalesce pairs in CL. */
365 static inline int
366 num_coalesce_pairs (coalesce_list_p cl)
368 return cl->list.elements ();
372 /* Iterate over CL using ITER, returning values in PAIR. */
374 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
375 FOR_EACH_HASH_TABLE_ELEMENT ((CL)->list, (PAIR), coalesce_pair_p, (ITER))
378 /* Prepare CL for removal of preferred pairs. When finished they are sorted
379 in order from most important coalesce to least important. */
381 static void
382 sort_coalesce_list (coalesce_list_p cl)
384 unsigned x, num;
385 coalesce_pair_p p;
386 coalesce_iterator_type ppi;
388 gcc_assert (cl->sorted == NULL);
390 num = num_coalesce_pairs (cl);
391 cl->num_sorted = num;
392 if (num == 0)
393 return;
395 /* Allocate a vector for the pair pointers. */
396 cl->sorted = XNEWVEC (coalesce_pair_p, num);
398 /* Populate the vector with pointers to the pairs. */
399 x = 0;
400 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
401 cl->sorted[x++] = p;
402 gcc_assert (x == num);
404 /* Already sorted. */
405 if (num == 1)
406 return;
408 /* If there are only 2, just pick swap them if the order isn't correct. */
409 if (num == 2)
411 if (cl->sorted[0]->cost > cl->sorted[1]->cost)
413 p = cl->sorted[0];
414 cl->sorted[0] = cl->sorted[1];
415 cl->sorted[1] = p;
417 return;
420 /* Only call qsort if there are more than 2 items. */
421 if (num > 2)
422 qsort (cl->sorted, num, sizeof (coalesce_pair_p), compare_pairs);
426 /* Send debug info for coalesce list CL to file F. */
428 static void
429 dump_coalesce_list (FILE *f, coalesce_list_p cl)
431 coalesce_pair_p node;
432 coalesce_iterator_type ppi;
434 int x;
435 tree var;
437 if (cl->sorted == NULL)
439 fprintf (f, "Coalesce List:\n");
440 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
442 tree var1 = ssa_name (node->first_element);
443 tree var2 = ssa_name (node->second_element);
444 print_generic_expr (f, var1, TDF_SLIM);
445 fprintf (f, " <-> ");
446 print_generic_expr (f, var2, TDF_SLIM);
447 fprintf (f, " (%1d), ", node->cost);
448 fprintf (f, "\n");
451 else
453 fprintf (f, "Sorted Coalesce list:\n");
454 for (x = cl->num_sorted - 1 ; x >=0; x--)
456 node = cl->sorted[x];
457 fprintf (f, "(%d) ", node->cost);
458 var = ssa_name (node->first_element);
459 print_generic_expr (f, var, TDF_SLIM);
460 fprintf (f, " <-> ");
461 var = ssa_name (node->second_element);
462 print_generic_expr (f, var, TDF_SLIM);
463 fprintf (f, "\n");
469 /* This represents a conflict graph. Implemented as an array of bitmaps.
470 A full matrix is used for conflicts rather than just upper triangular form.
471 this make sit much simpler and faster to perform conflict merges. */
473 typedef struct ssa_conflicts_d
475 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
476 vec<bitmap> conflicts;
477 } * ssa_conflicts_p;
479 /* Return an empty new conflict graph for SIZE elements. */
481 static inline ssa_conflicts_p
482 ssa_conflicts_new (unsigned size)
484 ssa_conflicts_p ptr;
486 ptr = XNEW (struct ssa_conflicts_d);
487 bitmap_obstack_initialize (&ptr->obstack);
488 ptr->conflicts.create (size);
489 ptr->conflicts.safe_grow_cleared (size);
490 return ptr;
494 /* Free storage for conflict graph PTR. */
496 static inline void
497 ssa_conflicts_delete (ssa_conflicts_p ptr)
499 bitmap_obstack_release (&ptr->obstack);
500 ptr->conflicts.release ();
501 free (ptr);
505 /* Test if elements X and Y conflict in graph PTR. */
507 static inline bool
508 ssa_conflicts_test_p (ssa_conflicts_p ptr, unsigned x, unsigned y)
510 bitmap bx = ptr->conflicts[x];
511 bitmap by = ptr->conflicts[y];
513 gcc_checking_assert (x != y);
515 if (bx)
516 /* Avoid the lookup if Y has no conflicts. */
517 return by ? bitmap_bit_p (bx, y) : false;
518 else
519 return false;
523 /* Add a conflict with Y to the bitmap for X in graph PTR. */
525 static inline void
526 ssa_conflicts_add_one (ssa_conflicts_p ptr, unsigned x, unsigned y)
528 bitmap bx = ptr->conflicts[x];
529 /* If there are no conflicts yet, allocate the bitmap and set bit. */
530 if (! bx)
531 bx = ptr->conflicts[x] = BITMAP_ALLOC (&ptr->obstack);
532 bitmap_set_bit (bx, y);
536 /* Add conflicts between X and Y in graph PTR. */
538 static inline void
539 ssa_conflicts_add (ssa_conflicts_p ptr, unsigned x, unsigned y)
541 gcc_checking_assert (x != y);
542 ssa_conflicts_add_one (ptr, x, y);
543 ssa_conflicts_add_one (ptr, y, x);
547 /* Merge all Y's conflict into X in graph PTR. */
549 static inline void
550 ssa_conflicts_merge (ssa_conflicts_p ptr, unsigned x, unsigned y)
552 unsigned z;
553 bitmap_iterator bi;
554 bitmap bx = ptr->conflicts[x];
555 bitmap by = ptr->conflicts[y];
557 gcc_checking_assert (x != y);
558 if (! by)
559 return;
561 /* Add a conflict between X and every one Y has. If the bitmap doesn't
562 exist, then it has already been coalesced, and we don't need to add a
563 conflict. */
564 EXECUTE_IF_SET_IN_BITMAP (by, 0, z, bi)
566 bitmap bz = ptr->conflicts[z];
567 if (bz)
568 bitmap_set_bit (bz, x);
571 if (bx)
573 /* If X has conflicts, add Y's to X. */
574 bitmap_ior_into (bx, by);
575 BITMAP_FREE (by);
576 ptr->conflicts[y] = NULL;
578 else
580 /* If X has no conflicts, simply use Y's. */
581 ptr->conflicts[x] = by;
582 ptr->conflicts[y] = NULL;
587 /* Dump a conflicts graph. */
589 static void
590 ssa_conflicts_dump (FILE *file, ssa_conflicts_p ptr)
592 unsigned x;
593 bitmap b;
595 fprintf (file, "\nConflict graph:\n");
597 FOR_EACH_VEC_ELT (ptr->conflicts, x, b)
598 if (b)
600 fprintf (file, "%d: ", x);
601 dump_bitmap (file, b);
606 /* This structure is used to efficiently record the current status of live
607 SSA_NAMES when building a conflict graph.
608 LIVE_BASE_VAR has a bit set for each base variable which has at least one
609 ssa version live.
610 LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
611 index, and is used to track what partitions of each base variable are
612 live. This makes it easy to add conflicts between just live partitions
613 with the same base variable.
614 The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
615 marked as being live. This delays clearing of these bitmaps until
616 they are actually needed again. */
618 typedef struct live_track_d
620 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
621 bitmap live_base_var; /* Indicates if a basevar is live. */
622 bitmap *live_base_partitions; /* Live partitions for each basevar. */
623 var_map map; /* Var_map being used for partition mapping. */
624 } * live_track_p;
627 /* This routine will create a new live track structure based on the partitions
628 in MAP. */
630 static live_track_p
631 new_live_track (var_map map)
633 live_track_p ptr;
634 int lim, x;
636 /* Make sure there is a partition view in place. */
637 gcc_assert (map->partition_to_base_index != NULL);
639 ptr = (live_track_p) xmalloc (sizeof (struct live_track_d));
640 ptr->map = map;
641 lim = num_basevars (map);
642 bitmap_obstack_initialize (&ptr->obstack);
643 ptr->live_base_partitions = (bitmap *) xmalloc (sizeof (bitmap *) * lim);
644 ptr->live_base_var = BITMAP_ALLOC (&ptr->obstack);
645 for (x = 0; x < lim; x++)
646 ptr->live_base_partitions[x] = BITMAP_ALLOC (&ptr->obstack);
647 return ptr;
651 /* This routine will free the memory associated with PTR. */
653 static void
654 delete_live_track (live_track_p ptr)
656 bitmap_obstack_release (&ptr->obstack);
657 free (ptr->live_base_partitions);
658 free (ptr);
662 /* This function will remove PARTITION from the live list in PTR. */
664 static inline void
665 live_track_remove_partition (live_track_p ptr, int partition)
667 int root;
669 root = basevar_index (ptr->map, partition);
670 bitmap_clear_bit (ptr->live_base_partitions[root], partition);
671 /* If the element list is empty, make the base variable not live either. */
672 if (bitmap_empty_p (ptr->live_base_partitions[root]))
673 bitmap_clear_bit (ptr->live_base_var, root);
677 /* This function will adds PARTITION to the live list in PTR. */
679 static inline void
680 live_track_add_partition (live_track_p ptr, int partition)
682 int root;
684 root = basevar_index (ptr->map, partition);
685 /* If this base var wasn't live before, it is now. Clear the element list
686 since it was delayed until needed. */
687 if (bitmap_set_bit (ptr->live_base_var, root))
688 bitmap_clear (ptr->live_base_partitions[root]);
689 bitmap_set_bit (ptr->live_base_partitions[root], partition);
694 /* Clear the live bit for VAR in PTR. */
696 static inline void
697 live_track_clear_var (live_track_p ptr, tree var)
699 int p;
701 p = var_to_partition (ptr->map, var);
702 if (p != NO_PARTITION)
703 live_track_remove_partition (ptr, p);
707 /* Return TRUE if VAR is live in PTR. */
709 static inline bool
710 live_track_live_p (live_track_p ptr, tree var)
712 int p, root;
714 p = var_to_partition (ptr->map, var);
715 if (p != NO_PARTITION)
717 root = basevar_index (ptr->map, p);
718 if (bitmap_bit_p (ptr->live_base_var, root))
719 return bitmap_bit_p (ptr->live_base_partitions[root], p);
721 return false;
725 /* This routine will add USE to PTR. USE will be marked as live in both the
726 ssa live map and the live bitmap for the root of USE. */
728 static inline void
729 live_track_process_use (live_track_p ptr, tree use)
731 int p;
733 p = var_to_partition (ptr->map, use);
734 if (p == NO_PARTITION)
735 return;
737 /* Mark as live in the appropriate live list. */
738 live_track_add_partition (ptr, p);
742 /* This routine will process a DEF in PTR. DEF will be removed from the live
743 lists, and if there are any other live partitions with the same base
744 variable, conflicts will be added to GRAPH. */
746 static inline void
747 live_track_process_def (live_track_p ptr, tree def, ssa_conflicts_p graph)
749 int p, root;
750 bitmap b;
751 unsigned x;
752 bitmap_iterator bi;
754 p = var_to_partition (ptr->map, def);
755 if (p == NO_PARTITION)
756 return;
758 /* Clear the liveness bit. */
759 live_track_remove_partition (ptr, p);
761 /* If the bitmap isn't empty now, conflicts need to be added. */
762 root = basevar_index (ptr->map, p);
763 if (bitmap_bit_p (ptr->live_base_var, root))
765 b = ptr->live_base_partitions[root];
766 EXECUTE_IF_SET_IN_BITMAP (b, 0, x, bi)
767 ssa_conflicts_add (graph, p, x);
772 /* Initialize PTR with the partitions set in INIT. */
774 static inline void
775 live_track_init (live_track_p ptr, bitmap init)
777 unsigned p;
778 bitmap_iterator bi;
780 /* Mark all live on exit partitions. */
781 EXECUTE_IF_SET_IN_BITMAP (init, 0, p, bi)
782 live_track_add_partition (ptr, p);
786 /* This routine will clear all live partitions in PTR. */
788 static inline void
789 live_track_clear_base_vars (live_track_p ptr)
791 /* Simply clear the live base list. Anything marked as live in the element
792 lists will be cleared later if/when the base variable ever comes alive
793 again. */
794 bitmap_clear (ptr->live_base_var);
798 /* Build a conflict graph based on LIVEINFO. Any partitions which are in the
799 partition view of the var_map liveinfo is based on get entries in the
800 conflict graph. Only conflicts between ssa_name partitions with the same
801 base variable are added. */
803 static ssa_conflicts_p
804 build_ssa_conflict_graph (tree_live_info_p liveinfo)
806 ssa_conflicts_p graph;
807 var_map map;
808 basic_block bb;
809 ssa_op_iter iter;
810 live_track_p live;
812 map = live_var_map (liveinfo);
813 graph = ssa_conflicts_new (num_var_partitions (map));
815 live = new_live_track (map);
817 FOR_EACH_BB (bb)
819 gimple_stmt_iterator gsi;
821 /* Start with live on exit temporaries. */
822 live_track_init (live, live_on_exit (liveinfo, bb));
824 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
826 tree var;
827 gimple stmt = gsi_stmt (gsi);
829 /* A copy between 2 partitions does not introduce an interference
830 by itself. If they did, you would never be able to coalesce
831 two things which are copied. If the two variables really do
832 conflict, they will conflict elsewhere in the program.
834 This is handled by simply removing the SRC of the copy from the
835 live list, and processing the stmt normally. */
836 if (is_gimple_assign (stmt))
838 tree lhs = gimple_assign_lhs (stmt);
839 tree rhs1 = gimple_assign_rhs1 (stmt);
840 if (gimple_assign_copy_p (stmt)
841 && TREE_CODE (lhs) == SSA_NAME
842 && TREE_CODE (rhs1) == SSA_NAME)
843 live_track_clear_var (live, rhs1);
845 else if (is_gimple_debug (stmt))
846 continue;
848 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
849 live_track_process_def (live, var, graph);
851 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
852 live_track_process_use (live, var);
855 /* If result of a PHI is unused, looping over the statements will not
856 record any conflicts since the def was never live. Since the PHI node
857 is going to be translated out of SSA form, it will insert a copy.
858 There must be a conflict recorded between the result of the PHI and
859 any variables that are live. Otherwise the out-of-ssa translation
860 may create incorrect code. */
861 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
863 gimple phi = gsi_stmt (gsi);
864 tree result = PHI_RESULT (phi);
865 if (live_track_live_p (live, result))
866 live_track_process_def (live, result, graph);
869 live_track_clear_base_vars (live);
872 delete_live_track (live);
873 return graph;
877 /* Shortcut routine to print messages to file F of the form:
878 "STR1 EXPR1 STR2 EXPR2 STR3." */
880 static inline void
881 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
882 tree expr2, const char *str3)
884 fprintf (f, "%s", str1);
885 print_generic_expr (f, expr1, TDF_SLIM);
886 fprintf (f, "%s", str2);
887 print_generic_expr (f, expr2, TDF_SLIM);
888 fprintf (f, "%s", str3);
892 /* Print a failure to coalesce a MUST_COALESCE pair X and Y. */
894 static inline void
895 fail_abnormal_edge_coalesce (int x, int y)
897 fprintf (stderr, "\nUnable to coalesce ssa_names %d and %d",x, y);
898 fprintf (stderr, " which are marked as MUST COALESCE.\n");
899 print_generic_expr (stderr, ssa_name (x), TDF_SLIM);
900 fprintf (stderr, " and ");
901 print_generic_stmt (stderr, ssa_name (y), TDF_SLIM);
903 internal_error ("SSA corruption");
907 /* This function creates a var_map for the current function as well as creating
908 a coalesce list for use later in the out of ssa process. */
910 static var_map
911 create_outofssa_var_map (coalesce_list_p cl, bitmap used_in_copy)
913 gimple_stmt_iterator gsi;
914 basic_block bb;
915 tree var;
916 gimple stmt;
917 tree first;
918 var_map map;
919 ssa_op_iter iter;
920 int v1, v2, cost;
921 unsigned i;
923 map = init_var_map (num_ssa_names);
925 FOR_EACH_BB (bb)
927 tree arg;
929 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
931 gimple phi = gsi_stmt (gsi);
932 size_t i;
933 int ver;
934 tree res;
935 bool saw_copy = false;
937 res = gimple_phi_result (phi);
938 ver = SSA_NAME_VERSION (res);
939 register_ssa_partition (map, res);
941 /* Register ssa_names and coalesces between the args and the result
942 of all PHI. */
943 for (i = 0; i < gimple_phi_num_args (phi); i++)
945 edge e = gimple_phi_arg_edge (phi, i);
946 arg = PHI_ARG_DEF (phi, i);
947 if (TREE_CODE (arg) != SSA_NAME)
948 continue;
950 register_ssa_partition (map, arg);
951 if (gimple_can_coalesce_p (arg, res)
952 || (e->flags & EDGE_ABNORMAL))
954 saw_copy = true;
955 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (arg));
956 if ((e->flags & EDGE_ABNORMAL) == 0)
958 int cost = coalesce_cost_edge (e);
959 if (cost == 1 && has_single_use (arg))
960 add_cost_one_coalesce (cl, ver, SSA_NAME_VERSION (arg));
961 else
962 add_coalesce (cl, ver, SSA_NAME_VERSION (arg), cost);
966 if (saw_copy)
967 bitmap_set_bit (used_in_copy, ver);
970 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
972 stmt = gsi_stmt (gsi);
974 if (is_gimple_debug (stmt))
975 continue;
977 /* Register USE and DEF operands in each statement. */
978 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
979 register_ssa_partition (map, var);
981 /* Check for copy coalesces. */
982 switch (gimple_code (stmt))
984 case GIMPLE_ASSIGN:
986 tree lhs = gimple_assign_lhs (stmt);
987 tree rhs1 = gimple_assign_rhs1 (stmt);
988 if (gimple_assign_ssa_name_copy_p (stmt)
989 && gimple_can_coalesce_p (lhs, 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 (gimple_can_coalesce_p (outputs[match], input))
1042 cost = coalesce_cost (REG_BR_PROB_BASE,
1043 optimize_bb_for_size_p (bb));
1044 add_coalesce (cl, v1, v2, cost);
1045 bitmap_set_bit (used_in_copy, v1);
1046 bitmap_set_bit (used_in_copy, v2);
1049 break;
1052 default:
1053 break;
1058 /* Now process result decls and live on entry variables for entry into
1059 the coalesce list. */
1060 first = NULL_TREE;
1061 for (i = 1; i < num_ssa_names; i++)
1063 var = ssa_name (i);
1064 if (var != NULL_TREE && !virtual_operand_p (var))
1066 /* Add coalesces between all the result decls. */
1067 if (SSA_NAME_VAR (var)
1068 && TREE_CODE (SSA_NAME_VAR (var)) == RESULT_DECL)
1070 if (first == NULL_TREE)
1071 first = var;
1072 else
1074 gcc_assert (gimple_can_coalesce_p (var, first));
1075 v1 = SSA_NAME_VERSION (first);
1076 v2 = SSA_NAME_VERSION (var);
1077 bitmap_set_bit (used_in_copy, v1);
1078 bitmap_set_bit (used_in_copy, v2);
1079 cost = coalesce_cost_bb (EXIT_BLOCK_PTR);
1080 add_coalesce (cl, v1, v2, cost);
1083 /* Mark any default_def variables as being in the coalesce list
1084 since they will have to be coalesced with the base variable. If
1085 not marked as present, they won't be in the coalesce view. */
1086 if (SSA_NAME_IS_DEFAULT_DEF (var)
1087 && !has_zero_uses (var))
1088 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1092 return map;
1096 /* Attempt to coalesce ssa versions X and Y together using the partition
1097 mapping in MAP and checking conflicts in GRAPH. Output any debug info to
1098 DEBUG, if it is nun-NULL. */
1100 static inline bool
1101 attempt_coalesce (var_map map, ssa_conflicts_p graph, int x, int y,
1102 FILE *debug)
1104 int z;
1105 tree var1, var2;
1106 int p1, p2;
1108 p1 = var_to_partition (map, ssa_name (x));
1109 p2 = var_to_partition (map, ssa_name (y));
1111 if (debug)
1113 fprintf (debug, "(%d)", x);
1114 print_generic_expr (debug, partition_to_var (map, p1), TDF_SLIM);
1115 fprintf (debug, " & (%d)", y);
1116 print_generic_expr (debug, partition_to_var (map, p2), TDF_SLIM);
1119 if (p1 == p2)
1121 if (debug)
1122 fprintf (debug, ": Already Coalesced.\n");
1123 return true;
1126 if (debug)
1127 fprintf (debug, " [map: %d, %d] ", p1, p2);
1130 if (!ssa_conflicts_test_p (graph, p1, p2))
1132 var1 = partition_to_var (map, p1);
1133 var2 = partition_to_var (map, p2);
1134 z = var_union (map, var1, var2);
1135 if (z == NO_PARTITION)
1137 if (debug)
1138 fprintf (debug, ": Unable to perform partition union.\n");
1139 return false;
1142 /* z is the new combined partition. Remove the other partition from
1143 the list, and merge the conflicts. */
1144 if (z == p1)
1145 ssa_conflicts_merge (graph, p1, p2);
1146 else
1147 ssa_conflicts_merge (graph, p2, p1);
1149 if (debug)
1150 fprintf (debug, ": Success -> %d\n", z);
1151 return true;
1154 if (debug)
1155 fprintf (debug, ": Fail due to conflict\n");
1157 return false;
1161 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1162 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1164 static void
1165 coalesce_partitions (var_map map, ssa_conflicts_p graph, coalesce_list_p cl,
1166 FILE *debug)
1168 int x = 0, y = 0;
1169 tree var1, var2;
1170 int cost;
1171 basic_block bb;
1172 edge e;
1173 edge_iterator ei;
1175 /* First, coalesce all the copies across abnormal edges. These are not placed
1176 in the coalesce list because they do not need to be sorted, and simply
1177 consume extra memory/compilation time in large programs. */
1179 FOR_EACH_BB (bb)
1181 FOR_EACH_EDGE (e, ei, bb->preds)
1182 if (e->flags & EDGE_ABNORMAL)
1184 gimple_stmt_iterator gsi;
1185 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1186 gsi_next (&gsi))
1188 gimple phi = gsi_stmt (gsi);
1189 tree res = PHI_RESULT (phi);
1190 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1191 int v1 = SSA_NAME_VERSION (res);
1192 int v2 = SSA_NAME_VERSION (arg);
1194 if (debug)
1195 fprintf (debug, "Abnormal coalesce: ");
1197 if (!attempt_coalesce (map, graph, v1, v2, debug))
1198 fail_abnormal_edge_coalesce (v1, v2);
1203 /* Now process the items in the coalesce list. */
1205 while ((cost = pop_best_coalesce (cl, &x, &y)) != NO_BEST_COALESCE)
1207 var1 = ssa_name (x);
1208 var2 = ssa_name (y);
1210 /* Assert the coalesces have the same base variable. */
1211 gcc_assert (gimple_can_coalesce_p (var1, var2));
1213 if (debug)
1214 fprintf (debug, "Coalesce list: ");
1215 attempt_coalesce (map, graph, x, y, debug);
1220 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1222 struct ssa_name_var_hash : typed_noop_remove <tree_node>
1224 typedef union tree_node value_type;
1225 typedef union tree_node compare_type;
1226 static inline hashval_t hash (const value_type *);
1227 static inline int equal (const value_type *, const compare_type *);
1230 inline hashval_t
1231 ssa_name_var_hash::hash (const_tree n)
1233 return DECL_UID (SSA_NAME_VAR (n));
1236 inline int
1237 ssa_name_var_hash::equal (const value_type *n1, const compare_type *n2)
1239 return SSA_NAME_VAR (n1) == SSA_NAME_VAR (n2);
1243 /* Reduce the number of copies by coalescing variables in the function. Return
1244 a partition map with the resulting coalesces. */
1246 extern var_map
1247 coalesce_ssa_name (void)
1249 tree_live_info_p liveinfo;
1250 ssa_conflicts_p graph;
1251 coalesce_list_p cl;
1252 bitmap used_in_copies = BITMAP_ALLOC (NULL);
1253 var_map map;
1254 unsigned int i;
1256 cl = create_coalesce_list ();
1257 map = create_outofssa_var_map (cl, used_in_copies);
1259 /* We need to coalesce all names originating same SSA_NAME_VAR
1260 so debug info remains undisturbed. */
1261 if (!optimize)
1263 hash_table <ssa_name_var_hash> ssa_name_hash;
1265 ssa_name_hash.create (10);
1266 for (i = 1; i < num_ssa_names; i++)
1268 tree a = ssa_name (i);
1270 if (a
1271 && SSA_NAME_VAR (a)
1272 && !DECL_IGNORED_P (SSA_NAME_VAR (a))
1273 && (!has_zero_uses (a) || !SSA_NAME_IS_DEFAULT_DEF (a)))
1275 tree *slot = ssa_name_hash.find_slot (a, INSERT);
1277 if (!*slot)
1278 *slot = a;
1279 else
1281 add_coalesce (cl, SSA_NAME_VERSION (a), SSA_NAME_VERSION (*slot),
1282 MUST_COALESCE_COST - 1);
1283 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (a));
1284 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (*slot));
1288 ssa_name_hash.dispose ();
1290 if (dump_file && (dump_flags & TDF_DETAILS))
1291 dump_var_map (dump_file, map);
1293 /* Don't calculate live ranges for variables not in the coalesce list. */
1294 partition_view_bitmap (map, used_in_copies, true);
1295 BITMAP_FREE (used_in_copies);
1297 if (num_var_partitions (map) < 1)
1299 delete_coalesce_list (cl);
1300 return map;
1303 if (dump_file && (dump_flags & TDF_DETAILS))
1304 dump_var_map (dump_file, map);
1306 liveinfo = calculate_live_ranges (map);
1308 if (dump_file && (dump_flags & TDF_DETAILS))
1309 dump_live_info (dump_file, liveinfo, LIVEDUMP_ENTRY);
1311 /* Build a conflict graph. */
1312 graph = build_ssa_conflict_graph (liveinfo);
1313 delete_tree_live_info (liveinfo);
1314 if (dump_file && (dump_flags & TDF_DETAILS))
1315 ssa_conflicts_dump (dump_file, graph);
1317 sort_coalesce_list (cl);
1319 if (dump_file && (dump_flags & TDF_DETAILS))
1321 fprintf (dump_file, "\nAfter sorting:\n");
1322 dump_coalesce_list (dump_file, cl);
1325 /* First, coalesce all live on entry variables to their base variable.
1326 This will ensure the first use is coming from the correct location. */
1328 if (dump_file && (dump_flags & TDF_DETAILS))
1329 dump_var_map (dump_file, map);
1331 /* Now coalesce everything in the list. */
1332 coalesce_partitions (map, graph, cl,
1333 ((dump_flags & TDF_DETAILS) ? dump_file
1334 : NULL));
1336 delete_coalesce_list (cl);
1337 ssa_conflicts_delete (graph);
1339 return map;