[AArch64] Implement copysign[ds]f3
[official-gcc.git] / gcc / tree-ssa-coalesce.c
blob6468012aa00792abf0e65c520f803fcc2985604b
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 "cfgexpand.h"
40 #include "explow.h"
41 #include "diagnostic-core.h"
44 /* This set of routines implements a coalesce_list. This is an object which
45 is used to track pairs of ssa_names which are desirable to coalesce
46 together to avoid copies. Costs are associated with each pair, and when
47 all desired information has been collected, the object can be used to
48 order the pairs for processing. */
50 /* This structure defines a pair entry. */
52 typedef struct coalesce_pair
54 int first_element;
55 int second_element;
56 int cost;
57 } * coalesce_pair_p;
58 typedef const struct coalesce_pair *const_coalesce_pair_p;
60 /* Coalesce pair hashtable helpers. */
62 struct coalesce_pair_hasher : nofree_ptr_hash <coalesce_pair>
64 static inline hashval_t hash (const coalesce_pair *);
65 static inline bool equal (const coalesce_pair *, const coalesce_pair *);
68 /* Hash function for coalesce list. Calculate hash for PAIR. */
70 inline hashval_t
71 coalesce_pair_hasher::hash (const coalesce_pair *pair)
73 hashval_t a = (hashval_t)(pair->first_element);
74 hashval_t b = (hashval_t)(pair->second_element);
76 return b * (b - 1) / 2 + a;
79 /* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
80 returning TRUE if the two pairs are equivalent. */
82 inline bool
83 coalesce_pair_hasher::equal (const coalesce_pair *p1, const coalesce_pair *p2)
85 return (p1->first_element == p2->first_element
86 && p1->second_element == p2->second_element);
89 typedef hash_table<coalesce_pair_hasher> coalesce_table_type;
90 typedef coalesce_table_type::iterator coalesce_iterator_type;
93 typedef struct cost_one_pair_d
95 int first_element;
96 int second_element;
97 struct cost_one_pair_d *next;
98 } * cost_one_pair_p;
100 /* This structure maintains the list of coalesce pairs. */
102 typedef struct coalesce_list_d
104 coalesce_table_type *list; /* Hash table. */
105 coalesce_pair_p *sorted; /* List when sorted. */
106 int num_sorted; /* Number in the sorted list. */
107 cost_one_pair_p cost_one_list;/* Single use coalesces with cost 1. */
108 } *coalesce_list_p;
110 #define NO_BEST_COALESCE -1
111 #define MUST_COALESCE_COST INT_MAX
114 /* Return cost of execution of copy instruction with FREQUENCY. */
116 static inline int
117 coalesce_cost (int frequency, bool optimize_for_size)
119 /* Base costs on BB frequencies bounded by 1. */
120 int cost = frequency;
122 if (!cost)
123 cost = 1;
125 if (optimize_for_size)
126 cost = 1;
128 return cost;
132 /* Return the cost of executing a copy instruction in basic block BB. */
134 static inline int
135 coalesce_cost_bb (basic_block bb)
137 return coalesce_cost (bb->frequency, optimize_bb_for_size_p (bb));
141 /* Return the cost of executing a copy instruction on edge E. */
143 static inline int
144 coalesce_cost_edge (edge e)
146 int mult = 1;
148 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
149 if (EDGE_CRITICAL_P (e))
150 mult = 2;
151 if (e->flags & EDGE_ABNORMAL)
152 return MUST_COALESCE_COST;
153 if (e->flags & EDGE_EH)
155 edge e2;
156 edge_iterator ei;
157 FOR_EACH_EDGE (e2, ei, e->dest->preds)
158 if (e2 != e)
160 /* Putting code on EH edge that leads to BB
161 with multiple predecestors imply splitting of
162 edge too. */
163 if (mult < 2)
164 mult = 2;
165 /* If there are multiple EH predecestors, we
166 also copy EH regions and produce separate
167 landing pad. This is expensive. */
168 if (e2->flags & EDGE_EH)
170 mult = 5;
171 break;
176 return coalesce_cost (EDGE_FREQUENCY (e),
177 optimize_edge_for_size_p (e)) * mult;
181 /* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
182 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
183 NO_BEST_COALESCE is returned if there aren't any. */
185 static inline int
186 pop_cost_one_pair (coalesce_list_p cl, int *p1, int *p2)
188 cost_one_pair_p ptr;
190 ptr = cl->cost_one_list;
191 if (!ptr)
192 return NO_BEST_COALESCE;
194 *p1 = ptr->first_element;
195 *p2 = ptr->second_element;
196 cl->cost_one_list = ptr->next;
198 free (ptr);
200 return 1;
203 /* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
204 2 elements via P1 and P2. Their calculated cost is returned by the function.
205 NO_BEST_COALESCE is returned if the coalesce list is empty. */
207 static inline int
208 pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
210 coalesce_pair_p node;
211 int ret;
213 if (cl->sorted == NULL)
214 return pop_cost_one_pair (cl, p1, p2);
216 if (cl->num_sorted == 0)
217 return pop_cost_one_pair (cl, p1, p2);
219 node = cl->sorted[--(cl->num_sorted)];
220 *p1 = node->first_element;
221 *p2 = node->second_element;
222 ret = node->cost;
223 free (node);
225 return ret;
229 /* Create a new empty coalesce list object and return it. */
231 static inline coalesce_list_p
232 create_coalesce_list (void)
234 coalesce_list_p list;
235 unsigned size = num_ssa_names * 3;
237 if (size < 40)
238 size = 40;
240 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
241 list->list = new coalesce_table_type (size);
242 list->sorted = NULL;
243 list->num_sorted = 0;
244 list->cost_one_list = NULL;
245 return list;
249 /* Delete coalesce list CL. */
251 static inline void
252 delete_coalesce_list (coalesce_list_p cl)
254 gcc_assert (cl->cost_one_list == NULL);
255 delete cl->list;
256 cl->list = NULL;
257 free (cl->sorted);
258 gcc_assert (cl->num_sorted == 0);
259 free (cl);
263 /* Find a matching coalesce pair object in CL for the pair P1 and P2. If
264 one isn't found, return NULL if CREATE is false, otherwise create a new
265 coalesce pair object and return it. */
267 static coalesce_pair_p
268 find_coalesce_pair (coalesce_list_p cl, int p1, int p2, bool create)
270 struct coalesce_pair p;
271 coalesce_pair **slot;
272 unsigned int hash;
274 /* Normalize so that p1 is the smaller value. */
275 if (p2 < p1)
277 p.first_element = p2;
278 p.second_element = p1;
280 else
282 p.first_element = p1;
283 p.second_element = p2;
286 hash = coalesce_pair_hasher::hash (&p);
287 slot = cl->list->find_slot_with_hash (&p, hash, create ? INSERT : NO_INSERT);
288 if (!slot)
289 return NULL;
291 if (!*slot)
293 struct coalesce_pair * pair = XNEW (struct coalesce_pair);
294 gcc_assert (cl->sorted == NULL);
295 pair->first_element = p.first_element;
296 pair->second_element = p.second_element;
297 pair->cost = 0;
298 *slot = pair;
301 return (struct coalesce_pair *) *slot;
304 static inline void
305 add_cost_one_coalesce (coalesce_list_p cl, int p1, int p2)
307 cost_one_pair_p pair;
309 pair = XNEW (struct cost_one_pair_d);
310 pair->first_element = p1;
311 pair->second_element = p2;
312 pair->next = cl->cost_one_list;
313 cl->cost_one_list = pair;
317 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
319 static inline void
320 add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
322 coalesce_pair_p node;
324 gcc_assert (cl->sorted == NULL);
325 if (p1 == p2)
326 return;
328 node = find_coalesce_pair (cl, p1, p2, true);
330 /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way. */
331 if (node->cost < MUST_COALESCE_COST - 1)
333 if (value < MUST_COALESCE_COST - 1)
334 node->cost += value;
335 else
336 node->cost = value;
341 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
343 static int
344 compare_pairs (const void *p1, const void *p2)
346 const_coalesce_pair_p const *const pp1 = (const_coalesce_pair_p const *) p1;
347 const_coalesce_pair_p const *const pp2 = (const_coalesce_pair_p const *) p2;
348 int result;
350 result = (* pp1)->cost - (* pp2)->cost;
351 /* Since qsort does not guarantee stability we use the elements
352 as a secondary key. This provides us with independence from
353 the host's implementation of the sorting algorithm. */
354 if (result == 0)
356 result = (* pp2)->first_element - (* pp1)->first_element;
357 if (result == 0)
358 result = (* pp2)->second_element - (* pp1)->second_element;
361 return result;
365 /* Return the number of unique coalesce pairs in CL. */
367 static inline int
368 num_coalesce_pairs (coalesce_list_p cl)
370 return cl->list->elements ();
374 /* Iterate over CL using ITER, returning values in PAIR. */
376 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
377 FOR_EACH_HASH_TABLE_ELEMENT (*(CL)->list, (PAIR), coalesce_pair_p, (ITER))
380 /* Prepare CL for removal of preferred pairs. When finished they are sorted
381 in order from most important coalesce to least important. */
383 static void
384 sort_coalesce_list (coalesce_list_p cl)
386 unsigned x, num;
387 coalesce_pair_p p;
388 coalesce_iterator_type ppi;
390 gcc_assert (cl->sorted == NULL);
392 num = num_coalesce_pairs (cl);
393 cl->num_sorted = num;
394 if (num == 0)
395 return;
397 /* Allocate a vector for the pair pointers. */
398 cl->sorted = XNEWVEC (coalesce_pair_p, num);
400 /* Populate the vector with pointers to the pairs. */
401 x = 0;
402 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
403 cl->sorted[x++] = p;
404 gcc_assert (x == num);
406 /* Already sorted. */
407 if (num == 1)
408 return;
410 /* If there are only 2, just pick swap them if the order isn't correct. */
411 if (num == 2)
413 if (cl->sorted[0]->cost > cl->sorted[1]->cost)
414 std::swap (cl->sorted[0], cl->sorted[1]);
415 return;
418 /* Only call qsort if there are more than 2 items.
419 ??? Maybe std::sort will do better, provided that compare_pairs
420 can be inlined. */
421 if (num > 2)
422 qsort (cl->sorted, num, sizeof (coalesce_pair_p), compare_pairs);
426 /* Send debug info for coalesce list CL to file F. */
428 static void
429 dump_coalesce_list (FILE *f, coalesce_list_p cl)
431 coalesce_pair_p node;
432 coalesce_iterator_type ppi;
434 int x;
435 tree var;
437 if (cl->sorted == NULL)
439 fprintf (f, "Coalesce List:\n");
440 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
442 tree var1 = ssa_name (node->first_element);
443 tree var2 = ssa_name (node->second_element);
444 print_generic_expr (f, var1, TDF_SLIM);
445 fprintf (f, " <-> ");
446 print_generic_expr (f, var2, TDF_SLIM);
447 fprintf (f, " (%1d), ", node->cost);
448 fprintf (f, "\n");
451 else
453 fprintf (f, "Sorted Coalesce list:\n");
454 for (x = cl->num_sorted - 1 ; x >=0; x--)
456 node = cl->sorted[x];
457 fprintf (f, "(%d) ", node->cost);
458 var = ssa_name (node->first_element);
459 print_generic_expr (f, var, TDF_SLIM);
460 fprintf (f, " <-> ");
461 var = ssa_name (node->second_element);
462 print_generic_expr (f, var, TDF_SLIM);
463 fprintf (f, "\n");
469 /* This represents a conflict graph. Implemented as an array of bitmaps.
470 A full matrix is used for conflicts rather than just upper triangular form.
471 this make sit much simpler and faster to perform conflict merges. */
473 typedef struct ssa_conflicts_d
475 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
476 vec<bitmap> conflicts;
477 } * ssa_conflicts_p;
479 /* Return an empty new conflict graph for SIZE elements. */
481 static inline ssa_conflicts_p
482 ssa_conflicts_new (unsigned size)
484 ssa_conflicts_p ptr;
486 ptr = XNEW (struct ssa_conflicts_d);
487 bitmap_obstack_initialize (&ptr->obstack);
488 ptr->conflicts.create (size);
489 ptr->conflicts.safe_grow_cleared (size);
490 return ptr;
494 /* Free storage for conflict graph PTR. */
496 static inline void
497 ssa_conflicts_delete (ssa_conflicts_p ptr)
499 bitmap_obstack_release (&ptr->obstack);
500 ptr->conflicts.release ();
501 free (ptr);
505 /* Test if elements X and Y conflict in graph PTR. */
507 static inline bool
508 ssa_conflicts_test_p (ssa_conflicts_p ptr, unsigned x, unsigned y)
510 bitmap bx = ptr->conflicts[x];
511 bitmap by = ptr->conflicts[y];
513 gcc_checking_assert (x != y);
515 if (bx)
516 /* Avoid the lookup if Y has no conflicts. */
517 return by ? bitmap_bit_p (bx, y) : false;
518 else
519 return false;
523 /* Add a conflict with Y to the bitmap for X in graph PTR. */
525 static inline void
526 ssa_conflicts_add_one (ssa_conflicts_p ptr, unsigned x, unsigned y)
528 bitmap bx = ptr->conflicts[x];
529 /* If there are no conflicts yet, allocate the bitmap and set bit. */
530 if (! bx)
531 bx = ptr->conflicts[x] = BITMAP_ALLOC (&ptr->obstack);
532 bitmap_set_bit (bx, y);
536 /* Add conflicts between X and Y in graph PTR. */
538 static inline void
539 ssa_conflicts_add (ssa_conflicts_p ptr, unsigned x, unsigned y)
541 gcc_checking_assert (x != y);
542 ssa_conflicts_add_one (ptr, x, y);
543 ssa_conflicts_add_one (ptr, y, x);
547 /* Merge all Y's conflict into X in graph PTR. */
549 static inline void
550 ssa_conflicts_merge (ssa_conflicts_p ptr, unsigned x, unsigned y)
552 unsigned z;
553 bitmap_iterator bi;
554 bitmap bx = ptr->conflicts[x];
555 bitmap by = ptr->conflicts[y];
557 gcc_checking_assert (x != y);
558 if (! by)
559 return;
561 /* Add a conflict between X and every one Y has. If the bitmap doesn't
562 exist, then it has already been coalesced, and we don't need to add a
563 conflict. */
564 EXECUTE_IF_SET_IN_BITMAP (by, 0, z, bi)
566 bitmap bz = ptr->conflicts[z];
567 if (bz)
568 bitmap_set_bit (bz, x);
571 if (bx)
573 /* If X has conflicts, add Y's to X. */
574 bitmap_ior_into (bx, by);
575 BITMAP_FREE (by);
576 ptr->conflicts[y] = NULL;
578 else
580 /* If X has no conflicts, simply use Y's. */
581 ptr->conflicts[x] = by;
582 ptr->conflicts[y] = NULL;
587 /* Dump a conflicts graph. */
589 static void
590 ssa_conflicts_dump (FILE *file, ssa_conflicts_p ptr)
592 unsigned x;
593 bitmap b;
595 fprintf (file, "\nConflict graph:\n");
597 FOR_EACH_VEC_ELT (ptr->conflicts, x, b)
598 if (b)
600 fprintf (file, "%d: ", x);
601 dump_bitmap (file, b);
606 /* This structure is used to efficiently record the current status of live
607 SSA_NAMES when building a conflict graph.
608 LIVE_BASE_VAR has a bit set for each base variable which has at least one
609 ssa version live.
610 LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
611 index, and is used to track what partitions of each base variable are
612 live. This makes it easy to add conflicts between just live partitions
613 with the same base variable.
614 The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
615 marked as being live. This delays clearing of these bitmaps until
616 they are actually needed again. */
618 typedef struct live_track_d
620 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
621 bitmap live_base_var; /* Indicates if a basevar is live. */
622 bitmap *live_base_partitions; /* Live partitions for each basevar. */
623 var_map map; /* Var_map being used for partition mapping. */
624 } * live_track_p;
627 /* This routine will create a new live track structure based on the partitions
628 in MAP. */
630 static live_track_p
631 new_live_track (var_map map)
633 live_track_p ptr;
634 int lim, x;
636 /* Make sure there is a partition view in place. */
637 gcc_assert (map->partition_to_base_index != NULL);
639 ptr = (live_track_p) xmalloc (sizeof (struct live_track_d));
640 ptr->map = map;
641 lim = num_basevars (map);
642 bitmap_obstack_initialize (&ptr->obstack);
643 ptr->live_base_partitions = (bitmap *) xmalloc (sizeof (bitmap *) * lim);
644 ptr->live_base_var = BITMAP_ALLOC (&ptr->obstack);
645 for (x = 0; x < lim; x++)
646 ptr->live_base_partitions[x] = BITMAP_ALLOC (&ptr->obstack);
647 return ptr;
651 /* This routine will free the memory associated with PTR. */
653 static void
654 delete_live_track (live_track_p ptr)
656 bitmap_obstack_release (&ptr->obstack);
657 free (ptr->live_base_partitions);
658 free (ptr);
662 /* This function will remove PARTITION from the live list in PTR. */
664 static inline void
665 live_track_remove_partition (live_track_p ptr, int partition)
667 int root;
669 root = basevar_index (ptr->map, partition);
670 bitmap_clear_bit (ptr->live_base_partitions[root], partition);
671 /* If the element list is empty, make the base variable not live either. */
672 if (bitmap_empty_p (ptr->live_base_partitions[root]))
673 bitmap_clear_bit (ptr->live_base_var, root);
677 /* This function will adds PARTITION to the live list in PTR. */
679 static inline void
680 live_track_add_partition (live_track_p ptr, int partition)
682 int root;
684 root = basevar_index (ptr->map, partition);
685 /* If this base var wasn't live before, it is now. Clear the element list
686 since it was delayed until needed. */
687 if (bitmap_set_bit (ptr->live_base_var, root))
688 bitmap_clear (ptr->live_base_partitions[root]);
689 bitmap_set_bit (ptr->live_base_partitions[root], partition);
694 /* Clear the live bit for VAR in PTR. */
696 static inline void
697 live_track_clear_var (live_track_p ptr, tree var)
699 int p;
701 p = var_to_partition (ptr->map, var);
702 if (p != NO_PARTITION)
703 live_track_remove_partition (ptr, p);
707 /* Return TRUE if VAR is live in PTR. */
709 static inline bool
710 live_track_live_p (live_track_p ptr, tree var)
712 int p, root;
714 p = var_to_partition (ptr->map, var);
715 if (p != NO_PARTITION)
717 root = basevar_index (ptr->map, p);
718 if (bitmap_bit_p (ptr->live_base_var, root))
719 return bitmap_bit_p (ptr->live_base_partitions[root], p);
721 return false;
725 /* This routine will add USE to PTR. USE will be marked as live in both the
726 ssa live map and the live bitmap for the root of USE. */
728 static inline void
729 live_track_process_use (live_track_p ptr, tree use)
731 int p;
733 p = var_to_partition (ptr->map, use);
734 if (p == NO_PARTITION)
735 return;
737 /* Mark as live in the appropriate live list. */
738 live_track_add_partition (ptr, p);
742 /* This routine will process a DEF in PTR. DEF will be removed from the live
743 lists, and if there are any other live partitions with the same base
744 variable, conflicts will be added to GRAPH. */
746 static inline void
747 live_track_process_def (live_track_p ptr, tree def, ssa_conflicts_p graph)
749 int p, root;
750 bitmap b;
751 unsigned x;
752 bitmap_iterator bi;
754 p = var_to_partition (ptr->map, def);
755 if (p == NO_PARTITION)
756 return;
758 /* Clear the liveness bit. */
759 live_track_remove_partition (ptr, p);
761 /* If the bitmap isn't empty now, conflicts need to be added. */
762 root = basevar_index (ptr->map, p);
763 if (bitmap_bit_p (ptr->live_base_var, root))
765 b = ptr->live_base_partitions[root];
766 EXECUTE_IF_SET_IN_BITMAP (b, 0, x, bi)
767 ssa_conflicts_add (graph, p, x);
772 /* Initialize PTR with the partitions set in INIT. */
774 static inline void
775 live_track_init (live_track_p ptr, bitmap init)
777 unsigned p;
778 bitmap_iterator bi;
780 /* Mark all live on exit partitions. */
781 EXECUTE_IF_SET_IN_BITMAP (init, 0, p, bi)
782 live_track_add_partition (ptr, p);
786 /* This routine will clear all live partitions in PTR. */
788 static inline void
789 live_track_clear_base_vars (live_track_p ptr)
791 /* Simply clear the live base list. Anything marked as live in the element
792 lists will be cleared later if/when the base variable ever comes alive
793 again. */
794 bitmap_clear (ptr->live_base_var);
798 /* Build a conflict graph based on LIVEINFO. Any partitions which are in the
799 partition view of the var_map liveinfo is based on get entries in the
800 conflict graph. Only conflicts between ssa_name partitions with the same
801 base variable are added. */
803 static ssa_conflicts_p
804 build_ssa_conflict_graph (tree_live_info_p liveinfo)
806 ssa_conflicts_p graph;
807 var_map map;
808 basic_block bb;
809 ssa_op_iter iter;
810 live_track_p live;
811 basic_block entry;
813 /* If inter-variable coalescing is enabled, we may attempt to
814 coalesce variables from different base variables, including
815 different parameters, so we have to make sure default defs live
816 at the entry block conflict with each other. */
817 if (flag_tree_coalesce_vars)
818 entry = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
819 else
820 entry = NULL;
822 map = live_var_map (liveinfo);
823 graph = ssa_conflicts_new (num_var_partitions (map));
825 live = new_live_track (map);
827 FOR_EACH_BB_FN (bb, cfun)
829 /* Start with live on exit temporaries. */
830 live_track_init (live, live_on_exit (liveinfo, bb));
832 for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);
833 gsi_prev (&gsi))
835 tree var;
836 gimple stmt = gsi_stmt (gsi);
838 /* A copy between 2 partitions does not introduce an interference
839 by itself. If they did, you would never be able to coalesce
840 two things which are copied. If the two variables really do
841 conflict, they will conflict elsewhere in the program.
843 This is handled by simply removing the SRC of the copy from the
844 live list, and processing the stmt normally. */
845 if (is_gimple_assign (stmt))
847 tree lhs = gimple_assign_lhs (stmt);
848 tree rhs1 = gimple_assign_rhs1 (stmt);
849 if (gimple_assign_copy_p (stmt)
850 && TREE_CODE (lhs) == SSA_NAME
851 && TREE_CODE (rhs1) == SSA_NAME)
852 live_track_clear_var (live, rhs1);
854 else if (is_gimple_debug (stmt))
855 continue;
857 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
858 live_track_process_def (live, var, graph);
860 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
861 live_track_process_use (live, var);
864 /* If result of a PHI is unused, looping over the statements will not
865 record any conflicts since the def was never live. Since the PHI node
866 is going to be translated out of SSA form, it will insert a copy.
867 There must be a conflict recorded between the result of the PHI and
868 any variables that are live. Otherwise the out-of-ssa translation
869 may create incorrect code. */
870 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
871 gsi_next (&gsi))
873 gphi *phi = gsi.phi ();
874 tree result = PHI_RESULT (phi);
875 if (live_track_live_p (live, result))
876 live_track_process_def (live, result, graph);
879 /* Pretend there are defs for params' default defs at the start
880 of the (post-)entry block. */
881 if (bb == entry)
883 unsigned base;
884 bitmap_iterator bi;
885 EXECUTE_IF_SET_IN_BITMAP (live->live_base_var, 0, base, bi)
887 bitmap_iterator bi2;
888 unsigned part;
889 EXECUTE_IF_SET_IN_BITMAP (live->live_base_partitions[base],
890 0, part, bi2)
892 tree var = partition_to_var (map, part);
893 if (!SSA_NAME_VAR (var)
894 || (TREE_CODE (SSA_NAME_VAR (var)) != PARM_DECL
895 && TREE_CODE (SSA_NAME_VAR (var)) != RESULT_DECL)
896 || !SSA_NAME_IS_DEFAULT_DEF (var))
897 continue;
898 live_track_process_def (live, var, graph);
903 live_track_clear_base_vars (live);
906 delete_live_track (live);
907 return graph;
911 /* Shortcut routine to print messages to file F of the form:
912 "STR1 EXPR1 STR2 EXPR2 STR3." */
914 static inline void
915 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
916 tree expr2, const char *str3)
918 fprintf (f, "%s", str1);
919 print_generic_expr (f, expr1, TDF_SLIM);
920 fprintf (f, "%s", str2);
921 print_generic_expr (f, expr2, TDF_SLIM);
922 fprintf (f, "%s", str3);
926 /* Print a failure to coalesce a MUST_COALESCE pair X and Y. */
928 static inline void
929 fail_abnormal_edge_coalesce (int x, int y)
931 fprintf (stderr, "\nUnable to coalesce ssa_names %d and %d",x, y);
932 fprintf (stderr, " which are marked as MUST COALESCE.\n");
933 print_generic_expr (stderr, ssa_name (x), TDF_SLIM);
934 fprintf (stderr, " and ");
935 print_generic_stmt (stderr, ssa_name (y), TDF_SLIM);
937 internal_error ("SSA corruption");
941 /* This function creates a var_map for the current function as well as creating
942 a coalesce list for use later in the out of ssa process. */
944 static var_map
945 create_outofssa_var_map (coalesce_list_p cl, bitmap used_in_copy)
947 gimple_stmt_iterator gsi;
948 basic_block bb;
949 tree var;
950 gimple stmt;
951 tree first;
952 var_map map;
953 ssa_op_iter iter;
954 int v1, v2, cost;
955 unsigned i;
957 map = init_var_map (num_ssa_names);
959 FOR_EACH_BB_FN (bb, cfun)
961 tree arg;
963 for (gphi_iterator gpi = gsi_start_phis (bb);
964 !gsi_end_p (gpi);
965 gsi_next (&gpi))
967 gphi *phi = gpi.phi ();
968 size_t i;
969 int ver;
970 tree res;
971 bool saw_copy = false;
973 res = gimple_phi_result (phi);
974 ver = SSA_NAME_VERSION (res);
975 register_ssa_partition (map, res);
977 /* Register ssa_names and coalesces between the args and the result
978 of all PHI. */
979 for (i = 0; i < gimple_phi_num_args (phi); i++)
981 edge e = gimple_phi_arg_edge (phi, i);
982 arg = PHI_ARG_DEF (phi, i);
983 if (TREE_CODE (arg) != SSA_NAME)
984 continue;
986 register_ssa_partition (map, arg);
987 if (gimple_can_coalesce_p (arg, res)
988 || (e->flags & EDGE_ABNORMAL))
990 saw_copy = true;
991 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (arg));
992 if ((e->flags & EDGE_ABNORMAL) == 0)
994 int cost = coalesce_cost_edge (e);
995 if (cost == 1 && has_single_use (arg))
996 add_cost_one_coalesce (cl, ver, SSA_NAME_VERSION (arg));
997 else
998 add_coalesce (cl, ver, SSA_NAME_VERSION (arg), cost);
1002 if (saw_copy)
1003 bitmap_set_bit (used_in_copy, ver);
1006 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1008 stmt = gsi_stmt (gsi);
1010 if (is_gimple_debug (stmt))
1011 continue;
1013 /* Register USE and DEF operands in each statement. */
1014 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
1015 register_ssa_partition (map, var);
1017 /* Check for copy coalesces. */
1018 switch (gimple_code (stmt))
1020 case GIMPLE_ASSIGN:
1022 tree lhs = gimple_assign_lhs (stmt);
1023 tree rhs1 = gimple_assign_rhs1 (stmt);
1024 if (gimple_assign_ssa_name_copy_p (stmt)
1025 && gimple_can_coalesce_p (lhs, rhs1))
1027 v1 = SSA_NAME_VERSION (lhs);
1028 v2 = SSA_NAME_VERSION (rhs1);
1029 cost = coalesce_cost_bb (bb);
1030 add_coalesce (cl, v1, v2, cost);
1031 bitmap_set_bit (used_in_copy, v1);
1032 bitmap_set_bit (used_in_copy, v2);
1035 break;
1037 case GIMPLE_ASM:
1039 gasm *asm_stmt = as_a <gasm *> (stmt);
1040 unsigned long noutputs, i;
1041 unsigned long ninputs;
1042 tree *outputs, link;
1043 noutputs = gimple_asm_noutputs (asm_stmt);
1044 ninputs = gimple_asm_ninputs (asm_stmt);
1045 outputs = (tree *) alloca (noutputs * sizeof (tree));
1046 for (i = 0; i < noutputs; ++i)
1048 link = gimple_asm_output_op (asm_stmt, i);
1049 outputs[i] = TREE_VALUE (link);
1052 for (i = 0; i < ninputs; ++i)
1054 const char *constraint;
1055 tree input;
1056 char *end;
1057 unsigned long match;
1059 link = gimple_asm_input_op (asm_stmt, i);
1060 constraint
1061 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1062 input = TREE_VALUE (link);
1064 if (TREE_CODE (input) != SSA_NAME)
1065 continue;
1067 match = strtoul (constraint, &end, 10);
1068 if (match >= noutputs || end == constraint)
1069 continue;
1071 if (TREE_CODE (outputs[match]) != SSA_NAME)
1072 continue;
1074 v1 = SSA_NAME_VERSION (outputs[match]);
1075 v2 = SSA_NAME_VERSION (input);
1077 if (gimple_can_coalesce_p (outputs[match], input))
1079 cost = coalesce_cost (REG_BR_PROB_BASE,
1080 optimize_bb_for_size_p (bb));
1081 add_coalesce (cl, v1, v2, cost);
1082 bitmap_set_bit (used_in_copy, v1);
1083 bitmap_set_bit (used_in_copy, v2);
1086 break;
1089 default:
1090 break;
1095 /* Now process result decls and live on entry variables for entry into
1096 the coalesce list. */
1097 first = NULL_TREE;
1098 for (i = 1; i < num_ssa_names; i++)
1100 var = ssa_name (i);
1101 if (var != NULL_TREE && !virtual_operand_p (var))
1103 /* Add coalesces between all the result decls. */
1104 if (SSA_NAME_VAR (var)
1105 && TREE_CODE (SSA_NAME_VAR (var)) == RESULT_DECL)
1107 if (first == NULL_TREE)
1108 first = var;
1109 else
1111 gcc_assert (gimple_can_coalesce_p (var, first));
1112 v1 = SSA_NAME_VERSION (first);
1113 v2 = SSA_NAME_VERSION (var);
1114 bitmap_set_bit (used_in_copy, v1);
1115 bitmap_set_bit (used_in_copy, v2);
1116 cost = coalesce_cost_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
1117 add_coalesce (cl, v1, v2, cost);
1120 /* Mark any default_def variables as being in the coalesce list
1121 since they will have to be coalesced with the base variable. If
1122 not marked as present, they won't be in the coalesce view. */
1123 if (SSA_NAME_IS_DEFAULT_DEF (var)
1124 && !has_zero_uses (var))
1125 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1129 return map;
1133 /* Attempt to coalesce ssa versions X and Y together using the partition
1134 mapping in MAP and checking conflicts in GRAPH. Output any debug info to
1135 DEBUG, if it is nun-NULL. */
1137 static inline bool
1138 attempt_coalesce (var_map map, ssa_conflicts_p graph, int x, int y,
1139 FILE *debug)
1141 int z;
1142 tree var1, var2;
1143 int p1, p2;
1145 p1 = var_to_partition (map, ssa_name (x));
1146 p2 = var_to_partition (map, ssa_name (y));
1148 if (debug)
1150 fprintf (debug, "(%d)", x);
1151 print_generic_expr (debug, partition_to_var (map, p1), TDF_SLIM);
1152 fprintf (debug, " & (%d)", y);
1153 print_generic_expr (debug, partition_to_var (map, p2), TDF_SLIM);
1156 if (p1 == p2)
1158 if (debug)
1159 fprintf (debug, ": Already Coalesced.\n");
1160 return true;
1163 if (debug)
1164 fprintf (debug, " [map: %d, %d] ", p1, p2);
1167 if (!ssa_conflicts_test_p (graph, p1, p2))
1169 var1 = partition_to_var (map, p1);
1170 var2 = partition_to_var (map, p2);
1172 z = var_union (map, var1, var2);
1173 if (z == NO_PARTITION)
1175 if (debug)
1176 fprintf (debug, ": Unable to perform partition union.\n");
1177 return false;
1180 /* z is the new combined partition. Remove the other partition from
1181 the list, and merge the conflicts. */
1182 if (z == p1)
1183 ssa_conflicts_merge (graph, p1, p2);
1184 else
1185 ssa_conflicts_merge (graph, p2, p1);
1187 if (debug)
1188 fprintf (debug, ": Success -> %d\n", z);
1190 return true;
1193 if (debug)
1194 fprintf (debug, ": Fail due to conflict\n");
1196 return false;
1200 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1201 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1203 static void
1204 coalesce_partitions (var_map map, ssa_conflicts_p graph, coalesce_list_p cl,
1205 FILE *debug)
1207 int x = 0, y = 0;
1208 tree var1, var2;
1209 int cost;
1210 basic_block bb;
1211 edge e;
1212 edge_iterator ei;
1214 /* First, coalesce all the copies across abnormal edges. These are not placed
1215 in the coalesce list because they do not need to be sorted, and simply
1216 consume extra memory/compilation time in large programs. */
1218 FOR_EACH_BB_FN (bb, cfun)
1220 FOR_EACH_EDGE (e, ei, bb->preds)
1221 if (e->flags & EDGE_ABNORMAL)
1223 gphi_iterator gsi;
1224 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1225 gsi_next (&gsi))
1227 gphi *phi = gsi.phi ();
1228 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1229 if (SSA_NAME_IS_DEFAULT_DEF (arg)
1230 && (!SSA_NAME_VAR (arg)
1231 || TREE_CODE (SSA_NAME_VAR (arg)) != PARM_DECL))
1232 continue;
1234 tree res = PHI_RESULT (phi);
1235 int v1 = SSA_NAME_VERSION (res);
1236 int v2 = SSA_NAME_VERSION (arg);
1238 if (debug)
1239 fprintf (debug, "Abnormal coalesce: ");
1241 if (!attempt_coalesce (map, graph, v1, v2, debug))
1242 fail_abnormal_edge_coalesce (v1, v2);
1247 /* Now process the items in the coalesce list. */
1249 while ((cost = pop_best_coalesce (cl, &x, &y)) != NO_BEST_COALESCE)
1251 var1 = ssa_name (x);
1252 var2 = ssa_name (y);
1254 /* Assert the coalesces have the same base variable. */
1255 gcc_assert (gimple_can_coalesce_p (var1, var2));
1257 if (debug)
1258 fprintf (debug, "Coalesce list: ");
1259 attempt_coalesce (map, graph, x, y, debug);
1264 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1266 struct ssa_name_var_hash : nofree_ptr_hash <tree_node>
1268 static inline hashval_t hash (const tree_node *);
1269 static inline int equal (const tree_node *, const tree_node *);
1272 inline hashval_t
1273 ssa_name_var_hash::hash (const_tree n)
1275 return DECL_UID (SSA_NAME_VAR (n));
1278 inline int
1279 ssa_name_var_hash::equal (const tree_node *n1, const tree_node *n2)
1281 return SSA_NAME_VAR (n1) == SSA_NAME_VAR (n2);
1285 /* Output partition map MAP with coalescing plan PART to file F. */
1287 void
1288 dump_part_var_map (FILE *f, partition part, var_map map)
1290 int t;
1291 unsigned x, y;
1292 int p;
1294 fprintf (f, "\nCoalescible Partition map \n\n");
1296 for (x = 0; x < map->num_partitions; x++)
1298 if (map->view_to_partition != NULL)
1299 p = map->view_to_partition[x];
1300 else
1301 p = x;
1303 if (ssa_name (p) == NULL_TREE
1304 || virtual_operand_p (ssa_name (p)))
1305 continue;
1307 t = 0;
1308 for (y = 1; y < num_ssa_names; y++)
1310 tree var = version_to_var (map, y);
1311 if (!var)
1312 continue;
1313 int q = var_to_partition (map, var);
1314 p = partition_find (part, q);
1315 gcc_assert (map->partition_to_base_index[q]
1316 == map->partition_to_base_index[p]);
1318 if (p == (int)x)
1320 if (t++ == 0)
1322 fprintf (f, "Partition %d, base %d (", x,
1323 map->partition_to_base_index[q]);
1324 print_generic_expr (f, partition_to_var (map, q), TDF_SLIM);
1325 fprintf (f, " - ");
1327 fprintf (f, "%d ", y);
1330 if (t != 0)
1331 fprintf (f, ")\n");
1333 fprintf (f, "\n");
1336 /* Given SSA_NAMEs NAME1 and NAME2, return true if they are candidates for
1337 coalescing together, false otherwise.
1339 This must stay consistent with var_map_base_init in tree-ssa-live.c. */
1341 bool
1342 gimple_can_coalesce_p (tree name1, tree name2)
1344 /* First check the SSA_NAME's associated DECL. Without
1345 optimization, we only want to coalesce if they have the same DECL
1346 or both have no associated DECL. */
1347 tree var1 = SSA_NAME_VAR (name1);
1348 tree var2 = SSA_NAME_VAR (name2);
1349 var1 = (var1 && (!VAR_P (var1) || !DECL_IGNORED_P (var1))) ? var1 : NULL_TREE;
1350 var2 = (var2 && (!VAR_P (var2) || !DECL_IGNORED_P (var2))) ? var2 : NULL_TREE;
1351 if (var1 != var2 && !flag_tree_coalesce_vars)
1352 return false;
1354 /* Now check the types. If the types are the same, then we should
1355 try to coalesce V1 and V2. */
1356 tree t1 = TREE_TYPE (name1);
1357 tree t2 = TREE_TYPE (name2);
1358 if (t1 == t2)
1360 check_modes:
1361 /* If the base variables are the same, we're good: none of the
1362 other tests below could possibly fail. */
1363 var1 = SSA_NAME_VAR (name1);
1364 var2 = SSA_NAME_VAR (name2);
1365 if (var1 == var2)
1366 return true;
1368 /* We don't want to coalesce two SSA names if one of the base
1369 variables is supposed to be a register while the other is
1370 supposed to be on the stack. Anonymous SSA names take
1371 registers, but when not optimizing, user variables should go
1372 on the stack, so coalescing them with the anonymous variable
1373 as the partition leader would end up assigning the user
1374 variable to a register. Don't do that! */
1375 bool reg1 = !var1 || use_register_for_decl (var1);
1376 bool reg2 = !var2 || use_register_for_decl (var2);
1377 if (reg1 != reg2)
1378 return false;
1380 /* Check that the promoted modes are the same. We don't want to
1381 coalesce if the promoted modes would be different. Only
1382 PARM_DECLs and RESULT_DECLs have different promotion rules,
1383 so skip the test if both are variables, or both are anonymous
1384 SSA_NAMEs. Now, if a parm or result has BLKmode, do not
1385 coalesce its SSA versions with those of any other variables,
1386 because it may be passed by reference. */
1387 return ((!var1 || VAR_P (var1)) && (!var2 || VAR_P (var2)))
1388 || (/* The case var1 == var2 is already covered above. */
1389 !parm_in_stack_slot_p (var1)
1390 && !parm_in_stack_slot_p (var2)
1391 && promote_ssa_mode (name1, NULL) == promote_ssa_mode (name2, NULL));
1394 /* If the types are not the same, check for a canonical type match. This
1395 (for example) allows coalescing when the types are fundamentally the
1396 same, but just have different names.
1398 Note pointer types with different address spaces may have the same
1399 canonical type. Those are rejected for coalescing by the
1400 types_compatible_p check. */
1401 if (TYPE_CANONICAL (t1)
1402 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)
1403 && types_compatible_p (t1, t2))
1404 goto check_modes;
1406 return false;
1409 /* Fill in MAP's partition_to_base_index, with one index for each
1410 partition of SSA names USED_IN_COPIES and related by CL coalesce
1411 possibilities. This must match gimple_can_coalesce_p in the
1412 optimized case. */
1414 static void
1415 compute_optimized_partition_bases (var_map map, bitmap used_in_copies,
1416 coalesce_list_p cl)
1418 int parts = num_var_partitions (map);
1419 partition tentative = partition_new (parts);
1421 /* Partition the SSA versions so that, for each coalescible
1422 pair, both of its members are in the same partition in
1423 TENTATIVE. */
1424 gcc_assert (!cl->sorted);
1425 coalesce_pair_p node;
1426 coalesce_iterator_type ppi;
1427 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
1429 tree v1 = ssa_name (node->first_element);
1430 int p1 = partition_find (tentative, var_to_partition (map, v1));
1431 tree v2 = ssa_name (node->second_element);
1432 int p2 = partition_find (tentative, var_to_partition (map, v2));
1434 if (p1 == p2)
1435 continue;
1437 partition_union (tentative, p1, p2);
1440 /* We have to deal with cost one pairs too. */
1441 for (cost_one_pair_d *co = cl->cost_one_list; co; co = co->next)
1443 tree v1 = ssa_name (co->first_element);
1444 int p1 = partition_find (tentative, var_to_partition (map, v1));
1445 tree v2 = ssa_name (co->second_element);
1446 int p2 = partition_find (tentative, var_to_partition (map, v2));
1448 if (p1 == p2)
1449 continue;
1451 partition_union (tentative, p1, p2);
1454 /* And also with abnormal edges. */
1455 basic_block bb;
1456 edge e;
1457 edge_iterator ei;
1458 FOR_EACH_BB_FN (bb, cfun)
1460 FOR_EACH_EDGE (e, ei, bb->preds)
1461 if (e->flags & EDGE_ABNORMAL)
1463 gphi_iterator gsi;
1464 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1465 gsi_next (&gsi))
1467 gphi *phi = gsi.phi ();
1468 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1469 if (SSA_NAME_IS_DEFAULT_DEF (arg)
1470 && (!SSA_NAME_VAR (arg)
1471 || TREE_CODE (SSA_NAME_VAR (arg)) != PARM_DECL))
1472 continue;
1474 tree res = PHI_RESULT (phi);
1476 int p1 = partition_find (tentative, var_to_partition (map, res));
1477 int p2 = partition_find (tentative, var_to_partition (map, arg));
1479 if (p1 == p2)
1480 continue;
1482 partition_union (tentative, p1, p2);
1487 map->partition_to_base_index = XCNEWVEC (int, parts);
1488 auto_vec<unsigned int> index_map (parts);
1489 if (parts)
1490 index_map.quick_grow (parts);
1492 const unsigned no_part = -1;
1493 unsigned count = parts;
1494 while (count)
1495 index_map[--count] = no_part;
1497 /* Initialize MAP's mapping from partition to base index, using
1498 as base indices an enumeration of the TENTATIVE partitions in
1499 which each SSA version ended up, so that we compute conflicts
1500 between all SSA versions that ended up in the same potential
1501 coalesce partition. */
1502 bitmap_iterator bi;
1503 unsigned i;
1504 EXECUTE_IF_SET_IN_BITMAP (used_in_copies, 0, i, bi)
1506 int pidx = var_to_partition (map, ssa_name (i));
1507 int base = partition_find (tentative, pidx);
1508 if (index_map[base] != no_part)
1509 continue;
1510 index_map[base] = count++;
1513 map->num_basevars = count;
1515 EXECUTE_IF_SET_IN_BITMAP (used_in_copies, 0, i, bi)
1517 int pidx = var_to_partition (map, ssa_name (i));
1518 int base = partition_find (tentative, pidx);
1519 gcc_assert (index_map[base] < count);
1520 map->partition_to_base_index[pidx] = index_map[base];
1523 if (dump_file && (dump_flags & TDF_DETAILS))
1524 dump_part_var_map (dump_file, tentative, map);
1526 partition_delete (tentative);
1529 /* Hashtable helpers. */
1531 struct tree_int_map_hasher : nofree_ptr_hash <tree_int_map>
1533 static inline hashval_t hash (const tree_int_map *);
1534 static inline bool equal (const tree_int_map *, const tree_int_map *);
1537 inline hashval_t
1538 tree_int_map_hasher::hash (const tree_int_map *v)
1540 return tree_map_base_hash (v);
1543 inline bool
1544 tree_int_map_hasher::equal (const tree_int_map *v, const tree_int_map *c)
1546 return tree_int_map_eq (v, c);
1549 /* This routine will initialize the basevar fields of MAP with base
1550 names. Partitions will share the same base if they have the same
1551 SSA_NAME_VAR, or, being anonymous variables, the same type. This
1552 must match gimple_can_coalesce_p in the non-optimized case. */
1554 static void
1555 compute_samebase_partition_bases (var_map map)
1557 int x, num_part;
1558 tree var;
1559 struct tree_int_map *m, *mapstorage;
1561 num_part = num_var_partitions (map);
1562 hash_table<tree_int_map_hasher> tree_to_index (num_part);
1563 /* We can have at most num_part entries in the hash tables, so it's
1564 enough to allocate so many map elements once, saving some malloc
1565 calls. */
1566 mapstorage = m = XNEWVEC (struct tree_int_map, num_part);
1568 /* If a base table already exists, clear it, otherwise create it. */
1569 free (map->partition_to_base_index);
1570 map->partition_to_base_index = (int *) xmalloc (sizeof (int) * num_part);
1572 /* Build the base variable list, and point partitions at their bases. */
1573 for (x = 0; x < num_part; x++)
1575 struct tree_int_map **slot;
1576 unsigned baseindex;
1577 var = partition_to_var (map, x);
1578 if (SSA_NAME_VAR (var)
1579 && (!VAR_P (SSA_NAME_VAR (var))
1580 || !DECL_IGNORED_P (SSA_NAME_VAR (var))))
1581 m->base.from = SSA_NAME_VAR (var);
1582 else
1583 /* This restricts what anonymous SSA names we can coalesce
1584 as it restricts the sets we compute conflicts for.
1585 Using TREE_TYPE to generate sets is the easies as
1586 type equivalency also holds for SSA names with the same
1587 underlying decl.
1589 Check gimple_can_coalesce_p when changing this code. */
1590 m->base.from = (TYPE_CANONICAL (TREE_TYPE (var))
1591 ? TYPE_CANONICAL (TREE_TYPE (var))
1592 : TREE_TYPE (var));
1593 /* If base variable hasn't been seen, set it up. */
1594 slot = tree_to_index.find_slot (m, INSERT);
1595 if (!*slot)
1597 baseindex = m - mapstorage;
1598 m->to = baseindex;
1599 *slot = m;
1600 m++;
1602 else
1603 baseindex = (*slot)->to;
1604 map->partition_to_base_index[x] = baseindex;
1607 map->num_basevars = m - mapstorage;
1609 free (mapstorage);
1612 /* Reduce the number of copies by coalescing variables in the function. Return
1613 a partition map with the resulting coalesces. */
1615 extern var_map
1616 coalesce_ssa_name (void)
1618 tree_live_info_p liveinfo;
1619 ssa_conflicts_p graph;
1620 coalesce_list_p cl;
1621 bitmap used_in_copies = BITMAP_ALLOC (NULL);
1622 var_map map;
1623 unsigned int i;
1625 cl = create_coalesce_list ();
1626 map = create_outofssa_var_map (cl, used_in_copies);
1628 /* If this optimization is disabled, we need to coalesce all the
1629 names originating from the same SSA_NAME_VAR so debug info
1630 remains undisturbed. */
1631 if (!flag_tree_coalesce_vars)
1633 hash_table<ssa_name_var_hash> ssa_name_hash (10);
1635 for (i = 1; i < num_ssa_names; i++)
1637 tree a = ssa_name (i);
1639 if (a
1640 && SSA_NAME_VAR (a)
1641 && !DECL_IGNORED_P (SSA_NAME_VAR (a))
1642 && (!has_zero_uses (a) || !SSA_NAME_IS_DEFAULT_DEF (a)))
1644 tree *slot = ssa_name_hash.find_slot (a, INSERT);
1646 if (!*slot)
1647 *slot = a;
1648 else
1650 /* If the variable is a PARM_DECL or a RESULT_DECL, we
1651 _require_ that all the names originating from it be
1652 coalesced, because there must be a single partition
1653 containing all the names so that it can be assigned
1654 the canonical RTL location of the DECL safely.
1655 If in_lto_p, a function could have been compiled
1656 originally with optimizations and only the link
1657 performed at -O0, so we can't actually require it. */
1658 const int cost
1659 = (TREE_CODE (SSA_NAME_VAR (a)) == VAR_DECL || in_lto_p)
1660 ? MUST_COALESCE_COST - 1 : MUST_COALESCE_COST;
1661 add_coalesce (cl, SSA_NAME_VERSION (a),
1662 SSA_NAME_VERSION (*slot), cost);
1663 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (a));
1664 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (*slot));
1669 if (dump_file && (dump_flags & TDF_DETAILS))
1670 dump_var_map (dump_file, map);
1672 partition_view_bitmap (map, used_in_copies);
1674 if (flag_tree_coalesce_vars)
1675 compute_optimized_partition_bases (map, used_in_copies, cl);
1676 else
1677 compute_samebase_partition_bases (map);
1679 BITMAP_FREE (used_in_copies);
1681 if (num_var_partitions (map) < 1)
1683 delete_coalesce_list (cl);
1684 return map;
1687 if (dump_file && (dump_flags & TDF_DETAILS))
1688 dump_var_map (dump_file, map);
1690 liveinfo = calculate_live_ranges (map, false);
1692 if (dump_file && (dump_flags & TDF_DETAILS))
1693 dump_live_info (dump_file, liveinfo, LIVEDUMP_ENTRY);
1695 /* Build a conflict graph. */
1696 graph = build_ssa_conflict_graph (liveinfo);
1697 delete_tree_live_info (liveinfo);
1698 if (dump_file && (dump_flags & TDF_DETAILS))
1699 ssa_conflicts_dump (dump_file, graph);
1701 sort_coalesce_list (cl);
1703 if (dump_file && (dump_flags & TDF_DETAILS))
1705 fprintf (dump_file, "\nAfter sorting:\n");
1706 dump_coalesce_list (dump_file, cl);
1709 /* First, coalesce all live on entry variables to their base variable.
1710 This will ensure the first use is coming from the correct location. */
1712 if (dump_file && (dump_flags & TDF_DETAILS))
1713 dump_var_map (dump_file, map);
1715 /* Now coalesce everything in the list. */
1716 coalesce_partitions (map, graph, cl,
1717 ((dump_flags & TDF_DETAILS) ? dump_file : NULL));
1719 delete_coalesce_list (cl);
1720 ssa_conflicts_delete (graph);
1722 return map;