[AArch64] Remove simd_type
[official-gcc.git] / gcc / tree-ssa-coalesce.c
blob942602e5fe3e0ae3559245447e525127582e07fb
1 /* Coalesce SSA_NAMES together for the out-of-ssa pass.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "tree-pretty-print.h"
28 #include "bitmap.h"
29 #include "dumpfile.h"
30 #include "gimple.h"
31 #include "gimple-iterator.h"
32 #include "gimple-ssa.h"
33 #include "tree-phinodes.h"
34 #include "ssa-iterators.h"
35 #include "tree-ssanames.h"
36 #include "hash-table.h"
37 #include "tree-ssa-live.h"
38 #include "tree-ssa-coalesce.h"
39 #include "diagnostic-core.h"
42 /* This set of routines implements a coalesce_list. This is an object which
43 is used to track pairs of ssa_names which are desirable to coalesce
44 together to avoid copies. Costs are associated with each pair, and when
45 all desired information has been collected, the object can be used to
46 order the pairs for processing. */
48 /* This structure defines a pair entry. */
50 typedef struct coalesce_pair
52 int first_element;
53 int second_element;
54 int cost;
55 } * coalesce_pair_p;
56 typedef const struct coalesce_pair *const_coalesce_pair_p;
58 /* Coalesce pair hashtable helpers. */
60 struct coalesce_pair_hasher : typed_noop_remove <coalesce_pair>
62 typedef coalesce_pair value_type;
63 typedef coalesce_pair compare_type;
64 static inline hashval_t hash (const value_type *);
65 static inline bool equal (const value_type *, const compare_type *);
68 /* Hash function for coalesce list. Calculate hash for PAIR. */
70 inline hashval_t
71 coalesce_pair_hasher::hash (const value_type *pair)
73 hashval_t a = (hashval_t)(pair->first_element);
74 hashval_t b = (hashval_t)(pair->second_element);
76 return b * (b - 1) / 2 + a;
79 /* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
80 returning TRUE if the two pairs are equivalent. */
82 inline bool
83 coalesce_pair_hasher::equal (const value_type *p1, const compare_type *p2)
85 return (p1->first_element == p2->first_element
86 && p1->second_element == p2->second_element);
89 typedef hash_table <coalesce_pair_hasher> coalesce_table_type;
90 typedef coalesce_table_type::iterator coalesce_iterator_type;
93 typedef struct cost_one_pair_d
95 int first_element;
96 int second_element;
97 struct cost_one_pair_d *next;
98 } * cost_one_pair_p;
100 /* This structure maintains the list of coalesce pairs. */
102 typedef struct coalesce_list_d
104 coalesce_table_type list; /* Hash table. */
105 coalesce_pair_p *sorted; /* List when sorted. */
106 int num_sorted; /* Number in the sorted list. */
107 cost_one_pair_p cost_one_list;/* Single use coalesces with cost 1. */
108 } *coalesce_list_p;
110 #define NO_BEST_COALESCE -1
111 #define MUST_COALESCE_COST INT_MAX
114 /* Return cost of execution of copy instruction with FREQUENCY. */
116 static inline int
117 coalesce_cost (int frequency, bool optimize_for_size)
119 /* Base costs on BB frequencies bounded by 1. */
120 int cost = frequency;
122 if (!cost)
123 cost = 1;
125 if (optimize_for_size)
126 cost = 1;
128 return cost;
132 /* Return the cost of executing a copy instruction in basic block BB. */
134 static inline int
135 coalesce_cost_bb (basic_block bb)
137 return coalesce_cost (bb->frequency, optimize_bb_for_size_p (bb));
141 /* Return the cost of executing a copy instruction on edge E. */
143 static inline int
144 coalesce_cost_edge (edge e)
146 int mult = 1;
148 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
149 if (EDGE_CRITICAL_P (e))
150 mult = 2;
151 if (e->flags & EDGE_ABNORMAL)
152 return MUST_COALESCE_COST;
153 if (e->flags & EDGE_EH)
155 edge e2;
156 edge_iterator ei;
157 FOR_EACH_EDGE (e2, ei, e->dest->preds)
158 if (e2 != e)
160 /* Putting code on EH edge that leads to BB
161 with multiple predecestors imply splitting of
162 edge too. */
163 if (mult < 2)
164 mult = 2;
165 /* If there are multiple EH predecestors, we
166 also copy EH regions and produce separate
167 landing pad. This is expensive. */
168 if (e2->flags & EDGE_EH)
170 mult = 5;
171 break;
176 return coalesce_cost (EDGE_FREQUENCY (e),
177 optimize_edge_for_size_p (e)) * mult;
181 /* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
182 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
183 NO_BEST_COALESCE is returned if there aren't any. */
185 static inline int
186 pop_cost_one_pair (coalesce_list_p cl, int *p1, int *p2)
188 cost_one_pair_p ptr;
190 ptr = cl->cost_one_list;
191 if (!ptr)
192 return NO_BEST_COALESCE;
194 *p1 = ptr->first_element;
195 *p2 = ptr->second_element;
196 cl->cost_one_list = ptr->next;
198 free (ptr);
200 return 1;
203 /* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
204 2 elements via P1 and P2. Their calculated cost is returned by the function.
205 NO_BEST_COALESCE is returned if the coalesce list is empty. */
207 static inline int
208 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
210 coalesce_pair_p node;
211 int ret;
213 if (cl->sorted == NULL)
214 return pop_cost_one_pair (cl, p1, p2);
216 if (cl->num_sorted == 0)
217 return pop_cost_one_pair (cl, p1, p2);
219 node = cl->sorted[--(cl->num_sorted)];
220 *p1 = node->first_element;
221 *p2 = node->second_element;
222 ret = node->cost;
223 free (node);
225 return ret;
229 /* Create a new empty coalesce list object and return it. */
231 static inline coalesce_list_p
232 create_coalesce_list (void)
234 coalesce_list_p list;
235 unsigned size = num_ssa_names * 3;
237 if (size < 40)
238 size = 40;
240 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
241 list->list.create (size);
242 list->sorted = NULL;
243 list->num_sorted = 0;
244 list->cost_one_list = NULL;
245 return list;
249 /* Delete coalesce list CL. */
251 static inline void
252 delete_coalesce_list (coalesce_list_p cl)
254 gcc_assert (cl->cost_one_list == NULL);
255 cl->list.dispose ();
256 free (cl->sorted);
257 gcc_assert (cl->num_sorted == 0);
258 free (cl);
262 /* Find a matching coalesce pair object in CL for the pair P1 and P2. If
263 one isn't found, return NULL if CREATE is false, otherwise create a new
264 coalesce pair object and return it. */
266 static coalesce_pair_p
267 find_coalesce_pair (coalesce_list_p cl, int p1, int p2, bool create)
269 struct coalesce_pair p;
270 coalesce_pair **slot;
271 unsigned int hash;
273 /* Normalize so that p1 is the smaller value. */
274 if (p2 < p1)
276 p.first_element = p2;
277 p.second_element = p1;
279 else
281 p.first_element = p1;
282 p.second_element = p2;
285 hash = coalesce_pair_hasher::hash (&p);
286 slot = cl->list.find_slot_with_hash (&p, hash, create ? INSERT : NO_INSERT);
287 if (!slot)
288 return NULL;
290 if (!*slot)
292 struct coalesce_pair * pair = XNEW (struct coalesce_pair);
293 gcc_assert (cl->sorted == NULL);
294 pair->first_element = p.first_element;
295 pair->second_element = p.second_element;
296 pair->cost = 0;
297 *slot = pair;
300 return (struct coalesce_pair *) *slot;
303 static inline void
304 add_cost_one_coalesce (coalesce_list_p cl, int p1, int p2)
306 cost_one_pair_p pair;
308 pair = XNEW (struct cost_one_pair_d);
309 pair->first_element = p1;
310 pair->second_element = p2;
311 pair->next = cl->cost_one_list;
312 cl->cost_one_list = pair;
316 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
318 static inline void
319 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
321 coalesce_pair_p node;
323 gcc_assert (cl->sorted == NULL);
324 if (p1 == p2)
325 return;
327 node = find_coalesce_pair (cl, p1, p2, true);
329 /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way. */
330 if (node->cost < MUST_COALESCE_COST - 1)
332 if (value < MUST_COALESCE_COST - 1)
333 node->cost += value;
334 else
335 node->cost = value;
340 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
342 static int
343 compare_pairs (const void *p1, const void *p2)
345 const_coalesce_pair_p const *const pp1 = (const_coalesce_pair_p const *) p1;
346 const_coalesce_pair_p const *const pp2 = (const_coalesce_pair_p const *) p2;
347 int result;
349 result = (* pp1)->cost - (* pp2)->cost;
350 /* Since qsort does not guarantee stability we use the elements
351 as a secondary key. This provides us with independence from
352 the host's implementation of the sorting algorithm. */
353 if (result == 0)
355 result = (* pp2)->first_element - (* pp1)->first_element;
356 if (result == 0)
357 result = (* pp2)->second_element - (* pp1)->second_element;
360 return result;
364 /* Return the number of unique coalesce pairs in CL. */
366 static inline int
367 num_coalesce_pairs (coalesce_list_p cl)
369 return cl->list.elements ();
373 /* Iterate over CL using ITER, returning values in PAIR. */
375 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
376 FOR_EACH_HASH_TABLE_ELEMENT ((CL)->list, (PAIR), coalesce_pair_p, (ITER))
379 /* Prepare CL for removal of preferred pairs. When finished they are sorted
380 in order from most important coalesce to least important. */
382 static void
383 sort_coalesce_list (coalesce_list_p cl)
385 unsigned x, num;
386 coalesce_pair_p p;
387 coalesce_iterator_type ppi;
389 gcc_assert (cl->sorted == NULL);
391 num = num_coalesce_pairs (cl);
392 cl->num_sorted = num;
393 if (num == 0)
394 return;
396 /* Allocate a vector for the pair pointers. */
397 cl->sorted = XNEWVEC (coalesce_pair_p, num);
399 /* Populate the vector with pointers to the pairs. */
400 x = 0;
401 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
402 cl->sorted[x++] = p;
403 gcc_assert (x == num);
405 /* Already sorted. */
406 if (num == 1)
407 return;
409 /* If there are only 2, just pick swap them if the order isn't correct. */
410 if (num == 2)
412 if (cl->sorted[0]->cost > cl->sorted[1]->cost)
414 p = cl->sorted[0];
415 cl->sorted[0] = cl->sorted[1];
416 cl->sorted[1] = p;
418 return;
421 /* Only call qsort if there are more than 2 items. */
422 if (num > 2)
423 qsort (cl->sorted, num, sizeof (coalesce_pair_p), compare_pairs);
427 /* Send debug info for coalesce list CL to file F. */
429 static void
430 dump_coalesce_list (FILE *f, coalesce_list_p cl)
432 coalesce_pair_p node;
433 coalesce_iterator_type ppi;
435 int x;
436 tree var;
438 if (cl->sorted == NULL)
440 fprintf (f, "Coalesce List:\n");
441 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
443 tree var1 = ssa_name (node->first_element);
444 tree var2 = ssa_name (node->second_element);
445 print_generic_expr (f, var1, TDF_SLIM);
446 fprintf (f, " <-> ");
447 print_generic_expr (f, var2, TDF_SLIM);
448 fprintf (f, " (%1d), ", node->cost);
449 fprintf (f, "\n");
452 else
454 fprintf (f, "Sorted Coalesce list:\n");
455 for (x = cl->num_sorted - 1 ; x >=0; x--)
457 node = cl->sorted[x];
458 fprintf (f, "(%d) ", node->cost);
459 var = ssa_name (node->first_element);
460 print_generic_expr (f, var, TDF_SLIM);
461 fprintf (f, " <-> ");
462 var = ssa_name (node->second_element);
463 print_generic_expr (f, var, TDF_SLIM);
464 fprintf (f, "\n");
470 /* This represents a conflict graph. Implemented as an array of bitmaps.
471 A full matrix is used for conflicts rather than just upper triangular form.
472 this make sit much simpler and faster to perform conflict merges. */
474 typedef struct ssa_conflicts_d
476 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
477 vec<bitmap> conflicts;
478 } * ssa_conflicts_p;
480 /* Return an empty new conflict graph for SIZE elements. */
482 static inline ssa_conflicts_p
483 ssa_conflicts_new (unsigned size)
485 ssa_conflicts_p ptr;
487 ptr = XNEW (struct ssa_conflicts_d);
488 bitmap_obstack_initialize (&ptr->obstack);
489 ptr->conflicts.create (size);
490 ptr->conflicts.safe_grow_cleared (size);
491 return ptr;
495 /* Free storage for conflict graph PTR. */
497 static inline void
498 ssa_conflicts_delete (ssa_conflicts_p ptr)
500 bitmap_obstack_release (&ptr->obstack);
501 ptr->conflicts.release ();
502 free (ptr);
506 /* Test if elements X and Y conflict in graph PTR. */
508 static inline bool
509 ssa_conflicts_test_p (ssa_conflicts_p ptr, unsigned x, unsigned y)
511 bitmap bx = ptr->conflicts[x];
512 bitmap by = ptr->conflicts[y];
514 gcc_checking_assert (x != y);
516 if (bx)
517 /* Avoid the lookup if Y has no conflicts. */
518 return by ? bitmap_bit_p (bx, y) : false;
519 else
520 return false;
524 /* Add a conflict with Y to the bitmap for X in graph PTR. */
526 static inline void
527 ssa_conflicts_add_one (ssa_conflicts_p ptr, unsigned x, unsigned y)
529 bitmap bx = ptr->conflicts[x];
530 /* If there are no conflicts yet, allocate the bitmap and set bit. */
531 if (! bx)
532 bx = ptr->conflicts[x] = BITMAP_ALLOC (&ptr->obstack);
533 bitmap_set_bit (bx, y);
537 /* Add conflicts between X and Y in graph PTR. */
539 static inline void
540 ssa_conflicts_add (ssa_conflicts_p ptr, unsigned x, unsigned y)
542 gcc_checking_assert (x != y);
543 ssa_conflicts_add_one (ptr, x, y);
544 ssa_conflicts_add_one (ptr, y, x);
548 /* Merge all Y's conflict into X in graph PTR. */
550 static inline void
551 ssa_conflicts_merge (ssa_conflicts_p ptr, unsigned x, unsigned y)
553 unsigned z;
554 bitmap_iterator bi;
555 bitmap bx = ptr->conflicts[x];
556 bitmap by = ptr->conflicts[y];
558 gcc_checking_assert (x != y);
559 if (! by)
560 return;
562 /* Add a conflict between X and every one Y has. If the bitmap doesn't
563 exist, then it has already been coalesced, and we don't need to add a
564 conflict. */
565 EXECUTE_IF_SET_IN_BITMAP (by, 0, z, bi)
567 bitmap bz = ptr->conflicts[z];
568 if (bz)
569 bitmap_set_bit (bz, x);
572 if (bx)
574 /* If X has conflicts, add Y's to X. */
575 bitmap_ior_into (bx, by);
576 BITMAP_FREE (by);
577 ptr->conflicts[y] = NULL;
579 else
581 /* If X has no conflicts, simply use Y's. */
582 ptr->conflicts[x] = by;
583 ptr->conflicts[y] = NULL;
588 /* Dump a conflicts graph. */
590 static void
591 ssa_conflicts_dump (FILE *file, ssa_conflicts_p ptr)
593 unsigned x;
594 bitmap b;
596 fprintf (file, "\nConflict graph:\n");
598 FOR_EACH_VEC_ELT (ptr->conflicts, x, b)
599 if (b)
601 fprintf (file, "%d: ", x);
602 dump_bitmap (file, b);
607 /* This structure is used to efficiently record the current status of live
608 SSA_NAMES when building a conflict graph.
609 LIVE_BASE_VAR has a bit set for each base variable which has at least one
610 ssa version live.
611 LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
612 index, and is used to track what partitions of each base variable are
613 live. This makes it easy to add conflicts between just live partitions
614 with the same base variable.
615 The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
616 marked as being live. This delays clearing of these bitmaps until
617 they are actually needed again. */
619 typedef struct live_track_d
621 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
622 bitmap live_base_var; /* Indicates if a basevar is live. */
623 bitmap *live_base_partitions; /* Live partitions for each basevar. */
624 var_map map; /* Var_map being used for partition mapping. */
625 } * live_track_p;
628 /* This routine will create a new live track structure based on the partitions
629 in MAP. */
631 static live_track_p
632 new_live_track (var_map map)
634 live_track_p ptr;
635 int lim, x;
637 /* Make sure there is a partition view in place. */
638 gcc_assert (map->partition_to_base_index != NULL);
640 ptr = (live_track_p) xmalloc (sizeof (struct live_track_d));
641 ptr->map = map;
642 lim = num_basevars (map);
643 bitmap_obstack_initialize (&ptr->obstack);
644 ptr->live_base_partitions = (bitmap *) xmalloc (sizeof (bitmap *) * lim);
645 ptr->live_base_var = BITMAP_ALLOC (&ptr->obstack);
646 for (x = 0; x < lim; x++)
647 ptr->live_base_partitions[x] = BITMAP_ALLOC (&ptr->obstack);
648 return ptr;
652 /* This routine will free the memory associated with PTR. */
654 static void
655 delete_live_track (live_track_p ptr)
657 bitmap_obstack_release (&ptr->obstack);
658 free (ptr->live_base_partitions);
659 free (ptr);
663 /* This function will remove PARTITION from the live list in PTR. */
665 static inline void
666 live_track_remove_partition (live_track_p ptr, int partition)
668 int root;
670 root = basevar_index (ptr->map, partition);
671 bitmap_clear_bit (ptr->live_base_partitions[root], partition);
672 /* If the element list is empty, make the base variable not live either. */
673 if (bitmap_empty_p (ptr->live_base_partitions[root]))
674 bitmap_clear_bit (ptr->live_base_var, root);
678 /* This function will adds PARTITION to the live list in PTR. */
680 static inline void
681 live_track_add_partition (live_track_p ptr, int partition)
683 int root;
685 root = basevar_index (ptr->map, partition);
686 /* If this base var wasn't live before, it is now. Clear the element list
687 since it was delayed until needed. */
688 if (bitmap_set_bit (ptr->live_base_var, root))
689 bitmap_clear (ptr->live_base_partitions[root]);
690 bitmap_set_bit (ptr->live_base_partitions[root], partition);
695 /* Clear the live bit for VAR in PTR. */
697 static inline void
698 live_track_clear_var (live_track_p ptr, tree var)
700 int p;
702 p = var_to_partition (ptr->map, var);
703 if (p != NO_PARTITION)
704 live_track_remove_partition (ptr, p);
708 /* Return TRUE if VAR is live in PTR. */
710 static inline bool
711 live_track_live_p (live_track_p ptr, tree var)
713 int p, root;
715 p = var_to_partition (ptr->map, var);
716 if (p != NO_PARTITION)
718 root = basevar_index (ptr->map, p);
719 if (bitmap_bit_p (ptr->live_base_var, root))
720 return bitmap_bit_p (ptr->live_base_partitions[root], p);
722 return false;
726 /* This routine will add USE to PTR. USE will be marked as live in both the
727 ssa live map and the live bitmap for the root of USE. */
729 static inline void
730 live_track_process_use (live_track_p ptr, tree use)
732 int p;
734 p = var_to_partition (ptr->map, use);
735 if (p == NO_PARTITION)
736 return;
738 /* Mark as live in the appropriate live list. */
739 live_track_add_partition (ptr, p);
743 /* This routine will process a DEF in PTR. DEF will be removed from the live
744 lists, and if there are any other live partitions with the same base
745 variable, conflicts will be added to GRAPH. */
747 static inline void
748 live_track_process_def (live_track_p ptr, tree def, ssa_conflicts_p graph)
750 int p, root;
751 bitmap b;
752 unsigned x;
753 bitmap_iterator bi;
755 p = var_to_partition (ptr->map, def);
756 if (p == NO_PARTITION)
757 return;
759 /* Clear the liveness bit. */
760 live_track_remove_partition (ptr, p);
762 /* If the bitmap isn't empty now, conflicts need to be added. */
763 root = basevar_index (ptr->map, p);
764 if (bitmap_bit_p (ptr->live_base_var, root))
766 b = ptr->live_base_partitions[root];
767 EXECUTE_IF_SET_IN_BITMAP (b, 0, x, bi)
768 ssa_conflicts_add (graph, p, x);
773 /* Initialize PTR with the partitions set in INIT. */
775 static inline void
776 live_track_init (live_track_p ptr, bitmap init)
778 unsigned p;
779 bitmap_iterator bi;
781 /* Mark all live on exit partitions. */
782 EXECUTE_IF_SET_IN_BITMAP (init, 0, p, bi)
783 live_track_add_partition (ptr, p);
787 /* This routine will clear all live partitions in PTR. */
789 static inline void
790 live_track_clear_base_vars (live_track_p ptr)
792 /* Simply clear the live base list. Anything marked as live in the element
793 lists will be cleared later if/when the base variable ever comes alive
794 again. */
795 bitmap_clear (ptr->live_base_var);
799 /* Build a conflict graph based on LIVEINFO. Any partitions which are in the
800 partition view of the var_map liveinfo is based on get entries in the
801 conflict graph. Only conflicts between ssa_name partitions with the same
802 base variable are added. */
804 static ssa_conflicts_p
805 build_ssa_conflict_graph (tree_live_info_p liveinfo)
807 ssa_conflicts_p graph;
808 var_map map;
809 basic_block bb;
810 ssa_op_iter iter;
811 live_track_p live;
813 map = live_var_map (liveinfo);
814 graph = ssa_conflicts_new (num_var_partitions (map));
816 live = new_live_track (map);
818 FOR_EACH_BB (bb)
820 gimple_stmt_iterator gsi;
822 /* Start with live on exit temporaries. */
823 live_track_init (live, live_on_exit (liveinfo, bb));
825 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
827 tree var;
828 gimple stmt = gsi_stmt (gsi);
830 /* A copy between 2 partitions does not introduce an interference
831 by itself. If they did, you would never be able to coalesce
832 two things which are copied. If the two variables really do
833 conflict, they will conflict elsewhere in the program.
835 This is handled by simply removing the SRC of the copy from the
836 live list, and processing the stmt normally. */
837 if (is_gimple_assign (stmt))
839 tree lhs = gimple_assign_lhs (stmt);
840 tree rhs1 = gimple_assign_rhs1 (stmt);
841 if (gimple_assign_copy_p (stmt)
842 && TREE_CODE (lhs) == SSA_NAME
843 && TREE_CODE (rhs1) == SSA_NAME)
844 live_track_clear_var (live, rhs1);
846 else if (is_gimple_debug (stmt))
847 continue;
849 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
850 live_track_process_def (live, var, graph);
852 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
853 live_track_process_use (live, var);
856 /* If result of a PHI is unused, looping over the statements will not
857 record any conflicts since the def was never live. Since the PHI node
858 is going to be translated out of SSA form, it will insert a copy.
859 There must be a conflict recorded between the result of the PHI and
860 any variables that are live. Otherwise the out-of-ssa translation
861 may create incorrect code. */
862 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
864 gimple phi = gsi_stmt (gsi);
865 tree result = PHI_RESULT (phi);
866 if (live_track_live_p (live, result))
867 live_track_process_def (live, result, graph);
870 live_track_clear_base_vars (live);
873 delete_live_track (live);
874 return graph;
878 /* Shortcut routine to print messages to file F of the form:
879 "STR1 EXPR1 STR2 EXPR2 STR3." */
881 static inline void
882 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
883 tree expr2, const char *str3)
885 fprintf (f, "%s", str1);
886 print_generic_expr (f, expr1, TDF_SLIM);
887 fprintf (f, "%s", str2);
888 print_generic_expr (f, expr2, TDF_SLIM);
889 fprintf (f, "%s", str3);
893 /* Print a failure to coalesce a MUST_COALESCE pair X and Y. */
895 static inline void
896 fail_abnormal_edge_coalesce (int x, int y)
898 fprintf (stderr, "\nUnable to coalesce ssa_names %d and %d",x, y);
899 fprintf (stderr, " which are marked as MUST COALESCE.\n");
900 print_generic_expr (stderr, ssa_name (x), TDF_SLIM);
901 fprintf (stderr, " and ");
902 print_generic_stmt (stderr, ssa_name (y), TDF_SLIM);
904 internal_error ("SSA corruption");
908 /* This function creates a var_map for the current function as well as creating
909 a coalesce list for use later in the out of ssa process. */
911 static var_map
912 create_outofssa_var_map (coalesce_list_p cl, bitmap used_in_copy)
914 gimple_stmt_iterator gsi;
915 basic_block bb;
916 tree var;
917 gimple stmt;
918 tree first;
919 var_map map;
920 ssa_op_iter iter;
921 int v1, v2, cost;
922 unsigned i;
924 map = init_var_map (num_ssa_names);
926 FOR_EACH_BB (bb)
928 tree arg;
930 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
932 gimple phi = gsi_stmt (gsi);
933 size_t i;
934 int ver;
935 tree res;
936 bool saw_copy = false;
938 res = gimple_phi_result (phi);
939 ver = SSA_NAME_VERSION (res);
940 register_ssa_partition (map, res);
942 /* Register ssa_names and coalesces between the args and the result
943 of all PHI. */
944 for (i = 0; i < gimple_phi_num_args (phi); i++)
946 edge e = gimple_phi_arg_edge (phi, i);
947 arg = PHI_ARG_DEF (phi, i);
948 if (TREE_CODE (arg) != SSA_NAME)
949 continue;
951 register_ssa_partition (map, arg);
952 if (gimple_can_coalesce_p (arg, res)
953 || (e->flags & EDGE_ABNORMAL))
955 saw_copy = true;
956 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (arg));
957 if ((e->flags & EDGE_ABNORMAL) == 0)
959 int cost = coalesce_cost_edge (e);
960 if (cost == 1 && has_single_use (arg))
961 add_cost_one_coalesce (cl, ver, SSA_NAME_VERSION (arg));
962 else
963 add_coalesce (cl, ver, SSA_NAME_VERSION (arg), cost);
967 if (saw_copy)
968 bitmap_set_bit (used_in_copy, ver);
971 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
973 stmt = gsi_stmt (gsi);
975 if (is_gimple_debug (stmt))
976 continue;
978 /* Register USE and DEF operands in each statement. */
979 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
980 register_ssa_partition (map, var);
982 /* Check for copy coalesces. */
983 switch (gimple_code (stmt))
985 case GIMPLE_ASSIGN:
987 tree lhs = gimple_assign_lhs (stmt);
988 tree rhs1 = gimple_assign_rhs1 (stmt);
989 if (gimple_assign_ssa_name_copy_p (stmt)
990 && gimple_can_coalesce_p (lhs, rhs1))
992 v1 = SSA_NAME_VERSION (lhs);
993 v2 = SSA_NAME_VERSION (rhs1);
994 cost = coalesce_cost_bb (bb);
995 add_coalesce (cl, v1, v2, cost);
996 bitmap_set_bit (used_in_copy, v1);
997 bitmap_set_bit (used_in_copy, v2);
1000 break;
1002 case GIMPLE_ASM:
1004 unsigned long noutputs, i;
1005 unsigned long ninputs;
1006 tree *outputs, link;
1007 noutputs = gimple_asm_noutputs (stmt);
1008 ninputs = gimple_asm_ninputs (stmt);
1009 outputs = (tree *) alloca (noutputs * sizeof (tree));
1010 for (i = 0; i < noutputs; ++i)
1012 link = gimple_asm_output_op (stmt, i);
1013 outputs[i] = TREE_VALUE (link);
1016 for (i = 0; i < ninputs; ++i)
1018 const char *constraint;
1019 tree input;
1020 char *end;
1021 unsigned long match;
1023 link = gimple_asm_input_op (stmt, i);
1024 constraint
1025 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1026 input = TREE_VALUE (link);
1028 if (TREE_CODE (input) != SSA_NAME)
1029 continue;
1031 match = strtoul (constraint, &end, 10);
1032 if (match >= noutputs || end == constraint)
1033 continue;
1035 if (TREE_CODE (outputs[match]) != SSA_NAME)
1036 continue;
1038 v1 = SSA_NAME_VERSION (outputs[match]);
1039 v2 = SSA_NAME_VERSION (input);
1041 if (gimple_can_coalesce_p (outputs[match], input))
1043 cost = coalesce_cost (REG_BR_PROB_BASE,
1044 optimize_bb_for_size_p (bb));
1045 add_coalesce (cl, v1, v2, cost);
1046 bitmap_set_bit (used_in_copy, v1);
1047 bitmap_set_bit (used_in_copy, v2);
1050 break;
1053 default:
1054 break;
1059 /* Now process result decls and live on entry variables for entry into
1060 the coalesce list. */
1061 first = NULL_TREE;
1062 for (i = 1; i < num_ssa_names; i++)
1064 var = ssa_name (i);
1065 if (var != NULL_TREE && !virtual_operand_p (var))
1067 /* Add coalesces between all the result decls. */
1068 if (SSA_NAME_VAR (var)
1069 && TREE_CODE (SSA_NAME_VAR (var)) == RESULT_DECL)
1071 if (first == NULL_TREE)
1072 first = var;
1073 else
1075 gcc_assert (gimple_can_coalesce_p (var, first));
1076 v1 = SSA_NAME_VERSION (first);
1077 v2 = SSA_NAME_VERSION (var);
1078 bitmap_set_bit (used_in_copy, v1);
1079 bitmap_set_bit (used_in_copy, v2);
1080 cost = coalesce_cost_bb (EXIT_BLOCK_PTR);
1081 add_coalesce (cl, v1, v2, cost);
1084 /* Mark any default_def variables as being in the coalesce list
1085 since they will have to be coalesced with the base variable. If
1086 not marked as present, they won't be in the coalesce view. */
1087 if (SSA_NAME_IS_DEFAULT_DEF (var)
1088 && !has_zero_uses (var))
1089 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1093 return map;
1097 /* Attempt to coalesce ssa versions X and Y together using the partition
1098 mapping in MAP and checking conflicts in GRAPH. Output any debug info to
1099 DEBUG, if it is nun-NULL. */
1101 static inline bool
1102 attempt_coalesce (var_map map, ssa_conflicts_p graph, int x, int y,
1103 FILE *debug)
1105 int z;
1106 tree var1, var2;
1107 int p1, p2;
1109 p1 = var_to_partition (map, ssa_name (x));
1110 p2 = var_to_partition (map, ssa_name (y));
1112 if (debug)
1114 fprintf (debug, "(%d)", x);
1115 print_generic_expr (debug, partition_to_var (map, p1), TDF_SLIM);
1116 fprintf (debug, " & (%d)", y);
1117 print_generic_expr (debug, partition_to_var (map, p2), TDF_SLIM);
1120 if (p1 == p2)
1122 if (debug)
1123 fprintf (debug, ": Already Coalesced.\n");
1124 return true;
1127 if (debug)
1128 fprintf (debug, " [map: %d, %d] ", p1, p2);
1131 if (!ssa_conflicts_test_p (graph, p1, p2))
1133 var1 = partition_to_var (map, p1);
1134 var2 = partition_to_var (map, p2);
1135 z = var_union (map, var1, var2);
1136 if (z == NO_PARTITION)
1138 if (debug)
1139 fprintf (debug, ": Unable to perform partition union.\n");
1140 return false;
1143 /* z is the new combined partition. Remove the other partition from
1144 the list, and merge the conflicts. */
1145 if (z == p1)
1146 ssa_conflicts_merge (graph, p1, p2);
1147 else
1148 ssa_conflicts_merge (graph, p2, p1);
1150 if (debug)
1151 fprintf (debug, ": Success -> %d\n", z);
1152 return true;
1155 if (debug)
1156 fprintf (debug, ": Fail due to conflict\n");
1158 return false;
1162 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1163 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1165 static void
1166 coalesce_partitions (var_map map, ssa_conflicts_p graph, coalesce_list_p cl,
1167 FILE *debug)
1169 int x = 0, y = 0;
1170 tree var1, var2;
1171 int cost;
1172 basic_block bb;
1173 edge e;
1174 edge_iterator ei;
1176 /* First, coalesce all the copies across abnormal edges. These are not placed
1177 in the coalesce list because they do not need to be sorted, and simply
1178 consume extra memory/compilation time in large programs. */
1180 FOR_EACH_BB (bb)
1182 FOR_EACH_EDGE (e, ei, bb->preds)
1183 if (e->flags & EDGE_ABNORMAL)
1185 gimple_stmt_iterator gsi;
1186 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1187 gsi_next (&gsi))
1189 gimple phi = gsi_stmt (gsi);
1190 tree res = PHI_RESULT (phi);
1191 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1192 int v1 = SSA_NAME_VERSION (res);
1193 int v2 = SSA_NAME_VERSION (arg);
1195 if (debug)
1196 fprintf (debug, "Abnormal coalesce: ");
1198 if (!attempt_coalesce (map, graph, v1, v2, debug))
1199 fail_abnormal_edge_coalesce (v1, v2);
1204 /* Now process the items in the coalesce list. */
1206 while ((cost = pop_best_coalesce (cl, &x, &y)) != NO_BEST_COALESCE)
1208 var1 = ssa_name (x);
1209 var2 = ssa_name (y);
1211 /* Assert the coalesces have the same base variable. */
1212 gcc_assert (gimple_can_coalesce_p (var1, var2));
1214 if (debug)
1215 fprintf (debug, "Coalesce list: ");
1216 attempt_coalesce (map, graph, x, y, debug);
1221 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1223 struct ssa_name_var_hash : typed_noop_remove <tree_node>
1225 typedef union tree_node value_type;
1226 typedef union tree_node compare_type;
1227 static inline hashval_t hash (const value_type *);
1228 static inline int equal (const value_type *, const compare_type *);
1231 inline hashval_t
1232 ssa_name_var_hash::hash (const_tree n)
1234 return DECL_UID (SSA_NAME_VAR (n));
1237 inline int
1238 ssa_name_var_hash::equal (const value_type *n1, const compare_type *n2)
1240 return SSA_NAME_VAR (n1) == SSA_NAME_VAR (n2);
1244 /* Reduce the number of copies by coalescing variables in the function. Return
1245 a partition map with the resulting coalesces. */
1247 extern var_map
1248 coalesce_ssa_name (void)
1250 tree_live_info_p liveinfo;
1251 ssa_conflicts_p graph;
1252 coalesce_list_p cl;
1253 bitmap used_in_copies = BITMAP_ALLOC (NULL);
1254 var_map map;
1255 unsigned int i;
1257 cl = create_coalesce_list ();
1258 map = create_outofssa_var_map (cl, used_in_copies);
1260 /* If optimization is disabled, we need to coalesce all the names originating
1261 from the same SSA_NAME_VAR so debug info remains undisturbed. */
1262 if (!optimize)
1264 hash_table <ssa_name_var_hash> ssa_name_hash;
1266 ssa_name_hash.create (10);
1267 for (i = 1; i < num_ssa_names; i++)
1269 tree a = ssa_name (i);
1271 if (a
1272 && SSA_NAME_VAR (a)
1273 && !DECL_IGNORED_P (SSA_NAME_VAR (a))
1274 && (!has_zero_uses (a) || !SSA_NAME_IS_DEFAULT_DEF (a)))
1276 tree *slot = ssa_name_hash.find_slot (a, INSERT);
1278 if (!*slot)
1279 *slot = a;
1280 else
1282 /* If the variable is a PARM_DECL or a RESULT_DECL, we
1283 _require_ that all the names originating from it be
1284 coalesced, because there must be a single partition
1285 containing all the names so that it can be assigned
1286 the canonical RTL location of the DECL safely. */
1287 const int cost
1288 = TREE_CODE (SSA_NAME_VAR (a)) == VAR_DECL
1289 ? MUST_COALESCE_COST - 1 : MUST_COALESCE_COST;
1290 add_coalesce (cl, SSA_NAME_VERSION (a),
1291 SSA_NAME_VERSION (*slot), cost);
1292 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (a));
1293 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (*slot));
1297 ssa_name_hash.dispose ();
1299 if (dump_file && (dump_flags & TDF_DETAILS))
1300 dump_var_map (dump_file, map);
1302 /* Don't calculate live ranges for variables not in the coalesce list. */
1303 partition_view_bitmap (map, used_in_copies, true);
1304 BITMAP_FREE (used_in_copies);
1306 if (num_var_partitions (map) < 1)
1308 delete_coalesce_list (cl);
1309 return map;
1312 if (dump_file && (dump_flags & TDF_DETAILS))
1313 dump_var_map (dump_file, map);
1315 liveinfo = calculate_live_ranges (map);
1317 if (dump_file && (dump_flags & TDF_DETAILS))
1318 dump_live_info (dump_file, liveinfo, LIVEDUMP_ENTRY);
1320 /* Build a conflict graph. */
1321 graph = build_ssa_conflict_graph (liveinfo);
1322 delete_tree_live_info (liveinfo);
1323 if (dump_file && (dump_flags & TDF_DETAILS))
1324 ssa_conflicts_dump (dump_file, graph);
1326 sort_coalesce_list (cl);
1328 if (dump_file && (dump_flags & TDF_DETAILS))
1330 fprintf (dump_file, "\nAfter sorting:\n");
1331 dump_coalesce_list (dump_file, cl);
1334 /* First, coalesce all live on entry variables to their base variable.
1335 This will ensure the first use is coming from the correct location. */
1337 if (dump_file && (dump_flags & TDF_DETAILS))
1338 dump_var_map (dump_file, map);
1340 /* Now coalesce everything in the list. */
1341 coalesce_partitions (map, graph, cl,
1342 ((dump_flags & TDF_DETAILS) ? dump_file
1343 : NULL));
1345 delete_coalesce_list (cl);
1346 ssa_conflicts_delete (graph);
1348 return map;