* df-scan.c (df_collection_rec): Adjust.
[official-gcc.git] / gcc / tree-ssa-coalesce.c
blobc92c7c5f4d539594ffe6df7a67d0e17faec6c4f8
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-ssa.h"
32 #include "tree-phinodes.h"
33 #include "ssa-iterators.h"
34 #include "tree-ssanames.h"
35 #include "hash-table.h"
36 #include "tree-outof-ssa.h"
37 #include "diagnostic-core.h"
40 /* This set of routines implements a coalesce_list. This is an object which
41 is used to track pairs of ssa_names which are desirable to coalesce
42 together to avoid copies. Costs are associated with each pair, and when
43 all desired information has been collected, the object can be used to
44 order the pairs for processing. */
46 /* This structure defines a pair entry. */
48 typedef struct coalesce_pair
50 int first_element;
51 int second_element;
52 int cost;
53 } * coalesce_pair_p;
54 typedef const struct coalesce_pair *const_coalesce_pair_p;
56 /* Coalesce pair hashtable helpers. */
58 struct coalesce_pair_hasher : typed_noop_remove <coalesce_pair>
60 typedef coalesce_pair value_type;
61 typedef coalesce_pair compare_type;
62 static inline hashval_t hash (const value_type *);
63 static inline bool equal (const value_type *, const compare_type *);
66 /* Hash function for coalesce list. Calculate hash for PAIR. */
68 inline hashval_t
69 coalesce_pair_hasher::hash (const value_type *pair)
71 hashval_t a = (hashval_t)(pair->first_element);
72 hashval_t b = (hashval_t)(pair->second_element);
74 return b * (b - 1) / 2 + a;
77 /* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
78 returning TRUE if the two pairs are equivalent. */
80 inline bool
81 coalesce_pair_hasher::equal (const value_type *p1, const compare_type *p2)
83 return (p1->first_element == p2->first_element
84 && p1->second_element == p2->second_element);
87 typedef hash_table <coalesce_pair_hasher> coalesce_table_type;
88 typedef coalesce_table_type::iterator coalesce_iterator_type;
91 typedef struct cost_one_pair_d
93 int first_element;
94 int second_element;
95 struct cost_one_pair_d *next;
96 } * cost_one_pair_p;
98 /* This structure maintains the list of coalesce pairs. */
100 typedef struct coalesce_list_d
102 coalesce_table_type list; /* Hash table. */
103 coalesce_pair_p *sorted; /* List when sorted. */
104 int num_sorted; /* Number in the sorted list. */
105 cost_one_pair_p cost_one_list;/* Single use coalesces with cost 1. */
106 } *coalesce_list_p;
108 #define NO_BEST_COALESCE -1
109 #define MUST_COALESCE_COST INT_MAX
112 /* Return cost of execution of copy instruction with FREQUENCY. */
114 static inline int
115 coalesce_cost (int frequency, bool optimize_for_size)
117 /* Base costs on BB frequencies bounded by 1. */
118 int cost = frequency;
120 if (!cost)
121 cost = 1;
123 if (optimize_for_size)
124 cost = 1;
126 return cost;
130 /* Return the cost of executing a copy instruction in basic block BB. */
132 static inline int
133 coalesce_cost_bb (basic_block bb)
135 return coalesce_cost (bb->frequency, optimize_bb_for_size_p (bb));
139 /* Return the cost of executing a copy instruction on edge E. */
141 static inline int
142 coalesce_cost_edge (edge e)
144 int mult = 1;
146 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
147 if (EDGE_CRITICAL_P (e))
148 mult = 2;
149 if (e->flags & EDGE_ABNORMAL)
150 return MUST_COALESCE_COST;
151 if (e->flags & EDGE_EH)
153 edge e2;
154 edge_iterator ei;
155 FOR_EACH_EDGE (e2, ei, e->dest->preds)
156 if (e2 != e)
158 /* Putting code on EH edge that leads to BB
159 with multiple predecestors imply splitting of
160 edge too. */
161 if (mult < 2)
162 mult = 2;
163 /* If there are multiple EH predecestors, we
164 also copy EH regions and produce separate
165 landing pad. This is expensive. */
166 if (e2->flags & EDGE_EH)
168 mult = 5;
169 break;
174 return coalesce_cost (EDGE_FREQUENCY (e),
175 optimize_edge_for_size_p (e)) * mult;
179 /* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
180 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
181 NO_BEST_COALESCE is returned if there aren't any. */
183 static inline int
184 pop_cost_one_pair (coalesce_list_p cl, int *p1, int *p2)
186 cost_one_pair_p ptr;
188 ptr = cl->cost_one_list;
189 if (!ptr)
190 return NO_BEST_COALESCE;
192 *p1 = ptr->first_element;
193 *p2 = ptr->second_element;
194 cl->cost_one_list = ptr->next;
196 free (ptr);
198 return 1;
201 /* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
202 2 elements via P1 and P2. Their calculated cost is returned by the function.
203 NO_BEST_COALESCE is returned if the coalesce list is empty. */
205 static inline int
206 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
208 coalesce_pair_p node;
209 int ret;
211 if (cl->sorted == NULL)
212 return pop_cost_one_pair (cl, p1, p2);
214 if (cl->num_sorted == 0)
215 return pop_cost_one_pair (cl, p1, p2);
217 node = cl->sorted[--(cl->num_sorted)];
218 *p1 = node->first_element;
219 *p2 = node->second_element;
220 ret = node->cost;
221 free (node);
223 return ret;
227 /* Create a new empty coalesce list object and return it. */
229 static inline coalesce_list_p
230 create_coalesce_list (void)
232 coalesce_list_p list;
233 unsigned size = num_ssa_names * 3;
235 if (size < 40)
236 size = 40;
238 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
239 list->list.create (size);
240 list->sorted = NULL;
241 list->num_sorted = 0;
242 list->cost_one_list = NULL;
243 return list;
247 /* Delete coalesce list CL. */
249 static inline void
250 delete_coalesce_list (coalesce_list_p cl)
252 gcc_assert (cl->cost_one_list == NULL);
253 cl->list.dispose ();
254 free (cl->sorted);
255 gcc_assert (cl->num_sorted == 0);
256 free (cl);
260 /* Find a matching coalesce pair object in CL for the pair P1 and P2. If
261 one isn't found, return NULL if CREATE is false, otherwise create a new
262 coalesce pair object and return it. */
264 static coalesce_pair_p
265 find_coalesce_pair (coalesce_list_p cl, int p1, int p2, bool create)
267 struct coalesce_pair p;
268 coalesce_pair **slot;
269 unsigned int hash;
271 /* Normalize so that p1 is the smaller value. */
272 if (p2 < p1)
274 p.first_element = p2;
275 p.second_element = p1;
277 else
279 p.first_element = p1;
280 p.second_element = p2;
283 hash = coalesce_pair_hasher::hash (&p);
284 slot = cl->list.find_slot_with_hash (&p, hash, create ? INSERT : NO_INSERT);
285 if (!slot)
286 return NULL;
288 if (!*slot)
290 struct coalesce_pair * pair = XNEW (struct coalesce_pair);
291 gcc_assert (cl->sorted == NULL);
292 pair->first_element = p.first_element;
293 pair->second_element = p.second_element;
294 pair->cost = 0;
295 *slot = pair;
298 return (struct coalesce_pair *) *slot;
301 static inline void
302 add_cost_one_coalesce (coalesce_list_p cl, int p1, int p2)
304 cost_one_pair_p pair;
306 pair = XNEW (struct cost_one_pair_d);
307 pair->first_element = p1;
308 pair->second_element = p2;
309 pair->next = cl->cost_one_list;
310 cl->cost_one_list = pair;
314 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
316 static inline void
317 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
319 coalesce_pair_p node;
321 gcc_assert (cl->sorted == NULL);
322 if (p1 == p2)
323 return;
325 node = find_coalesce_pair (cl, p1, p2, true);
327 /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way. */
328 if (node->cost < MUST_COALESCE_COST - 1)
330 if (value < MUST_COALESCE_COST - 1)
331 node->cost += value;
332 else
333 node->cost = value;
338 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
340 static int
341 compare_pairs (const void *p1, const void *p2)
343 const_coalesce_pair_p const *const pp1 = (const_coalesce_pair_p const *) p1;
344 const_coalesce_pair_p const *const pp2 = (const_coalesce_pair_p const *) p2;
345 int result;
347 result = (* pp1)->cost - (* pp2)->cost;
348 /* Since qsort does not guarantee stability we use the elements
349 as a secondary key. This provides us with independence from
350 the host's implementation of the sorting algorithm. */
351 if (result == 0)
353 result = (* pp2)->first_element - (* pp1)->first_element;
354 if (result == 0)
355 result = (* pp2)->second_element - (* pp1)->second_element;
358 return result;
362 /* Return the number of unique coalesce pairs in CL. */
364 static inline int
365 num_coalesce_pairs (coalesce_list_p cl)
367 return cl->list.elements ();
371 /* Iterate over CL using ITER, returning values in PAIR. */
373 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
374 FOR_EACH_HASH_TABLE_ELEMENT ((CL)->list, (PAIR), coalesce_pair_p, (ITER))
377 /* Prepare CL for removal of preferred pairs. When finished they are sorted
378 in order from most important coalesce to least important. */
380 static void
381 sort_coalesce_list (coalesce_list_p cl)
383 unsigned x, num;
384 coalesce_pair_p p;
385 coalesce_iterator_type ppi;
387 gcc_assert (cl->sorted == NULL);
389 num = num_coalesce_pairs (cl);
390 cl->num_sorted = num;
391 if (num == 0)
392 return;
394 /* Allocate a vector for the pair pointers. */
395 cl->sorted = XNEWVEC (coalesce_pair_p, num);
397 /* Populate the vector with pointers to the pairs. */
398 x = 0;
399 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
400 cl->sorted[x++] = p;
401 gcc_assert (x == num);
403 /* Already sorted. */
404 if (num == 1)
405 return;
407 /* If there are only 2, just pick swap them if the order isn't correct. */
408 if (num == 2)
410 if (cl->sorted[0]->cost > cl->sorted[1]->cost)
412 p = cl->sorted[0];
413 cl->sorted[0] = cl->sorted[1];
414 cl->sorted[1] = p;
416 return;
419 /* Only call qsort if there are more than 2 items. */
420 if (num > 2)
421 qsort (cl->sorted, num, sizeof (coalesce_pair_p), compare_pairs);
425 /* Send debug info for coalesce list CL to file F. */
427 static void
428 dump_coalesce_list (FILE *f, coalesce_list_p cl)
430 coalesce_pair_p node;
431 coalesce_iterator_type ppi;
433 int x;
434 tree var;
436 if (cl->sorted == NULL)
438 fprintf (f, "Coalesce List:\n");
439 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
441 tree var1 = ssa_name (node->first_element);
442 tree var2 = ssa_name (node->second_element);
443 print_generic_expr (f, var1, TDF_SLIM);
444 fprintf (f, " <-> ");
445 print_generic_expr (f, var2, TDF_SLIM);
446 fprintf (f, " (%1d), ", node->cost);
447 fprintf (f, "\n");
450 else
452 fprintf (f, "Sorted Coalesce list:\n");
453 for (x = cl->num_sorted - 1 ; x >=0; x--)
455 node = cl->sorted[x];
456 fprintf (f, "(%d) ", node->cost);
457 var = ssa_name (node->first_element);
458 print_generic_expr (f, var, TDF_SLIM);
459 fprintf (f, " <-> ");
460 var = ssa_name (node->second_element);
461 print_generic_expr (f, var, TDF_SLIM);
462 fprintf (f, "\n");
468 /* This represents a conflict graph. Implemented as an array of bitmaps.
469 A full matrix is used for conflicts rather than just upper triangular form.
470 this make sit much simpler and faster to perform conflict merges. */
472 typedef struct ssa_conflicts_d
474 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
475 vec<bitmap> conflicts;
476 } * ssa_conflicts_p;
478 /* Return an empty new conflict graph for SIZE elements. */
480 static inline ssa_conflicts_p
481 ssa_conflicts_new (unsigned size)
483 ssa_conflicts_p ptr;
485 ptr = XNEW (struct ssa_conflicts_d);
486 bitmap_obstack_initialize (&ptr->obstack);
487 ptr->conflicts.create (size);
488 ptr->conflicts.safe_grow_cleared (size);
489 return ptr;
493 /* Free storage for conflict graph PTR. */
495 static inline void
496 ssa_conflicts_delete (ssa_conflicts_p ptr)
498 bitmap_obstack_release (&ptr->obstack);
499 ptr->conflicts.release ();
500 free (ptr);
504 /* Test if elements X and Y conflict in graph PTR. */
506 static inline bool
507 ssa_conflicts_test_p (ssa_conflicts_p ptr, unsigned x, unsigned y)
509 bitmap bx = ptr->conflicts[x];
510 bitmap by = ptr->conflicts[y];
512 gcc_checking_assert (x != y);
514 if (bx)
515 /* Avoid the lookup if Y has no conflicts. */
516 return by ? bitmap_bit_p (bx, y) : false;
517 else
518 return false;
522 /* Add a conflict with Y to the bitmap for X in graph PTR. */
524 static inline void
525 ssa_conflicts_add_one (ssa_conflicts_p ptr, unsigned x, unsigned y)
527 bitmap bx = ptr->conflicts[x];
528 /* If there are no conflicts yet, allocate the bitmap and set bit. */
529 if (! bx)
530 bx = ptr->conflicts[x] = BITMAP_ALLOC (&ptr->obstack);
531 bitmap_set_bit (bx, y);
535 /* Add conflicts between X and Y in graph PTR. */
537 static inline void
538 ssa_conflicts_add (ssa_conflicts_p ptr, unsigned x, unsigned y)
540 gcc_checking_assert (x != y);
541 ssa_conflicts_add_one (ptr, x, y);
542 ssa_conflicts_add_one (ptr, y, x);
546 /* Merge all Y's conflict into X in graph PTR. */
548 static inline void
549 ssa_conflicts_merge (ssa_conflicts_p ptr, unsigned x, unsigned y)
551 unsigned z;
552 bitmap_iterator bi;
553 bitmap bx = ptr->conflicts[x];
554 bitmap by = ptr->conflicts[y];
556 gcc_checking_assert (x != y);
557 if (! by)
558 return;
560 /* Add a conflict between X and every one Y has. If the bitmap doesn't
561 exist, then it has already been coalesced, and we don't need to add a
562 conflict. */
563 EXECUTE_IF_SET_IN_BITMAP (by, 0, z, bi)
565 bitmap bz = ptr->conflicts[z];
566 if (bz)
567 bitmap_set_bit (bz, x);
570 if (bx)
572 /* If X has conflicts, add Y's to X. */
573 bitmap_ior_into (bx, by);
574 BITMAP_FREE (by);
575 ptr->conflicts[y] = NULL;
577 else
579 /* If X has no conflicts, simply use Y's. */
580 ptr->conflicts[x] = by;
581 ptr->conflicts[y] = NULL;
586 /* Dump a conflicts graph. */
588 static void
589 ssa_conflicts_dump (FILE *file, ssa_conflicts_p ptr)
591 unsigned x;
592 bitmap b;
594 fprintf (file, "\nConflict graph:\n");
596 FOR_EACH_VEC_ELT (ptr->conflicts, x, b)
597 if (b)
599 fprintf (file, "%d: ", x);
600 dump_bitmap (file, b);
605 /* This structure is used to efficiently record the current status of live
606 SSA_NAMES when building a conflict graph.
607 LIVE_BASE_VAR has a bit set for each base variable which has at least one
608 ssa version live.
609 LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
610 index, and is used to track what partitions of each base variable are
611 live. This makes it easy to add conflicts between just live partitions
612 with the same base variable.
613 The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
614 marked as being live. This delays clearing of these bitmaps until
615 they are actually needed again. */
617 typedef struct live_track_d
619 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
620 bitmap live_base_var; /* Indicates if a basevar is live. */
621 bitmap *live_base_partitions; /* Live partitions for each basevar. */
622 var_map map; /* Var_map being used for partition mapping. */
623 } * live_track_p;
626 /* This routine will create a new live track structure based on the partitions
627 in MAP. */
629 static live_track_p
630 new_live_track (var_map map)
632 live_track_p ptr;
633 int lim, x;
635 /* Make sure there is a partition view in place. */
636 gcc_assert (map->partition_to_base_index != NULL);
638 ptr = (live_track_p) xmalloc (sizeof (struct live_track_d));
639 ptr->map = map;
640 lim = num_basevars (map);
641 bitmap_obstack_initialize (&ptr->obstack);
642 ptr->live_base_partitions = (bitmap *) xmalloc (sizeof (bitmap *) * lim);
643 ptr->live_base_var = BITMAP_ALLOC (&ptr->obstack);
644 for (x = 0; x < lim; x++)
645 ptr->live_base_partitions[x] = BITMAP_ALLOC (&ptr->obstack);
646 return ptr;
650 /* This routine will free the memory associated with PTR. */
652 static void
653 delete_live_track (live_track_p ptr)
655 bitmap_obstack_release (&ptr->obstack);
656 free (ptr->live_base_partitions);
657 free (ptr);
661 /* This function will remove PARTITION from the live list in PTR. */
663 static inline void
664 live_track_remove_partition (live_track_p ptr, int partition)
666 int root;
668 root = basevar_index (ptr->map, partition);
669 bitmap_clear_bit (ptr->live_base_partitions[root], partition);
670 /* If the element list is empty, make the base variable not live either. */
671 if (bitmap_empty_p (ptr->live_base_partitions[root]))
672 bitmap_clear_bit (ptr->live_base_var, root);
676 /* This function will adds PARTITION to the live list in PTR. */
678 static inline void
679 live_track_add_partition (live_track_p ptr, int partition)
681 int root;
683 root = basevar_index (ptr->map, partition);
684 /* If this base var wasn't live before, it is now. Clear the element list
685 since it was delayed until needed. */
686 if (bitmap_set_bit (ptr->live_base_var, root))
687 bitmap_clear (ptr->live_base_partitions[root]);
688 bitmap_set_bit (ptr->live_base_partitions[root], partition);
693 /* Clear the live bit for VAR in PTR. */
695 static inline void
696 live_track_clear_var (live_track_p ptr, tree var)
698 int p;
700 p = var_to_partition (ptr->map, var);
701 if (p != NO_PARTITION)
702 live_track_remove_partition (ptr, p);
706 /* Return TRUE if VAR is live in PTR. */
708 static inline bool
709 live_track_live_p (live_track_p ptr, tree var)
711 int p, root;
713 p = var_to_partition (ptr->map, var);
714 if (p != NO_PARTITION)
716 root = basevar_index (ptr->map, p);
717 if (bitmap_bit_p (ptr->live_base_var, root))
718 return bitmap_bit_p (ptr->live_base_partitions[root], p);
720 return false;
724 /* This routine will add USE to PTR. USE will be marked as live in both the
725 ssa live map and the live bitmap for the root of USE. */
727 static inline void
728 live_track_process_use (live_track_p ptr, tree use)
730 int p;
732 p = var_to_partition (ptr->map, use);
733 if (p == NO_PARTITION)
734 return;
736 /* Mark as live in the appropriate live list. */
737 live_track_add_partition (ptr, p);
741 /* This routine will process a DEF in PTR. DEF will be removed from the live
742 lists, and if there are any other live partitions with the same base
743 variable, conflicts will be added to GRAPH. */
745 static inline void
746 live_track_process_def (live_track_p ptr, tree def, ssa_conflicts_p graph)
748 int p, root;
749 bitmap b;
750 unsigned x;
751 bitmap_iterator bi;
753 p = var_to_partition (ptr->map, def);
754 if (p == NO_PARTITION)
755 return;
757 /* Clear the liveness bit. */
758 live_track_remove_partition (ptr, p);
760 /* If the bitmap isn't empty now, conflicts need to be added. */
761 root = basevar_index (ptr->map, p);
762 if (bitmap_bit_p (ptr->live_base_var, root))
764 b = ptr->live_base_partitions[root];
765 EXECUTE_IF_SET_IN_BITMAP (b, 0, x, bi)
766 ssa_conflicts_add (graph, p, x);
771 /* Initialize PTR with the partitions set in INIT. */
773 static inline void
774 live_track_init (live_track_p ptr, bitmap init)
776 unsigned p;
777 bitmap_iterator bi;
779 /* Mark all live on exit partitions. */
780 EXECUTE_IF_SET_IN_BITMAP (init, 0, p, bi)
781 live_track_add_partition (ptr, p);
785 /* This routine will clear all live partitions in PTR. */
787 static inline void
788 live_track_clear_base_vars (live_track_p ptr)
790 /* Simply clear the live base list. Anything marked as live in the element
791 lists will be cleared later if/when the base variable ever comes alive
792 again. */
793 bitmap_clear (ptr->live_base_var);
797 /* Build a conflict graph based on LIVEINFO. Any partitions which are in the
798 partition view of the var_map liveinfo is based on get entries in the
799 conflict graph. Only conflicts between ssa_name partitions with the same
800 base variable are added. */
802 static ssa_conflicts_p
803 build_ssa_conflict_graph (tree_live_info_p liveinfo)
805 ssa_conflicts_p graph;
806 var_map map;
807 basic_block bb;
808 ssa_op_iter iter;
809 live_track_p live;
811 map = live_var_map (liveinfo);
812 graph = ssa_conflicts_new (num_var_partitions (map));
814 live = new_live_track (map);
816 FOR_EACH_BB (bb)
818 gimple_stmt_iterator gsi;
820 /* Start with live on exit temporaries. */
821 live_track_init (live, live_on_exit (liveinfo, bb));
823 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
825 tree var;
826 gimple stmt = gsi_stmt (gsi);
828 /* A copy between 2 partitions does not introduce an interference
829 by itself. If they did, you would never be able to coalesce
830 two things which are copied. If the two variables really do
831 conflict, they will conflict elsewhere in the program.
833 This is handled by simply removing the SRC of the copy from the
834 live list, and processing the stmt normally. */
835 if (is_gimple_assign (stmt))
837 tree lhs = gimple_assign_lhs (stmt);
838 tree rhs1 = gimple_assign_rhs1 (stmt);
839 if (gimple_assign_copy_p (stmt)
840 && TREE_CODE (lhs) == SSA_NAME
841 && TREE_CODE (rhs1) == SSA_NAME)
842 live_track_clear_var (live, rhs1);
844 else if (is_gimple_debug (stmt))
845 continue;
847 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
848 live_track_process_def (live, var, graph);
850 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
851 live_track_process_use (live, var);
854 /* If result of a PHI is unused, looping over the statements will not
855 record any conflicts since the def was never live. Since the PHI node
856 is going to be translated out of SSA form, it will insert a copy.
857 There must be a conflict recorded between the result of the PHI and
858 any variables that are live. Otherwise the out-of-ssa translation
859 may create incorrect code. */
860 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
862 gimple phi = gsi_stmt (gsi);
863 tree result = PHI_RESULT (phi);
864 if (live_track_live_p (live, result))
865 live_track_process_def (live, result, graph);
868 live_track_clear_base_vars (live);
871 delete_live_track (live);
872 return graph;
876 /* Shortcut routine to print messages to file F of the form:
877 "STR1 EXPR1 STR2 EXPR2 STR3." */
879 static inline void
880 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
881 tree expr2, const char *str3)
883 fprintf (f, "%s", str1);
884 print_generic_expr (f, expr1, TDF_SLIM);
885 fprintf (f, "%s", str2);
886 print_generic_expr (f, expr2, TDF_SLIM);
887 fprintf (f, "%s", str3);
891 /* Print a failure to coalesce a MUST_COALESCE pair X and Y. */
893 static inline void
894 fail_abnormal_edge_coalesce (int x, int y)
896 fprintf (stderr, "\nUnable to coalesce ssa_names %d and %d",x, y);
897 fprintf (stderr, " which are marked as MUST COALESCE.\n");
898 print_generic_expr (stderr, ssa_name (x), TDF_SLIM);
899 fprintf (stderr, " and ");
900 print_generic_stmt (stderr, ssa_name (y), TDF_SLIM);
902 internal_error ("SSA corruption");
906 /* This function creates a var_map for the current function as well as creating
907 a coalesce list for use later in the out of ssa process. */
909 static var_map
910 create_outofssa_var_map (coalesce_list_p cl, bitmap used_in_copy)
912 gimple_stmt_iterator gsi;
913 basic_block bb;
914 tree var;
915 gimple stmt;
916 tree first;
917 var_map map;
918 ssa_op_iter iter;
919 int v1, v2, cost;
920 unsigned i;
922 map = init_var_map (num_ssa_names);
924 FOR_EACH_BB (bb)
926 tree arg;
928 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
930 gimple phi = gsi_stmt (gsi);
931 size_t i;
932 int ver;
933 tree res;
934 bool saw_copy = false;
936 res = gimple_phi_result (phi);
937 ver = SSA_NAME_VERSION (res);
938 register_ssa_partition (map, res);
940 /* Register ssa_names and coalesces between the args and the result
941 of all PHI. */
942 for (i = 0; i < gimple_phi_num_args (phi); i++)
944 edge e = gimple_phi_arg_edge (phi, i);
945 arg = PHI_ARG_DEF (phi, i);
946 if (TREE_CODE (arg) != SSA_NAME)
947 continue;
949 register_ssa_partition (map, arg);
950 if (gimple_can_coalesce_p (arg, res)
951 || (e->flags & EDGE_ABNORMAL))
953 saw_copy = true;
954 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (arg));
955 if ((e->flags & EDGE_ABNORMAL) == 0)
957 int cost = coalesce_cost_edge (e);
958 if (cost == 1 && has_single_use (arg))
959 add_cost_one_coalesce (cl, ver, SSA_NAME_VERSION (arg));
960 else
961 add_coalesce (cl, ver, SSA_NAME_VERSION (arg), cost);
965 if (saw_copy)
966 bitmap_set_bit (used_in_copy, ver);
969 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
971 stmt = gsi_stmt (gsi);
973 if (is_gimple_debug (stmt))
974 continue;
976 /* Register USE and DEF operands in each statement. */
977 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
978 register_ssa_partition (map, var);
980 /* Check for copy coalesces. */
981 switch (gimple_code (stmt))
983 case GIMPLE_ASSIGN:
985 tree lhs = gimple_assign_lhs (stmt);
986 tree rhs1 = gimple_assign_rhs1 (stmt);
987 if (gimple_assign_ssa_name_copy_p (stmt)
988 && gimple_can_coalesce_p (lhs, rhs1))
990 v1 = SSA_NAME_VERSION (lhs);
991 v2 = SSA_NAME_VERSION (rhs1);
992 cost = coalesce_cost_bb (bb);
993 add_coalesce (cl, v1, v2, cost);
994 bitmap_set_bit (used_in_copy, v1);
995 bitmap_set_bit (used_in_copy, v2);
998 break;
1000 case GIMPLE_ASM:
1002 unsigned long noutputs, i;
1003 unsigned long ninputs;
1004 tree *outputs, link;
1005 noutputs = gimple_asm_noutputs (stmt);
1006 ninputs = gimple_asm_ninputs (stmt);
1007 outputs = (tree *) alloca (noutputs * sizeof (tree));
1008 for (i = 0; i < noutputs; ++i)
1010 link = gimple_asm_output_op (stmt, i);
1011 outputs[i] = TREE_VALUE (link);
1014 for (i = 0; i < ninputs; ++i)
1016 const char *constraint;
1017 tree input;
1018 char *end;
1019 unsigned long match;
1021 link = gimple_asm_input_op (stmt, i);
1022 constraint
1023 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1024 input = TREE_VALUE (link);
1026 if (TREE_CODE (input) != SSA_NAME)
1027 continue;
1029 match = strtoul (constraint, &end, 10);
1030 if (match >= noutputs || end == constraint)
1031 continue;
1033 if (TREE_CODE (outputs[match]) != SSA_NAME)
1034 continue;
1036 v1 = SSA_NAME_VERSION (outputs[match]);
1037 v2 = SSA_NAME_VERSION (input);
1039 if (gimple_can_coalesce_p (outputs[match], input))
1041 cost = coalesce_cost (REG_BR_PROB_BASE,
1042 optimize_bb_for_size_p (bb));
1043 add_coalesce (cl, v1, v2, cost);
1044 bitmap_set_bit (used_in_copy, v1);
1045 bitmap_set_bit (used_in_copy, v2);
1048 break;
1051 default:
1052 break;
1057 /* Now process result decls and live on entry variables for entry into
1058 the coalesce list. */
1059 first = NULL_TREE;
1060 for (i = 1; i < num_ssa_names; i++)
1062 var = ssa_name (i);
1063 if (var != NULL_TREE && !virtual_operand_p (var))
1065 /* Add coalesces between all the result decls. */
1066 if (SSA_NAME_VAR (var)
1067 && TREE_CODE (SSA_NAME_VAR (var)) == RESULT_DECL)
1069 if (first == NULL_TREE)
1070 first = var;
1071 else
1073 gcc_assert (gimple_can_coalesce_p (var, first));
1074 v1 = SSA_NAME_VERSION (first);
1075 v2 = SSA_NAME_VERSION (var);
1076 bitmap_set_bit (used_in_copy, v1);
1077 bitmap_set_bit (used_in_copy, v2);
1078 cost = coalesce_cost_bb (EXIT_BLOCK_PTR);
1079 add_coalesce (cl, v1, v2, cost);
1082 /* Mark any default_def variables as being in the coalesce list
1083 since they will have to be coalesced with the base variable. If
1084 not marked as present, they won't be in the coalesce view. */
1085 if (SSA_NAME_IS_DEFAULT_DEF (var)
1086 && !has_zero_uses (var))
1087 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1091 return map;
1095 /* Attempt to coalesce ssa versions X and Y together using the partition
1096 mapping in MAP and checking conflicts in GRAPH. Output any debug info to
1097 DEBUG, if it is nun-NULL. */
1099 static inline bool
1100 attempt_coalesce (var_map map, ssa_conflicts_p graph, int x, int y,
1101 FILE *debug)
1103 int z;
1104 tree var1, var2;
1105 int p1, p2;
1107 p1 = var_to_partition (map, ssa_name (x));
1108 p2 = var_to_partition (map, ssa_name (y));
1110 if (debug)
1112 fprintf (debug, "(%d)", x);
1113 print_generic_expr (debug, partition_to_var (map, p1), TDF_SLIM);
1114 fprintf (debug, " & (%d)", y);
1115 print_generic_expr (debug, partition_to_var (map, p2), TDF_SLIM);
1118 if (p1 == p2)
1120 if (debug)
1121 fprintf (debug, ": Already Coalesced.\n");
1122 return true;
1125 if (debug)
1126 fprintf (debug, " [map: %d, %d] ", p1, p2);
1129 if (!ssa_conflicts_test_p (graph, p1, p2))
1131 var1 = partition_to_var (map, p1);
1132 var2 = partition_to_var (map, p2);
1133 z = var_union (map, var1, var2);
1134 if (z == NO_PARTITION)
1136 if (debug)
1137 fprintf (debug, ": Unable to perform partition union.\n");
1138 return false;
1141 /* z is the new combined partition. Remove the other partition from
1142 the list, and merge the conflicts. */
1143 if (z == p1)
1144 ssa_conflicts_merge (graph, p1, p2);
1145 else
1146 ssa_conflicts_merge (graph, p2, p1);
1148 if (debug)
1149 fprintf (debug, ": Success -> %d\n", z);
1150 return true;
1153 if (debug)
1154 fprintf (debug, ": Fail due to conflict\n");
1156 return false;
1160 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1161 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1163 static void
1164 coalesce_partitions (var_map map, ssa_conflicts_p graph, coalesce_list_p cl,
1165 FILE *debug)
1167 int x = 0, y = 0;
1168 tree var1, var2;
1169 int cost;
1170 basic_block bb;
1171 edge e;
1172 edge_iterator ei;
1174 /* First, coalesce all the copies across abnormal edges. These are not placed
1175 in the coalesce list because they do not need to be sorted, and simply
1176 consume extra memory/compilation time in large programs. */
1178 FOR_EACH_BB (bb)
1180 FOR_EACH_EDGE (e, ei, bb->preds)
1181 if (e->flags & EDGE_ABNORMAL)
1183 gimple_stmt_iterator gsi;
1184 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1185 gsi_next (&gsi))
1187 gimple phi = gsi_stmt (gsi);
1188 tree res = PHI_RESULT (phi);
1189 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1190 int v1 = SSA_NAME_VERSION (res);
1191 int v2 = SSA_NAME_VERSION (arg);
1193 if (debug)
1194 fprintf (debug, "Abnormal coalesce: ");
1196 if (!attempt_coalesce (map, graph, v1, v2, debug))
1197 fail_abnormal_edge_coalesce (v1, v2);
1202 /* Now process the items in the coalesce list. */
1204 while ((cost = pop_best_coalesce (cl, &x, &y)) != NO_BEST_COALESCE)
1206 var1 = ssa_name (x);
1207 var2 = ssa_name (y);
1209 /* Assert the coalesces have the same base variable. */
1210 gcc_assert (gimple_can_coalesce_p (var1, var2));
1212 if (debug)
1213 fprintf (debug, "Coalesce list: ");
1214 attempt_coalesce (map, graph, x, y, debug);
1219 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1221 struct ssa_name_var_hash : typed_noop_remove <tree_node>
1223 typedef union tree_node value_type;
1224 typedef union tree_node compare_type;
1225 static inline hashval_t hash (const value_type *);
1226 static inline int equal (const value_type *, const compare_type *);
1229 inline hashval_t
1230 ssa_name_var_hash::hash (const_tree n)
1232 return DECL_UID (SSA_NAME_VAR (n));
1235 inline int
1236 ssa_name_var_hash::equal (const value_type *n1, const compare_type *n2)
1238 return SSA_NAME_VAR (n1) == SSA_NAME_VAR (n2);
1242 /* Reduce the number of copies by coalescing variables in the function. Return
1243 a partition map with the resulting coalesces. */
1245 extern var_map
1246 coalesce_ssa_name (void)
1248 tree_live_info_p liveinfo;
1249 ssa_conflicts_p graph;
1250 coalesce_list_p cl;
1251 bitmap used_in_copies = BITMAP_ALLOC (NULL);
1252 var_map map;
1253 unsigned int i;
1255 cl = create_coalesce_list ();
1256 map = create_outofssa_var_map (cl, used_in_copies);
1258 /* We need to coalesce all names originating same SSA_NAME_VAR
1259 so debug info remains undisturbed. */
1260 if (!optimize)
1262 hash_table <ssa_name_var_hash> ssa_name_hash;
1264 ssa_name_hash.create (10);
1265 for (i = 1; i < num_ssa_names; i++)
1267 tree a = ssa_name (i);
1269 if (a
1270 && SSA_NAME_VAR (a)
1271 && !DECL_IGNORED_P (SSA_NAME_VAR (a))
1272 && (!has_zero_uses (a) || !SSA_NAME_IS_DEFAULT_DEF (a)))
1274 tree *slot = ssa_name_hash.find_slot (a, INSERT);
1276 if (!*slot)
1277 *slot = a;
1278 else
1280 add_coalesce (cl, SSA_NAME_VERSION (a), SSA_NAME_VERSION (*slot),
1281 MUST_COALESCE_COST - 1);
1282 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (a));
1283 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (*slot));
1287 ssa_name_hash.dispose ();
1289 if (dump_file && (dump_flags & TDF_DETAILS))
1290 dump_var_map (dump_file, map);
1292 /* Don't calculate live ranges for variables not in the coalesce list. */
1293 partition_view_bitmap (map, used_in_copies, true);
1294 BITMAP_FREE (used_in_copies);
1296 if (num_var_partitions (map) < 1)
1298 delete_coalesce_list (cl);
1299 return map;
1302 if (dump_file && (dump_flags & TDF_DETAILS))
1303 dump_var_map (dump_file, map);
1305 liveinfo = calculate_live_ranges (map);
1307 if (dump_file && (dump_flags & TDF_DETAILS))
1308 dump_live_info (dump_file, liveinfo, LIVEDUMP_ENTRY);
1310 /* Build a conflict graph. */
1311 graph = build_ssa_conflict_graph (liveinfo);
1312 delete_tree_live_info (liveinfo);
1313 if (dump_file && (dump_flags & TDF_DETAILS))
1314 ssa_conflicts_dump (dump_file, graph);
1316 sort_coalesce_list (cl);
1318 if (dump_file && (dump_flags & TDF_DETAILS))
1320 fprintf (dump_file, "\nAfter sorting:\n");
1321 dump_coalesce_list (dump_file, cl);
1324 /* First, coalesce all live on entry variables to their base variable.
1325 This will ensure the first use is coming from the correct location. */
1327 if (dump_file && (dump_flags & TDF_DETAILS))
1328 dump_var_map (dump_file, map);
1330 /* Now coalesce everything in the list. */
1331 coalesce_partitions (map, graph, cl,
1332 ((dump_flags & TDF_DETAILS) ? dump_file
1333 : NULL));
1335 delete_coalesce_list (cl);
1336 ssa_conflicts_delete (graph);
1338 return map;