[PR64164] Drop copyrename, use coalescible partition as base when optimizing.
[official-gcc.git] / gcc / tree-ssa-coalesce.c
bloba6227285ba3b81d916616c051b92c33421a366d8
1 /* Coalesce SSA_NAMES together for the out-of-ssa pass.
2 Copyright (C) 2004-2015 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 "predict.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "hard-reg-set.h"
29 #include "ssa.h"
30 #include "alias.h"
31 #include "fold-const.h"
32 #include "flags.h"
33 #include "tree-pretty-print.h"
34 #include "dumpfile.h"
35 #include "internal-fn.h"
36 #include "gimple-iterator.h"
37 #include "tree-ssa-live.h"
38 #include "tree-ssa-coalesce.h"
39 #include "explow.h"
40 #include "diagnostic-core.h"
43 /* This set of routines implements a coalesce_list. This is an object which
44 is used to track pairs of ssa_names which are desirable to coalesce
45 together to avoid copies. Costs are associated with each pair, and when
46 all desired information has been collected, the object can be used to
47 order the pairs for processing. */
49 /* This structure defines a pair entry. */
51 typedef struct coalesce_pair
53 int first_element;
54 int second_element;
55 int cost;
56 } * coalesce_pair_p;
57 typedef const struct coalesce_pair *const_coalesce_pair_p;
59 /* Coalesce pair hashtable helpers. */
61 struct coalesce_pair_hasher : nofree_ptr_hash <coalesce_pair>
63 static inline hashval_t hash (const coalesce_pair *);
64 static inline bool equal (const coalesce_pair *, const coalesce_pair *);
67 /* Hash function for coalesce list. Calculate hash for PAIR. */
69 inline hashval_t
70 coalesce_pair_hasher::hash (const coalesce_pair *pair)
72 hashval_t a = (hashval_t)(pair->first_element);
73 hashval_t b = (hashval_t)(pair->second_element);
75 return b * (b - 1) / 2 + a;
78 /* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
79 returning TRUE if the two pairs are equivalent. */
81 inline bool
82 coalesce_pair_hasher::equal (const coalesce_pair *p1, const coalesce_pair *p2)
84 return (p1->first_element == p2->first_element
85 && p1->second_element == p2->second_element);
88 typedef hash_table<coalesce_pair_hasher> coalesce_table_type;
89 typedef coalesce_table_type::iterator coalesce_iterator_type;
92 typedef struct cost_one_pair_d
94 int first_element;
95 int second_element;
96 struct cost_one_pair_d *next;
97 } * cost_one_pair_p;
99 /* This structure maintains the list of coalesce pairs. */
101 typedef struct coalesce_list_d
103 coalesce_table_type *list; /* Hash table. */
104 coalesce_pair_p *sorted; /* List when sorted. */
105 int num_sorted; /* Number in the sorted list. */
106 cost_one_pair_p cost_one_list;/* Single use coalesces with cost 1. */
107 } *coalesce_list_p;
109 #define NO_BEST_COALESCE -1
110 #define MUST_COALESCE_COST INT_MAX
113 /* Return cost of execution of copy instruction with FREQUENCY. */
115 static inline int
116 coalesce_cost (int frequency, bool optimize_for_size)
118 /* Base costs on BB frequencies bounded by 1. */
119 int cost = frequency;
121 if (!cost)
122 cost = 1;
124 if (optimize_for_size)
125 cost = 1;
127 return cost;
131 /* Return the cost of executing a copy instruction in basic block BB. */
133 static inline int
134 coalesce_cost_bb (basic_block bb)
136 return coalesce_cost (bb->frequency, optimize_bb_for_size_p (bb));
140 /* Return the cost of executing a copy instruction on edge E. */
142 static inline int
143 coalesce_cost_edge (edge e)
145 int mult = 1;
147 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
148 if (EDGE_CRITICAL_P (e))
149 mult = 2;
150 if (e->flags & EDGE_ABNORMAL)
151 return MUST_COALESCE_COST;
152 if (e->flags & EDGE_EH)
154 edge e2;
155 edge_iterator ei;
156 FOR_EACH_EDGE (e2, ei, e->dest->preds)
157 if (e2 != e)
159 /* Putting code on EH edge that leads to BB
160 with multiple predecestors imply splitting of
161 edge too. */
162 if (mult < 2)
163 mult = 2;
164 /* If there are multiple EH predecestors, we
165 also copy EH regions and produce separate
166 landing pad. This is expensive. */
167 if (e2->flags & EDGE_EH)
169 mult = 5;
170 break;
175 return coalesce_cost (EDGE_FREQUENCY (e),
176 optimize_edge_for_size_p (e)) * mult;
180 /* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
181 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
182 NO_BEST_COALESCE is returned if there aren't any. */
184 static inline int
185 pop_cost_one_pair (coalesce_list_p cl, int *p1, int *p2)
187 cost_one_pair_p ptr;
189 ptr = cl->cost_one_list;
190 if (!ptr)
191 return NO_BEST_COALESCE;
193 *p1 = ptr->first_element;
194 *p2 = ptr->second_element;
195 cl->cost_one_list = ptr->next;
197 free (ptr);
199 return 1;
202 /* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
203 2 elements via P1 and P2. Their calculated cost is returned by the function.
204 NO_BEST_COALESCE is returned if the coalesce list is empty. */
206 static inline int
207 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
209 coalesce_pair_p node;
210 int ret;
212 if (cl->sorted == NULL)
213 return pop_cost_one_pair (cl, p1, p2);
215 if (cl->num_sorted == 0)
216 return pop_cost_one_pair (cl, p1, p2);
218 node = cl->sorted[--(cl->num_sorted)];
219 *p1 = node->first_element;
220 *p2 = node->second_element;
221 ret = node->cost;
222 free (node);
224 return ret;
228 /* Create a new empty coalesce list object and return it. */
230 static inline coalesce_list_p
231 create_coalesce_list (void)
233 coalesce_list_p list;
234 unsigned size = num_ssa_names * 3;
236 if (size < 40)
237 size = 40;
239 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
240 list->list = new coalesce_table_type (size);
241 list->sorted = NULL;
242 list->num_sorted = 0;
243 list->cost_one_list = NULL;
244 return list;
248 /* Delete coalesce list CL. */
250 static inline void
251 delete_coalesce_list (coalesce_list_p cl)
253 gcc_assert (cl->cost_one_list == NULL);
254 delete cl->list;
255 cl->list = NULL;
256 free (cl->sorted);
257 gcc_assert (cl->num_sorted == 0);
258 free (cl);
262 /* Find a matching coalesce pair object in CL for the pair P1 and P2. If
263 one isn't found, return NULL if CREATE is false, otherwise create a new
264 coalesce pair object and return it. */
266 static coalesce_pair_p
267 find_coalesce_pair (coalesce_list_p cl, int p1, int p2, bool create)
269 struct coalesce_pair p;
270 coalesce_pair **slot;
271 unsigned int hash;
273 /* Normalize so that p1 is the smaller value. */
274 if (p2 < p1)
276 p.first_element = p2;
277 p.second_element = p1;
279 else
281 p.first_element = p1;
282 p.second_element = p2;
285 hash = coalesce_pair_hasher::hash (&p);
286 slot = cl->list->find_slot_with_hash (&p, hash, create ? INSERT : NO_INSERT);
287 if (!slot)
288 return NULL;
290 if (!*slot)
292 struct coalesce_pair * pair = XNEW (struct coalesce_pair);
293 gcc_assert (cl->sorted == NULL);
294 pair->first_element = p.first_element;
295 pair->second_element = p.second_element;
296 pair->cost = 0;
297 *slot = pair;
300 return (struct coalesce_pair *) *slot;
303 static inline void
304 add_cost_one_coalesce (coalesce_list_p cl, int p1, int p2)
306 cost_one_pair_p pair;
308 pair = XNEW (struct cost_one_pair_d);
309 pair->first_element = p1;
310 pair->second_element = p2;
311 pair->next = cl->cost_one_list;
312 cl->cost_one_list = pair;
316 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
318 static inline void
319 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
321 coalesce_pair_p node;
323 gcc_assert (cl->sorted == NULL);
324 if (p1 == p2)
325 return;
327 node = find_coalesce_pair (cl, p1, p2, true);
329 /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way. */
330 if (node->cost < MUST_COALESCE_COST - 1)
332 if (value < MUST_COALESCE_COST - 1)
333 node->cost += value;
334 else
335 node->cost = value;
340 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
342 static int
343 compare_pairs (const void *p1, const void *p2)
345 const_coalesce_pair_p const *const pp1 = (const_coalesce_pair_p const *) p1;
346 const_coalesce_pair_p const *const pp2 = (const_coalesce_pair_p const *) p2;
347 int result;
349 result = (* pp1)->cost - (* pp2)->cost;
350 /* Since qsort does not guarantee stability we use the elements
351 as a secondary key. This provides us with independence from
352 the host's implementation of the sorting algorithm. */
353 if (result == 0)
355 result = (* pp2)->first_element - (* pp1)->first_element;
356 if (result == 0)
357 result = (* pp2)->second_element - (* pp1)->second_element;
360 return result;
364 /* Return the number of unique coalesce pairs in CL. */
366 static inline int
367 num_coalesce_pairs (coalesce_list_p cl)
369 return cl->list->elements ();
373 /* Iterate over CL using ITER, returning values in PAIR. */
375 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
376 FOR_EACH_HASH_TABLE_ELEMENT (*(CL)->list, (PAIR), coalesce_pair_p, (ITER))
379 /* Prepare CL for removal of preferred pairs. When finished they are sorted
380 in order from most important coalesce to least important. */
382 static void
383 sort_coalesce_list (coalesce_list_p cl)
385 unsigned x, num;
386 coalesce_pair_p p;
387 coalesce_iterator_type ppi;
389 gcc_assert (cl->sorted == NULL);
391 num = num_coalesce_pairs (cl);
392 cl->num_sorted = num;
393 if (num == 0)
394 return;
396 /* Allocate a vector for the pair pointers. */
397 cl->sorted = XNEWVEC (coalesce_pair_p, num);
399 /* Populate the vector with pointers to the pairs. */
400 x = 0;
401 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
402 cl->sorted[x++] = p;
403 gcc_assert (x == num);
405 /* Already sorted. */
406 if (num == 1)
407 return;
409 /* If there are only 2, just pick swap them if the order isn't correct. */
410 if (num == 2)
412 if (cl->sorted[0]->cost > cl->sorted[1]->cost)
413 std::swap (cl->sorted[0], cl->sorted[1]);
414 return;
417 /* Only call qsort if there are more than 2 items.
418 ??? Maybe std::sort will do better, provided that compare_pairs
419 can be inlined. */
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;
810 basic_block entry;
812 /* If inter-variable coalescing is enabled, we may attempt to
813 coalesce variables from different base variables, including
814 different parameters, so we have to make sure default defs live
815 at the entry block conflict with each other. */
816 if (flag_tree_coalesce_vars)
817 entry = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
818 else
819 entry = NULL;
821 map = live_var_map (liveinfo);
822 graph = ssa_conflicts_new (num_var_partitions (map));
824 live = new_live_track (map);
826 FOR_EACH_BB_FN (bb, cfun)
828 /* Start with live on exit temporaries. */
829 live_track_init (live, live_on_exit (liveinfo, bb));
831 for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);
832 gsi_prev (&gsi))
834 tree var;
835 gimple stmt = gsi_stmt (gsi);
837 /* A copy between 2 partitions does not introduce an interference
838 by itself. If they did, you would never be able to coalesce
839 two things which are copied. If the two variables really do
840 conflict, they will conflict elsewhere in the program.
842 This is handled by simply removing the SRC of the copy from the
843 live list, and processing the stmt normally. */
844 if (is_gimple_assign (stmt))
846 tree lhs = gimple_assign_lhs (stmt);
847 tree rhs1 = gimple_assign_rhs1 (stmt);
848 if (gimple_assign_copy_p (stmt)
849 && TREE_CODE (lhs) == SSA_NAME
850 && TREE_CODE (rhs1) == SSA_NAME)
851 live_track_clear_var (live, rhs1);
853 else if (is_gimple_debug (stmt))
854 continue;
856 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
857 live_track_process_def (live, var, graph);
859 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
860 live_track_process_use (live, var);
863 /* If result of a PHI is unused, looping over the statements will not
864 record any conflicts since the def was never live. Since the PHI node
865 is going to be translated out of SSA form, it will insert a copy.
866 There must be a conflict recorded between the result of the PHI and
867 any variables that are live. Otherwise the out-of-ssa translation
868 may create incorrect code. */
869 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
870 gsi_next (&gsi))
872 gphi *phi = gsi.phi ();
873 tree result = PHI_RESULT (phi);
874 if (live_track_live_p (live, result))
875 live_track_process_def (live, result, graph);
878 /* Pretend there are defs for params' default defs at the start
879 of the (post-)entry block. */
880 if (bb == entry)
882 unsigned base;
883 bitmap_iterator bi;
884 EXECUTE_IF_SET_IN_BITMAP (live->live_base_var, 0, base, bi)
886 bitmap_iterator bi2;
887 unsigned part;
888 EXECUTE_IF_SET_IN_BITMAP (live->live_base_partitions[base],
889 0, part, bi2)
891 tree var = partition_to_var (map, part);
892 if (!SSA_NAME_VAR (var)
893 || (TREE_CODE (SSA_NAME_VAR (var)) != PARM_DECL
894 && TREE_CODE (SSA_NAME_VAR (var)) != RESULT_DECL)
895 || !SSA_NAME_IS_DEFAULT_DEF (var))
896 continue;
897 live_track_process_def (live, var, graph);
902 live_track_clear_base_vars (live);
905 delete_live_track (live);
906 return graph;
910 /* Shortcut routine to print messages to file F of the form:
911 "STR1 EXPR1 STR2 EXPR2 STR3." */
913 static inline void
914 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
915 tree expr2, const char *str3)
917 fprintf (f, "%s", str1);
918 print_generic_expr (f, expr1, TDF_SLIM);
919 fprintf (f, "%s", str2);
920 print_generic_expr (f, expr2, TDF_SLIM);
921 fprintf (f, "%s", str3);
925 /* Print a failure to coalesce a MUST_COALESCE pair X and Y. */
927 static inline void
928 fail_abnormal_edge_coalesce (int x, int y)
930 fprintf (stderr, "\nUnable to coalesce ssa_names %d and %d",x, y);
931 fprintf (stderr, " which are marked as MUST COALESCE.\n");
932 print_generic_expr (stderr, ssa_name (x), TDF_SLIM);
933 fprintf (stderr, " and ");
934 print_generic_stmt (stderr, ssa_name (y), TDF_SLIM);
936 internal_error ("SSA corruption");
940 /* This function creates a var_map for the current function as well as creating
941 a coalesce list for use later in the out of ssa process. */
943 static var_map
944 create_outofssa_var_map (coalesce_list_p cl, bitmap used_in_copy)
946 gimple_stmt_iterator gsi;
947 basic_block bb;
948 tree var;
949 gimple stmt;
950 tree first;
951 var_map map;
952 ssa_op_iter iter;
953 int v1, v2, cost;
954 unsigned i;
956 map = init_var_map (num_ssa_names);
958 FOR_EACH_BB_FN (bb, cfun)
960 tree arg;
962 for (gphi_iterator gpi = gsi_start_phis (bb);
963 !gsi_end_p (gpi);
964 gsi_next (&gpi))
966 gphi *phi = gpi.phi ();
967 size_t i;
968 int ver;
969 tree res;
970 bool saw_copy = false;
972 res = gimple_phi_result (phi);
973 ver = SSA_NAME_VERSION (res);
974 register_ssa_partition (map, res);
976 /* Register ssa_names and coalesces between the args and the result
977 of all PHI. */
978 for (i = 0; i < gimple_phi_num_args (phi); i++)
980 edge e = gimple_phi_arg_edge (phi, i);
981 arg = PHI_ARG_DEF (phi, i);
982 if (TREE_CODE (arg) != SSA_NAME)
983 continue;
985 register_ssa_partition (map, arg);
986 if (gimple_can_coalesce_p (arg, res)
987 || (e->flags & EDGE_ABNORMAL))
989 saw_copy = true;
990 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (arg));
991 if ((e->flags & EDGE_ABNORMAL) == 0)
993 int cost = coalesce_cost_edge (e);
994 if (cost == 1 && has_single_use (arg))
995 add_cost_one_coalesce (cl, ver, SSA_NAME_VERSION (arg));
996 else
997 add_coalesce (cl, ver, SSA_NAME_VERSION (arg), cost);
1001 if (saw_copy)
1002 bitmap_set_bit (used_in_copy, ver);
1005 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1007 stmt = gsi_stmt (gsi);
1009 if (is_gimple_debug (stmt))
1010 continue;
1012 /* Register USE and DEF operands in each statement. */
1013 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
1014 register_ssa_partition (map, var);
1016 /* Check for copy coalesces. */
1017 switch (gimple_code (stmt))
1019 case GIMPLE_ASSIGN:
1021 tree lhs = gimple_assign_lhs (stmt);
1022 tree rhs1 = gimple_assign_rhs1 (stmt);
1023 if (gimple_assign_ssa_name_copy_p (stmt)
1024 && gimple_can_coalesce_p (lhs, rhs1))
1026 v1 = SSA_NAME_VERSION (lhs);
1027 v2 = SSA_NAME_VERSION (rhs1);
1028 cost = coalesce_cost_bb (bb);
1029 add_coalesce (cl, v1, v2, cost);
1030 bitmap_set_bit (used_in_copy, v1);
1031 bitmap_set_bit (used_in_copy, v2);
1034 break;
1036 case GIMPLE_ASM:
1038 gasm *asm_stmt = as_a <gasm *> (stmt);
1039 unsigned long noutputs, i;
1040 unsigned long ninputs;
1041 tree *outputs, link;
1042 noutputs = gimple_asm_noutputs (asm_stmt);
1043 ninputs = gimple_asm_ninputs (asm_stmt);
1044 outputs = (tree *) alloca (noutputs * sizeof (tree));
1045 for (i = 0; i < noutputs; ++i)
1047 link = gimple_asm_output_op (asm_stmt, i);
1048 outputs[i] = TREE_VALUE (link);
1051 for (i = 0; i < ninputs; ++i)
1053 const char *constraint;
1054 tree input;
1055 char *end;
1056 unsigned long match;
1058 link = gimple_asm_input_op (asm_stmt, i);
1059 constraint
1060 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1061 input = TREE_VALUE (link);
1063 if (TREE_CODE (input) != SSA_NAME)
1064 continue;
1066 match = strtoul (constraint, &end, 10);
1067 if (match >= noutputs || end == constraint)
1068 continue;
1070 if (TREE_CODE (outputs[match]) != SSA_NAME)
1071 continue;
1073 v1 = SSA_NAME_VERSION (outputs[match]);
1074 v2 = SSA_NAME_VERSION (input);
1076 if (gimple_can_coalesce_p (outputs[match], input))
1078 cost = coalesce_cost (REG_BR_PROB_BASE,
1079 optimize_bb_for_size_p (bb));
1080 add_coalesce (cl, v1, v2, cost);
1081 bitmap_set_bit (used_in_copy, v1);
1082 bitmap_set_bit (used_in_copy, v2);
1085 break;
1088 default:
1089 break;
1094 /* Now process result decls and live on entry variables for entry into
1095 the coalesce list. */
1096 first = NULL_TREE;
1097 for (i = 1; i < num_ssa_names; i++)
1099 var = ssa_name (i);
1100 if (var != NULL_TREE && !virtual_operand_p (var))
1102 /* Add coalesces between all the result decls. */
1103 if (SSA_NAME_VAR (var)
1104 && TREE_CODE (SSA_NAME_VAR (var)) == RESULT_DECL)
1106 if (first == NULL_TREE)
1107 first = var;
1108 else
1110 gcc_assert (gimple_can_coalesce_p (var, first));
1111 v1 = SSA_NAME_VERSION (first);
1112 v2 = SSA_NAME_VERSION (var);
1113 bitmap_set_bit (used_in_copy, v1);
1114 bitmap_set_bit (used_in_copy, v2);
1115 cost = coalesce_cost_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
1116 add_coalesce (cl, v1, v2, cost);
1119 /* Mark any default_def variables as being in the coalesce list
1120 since they will have to be coalesced with the base variable. If
1121 not marked as present, they won't be in the coalesce view. */
1122 if (SSA_NAME_IS_DEFAULT_DEF (var)
1123 && !has_zero_uses (var))
1124 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1128 return map;
1132 /* Attempt to coalesce ssa versions X and Y together using the partition
1133 mapping in MAP and checking conflicts in GRAPH. Output any debug info to
1134 DEBUG, if it is nun-NULL. */
1136 static inline bool
1137 attempt_coalesce (var_map map, ssa_conflicts_p graph, int x, int y,
1138 FILE *debug)
1140 int z;
1141 tree var1, var2;
1142 int p1, p2;
1144 p1 = var_to_partition (map, ssa_name (x));
1145 p2 = var_to_partition (map, ssa_name (y));
1147 if (debug)
1149 fprintf (debug, "(%d)", x);
1150 print_generic_expr (debug, partition_to_var (map, p1), TDF_SLIM);
1151 fprintf (debug, " & (%d)", y);
1152 print_generic_expr (debug, partition_to_var (map, p2), TDF_SLIM);
1155 if (p1 == p2)
1157 if (debug)
1158 fprintf (debug, ": Already Coalesced.\n");
1159 return true;
1162 if (debug)
1163 fprintf (debug, " [map: %d, %d] ", p1, p2);
1166 if (!ssa_conflicts_test_p (graph, p1, p2))
1168 var1 = partition_to_var (map, p1);
1169 var2 = partition_to_var (map, p2);
1171 z = var_union (map, var1, var2);
1172 if (z == NO_PARTITION)
1174 if (debug)
1175 fprintf (debug, ": Unable to perform partition union.\n");
1176 return false;
1179 /* z is the new combined partition. Remove the other partition from
1180 the list, and merge the conflicts. */
1181 if (z == p1)
1182 ssa_conflicts_merge (graph, p1, p2);
1183 else
1184 ssa_conflicts_merge (graph, p2, p1);
1186 if (debug)
1187 fprintf (debug, ": Success -> %d\n", z);
1189 return true;
1192 if (debug)
1193 fprintf (debug, ": Fail due to conflict\n");
1195 return false;
1199 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1200 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1202 static void
1203 coalesce_partitions (var_map map, ssa_conflicts_p graph, coalesce_list_p cl,
1204 FILE *debug)
1206 int x = 0, y = 0;
1207 tree var1, var2;
1208 int cost;
1209 basic_block bb;
1210 edge e;
1211 edge_iterator ei;
1213 /* First, coalesce all the copies across abnormal edges. These are not placed
1214 in the coalesce list because they do not need to be sorted, and simply
1215 consume extra memory/compilation time in large programs. */
1217 FOR_EACH_BB_FN (bb, cfun)
1219 FOR_EACH_EDGE (e, ei, bb->preds)
1220 if (e->flags & EDGE_ABNORMAL)
1222 gphi_iterator gsi;
1223 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1224 gsi_next (&gsi))
1226 gphi *phi = gsi.phi ();
1227 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1228 if (SSA_NAME_IS_DEFAULT_DEF (arg)
1229 && (!SSA_NAME_VAR (arg)
1230 || TREE_CODE (SSA_NAME_VAR (arg)) != PARM_DECL))
1231 continue;
1233 tree res = PHI_RESULT (phi);
1234 int v1 = SSA_NAME_VERSION (res);
1235 int v2 = SSA_NAME_VERSION (arg);
1237 if (debug)
1238 fprintf (debug, "Abnormal coalesce: ");
1240 if (!attempt_coalesce (map, graph, v1, v2, debug))
1241 fail_abnormal_edge_coalesce (v1, v2);
1246 /* Now process the items in the coalesce list. */
1248 while ((cost = pop_best_coalesce (cl, &x, &y)) != NO_BEST_COALESCE)
1250 var1 = ssa_name (x);
1251 var2 = ssa_name (y);
1253 /* Assert the coalesces have the same base variable. */
1254 gcc_assert (gimple_can_coalesce_p (var1, var2));
1256 if (debug)
1257 fprintf (debug, "Coalesce list: ");
1258 attempt_coalesce (map, graph, x, y, debug);
1263 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1265 struct ssa_name_var_hash : nofree_ptr_hash <tree_node>
1267 static inline hashval_t hash (const tree_node *);
1268 static inline int equal (const tree_node *, const tree_node *);
1271 inline hashval_t
1272 ssa_name_var_hash::hash (const_tree n)
1274 return DECL_UID (SSA_NAME_VAR (n));
1277 inline int
1278 ssa_name_var_hash::equal (const tree_node *n1, const tree_node *n2)
1280 return SSA_NAME_VAR (n1) == SSA_NAME_VAR (n2);
1284 /* Output partition map MAP with coalescing plan PART to file F. */
1286 void
1287 dump_part_var_map (FILE *f, partition part, var_map map)
1289 int t;
1290 unsigned x, y;
1291 int p;
1293 fprintf (f, "\nCoalescible Partition map \n\n");
1295 for (x = 0; x < map->num_partitions; x++)
1297 if (map->view_to_partition != NULL)
1298 p = map->view_to_partition[x];
1299 else
1300 p = x;
1302 if (ssa_name (p) == NULL_TREE
1303 || virtual_operand_p (ssa_name (p)))
1304 continue;
1306 t = 0;
1307 for (y = 1; y < num_ssa_names; y++)
1309 tree var = version_to_var (map, y);
1310 if (!var)
1311 continue;
1312 int q = var_to_partition (map, var);
1313 p = partition_find (part, q);
1314 gcc_assert (map->partition_to_base_index[q]
1315 == map->partition_to_base_index[p]);
1317 if (p == (int)x)
1319 if (t++ == 0)
1321 fprintf (f, "Partition %d, base %d (", x,
1322 map->partition_to_base_index[q]);
1323 print_generic_expr (f, partition_to_var (map, q), TDF_SLIM);
1324 fprintf (f, " - ");
1326 fprintf (f, "%d ", y);
1329 if (t != 0)
1330 fprintf (f, ")\n");
1332 fprintf (f, "\n");
1335 /* Given SSA_NAMEs NAME1 and NAME2, return true if they are candidates for
1336 coalescing together, false otherwise.
1338 This must stay consistent with var_map_base_init in tree-ssa-live.c. */
1340 bool
1341 gimple_can_coalesce_p (tree name1, tree name2)
1343 /* First check the SSA_NAME's associated DECL. Without
1344 optimization, we only want to coalesce if they have the same DECL
1345 or both have no associated DECL. */
1346 tree var1 = SSA_NAME_VAR (name1);
1347 tree var2 = SSA_NAME_VAR (name2);
1348 var1 = (var1 && (!VAR_P (var1) || !DECL_IGNORED_P (var1))) ? var1 : NULL_TREE;
1349 var2 = (var2 && (!VAR_P (var2) || !DECL_IGNORED_P (var2))) ? var2 : NULL_TREE;
1350 if (var1 != var2 && !flag_tree_coalesce_vars)
1351 return false;
1353 /* Now check the types. If the types are the same, then we should
1354 try to coalesce V1 and V2. */
1355 tree t1 = TREE_TYPE (name1);
1356 tree t2 = TREE_TYPE (name2);
1357 if (t1 == t2)
1359 check_modes:
1360 /* If the base variables are the same, we're good: none of the
1361 other tests below could possibly fail. */
1362 var1 = SSA_NAME_VAR (name1);
1363 var2 = SSA_NAME_VAR (name2);
1364 if (var1 == var2)
1365 return true;
1367 /* We don't want to coalesce two SSA names if one of the base
1368 variables is supposed to be a register while the other is
1369 supposed to be on the stack. Anonymous SSA names take
1370 registers, but when not optimizing, user variables should go
1371 on the stack, so coalescing them with the anonymous variable
1372 as the partition leader would end up assigning the user
1373 variable to a register. Don't do that! */
1374 bool reg1 = !var1 || use_register_for_decl (var1);
1375 bool reg2 = !var2 || use_register_for_decl (var2);
1376 if (reg1 != reg2)
1377 return false;
1379 /* Check that the promoted modes are the same. We don't want to
1380 coalesce if the promoted modes would be different. Only
1381 PARM_DECLs and RESULT_DECLs have different promotion rules,
1382 so skip the test if we both are variables or anonymous
1383 SSA_NAMEs. */
1384 return ((!var1 || VAR_P (var1)) && (!var2 || VAR_P (var2)))
1385 || promote_ssa_mode (name1, NULL) == promote_ssa_mode (name2, NULL);
1388 /* If the types are not the same, check for a canonical type match. This
1389 (for example) allows coalescing when the types are fundamentally the
1390 same, but just have different names.
1392 Note pointer types with different address spaces may have the same
1393 canonical type. Those are rejected for coalescing by the
1394 types_compatible_p check. */
1395 if (TYPE_CANONICAL (t1)
1396 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)
1397 && types_compatible_p (t1, t2))
1398 goto check_modes;
1400 return false;
1403 /* Fill in MAP's partition_to_base_index, with one index for each
1404 partition of SSA names USED_IN_COPIES and related by CL coalesce
1405 possibilities. This must match gimple_can_coalesce_p in the
1406 optimized case. */
1408 static void
1409 compute_optimized_partition_bases (var_map map, bitmap used_in_copies,
1410 coalesce_list_p cl)
1412 int parts = num_var_partitions (map);
1413 partition tentative = partition_new (parts);
1415 /* Partition the SSA versions so that, for each coalescible
1416 pair, both of its members are in the same partition in
1417 TENTATIVE. */
1418 gcc_assert (!cl->sorted);
1419 coalesce_pair_p node;
1420 coalesce_iterator_type ppi;
1421 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
1423 tree v1 = ssa_name (node->first_element);
1424 int p1 = partition_find (tentative, var_to_partition (map, v1));
1425 tree v2 = ssa_name (node->second_element);
1426 int p2 = partition_find (tentative, var_to_partition (map, v2));
1428 if (p1 == p2)
1429 continue;
1431 partition_union (tentative, p1, p2);
1434 /* We have to deal with cost one pairs too. */
1435 for (cost_one_pair_d *co = cl->cost_one_list; co; co = co->next)
1437 tree v1 = ssa_name (co->first_element);
1438 int p1 = partition_find (tentative, var_to_partition (map, v1));
1439 tree v2 = ssa_name (co->second_element);
1440 int p2 = partition_find (tentative, var_to_partition (map, v2));
1442 if (p1 == p2)
1443 continue;
1445 partition_union (tentative, p1, p2);
1448 /* And also with abnormal edges. */
1449 basic_block bb;
1450 edge e;
1451 edge_iterator ei;
1452 FOR_EACH_BB_FN (bb, cfun)
1454 FOR_EACH_EDGE (e, ei, bb->preds)
1455 if (e->flags & EDGE_ABNORMAL)
1457 gphi_iterator gsi;
1458 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1459 gsi_next (&gsi))
1461 gphi *phi = gsi.phi ();
1462 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1463 if (SSA_NAME_IS_DEFAULT_DEF (arg)
1464 && (!SSA_NAME_VAR (arg)
1465 || TREE_CODE (SSA_NAME_VAR (arg)) != PARM_DECL))
1466 continue;
1468 tree res = PHI_RESULT (phi);
1470 int p1 = partition_find (tentative, var_to_partition (map, res));
1471 int p2 = partition_find (tentative, var_to_partition (map, arg));
1473 if (p1 == p2)
1474 continue;
1476 partition_union (tentative, p1, p2);
1481 map->partition_to_base_index = XCNEWVEC (int, parts);
1482 auto_vec<unsigned int> index_map (parts);
1483 if (parts)
1484 index_map.quick_grow (parts);
1486 const unsigned no_part = -1;
1487 unsigned count = parts;
1488 while (count)
1489 index_map[--count] = no_part;
1491 /* Initialize MAP's mapping from partition to base index, using
1492 as base indices an enumeration of the TENTATIVE partitions in
1493 which each SSA version ended up, so that we compute conflicts
1494 between all SSA versions that ended up in the same potential
1495 coalesce partition. */
1496 bitmap_iterator bi;
1497 unsigned i;
1498 EXECUTE_IF_SET_IN_BITMAP (used_in_copies, 0, i, bi)
1500 int pidx = var_to_partition (map, ssa_name (i));
1501 int base = partition_find (tentative, pidx);
1502 if (index_map[base] != no_part)
1503 continue;
1504 index_map[base] = count++;
1507 map->num_basevars = count;
1509 EXECUTE_IF_SET_IN_BITMAP (used_in_copies, 0, i, bi)
1511 int pidx = var_to_partition (map, ssa_name (i));
1512 int base = partition_find (tentative, pidx);
1513 gcc_assert (index_map[base] < count);
1514 map->partition_to_base_index[pidx] = index_map[base];
1517 if (dump_file && (dump_flags & TDF_DETAILS))
1518 dump_part_var_map (dump_file, tentative, map);
1520 partition_delete (tentative);
1523 /* Hashtable helpers. */
1525 struct tree_int_map_hasher : nofree_ptr_hash <tree_int_map>
1527 static inline hashval_t hash (const tree_int_map *);
1528 static inline bool equal (const tree_int_map *, const tree_int_map *);
1531 inline hashval_t
1532 tree_int_map_hasher::hash (const tree_int_map *v)
1534 return tree_map_base_hash (v);
1537 inline bool
1538 tree_int_map_hasher::equal (const tree_int_map *v, const tree_int_map *c)
1540 return tree_int_map_eq (v, c);
1543 /* This routine will initialize the basevar fields of MAP with base
1544 names. Partitions will share the same base if they have the same
1545 SSA_NAME_VAR, or, being anonymous variables, the same type. This
1546 must match gimple_can_coalesce_p in the non-optimized case. */
1548 static void
1549 compute_samebase_partition_bases (var_map map)
1551 int x, num_part;
1552 tree var;
1553 struct tree_int_map *m, *mapstorage;
1555 num_part = num_var_partitions (map);
1556 hash_table<tree_int_map_hasher> tree_to_index (num_part);
1557 /* We can have at most num_part entries in the hash tables, so it's
1558 enough to allocate so many map elements once, saving some malloc
1559 calls. */
1560 mapstorage = m = XNEWVEC (struct tree_int_map, num_part);
1562 /* If a base table already exists, clear it, otherwise create it. */
1563 free (map->partition_to_base_index);
1564 map->partition_to_base_index = (int *) xmalloc (sizeof (int) * num_part);
1566 /* Build the base variable list, and point partitions at their bases. */
1567 for (x = 0; x < num_part; x++)
1569 struct tree_int_map **slot;
1570 unsigned baseindex;
1571 var = partition_to_var (map, x);
1572 if (SSA_NAME_VAR (var)
1573 && (!VAR_P (SSA_NAME_VAR (var))
1574 || !DECL_IGNORED_P (SSA_NAME_VAR (var))))
1575 m->base.from = SSA_NAME_VAR (var);
1576 else
1577 /* This restricts what anonymous SSA names we can coalesce
1578 as it restricts the sets we compute conflicts for.
1579 Using TREE_TYPE to generate sets is the easies as
1580 type equivalency also holds for SSA names with the same
1581 underlying decl.
1583 Check gimple_can_coalesce_p when changing this code. */
1584 m->base.from = (TYPE_CANONICAL (TREE_TYPE (var))
1585 ? TYPE_CANONICAL (TREE_TYPE (var))
1586 : TREE_TYPE (var));
1587 /* If base variable hasn't been seen, set it up. */
1588 slot = tree_to_index.find_slot (m, INSERT);
1589 if (!*slot)
1591 baseindex = m - mapstorage;
1592 m->to = baseindex;
1593 *slot = m;
1594 m++;
1596 else
1597 baseindex = (*slot)->to;
1598 map->partition_to_base_index[x] = baseindex;
1601 map->num_basevars = m - mapstorage;
1603 free (mapstorage);
1606 /* Reduce the number of copies by coalescing variables in the function. Return
1607 a partition map with the resulting coalesces. */
1609 extern var_map
1610 coalesce_ssa_name (void)
1612 tree_live_info_p liveinfo;
1613 ssa_conflicts_p graph;
1614 coalesce_list_p cl;
1615 bitmap used_in_copies = BITMAP_ALLOC (NULL);
1616 var_map map;
1617 unsigned int i;
1619 cl = create_coalesce_list ();
1620 map = create_outofssa_var_map (cl, used_in_copies);
1622 /* If this optimization is disabled, we need to coalesce all the
1623 names originating from the same SSA_NAME_VAR so debug info
1624 remains undisturbed. */
1625 if (!flag_tree_coalesce_vars)
1627 hash_table<ssa_name_var_hash> ssa_name_hash (10);
1629 for (i = 1; i < num_ssa_names; i++)
1631 tree a = ssa_name (i);
1633 if (a
1634 && SSA_NAME_VAR (a)
1635 && !DECL_IGNORED_P (SSA_NAME_VAR (a))
1636 && (!has_zero_uses (a) || !SSA_NAME_IS_DEFAULT_DEF (a)))
1638 tree *slot = ssa_name_hash.find_slot (a, INSERT);
1640 if (!*slot)
1641 *slot = a;
1642 else
1644 /* If the variable is a PARM_DECL or a RESULT_DECL, we
1645 _require_ that all the names originating from it be
1646 coalesced, because there must be a single partition
1647 containing all the names so that it can be assigned
1648 the canonical RTL location of the DECL safely.
1649 If in_lto_p, a function could have been compiled
1650 originally with optimizations and only the link
1651 performed at -O0, so we can't actually require it. */
1652 const int cost
1653 = (TREE_CODE (SSA_NAME_VAR (a)) == VAR_DECL || in_lto_p)
1654 ? MUST_COALESCE_COST - 1 : MUST_COALESCE_COST;
1655 add_coalesce (cl, SSA_NAME_VERSION (a),
1656 SSA_NAME_VERSION (*slot), cost);
1657 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (a));
1658 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (*slot));
1663 if (dump_file && (dump_flags & TDF_DETAILS))
1664 dump_var_map (dump_file, map);
1666 partition_view_bitmap (map, used_in_copies);
1668 if (flag_tree_coalesce_vars)
1669 compute_optimized_partition_bases (map, used_in_copies, cl);
1670 else
1671 compute_samebase_partition_bases (map);
1673 BITMAP_FREE (used_in_copies);
1675 if (num_var_partitions (map) < 1)
1677 delete_coalesce_list (cl);
1678 return map;
1681 if (dump_file && (dump_flags & TDF_DETAILS))
1682 dump_var_map (dump_file, map);
1684 liveinfo = calculate_live_ranges (map, false);
1686 if (dump_file && (dump_flags & TDF_DETAILS))
1687 dump_live_info (dump_file, liveinfo, LIVEDUMP_ENTRY);
1689 /* Build a conflict graph. */
1690 graph = build_ssa_conflict_graph (liveinfo);
1691 delete_tree_live_info (liveinfo);
1692 if (dump_file && (dump_flags & TDF_DETAILS))
1693 ssa_conflicts_dump (dump_file, graph);
1695 sort_coalesce_list (cl);
1697 if (dump_file && (dump_flags & TDF_DETAILS))
1699 fprintf (dump_file, "\nAfter sorting:\n");
1700 dump_coalesce_list (dump_file, cl);
1703 /* First, coalesce all live on entry variables to their base variable.
1704 This will ensure the first use is coming from the correct location. */
1706 if (dump_file && (dump_flags & TDF_DETAILS))
1707 dump_var_map (dump_file, map);
1709 /* Now coalesce everything in the list. */
1710 coalesce_partitions (map, graph, cl,
1711 ((dump_flags & TDF_DETAILS) ? dump_file : NULL));
1713 delete_coalesce_list (cl);
1714 ssa_conflicts_delete (graph);
1716 return map;