* tree-outof-ssa.c (tree-ssa.h, tree-dfa.h): Include header files.
[official-gcc.git] / gcc / tree-ssa-coalesce.c
blobe4f576fbc19900a2c93e7f30d173715dcc0c1a06
1 /* Coalesce SSA_NAMES together for the out-of-ssa pass.
2 Copyright (C) 2004-2018 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 "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "predict.h"
28 #include "memmodel.h"
29 #include "tm_p.h"
30 #include "ssa.h"
31 #include "tree-ssa.h"
32 #include "tree-pretty-print.h"
33 #include "diagnostic-core.h"
34 #include "dumpfile.h"
35 #include "gimple-iterator.h"
36 #include "tree-ssa-live.h"
37 #include "tree-ssa-coalesce.h"
38 #include "explow.h"
39 #include "tree-dfa.h"
40 #include "stor-layout.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 struct coalesce_pair
52 int first_element;
53 int second_element;
54 int cost;
56 /* A count of the number of unique partitions this pair would conflict
57 with if coalescing was successful. This is the secondary sort key,
58 given two pairs with equal costs, we will prefer the pair with a smaller
59 conflict set.
61 This is lazily initialized when we discover two coalescing pairs have
62 the same primary cost.
64 Note this is not updated and propagated as pairs are coalesced. */
65 int conflict_count;
67 /* The order in which coalescing pairs are discovered is recorded in this
68 field, which is used as the final tie breaker when sorting coalesce
69 pairs. */
70 int index;
73 /* This represents a conflict graph. Implemented as an array of bitmaps.
74 A full matrix is used for conflicts rather than just upper triangular form.
75 this makes it much simpler and faster to perform conflict merges. */
77 struct ssa_conflicts
79 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
80 vec<bitmap> conflicts;
83 /* The narrow API of the qsort comparison function doesn't allow easy
84 access to additional arguments. So we have two globals (ick) to hold
85 the data we need. They're initialized before the call to qsort and
86 wiped immediately after. */
87 static ssa_conflicts *conflicts_;
88 static var_map map_;
90 /* Coalesce pair hashtable helpers. */
92 struct coalesce_pair_hasher : nofree_ptr_hash <coalesce_pair>
94 static inline hashval_t hash (const coalesce_pair *);
95 static inline bool equal (const coalesce_pair *, const coalesce_pair *);
98 /* Hash function for coalesce list. Calculate hash for PAIR. */
100 inline hashval_t
101 coalesce_pair_hasher::hash (const coalesce_pair *pair)
103 hashval_t a = (hashval_t)(pair->first_element);
104 hashval_t b = (hashval_t)(pair->second_element);
106 return b * (b - 1) / 2 + a;
109 /* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
110 returning TRUE if the two pairs are equivalent. */
112 inline bool
113 coalesce_pair_hasher::equal (const coalesce_pair *p1, const coalesce_pair *p2)
115 return (p1->first_element == p2->first_element
116 && p1->second_element == p2->second_element);
119 typedef hash_table<coalesce_pair_hasher> coalesce_table_type;
120 typedef coalesce_table_type::iterator coalesce_iterator_type;
123 struct cost_one_pair
125 int first_element;
126 int second_element;
127 cost_one_pair *next;
130 /* This structure maintains the list of coalesce pairs. */
132 struct coalesce_list
134 coalesce_table_type *list; /* Hash table. */
135 coalesce_pair **sorted; /* List when sorted. */
136 int num_sorted; /* Number in the sorted list. */
137 cost_one_pair *cost_one_list;/* Single use coalesces with cost 1. */
140 #define NO_BEST_COALESCE -1
141 #define MUST_COALESCE_COST INT_MAX
144 /* Return cost of execution of copy instruction with FREQUENCY. */
146 static inline int
147 coalesce_cost (int frequency, bool optimize_for_size)
149 /* Base costs on BB frequencies bounded by 1. */
150 int cost = frequency;
152 if (!cost)
153 cost = 1;
155 if (optimize_for_size)
156 cost = 1;
158 return cost;
162 /* Return the cost of executing a copy instruction in basic block BB. */
164 static inline int
165 coalesce_cost_bb (basic_block bb)
167 return coalesce_cost (bb->count.to_frequency (cfun),
168 optimize_bb_for_size_p (bb));
172 /* Return the cost of executing a copy instruction on edge E. */
174 static inline int
175 coalesce_cost_edge (edge e)
177 int mult = 1;
179 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
180 if (EDGE_CRITICAL_P (e))
181 mult = 2;
182 if (e->flags & EDGE_ABNORMAL)
183 return MUST_COALESCE_COST;
184 if (e->flags & EDGE_EH)
186 edge e2;
187 edge_iterator ei;
188 FOR_EACH_EDGE (e2, ei, e->dest->preds)
189 if (e2 != e)
191 /* Putting code on EH edge that leads to BB
192 with multiple predecestors imply splitting of
193 edge too. */
194 if (mult < 2)
195 mult = 2;
196 /* If there are multiple EH predecestors, we
197 also copy EH regions and produce separate
198 landing pad. This is expensive. */
199 if (e2->flags & EDGE_EH)
201 mult = 5;
202 break;
207 return coalesce_cost (EDGE_FREQUENCY (e),
208 optimize_edge_for_size_p (e)) * mult;
212 /* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
213 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
214 NO_BEST_COALESCE is returned if there aren't any. */
216 static inline int
217 pop_cost_one_pair (coalesce_list *cl, int *p1, int *p2)
219 cost_one_pair *ptr;
221 ptr = cl->cost_one_list;
222 if (!ptr)
223 return NO_BEST_COALESCE;
225 *p1 = ptr->first_element;
226 *p2 = ptr->second_element;
227 cl->cost_one_list = ptr->next;
229 free (ptr);
231 return 1;
234 /* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
235 2 elements via P1 and P2. Their calculated cost is returned by the function.
236 NO_BEST_COALESCE is returned if the coalesce list is empty. */
238 static inline int
239 pop_best_coalesce (coalesce_list *cl, int *p1, int *p2)
241 coalesce_pair *node;
242 int ret;
244 if (cl->sorted == NULL)
245 return pop_cost_one_pair (cl, p1, p2);
247 if (cl->num_sorted == 0)
248 return pop_cost_one_pair (cl, p1, p2);
250 node = cl->sorted[--(cl->num_sorted)];
251 *p1 = node->first_element;
252 *p2 = node->second_element;
253 ret = node->cost;
254 free (node);
256 return ret;
260 /* Create a new empty coalesce list object and return it. */
262 static inline coalesce_list *
263 create_coalesce_list (void)
265 coalesce_list *list;
266 unsigned size = num_ssa_names * 3;
268 if (size < 40)
269 size = 40;
271 list = (coalesce_list *) xmalloc (sizeof (struct coalesce_list));
272 list->list = new coalesce_table_type (size);
273 list->sorted = NULL;
274 list->num_sorted = 0;
275 list->cost_one_list = NULL;
276 return list;
280 /* Delete coalesce list CL. */
282 static inline void
283 delete_coalesce_list (coalesce_list *cl)
285 gcc_assert (cl->cost_one_list == NULL);
286 delete cl->list;
287 cl->list = NULL;
288 free (cl->sorted);
289 gcc_assert (cl->num_sorted == 0);
290 free (cl);
293 /* Return the number of unique coalesce pairs in CL. */
295 static inline int
296 num_coalesce_pairs (coalesce_list *cl)
298 return cl->list->elements ();
301 /* Find a matching coalesce pair object in CL for the pair P1 and P2. If
302 one isn't found, return NULL if CREATE is false, otherwise create a new
303 coalesce pair object and return it. */
305 static coalesce_pair *
306 find_coalesce_pair (coalesce_list *cl, int p1, int p2, bool create)
308 struct coalesce_pair p;
309 coalesce_pair **slot;
310 unsigned int hash;
312 /* Normalize so that p1 is the smaller value. */
313 if (p2 < p1)
315 p.first_element = p2;
316 p.second_element = p1;
318 else
320 p.first_element = p1;
321 p.second_element = p2;
324 hash = coalesce_pair_hasher::hash (&p);
325 slot = cl->list->find_slot_with_hash (&p, hash, create ? INSERT : NO_INSERT);
326 if (!slot)
327 return NULL;
329 if (!*slot)
331 struct coalesce_pair * pair = XNEW (struct coalesce_pair);
332 gcc_assert (cl->sorted == NULL);
333 pair->first_element = p.first_element;
334 pair->second_element = p.second_element;
335 pair->cost = 0;
336 pair->index = num_coalesce_pairs (cl);
337 pair->conflict_count = 0;
338 *slot = pair;
341 return (struct coalesce_pair *) *slot;
344 static inline void
345 add_cost_one_coalesce (coalesce_list *cl, int p1, int p2)
347 cost_one_pair *pair;
349 pair = XNEW (cost_one_pair);
350 pair->first_element = p1;
351 pair->second_element = p2;
352 pair->next = cl->cost_one_list;
353 cl->cost_one_list = pair;
357 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
359 static inline void
360 add_coalesce (coalesce_list *cl, int p1, int p2, int value)
362 coalesce_pair *node;
364 gcc_assert (cl->sorted == NULL);
365 if (p1 == p2)
366 return;
368 node = find_coalesce_pair (cl, p1, p2, true);
370 /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way. */
371 if (node->cost < MUST_COALESCE_COST - 1)
373 if (value < MUST_COALESCE_COST - 1)
374 node->cost += value;
375 else
376 node->cost = value;
380 /* Compute and record how many unique conflicts would exist for the
381 representative partition for each coalesce pair in CL.
383 CONFLICTS is the conflict graph and MAP is the current partition view. */
385 static void
386 initialize_conflict_count (coalesce_pair *p,
387 ssa_conflicts *conflicts,
388 var_map map)
390 int p1 = var_to_partition (map, ssa_name (p->first_element));
391 int p2 = var_to_partition (map, ssa_name (p->second_element));
393 /* 4 cases. If both P1 and P2 have conflicts, then build their
394 union and count the members. Else handle the degenerate cases
395 in the obvious ways. */
396 if (conflicts->conflicts[p1] && conflicts->conflicts[p2])
397 p->conflict_count = bitmap_count_unique_bits (conflicts->conflicts[p1],
398 conflicts->conflicts[p2]);
399 else if (conflicts->conflicts[p1])
400 p->conflict_count = bitmap_count_bits (conflicts->conflicts[p1]);
401 else if (conflicts->conflicts[p2])
402 p->conflict_count = bitmap_count_bits (conflicts->conflicts[p2]);
403 else
404 p->conflict_count = 0;
408 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
410 static int
411 compare_pairs (const void *p1, const void *p2)
413 coalesce_pair *const *const pp1 = (coalesce_pair *const *) p1;
414 coalesce_pair *const *const pp2 = (coalesce_pair *const *) p2;
415 int result;
417 result = (* pp1)->cost - (* pp2)->cost;
418 /* We use the size of the resulting conflict set as the secondary sort key.
419 Given two equal costing coalesce pairs, we want to prefer the pair that
420 has the smaller conflict set. */
421 if (result == 0)
423 if (flag_expensive_optimizations)
425 /* Lazily initialize the conflict counts as it's fairly expensive
426 to compute. */
427 if ((*pp2)->conflict_count == 0)
428 initialize_conflict_count (*pp2, conflicts_, map_);
429 if ((*pp1)->conflict_count == 0)
430 initialize_conflict_count (*pp1, conflicts_, map_);
432 result = (*pp2)->conflict_count - (*pp1)->conflict_count;
435 /* And if everything else is equal, then sort based on which
436 coalesce pair was found first. */
437 if (result == 0)
438 result = (*pp2)->index - (*pp1)->index;
441 return result;
444 /* Iterate over CL using ITER, returning values in PAIR. */
446 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
447 FOR_EACH_HASH_TABLE_ELEMENT (*(CL)->list, (PAIR), coalesce_pair_p, (ITER))
450 /* Prepare CL for removal of preferred pairs. When finished they are sorted
451 in order from most important coalesce to least important. */
453 static void
454 sort_coalesce_list (coalesce_list *cl, ssa_conflicts *conflicts, var_map map)
456 unsigned x, num;
457 coalesce_pair *p;
458 coalesce_iterator_type ppi;
460 gcc_assert (cl->sorted == NULL);
462 num = num_coalesce_pairs (cl);
463 cl->num_sorted = num;
464 if (num == 0)
465 return;
467 /* Allocate a vector for the pair pointers. */
468 cl->sorted = XNEWVEC (coalesce_pair *, num);
470 /* Populate the vector with pointers to the pairs. */
471 x = 0;
472 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
473 cl->sorted[x++] = p;
474 gcc_assert (x == num);
476 /* Already sorted. */
477 if (num == 1)
478 return;
480 /* We don't want to depend on qsort_r, so we have to stuff away
481 additional data into globals so it can be referenced in
482 compare_pairs. */
483 conflicts_ = conflicts;
484 map_ = map;
485 qsort (cl->sorted, num, sizeof (coalesce_pair *), compare_pairs);
486 conflicts_ = NULL;
487 map_ = NULL;
491 /* Send debug info for coalesce list CL to file F. */
493 static void
494 dump_coalesce_list (FILE *f, coalesce_list *cl)
496 coalesce_pair *node;
497 coalesce_iterator_type ppi;
499 int x;
500 tree var;
502 if (cl->sorted == NULL)
504 fprintf (f, "Coalesce List:\n");
505 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
507 tree var1 = ssa_name (node->first_element);
508 tree var2 = ssa_name (node->second_element);
509 print_generic_expr (f, var1, TDF_SLIM);
510 fprintf (f, " <-> ");
511 print_generic_expr (f, var2, TDF_SLIM);
512 fprintf (f, " (%1d, %1d), ", node->cost, node->conflict_count);
513 fprintf (f, "\n");
516 else
518 fprintf (f, "Sorted Coalesce list:\n");
519 for (x = cl->num_sorted - 1 ; x >=0; x--)
521 node = cl->sorted[x];
522 fprintf (f, "(%d, %d) ", node->cost, node->conflict_count);
523 var = ssa_name (node->first_element);
524 print_generic_expr (f, var, TDF_SLIM);
525 fprintf (f, " <-> ");
526 var = ssa_name (node->second_element);
527 print_generic_expr (f, var, TDF_SLIM);
528 fprintf (f, "\n");
534 /* Return an empty new conflict graph for SIZE elements. */
536 static inline ssa_conflicts *
537 ssa_conflicts_new (unsigned size)
539 ssa_conflicts *ptr;
541 ptr = XNEW (ssa_conflicts);
542 bitmap_obstack_initialize (&ptr->obstack);
543 ptr->conflicts.create (size);
544 ptr->conflicts.safe_grow_cleared (size);
545 return ptr;
549 /* Free storage for conflict graph PTR. */
551 static inline void
552 ssa_conflicts_delete (ssa_conflicts *ptr)
554 bitmap_obstack_release (&ptr->obstack);
555 ptr->conflicts.release ();
556 free (ptr);
560 /* Test if elements X and Y conflict in graph PTR. */
562 static inline bool
563 ssa_conflicts_test_p (ssa_conflicts *ptr, unsigned x, unsigned y)
565 bitmap bx = ptr->conflicts[x];
566 bitmap by = ptr->conflicts[y];
568 gcc_checking_assert (x != y);
570 if (bx)
571 /* Avoid the lookup if Y has no conflicts. */
572 return by ? bitmap_bit_p (bx, y) : false;
573 else
574 return false;
578 /* Add a conflict with Y to the bitmap for X in graph PTR. */
580 static inline void
581 ssa_conflicts_add_one (ssa_conflicts *ptr, unsigned x, unsigned y)
583 bitmap bx = ptr->conflicts[x];
584 /* If there are no conflicts yet, allocate the bitmap and set bit. */
585 if (! bx)
586 bx = ptr->conflicts[x] = BITMAP_ALLOC (&ptr->obstack);
587 bitmap_set_bit (bx, y);
591 /* Add conflicts between X and Y in graph PTR. */
593 static inline void
594 ssa_conflicts_add (ssa_conflicts *ptr, unsigned x, unsigned y)
596 gcc_checking_assert (x != y);
597 ssa_conflicts_add_one (ptr, x, y);
598 ssa_conflicts_add_one (ptr, y, x);
602 /* Merge all Y's conflict into X in graph PTR. */
604 static inline void
605 ssa_conflicts_merge (ssa_conflicts *ptr, unsigned x, unsigned y)
607 unsigned z;
608 bitmap_iterator bi;
609 bitmap bx = ptr->conflicts[x];
610 bitmap by = ptr->conflicts[y];
612 gcc_checking_assert (x != y);
613 if (! by)
614 return;
616 /* Add a conflict between X and every one Y has. If the bitmap doesn't
617 exist, then it has already been coalesced, and we don't need to add a
618 conflict. */
619 EXECUTE_IF_SET_IN_BITMAP (by, 0, z, bi)
621 bitmap bz = ptr->conflicts[z];
622 if (bz)
623 bitmap_set_bit (bz, x);
626 if (bx)
628 /* If X has conflicts, add Y's to X. */
629 bitmap_ior_into (bx, by);
630 BITMAP_FREE (by);
631 ptr->conflicts[y] = NULL;
633 else
635 /* If X has no conflicts, simply use Y's. */
636 ptr->conflicts[x] = by;
637 ptr->conflicts[y] = NULL;
642 /* Dump a conflicts graph. */
644 static void
645 ssa_conflicts_dump (FILE *file, ssa_conflicts *ptr)
647 unsigned x;
648 bitmap b;
650 fprintf (file, "\nConflict graph:\n");
652 FOR_EACH_VEC_ELT (ptr->conflicts, x, b)
653 if (b)
655 fprintf (file, "%d: ", x);
656 dump_bitmap (file, b);
661 /* This structure is used to efficiently record the current status of live
662 SSA_NAMES when building a conflict graph.
663 LIVE_BASE_VAR has a bit set for each base variable which has at least one
664 ssa version live.
665 LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
666 index, and is used to track what partitions of each base variable are
667 live. This makes it easy to add conflicts between just live partitions
668 with the same base variable.
669 The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
670 marked as being live. This delays clearing of these bitmaps until
671 they are actually needed again. */
673 struct live_track
675 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
676 bitmap live_base_var; /* Indicates if a basevar is live. */
677 bitmap *live_base_partitions; /* Live partitions for each basevar. */
678 var_map map; /* Var_map being used for partition mapping. */
682 /* This routine will create a new live track structure based on the partitions
683 in MAP. */
685 static live_track *
686 new_live_track (var_map map)
688 live_track *ptr;
689 int lim, x;
691 /* Make sure there is a partition view in place. */
692 gcc_assert (map->partition_to_base_index != NULL);
694 ptr = (live_track *) xmalloc (sizeof (live_track));
695 ptr->map = map;
696 lim = num_basevars (map);
697 bitmap_obstack_initialize (&ptr->obstack);
698 ptr->live_base_partitions = (bitmap *) xmalloc (sizeof (bitmap *) * lim);
699 ptr->live_base_var = BITMAP_ALLOC (&ptr->obstack);
700 for (x = 0; x < lim; x++)
701 ptr->live_base_partitions[x] = BITMAP_ALLOC (&ptr->obstack);
702 return ptr;
706 /* This routine will free the memory associated with PTR. */
708 static void
709 delete_live_track (live_track *ptr)
711 bitmap_obstack_release (&ptr->obstack);
712 free (ptr->live_base_partitions);
713 free (ptr);
717 /* This function will remove PARTITION from the live list in PTR. */
719 static inline void
720 live_track_remove_partition (live_track *ptr, int partition)
722 int root;
724 root = basevar_index (ptr->map, partition);
725 bitmap_clear_bit (ptr->live_base_partitions[root], partition);
726 /* If the element list is empty, make the base variable not live either. */
727 if (bitmap_empty_p (ptr->live_base_partitions[root]))
728 bitmap_clear_bit (ptr->live_base_var, root);
732 /* This function will adds PARTITION to the live list in PTR. */
734 static inline void
735 live_track_add_partition (live_track *ptr, int partition)
737 int root;
739 root = basevar_index (ptr->map, partition);
740 /* If this base var wasn't live before, it is now. Clear the element list
741 since it was delayed until needed. */
742 if (bitmap_set_bit (ptr->live_base_var, root))
743 bitmap_clear (ptr->live_base_partitions[root]);
744 bitmap_set_bit (ptr->live_base_partitions[root], partition);
749 /* Clear the live bit for VAR in PTR. */
751 static inline void
752 live_track_clear_var (live_track *ptr, tree var)
754 int p;
756 p = var_to_partition (ptr->map, var);
757 if (p != NO_PARTITION)
758 live_track_remove_partition (ptr, p);
762 /* Return TRUE if VAR is live in PTR. */
764 static inline bool
765 live_track_live_p (live_track *ptr, tree var)
767 int p, root;
769 p = var_to_partition (ptr->map, var);
770 if (p != NO_PARTITION)
772 root = basevar_index (ptr->map, p);
773 if (bitmap_bit_p (ptr->live_base_var, root))
774 return bitmap_bit_p (ptr->live_base_partitions[root], p);
776 return false;
780 /* This routine will add USE to PTR. USE will be marked as live in both the
781 ssa live map and the live bitmap for the root of USE. */
783 static inline void
784 live_track_process_use (live_track *ptr, tree use)
786 int p;
788 p = var_to_partition (ptr->map, use);
789 if (p == NO_PARTITION)
790 return;
792 /* Mark as live in the appropriate live list. */
793 live_track_add_partition (ptr, p);
797 /* This routine will process a DEF in PTR. DEF will be removed from the live
798 lists, and if there are any other live partitions with the same base
799 variable, conflicts will be added to GRAPH. */
801 static inline void
802 live_track_process_def (live_track *ptr, tree def, ssa_conflicts *graph)
804 int p, root;
805 bitmap b;
806 unsigned x;
807 bitmap_iterator bi;
809 p = var_to_partition (ptr->map, def);
810 if (p == NO_PARTITION)
811 return;
813 /* Clear the liveness bit. */
814 live_track_remove_partition (ptr, p);
816 /* If the bitmap isn't empty now, conflicts need to be added. */
817 root = basevar_index (ptr->map, p);
818 if (bitmap_bit_p (ptr->live_base_var, root))
820 b = ptr->live_base_partitions[root];
821 EXECUTE_IF_SET_IN_BITMAP (b, 0, x, bi)
822 ssa_conflicts_add (graph, p, x);
827 /* Initialize PTR with the partitions set in INIT. */
829 static inline void
830 live_track_init (live_track *ptr, bitmap init)
832 unsigned p;
833 bitmap_iterator bi;
835 /* Mark all live on exit partitions. */
836 EXECUTE_IF_SET_IN_BITMAP (init, 0, p, bi)
837 live_track_add_partition (ptr, p);
841 /* This routine will clear all live partitions in PTR. */
843 static inline void
844 live_track_clear_base_vars (live_track *ptr)
846 /* Simply clear the live base list. Anything marked as live in the element
847 lists will be cleared later if/when the base variable ever comes alive
848 again. */
849 bitmap_clear (ptr->live_base_var);
853 /* Build a conflict graph based on LIVEINFO. Any partitions which are in the
854 partition view of the var_map liveinfo is based on get entries in the
855 conflict graph. Only conflicts between ssa_name partitions with the same
856 base variable are added. */
858 static ssa_conflicts *
859 build_ssa_conflict_graph (tree_live_info_p liveinfo)
861 ssa_conflicts *graph;
862 var_map map;
863 basic_block bb;
864 ssa_op_iter iter;
865 live_track *live;
866 basic_block entry;
868 /* If inter-variable coalescing is enabled, we may attempt to
869 coalesce variables from different base variables, including
870 different parameters, so we have to make sure default defs live
871 at the entry block conflict with each other. */
872 if (flag_tree_coalesce_vars)
873 entry = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
874 else
875 entry = NULL;
877 map = live_var_map (liveinfo);
878 graph = ssa_conflicts_new (num_var_partitions (map));
880 live = new_live_track (map);
882 for (unsigned i = 0; liveinfo->map->vec_bbs.iterate (i, &bb); ++i)
884 /* Start with live on exit temporaries. */
885 live_track_init (live, live_on_exit (liveinfo, bb));
887 for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);
888 gsi_prev (&gsi))
890 tree var;
891 gimple *stmt = gsi_stmt (gsi);
893 /* A copy between 2 partitions does not introduce an interference
894 by itself. If they did, you would never be able to coalesce
895 two things which are copied. If the two variables really do
896 conflict, they will conflict elsewhere in the program.
898 This is handled by simply removing the SRC of the copy from the
899 live list, and processing the stmt normally. */
900 if (is_gimple_assign (stmt))
902 tree lhs = gimple_assign_lhs (stmt);
903 tree rhs1 = gimple_assign_rhs1 (stmt);
904 if (gimple_assign_copy_p (stmt)
905 && TREE_CODE (lhs) == SSA_NAME
906 && TREE_CODE (rhs1) == SSA_NAME)
907 live_track_clear_var (live, rhs1);
909 else if (is_gimple_debug (stmt))
910 continue;
912 /* For stmts with more than one SSA_NAME definition pretend all the
913 SSA_NAME outputs but the first one are live at this point, so
914 that conflicts are added in between all those even when they are
915 actually not really live after the asm, because expansion might
916 copy those into pseudos after the asm and if multiple outputs
917 share the same partition, it might overwrite those that should
918 be live. E.g.
919 asm volatile (".." : "=r" (a) : "=r" (b) : "0" (a), "1" (a));
920 return a;
921 See PR70593. */
922 bool first = true;
923 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
924 if (first)
925 first = false;
926 else
927 live_track_process_use (live, var);
929 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
930 live_track_process_def (live, var, graph);
932 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
933 live_track_process_use (live, var);
936 /* If result of a PHI is unused, looping over the statements will not
937 record any conflicts since the def was never live. Since the PHI node
938 is going to be translated out of SSA form, it will insert a copy.
939 There must be a conflict recorded between the result of the PHI and
940 any variables that are live. Otherwise the out-of-ssa translation
941 may create incorrect code. */
942 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
943 gsi_next (&gsi))
945 gphi *phi = gsi.phi ();
946 tree result = PHI_RESULT (phi);
947 if (virtual_operand_p (result))
948 continue;
949 if (live_track_live_p (live, result))
950 live_track_process_def (live, result, graph);
953 /* Pretend there are defs for params' default defs at the start
954 of the (post-)entry block. This will prevent PARM_DECLs from
955 coalescing into the same partition. Although RESULT_DECLs'
956 default defs don't have a useful initial value, we have to
957 prevent them from coalescing with PARM_DECLs' default defs
958 too, otherwise assign_parms would attempt to assign different
959 RTL to the same partition. */
960 if (bb == entry)
962 unsigned i;
963 tree var;
965 FOR_EACH_SSA_NAME (i, var, cfun)
967 if (!SSA_NAME_IS_DEFAULT_DEF (var)
968 || !SSA_NAME_VAR (var)
969 || VAR_P (SSA_NAME_VAR (var)))
970 continue;
972 live_track_process_def (live, var, graph);
973 /* Process a use too, so that it remains live and
974 conflicts with other parms' default defs, even unused
975 ones. */
976 live_track_process_use (live, var);
980 live_track_clear_base_vars (live);
983 delete_live_track (live);
984 return graph;
988 /* Shortcut routine to print messages to file F of the form:
989 "STR1 EXPR1 STR2 EXPR2 STR3." */
991 static inline void
992 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
993 tree expr2, const char *str3)
995 fprintf (f, "%s", str1);
996 print_generic_expr (f, expr1, TDF_SLIM);
997 fprintf (f, "%s", str2);
998 print_generic_expr (f, expr2, TDF_SLIM);
999 fprintf (f, "%s", str3);
1003 /* Print a failure to coalesce a MUST_COALESCE pair X and Y. */
1005 static inline void
1006 fail_abnormal_edge_coalesce (int x, int y)
1008 fprintf (stderr, "\nUnable to coalesce ssa_names %d and %d",x, y);
1009 fprintf (stderr, " which are marked as MUST COALESCE.\n");
1010 print_generic_expr (stderr, ssa_name (x), TDF_SLIM);
1011 fprintf (stderr, " and ");
1012 print_generic_stmt (stderr, ssa_name (y), TDF_SLIM);
1014 internal_error ("SSA corruption");
1017 /* If VAR is an SSA_NAME associated with a PARM_DECL or a RESULT_DECL,
1018 and the DECL's default def is unused (i.e., it was introduced by
1019 create_default_def for out-of-ssa), mark VAR and the default def for
1020 coalescing. */
1022 static void
1023 coalesce_with_default (tree var, coalesce_list *cl, bitmap used_in_copy)
1025 if (SSA_NAME_IS_DEFAULT_DEF (var)
1026 || !SSA_NAME_VAR (var)
1027 || VAR_P (SSA_NAME_VAR (var)))
1028 return;
1030 tree ssa = ssa_default_def (cfun, SSA_NAME_VAR (var));
1031 if (!has_zero_uses (ssa))
1032 return;
1034 add_cost_one_coalesce (cl, SSA_NAME_VERSION (ssa), SSA_NAME_VERSION (var));
1035 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1036 /* Default defs will have their used_in_copy bits set at the beginning of
1037 populate_coalesce_list_for_outofssa. */
1041 /* Given var_map MAP for a region, this function creates and returns a coalesce
1042 list as well as recording related ssa names in USED_IN_COPIES for use later
1043 in the out-of-ssa or live range computation process. */
1045 static coalesce_list *
1046 create_coalesce_list_for_region (var_map map, bitmap used_in_copy)
1048 gimple_stmt_iterator gsi;
1049 basic_block bb;
1050 coalesce_list *cl = create_coalesce_list ();
1051 gimple *stmt;
1052 int v1, v2, cost;
1054 for (unsigned j = 0; map->vec_bbs.iterate (j, &bb); ++j)
1056 tree arg;
1058 for (gphi_iterator gpi = gsi_start_phis (bb);
1059 !gsi_end_p (gpi);
1060 gsi_next (&gpi))
1062 gphi *phi = gpi.phi ();
1063 size_t i;
1064 int ver;
1065 tree res;
1066 bool saw_copy = false;
1068 res = gimple_phi_result (phi);
1069 if (virtual_operand_p (res))
1070 continue;
1071 ver = SSA_NAME_VERSION (res);
1073 /* Register ssa_names and coalesces between the args and the result
1074 of all PHI. */
1075 for (i = 0; i < gimple_phi_num_args (phi); i++)
1077 edge e = gimple_phi_arg_edge (phi, i);
1078 arg = PHI_ARG_DEF (phi, i);
1079 if (TREE_CODE (arg) != SSA_NAME)
1080 continue;
1082 if (gimple_can_coalesce_p (arg, res)
1083 || (e->flags & EDGE_ABNORMAL))
1085 saw_copy = true;
1086 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (arg));
1087 if ((e->flags & EDGE_ABNORMAL) == 0)
1089 int cost = coalesce_cost_edge (e);
1090 if (cost == 1 && has_single_use (arg))
1091 add_cost_one_coalesce (cl, ver, SSA_NAME_VERSION (arg));
1092 else
1093 add_coalesce (cl, ver, SSA_NAME_VERSION (arg), cost);
1097 if (saw_copy)
1098 bitmap_set_bit (used_in_copy, ver);
1101 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1103 stmt = gsi_stmt (gsi);
1105 if (is_gimple_debug (stmt))
1106 continue;
1108 /* Check for copy coalesces. */
1109 switch (gimple_code (stmt))
1111 case GIMPLE_ASSIGN:
1113 tree lhs = gimple_assign_lhs (stmt);
1114 tree rhs1 = gimple_assign_rhs1 (stmt);
1115 if (gimple_assign_ssa_name_copy_p (stmt)
1116 && gimple_can_coalesce_p (lhs, rhs1))
1118 v1 = SSA_NAME_VERSION (lhs);
1119 v2 = SSA_NAME_VERSION (rhs1);
1120 cost = coalesce_cost_bb (bb);
1121 add_coalesce (cl, v1, v2, cost);
1122 bitmap_set_bit (used_in_copy, v1);
1123 bitmap_set_bit (used_in_copy, v2);
1126 break;
1128 case GIMPLE_RETURN:
1130 tree res = DECL_RESULT (current_function_decl);
1131 if (VOID_TYPE_P (TREE_TYPE (res))
1132 || !is_gimple_reg (res))
1133 break;
1134 tree rhs1 = gimple_return_retval (as_a <greturn *> (stmt));
1135 if (!rhs1)
1136 break;
1137 tree lhs = ssa_default_def (cfun, res);
1138 gcc_assert (lhs);
1139 if (TREE_CODE (rhs1) == SSA_NAME
1140 && gimple_can_coalesce_p (lhs, rhs1))
1142 v1 = SSA_NAME_VERSION (lhs);
1143 v2 = SSA_NAME_VERSION (rhs1);
1144 cost = coalesce_cost_bb (bb);
1145 add_coalesce (cl, v1, v2, cost);
1146 bitmap_set_bit (used_in_copy, v1);
1147 bitmap_set_bit (used_in_copy, v2);
1149 break;
1152 case GIMPLE_ASM:
1154 gasm *asm_stmt = as_a <gasm *> (stmt);
1155 unsigned long noutputs, i;
1156 unsigned long ninputs;
1157 tree *outputs, link;
1158 noutputs = gimple_asm_noutputs (asm_stmt);
1159 ninputs = gimple_asm_ninputs (asm_stmt);
1160 outputs = (tree *) alloca (noutputs * sizeof (tree));
1161 for (i = 0; i < noutputs; ++i)
1163 link = gimple_asm_output_op (asm_stmt, i);
1164 outputs[i] = TREE_VALUE (link);
1167 for (i = 0; i < ninputs; ++i)
1169 const char *constraint;
1170 tree input;
1171 char *end;
1172 unsigned long match;
1174 link = gimple_asm_input_op (asm_stmt, i);
1175 constraint
1176 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1177 input = TREE_VALUE (link);
1179 if (TREE_CODE (input) != SSA_NAME)
1180 continue;
1182 match = strtoul (constraint, &end, 10);
1183 if (match >= noutputs || end == constraint)
1184 continue;
1186 if (TREE_CODE (outputs[match]) != SSA_NAME)
1187 continue;
1189 v1 = SSA_NAME_VERSION (outputs[match]);
1190 v2 = SSA_NAME_VERSION (input);
1192 if (gimple_can_coalesce_p (outputs[match], input))
1194 cost = coalesce_cost (REG_BR_PROB_BASE,
1195 optimize_bb_for_size_p (bb));
1196 add_coalesce (cl, v1, v2, cost);
1197 bitmap_set_bit (used_in_copy, v1);
1198 bitmap_set_bit (used_in_copy, v2);
1201 break;
1204 default:
1205 break;
1210 return cl;
1214 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1216 struct ssa_name_var_hash : nofree_ptr_hash <tree_node>
1218 static inline hashval_t hash (const tree_node *);
1219 static inline int equal (const tree_node *, const tree_node *);
1222 inline hashval_t
1223 ssa_name_var_hash::hash (const_tree n)
1225 return DECL_UID (SSA_NAME_VAR (n));
1228 inline int
1229 ssa_name_var_hash::equal (const tree_node *n1, const tree_node *n2)
1231 return SSA_NAME_VAR (n1) == SSA_NAME_VAR (n2);
1235 /* This function populates coalesce list CL as well as recording related ssa
1236 names in USED_IN_COPIES for use later in the out-of-ssa process. */
1238 static void
1239 populate_coalesce_list_for_outofssa (coalesce_list *cl, bitmap used_in_copy)
1241 tree var;
1242 tree first;
1243 int v1, v2, cost;
1244 unsigned i;
1246 /* Process result decls and live on entry variables for entry into the
1247 coalesce list. */
1248 first = NULL_TREE;
1249 FOR_EACH_SSA_NAME (i, var, cfun)
1251 if (!virtual_operand_p (var))
1253 coalesce_with_default (var, cl, used_in_copy);
1255 /* Add coalesces between all the result decls. */
1256 if (SSA_NAME_VAR (var)
1257 && TREE_CODE (SSA_NAME_VAR (var)) == RESULT_DECL)
1259 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1260 if (first == NULL_TREE)
1261 first = var;
1262 else
1264 gcc_assert (gimple_can_coalesce_p (var, first));
1265 v1 = SSA_NAME_VERSION (first);
1266 v2 = SSA_NAME_VERSION (var);
1267 cost = coalesce_cost_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
1268 add_coalesce (cl, v1, v2, cost);
1271 /* Mark any default_def variables as being in the coalesce list
1272 since they will have to be coalesced with the base variable. If
1273 not marked as present, they won't be in the coalesce view. */
1274 if (SSA_NAME_IS_DEFAULT_DEF (var)
1275 && (!has_zero_uses (var)
1276 || (SSA_NAME_VAR (var)
1277 && !VAR_P (SSA_NAME_VAR (var)))))
1278 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1282 /* If this optimization is disabled, we need to coalesce all the
1283 names originating from the same SSA_NAME_VAR so debug info
1284 remains undisturbed. */
1285 if (!flag_tree_coalesce_vars)
1287 tree a;
1288 hash_table<ssa_name_var_hash> ssa_name_hash (10);
1290 FOR_EACH_SSA_NAME (i, a, cfun)
1292 if (SSA_NAME_VAR (a)
1293 && !DECL_IGNORED_P (SSA_NAME_VAR (a))
1294 && (!has_zero_uses (a) || !SSA_NAME_IS_DEFAULT_DEF (a)
1295 || !VAR_P (SSA_NAME_VAR (a))))
1297 tree *slot = ssa_name_hash.find_slot (a, INSERT);
1299 if (!*slot)
1300 *slot = a;
1301 else
1303 /* If the variable is a PARM_DECL or a RESULT_DECL, we
1304 _require_ that all the names originating from it be
1305 coalesced, because there must be a single partition
1306 containing all the names so that it can be assigned
1307 the canonical RTL location of the DECL safely.
1308 If in_lto_p, a function could have been compiled
1309 originally with optimizations and only the link
1310 performed at -O0, so we can't actually require it. */
1311 const int cost
1312 = (TREE_CODE (SSA_NAME_VAR (a)) == VAR_DECL || in_lto_p)
1313 ? MUST_COALESCE_COST - 1 : MUST_COALESCE_COST;
1314 add_coalesce (cl, SSA_NAME_VERSION (a),
1315 SSA_NAME_VERSION (*slot), cost);
1316 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (a));
1317 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (*slot));
1325 /* Attempt to coalesce ssa versions X and Y together using the partition
1326 mapping in MAP and checking conflicts in GRAPH. Output any debug info to
1327 DEBUG, if it is nun-NULL. */
1329 static inline bool
1330 attempt_coalesce (var_map map, ssa_conflicts *graph, int x, int y,
1331 FILE *debug)
1333 int z;
1334 tree var1, var2;
1335 int p1, p2;
1337 p1 = var_to_partition (map, ssa_name (x));
1338 p2 = var_to_partition (map, ssa_name (y));
1340 if (debug)
1342 fprintf (debug, "(%d)", x);
1343 print_generic_expr (debug, partition_to_var (map, p1), TDF_SLIM);
1344 fprintf (debug, " & (%d)", y);
1345 print_generic_expr (debug, partition_to_var (map, p2), TDF_SLIM);
1348 if (p1 == p2)
1350 if (debug)
1351 fprintf (debug, ": Already Coalesced.\n");
1352 return true;
1355 if (debug)
1356 fprintf (debug, " [map: %d, %d] ", p1, p2);
1359 if (!ssa_conflicts_test_p (graph, p1, p2))
1361 var1 = partition_to_var (map, p1);
1362 var2 = partition_to_var (map, p2);
1364 z = var_union (map, var1, var2);
1365 if (z == NO_PARTITION)
1367 if (debug)
1368 fprintf (debug, ": Unable to perform partition union.\n");
1369 return false;
1372 /* z is the new combined partition. Remove the other partition from
1373 the list, and merge the conflicts. */
1374 if (z == p1)
1375 ssa_conflicts_merge (graph, p1, p2);
1376 else
1377 ssa_conflicts_merge (graph, p2, p1);
1379 if (debug)
1380 fprintf (debug, ": Success -> %d\n", z);
1382 return true;
1385 if (debug)
1386 fprintf (debug, ": Fail due to conflict\n");
1388 return false;
1392 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1393 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1395 static void
1396 coalesce_partitions (var_map map, ssa_conflicts *graph, coalesce_list *cl,
1397 FILE *debug)
1399 int x = 0, y = 0;
1400 tree var1, var2;
1401 int cost;
1402 basic_block bb;
1403 edge e;
1404 edge_iterator ei;
1406 /* First, coalesce all the copies across abnormal edges. These are not placed
1407 in the coalesce list because they do not need to be sorted, and simply
1408 consume extra memory/compilation time in large programs. */
1410 FOR_EACH_BB_FN (bb, cfun)
1412 FOR_EACH_EDGE (e, ei, bb->preds)
1413 if (e->flags & EDGE_ABNORMAL)
1415 gphi_iterator gsi;
1416 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1417 gsi_next (&gsi))
1419 gphi *phi = gsi.phi ();
1420 tree res = PHI_RESULT (phi);
1421 if (virtual_operand_p (res))
1422 continue;
1423 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1424 if (SSA_NAME_IS_DEFAULT_DEF (arg)
1425 && (!SSA_NAME_VAR (arg)
1426 || TREE_CODE (SSA_NAME_VAR (arg)) != PARM_DECL))
1427 continue;
1429 int v1 = SSA_NAME_VERSION (res);
1430 int v2 = SSA_NAME_VERSION (arg);
1432 if (debug)
1433 fprintf (debug, "Abnormal coalesce: ");
1435 if (!attempt_coalesce (map, graph, v1, v2, debug))
1436 fail_abnormal_edge_coalesce (v1, v2);
1441 /* Now process the items in the coalesce list. */
1443 while ((cost = pop_best_coalesce (cl, &x, &y)) != NO_BEST_COALESCE)
1445 var1 = ssa_name (x);
1446 var2 = ssa_name (y);
1448 /* Assert the coalesces have the same base variable. */
1449 gcc_assert (gimple_can_coalesce_p (var1, var2));
1451 if (debug)
1452 fprintf (debug, "Coalesce list: ");
1453 attempt_coalesce (map, graph, x, y, debug);
1458 /* Output partition map MAP with coalescing plan PART to file F. */
1460 void
1461 dump_part_var_map (FILE *f, partition part, var_map map)
1463 int t;
1464 unsigned x, y;
1465 int p;
1467 fprintf (f, "\nCoalescible Partition map \n\n");
1469 for (x = 0; x < map->num_partitions; x++)
1471 if (map->view_to_partition != NULL)
1472 p = map->view_to_partition[x];
1473 else
1474 p = x;
1476 if (ssa_name (p) == NULL_TREE
1477 || virtual_operand_p (ssa_name (p)))
1478 continue;
1480 t = 0;
1481 for (y = 1; y < num_ssa_names; y++)
1483 tree var = version_to_var (map, y);
1484 if (!var)
1485 continue;
1486 int q = var_to_partition (map, var);
1487 p = partition_find (part, q);
1488 gcc_assert (map->partition_to_base_index[q]
1489 == map->partition_to_base_index[p]);
1491 if (p == (int)x)
1493 if (t++ == 0)
1495 fprintf (f, "Partition %d, base %d (", x,
1496 map->partition_to_base_index[q]);
1497 print_generic_expr (f, partition_to_var (map, q), TDF_SLIM);
1498 fprintf (f, " - ");
1500 fprintf (f, "%d ", y);
1503 if (t != 0)
1504 fprintf (f, ")\n");
1506 fprintf (f, "\n");
1509 /* Given SSA_NAMEs NAME1 and NAME2, return true if they are candidates for
1510 coalescing together, false otherwise.
1512 This must stay consistent with compute_samebase_partition_bases and
1513 compute_optimized_partition_bases. */
1515 bool
1516 gimple_can_coalesce_p (tree name1, tree name2)
1518 /* First check the SSA_NAME's associated DECL. Without
1519 optimization, we only want to coalesce if they have the same DECL
1520 or both have no associated DECL. */
1521 tree var1 = SSA_NAME_VAR (name1);
1522 tree var2 = SSA_NAME_VAR (name2);
1523 var1 = (var1 && (!VAR_P (var1) || !DECL_IGNORED_P (var1))) ? var1 : NULL_TREE;
1524 var2 = (var2 && (!VAR_P (var2) || !DECL_IGNORED_P (var2))) ? var2 : NULL_TREE;
1525 if (var1 != var2 && !flag_tree_coalesce_vars)
1526 return false;
1528 /* Now check the types. If the types are the same, then we should
1529 try to coalesce V1 and V2. */
1530 tree t1 = TREE_TYPE (name1);
1531 tree t2 = TREE_TYPE (name2);
1532 if (t1 == t2)
1534 check_modes:
1535 /* If the base variables are the same, we're good: none of the
1536 other tests below could possibly fail. */
1537 var1 = SSA_NAME_VAR (name1);
1538 var2 = SSA_NAME_VAR (name2);
1539 if (var1 == var2)
1540 return true;
1542 /* We don't want to coalesce two SSA names if one of the base
1543 variables is supposed to be a register while the other is
1544 supposed to be on the stack. Anonymous SSA names most often
1545 take registers, but when not optimizing, user variables
1546 should go on the stack, so coalescing them with the anonymous
1547 variable as the partition leader would end up assigning the
1548 user variable to a register. Don't do that! */
1549 bool reg1 = use_register_for_decl (name1);
1550 bool reg2 = use_register_for_decl (name2);
1551 if (reg1 != reg2)
1552 return false;
1554 /* Check that the promoted modes and unsignedness are the same.
1555 We don't want to coalesce if the promoted modes would be
1556 different, or if they would sign-extend differently. Only
1557 PARM_DECLs and RESULT_DECLs have different promotion rules,
1558 so skip the test if both are variables, or both are anonymous
1559 SSA_NAMEs. */
1560 int unsigned1, unsigned2;
1561 return ((!var1 || VAR_P (var1)) && (!var2 || VAR_P (var2)))
1562 || ((promote_ssa_mode (name1, &unsigned1)
1563 == promote_ssa_mode (name2, &unsigned2))
1564 && unsigned1 == unsigned2);
1567 /* If alignment requirements are different, we can't coalesce. */
1568 if (MINIMUM_ALIGNMENT (t1,
1569 var1 ? DECL_MODE (var1) : TYPE_MODE (t1),
1570 var1 ? LOCAL_DECL_ALIGNMENT (var1) : TYPE_ALIGN (t1))
1571 != MINIMUM_ALIGNMENT (t2,
1572 var2 ? DECL_MODE (var2) : TYPE_MODE (t2),
1573 var2 ? LOCAL_DECL_ALIGNMENT (var2) : TYPE_ALIGN (t2)))
1574 return false;
1576 /* If the types are not the same, see whether they are compatible. This
1577 (for example) allows coalescing when the types are fundamentally the
1578 same, but just have different names.
1580 In the non-optimized case, we must first test TYPE_CANONICAL because
1581 we use it to compute the partition_to_base_index of the map. */
1582 if (flag_tree_coalesce_vars)
1584 if (types_compatible_p (t1, t2))
1585 goto check_modes;
1587 else
1589 if (TYPE_CANONICAL (t1)
1590 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)
1591 && types_compatible_p (t1, t2))
1592 goto check_modes;
1595 return false;
1598 /* Fill in MAP's partition_to_base_index, with one index for each
1599 partition of SSA names USED_IN_COPIES and related by CL coalesce
1600 possibilities. This must match gimple_can_coalesce_p in the
1601 optimized case. */
1603 static void
1604 compute_optimized_partition_bases (var_map map, bitmap used_in_copies,
1605 coalesce_list *cl)
1607 int parts = num_var_partitions (map);
1608 partition tentative = partition_new (parts);
1610 /* Partition the SSA versions so that, for each coalescible
1611 pair, both of its members are in the same partition in
1612 TENTATIVE. */
1613 gcc_assert (!cl->sorted);
1614 coalesce_pair *node;
1615 coalesce_iterator_type ppi;
1616 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
1618 tree v1 = ssa_name (node->first_element);
1619 int p1 = partition_find (tentative, var_to_partition (map, v1));
1620 tree v2 = ssa_name (node->second_element);
1621 int p2 = partition_find (tentative, var_to_partition (map, v2));
1623 if (p1 == p2)
1624 continue;
1626 partition_union (tentative, p1, p2);
1629 /* We have to deal with cost one pairs too. */
1630 for (cost_one_pair *co = cl->cost_one_list; co; co = co->next)
1632 tree v1 = ssa_name (co->first_element);
1633 int p1 = partition_find (tentative, var_to_partition (map, v1));
1634 tree v2 = ssa_name (co->second_element);
1635 int p2 = partition_find (tentative, var_to_partition (map, v2));
1637 if (p1 == p2)
1638 continue;
1640 partition_union (tentative, p1, p2);
1643 /* And also with abnormal edges. */
1644 basic_block bb;
1645 edge e;
1646 unsigned i;
1647 edge_iterator ei;
1648 for (i = 0; map->vec_bbs.iterate (i, &bb); ++i)
1650 FOR_EACH_EDGE (e, ei, bb->preds)
1651 if (e->flags & EDGE_ABNORMAL)
1653 gphi_iterator gsi;
1654 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1655 gsi_next (&gsi))
1657 gphi *phi = gsi.phi ();
1658 tree res = PHI_RESULT (phi);
1659 if (virtual_operand_p (res))
1660 continue;
1661 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1662 if (SSA_NAME_IS_DEFAULT_DEF (arg)
1663 && (!SSA_NAME_VAR (arg)
1664 || TREE_CODE (SSA_NAME_VAR (arg)) != PARM_DECL))
1665 continue;
1667 int p1 = partition_find (tentative, var_to_partition (map, res));
1668 int p2 = partition_find (tentative, var_to_partition (map, arg));
1670 if (p1 == p2)
1671 continue;
1673 partition_union (tentative, p1, p2);
1678 map->partition_to_base_index = XCNEWVEC (int, parts);
1679 auto_vec<unsigned int> index_map (parts);
1680 if (parts)
1681 index_map.quick_grow (parts);
1683 const unsigned no_part = -1;
1684 unsigned count = parts;
1685 while (count)
1686 index_map[--count] = no_part;
1688 /* Initialize MAP's mapping from partition to base index, using
1689 as base indices an enumeration of the TENTATIVE partitions in
1690 which each SSA version ended up, so that we compute conflicts
1691 between all SSA versions that ended up in the same potential
1692 coalesce partition. */
1693 bitmap_iterator bi;
1694 EXECUTE_IF_SET_IN_BITMAP (used_in_copies, 0, i, bi)
1696 int pidx = var_to_partition (map, ssa_name (i));
1697 int base = partition_find (tentative, pidx);
1698 if (index_map[base] != no_part)
1699 continue;
1700 index_map[base] = count++;
1703 map->num_basevars = count;
1705 EXECUTE_IF_SET_IN_BITMAP (used_in_copies, 0, i, bi)
1707 int pidx = var_to_partition (map, ssa_name (i));
1708 int base = partition_find (tentative, pidx);
1709 gcc_assert (index_map[base] < count);
1710 map->partition_to_base_index[pidx] = index_map[base];
1713 if (dump_file && (dump_flags & TDF_DETAILS))
1714 dump_part_var_map (dump_file, tentative, map);
1716 partition_delete (tentative);
1719 /* Hashtable helpers. */
1721 struct tree_int_map_hasher : nofree_ptr_hash <tree_int_map>
1723 static inline hashval_t hash (const tree_int_map *);
1724 static inline bool equal (const tree_int_map *, const tree_int_map *);
1727 inline hashval_t
1728 tree_int_map_hasher::hash (const tree_int_map *v)
1730 return tree_map_base_hash (v);
1733 inline bool
1734 tree_int_map_hasher::equal (const tree_int_map *v, const tree_int_map *c)
1736 return tree_int_map_eq (v, c);
1739 /* This routine will initialize the basevar fields of MAP with base
1740 names. Partitions will share the same base if they have the same
1741 SSA_NAME_VAR, or, being anonymous variables, the same type. This
1742 must match gimple_can_coalesce_p in the non-optimized case. */
1744 static void
1745 compute_samebase_partition_bases (var_map map)
1747 int x, num_part;
1748 tree var;
1749 struct tree_int_map *m, *mapstorage;
1751 num_part = num_var_partitions (map);
1752 hash_table<tree_int_map_hasher> tree_to_index (num_part);
1753 /* We can have at most num_part entries in the hash tables, so it's
1754 enough to allocate so many map elements once, saving some malloc
1755 calls. */
1756 mapstorage = m = XNEWVEC (struct tree_int_map, num_part);
1758 /* If a base table already exists, clear it, otherwise create it. */
1759 free (map->partition_to_base_index);
1760 map->partition_to_base_index = (int *) xmalloc (sizeof (int) * num_part);
1762 /* Build the base variable list, and point partitions at their bases. */
1763 for (x = 0; x < num_part; x++)
1765 struct tree_int_map **slot;
1766 unsigned baseindex;
1767 var = partition_to_var (map, x);
1768 if (SSA_NAME_VAR (var)
1769 && (!VAR_P (SSA_NAME_VAR (var))
1770 || !DECL_IGNORED_P (SSA_NAME_VAR (var))))
1771 m->base.from = SSA_NAME_VAR (var);
1772 else
1773 /* This restricts what anonymous SSA names we can coalesce
1774 as it restricts the sets we compute conflicts for.
1775 Using TREE_TYPE to generate sets is the easiest as
1776 type equivalency also holds for SSA names with the same
1777 underlying decl.
1779 Check gimple_can_coalesce_p when changing this code. */
1780 m->base.from = (TYPE_CANONICAL (TREE_TYPE (var))
1781 ? TYPE_CANONICAL (TREE_TYPE (var))
1782 : TREE_TYPE (var));
1783 /* If base variable hasn't been seen, set it up. */
1784 slot = tree_to_index.find_slot (m, INSERT);
1785 if (!*slot)
1787 baseindex = m - mapstorage;
1788 m->to = baseindex;
1789 *slot = m;
1790 m++;
1792 else
1793 baseindex = (*slot)->to;
1794 map->partition_to_base_index[x] = baseindex;
1797 map->num_basevars = m - mapstorage;
1799 free (mapstorage);
1802 /* Given an initial var_map MAP, coalesce variables and return a partition map
1803 with the resulting coalesce. Note that this function is called in either
1804 live range computation context or out-of-ssa context, indicated by MAP. */
1806 extern void
1807 coalesce_ssa_name (var_map map)
1809 tree_live_info_p liveinfo;
1810 ssa_conflicts *graph;
1811 coalesce_list *cl;
1812 auto_bitmap used_in_copies;
1814 cl = create_coalesce_list_for_region (map, used_in_copies);
1815 if (map->outofssa_p)
1816 populate_coalesce_list_for_outofssa (cl, used_in_copies);
1818 if (dump_file && (dump_flags & TDF_DETAILS))
1819 dump_var_map (dump_file, map);
1821 partition_view_bitmap (map, used_in_copies);
1823 if (flag_tree_coalesce_vars)
1824 compute_optimized_partition_bases (map, used_in_copies, cl);
1825 else
1826 compute_samebase_partition_bases (map);
1828 if (num_var_partitions (map) < 1)
1830 delete_coalesce_list (cl);
1831 return;
1834 if (dump_file && (dump_flags & TDF_DETAILS))
1835 dump_var_map (dump_file, map);
1837 liveinfo = calculate_live_ranges (map, false);
1839 if (dump_file && (dump_flags & TDF_DETAILS))
1840 dump_live_info (dump_file, liveinfo, LIVEDUMP_ENTRY);
1842 /* Build a conflict graph. */
1843 graph = build_ssa_conflict_graph (liveinfo);
1844 delete_tree_live_info (liveinfo);
1845 if (dump_file && (dump_flags & TDF_DETAILS))
1846 ssa_conflicts_dump (dump_file, graph);
1848 sort_coalesce_list (cl, graph, map);
1850 if (dump_file && (dump_flags & TDF_DETAILS))
1852 fprintf (dump_file, "\nAfter sorting:\n");
1853 dump_coalesce_list (dump_file, cl);
1856 /* First, coalesce all live on entry variables to their base variable.
1857 This will ensure the first use is coming from the correct location. */
1859 if (dump_file && (dump_flags & TDF_DETAILS))
1860 dump_var_map (dump_file, map);
1862 /* Now coalesce everything in the list. */
1863 coalesce_partitions (map, graph, cl,
1864 ((dump_flags & TDF_DETAILS) ? dump_file : NULL));
1866 delete_coalesce_list (cl);
1867 ssa_conflicts_delete (graph);