gcc/
[official-gcc.git] / gcc / tree-ssa-coalesce.c
blobee30c5ca9e81fec0a745650a5da00c424c1e1f13
1 /* Coalesce SSA_NAMES together for the out-of-ssa pass.
2 Copyright (C) 2004-2015 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 "alias.h"
26 #include "symtab.h"
27 #include "tree.h"
28 #include "fold-const.h"
29 #include "flags.h"
30 #include "tree-pretty-print.h"
31 #include "bitmap.h"
32 #include "dumpfile.h"
33 #include "predict.h"
34 #include "hard-reg-set.h"
35 #include "function.h"
36 #include "dominance.h"
37 #include "cfg.h"
38 #include "basic-block.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-expr.h"
42 #include "gimple.h"
43 #include "gimple-iterator.h"
44 #include "gimple-ssa.h"
45 #include "tree-phinodes.h"
46 #include "ssa-iterators.h"
47 #include "stringpool.h"
48 #include "tree-ssanames.h"
49 #include "tree-ssa-live.h"
50 #include "tree-ssa-coalesce.h"
51 #include "diagnostic-core.h"
54 /* This set of routines implements a coalesce_list. This is an object which
55 is used to track pairs of ssa_names which are desirable to coalesce
56 together to avoid copies. Costs are associated with each pair, and when
57 all desired information has been collected, the object can be used to
58 order the pairs for processing. */
60 /* This structure defines a pair entry. */
62 typedef struct coalesce_pair
64 int first_element;
65 int second_element;
66 int cost;
67 } * coalesce_pair_p;
68 typedef const struct coalesce_pair *const_coalesce_pair_p;
70 /* Coalesce pair hashtable helpers. */
72 struct coalesce_pair_hasher : nofree_ptr_hash <coalesce_pair>
74 static inline hashval_t hash (const coalesce_pair *);
75 static inline bool equal (const coalesce_pair *, const coalesce_pair *);
78 /* Hash function for coalesce list. Calculate hash for PAIR. */
80 inline hashval_t
81 coalesce_pair_hasher::hash (const coalesce_pair *pair)
83 hashval_t a = (hashval_t)(pair->first_element);
84 hashval_t b = (hashval_t)(pair->second_element);
86 return b * (b - 1) / 2 + a;
89 /* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
90 returning TRUE if the two pairs are equivalent. */
92 inline bool
93 coalesce_pair_hasher::equal (const coalesce_pair *p1, const coalesce_pair *p2)
95 return (p1->first_element == p2->first_element
96 && p1->second_element == p2->second_element);
99 typedef hash_table<coalesce_pair_hasher> coalesce_table_type;
100 typedef coalesce_table_type::iterator coalesce_iterator_type;
103 typedef struct cost_one_pair_d
105 int first_element;
106 int second_element;
107 struct cost_one_pair_d *next;
108 } * cost_one_pair_p;
110 /* This structure maintains the list of coalesce pairs. */
112 typedef struct coalesce_list_d
114 coalesce_table_type *list; /* Hash table. */
115 coalesce_pair_p *sorted; /* List when sorted. */
116 int num_sorted; /* Number in the sorted list. */
117 cost_one_pair_p cost_one_list;/* Single use coalesces with cost 1. */
118 } *coalesce_list_p;
120 #define NO_BEST_COALESCE -1
121 #define MUST_COALESCE_COST INT_MAX
124 /* Return cost of execution of copy instruction with FREQUENCY. */
126 static inline int
127 coalesce_cost (int frequency, bool optimize_for_size)
129 /* Base costs on BB frequencies bounded by 1. */
130 int cost = frequency;
132 if (!cost)
133 cost = 1;
135 if (optimize_for_size)
136 cost = 1;
138 return cost;
142 /* Return the cost of executing a copy instruction in basic block BB. */
144 static inline int
145 coalesce_cost_bb (basic_block bb)
147 return coalesce_cost (bb->frequency, optimize_bb_for_size_p (bb));
151 /* Return the cost of executing a copy instruction on edge E. */
153 static inline int
154 coalesce_cost_edge (edge e)
156 int mult = 1;
158 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
159 if (EDGE_CRITICAL_P (e))
160 mult = 2;
161 if (e->flags & EDGE_ABNORMAL)
162 return MUST_COALESCE_COST;
163 if (e->flags & EDGE_EH)
165 edge e2;
166 edge_iterator ei;
167 FOR_EACH_EDGE (e2, ei, e->dest->preds)
168 if (e2 != e)
170 /* Putting code on EH edge that leads to BB
171 with multiple predecestors imply splitting of
172 edge too. */
173 if (mult < 2)
174 mult = 2;
175 /* If there are multiple EH predecestors, we
176 also copy EH regions and produce separate
177 landing pad. This is expensive. */
178 if (e2->flags & EDGE_EH)
180 mult = 5;
181 break;
186 return coalesce_cost (EDGE_FREQUENCY (e),
187 optimize_edge_for_size_p (e)) * mult;
191 /* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
192 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
193 NO_BEST_COALESCE is returned if there aren't any. */
195 static inline int
196 pop_cost_one_pair (coalesce_list_p cl, int *p1, int *p2)
198 cost_one_pair_p ptr;
200 ptr = cl->cost_one_list;
201 if (!ptr)
202 return NO_BEST_COALESCE;
204 *p1 = ptr->first_element;
205 *p2 = ptr->second_element;
206 cl->cost_one_list = ptr->next;
208 free (ptr);
210 return 1;
213 /* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
214 2 elements via P1 and P2. Their calculated cost is returned by the function.
215 NO_BEST_COALESCE is returned if the coalesce list is empty. */
217 static inline int
218 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
220 coalesce_pair_p node;
221 int ret;
223 if (cl->sorted == NULL)
224 return pop_cost_one_pair (cl, p1, p2);
226 if (cl->num_sorted == 0)
227 return pop_cost_one_pair (cl, p1, p2);
229 node = cl->sorted[--(cl->num_sorted)];
230 *p1 = node->first_element;
231 *p2 = node->second_element;
232 ret = node->cost;
233 free (node);
235 return ret;
239 /* Create a new empty coalesce list object and return it. */
241 static inline coalesce_list_p
242 create_coalesce_list (void)
244 coalesce_list_p list;
245 unsigned size = num_ssa_names * 3;
247 if (size < 40)
248 size = 40;
250 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
251 list->list = new coalesce_table_type (size);
252 list->sorted = NULL;
253 list->num_sorted = 0;
254 list->cost_one_list = NULL;
255 return list;
259 /* Delete coalesce list CL. */
261 static inline void
262 delete_coalesce_list (coalesce_list_p cl)
264 gcc_assert (cl->cost_one_list == NULL);
265 delete cl->list;
266 cl->list = NULL;
267 free (cl->sorted);
268 gcc_assert (cl->num_sorted == 0);
269 free (cl);
273 /* Find a matching coalesce pair object in CL for the pair P1 and P2. If
274 one isn't found, return NULL if CREATE is false, otherwise create a new
275 coalesce pair object and return it. */
277 static coalesce_pair_p
278 find_coalesce_pair (coalesce_list_p cl, int p1, int p2, bool create)
280 struct coalesce_pair p;
281 coalesce_pair **slot;
282 unsigned int hash;
284 /* Normalize so that p1 is the smaller value. */
285 if (p2 < p1)
287 p.first_element = p2;
288 p.second_element = p1;
290 else
292 p.first_element = p1;
293 p.second_element = p2;
296 hash = coalesce_pair_hasher::hash (&p);
297 slot = cl->list->find_slot_with_hash (&p, hash, create ? INSERT : NO_INSERT);
298 if (!slot)
299 return NULL;
301 if (!*slot)
303 struct coalesce_pair * pair = XNEW (struct coalesce_pair);
304 gcc_assert (cl->sorted == NULL);
305 pair->first_element = p.first_element;
306 pair->second_element = p.second_element;
307 pair->cost = 0;
308 *slot = pair;
311 return (struct coalesce_pair *) *slot;
314 static inline void
315 add_cost_one_coalesce (coalesce_list_p cl, int p1, int p2)
317 cost_one_pair_p pair;
319 pair = XNEW (struct cost_one_pair_d);
320 pair->first_element = p1;
321 pair->second_element = p2;
322 pair->next = cl->cost_one_list;
323 cl->cost_one_list = pair;
327 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
329 static inline void
330 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
332 coalesce_pair_p node;
334 gcc_assert (cl->sorted == NULL);
335 if (p1 == p2)
336 return;
338 node = find_coalesce_pair (cl, p1, p2, true);
340 /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way. */
341 if (node->cost < MUST_COALESCE_COST - 1)
343 if (value < MUST_COALESCE_COST - 1)
344 node->cost += value;
345 else
346 node->cost = value;
351 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
353 static int
354 compare_pairs (const void *p1, const void *p2)
356 const_coalesce_pair_p const *const pp1 = (const_coalesce_pair_p const *) p1;
357 const_coalesce_pair_p const *const pp2 = (const_coalesce_pair_p const *) p2;
358 int result;
360 result = (* pp1)->cost - (* pp2)->cost;
361 /* Since qsort does not guarantee stability we use the elements
362 as a secondary key. This provides us with independence from
363 the host's implementation of the sorting algorithm. */
364 if (result == 0)
366 result = (* pp2)->first_element - (* pp1)->first_element;
367 if (result == 0)
368 result = (* pp2)->second_element - (* pp1)->second_element;
371 return result;
375 /* Return the number of unique coalesce pairs in CL. */
377 static inline int
378 num_coalesce_pairs (coalesce_list_p cl)
380 return cl->list->elements ();
384 /* Iterate over CL using ITER, returning values in PAIR. */
386 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
387 FOR_EACH_HASH_TABLE_ELEMENT (*(CL)->list, (PAIR), coalesce_pair_p, (ITER))
390 /* Prepare CL for removal of preferred pairs. When finished they are sorted
391 in order from most important coalesce to least important. */
393 static void
394 sort_coalesce_list (coalesce_list_p cl)
396 unsigned x, num;
397 coalesce_pair_p p;
398 coalesce_iterator_type ppi;
400 gcc_assert (cl->sorted == NULL);
402 num = num_coalesce_pairs (cl);
403 cl->num_sorted = num;
404 if (num == 0)
405 return;
407 /* Allocate a vector for the pair pointers. */
408 cl->sorted = XNEWVEC (coalesce_pair_p, num);
410 /* Populate the vector with pointers to the pairs. */
411 x = 0;
412 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
413 cl->sorted[x++] = p;
414 gcc_assert (x == num);
416 /* Already sorted. */
417 if (num == 1)
418 return;
420 /* If there are only 2, just pick swap them if the order isn't correct. */
421 if (num == 2)
423 if (cl->sorted[0]->cost > cl->sorted[1]->cost)
424 std::swap (cl->sorted[0], cl->sorted[1]);
425 return;
428 /* Only call qsort if there are more than 2 items.
429 ??? Maybe std::sort will do better, provided that compare_pairs
430 can be inlined. */
431 if (num > 2)
432 qsort (cl->sorted, num, sizeof (coalesce_pair_p), compare_pairs);
436 /* Send debug info for coalesce list CL to file F. */
438 static void
439 dump_coalesce_list (FILE *f, coalesce_list_p cl)
441 coalesce_pair_p node;
442 coalesce_iterator_type ppi;
444 int x;
445 tree var;
447 if (cl->sorted == NULL)
449 fprintf (f, "Coalesce List:\n");
450 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
452 tree var1 = ssa_name (node->first_element);
453 tree var2 = ssa_name (node->second_element);
454 print_generic_expr (f, var1, TDF_SLIM);
455 fprintf (f, " <-> ");
456 print_generic_expr (f, var2, TDF_SLIM);
457 fprintf (f, " (%1d), ", node->cost);
458 fprintf (f, "\n");
461 else
463 fprintf (f, "Sorted Coalesce list:\n");
464 for (x = cl->num_sorted - 1 ; x >=0; x--)
466 node = cl->sorted[x];
467 fprintf (f, "(%d) ", node->cost);
468 var = ssa_name (node->first_element);
469 print_generic_expr (f, var, TDF_SLIM);
470 fprintf (f, " <-> ");
471 var = ssa_name (node->second_element);
472 print_generic_expr (f, var, TDF_SLIM);
473 fprintf (f, "\n");
479 /* This represents a conflict graph. Implemented as an array of bitmaps.
480 A full matrix is used for conflicts rather than just upper triangular form.
481 this make sit much simpler and faster to perform conflict merges. */
483 typedef struct ssa_conflicts_d
485 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
486 vec<bitmap> conflicts;
487 } * ssa_conflicts_p;
489 /* Return an empty new conflict graph for SIZE elements. */
491 static inline ssa_conflicts_p
492 ssa_conflicts_new (unsigned size)
494 ssa_conflicts_p ptr;
496 ptr = XNEW (struct ssa_conflicts_d);
497 bitmap_obstack_initialize (&ptr->obstack);
498 ptr->conflicts.create (size);
499 ptr->conflicts.safe_grow_cleared (size);
500 return ptr;
504 /* Free storage for conflict graph PTR. */
506 static inline void
507 ssa_conflicts_delete (ssa_conflicts_p ptr)
509 bitmap_obstack_release (&ptr->obstack);
510 ptr->conflicts.release ();
511 free (ptr);
515 /* Test if elements X and Y conflict in graph PTR. */
517 static inline bool
518 ssa_conflicts_test_p (ssa_conflicts_p ptr, unsigned x, unsigned y)
520 bitmap bx = ptr->conflicts[x];
521 bitmap by = ptr->conflicts[y];
523 gcc_checking_assert (x != y);
525 if (bx)
526 /* Avoid the lookup if Y has no conflicts. */
527 return by ? bitmap_bit_p (bx, y) : false;
528 else
529 return false;
533 /* Add a conflict with Y to the bitmap for X in graph PTR. */
535 static inline void
536 ssa_conflicts_add_one (ssa_conflicts_p ptr, unsigned x, unsigned y)
538 bitmap bx = ptr->conflicts[x];
539 /* If there are no conflicts yet, allocate the bitmap and set bit. */
540 if (! bx)
541 bx = ptr->conflicts[x] = BITMAP_ALLOC (&ptr->obstack);
542 bitmap_set_bit (bx, y);
546 /* Add conflicts between X and Y in graph PTR. */
548 static inline void
549 ssa_conflicts_add (ssa_conflicts_p ptr, unsigned x, unsigned y)
551 gcc_checking_assert (x != y);
552 ssa_conflicts_add_one (ptr, x, y);
553 ssa_conflicts_add_one (ptr, y, x);
557 /* Merge all Y's conflict into X in graph PTR. */
559 static inline void
560 ssa_conflicts_merge (ssa_conflicts_p ptr, unsigned x, unsigned y)
562 unsigned z;
563 bitmap_iterator bi;
564 bitmap bx = ptr->conflicts[x];
565 bitmap by = ptr->conflicts[y];
567 gcc_checking_assert (x != y);
568 if (! by)
569 return;
571 /* Add a conflict between X and every one Y has. If the bitmap doesn't
572 exist, then it has already been coalesced, and we don't need to add a
573 conflict. */
574 EXECUTE_IF_SET_IN_BITMAP (by, 0, z, bi)
576 bitmap bz = ptr->conflicts[z];
577 if (bz)
578 bitmap_set_bit (bz, x);
581 if (bx)
583 /* If X has conflicts, add Y's to X. */
584 bitmap_ior_into (bx, by);
585 BITMAP_FREE (by);
586 ptr->conflicts[y] = NULL;
588 else
590 /* If X has no conflicts, simply use Y's. */
591 ptr->conflicts[x] = by;
592 ptr->conflicts[y] = NULL;
597 /* Dump a conflicts graph. */
599 static void
600 ssa_conflicts_dump (FILE *file, ssa_conflicts_p ptr)
602 unsigned x;
603 bitmap b;
605 fprintf (file, "\nConflict graph:\n");
607 FOR_EACH_VEC_ELT (ptr->conflicts, x, b)
608 if (b)
610 fprintf (file, "%d: ", x);
611 dump_bitmap (file, b);
616 /* This structure is used to efficiently record the current status of live
617 SSA_NAMES when building a conflict graph.
618 LIVE_BASE_VAR has a bit set for each base variable which has at least one
619 ssa version live.
620 LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
621 index, and is used to track what partitions of each base variable are
622 live. This makes it easy to add conflicts between just live partitions
623 with the same base variable.
624 The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
625 marked as being live. This delays clearing of these bitmaps until
626 they are actually needed again. */
628 typedef struct live_track_d
630 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
631 bitmap live_base_var; /* Indicates if a basevar is live. */
632 bitmap *live_base_partitions; /* Live partitions for each basevar. */
633 var_map map; /* Var_map being used for partition mapping. */
634 } * live_track_p;
637 /* This routine will create a new live track structure based on the partitions
638 in MAP. */
640 static live_track_p
641 new_live_track (var_map map)
643 live_track_p ptr;
644 int lim, x;
646 /* Make sure there is a partition view in place. */
647 gcc_assert (map->partition_to_base_index != NULL);
649 ptr = (live_track_p) xmalloc (sizeof (struct live_track_d));
650 ptr->map = map;
651 lim = num_basevars (map);
652 bitmap_obstack_initialize (&ptr->obstack);
653 ptr->live_base_partitions = (bitmap *) xmalloc (sizeof (bitmap *) * lim);
654 ptr->live_base_var = BITMAP_ALLOC (&ptr->obstack);
655 for (x = 0; x < lim; x++)
656 ptr->live_base_partitions[x] = BITMAP_ALLOC (&ptr->obstack);
657 return ptr;
661 /* This routine will free the memory associated with PTR. */
663 static void
664 delete_live_track (live_track_p ptr)
666 bitmap_obstack_release (&ptr->obstack);
667 free (ptr->live_base_partitions);
668 free (ptr);
672 /* This function will remove PARTITION from the live list in PTR. */
674 static inline void
675 live_track_remove_partition (live_track_p ptr, int partition)
677 int root;
679 root = basevar_index (ptr->map, partition);
680 bitmap_clear_bit (ptr->live_base_partitions[root], partition);
681 /* If the element list is empty, make the base variable not live either. */
682 if (bitmap_empty_p (ptr->live_base_partitions[root]))
683 bitmap_clear_bit (ptr->live_base_var, root);
687 /* This function will adds PARTITION to the live list in PTR. */
689 static inline void
690 live_track_add_partition (live_track_p ptr, int partition)
692 int root;
694 root = basevar_index (ptr->map, partition);
695 /* If this base var wasn't live before, it is now. Clear the element list
696 since it was delayed until needed. */
697 if (bitmap_set_bit (ptr->live_base_var, root))
698 bitmap_clear (ptr->live_base_partitions[root]);
699 bitmap_set_bit (ptr->live_base_partitions[root], partition);
704 /* Clear the live bit for VAR in PTR. */
706 static inline void
707 live_track_clear_var (live_track_p ptr, tree var)
709 int p;
711 p = var_to_partition (ptr->map, var);
712 if (p != NO_PARTITION)
713 live_track_remove_partition (ptr, p);
717 /* Return TRUE if VAR is live in PTR. */
719 static inline bool
720 live_track_live_p (live_track_p ptr, tree var)
722 int p, root;
724 p = var_to_partition (ptr->map, var);
725 if (p != NO_PARTITION)
727 root = basevar_index (ptr->map, p);
728 if (bitmap_bit_p (ptr->live_base_var, root))
729 return bitmap_bit_p (ptr->live_base_partitions[root], p);
731 return false;
735 /* This routine will add USE to PTR. USE will be marked as live in both the
736 ssa live map and the live bitmap for the root of USE. */
738 static inline void
739 live_track_process_use (live_track_p ptr, tree use)
741 int p;
743 p = var_to_partition (ptr->map, use);
744 if (p == NO_PARTITION)
745 return;
747 /* Mark as live in the appropriate live list. */
748 live_track_add_partition (ptr, p);
752 /* This routine will process a DEF in PTR. DEF will be removed from the live
753 lists, and if there are any other live partitions with the same base
754 variable, conflicts will be added to GRAPH. */
756 static inline void
757 live_track_process_def (live_track_p ptr, tree def, ssa_conflicts_p graph)
759 int p, root;
760 bitmap b;
761 unsigned x;
762 bitmap_iterator bi;
764 p = var_to_partition (ptr->map, def);
765 if (p == NO_PARTITION)
766 return;
768 /* Clear the liveness bit. */
769 live_track_remove_partition (ptr, p);
771 /* If the bitmap isn't empty now, conflicts need to be added. */
772 root = basevar_index (ptr->map, p);
773 if (bitmap_bit_p (ptr->live_base_var, root))
775 b = ptr->live_base_partitions[root];
776 EXECUTE_IF_SET_IN_BITMAP (b, 0, x, bi)
777 ssa_conflicts_add (graph, p, x);
782 /* Initialize PTR with the partitions set in INIT. */
784 static inline void
785 live_track_init (live_track_p ptr, bitmap init)
787 unsigned p;
788 bitmap_iterator bi;
790 /* Mark all live on exit partitions. */
791 EXECUTE_IF_SET_IN_BITMAP (init, 0, p, bi)
792 live_track_add_partition (ptr, p);
796 /* This routine will clear all live partitions in PTR. */
798 static inline void
799 live_track_clear_base_vars (live_track_p ptr)
801 /* Simply clear the live base list. Anything marked as live in the element
802 lists will be cleared later if/when the base variable ever comes alive
803 again. */
804 bitmap_clear (ptr->live_base_var);
808 /* Build a conflict graph based on LIVEINFO. Any partitions which are in the
809 partition view of the var_map liveinfo is based on get entries in the
810 conflict graph. Only conflicts between ssa_name partitions with the same
811 base variable are added. */
813 static ssa_conflicts_p
814 build_ssa_conflict_graph (tree_live_info_p liveinfo)
816 ssa_conflicts_p graph;
817 var_map map;
818 basic_block bb;
819 ssa_op_iter iter;
820 live_track_p live;
822 map = live_var_map (liveinfo);
823 graph = ssa_conflicts_new (num_var_partitions (map));
825 live = new_live_track (map);
827 FOR_EACH_BB_FN (bb, cfun)
829 /* Start with live on exit temporaries. */
830 live_track_init (live, live_on_exit (liveinfo, bb));
832 for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);
833 gsi_prev (&gsi))
835 tree var;
836 gimple stmt = gsi_stmt (gsi);
838 /* A copy between 2 partitions does not introduce an interference
839 by itself. If they did, you would never be able to coalesce
840 two things which are copied. If the two variables really do
841 conflict, they will conflict elsewhere in the program.
843 This is handled by simply removing the SRC of the copy from the
844 live list, and processing the stmt normally. */
845 if (is_gimple_assign (stmt))
847 tree lhs = gimple_assign_lhs (stmt);
848 tree rhs1 = gimple_assign_rhs1 (stmt);
849 if (gimple_assign_copy_p (stmt)
850 && TREE_CODE (lhs) == SSA_NAME
851 && TREE_CODE (rhs1) == SSA_NAME)
852 live_track_clear_var (live, rhs1);
854 else if (is_gimple_debug (stmt))
855 continue;
857 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
858 live_track_process_def (live, var, graph);
860 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
861 live_track_process_use (live, var);
864 /* If result of a PHI is unused, looping over the statements will not
865 record any conflicts since the def was never live. Since the PHI node
866 is going to be translated out of SSA form, it will insert a copy.
867 There must be a conflict recorded between the result of the PHI and
868 any variables that are live. Otherwise the out-of-ssa translation
869 may create incorrect code. */
870 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
871 gsi_next (&gsi))
873 gphi *phi = gsi.phi ();
874 tree result = PHI_RESULT (phi);
875 if (live_track_live_p (live, result))
876 live_track_process_def (live, result, graph);
879 live_track_clear_base_vars (live);
882 delete_live_track (live);
883 return graph;
887 /* Shortcut routine to print messages to file F of the form:
888 "STR1 EXPR1 STR2 EXPR2 STR3." */
890 static inline void
891 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
892 tree expr2, const char *str3)
894 fprintf (f, "%s", str1);
895 print_generic_expr (f, expr1, TDF_SLIM);
896 fprintf (f, "%s", str2);
897 print_generic_expr (f, expr2, TDF_SLIM);
898 fprintf (f, "%s", str3);
902 /* Print a failure to coalesce a MUST_COALESCE pair X and Y. */
904 static inline void
905 fail_abnormal_edge_coalesce (int x, int y)
907 fprintf (stderr, "\nUnable to coalesce ssa_names %d and %d",x, y);
908 fprintf (stderr, " which are marked as MUST COALESCE.\n");
909 print_generic_expr (stderr, ssa_name (x), TDF_SLIM);
910 fprintf (stderr, " and ");
911 print_generic_stmt (stderr, ssa_name (y), TDF_SLIM);
913 internal_error ("SSA corruption");
917 /* This function creates a var_map for the current function as well as creating
918 a coalesce list for use later in the out of ssa process. */
920 static var_map
921 create_outofssa_var_map (coalesce_list_p cl, bitmap used_in_copy)
923 gimple_stmt_iterator gsi;
924 basic_block bb;
925 tree var;
926 gimple stmt;
927 tree first;
928 var_map map;
929 ssa_op_iter iter;
930 int v1, v2, cost;
931 unsigned i;
933 map = init_var_map (num_ssa_names);
935 FOR_EACH_BB_FN (bb, cfun)
937 tree arg;
939 for (gphi_iterator gpi = gsi_start_phis (bb);
940 !gsi_end_p (gpi);
941 gsi_next (&gpi))
943 gphi *phi = gpi.phi ();
944 size_t i;
945 int ver;
946 tree res;
947 bool saw_copy = false;
949 res = gimple_phi_result (phi);
950 ver = SSA_NAME_VERSION (res);
951 register_ssa_partition (map, res);
953 /* Register ssa_names and coalesces between the args and the result
954 of all PHI. */
955 for (i = 0; i < gimple_phi_num_args (phi); i++)
957 edge e = gimple_phi_arg_edge (phi, i);
958 arg = PHI_ARG_DEF (phi, i);
959 if (TREE_CODE (arg) != SSA_NAME)
960 continue;
962 register_ssa_partition (map, arg);
963 if (gimple_can_coalesce_p (arg, res)
964 || (e->flags & EDGE_ABNORMAL))
966 saw_copy = true;
967 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (arg));
968 if ((e->flags & EDGE_ABNORMAL) == 0)
970 int cost = coalesce_cost_edge (e);
971 if (cost == 1 && has_single_use (arg))
972 add_cost_one_coalesce (cl, ver, SSA_NAME_VERSION (arg));
973 else
974 add_coalesce (cl, ver, SSA_NAME_VERSION (arg), cost);
978 if (saw_copy)
979 bitmap_set_bit (used_in_copy, ver);
982 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
984 stmt = gsi_stmt (gsi);
986 if (is_gimple_debug (stmt))
987 continue;
989 /* Register USE and DEF operands in each statement. */
990 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
991 register_ssa_partition (map, var);
993 /* Check for copy coalesces. */
994 switch (gimple_code (stmt))
996 case GIMPLE_ASSIGN:
998 tree lhs = gimple_assign_lhs (stmt);
999 tree rhs1 = gimple_assign_rhs1 (stmt);
1000 if (gimple_assign_ssa_name_copy_p (stmt)
1001 && gimple_can_coalesce_p (lhs, rhs1))
1003 v1 = SSA_NAME_VERSION (lhs);
1004 v2 = SSA_NAME_VERSION (rhs1);
1005 cost = coalesce_cost_bb (bb);
1006 add_coalesce (cl, v1, v2, cost);
1007 bitmap_set_bit (used_in_copy, v1);
1008 bitmap_set_bit (used_in_copy, v2);
1011 break;
1013 case GIMPLE_ASM:
1015 gasm *asm_stmt = as_a <gasm *> (stmt);
1016 unsigned long noutputs, i;
1017 unsigned long ninputs;
1018 tree *outputs, link;
1019 noutputs = gimple_asm_noutputs (asm_stmt);
1020 ninputs = gimple_asm_ninputs (asm_stmt);
1021 outputs = (tree *) alloca (noutputs * sizeof (tree));
1022 for (i = 0; i < noutputs; ++i)
1024 link = gimple_asm_output_op (asm_stmt, i);
1025 outputs[i] = TREE_VALUE (link);
1028 for (i = 0; i < ninputs; ++i)
1030 const char *constraint;
1031 tree input;
1032 char *end;
1033 unsigned long match;
1035 link = gimple_asm_input_op (asm_stmt, i);
1036 constraint
1037 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1038 input = TREE_VALUE (link);
1040 if (TREE_CODE (input) != SSA_NAME)
1041 continue;
1043 match = strtoul (constraint, &end, 10);
1044 if (match >= noutputs || end == constraint)
1045 continue;
1047 if (TREE_CODE (outputs[match]) != SSA_NAME)
1048 continue;
1050 v1 = SSA_NAME_VERSION (outputs[match]);
1051 v2 = SSA_NAME_VERSION (input);
1053 if (gimple_can_coalesce_p (outputs[match], input))
1055 cost = coalesce_cost (REG_BR_PROB_BASE,
1056 optimize_bb_for_size_p (bb));
1057 add_coalesce (cl, v1, v2, cost);
1058 bitmap_set_bit (used_in_copy, v1);
1059 bitmap_set_bit (used_in_copy, v2);
1062 break;
1065 default:
1066 break;
1071 /* Now process result decls and live on entry variables for entry into
1072 the coalesce list. */
1073 first = NULL_TREE;
1074 for (i = 1; i < num_ssa_names; i++)
1076 var = ssa_name (i);
1077 if (var != NULL_TREE && !virtual_operand_p (var))
1079 /* Add coalesces between all the result decls. */
1080 if (SSA_NAME_VAR (var)
1081 && TREE_CODE (SSA_NAME_VAR (var)) == RESULT_DECL)
1083 if (first == NULL_TREE)
1084 first = var;
1085 else
1087 gcc_assert (gimple_can_coalesce_p (var, first));
1088 v1 = SSA_NAME_VERSION (first);
1089 v2 = SSA_NAME_VERSION (var);
1090 bitmap_set_bit (used_in_copy, v1);
1091 bitmap_set_bit (used_in_copy, v2);
1092 cost = coalesce_cost_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
1093 add_coalesce (cl, v1, v2, cost);
1096 /* Mark any default_def variables as being in the coalesce list
1097 since they will have to be coalesced with the base variable. If
1098 not marked as present, they won't be in the coalesce view. */
1099 if (SSA_NAME_IS_DEFAULT_DEF (var)
1100 && !has_zero_uses (var))
1101 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1105 return map;
1109 /* Attempt to coalesce ssa versions X and Y together using the partition
1110 mapping in MAP and checking conflicts in GRAPH. Output any debug info to
1111 DEBUG, if it is nun-NULL. */
1113 static inline bool
1114 attempt_coalesce (var_map map, ssa_conflicts_p graph, int x, int y,
1115 FILE *debug)
1117 int z;
1118 tree var1, var2;
1119 int p1, p2;
1121 p1 = var_to_partition (map, ssa_name (x));
1122 p2 = var_to_partition (map, ssa_name (y));
1124 if (debug)
1126 fprintf (debug, "(%d)", x);
1127 print_generic_expr (debug, partition_to_var (map, p1), TDF_SLIM);
1128 fprintf (debug, " & (%d)", y);
1129 print_generic_expr (debug, partition_to_var (map, p2), TDF_SLIM);
1132 if (p1 == p2)
1134 if (debug)
1135 fprintf (debug, ": Already Coalesced.\n");
1136 return true;
1139 if (debug)
1140 fprintf (debug, " [map: %d, %d] ", p1, p2);
1143 if (!ssa_conflicts_test_p (graph, p1, p2))
1145 var1 = partition_to_var (map, p1);
1146 var2 = partition_to_var (map, p2);
1147 z = var_union (map, var1, var2);
1148 if (z == NO_PARTITION)
1150 if (debug)
1151 fprintf (debug, ": Unable to perform partition union.\n");
1152 return false;
1155 /* z is the new combined partition. Remove the other partition from
1156 the list, and merge the conflicts. */
1157 if (z == p1)
1158 ssa_conflicts_merge (graph, p1, p2);
1159 else
1160 ssa_conflicts_merge (graph, p2, p1);
1162 if (debug)
1163 fprintf (debug, ": Success -> %d\n", z);
1164 return true;
1167 if (debug)
1168 fprintf (debug, ": Fail due to conflict\n");
1170 return false;
1174 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1175 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1177 static void
1178 coalesce_partitions (var_map map, ssa_conflicts_p graph, coalesce_list_p cl,
1179 FILE *debug)
1181 int x = 0, y = 0;
1182 tree var1, var2;
1183 int cost;
1184 basic_block bb;
1185 edge e;
1186 edge_iterator ei;
1188 /* First, coalesce all the copies across abnormal edges. These are not placed
1189 in the coalesce list because they do not need to be sorted, and simply
1190 consume extra memory/compilation time in large programs. */
1192 FOR_EACH_BB_FN (bb, cfun)
1194 FOR_EACH_EDGE (e, ei, bb->preds)
1195 if (e->flags & EDGE_ABNORMAL)
1197 gphi_iterator gsi;
1198 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1199 gsi_next (&gsi))
1201 gphi *phi = gsi.phi ();
1202 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1203 if (SSA_NAME_IS_DEFAULT_DEF (arg)
1204 && (!SSA_NAME_VAR (arg)
1205 || TREE_CODE (SSA_NAME_VAR (arg)) != PARM_DECL))
1206 continue;
1208 tree res = PHI_RESULT (phi);
1209 int v1 = SSA_NAME_VERSION (res);
1210 int v2 = SSA_NAME_VERSION (arg);
1212 if (debug)
1213 fprintf (debug, "Abnormal coalesce: ");
1215 if (!attempt_coalesce (map, graph, v1, v2, debug))
1216 fail_abnormal_edge_coalesce (v1, v2);
1221 /* Now process the items in the coalesce list. */
1223 while ((cost = pop_best_coalesce (cl, &x, &y)) != NO_BEST_COALESCE)
1225 var1 = ssa_name (x);
1226 var2 = ssa_name (y);
1228 /* Assert the coalesces have the same base variable. */
1229 gcc_assert (gimple_can_coalesce_p (var1, var2));
1231 if (debug)
1232 fprintf (debug, "Coalesce list: ");
1233 attempt_coalesce (map, graph, x, y, debug);
1238 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1240 struct ssa_name_var_hash : nofree_ptr_hash <tree_node>
1242 static inline hashval_t hash (const tree_node *);
1243 static inline int equal (const tree_node *, const tree_node *);
1246 inline hashval_t
1247 ssa_name_var_hash::hash (const_tree n)
1249 return DECL_UID (SSA_NAME_VAR (n));
1252 inline int
1253 ssa_name_var_hash::equal (const tree_node *n1, const tree_node *n2)
1255 return SSA_NAME_VAR (n1) == SSA_NAME_VAR (n2);
1259 /* Reduce the number of copies by coalescing variables in the function. Return
1260 a partition map with the resulting coalesces. */
1262 extern var_map
1263 coalesce_ssa_name (void)
1265 tree_live_info_p liveinfo;
1266 ssa_conflicts_p graph;
1267 coalesce_list_p cl;
1268 bitmap used_in_copies = BITMAP_ALLOC (NULL);
1269 var_map map;
1270 unsigned int i;
1272 cl = create_coalesce_list ();
1273 map = create_outofssa_var_map (cl, used_in_copies);
1275 /* If optimization is disabled, we need to coalesce all the names originating
1276 from the same SSA_NAME_VAR so debug info remains undisturbed. */
1277 if (!optimize)
1279 hash_table<ssa_name_var_hash> ssa_name_hash (10);
1281 for (i = 1; i < num_ssa_names; i++)
1283 tree a = ssa_name (i);
1285 if (a
1286 && SSA_NAME_VAR (a)
1287 && !DECL_IGNORED_P (SSA_NAME_VAR (a))
1288 && (!has_zero_uses (a) || !SSA_NAME_IS_DEFAULT_DEF (a)))
1290 tree *slot = ssa_name_hash.find_slot (a, INSERT);
1292 if (!*slot)
1293 *slot = a;
1294 else
1296 /* If the variable is a PARM_DECL or a RESULT_DECL, we
1297 _require_ that all the names originating from it be
1298 coalesced, because there must be a single partition
1299 containing all the names so that it can be assigned
1300 the canonical RTL location of the DECL safely.
1301 If in_lto_p, a function could have been compiled
1302 originally with optimizations and only the link
1303 performed at -O0, so we can't actually require it. */
1304 const int cost
1305 = (TREE_CODE (SSA_NAME_VAR (a)) == VAR_DECL || in_lto_p)
1306 ? MUST_COALESCE_COST - 1 : MUST_COALESCE_COST;
1307 add_coalesce (cl, SSA_NAME_VERSION (a),
1308 SSA_NAME_VERSION (*slot), cost);
1309 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (a));
1310 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (*slot));
1315 if (dump_file && (dump_flags & TDF_DETAILS))
1316 dump_var_map (dump_file, map);
1318 /* Don't calculate live ranges for variables not in the coalesce list. */
1319 partition_view_bitmap (map, used_in_copies, true);
1320 BITMAP_FREE (used_in_copies);
1322 if (num_var_partitions (map) < 1)
1324 delete_coalesce_list (cl);
1325 return map;
1328 if (dump_file && (dump_flags & TDF_DETAILS))
1329 dump_var_map (dump_file, map);
1331 liveinfo = calculate_live_ranges (map, false);
1333 if (dump_file && (dump_flags & TDF_DETAILS))
1334 dump_live_info (dump_file, liveinfo, LIVEDUMP_ENTRY);
1336 /* Build a conflict graph. */
1337 graph = build_ssa_conflict_graph (liveinfo);
1338 delete_tree_live_info (liveinfo);
1339 if (dump_file && (dump_flags & TDF_DETAILS))
1340 ssa_conflicts_dump (dump_file, graph);
1342 sort_coalesce_list (cl);
1344 if (dump_file && (dump_flags & TDF_DETAILS))
1346 fprintf (dump_file, "\nAfter sorting:\n");
1347 dump_coalesce_list (dump_file, cl);
1350 /* First, coalesce all live on entry variables to their base variable.
1351 This will ensure the first use is coming from the correct location. */
1353 if (dump_file && (dump_flags & TDF_DETAILS))
1354 dump_var_map (dump_file, map);
1356 /* Now coalesce everything in the list. */
1357 coalesce_partitions (map, graph, cl,
1358 ((dump_flags & TDF_DETAILS) ? dump_file
1359 : NULL));
1361 delete_coalesce_list (cl);
1362 ssa_conflicts_delete (graph);
1364 return map;