2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / tree-ssa-coalesce.c
blob0504aeb7f0452f5e822e4756ebbd2a4a4e9035db
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 "input.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "flags.h"
31 #include "tree-pretty-print.h"
32 #include "bitmap.h"
33 #include "dumpfile.h"
34 #include "predict.h"
35 #include "hard-reg-set.h"
36 #include "input.h"
37 #include "function.h"
38 #include "dominance.h"
39 #include "cfg.h"
40 #include "basic-block.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-expr.h"
44 #include "is-a.h"
45 #include "gimple.h"
46 #include "gimple-iterator.h"
47 #include "gimple-ssa.h"
48 #include "tree-phinodes.h"
49 #include "ssa-iterators.h"
50 #include "stringpool.h"
51 #include "tree-ssanames.h"
52 #include "tree-ssa-live.h"
53 #include "tree-ssa-coalesce.h"
54 #include "diagnostic-core.h"
57 /* This set of routines implements a coalesce_list. This is an object which
58 is used to track pairs of ssa_names which are desirable to coalesce
59 together to avoid copies. Costs are associated with each pair, and when
60 all desired information has been collected, the object can be used to
61 order the pairs for processing. */
63 /* This structure defines a pair entry. */
65 typedef struct coalesce_pair
67 int first_element;
68 int second_element;
69 int cost;
70 } * coalesce_pair_p;
71 typedef const struct coalesce_pair *const_coalesce_pair_p;
73 /* Coalesce pair hashtable helpers. */
75 struct coalesce_pair_hasher : typed_noop_remove <coalesce_pair>
77 typedef coalesce_pair *value_type;
78 typedef coalesce_pair *compare_type;
79 static inline hashval_t hash (const coalesce_pair *);
80 static inline bool equal (const coalesce_pair *, const coalesce_pair *);
83 /* Hash function for coalesce list. Calculate hash for PAIR. */
85 inline hashval_t
86 coalesce_pair_hasher::hash (const coalesce_pair *pair)
88 hashval_t a = (hashval_t)(pair->first_element);
89 hashval_t b = (hashval_t)(pair->second_element);
91 return b * (b - 1) / 2 + a;
94 /* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
95 returning TRUE if the two pairs are equivalent. */
97 inline bool
98 coalesce_pair_hasher::equal (const coalesce_pair *p1, const coalesce_pair *p2)
100 return (p1->first_element == p2->first_element
101 && p1->second_element == p2->second_element);
104 typedef hash_table<coalesce_pair_hasher> coalesce_table_type;
105 typedef coalesce_table_type::iterator coalesce_iterator_type;
108 typedef struct cost_one_pair_d
110 int first_element;
111 int second_element;
112 struct cost_one_pair_d *next;
113 } * cost_one_pair_p;
115 /* This structure maintains the list of coalesce pairs. */
117 typedef struct coalesce_list_d
119 coalesce_table_type *list; /* Hash table. */
120 coalesce_pair_p *sorted; /* List when sorted. */
121 int num_sorted; /* Number in the sorted list. */
122 cost_one_pair_p cost_one_list;/* Single use coalesces with cost 1. */
123 } *coalesce_list_p;
125 #define NO_BEST_COALESCE -1
126 #define MUST_COALESCE_COST INT_MAX
129 /* Return cost of execution of copy instruction with FREQUENCY. */
131 static inline int
132 coalesce_cost (int frequency, bool optimize_for_size)
134 /* Base costs on BB frequencies bounded by 1. */
135 int cost = frequency;
137 if (!cost)
138 cost = 1;
140 if (optimize_for_size)
141 cost = 1;
143 return cost;
147 /* Return the cost of executing a copy instruction in basic block BB. */
149 static inline int
150 coalesce_cost_bb (basic_block bb)
152 return coalesce_cost (bb->frequency, optimize_bb_for_size_p (bb));
156 /* Return the cost of executing a copy instruction on edge E. */
158 static inline int
159 coalesce_cost_edge (edge e)
161 int mult = 1;
163 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
164 if (EDGE_CRITICAL_P (e))
165 mult = 2;
166 if (e->flags & EDGE_ABNORMAL)
167 return MUST_COALESCE_COST;
168 if (e->flags & EDGE_EH)
170 edge e2;
171 edge_iterator ei;
172 FOR_EACH_EDGE (e2, ei, e->dest->preds)
173 if (e2 != e)
175 /* Putting code on EH edge that leads to BB
176 with multiple predecestors imply splitting of
177 edge too. */
178 if (mult < 2)
179 mult = 2;
180 /* If there are multiple EH predecestors, we
181 also copy EH regions and produce separate
182 landing pad. This is expensive. */
183 if (e2->flags & EDGE_EH)
185 mult = 5;
186 break;
191 return coalesce_cost (EDGE_FREQUENCY (e),
192 optimize_edge_for_size_p (e)) * mult;
196 /* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
197 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
198 NO_BEST_COALESCE is returned if there aren't any. */
200 static inline int
201 pop_cost_one_pair (coalesce_list_p cl, int *p1, int *p2)
203 cost_one_pair_p ptr;
205 ptr = cl->cost_one_list;
206 if (!ptr)
207 return NO_BEST_COALESCE;
209 *p1 = ptr->first_element;
210 *p2 = ptr->second_element;
211 cl->cost_one_list = ptr->next;
213 free (ptr);
215 return 1;
218 /* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
219 2 elements via P1 and P2. Their calculated cost is returned by the function.
220 NO_BEST_COALESCE is returned if the coalesce list is empty. */
222 static inline int
223 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
225 coalesce_pair_p node;
226 int ret;
228 if (cl->sorted == NULL)
229 return pop_cost_one_pair (cl, p1, p2);
231 if (cl->num_sorted == 0)
232 return pop_cost_one_pair (cl, p1, p2);
234 node = cl->sorted[--(cl->num_sorted)];
235 *p1 = node->first_element;
236 *p2 = node->second_element;
237 ret = node->cost;
238 free (node);
240 return ret;
244 /* Create a new empty coalesce list object and return it. */
246 static inline coalesce_list_p
247 create_coalesce_list (void)
249 coalesce_list_p list;
250 unsigned size = num_ssa_names * 3;
252 if (size < 40)
253 size = 40;
255 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
256 list->list = new coalesce_table_type (size);
257 list->sorted = NULL;
258 list->num_sorted = 0;
259 list->cost_one_list = NULL;
260 return list;
264 /* Delete coalesce list CL. */
266 static inline void
267 delete_coalesce_list (coalesce_list_p cl)
269 gcc_assert (cl->cost_one_list == NULL);
270 delete cl->list;
271 cl->list = NULL;
272 free (cl->sorted);
273 gcc_assert (cl->num_sorted == 0);
274 free (cl);
278 /* Find a matching coalesce pair object in CL for the pair P1 and P2. If
279 one isn't found, return NULL if CREATE is false, otherwise create a new
280 coalesce pair object and return it. */
282 static coalesce_pair_p
283 find_coalesce_pair (coalesce_list_p cl, int p1, int p2, bool create)
285 struct coalesce_pair p;
286 coalesce_pair **slot;
287 unsigned int hash;
289 /* Normalize so that p1 is the smaller value. */
290 if (p2 < p1)
292 p.first_element = p2;
293 p.second_element = p1;
295 else
297 p.first_element = p1;
298 p.second_element = p2;
301 hash = coalesce_pair_hasher::hash (&p);
302 slot = cl->list->find_slot_with_hash (&p, hash, create ? INSERT : NO_INSERT);
303 if (!slot)
304 return NULL;
306 if (!*slot)
308 struct coalesce_pair * pair = XNEW (struct coalesce_pair);
309 gcc_assert (cl->sorted == NULL);
310 pair->first_element = p.first_element;
311 pair->second_element = p.second_element;
312 pair->cost = 0;
313 *slot = pair;
316 return (struct coalesce_pair *) *slot;
319 static inline void
320 add_cost_one_coalesce (coalesce_list_p cl, int p1, int p2)
322 cost_one_pair_p pair;
324 pair = XNEW (struct cost_one_pair_d);
325 pair->first_element = p1;
326 pair->second_element = p2;
327 pair->next = cl->cost_one_list;
328 cl->cost_one_list = pair;
332 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
334 static inline void
335 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
337 coalesce_pair_p node;
339 gcc_assert (cl->sorted == NULL);
340 if (p1 == p2)
341 return;
343 node = find_coalesce_pair (cl, p1, p2, true);
345 /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way. */
346 if (node->cost < MUST_COALESCE_COST - 1)
348 if (value < MUST_COALESCE_COST - 1)
349 node->cost += value;
350 else
351 node->cost = value;
356 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
358 static int
359 compare_pairs (const void *p1, const void *p2)
361 const_coalesce_pair_p const *const pp1 = (const_coalesce_pair_p const *) p1;
362 const_coalesce_pair_p const *const pp2 = (const_coalesce_pair_p const *) p2;
363 int result;
365 result = (* pp1)->cost - (* pp2)->cost;
366 /* Since qsort does not guarantee stability we use the elements
367 as a secondary key. This provides us with independence from
368 the host's implementation of the sorting algorithm. */
369 if (result == 0)
371 result = (* pp2)->first_element - (* pp1)->first_element;
372 if (result == 0)
373 result = (* pp2)->second_element - (* pp1)->second_element;
376 return result;
380 /* Return the number of unique coalesce pairs in CL. */
382 static inline int
383 num_coalesce_pairs (coalesce_list_p cl)
385 return cl->list->elements ();
389 /* Iterate over CL using ITER, returning values in PAIR. */
391 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
392 FOR_EACH_HASH_TABLE_ELEMENT (*(CL)->list, (PAIR), coalesce_pair_p, (ITER))
395 /* Prepare CL for removal of preferred pairs. When finished they are sorted
396 in order from most important coalesce to least important. */
398 static void
399 sort_coalesce_list (coalesce_list_p cl)
401 unsigned x, num;
402 coalesce_pair_p p;
403 coalesce_iterator_type ppi;
405 gcc_assert (cl->sorted == NULL);
407 num = num_coalesce_pairs (cl);
408 cl->num_sorted = num;
409 if (num == 0)
410 return;
412 /* Allocate a vector for the pair pointers. */
413 cl->sorted = XNEWVEC (coalesce_pair_p, num);
415 /* Populate the vector with pointers to the pairs. */
416 x = 0;
417 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
418 cl->sorted[x++] = p;
419 gcc_assert (x == num);
421 /* Already sorted. */
422 if (num == 1)
423 return;
425 /* If there are only 2, just pick swap them if the order isn't correct. */
426 if (num == 2)
428 if (cl->sorted[0]->cost > cl->sorted[1]->cost)
429 std::swap (cl->sorted[0], cl->sorted[1]);
430 return;
433 /* Only call qsort if there are more than 2 items.
434 ??? Maybe std::sort will do better, provided that compare_pairs
435 can be inlined. */
436 if (num > 2)
437 qsort (cl->sorted, num, sizeof (coalesce_pair_p), compare_pairs);
441 /* Send debug info for coalesce list CL to file F. */
443 static void
444 dump_coalesce_list (FILE *f, coalesce_list_p cl)
446 coalesce_pair_p node;
447 coalesce_iterator_type ppi;
449 int x;
450 tree var;
452 if (cl->sorted == NULL)
454 fprintf (f, "Coalesce List:\n");
455 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
457 tree var1 = ssa_name (node->first_element);
458 tree var2 = ssa_name (node->second_element);
459 print_generic_expr (f, var1, TDF_SLIM);
460 fprintf (f, " <-> ");
461 print_generic_expr (f, var2, TDF_SLIM);
462 fprintf (f, " (%1d), ", node->cost);
463 fprintf (f, "\n");
466 else
468 fprintf (f, "Sorted Coalesce list:\n");
469 for (x = cl->num_sorted - 1 ; x >=0; x--)
471 node = cl->sorted[x];
472 fprintf (f, "(%d) ", node->cost);
473 var = ssa_name (node->first_element);
474 print_generic_expr (f, var, TDF_SLIM);
475 fprintf (f, " <-> ");
476 var = ssa_name (node->second_element);
477 print_generic_expr (f, var, TDF_SLIM);
478 fprintf (f, "\n");
484 /* This represents a conflict graph. Implemented as an array of bitmaps.
485 A full matrix is used for conflicts rather than just upper triangular form.
486 this make sit much simpler and faster to perform conflict merges. */
488 typedef struct ssa_conflicts_d
490 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
491 vec<bitmap> conflicts;
492 } * ssa_conflicts_p;
494 /* Return an empty new conflict graph for SIZE elements. */
496 static inline ssa_conflicts_p
497 ssa_conflicts_new (unsigned size)
499 ssa_conflicts_p ptr;
501 ptr = XNEW (struct ssa_conflicts_d);
502 bitmap_obstack_initialize (&ptr->obstack);
503 ptr->conflicts.create (size);
504 ptr->conflicts.safe_grow_cleared (size);
505 return ptr;
509 /* Free storage for conflict graph PTR. */
511 static inline void
512 ssa_conflicts_delete (ssa_conflicts_p ptr)
514 bitmap_obstack_release (&ptr->obstack);
515 ptr->conflicts.release ();
516 free (ptr);
520 /* Test if elements X and Y conflict in graph PTR. */
522 static inline bool
523 ssa_conflicts_test_p (ssa_conflicts_p ptr, unsigned x, unsigned y)
525 bitmap bx = ptr->conflicts[x];
526 bitmap by = ptr->conflicts[y];
528 gcc_checking_assert (x != y);
530 if (bx)
531 /* Avoid the lookup if Y has no conflicts. */
532 return by ? bitmap_bit_p (bx, y) : false;
533 else
534 return false;
538 /* Add a conflict with Y to the bitmap for X in graph PTR. */
540 static inline void
541 ssa_conflicts_add_one (ssa_conflicts_p ptr, unsigned x, unsigned y)
543 bitmap bx = ptr->conflicts[x];
544 /* If there are no conflicts yet, allocate the bitmap and set bit. */
545 if (! bx)
546 bx = ptr->conflicts[x] = BITMAP_ALLOC (&ptr->obstack);
547 bitmap_set_bit (bx, y);
551 /* Add conflicts between X and Y in graph PTR. */
553 static inline void
554 ssa_conflicts_add (ssa_conflicts_p ptr, unsigned x, unsigned y)
556 gcc_checking_assert (x != y);
557 ssa_conflicts_add_one (ptr, x, y);
558 ssa_conflicts_add_one (ptr, y, x);
562 /* Merge all Y's conflict into X in graph PTR. */
564 static inline void
565 ssa_conflicts_merge (ssa_conflicts_p ptr, unsigned x, unsigned y)
567 unsigned z;
568 bitmap_iterator bi;
569 bitmap bx = ptr->conflicts[x];
570 bitmap by = ptr->conflicts[y];
572 gcc_checking_assert (x != y);
573 if (! by)
574 return;
576 /* Add a conflict between X and every one Y has. If the bitmap doesn't
577 exist, then it has already been coalesced, and we don't need to add a
578 conflict. */
579 EXECUTE_IF_SET_IN_BITMAP (by, 0, z, bi)
581 bitmap bz = ptr->conflicts[z];
582 if (bz)
583 bitmap_set_bit (bz, x);
586 if (bx)
588 /* If X has conflicts, add Y's to X. */
589 bitmap_ior_into (bx, by);
590 BITMAP_FREE (by);
591 ptr->conflicts[y] = NULL;
593 else
595 /* If X has no conflicts, simply use Y's. */
596 ptr->conflicts[x] = by;
597 ptr->conflicts[y] = NULL;
602 /* Dump a conflicts graph. */
604 static void
605 ssa_conflicts_dump (FILE *file, ssa_conflicts_p ptr)
607 unsigned x;
608 bitmap b;
610 fprintf (file, "\nConflict graph:\n");
612 FOR_EACH_VEC_ELT (ptr->conflicts, x, b)
613 if (b)
615 fprintf (file, "%d: ", x);
616 dump_bitmap (file, b);
621 /* This structure is used to efficiently record the current status of live
622 SSA_NAMES when building a conflict graph.
623 LIVE_BASE_VAR has a bit set for each base variable which has at least one
624 ssa version live.
625 LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
626 index, and is used to track what partitions of each base variable are
627 live. This makes it easy to add conflicts between just live partitions
628 with the same base variable.
629 The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
630 marked as being live. This delays clearing of these bitmaps until
631 they are actually needed again. */
633 typedef struct live_track_d
635 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
636 bitmap live_base_var; /* Indicates if a basevar is live. */
637 bitmap *live_base_partitions; /* Live partitions for each basevar. */
638 var_map map; /* Var_map being used for partition mapping. */
639 } * live_track_p;
642 /* This routine will create a new live track structure based on the partitions
643 in MAP. */
645 static live_track_p
646 new_live_track (var_map map)
648 live_track_p ptr;
649 int lim, x;
651 /* Make sure there is a partition view in place. */
652 gcc_assert (map->partition_to_base_index != NULL);
654 ptr = (live_track_p) xmalloc (sizeof (struct live_track_d));
655 ptr->map = map;
656 lim = num_basevars (map);
657 bitmap_obstack_initialize (&ptr->obstack);
658 ptr->live_base_partitions = (bitmap *) xmalloc (sizeof (bitmap *) * lim);
659 ptr->live_base_var = BITMAP_ALLOC (&ptr->obstack);
660 for (x = 0; x < lim; x++)
661 ptr->live_base_partitions[x] = BITMAP_ALLOC (&ptr->obstack);
662 return ptr;
666 /* This routine will free the memory associated with PTR. */
668 static void
669 delete_live_track (live_track_p ptr)
671 bitmap_obstack_release (&ptr->obstack);
672 free (ptr->live_base_partitions);
673 free (ptr);
677 /* This function will remove PARTITION from the live list in PTR. */
679 static inline void
680 live_track_remove_partition (live_track_p ptr, int partition)
682 int root;
684 root = basevar_index (ptr->map, partition);
685 bitmap_clear_bit (ptr->live_base_partitions[root], partition);
686 /* If the element list is empty, make the base variable not live either. */
687 if (bitmap_empty_p (ptr->live_base_partitions[root]))
688 bitmap_clear_bit (ptr->live_base_var, root);
692 /* This function will adds PARTITION to the live list in PTR. */
694 static inline void
695 live_track_add_partition (live_track_p ptr, int partition)
697 int root;
699 root = basevar_index (ptr->map, partition);
700 /* If this base var wasn't live before, it is now. Clear the element list
701 since it was delayed until needed. */
702 if (bitmap_set_bit (ptr->live_base_var, root))
703 bitmap_clear (ptr->live_base_partitions[root]);
704 bitmap_set_bit (ptr->live_base_partitions[root], partition);
709 /* Clear the live bit for VAR in PTR. */
711 static inline void
712 live_track_clear_var (live_track_p ptr, tree var)
714 int p;
716 p = var_to_partition (ptr->map, var);
717 if (p != NO_PARTITION)
718 live_track_remove_partition (ptr, p);
722 /* Return TRUE if VAR is live in PTR. */
724 static inline bool
725 live_track_live_p (live_track_p ptr, tree var)
727 int p, root;
729 p = var_to_partition (ptr->map, var);
730 if (p != NO_PARTITION)
732 root = basevar_index (ptr->map, p);
733 if (bitmap_bit_p (ptr->live_base_var, root))
734 return bitmap_bit_p (ptr->live_base_partitions[root], p);
736 return false;
740 /* This routine will add USE to PTR. USE will be marked as live in both the
741 ssa live map and the live bitmap for the root of USE. */
743 static inline void
744 live_track_process_use (live_track_p ptr, tree use)
746 int p;
748 p = var_to_partition (ptr->map, use);
749 if (p == NO_PARTITION)
750 return;
752 /* Mark as live in the appropriate live list. */
753 live_track_add_partition (ptr, p);
757 /* This routine will process a DEF in PTR. DEF will be removed from the live
758 lists, and if there are any other live partitions with the same base
759 variable, conflicts will be added to GRAPH. */
761 static inline void
762 live_track_process_def (live_track_p ptr, tree def, ssa_conflicts_p graph)
764 int p, root;
765 bitmap b;
766 unsigned x;
767 bitmap_iterator bi;
769 p = var_to_partition (ptr->map, def);
770 if (p == NO_PARTITION)
771 return;
773 /* Clear the liveness bit. */
774 live_track_remove_partition (ptr, p);
776 /* If the bitmap isn't empty now, conflicts need to be added. */
777 root = basevar_index (ptr->map, p);
778 if (bitmap_bit_p (ptr->live_base_var, root))
780 b = ptr->live_base_partitions[root];
781 EXECUTE_IF_SET_IN_BITMAP (b, 0, x, bi)
782 ssa_conflicts_add (graph, p, x);
787 /* Initialize PTR with the partitions set in INIT. */
789 static inline void
790 live_track_init (live_track_p ptr, bitmap init)
792 unsigned p;
793 bitmap_iterator bi;
795 /* Mark all live on exit partitions. */
796 EXECUTE_IF_SET_IN_BITMAP (init, 0, p, bi)
797 live_track_add_partition (ptr, p);
801 /* This routine will clear all live partitions in PTR. */
803 static inline void
804 live_track_clear_base_vars (live_track_p ptr)
806 /* Simply clear the live base list. Anything marked as live in the element
807 lists will be cleared later if/when the base variable ever comes alive
808 again. */
809 bitmap_clear (ptr->live_base_var);
813 /* Build a conflict graph based on LIVEINFO. Any partitions which are in the
814 partition view of the var_map liveinfo is based on get entries in the
815 conflict graph. Only conflicts between ssa_name partitions with the same
816 base variable are added. */
818 static ssa_conflicts_p
819 build_ssa_conflict_graph (tree_live_info_p liveinfo)
821 ssa_conflicts_p graph;
822 var_map map;
823 basic_block bb;
824 ssa_op_iter iter;
825 live_track_p live;
827 map = live_var_map (liveinfo);
828 graph = ssa_conflicts_new (num_var_partitions (map));
830 live = new_live_track (map);
832 FOR_EACH_BB_FN (bb, cfun)
834 /* Start with live on exit temporaries. */
835 live_track_init (live, live_on_exit (liveinfo, bb));
837 for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);
838 gsi_prev (&gsi))
840 tree var;
841 gimple stmt = gsi_stmt (gsi);
843 /* A copy between 2 partitions does not introduce an interference
844 by itself. If they did, you would never be able to coalesce
845 two things which are copied. If the two variables really do
846 conflict, they will conflict elsewhere in the program.
848 This is handled by simply removing the SRC of the copy from the
849 live list, and processing the stmt normally. */
850 if (is_gimple_assign (stmt))
852 tree lhs = gimple_assign_lhs (stmt);
853 tree rhs1 = gimple_assign_rhs1 (stmt);
854 if (gimple_assign_copy_p (stmt)
855 && TREE_CODE (lhs) == SSA_NAME
856 && TREE_CODE (rhs1) == SSA_NAME)
857 live_track_clear_var (live, rhs1);
859 else if (is_gimple_debug (stmt))
860 continue;
862 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
863 live_track_process_def (live, var, graph);
865 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
866 live_track_process_use (live, var);
869 /* If result of a PHI is unused, looping over the statements will not
870 record any conflicts since the def was never live. Since the PHI node
871 is going to be translated out of SSA form, it will insert a copy.
872 There must be a conflict recorded between the result of the PHI and
873 any variables that are live. Otherwise the out-of-ssa translation
874 may create incorrect code. */
875 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
876 gsi_next (&gsi))
878 gphi *phi = gsi.phi ();
879 tree result = PHI_RESULT (phi);
880 if (live_track_live_p (live, result))
881 live_track_process_def (live, result, graph);
884 live_track_clear_base_vars (live);
887 delete_live_track (live);
888 return graph;
892 /* Shortcut routine to print messages to file F of the form:
893 "STR1 EXPR1 STR2 EXPR2 STR3." */
895 static inline void
896 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
897 tree expr2, const char *str3)
899 fprintf (f, "%s", str1);
900 print_generic_expr (f, expr1, TDF_SLIM);
901 fprintf (f, "%s", str2);
902 print_generic_expr (f, expr2, TDF_SLIM);
903 fprintf (f, "%s", str3);
907 /* Print a failure to coalesce a MUST_COALESCE pair X and Y. */
909 static inline void
910 fail_abnormal_edge_coalesce (int x, int y)
912 fprintf (stderr, "\nUnable to coalesce ssa_names %d and %d",x, y);
913 fprintf (stderr, " which are marked as MUST COALESCE.\n");
914 print_generic_expr (stderr, ssa_name (x), TDF_SLIM);
915 fprintf (stderr, " and ");
916 print_generic_stmt (stderr, ssa_name (y), TDF_SLIM);
918 internal_error ("SSA corruption");
922 /* This function creates a var_map for the current function as well as creating
923 a coalesce list for use later in the out of ssa process. */
925 static var_map
926 create_outofssa_var_map (coalesce_list_p cl, bitmap used_in_copy)
928 gimple_stmt_iterator gsi;
929 basic_block bb;
930 tree var;
931 gimple stmt;
932 tree first;
933 var_map map;
934 ssa_op_iter iter;
935 int v1, v2, cost;
936 unsigned i;
938 map = init_var_map (num_ssa_names);
940 FOR_EACH_BB_FN (bb, cfun)
942 tree arg;
944 for (gphi_iterator gpi = gsi_start_phis (bb);
945 !gsi_end_p (gpi);
946 gsi_next (&gpi))
948 gphi *phi = gpi.phi ();
949 size_t i;
950 int ver;
951 tree res;
952 bool saw_copy = false;
954 res = gimple_phi_result (phi);
955 ver = SSA_NAME_VERSION (res);
956 register_ssa_partition (map, res);
958 /* Register ssa_names and coalesces between the args and the result
959 of all PHI. */
960 for (i = 0; i < gimple_phi_num_args (phi); i++)
962 edge e = gimple_phi_arg_edge (phi, i);
963 arg = PHI_ARG_DEF (phi, i);
964 if (TREE_CODE (arg) != SSA_NAME)
965 continue;
967 register_ssa_partition (map, arg);
968 if (gimple_can_coalesce_p (arg, res)
969 || (e->flags & EDGE_ABNORMAL))
971 saw_copy = true;
972 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (arg));
973 if ((e->flags & EDGE_ABNORMAL) == 0)
975 int cost = coalesce_cost_edge (e);
976 if (cost == 1 && has_single_use (arg))
977 add_cost_one_coalesce (cl, ver, SSA_NAME_VERSION (arg));
978 else
979 add_coalesce (cl, ver, SSA_NAME_VERSION (arg), cost);
983 if (saw_copy)
984 bitmap_set_bit (used_in_copy, ver);
987 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
989 stmt = gsi_stmt (gsi);
991 if (is_gimple_debug (stmt))
992 continue;
994 /* Register USE and DEF operands in each statement. */
995 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
996 register_ssa_partition (map, var);
998 /* Check for copy coalesces. */
999 switch (gimple_code (stmt))
1001 case GIMPLE_ASSIGN:
1003 tree lhs = gimple_assign_lhs (stmt);
1004 tree rhs1 = gimple_assign_rhs1 (stmt);
1005 if (gimple_assign_ssa_name_copy_p (stmt)
1006 && gimple_can_coalesce_p (lhs, rhs1))
1008 v1 = SSA_NAME_VERSION (lhs);
1009 v2 = SSA_NAME_VERSION (rhs1);
1010 cost = coalesce_cost_bb (bb);
1011 add_coalesce (cl, v1, v2, cost);
1012 bitmap_set_bit (used_in_copy, v1);
1013 bitmap_set_bit (used_in_copy, v2);
1016 break;
1018 case GIMPLE_ASM:
1020 gasm *asm_stmt = as_a <gasm *> (stmt);
1021 unsigned long noutputs, i;
1022 unsigned long ninputs;
1023 tree *outputs, link;
1024 noutputs = gimple_asm_noutputs (asm_stmt);
1025 ninputs = gimple_asm_ninputs (asm_stmt);
1026 outputs = (tree *) alloca (noutputs * sizeof (tree));
1027 for (i = 0; i < noutputs; ++i)
1029 link = gimple_asm_output_op (asm_stmt, i);
1030 outputs[i] = TREE_VALUE (link);
1033 for (i = 0; i < ninputs; ++i)
1035 const char *constraint;
1036 tree input;
1037 char *end;
1038 unsigned long match;
1040 link = gimple_asm_input_op (asm_stmt, i);
1041 constraint
1042 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1043 input = TREE_VALUE (link);
1045 if (TREE_CODE (input) != SSA_NAME)
1046 continue;
1048 match = strtoul (constraint, &end, 10);
1049 if (match >= noutputs || end == constraint)
1050 continue;
1052 if (TREE_CODE (outputs[match]) != SSA_NAME)
1053 continue;
1055 v1 = SSA_NAME_VERSION (outputs[match]);
1056 v2 = SSA_NAME_VERSION (input);
1058 if (gimple_can_coalesce_p (outputs[match], input))
1060 cost = coalesce_cost (REG_BR_PROB_BASE,
1061 optimize_bb_for_size_p (bb));
1062 add_coalesce (cl, v1, v2, cost);
1063 bitmap_set_bit (used_in_copy, v1);
1064 bitmap_set_bit (used_in_copy, v2);
1067 break;
1070 default:
1071 break;
1076 /* Now process result decls and live on entry variables for entry into
1077 the coalesce list. */
1078 first = NULL_TREE;
1079 for (i = 1; i < num_ssa_names; i++)
1081 var = ssa_name (i);
1082 if (var != NULL_TREE && !virtual_operand_p (var))
1084 /* Add coalesces between all the result decls. */
1085 if (SSA_NAME_VAR (var)
1086 && TREE_CODE (SSA_NAME_VAR (var)) == RESULT_DECL)
1088 if (first == NULL_TREE)
1089 first = var;
1090 else
1092 gcc_assert (gimple_can_coalesce_p (var, first));
1093 v1 = SSA_NAME_VERSION (first);
1094 v2 = SSA_NAME_VERSION (var);
1095 bitmap_set_bit (used_in_copy, v1);
1096 bitmap_set_bit (used_in_copy, v2);
1097 cost = coalesce_cost_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
1098 add_coalesce (cl, v1, v2, cost);
1101 /* Mark any default_def variables as being in the coalesce list
1102 since they will have to be coalesced with the base variable. If
1103 not marked as present, they won't be in the coalesce view. */
1104 if (SSA_NAME_IS_DEFAULT_DEF (var)
1105 && !has_zero_uses (var))
1106 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1110 return map;
1114 /* Attempt to coalesce ssa versions X and Y together using the partition
1115 mapping in MAP and checking conflicts in GRAPH. Output any debug info to
1116 DEBUG, if it is nun-NULL. */
1118 static inline bool
1119 attempt_coalesce (var_map map, ssa_conflicts_p graph, int x, int y,
1120 FILE *debug)
1122 int z;
1123 tree var1, var2;
1124 int p1, p2;
1126 p1 = var_to_partition (map, ssa_name (x));
1127 p2 = var_to_partition (map, ssa_name (y));
1129 if (debug)
1131 fprintf (debug, "(%d)", x);
1132 print_generic_expr (debug, partition_to_var (map, p1), TDF_SLIM);
1133 fprintf (debug, " & (%d)", y);
1134 print_generic_expr (debug, partition_to_var (map, p2), TDF_SLIM);
1137 if (p1 == p2)
1139 if (debug)
1140 fprintf (debug, ": Already Coalesced.\n");
1141 return true;
1144 if (debug)
1145 fprintf (debug, " [map: %d, %d] ", p1, p2);
1148 if (!ssa_conflicts_test_p (graph, p1, p2))
1150 var1 = partition_to_var (map, p1);
1151 var2 = partition_to_var (map, p2);
1152 z = var_union (map, var1, var2);
1153 if (z == NO_PARTITION)
1155 if (debug)
1156 fprintf (debug, ": Unable to perform partition union.\n");
1157 return false;
1160 /* z is the new combined partition. Remove the other partition from
1161 the list, and merge the conflicts. */
1162 if (z == p1)
1163 ssa_conflicts_merge (graph, p1, p2);
1164 else
1165 ssa_conflicts_merge (graph, p2, p1);
1167 if (debug)
1168 fprintf (debug, ": Success -> %d\n", z);
1169 return true;
1172 if (debug)
1173 fprintf (debug, ": Fail due to conflict\n");
1175 return false;
1179 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1180 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1182 static void
1183 coalesce_partitions (var_map map, ssa_conflicts_p graph, coalesce_list_p cl,
1184 FILE *debug)
1186 int x = 0, y = 0;
1187 tree var1, var2;
1188 int cost;
1189 basic_block bb;
1190 edge e;
1191 edge_iterator ei;
1193 /* First, coalesce all the copies across abnormal edges. These are not placed
1194 in the coalesce list because they do not need to be sorted, and simply
1195 consume extra memory/compilation time in large programs. */
1197 FOR_EACH_BB_FN (bb, cfun)
1199 FOR_EACH_EDGE (e, ei, bb->preds)
1200 if (e->flags & EDGE_ABNORMAL)
1202 gphi_iterator gsi;
1203 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1204 gsi_next (&gsi))
1206 gphi *phi = gsi.phi ();
1207 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1208 if (SSA_NAME_IS_DEFAULT_DEF (arg)
1209 && (!SSA_NAME_VAR (arg)
1210 || TREE_CODE (SSA_NAME_VAR (arg)) != PARM_DECL))
1211 continue;
1213 tree res = PHI_RESULT (phi);
1214 int v1 = SSA_NAME_VERSION (res);
1215 int v2 = SSA_NAME_VERSION (arg);
1217 if (debug)
1218 fprintf (debug, "Abnormal coalesce: ");
1220 if (!attempt_coalesce (map, graph, v1, v2, debug))
1221 fail_abnormal_edge_coalesce (v1, v2);
1226 /* Now process the items in the coalesce list. */
1228 while ((cost = pop_best_coalesce (cl, &x, &y)) != NO_BEST_COALESCE)
1230 var1 = ssa_name (x);
1231 var2 = ssa_name (y);
1233 /* Assert the coalesces have the same base variable. */
1234 gcc_assert (gimple_can_coalesce_p (var1, var2));
1236 if (debug)
1237 fprintf (debug, "Coalesce list: ");
1238 attempt_coalesce (map, graph, x, y, debug);
1243 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1245 struct ssa_name_var_hash : typed_noop_remove <tree_node>
1247 typedef union tree_node *value_type;
1248 typedef union tree_node *compare_type;
1249 static inline hashval_t hash (const tree_node *);
1250 static inline int equal (const tree_node *, const tree_node *);
1253 inline hashval_t
1254 ssa_name_var_hash::hash (const_tree n)
1256 return DECL_UID (SSA_NAME_VAR (n));
1259 inline int
1260 ssa_name_var_hash::equal (const tree_node *n1, const tree_node *n2)
1262 return SSA_NAME_VAR (n1) == SSA_NAME_VAR (n2);
1266 /* Reduce the number of copies by coalescing variables in the function. Return
1267 a partition map with the resulting coalesces. */
1269 extern var_map
1270 coalesce_ssa_name (void)
1272 tree_live_info_p liveinfo;
1273 ssa_conflicts_p graph;
1274 coalesce_list_p cl;
1275 bitmap used_in_copies = BITMAP_ALLOC (NULL);
1276 var_map map;
1277 unsigned int i;
1279 cl = create_coalesce_list ();
1280 map = create_outofssa_var_map (cl, used_in_copies);
1282 /* If optimization is disabled, we need to coalesce all the names originating
1283 from the same SSA_NAME_VAR so debug info remains undisturbed. */
1284 if (!optimize)
1286 hash_table<ssa_name_var_hash> ssa_name_hash (10);
1288 for (i = 1; i < num_ssa_names; i++)
1290 tree a = ssa_name (i);
1292 if (a
1293 && SSA_NAME_VAR (a)
1294 && !DECL_IGNORED_P (SSA_NAME_VAR (a))
1295 && (!has_zero_uses (a) || !SSA_NAME_IS_DEFAULT_DEF (a)))
1297 tree *slot = ssa_name_hash.find_slot (a, INSERT);
1299 if (!*slot)
1300 *slot = a;
1301 else
1303 /* If the variable is a PARM_DECL or a RESULT_DECL, we
1304 _require_ that all the names originating from it be
1305 coalesced, because there must be a single partition
1306 containing all the names so that it can be assigned
1307 the canonical RTL location of the DECL safely.
1308 If in_lto_p, a function could have been compiled
1309 originally with optimizations and only the link
1310 performed at -O0, so we can't actually require it. */
1311 const int cost
1312 = (TREE_CODE (SSA_NAME_VAR (a)) == VAR_DECL || in_lto_p)
1313 ? MUST_COALESCE_COST - 1 : MUST_COALESCE_COST;
1314 add_coalesce (cl, SSA_NAME_VERSION (a),
1315 SSA_NAME_VERSION (*slot), cost);
1316 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (a));
1317 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (*slot));
1322 if (dump_file && (dump_flags & TDF_DETAILS))
1323 dump_var_map (dump_file, map);
1325 /* Don't calculate live ranges for variables not in the coalesce list. */
1326 partition_view_bitmap (map, used_in_copies, true);
1327 BITMAP_FREE (used_in_copies);
1329 if (num_var_partitions (map) < 1)
1331 delete_coalesce_list (cl);
1332 return map;
1335 if (dump_file && (dump_flags & TDF_DETAILS))
1336 dump_var_map (dump_file, map);
1338 liveinfo = calculate_live_ranges (map, false);
1340 if (dump_file && (dump_flags & TDF_DETAILS))
1341 dump_live_info (dump_file, liveinfo, LIVEDUMP_ENTRY);
1343 /* Build a conflict graph. */
1344 graph = build_ssa_conflict_graph (liveinfo);
1345 delete_tree_live_info (liveinfo);
1346 if (dump_file && (dump_flags & TDF_DETAILS))
1347 ssa_conflicts_dump (dump_file, graph);
1349 sort_coalesce_list (cl);
1351 if (dump_file && (dump_flags & TDF_DETAILS))
1353 fprintf (dump_file, "\nAfter sorting:\n");
1354 dump_coalesce_list (dump_file, cl);
1357 /* First, coalesce all live on entry variables to their base variable.
1358 This will ensure the first use is coming from the correct location. */
1360 if (dump_file && (dump_flags & TDF_DETAILS))
1361 dump_var_map (dump_file, map);
1363 /* Now coalesce everything in the list. */
1364 coalesce_partitions (map, graph, cl,
1365 ((dump_flags & TDF_DETAILS) ? dump_file
1366 : NULL));
1368 delete_coalesce_list (cl);
1369 ssa_conflicts_delete (graph);
1371 return map;