* lib/ubsan-dg.exp (check_effective_target_fsanitize_undefined):
[official-gcc.git] / gcc / tree-ssa-coalesce.c
blob7d1825d59987c5583138f3021881b0ebdd47f2c6
1 /* Coalesce SSA_NAMES together for the out-of-ssa pass.
2 Copyright (C) 2004-2014 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 "hash-table.h"
31 #include "predict.h"
32 #include "vec.h"
33 #include "hashtab.h"
34 #include "hash-set.h"
35 #include "machmode.h"
36 #include "hard-reg-set.h"
37 #include "input.h"
38 #include "function.h"
39 #include "dominance.h"
40 #include "cfg.h"
41 #include "basic-block.h"
42 #include "tree-ssa-alias.h"
43 #include "internal-fn.h"
44 #include "gimple-expr.h"
45 #include "is-a.h"
46 #include "gimple.h"
47 #include "gimple-iterator.h"
48 #include "gimple-ssa.h"
49 #include "tree-phinodes.h"
50 #include "ssa-iterators.h"
51 #include "stringpool.h"
52 #include "tree-ssanames.h"
53 #include "tree-ssa-live.h"
54 #include "tree-ssa-coalesce.h"
55 #include "diagnostic-core.h"
58 /* This set of routines implements a coalesce_list. This is an object which
59 is used to track pairs of ssa_names which are desirable to coalesce
60 together to avoid copies. Costs are associated with each pair, and when
61 all desired information has been collected, the object can be used to
62 order the pairs for processing. */
64 /* This structure defines a pair entry. */
66 typedef struct coalesce_pair
68 int first_element;
69 int second_element;
70 int cost;
71 } * coalesce_pair_p;
72 typedef const struct coalesce_pair *const_coalesce_pair_p;
74 /* Coalesce pair hashtable helpers. */
76 struct coalesce_pair_hasher : typed_noop_remove <coalesce_pair>
78 typedef coalesce_pair value_type;
79 typedef coalesce_pair compare_type;
80 static inline hashval_t hash (const value_type *);
81 static inline bool equal (const value_type *, const compare_type *);
84 /* Hash function for coalesce list. Calculate hash for PAIR. */
86 inline hashval_t
87 coalesce_pair_hasher::hash (const value_type *pair)
89 hashval_t a = (hashval_t)(pair->first_element);
90 hashval_t b = (hashval_t)(pair->second_element);
92 return b * (b - 1) / 2 + a;
95 /* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
96 returning TRUE if the two pairs are equivalent. */
98 inline bool
99 coalesce_pair_hasher::equal (const value_type *p1, const compare_type *p2)
101 return (p1->first_element == p2->first_element
102 && p1->second_element == p2->second_element);
105 typedef hash_table<coalesce_pair_hasher> coalesce_table_type;
106 typedef coalesce_table_type::iterator coalesce_iterator_type;
109 typedef struct cost_one_pair_d
111 int first_element;
112 int second_element;
113 struct cost_one_pair_d *next;
114 } * cost_one_pair_p;
116 /* This structure maintains the list of coalesce pairs. */
118 typedef struct coalesce_list_d
120 coalesce_table_type *list; /* Hash table. */
121 coalesce_pair_p *sorted; /* List when sorted. */
122 int num_sorted; /* Number in the sorted list. */
123 cost_one_pair_p cost_one_list;/* Single use coalesces with cost 1. */
124 } *coalesce_list_p;
126 #define NO_BEST_COALESCE -1
127 #define MUST_COALESCE_COST INT_MAX
130 /* Return cost of execution of copy instruction with FREQUENCY. */
132 static inline int
133 coalesce_cost (int frequency, bool optimize_for_size)
135 /* Base costs on BB frequencies bounded by 1. */
136 int cost = frequency;
138 if (!cost)
139 cost = 1;
141 if (optimize_for_size)
142 cost = 1;
144 return cost;
148 /* Return the cost of executing a copy instruction in basic block BB. */
150 static inline int
151 coalesce_cost_bb (basic_block bb)
153 return coalesce_cost (bb->frequency, optimize_bb_for_size_p (bb));
157 /* Return the cost of executing a copy instruction on edge E. */
159 static inline int
160 coalesce_cost_edge (edge e)
162 int mult = 1;
164 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
165 if (EDGE_CRITICAL_P (e))
166 mult = 2;
167 if (e->flags & EDGE_ABNORMAL)
168 return MUST_COALESCE_COST;
169 if (e->flags & EDGE_EH)
171 edge e2;
172 edge_iterator ei;
173 FOR_EACH_EDGE (e2, ei, e->dest->preds)
174 if (e2 != e)
176 /* Putting code on EH edge that leads to BB
177 with multiple predecestors imply splitting of
178 edge too. */
179 if (mult < 2)
180 mult = 2;
181 /* If there are multiple EH predecestors, we
182 also copy EH regions and produce separate
183 landing pad. This is expensive. */
184 if (e2->flags & EDGE_EH)
186 mult = 5;
187 break;
192 return coalesce_cost (EDGE_FREQUENCY (e),
193 optimize_edge_for_size_p (e)) * mult;
197 /* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
198 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
199 NO_BEST_COALESCE is returned if there aren't any. */
201 static inline int
202 pop_cost_one_pair (coalesce_list_p cl, int *p1, int *p2)
204 cost_one_pair_p ptr;
206 ptr = cl->cost_one_list;
207 if (!ptr)
208 return NO_BEST_COALESCE;
210 *p1 = ptr->first_element;
211 *p2 = ptr->second_element;
212 cl->cost_one_list = ptr->next;
214 free (ptr);
216 return 1;
219 /* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
220 2 elements via P1 and P2. Their calculated cost is returned by the function.
221 NO_BEST_COALESCE is returned if the coalesce list is empty. */
223 static inline int
224 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
226 coalesce_pair_p node;
227 int ret;
229 if (cl->sorted == NULL)
230 return pop_cost_one_pair (cl, p1, p2);
232 if (cl->num_sorted == 0)
233 return pop_cost_one_pair (cl, p1, p2);
235 node = cl->sorted[--(cl->num_sorted)];
236 *p1 = node->first_element;
237 *p2 = node->second_element;
238 ret = node->cost;
239 free (node);
241 return ret;
245 /* Create a new empty coalesce list object and return it. */
247 static inline coalesce_list_p
248 create_coalesce_list (void)
250 coalesce_list_p list;
251 unsigned size = num_ssa_names * 3;
253 if (size < 40)
254 size = 40;
256 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
257 list->list = new coalesce_table_type (size);
258 list->sorted = NULL;
259 list->num_sorted = 0;
260 list->cost_one_list = NULL;
261 return list;
265 /* Delete coalesce list CL. */
267 static inline void
268 delete_coalesce_list (coalesce_list_p cl)
270 gcc_assert (cl->cost_one_list == NULL);
271 delete cl->list;
272 cl->list = NULL;
273 free (cl->sorted);
274 gcc_assert (cl->num_sorted == 0);
275 free (cl);
279 /* Find a matching coalesce pair object in CL for the pair P1 and P2. If
280 one isn't found, return NULL if CREATE is false, otherwise create a new
281 coalesce pair object and return it. */
283 static coalesce_pair_p
284 find_coalesce_pair (coalesce_list_p cl, int p1, int p2, bool create)
286 struct coalesce_pair p;
287 coalesce_pair **slot;
288 unsigned int hash;
290 /* Normalize so that p1 is the smaller value. */
291 if (p2 < p1)
293 p.first_element = p2;
294 p.second_element = p1;
296 else
298 p.first_element = p1;
299 p.second_element = p2;
302 hash = coalesce_pair_hasher::hash (&p);
303 slot = cl->list->find_slot_with_hash (&p, hash, create ? INSERT : NO_INSERT);
304 if (!slot)
305 return NULL;
307 if (!*slot)
309 struct coalesce_pair * pair = XNEW (struct coalesce_pair);
310 gcc_assert (cl->sorted == NULL);
311 pair->first_element = p.first_element;
312 pair->second_element = p.second_element;
313 pair->cost = 0;
314 *slot = pair;
317 return (struct coalesce_pair *) *slot;
320 static inline void
321 add_cost_one_coalesce (coalesce_list_p cl, int p1, int p2)
323 cost_one_pair_p pair;
325 pair = XNEW (struct cost_one_pair_d);
326 pair->first_element = p1;
327 pair->second_element = p2;
328 pair->next = cl->cost_one_list;
329 cl->cost_one_list = pair;
333 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
335 static inline void
336 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
338 coalesce_pair_p node;
340 gcc_assert (cl->sorted == NULL);
341 if (p1 == p2)
342 return;
344 node = find_coalesce_pair (cl, p1, p2, true);
346 /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way. */
347 if (node->cost < MUST_COALESCE_COST - 1)
349 if (value < MUST_COALESCE_COST - 1)
350 node->cost += value;
351 else
352 node->cost = value;
357 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
359 static int
360 compare_pairs (const void *p1, const void *p2)
362 const_coalesce_pair_p const *const pp1 = (const_coalesce_pair_p const *) p1;
363 const_coalesce_pair_p const *const pp2 = (const_coalesce_pair_p const *) p2;
364 int result;
366 result = (* pp1)->cost - (* pp2)->cost;
367 /* Since qsort does not guarantee stability we use the elements
368 as a secondary key. This provides us with independence from
369 the host's implementation of the sorting algorithm. */
370 if (result == 0)
372 result = (* pp2)->first_element - (* pp1)->first_element;
373 if (result == 0)
374 result = (* pp2)->second_element - (* pp1)->second_element;
377 return result;
381 /* Return the number of unique coalesce pairs in CL. */
383 static inline int
384 num_coalesce_pairs (coalesce_list_p cl)
386 return cl->list->elements ();
390 /* Iterate over CL using ITER, returning values in PAIR. */
392 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
393 FOR_EACH_HASH_TABLE_ELEMENT (*(CL)->list, (PAIR), coalesce_pair_p, (ITER))
396 /* Prepare CL for removal of preferred pairs. When finished they are sorted
397 in order from most important coalesce to least important. */
399 static void
400 sort_coalesce_list (coalesce_list_p cl)
402 unsigned x, num;
403 coalesce_pair_p p;
404 coalesce_iterator_type ppi;
406 gcc_assert (cl->sorted == NULL);
408 num = num_coalesce_pairs (cl);
409 cl->num_sorted = num;
410 if (num == 0)
411 return;
413 /* Allocate a vector for the pair pointers. */
414 cl->sorted = XNEWVEC (coalesce_pair_p, num);
416 /* Populate the vector with pointers to the pairs. */
417 x = 0;
418 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
419 cl->sorted[x++] = p;
420 gcc_assert (x == num);
422 /* Already sorted. */
423 if (num == 1)
424 return;
426 /* If there are only 2, just pick swap them if the order isn't correct. */
427 if (num == 2)
429 if (cl->sorted[0]->cost > cl->sorted[1]->cost)
431 p = cl->sorted[0];
432 cl->sorted[0] = cl->sorted[1];
433 cl->sorted[1] = p;
435 return;
438 /* Only call qsort if there are more than 2 items. */
439 if (num > 2)
440 qsort (cl->sorted, num, sizeof (coalesce_pair_p), compare_pairs);
444 /* Send debug info for coalesce list CL to file F. */
446 static void
447 dump_coalesce_list (FILE *f, coalesce_list_p cl)
449 coalesce_pair_p node;
450 coalesce_iterator_type ppi;
452 int x;
453 tree var;
455 if (cl->sorted == NULL)
457 fprintf (f, "Coalesce List:\n");
458 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
460 tree var1 = ssa_name (node->first_element);
461 tree var2 = ssa_name (node->second_element);
462 print_generic_expr (f, var1, TDF_SLIM);
463 fprintf (f, " <-> ");
464 print_generic_expr (f, var2, TDF_SLIM);
465 fprintf (f, " (%1d), ", node->cost);
466 fprintf (f, "\n");
469 else
471 fprintf (f, "Sorted Coalesce list:\n");
472 for (x = cl->num_sorted - 1 ; x >=0; x--)
474 node = cl->sorted[x];
475 fprintf (f, "(%d) ", node->cost);
476 var = ssa_name (node->first_element);
477 print_generic_expr (f, var, TDF_SLIM);
478 fprintf (f, " <-> ");
479 var = ssa_name (node->second_element);
480 print_generic_expr (f, var, TDF_SLIM);
481 fprintf (f, "\n");
487 /* This represents a conflict graph. Implemented as an array of bitmaps.
488 A full matrix is used for conflicts rather than just upper triangular form.
489 this make sit much simpler and faster to perform conflict merges. */
491 typedef struct ssa_conflicts_d
493 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
494 vec<bitmap> conflicts;
495 } * ssa_conflicts_p;
497 /* Return an empty new conflict graph for SIZE elements. */
499 static inline ssa_conflicts_p
500 ssa_conflicts_new (unsigned size)
502 ssa_conflicts_p ptr;
504 ptr = XNEW (struct ssa_conflicts_d);
505 bitmap_obstack_initialize (&ptr->obstack);
506 ptr->conflicts.create (size);
507 ptr->conflicts.safe_grow_cleared (size);
508 return ptr;
512 /* Free storage for conflict graph PTR. */
514 static inline void
515 ssa_conflicts_delete (ssa_conflicts_p ptr)
517 bitmap_obstack_release (&ptr->obstack);
518 ptr->conflicts.release ();
519 free (ptr);
523 /* Test if elements X and Y conflict in graph PTR. */
525 static inline bool
526 ssa_conflicts_test_p (ssa_conflicts_p ptr, unsigned x, unsigned y)
528 bitmap bx = ptr->conflicts[x];
529 bitmap by = ptr->conflicts[y];
531 gcc_checking_assert (x != y);
533 if (bx)
534 /* Avoid the lookup if Y has no conflicts. */
535 return by ? bitmap_bit_p (bx, y) : false;
536 else
537 return false;
541 /* Add a conflict with Y to the bitmap for X in graph PTR. */
543 static inline void
544 ssa_conflicts_add_one (ssa_conflicts_p ptr, unsigned x, unsigned y)
546 bitmap bx = ptr->conflicts[x];
547 /* If there are no conflicts yet, allocate the bitmap and set bit. */
548 if (! bx)
549 bx = ptr->conflicts[x] = BITMAP_ALLOC (&ptr->obstack);
550 bitmap_set_bit (bx, y);
554 /* Add conflicts between X and Y in graph PTR. */
556 static inline void
557 ssa_conflicts_add (ssa_conflicts_p ptr, unsigned x, unsigned y)
559 gcc_checking_assert (x != y);
560 ssa_conflicts_add_one (ptr, x, y);
561 ssa_conflicts_add_one (ptr, y, x);
565 /* Merge all Y's conflict into X in graph PTR. */
567 static inline void
568 ssa_conflicts_merge (ssa_conflicts_p ptr, unsigned x, unsigned y)
570 unsigned z;
571 bitmap_iterator bi;
572 bitmap bx = ptr->conflicts[x];
573 bitmap by = ptr->conflicts[y];
575 gcc_checking_assert (x != y);
576 if (! by)
577 return;
579 /* Add a conflict between X and every one Y has. If the bitmap doesn't
580 exist, then it has already been coalesced, and we don't need to add a
581 conflict. */
582 EXECUTE_IF_SET_IN_BITMAP (by, 0, z, bi)
584 bitmap bz = ptr->conflicts[z];
585 if (bz)
586 bitmap_set_bit (bz, x);
589 if (bx)
591 /* If X has conflicts, add Y's to X. */
592 bitmap_ior_into (bx, by);
593 BITMAP_FREE (by);
594 ptr->conflicts[y] = NULL;
596 else
598 /* If X has no conflicts, simply use Y's. */
599 ptr->conflicts[x] = by;
600 ptr->conflicts[y] = NULL;
605 /* Dump a conflicts graph. */
607 static void
608 ssa_conflicts_dump (FILE *file, ssa_conflicts_p ptr)
610 unsigned x;
611 bitmap b;
613 fprintf (file, "\nConflict graph:\n");
615 FOR_EACH_VEC_ELT (ptr->conflicts, x, b)
616 if (b)
618 fprintf (file, "%d: ", x);
619 dump_bitmap (file, b);
624 /* This structure is used to efficiently record the current status of live
625 SSA_NAMES when building a conflict graph.
626 LIVE_BASE_VAR has a bit set for each base variable which has at least one
627 ssa version live.
628 LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
629 index, and is used to track what partitions of each base variable are
630 live. This makes it easy to add conflicts between just live partitions
631 with the same base variable.
632 The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
633 marked as being live. This delays clearing of these bitmaps until
634 they are actually needed again. */
636 typedef struct live_track_d
638 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
639 bitmap live_base_var; /* Indicates if a basevar is live. */
640 bitmap *live_base_partitions; /* Live partitions for each basevar. */
641 var_map map; /* Var_map being used for partition mapping. */
642 } * live_track_p;
645 /* This routine will create a new live track structure based on the partitions
646 in MAP. */
648 static live_track_p
649 new_live_track (var_map map)
651 live_track_p ptr;
652 int lim, x;
654 /* Make sure there is a partition view in place. */
655 gcc_assert (map->partition_to_base_index != NULL);
657 ptr = (live_track_p) xmalloc (sizeof (struct live_track_d));
658 ptr->map = map;
659 lim = num_basevars (map);
660 bitmap_obstack_initialize (&ptr->obstack);
661 ptr->live_base_partitions = (bitmap *) xmalloc (sizeof (bitmap *) * lim);
662 ptr->live_base_var = BITMAP_ALLOC (&ptr->obstack);
663 for (x = 0; x < lim; x++)
664 ptr->live_base_partitions[x] = BITMAP_ALLOC (&ptr->obstack);
665 return ptr;
669 /* This routine will free the memory associated with PTR. */
671 static void
672 delete_live_track (live_track_p ptr)
674 bitmap_obstack_release (&ptr->obstack);
675 free (ptr->live_base_partitions);
676 free (ptr);
680 /* This function will remove PARTITION from the live list in PTR. */
682 static inline void
683 live_track_remove_partition (live_track_p ptr, int partition)
685 int root;
687 root = basevar_index (ptr->map, partition);
688 bitmap_clear_bit (ptr->live_base_partitions[root], partition);
689 /* If the element list is empty, make the base variable not live either. */
690 if (bitmap_empty_p (ptr->live_base_partitions[root]))
691 bitmap_clear_bit (ptr->live_base_var, root);
695 /* This function will adds PARTITION to the live list in PTR. */
697 static inline void
698 live_track_add_partition (live_track_p ptr, int partition)
700 int root;
702 root = basevar_index (ptr->map, partition);
703 /* If this base var wasn't live before, it is now. Clear the element list
704 since it was delayed until needed. */
705 if (bitmap_set_bit (ptr->live_base_var, root))
706 bitmap_clear (ptr->live_base_partitions[root]);
707 bitmap_set_bit (ptr->live_base_partitions[root], partition);
712 /* Clear the live bit for VAR in PTR. */
714 static inline void
715 live_track_clear_var (live_track_p ptr, tree var)
717 int p;
719 p = var_to_partition (ptr->map, var);
720 if (p != NO_PARTITION)
721 live_track_remove_partition (ptr, p);
725 /* Return TRUE if VAR is live in PTR. */
727 static inline bool
728 live_track_live_p (live_track_p ptr, tree var)
730 int p, root;
732 p = var_to_partition (ptr->map, var);
733 if (p != NO_PARTITION)
735 root = basevar_index (ptr->map, p);
736 if (bitmap_bit_p (ptr->live_base_var, root))
737 return bitmap_bit_p (ptr->live_base_partitions[root], p);
739 return false;
743 /* This routine will add USE to PTR. USE will be marked as live in both the
744 ssa live map and the live bitmap for the root of USE. */
746 static inline void
747 live_track_process_use (live_track_p ptr, tree use)
749 int p;
751 p = var_to_partition (ptr->map, use);
752 if (p == NO_PARTITION)
753 return;
755 /* Mark as live in the appropriate live list. */
756 live_track_add_partition (ptr, p);
760 /* This routine will process a DEF in PTR. DEF will be removed from the live
761 lists, and if there are any other live partitions with the same base
762 variable, conflicts will be added to GRAPH. */
764 static inline void
765 live_track_process_def (live_track_p ptr, tree def, ssa_conflicts_p graph)
767 int p, root;
768 bitmap b;
769 unsigned x;
770 bitmap_iterator bi;
772 p = var_to_partition (ptr->map, def);
773 if (p == NO_PARTITION)
774 return;
776 /* Clear the liveness bit. */
777 live_track_remove_partition (ptr, p);
779 /* If the bitmap isn't empty now, conflicts need to be added. */
780 root = basevar_index (ptr->map, p);
781 if (bitmap_bit_p (ptr->live_base_var, root))
783 b = ptr->live_base_partitions[root];
784 EXECUTE_IF_SET_IN_BITMAP (b, 0, x, bi)
785 ssa_conflicts_add (graph, p, x);
790 /* Initialize PTR with the partitions set in INIT. */
792 static inline void
793 live_track_init (live_track_p ptr, bitmap init)
795 unsigned p;
796 bitmap_iterator bi;
798 /* Mark all live on exit partitions. */
799 EXECUTE_IF_SET_IN_BITMAP (init, 0, p, bi)
800 live_track_add_partition (ptr, p);
804 /* This routine will clear all live partitions in PTR. */
806 static inline void
807 live_track_clear_base_vars (live_track_p ptr)
809 /* Simply clear the live base list. Anything marked as live in the element
810 lists will be cleared later if/when the base variable ever comes alive
811 again. */
812 bitmap_clear (ptr->live_base_var);
816 /* Build a conflict graph based on LIVEINFO. Any partitions which are in the
817 partition view of the var_map liveinfo is based on get entries in the
818 conflict graph. Only conflicts between ssa_name partitions with the same
819 base variable are added. */
821 static ssa_conflicts_p
822 build_ssa_conflict_graph (tree_live_info_p liveinfo)
824 ssa_conflicts_p graph;
825 var_map map;
826 basic_block bb;
827 ssa_op_iter iter;
828 live_track_p live;
830 map = live_var_map (liveinfo);
831 graph = ssa_conflicts_new (num_var_partitions (map));
833 live = new_live_track (map);
835 FOR_EACH_BB_FN (bb, cfun)
837 /* Start with live on exit temporaries. */
838 live_track_init (live, live_on_exit (liveinfo, bb));
840 for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);
841 gsi_prev (&gsi))
843 tree var;
844 gimple stmt = gsi_stmt (gsi);
846 /* A copy between 2 partitions does not introduce an interference
847 by itself. If they did, you would never be able to coalesce
848 two things which are copied. If the two variables really do
849 conflict, they will conflict elsewhere in the program.
851 This is handled by simply removing the SRC of the copy from the
852 live list, and processing the stmt normally. */
853 if (is_gimple_assign (stmt))
855 tree lhs = gimple_assign_lhs (stmt);
856 tree rhs1 = gimple_assign_rhs1 (stmt);
857 if (gimple_assign_copy_p (stmt)
858 && TREE_CODE (lhs) == SSA_NAME
859 && TREE_CODE (rhs1) == SSA_NAME)
860 live_track_clear_var (live, rhs1);
862 else if (is_gimple_debug (stmt))
863 continue;
865 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
866 live_track_process_def (live, var, graph);
868 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
869 live_track_process_use (live, var);
872 /* If result of a PHI is unused, looping over the statements will not
873 record any conflicts since the def was never live. Since the PHI node
874 is going to be translated out of SSA form, it will insert a copy.
875 There must be a conflict recorded between the result of the PHI and
876 any variables that are live. Otherwise the out-of-ssa translation
877 may create incorrect code. */
878 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
879 gsi_next (&gsi))
881 gphi *phi = gsi.phi ();
882 tree result = PHI_RESULT (phi);
883 if (live_track_live_p (live, result))
884 live_track_process_def (live, result, graph);
887 live_track_clear_base_vars (live);
890 delete_live_track (live);
891 return graph;
895 /* Shortcut routine to print messages to file F of the form:
896 "STR1 EXPR1 STR2 EXPR2 STR3." */
898 static inline void
899 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
900 tree expr2, const char *str3)
902 fprintf (f, "%s", str1);
903 print_generic_expr (f, expr1, TDF_SLIM);
904 fprintf (f, "%s", str2);
905 print_generic_expr (f, expr2, TDF_SLIM);
906 fprintf (f, "%s", str3);
910 /* Print a failure to coalesce a MUST_COALESCE pair X and Y. */
912 static inline void
913 fail_abnormal_edge_coalesce (int x, int y)
915 fprintf (stderr, "\nUnable to coalesce ssa_names %d and %d",x, y);
916 fprintf (stderr, " which are marked as MUST COALESCE.\n");
917 print_generic_expr (stderr, ssa_name (x), TDF_SLIM);
918 fprintf (stderr, " and ");
919 print_generic_stmt (stderr, ssa_name (y), TDF_SLIM);
921 internal_error ("SSA corruption");
925 /* This function creates a var_map for the current function as well as creating
926 a coalesce list for use later in the out of ssa process. */
928 static var_map
929 create_outofssa_var_map (coalesce_list_p cl, bitmap used_in_copy)
931 gimple_stmt_iterator gsi;
932 basic_block bb;
933 tree var;
934 gimple stmt;
935 tree first;
936 var_map map;
937 ssa_op_iter iter;
938 int v1, v2, cost;
939 unsigned i;
941 map = init_var_map (num_ssa_names);
943 FOR_EACH_BB_FN (bb, cfun)
945 tree arg;
947 for (gphi_iterator gpi = gsi_start_phis (bb);
948 !gsi_end_p (gpi);
949 gsi_next (&gpi))
951 gphi *phi = gpi.phi ();
952 size_t i;
953 int ver;
954 tree res;
955 bool saw_copy = false;
957 res = gimple_phi_result (phi);
958 ver = SSA_NAME_VERSION (res);
959 register_ssa_partition (map, res);
961 /* Register ssa_names and coalesces between the args and the result
962 of all PHI. */
963 for (i = 0; i < gimple_phi_num_args (phi); i++)
965 edge e = gimple_phi_arg_edge (phi, i);
966 arg = PHI_ARG_DEF (phi, i);
967 if (TREE_CODE (arg) != SSA_NAME)
968 continue;
970 register_ssa_partition (map, arg);
971 if (gimple_can_coalesce_p (arg, res)
972 || (e->flags & EDGE_ABNORMAL))
974 saw_copy = true;
975 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (arg));
976 if ((e->flags & EDGE_ABNORMAL) == 0)
978 int cost = coalesce_cost_edge (e);
979 if (cost == 1 && has_single_use (arg))
980 add_cost_one_coalesce (cl, ver, SSA_NAME_VERSION (arg));
981 else
982 add_coalesce (cl, ver, SSA_NAME_VERSION (arg), cost);
986 if (saw_copy)
987 bitmap_set_bit (used_in_copy, ver);
990 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
992 stmt = gsi_stmt (gsi);
994 if (is_gimple_debug (stmt))
995 continue;
997 /* Register USE and DEF operands in each statement. */
998 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
999 register_ssa_partition (map, var);
1001 /* Check for copy coalesces. */
1002 switch (gimple_code (stmt))
1004 case GIMPLE_ASSIGN:
1006 tree lhs = gimple_assign_lhs (stmt);
1007 tree rhs1 = gimple_assign_rhs1 (stmt);
1008 if (gimple_assign_ssa_name_copy_p (stmt)
1009 && gimple_can_coalesce_p (lhs, rhs1))
1011 v1 = SSA_NAME_VERSION (lhs);
1012 v2 = SSA_NAME_VERSION (rhs1);
1013 cost = coalesce_cost_bb (bb);
1014 add_coalesce (cl, v1, v2, cost);
1015 bitmap_set_bit (used_in_copy, v1);
1016 bitmap_set_bit (used_in_copy, v2);
1019 break;
1021 case GIMPLE_ASM:
1023 gasm *asm_stmt = as_a <gasm *> (stmt);
1024 unsigned long noutputs, i;
1025 unsigned long ninputs;
1026 tree *outputs, link;
1027 noutputs = gimple_asm_noutputs (asm_stmt);
1028 ninputs = gimple_asm_ninputs (asm_stmt);
1029 outputs = (tree *) alloca (noutputs * sizeof (tree));
1030 for (i = 0; i < noutputs; ++i)
1032 link = gimple_asm_output_op (asm_stmt, i);
1033 outputs[i] = TREE_VALUE (link);
1036 for (i = 0; i < ninputs; ++i)
1038 const char *constraint;
1039 tree input;
1040 char *end;
1041 unsigned long match;
1043 link = gimple_asm_input_op (asm_stmt, i);
1044 constraint
1045 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1046 input = TREE_VALUE (link);
1048 if (TREE_CODE (input) != SSA_NAME)
1049 continue;
1051 match = strtoul (constraint, &end, 10);
1052 if (match >= noutputs || end == constraint)
1053 continue;
1055 if (TREE_CODE (outputs[match]) != SSA_NAME)
1056 continue;
1058 v1 = SSA_NAME_VERSION (outputs[match]);
1059 v2 = SSA_NAME_VERSION (input);
1061 if (gimple_can_coalesce_p (outputs[match], input))
1063 cost = coalesce_cost (REG_BR_PROB_BASE,
1064 optimize_bb_for_size_p (bb));
1065 add_coalesce (cl, v1, v2, cost);
1066 bitmap_set_bit (used_in_copy, v1);
1067 bitmap_set_bit (used_in_copy, v2);
1070 break;
1073 default:
1074 break;
1079 /* Now process result decls and live on entry variables for entry into
1080 the coalesce list. */
1081 first = NULL_TREE;
1082 for (i = 1; i < num_ssa_names; i++)
1084 var = ssa_name (i);
1085 if (var != NULL_TREE && !virtual_operand_p (var))
1087 /* Add coalesces between all the result decls. */
1088 if (SSA_NAME_VAR (var)
1089 && TREE_CODE (SSA_NAME_VAR (var)) == RESULT_DECL)
1091 if (first == NULL_TREE)
1092 first = var;
1093 else
1095 gcc_assert (gimple_can_coalesce_p (var, first));
1096 v1 = SSA_NAME_VERSION (first);
1097 v2 = SSA_NAME_VERSION (var);
1098 bitmap_set_bit (used_in_copy, v1);
1099 bitmap_set_bit (used_in_copy, v2);
1100 cost = coalesce_cost_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
1101 add_coalesce (cl, v1, v2, cost);
1104 /* Mark any default_def variables as being in the coalesce list
1105 since they will have to be coalesced with the base variable. If
1106 not marked as present, they won't be in the coalesce view. */
1107 if (SSA_NAME_IS_DEFAULT_DEF (var)
1108 && !has_zero_uses (var))
1109 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1113 return map;
1117 /* Attempt to coalesce ssa versions X and Y together using the partition
1118 mapping in MAP and checking conflicts in GRAPH. Output any debug info to
1119 DEBUG, if it is nun-NULL. */
1121 static inline bool
1122 attempt_coalesce (var_map map, ssa_conflicts_p graph, int x, int y,
1123 FILE *debug)
1125 int z;
1126 tree var1, var2;
1127 int p1, p2;
1129 p1 = var_to_partition (map, ssa_name (x));
1130 p2 = var_to_partition (map, ssa_name (y));
1132 if (debug)
1134 fprintf (debug, "(%d)", x);
1135 print_generic_expr (debug, partition_to_var (map, p1), TDF_SLIM);
1136 fprintf (debug, " & (%d)", y);
1137 print_generic_expr (debug, partition_to_var (map, p2), TDF_SLIM);
1140 if (p1 == p2)
1142 if (debug)
1143 fprintf (debug, ": Already Coalesced.\n");
1144 return true;
1147 if (debug)
1148 fprintf (debug, " [map: %d, %d] ", p1, p2);
1151 if (!ssa_conflicts_test_p (graph, p1, p2))
1153 var1 = partition_to_var (map, p1);
1154 var2 = partition_to_var (map, p2);
1155 z = var_union (map, var1, var2);
1156 if (z == NO_PARTITION)
1158 if (debug)
1159 fprintf (debug, ": Unable to perform partition union.\n");
1160 return false;
1163 /* z is the new combined partition. Remove the other partition from
1164 the list, and merge the conflicts. */
1165 if (z == p1)
1166 ssa_conflicts_merge (graph, p1, p2);
1167 else
1168 ssa_conflicts_merge (graph, p2, p1);
1170 if (debug)
1171 fprintf (debug, ": Success -> %d\n", z);
1172 return true;
1175 if (debug)
1176 fprintf (debug, ": Fail due to conflict\n");
1178 return false;
1182 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1183 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1185 static void
1186 coalesce_partitions (var_map map, ssa_conflicts_p graph, coalesce_list_p cl,
1187 FILE *debug)
1189 int x = 0, y = 0;
1190 tree var1, var2;
1191 int cost;
1192 basic_block bb;
1193 edge e;
1194 edge_iterator ei;
1196 /* First, coalesce all the copies across abnormal edges. These are not placed
1197 in the coalesce list because they do not need to be sorted, and simply
1198 consume extra memory/compilation time in large programs. */
1200 FOR_EACH_BB_FN (bb, cfun)
1202 FOR_EACH_EDGE (e, ei, bb->preds)
1203 if (e->flags & EDGE_ABNORMAL)
1205 gphi_iterator gsi;
1206 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1207 gsi_next (&gsi))
1209 gphi *phi = gsi.phi ();
1210 tree res = PHI_RESULT (phi);
1211 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1212 int v1 = SSA_NAME_VERSION (res);
1213 int v2 = SSA_NAME_VERSION (arg);
1215 if (debug)
1216 fprintf (debug, "Abnormal coalesce: ");
1218 if (!attempt_coalesce (map, graph, v1, v2, debug))
1219 fail_abnormal_edge_coalesce (v1, v2);
1224 /* Now process the items in the coalesce list. */
1226 while ((cost = pop_best_coalesce (cl, &x, &y)) != NO_BEST_COALESCE)
1228 var1 = ssa_name (x);
1229 var2 = ssa_name (y);
1231 /* Assert the coalesces have the same base variable. */
1232 gcc_assert (gimple_can_coalesce_p (var1, var2));
1234 if (debug)
1235 fprintf (debug, "Coalesce list: ");
1236 attempt_coalesce (map, graph, x, y, debug);
1241 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1243 struct ssa_name_var_hash : typed_noop_remove <tree_node>
1245 typedef union tree_node value_type;
1246 typedef union tree_node compare_type;
1247 static inline hashval_t hash (const value_type *);
1248 static inline int equal (const value_type *, const compare_type *);
1251 inline hashval_t
1252 ssa_name_var_hash::hash (const_tree n)
1254 return DECL_UID (SSA_NAME_VAR (n));
1257 inline int
1258 ssa_name_var_hash::equal (const value_type *n1, const compare_type *n2)
1260 return SSA_NAME_VAR (n1) == SSA_NAME_VAR (n2);
1264 /* Reduce the number of copies by coalescing variables in the function. Return
1265 a partition map with the resulting coalesces. */
1267 extern var_map
1268 coalesce_ssa_name (void)
1270 tree_live_info_p liveinfo;
1271 ssa_conflicts_p graph;
1272 coalesce_list_p cl;
1273 bitmap used_in_copies = BITMAP_ALLOC (NULL);
1274 var_map map;
1275 unsigned int i;
1277 cl = create_coalesce_list ();
1278 map = create_outofssa_var_map (cl, used_in_copies);
1280 /* If optimization is disabled, we need to coalesce all the names originating
1281 from the same SSA_NAME_VAR so debug info remains undisturbed. */
1282 if (!optimize)
1284 hash_table<ssa_name_var_hash> ssa_name_hash (10);
1286 for (i = 1; i < num_ssa_names; i++)
1288 tree a = ssa_name (i);
1290 if (a
1291 && SSA_NAME_VAR (a)
1292 && !DECL_IGNORED_P (SSA_NAME_VAR (a))
1293 && (!has_zero_uses (a) || !SSA_NAME_IS_DEFAULT_DEF (a)))
1295 tree *slot = ssa_name_hash.find_slot (a, INSERT);
1297 if (!*slot)
1298 *slot = a;
1299 else
1301 /* If the variable is a PARM_DECL or a RESULT_DECL, we
1302 _require_ that all the names originating from it be
1303 coalesced, because there must be a single partition
1304 containing all the names so that it can be assigned
1305 the canonical RTL location of the DECL safely.
1306 If in_lto_p, a function could have been compiled
1307 originally with optimizations and only the link
1308 performed at -O0, so we can't actually require it. */
1309 const int cost
1310 = (TREE_CODE (SSA_NAME_VAR (a)) == VAR_DECL || in_lto_p)
1311 ? MUST_COALESCE_COST - 1 : MUST_COALESCE_COST;
1312 add_coalesce (cl, SSA_NAME_VERSION (a),
1313 SSA_NAME_VERSION (*slot), cost);
1314 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (a));
1315 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (*slot));
1320 if (dump_file && (dump_flags & TDF_DETAILS))
1321 dump_var_map (dump_file, map);
1323 /* Don't calculate live ranges for variables not in the coalesce list. */
1324 partition_view_bitmap (map, used_in_copies, true);
1325 BITMAP_FREE (used_in_copies);
1327 if (num_var_partitions (map) < 1)
1329 delete_coalesce_list (cl);
1330 return map;
1333 if (dump_file && (dump_flags & TDF_DETAILS))
1334 dump_var_map (dump_file, map);
1336 liveinfo = calculate_live_ranges (map);
1338 if (dump_file && (dump_flags & TDF_DETAILS))
1339 dump_live_info (dump_file, liveinfo, LIVEDUMP_ENTRY);
1341 /* Build a conflict graph. */
1342 graph = build_ssa_conflict_graph (liveinfo);
1343 delete_tree_live_info (liveinfo);
1344 if (dump_file && (dump_flags & TDF_DETAILS))
1345 ssa_conflicts_dump (dump_file, graph);
1347 sort_coalesce_list (cl);
1349 if (dump_file && (dump_flags & TDF_DETAILS))
1351 fprintf (dump_file, "\nAfter sorting:\n");
1352 dump_coalesce_list (dump_file, cl);
1355 /* First, coalesce all live on entry variables to their base variable.
1356 This will ensure the first use is coming from the correct location. */
1358 if (dump_file && (dump_flags & TDF_DETAILS))
1359 dump_var_map (dump_file, map);
1361 /* Now coalesce everything in the list. */
1362 coalesce_partitions (map, graph, cl,
1363 ((dump_flags & TDF_DETAILS) ? dump_file
1364 : NULL));
1366 delete_coalesce_list (cl);
1367 ssa_conflicts_delete (graph);
1369 return map;