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