2015-05-22 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / tree-ssa-coalesce.c
blobb05a86086efbe25276ef277ba890cb924fcb526c
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 "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "flags.h"
37 #include "tree-pretty-print.h"
38 #include "bitmap.h"
39 #include "dumpfile.h"
40 #include "hash-table.h"
41 #include "predict.h"
42 #include "hard-reg-set.h"
43 #include "input.h"
44 #include "function.h"
45 #include "dominance.h"
46 #include "cfg.h"
47 #include "basic-block.h"
48 #include "tree-ssa-alias.h"
49 #include "internal-fn.h"
50 #include "gimple-expr.h"
51 #include "is-a.h"
52 #include "gimple.h"
53 #include "gimple-iterator.h"
54 #include "gimple-ssa.h"
55 #include "tree-phinodes.h"
56 #include "ssa-iterators.h"
57 #include "stringpool.h"
58 #include "tree-ssanames.h"
59 #include "tree-ssa-live.h"
60 #include "tree-ssa-coalesce.h"
61 #include "diagnostic-core.h"
64 /* This set of routines implements a coalesce_list. This is an object which
65 is used to track pairs of ssa_names which are desirable to coalesce
66 together to avoid copies. Costs are associated with each pair, and when
67 all desired information has been collected, the object can be used to
68 order the pairs for processing. */
70 /* This structure defines a pair entry. */
72 typedef struct coalesce_pair
74 int first_element;
75 int second_element;
76 int cost;
77 } * coalesce_pair_p;
78 typedef const struct coalesce_pair *const_coalesce_pair_p;
80 /* Coalesce pair hashtable helpers. */
82 struct coalesce_pair_hasher : typed_noop_remove <coalesce_pair>
84 typedef coalesce_pair *value_type;
85 typedef coalesce_pair *compare_type;
86 static inline hashval_t hash (const coalesce_pair *);
87 static inline bool equal (const coalesce_pair *, const coalesce_pair *);
90 /* Hash function for coalesce list. Calculate hash for PAIR. */
92 inline hashval_t
93 coalesce_pair_hasher::hash (const coalesce_pair *pair)
95 hashval_t a = (hashval_t)(pair->first_element);
96 hashval_t b = (hashval_t)(pair->second_element);
98 return b * (b - 1) / 2 + a;
101 /* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
102 returning TRUE if the two pairs are equivalent. */
104 inline bool
105 coalesce_pair_hasher::equal (const coalesce_pair *p1, const coalesce_pair *p2)
107 return (p1->first_element == p2->first_element
108 && p1->second_element == p2->second_element);
111 typedef hash_table<coalesce_pair_hasher> coalesce_table_type;
112 typedef coalesce_table_type::iterator coalesce_iterator_type;
115 typedef struct cost_one_pair_d
117 int first_element;
118 int second_element;
119 struct cost_one_pair_d *next;
120 } * cost_one_pair_p;
122 /* This structure maintains the list of coalesce pairs. */
124 typedef struct coalesce_list_d
126 coalesce_table_type *list; /* Hash table. */
127 coalesce_pair_p *sorted; /* List when sorted. */
128 int num_sorted; /* Number in the sorted list. */
129 cost_one_pair_p cost_one_list;/* Single use coalesces with cost 1. */
130 } *coalesce_list_p;
132 #define NO_BEST_COALESCE -1
133 #define MUST_COALESCE_COST INT_MAX
136 /* Return cost of execution of copy instruction with FREQUENCY. */
138 static inline int
139 coalesce_cost (int frequency, bool optimize_for_size)
141 /* Base costs on BB frequencies bounded by 1. */
142 int cost = frequency;
144 if (!cost)
145 cost = 1;
147 if (optimize_for_size)
148 cost = 1;
150 return cost;
154 /* Return the cost of executing a copy instruction in basic block BB. */
156 static inline int
157 coalesce_cost_bb (basic_block bb)
159 return coalesce_cost (bb->frequency, optimize_bb_for_size_p (bb));
163 /* Return the cost of executing a copy instruction on edge E. */
165 static inline int
166 coalesce_cost_edge (edge e)
168 int mult = 1;
170 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
171 if (EDGE_CRITICAL_P (e))
172 mult = 2;
173 if (e->flags & EDGE_ABNORMAL)
174 return MUST_COALESCE_COST;
175 if (e->flags & EDGE_EH)
177 edge e2;
178 edge_iterator ei;
179 FOR_EACH_EDGE (e2, ei, e->dest->preds)
180 if (e2 != e)
182 /* Putting code on EH edge that leads to BB
183 with multiple predecestors imply splitting of
184 edge too. */
185 if (mult < 2)
186 mult = 2;
187 /* If there are multiple EH predecestors, we
188 also copy EH regions and produce separate
189 landing pad. This is expensive. */
190 if (e2->flags & EDGE_EH)
192 mult = 5;
193 break;
198 return coalesce_cost (EDGE_FREQUENCY (e),
199 optimize_edge_for_size_p (e)) * mult;
203 /* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
204 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
205 NO_BEST_COALESCE is returned if there aren't any. */
207 static inline int
208 pop_cost_one_pair (coalesce_list_p cl, int *p1, int *p2)
210 cost_one_pair_p ptr;
212 ptr = cl->cost_one_list;
213 if (!ptr)
214 return NO_BEST_COALESCE;
216 *p1 = ptr->first_element;
217 *p2 = ptr->second_element;
218 cl->cost_one_list = ptr->next;
220 free (ptr);
222 return 1;
225 /* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
226 2 elements via P1 and P2. Their calculated cost is returned by the function.
227 NO_BEST_COALESCE is returned if the coalesce list is empty. */
229 static inline int
230 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
232 coalesce_pair_p node;
233 int ret;
235 if (cl->sorted == NULL)
236 return pop_cost_one_pair (cl, p1, p2);
238 if (cl->num_sorted == 0)
239 return pop_cost_one_pair (cl, p1, p2);
241 node = cl->sorted[--(cl->num_sorted)];
242 *p1 = node->first_element;
243 *p2 = node->second_element;
244 ret = node->cost;
245 free (node);
247 return ret;
251 /* Create a new empty coalesce list object and return it. */
253 static inline coalesce_list_p
254 create_coalesce_list (void)
256 coalesce_list_p list;
257 unsigned size = num_ssa_names * 3;
259 if (size < 40)
260 size = 40;
262 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
263 list->list = new coalesce_table_type (size);
264 list->sorted = NULL;
265 list->num_sorted = 0;
266 list->cost_one_list = NULL;
267 return list;
271 /* Delete coalesce list CL. */
273 static inline void
274 delete_coalesce_list (coalesce_list_p cl)
276 gcc_assert (cl->cost_one_list == NULL);
277 delete cl->list;
278 cl->list = NULL;
279 free (cl->sorted);
280 gcc_assert (cl->num_sorted == 0);
281 free (cl);
285 /* Find a matching coalesce pair object in CL for the pair P1 and P2. If
286 one isn't found, return NULL if CREATE is false, otherwise create a new
287 coalesce pair object and return it. */
289 static coalesce_pair_p
290 find_coalesce_pair (coalesce_list_p cl, int p1, int p2, bool create)
292 struct coalesce_pair p;
293 coalesce_pair **slot;
294 unsigned int hash;
296 /* Normalize so that p1 is the smaller value. */
297 if (p2 < p1)
299 p.first_element = p2;
300 p.second_element = p1;
302 else
304 p.first_element = p1;
305 p.second_element = p2;
308 hash = coalesce_pair_hasher::hash (&p);
309 slot = cl->list->find_slot_with_hash (&p, hash, create ? INSERT : NO_INSERT);
310 if (!slot)
311 return NULL;
313 if (!*slot)
315 struct coalesce_pair * pair = XNEW (struct coalesce_pair);
316 gcc_assert (cl->sorted == NULL);
317 pair->first_element = p.first_element;
318 pair->second_element = p.second_element;
319 pair->cost = 0;
320 *slot = pair;
323 return (struct coalesce_pair *) *slot;
326 static inline void
327 add_cost_one_coalesce (coalesce_list_p cl, int p1, int p2)
329 cost_one_pair_p pair;
331 pair = XNEW (struct cost_one_pair_d);
332 pair->first_element = p1;
333 pair->second_element = p2;
334 pair->next = cl->cost_one_list;
335 cl->cost_one_list = pair;
339 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
341 static inline void
342 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
344 coalesce_pair_p node;
346 gcc_assert (cl->sorted == NULL);
347 if (p1 == p2)
348 return;
350 node = find_coalesce_pair (cl, p1, p2, true);
352 /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way. */
353 if (node->cost < MUST_COALESCE_COST - 1)
355 if (value < MUST_COALESCE_COST - 1)
356 node->cost += value;
357 else
358 node->cost = value;
363 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
365 static int
366 compare_pairs (const void *p1, const void *p2)
368 const_coalesce_pair_p const *const pp1 = (const_coalesce_pair_p const *) p1;
369 const_coalesce_pair_p const *const pp2 = (const_coalesce_pair_p const *) p2;
370 int result;
372 result = (* pp1)->cost - (* pp2)->cost;
373 /* Since qsort does not guarantee stability we use the elements
374 as a secondary key. This provides us with independence from
375 the host's implementation of the sorting algorithm. */
376 if (result == 0)
378 result = (* pp2)->first_element - (* pp1)->first_element;
379 if (result == 0)
380 result = (* pp2)->second_element - (* pp1)->second_element;
383 return result;
387 /* Return the number of unique coalesce pairs in CL. */
389 static inline int
390 num_coalesce_pairs (coalesce_list_p cl)
392 return cl->list->elements ();
396 /* Iterate over CL using ITER, returning values in PAIR. */
398 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
399 FOR_EACH_HASH_TABLE_ELEMENT (*(CL)->list, (PAIR), coalesce_pair_p, (ITER))
402 /* Prepare CL for removal of preferred pairs. When finished they are sorted
403 in order from most important coalesce to least important. */
405 static void
406 sort_coalesce_list (coalesce_list_p cl)
408 unsigned x, num;
409 coalesce_pair_p p;
410 coalesce_iterator_type ppi;
412 gcc_assert (cl->sorted == NULL);
414 num = num_coalesce_pairs (cl);
415 cl->num_sorted = num;
416 if (num == 0)
417 return;
419 /* Allocate a vector for the pair pointers. */
420 cl->sorted = XNEWVEC (coalesce_pair_p, num);
422 /* Populate the vector with pointers to the pairs. */
423 x = 0;
424 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
425 cl->sorted[x++] = p;
426 gcc_assert (x == num);
428 /* Already sorted. */
429 if (num == 1)
430 return;
432 /* If there are only 2, just pick swap them if the order isn't correct. */
433 if (num == 2)
435 if (cl->sorted[0]->cost > cl->sorted[1]->cost)
436 std::swap (cl->sorted[0], cl->sorted[1]);
437 return;
440 /* Only call qsort if there are more than 2 items.
441 ??? Maybe std::sort will do better, provided that compare_pairs
442 can be inlined. */
443 if (num > 2)
444 qsort (cl->sorted, num, sizeof (coalesce_pair_p), compare_pairs);
448 /* Send debug info for coalesce list CL to file F. */
450 static void
451 dump_coalesce_list (FILE *f, coalesce_list_p cl)
453 coalesce_pair_p node;
454 coalesce_iterator_type ppi;
456 int x;
457 tree var;
459 if (cl->sorted == NULL)
461 fprintf (f, "Coalesce List:\n");
462 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
464 tree var1 = ssa_name (node->first_element);
465 tree var2 = ssa_name (node->second_element);
466 print_generic_expr (f, var1, TDF_SLIM);
467 fprintf (f, " <-> ");
468 print_generic_expr (f, var2, TDF_SLIM);
469 fprintf (f, " (%1d), ", node->cost);
470 fprintf (f, "\n");
473 else
475 fprintf (f, "Sorted Coalesce list:\n");
476 for (x = cl->num_sorted - 1 ; x >=0; x--)
478 node = cl->sorted[x];
479 fprintf (f, "(%d) ", node->cost);
480 var = ssa_name (node->first_element);
481 print_generic_expr (f, var, TDF_SLIM);
482 fprintf (f, " <-> ");
483 var = ssa_name (node->second_element);
484 print_generic_expr (f, var, TDF_SLIM);
485 fprintf (f, "\n");
491 /* This represents a conflict graph. Implemented as an array of bitmaps.
492 A full matrix is used for conflicts rather than just upper triangular form.
493 this make sit much simpler and faster to perform conflict merges. */
495 typedef struct ssa_conflicts_d
497 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
498 vec<bitmap> conflicts;
499 } * ssa_conflicts_p;
501 /* Return an empty new conflict graph for SIZE elements. */
503 static inline ssa_conflicts_p
504 ssa_conflicts_new (unsigned size)
506 ssa_conflicts_p ptr;
508 ptr = XNEW (struct ssa_conflicts_d);
509 bitmap_obstack_initialize (&ptr->obstack);
510 ptr->conflicts.create (size);
511 ptr->conflicts.safe_grow_cleared (size);
512 return ptr;
516 /* Free storage for conflict graph PTR. */
518 static inline void
519 ssa_conflicts_delete (ssa_conflicts_p ptr)
521 bitmap_obstack_release (&ptr->obstack);
522 ptr->conflicts.release ();
523 free (ptr);
527 /* Test if elements X and Y conflict in graph PTR. */
529 static inline bool
530 ssa_conflicts_test_p (ssa_conflicts_p ptr, unsigned x, unsigned y)
532 bitmap bx = ptr->conflicts[x];
533 bitmap by = ptr->conflicts[y];
535 gcc_checking_assert (x != y);
537 if (bx)
538 /* Avoid the lookup if Y has no conflicts. */
539 return by ? bitmap_bit_p (bx, y) : false;
540 else
541 return false;
545 /* Add a conflict with Y to the bitmap for X in graph PTR. */
547 static inline void
548 ssa_conflicts_add_one (ssa_conflicts_p ptr, unsigned x, unsigned y)
550 bitmap bx = ptr->conflicts[x];
551 /* If there are no conflicts yet, allocate the bitmap and set bit. */
552 if (! bx)
553 bx = ptr->conflicts[x] = BITMAP_ALLOC (&ptr->obstack);
554 bitmap_set_bit (bx, y);
558 /* Add conflicts between X and Y in graph PTR. */
560 static inline void
561 ssa_conflicts_add (ssa_conflicts_p ptr, unsigned x, unsigned y)
563 gcc_checking_assert (x != y);
564 ssa_conflicts_add_one (ptr, x, y);
565 ssa_conflicts_add_one (ptr, y, x);
569 /* Merge all Y's conflict into X in graph PTR. */
571 static inline void
572 ssa_conflicts_merge (ssa_conflicts_p ptr, unsigned x, unsigned y)
574 unsigned z;
575 bitmap_iterator bi;
576 bitmap bx = ptr->conflicts[x];
577 bitmap by = ptr->conflicts[y];
579 gcc_checking_assert (x != y);
580 if (! by)
581 return;
583 /* Add a conflict between X and every one Y has. If the bitmap doesn't
584 exist, then it has already been coalesced, and we don't need to add a
585 conflict. */
586 EXECUTE_IF_SET_IN_BITMAP (by, 0, z, bi)
588 bitmap bz = ptr->conflicts[z];
589 if (bz)
590 bitmap_set_bit (bz, x);
593 if (bx)
595 /* If X has conflicts, add Y's to X. */
596 bitmap_ior_into (bx, by);
597 BITMAP_FREE (by);
598 ptr->conflicts[y] = NULL;
600 else
602 /* If X has no conflicts, simply use Y's. */
603 ptr->conflicts[x] = by;
604 ptr->conflicts[y] = NULL;
609 /* Dump a conflicts graph. */
611 static void
612 ssa_conflicts_dump (FILE *file, ssa_conflicts_p ptr)
614 unsigned x;
615 bitmap b;
617 fprintf (file, "\nConflict graph:\n");
619 FOR_EACH_VEC_ELT (ptr->conflicts, x, b)
620 if (b)
622 fprintf (file, "%d: ", x);
623 dump_bitmap (file, b);
628 /* This structure is used to efficiently record the current status of live
629 SSA_NAMES when building a conflict graph.
630 LIVE_BASE_VAR has a bit set for each base variable which has at least one
631 ssa version live.
632 LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
633 index, and is used to track what partitions of each base variable are
634 live. This makes it easy to add conflicts between just live partitions
635 with the same base variable.
636 The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
637 marked as being live. This delays clearing of these bitmaps until
638 they are actually needed again. */
640 typedef struct live_track_d
642 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
643 bitmap live_base_var; /* Indicates if a basevar is live. */
644 bitmap *live_base_partitions; /* Live partitions for each basevar. */
645 var_map map; /* Var_map being used for partition mapping. */
646 } * live_track_p;
649 /* This routine will create a new live track structure based on the partitions
650 in MAP. */
652 static live_track_p
653 new_live_track (var_map map)
655 live_track_p ptr;
656 int lim, x;
658 /* Make sure there is a partition view in place. */
659 gcc_assert (map->partition_to_base_index != NULL);
661 ptr = (live_track_p) xmalloc (sizeof (struct live_track_d));
662 ptr->map = map;
663 lim = num_basevars (map);
664 bitmap_obstack_initialize (&ptr->obstack);
665 ptr->live_base_partitions = (bitmap *) xmalloc (sizeof (bitmap *) * lim);
666 ptr->live_base_var = BITMAP_ALLOC (&ptr->obstack);
667 for (x = 0; x < lim; x++)
668 ptr->live_base_partitions[x] = BITMAP_ALLOC (&ptr->obstack);
669 return ptr;
673 /* This routine will free the memory associated with PTR. */
675 static void
676 delete_live_track (live_track_p ptr)
678 bitmap_obstack_release (&ptr->obstack);
679 free (ptr->live_base_partitions);
680 free (ptr);
684 /* This function will remove PARTITION from the live list in PTR. */
686 static inline void
687 live_track_remove_partition (live_track_p ptr, int partition)
689 int root;
691 root = basevar_index (ptr->map, partition);
692 bitmap_clear_bit (ptr->live_base_partitions[root], partition);
693 /* If the element list is empty, make the base variable not live either. */
694 if (bitmap_empty_p (ptr->live_base_partitions[root]))
695 bitmap_clear_bit (ptr->live_base_var, root);
699 /* This function will adds PARTITION to the live list in PTR. */
701 static inline void
702 live_track_add_partition (live_track_p ptr, int partition)
704 int root;
706 root = basevar_index (ptr->map, partition);
707 /* If this base var wasn't live before, it is now. Clear the element list
708 since it was delayed until needed. */
709 if (bitmap_set_bit (ptr->live_base_var, root))
710 bitmap_clear (ptr->live_base_partitions[root]);
711 bitmap_set_bit (ptr->live_base_partitions[root], partition);
716 /* Clear the live bit for VAR in PTR. */
718 static inline void
719 live_track_clear_var (live_track_p ptr, tree var)
721 int p;
723 p = var_to_partition (ptr->map, var);
724 if (p != NO_PARTITION)
725 live_track_remove_partition (ptr, p);
729 /* Return TRUE if VAR is live in PTR. */
731 static inline bool
732 live_track_live_p (live_track_p ptr, tree var)
734 int p, root;
736 p = var_to_partition (ptr->map, var);
737 if (p != NO_PARTITION)
739 root = basevar_index (ptr->map, p);
740 if (bitmap_bit_p (ptr->live_base_var, root))
741 return bitmap_bit_p (ptr->live_base_partitions[root], p);
743 return false;
747 /* This routine will add USE to PTR. USE will be marked as live in both the
748 ssa live map and the live bitmap for the root of USE. */
750 static inline void
751 live_track_process_use (live_track_p ptr, tree use)
753 int p;
755 p = var_to_partition (ptr->map, use);
756 if (p == NO_PARTITION)
757 return;
759 /* Mark as live in the appropriate live list. */
760 live_track_add_partition (ptr, p);
764 /* This routine will process a DEF in PTR. DEF will be removed from the live
765 lists, and if there are any other live partitions with the same base
766 variable, conflicts will be added to GRAPH. */
768 static inline void
769 live_track_process_def (live_track_p ptr, tree def, ssa_conflicts_p graph)
771 int p, root;
772 bitmap b;
773 unsigned x;
774 bitmap_iterator bi;
776 p = var_to_partition (ptr->map, def);
777 if (p == NO_PARTITION)
778 return;
780 /* Clear the liveness bit. */
781 live_track_remove_partition (ptr, p);
783 /* If the bitmap isn't empty now, conflicts need to be added. */
784 root = basevar_index (ptr->map, p);
785 if (bitmap_bit_p (ptr->live_base_var, root))
787 b = ptr->live_base_partitions[root];
788 EXECUTE_IF_SET_IN_BITMAP (b, 0, x, bi)
789 ssa_conflicts_add (graph, p, x);
794 /* Initialize PTR with the partitions set in INIT. */
796 static inline void
797 live_track_init (live_track_p ptr, bitmap init)
799 unsigned p;
800 bitmap_iterator bi;
802 /* Mark all live on exit partitions. */
803 EXECUTE_IF_SET_IN_BITMAP (init, 0, p, bi)
804 live_track_add_partition (ptr, p);
808 /* This routine will clear all live partitions in PTR. */
810 static inline void
811 live_track_clear_base_vars (live_track_p ptr)
813 /* Simply clear the live base list. Anything marked as live in the element
814 lists will be cleared later if/when the base variable ever comes alive
815 again. */
816 bitmap_clear (ptr->live_base_var);
820 /* Build a conflict graph based on LIVEINFO. Any partitions which are in the
821 partition view of the var_map liveinfo is based on get entries in the
822 conflict graph. Only conflicts between ssa_name partitions with the same
823 base variable are added. */
825 static ssa_conflicts_p
826 build_ssa_conflict_graph (tree_live_info_p liveinfo)
828 ssa_conflicts_p graph;
829 var_map map;
830 basic_block bb;
831 ssa_op_iter iter;
832 live_track_p live;
834 map = live_var_map (liveinfo);
835 graph = ssa_conflicts_new (num_var_partitions (map));
837 live = new_live_track (map);
839 FOR_EACH_BB_FN (bb, cfun)
841 /* Start with live on exit temporaries. */
842 live_track_init (live, live_on_exit (liveinfo, bb));
844 for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);
845 gsi_prev (&gsi))
847 tree var;
848 gimple stmt = gsi_stmt (gsi);
850 /* A copy between 2 partitions does not introduce an interference
851 by itself. If they did, you would never be able to coalesce
852 two things which are copied. If the two variables really do
853 conflict, they will conflict elsewhere in the program.
855 This is handled by simply removing the SRC of the copy from the
856 live list, and processing the stmt normally. */
857 if (is_gimple_assign (stmt))
859 tree lhs = gimple_assign_lhs (stmt);
860 tree rhs1 = gimple_assign_rhs1 (stmt);
861 if (gimple_assign_copy_p (stmt)
862 && TREE_CODE (lhs) == SSA_NAME
863 && TREE_CODE (rhs1) == SSA_NAME)
864 live_track_clear_var (live, rhs1);
866 else if (is_gimple_debug (stmt))
867 continue;
869 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
870 live_track_process_def (live, var, graph);
872 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
873 live_track_process_use (live, var);
876 /* If result of a PHI is unused, looping over the statements will not
877 record any conflicts since the def was never live. Since the PHI node
878 is going to be translated out of SSA form, it will insert a copy.
879 There must be a conflict recorded between the result of the PHI and
880 any variables that are live. Otherwise the out-of-ssa translation
881 may create incorrect code. */
882 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
883 gsi_next (&gsi))
885 gphi *phi = gsi.phi ();
886 tree result = PHI_RESULT (phi);
887 if (live_track_live_p (live, result))
888 live_track_process_def (live, result, graph);
891 live_track_clear_base_vars (live);
894 delete_live_track (live);
895 return graph;
899 /* Shortcut routine to print messages to file F of the form:
900 "STR1 EXPR1 STR2 EXPR2 STR3." */
902 static inline void
903 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
904 tree expr2, const char *str3)
906 fprintf (f, "%s", str1);
907 print_generic_expr (f, expr1, TDF_SLIM);
908 fprintf (f, "%s", str2);
909 print_generic_expr (f, expr2, TDF_SLIM);
910 fprintf (f, "%s", str3);
914 /* Print a failure to coalesce a MUST_COALESCE pair X and Y. */
916 static inline void
917 fail_abnormal_edge_coalesce (int x, int y)
919 fprintf (stderr, "\nUnable to coalesce ssa_names %d and %d",x, y);
920 fprintf (stderr, " which are marked as MUST COALESCE.\n");
921 print_generic_expr (stderr, ssa_name (x), TDF_SLIM);
922 fprintf (stderr, " and ");
923 print_generic_stmt (stderr, ssa_name (y), TDF_SLIM);
925 internal_error ("SSA corruption");
929 /* This function creates a var_map for the current function as well as creating
930 a coalesce list for use later in the out of ssa process. */
932 static var_map
933 create_outofssa_var_map (coalesce_list_p cl, bitmap used_in_copy)
935 gimple_stmt_iterator gsi;
936 basic_block bb;
937 tree var;
938 gimple stmt;
939 tree first;
940 var_map map;
941 ssa_op_iter iter;
942 int v1, v2, cost;
943 unsigned i;
945 map = init_var_map (num_ssa_names);
947 FOR_EACH_BB_FN (bb, cfun)
949 tree arg;
951 for (gphi_iterator gpi = gsi_start_phis (bb);
952 !gsi_end_p (gpi);
953 gsi_next (&gpi))
955 gphi *phi = gpi.phi ();
956 size_t i;
957 int ver;
958 tree res;
959 bool saw_copy = false;
961 res = gimple_phi_result (phi);
962 ver = SSA_NAME_VERSION (res);
963 register_ssa_partition (map, res);
965 /* Register ssa_names and coalesces between the args and the result
966 of all PHI. */
967 for (i = 0; i < gimple_phi_num_args (phi); i++)
969 edge e = gimple_phi_arg_edge (phi, i);
970 arg = PHI_ARG_DEF (phi, i);
971 if (TREE_CODE (arg) != SSA_NAME)
972 continue;
974 register_ssa_partition (map, arg);
975 if (gimple_can_coalesce_p (arg, res)
976 || (e->flags & EDGE_ABNORMAL))
978 saw_copy = true;
979 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (arg));
980 if ((e->flags & EDGE_ABNORMAL) == 0)
982 int cost = coalesce_cost_edge (e);
983 if (cost == 1 && has_single_use (arg))
984 add_cost_one_coalesce (cl, ver, SSA_NAME_VERSION (arg));
985 else
986 add_coalesce (cl, ver, SSA_NAME_VERSION (arg), cost);
990 if (saw_copy)
991 bitmap_set_bit (used_in_copy, ver);
994 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
996 stmt = gsi_stmt (gsi);
998 if (is_gimple_debug (stmt))
999 continue;
1001 /* Register USE and DEF operands in each statement. */
1002 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
1003 register_ssa_partition (map, var);
1005 /* Check for copy coalesces. */
1006 switch (gimple_code (stmt))
1008 case GIMPLE_ASSIGN:
1010 tree lhs = gimple_assign_lhs (stmt);
1011 tree rhs1 = gimple_assign_rhs1 (stmt);
1012 if (gimple_assign_ssa_name_copy_p (stmt)
1013 && gimple_can_coalesce_p (lhs, rhs1))
1015 v1 = SSA_NAME_VERSION (lhs);
1016 v2 = SSA_NAME_VERSION (rhs1);
1017 cost = coalesce_cost_bb (bb);
1018 add_coalesce (cl, v1, v2, cost);
1019 bitmap_set_bit (used_in_copy, v1);
1020 bitmap_set_bit (used_in_copy, v2);
1023 break;
1025 case GIMPLE_ASM:
1027 gasm *asm_stmt = as_a <gasm *> (stmt);
1028 unsigned long noutputs, i;
1029 unsigned long ninputs;
1030 tree *outputs, link;
1031 noutputs = gimple_asm_noutputs (asm_stmt);
1032 ninputs = gimple_asm_ninputs (asm_stmt);
1033 outputs = (tree *) alloca (noutputs * sizeof (tree));
1034 for (i = 0; i < noutputs; ++i)
1036 link = gimple_asm_output_op (asm_stmt, i);
1037 outputs[i] = TREE_VALUE (link);
1040 for (i = 0; i < ninputs; ++i)
1042 const char *constraint;
1043 tree input;
1044 char *end;
1045 unsigned long match;
1047 link = gimple_asm_input_op (asm_stmt, i);
1048 constraint
1049 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1050 input = TREE_VALUE (link);
1052 if (TREE_CODE (input) != SSA_NAME)
1053 continue;
1055 match = strtoul (constraint, &end, 10);
1056 if (match >= noutputs || end == constraint)
1057 continue;
1059 if (TREE_CODE (outputs[match]) != SSA_NAME)
1060 continue;
1062 v1 = SSA_NAME_VERSION (outputs[match]);
1063 v2 = SSA_NAME_VERSION (input);
1065 if (gimple_can_coalesce_p (outputs[match], input))
1067 cost = coalesce_cost (REG_BR_PROB_BASE,
1068 optimize_bb_for_size_p (bb));
1069 add_coalesce (cl, v1, v2, cost);
1070 bitmap_set_bit (used_in_copy, v1);
1071 bitmap_set_bit (used_in_copy, v2);
1074 break;
1077 default:
1078 break;
1083 /* Now process result decls and live on entry variables for entry into
1084 the coalesce list. */
1085 first = NULL_TREE;
1086 for (i = 1; i < num_ssa_names; i++)
1088 var = ssa_name (i);
1089 if (var != NULL_TREE && !virtual_operand_p (var))
1091 /* Add coalesces between all the result decls. */
1092 if (SSA_NAME_VAR (var)
1093 && TREE_CODE (SSA_NAME_VAR (var)) == RESULT_DECL)
1095 if (first == NULL_TREE)
1096 first = var;
1097 else
1099 gcc_assert (gimple_can_coalesce_p (var, first));
1100 v1 = SSA_NAME_VERSION (first);
1101 v2 = SSA_NAME_VERSION (var);
1102 bitmap_set_bit (used_in_copy, v1);
1103 bitmap_set_bit (used_in_copy, v2);
1104 cost = coalesce_cost_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
1105 add_coalesce (cl, v1, v2, cost);
1108 /* Mark any default_def variables as being in the coalesce list
1109 since they will have to be coalesced with the base variable. If
1110 not marked as present, they won't be in the coalesce view. */
1111 if (SSA_NAME_IS_DEFAULT_DEF (var)
1112 && !has_zero_uses (var))
1113 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1117 return map;
1121 /* Attempt to coalesce ssa versions X and Y together using the partition
1122 mapping in MAP and checking conflicts in GRAPH. Output any debug info to
1123 DEBUG, if it is nun-NULL. */
1125 static inline bool
1126 attempt_coalesce (var_map map, ssa_conflicts_p graph, int x, int y,
1127 FILE *debug)
1129 int z;
1130 tree var1, var2;
1131 int p1, p2;
1133 p1 = var_to_partition (map, ssa_name (x));
1134 p2 = var_to_partition (map, ssa_name (y));
1136 if (debug)
1138 fprintf (debug, "(%d)", x);
1139 print_generic_expr (debug, partition_to_var (map, p1), TDF_SLIM);
1140 fprintf (debug, " & (%d)", y);
1141 print_generic_expr (debug, partition_to_var (map, p2), TDF_SLIM);
1144 if (p1 == p2)
1146 if (debug)
1147 fprintf (debug, ": Already Coalesced.\n");
1148 return true;
1151 if (debug)
1152 fprintf (debug, " [map: %d, %d] ", p1, p2);
1155 if (!ssa_conflicts_test_p (graph, p1, p2))
1157 var1 = partition_to_var (map, p1);
1158 var2 = partition_to_var (map, p2);
1159 z = var_union (map, var1, var2);
1160 if (z == NO_PARTITION)
1162 if (debug)
1163 fprintf (debug, ": Unable to perform partition union.\n");
1164 return false;
1167 /* z is the new combined partition. Remove the other partition from
1168 the list, and merge the conflicts. */
1169 if (z == p1)
1170 ssa_conflicts_merge (graph, p1, p2);
1171 else
1172 ssa_conflicts_merge (graph, p2, p1);
1174 if (debug)
1175 fprintf (debug, ": Success -> %d\n", z);
1176 return true;
1179 if (debug)
1180 fprintf (debug, ": Fail due to conflict\n");
1182 return false;
1186 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1187 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1189 static void
1190 coalesce_partitions (var_map map, ssa_conflicts_p graph, coalesce_list_p cl,
1191 FILE *debug)
1193 int x = 0, y = 0;
1194 tree var1, var2;
1195 int cost;
1196 basic_block bb;
1197 edge e;
1198 edge_iterator ei;
1200 /* First, coalesce all the copies across abnormal edges. These are not placed
1201 in the coalesce list because they do not need to be sorted, and simply
1202 consume extra memory/compilation time in large programs. */
1204 FOR_EACH_BB_FN (bb, cfun)
1206 FOR_EACH_EDGE (e, ei, bb->preds)
1207 if (e->flags & EDGE_ABNORMAL)
1209 gphi_iterator gsi;
1210 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1211 gsi_next (&gsi))
1213 gphi *phi = gsi.phi ();
1214 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1215 if (SSA_NAME_IS_DEFAULT_DEF (arg)
1216 && (!SSA_NAME_VAR (arg)
1217 || TREE_CODE (SSA_NAME_VAR (arg)) != PARM_DECL))
1218 continue;
1220 tree res = PHI_RESULT (phi);
1221 int v1 = SSA_NAME_VERSION (res);
1222 int v2 = SSA_NAME_VERSION (arg);
1224 if (debug)
1225 fprintf (debug, "Abnormal coalesce: ");
1227 if (!attempt_coalesce (map, graph, v1, v2, debug))
1228 fail_abnormal_edge_coalesce (v1, v2);
1233 /* Now process the items in the coalesce list. */
1235 while ((cost = pop_best_coalesce (cl, &x, &y)) != NO_BEST_COALESCE)
1237 var1 = ssa_name (x);
1238 var2 = ssa_name (y);
1240 /* Assert the coalesces have the same base variable. */
1241 gcc_assert (gimple_can_coalesce_p (var1, var2));
1243 if (debug)
1244 fprintf (debug, "Coalesce list: ");
1245 attempt_coalesce (map, graph, x, y, debug);
1250 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1252 struct ssa_name_var_hash : typed_noop_remove <tree_node>
1254 typedef union tree_node *value_type;
1255 typedef union tree_node *compare_type;
1256 static inline hashval_t hash (const tree_node *);
1257 static inline int equal (const tree_node *, const tree_node *);
1260 inline hashval_t
1261 ssa_name_var_hash::hash (const_tree n)
1263 return DECL_UID (SSA_NAME_VAR (n));
1266 inline int
1267 ssa_name_var_hash::equal (const tree_node *n1, const tree_node *n2)
1269 return SSA_NAME_VAR (n1) == SSA_NAME_VAR (n2);
1273 /* Reduce the number of copies by coalescing variables in the function. Return
1274 a partition map with the resulting coalesces. */
1276 extern var_map
1277 coalesce_ssa_name (void)
1279 tree_live_info_p liveinfo;
1280 ssa_conflicts_p graph;
1281 coalesce_list_p cl;
1282 bitmap used_in_copies = BITMAP_ALLOC (NULL);
1283 var_map map;
1284 unsigned int i;
1286 cl = create_coalesce_list ();
1287 map = create_outofssa_var_map (cl, used_in_copies);
1289 /* If optimization is disabled, we need to coalesce all the names originating
1290 from the same SSA_NAME_VAR so debug info remains undisturbed. */
1291 if (!optimize)
1293 hash_table<ssa_name_var_hash> ssa_name_hash (10);
1295 for (i = 1; i < num_ssa_names; i++)
1297 tree a = ssa_name (i);
1299 if (a
1300 && SSA_NAME_VAR (a)
1301 && !DECL_IGNORED_P (SSA_NAME_VAR (a))
1302 && (!has_zero_uses (a) || !SSA_NAME_IS_DEFAULT_DEF (a)))
1304 tree *slot = ssa_name_hash.find_slot (a, INSERT);
1306 if (!*slot)
1307 *slot = a;
1308 else
1310 /* If the variable is a PARM_DECL or a RESULT_DECL, we
1311 _require_ that all the names originating from it be
1312 coalesced, because there must be a single partition
1313 containing all the names so that it can be assigned
1314 the canonical RTL location of the DECL safely.
1315 If in_lto_p, a function could have been compiled
1316 originally with optimizations and only the link
1317 performed at -O0, so we can't actually require it. */
1318 const int cost
1319 = (TREE_CODE (SSA_NAME_VAR (a)) == VAR_DECL || in_lto_p)
1320 ? MUST_COALESCE_COST - 1 : MUST_COALESCE_COST;
1321 add_coalesce (cl, SSA_NAME_VERSION (a),
1322 SSA_NAME_VERSION (*slot), cost);
1323 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (a));
1324 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (*slot));
1329 if (dump_file && (dump_flags & TDF_DETAILS))
1330 dump_var_map (dump_file, map);
1332 /* Don't calculate live ranges for variables not in the coalesce list. */
1333 partition_view_bitmap (map, used_in_copies, true);
1334 BITMAP_FREE (used_in_copies);
1336 if (num_var_partitions (map) < 1)
1338 delete_coalesce_list (cl);
1339 return map;
1342 if (dump_file && (dump_flags & TDF_DETAILS))
1343 dump_var_map (dump_file, map);
1345 liveinfo = calculate_live_ranges (map, false);
1347 if (dump_file && (dump_flags & TDF_DETAILS))
1348 dump_live_info (dump_file, liveinfo, LIVEDUMP_ENTRY);
1350 /* Build a conflict graph. */
1351 graph = build_ssa_conflict_graph (liveinfo);
1352 delete_tree_live_info (liveinfo);
1353 if (dump_file && (dump_flags & TDF_DETAILS))
1354 ssa_conflicts_dump (dump_file, graph);
1356 sort_coalesce_list (cl);
1358 if (dump_file && (dump_flags & TDF_DETAILS))
1360 fprintf (dump_file, "\nAfter sorting:\n");
1361 dump_coalesce_list (dump_file, cl);
1364 /* First, coalesce all live on entry variables to their base variable.
1365 This will ensure the first use is coming from the correct location. */
1367 if (dump_file && (dump_flags & TDF_DETAILS))
1368 dump_var_map (dump_file, map);
1370 /* Now coalesce everything in the list. */
1371 coalesce_partitions (map, graph, cl,
1372 ((dump_flags & TDF_DETAILS) ? dump_file
1373 : NULL));
1375 delete_coalesce_list (cl);
1376 ssa_conflicts_delete (graph);
1378 return map;