* config/msp430/msp430.c (msp430_start_function): Add function type.
[official-gcc.git] / gcc / tree-ssa-coalesce.c
blob6624e7e5a9b6f85bca2efe04de9f92da6f6d3614
1 /* Coalesce SSA_NAMES together for the out-of-ssa pass.
2 Copyright (C) 2004-2016 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "predict.h"
28 #include "tm_p.h"
29 #include "ssa.h"
30 #include "tree-pretty-print.h"
31 #include "diagnostic-core.h"
32 #include "dumpfile.h"
33 #include "gimple-iterator.h"
34 #include "tree-ssa-live.h"
35 #include "tree-ssa-coalesce.h"
36 #include "explow.h"
37 #include "tree-dfa.h"
38 #include "stor-layout.h"
40 /* This set of routines implements a coalesce_list. This is an object which
41 is used to track pairs of ssa_names which are desirable to coalesce
42 together to avoid copies. Costs are associated with each pair, and when
43 all desired information has been collected, the object can be used to
44 order the pairs for processing. */
46 /* This structure defines a pair entry. */
48 struct coalesce_pair
50 int first_element;
51 int second_element;
52 int cost;
55 /* Coalesce pair hashtable helpers. */
57 struct coalesce_pair_hasher : nofree_ptr_hash <coalesce_pair>
59 static inline hashval_t hash (const coalesce_pair *);
60 static inline bool equal (const coalesce_pair *, const coalesce_pair *);
63 /* Hash function for coalesce list. Calculate hash for PAIR. */
65 inline hashval_t
66 coalesce_pair_hasher::hash (const coalesce_pair *pair)
68 hashval_t a = (hashval_t)(pair->first_element);
69 hashval_t b = (hashval_t)(pair->second_element);
71 return b * (b - 1) / 2 + a;
74 /* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
75 returning TRUE if the two pairs are equivalent. */
77 inline bool
78 coalesce_pair_hasher::equal (const coalesce_pair *p1, const coalesce_pair *p2)
80 return (p1->first_element == p2->first_element
81 && p1->second_element == p2->second_element);
84 typedef hash_table<coalesce_pair_hasher> coalesce_table_type;
85 typedef coalesce_table_type::iterator coalesce_iterator_type;
88 struct cost_one_pair
90 int first_element;
91 int second_element;
92 cost_one_pair *next;
95 /* This structure maintains the list of coalesce pairs. */
97 struct coalesce_list
99 coalesce_table_type *list; /* Hash table. */
100 coalesce_pair **sorted; /* List when sorted. */
101 int num_sorted; /* Number in the sorted list. */
102 cost_one_pair *cost_one_list;/* Single use coalesces with cost 1. */
105 #define NO_BEST_COALESCE -1
106 #define MUST_COALESCE_COST INT_MAX
109 /* Return cost of execution of copy instruction with FREQUENCY. */
111 static inline int
112 coalesce_cost (int frequency, bool optimize_for_size)
114 /* Base costs on BB frequencies bounded by 1. */
115 int cost = frequency;
117 if (!cost)
118 cost = 1;
120 if (optimize_for_size)
121 cost = 1;
123 return cost;
127 /* Return the cost of executing a copy instruction in basic block BB. */
129 static inline int
130 coalesce_cost_bb (basic_block bb)
132 return coalesce_cost (bb->frequency, optimize_bb_for_size_p (bb));
136 /* Return the cost of executing a copy instruction on edge E. */
138 static inline int
139 coalesce_cost_edge (edge e)
141 int mult = 1;
143 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
144 if (EDGE_CRITICAL_P (e))
145 mult = 2;
146 if (e->flags & EDGE_ABNORMAL)
147 return MUST_COALESCE_COST;
148 if (e->flags & EDGE_EH)
150 edge e2;
151 edge_iterator ei;
152 FOR_EACH_EDGE (e2, ei, e->dest->preds)
153 if (e2 != e)
155 /* Putting code on EH edge that leads to BB
156 with multiple predecestors imply splitting of
157 edge too. */
158 if (mult < 2)
159 mult = 2;
160 /* If there are multiple EH predecestors, we
161 also copy EH regions and produce separate
162 landing pad. This is expensive. */
163 if (e2->flags & EDGE_EH)
165 mult = 5;
166 break;
171 return coalesce_cost (EDGE_FREQUENCY (e),
172 optimize_edge_for_size_p (e)) * mult;
176 /* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
177 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
178 NO_BEST_COALESCE is returned if there aren't any. */
180 static inline int
181 pop_cost_one_pair (coalesce_list *cl, int *p1, int *p2)
183 cost_one_pair *ptr;
185 ptr = cl->cost_one_list;
186 if (!ptr)
187 return NO_BEST_COALESCE;
189 *p1 = ptr->first_element;
190 *p2 = ptr->second_element;
191 cl->cost_one_list = ptr->next;
193 free (ptr);
195 return 1;
198 /* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
199 2 elements via P1 and P2. Their calculated cost is returned by the function.
200 NO_BEST_COALESCE is returned if the coalesce list is empty. */
202 static inline int
203 pop_best_coalesce (coalesce_list *cl, int *p1, int *p2)
205 coalesce_pair *node;
206 int ret;
208 if (cl->sorted == NULL)
209 return pop_cost_one_pair (cl, p1, p2);
211 if (cl->num_sorted == 0)
212 return pop_cost_one_pair (cl, p1, p2);
214 node = cl->sorted[--(cl->num_sorted)];
215 *p1 = node->first_element;
216 *p2 = node->second_element;
217 ret = node->cost;
218 free (node);
220 return ret;
224 /* Create a new empty coalesce list object and return it. */
226 static inline coalesce_list *
227 create_coalesce_list (void)
229 coalesce_list *list;
230 unsigned size = num_ssa_names * 3;
232 if (size < 40)
233 size = 40;
235 list = (coalesce_list *) xmalloc (sizeof (struct coalesce_list));
236 list->list = new coalesce_table_type (size);
237 list->sorted = NULL;
238 list->num_sorted = 0;
239 list->cost_one_list = NULL;
240 return list;
244 /* Delete coalesce list CL. */
246 static inline void
247 delete_coalesce_list (coalesce_list *cl)
249 gcc_assert (cl->cost_one_list == NULL);
250 delete cl->list;
251 cl->list = NULL;
252 free (cl->sorted);
253 gcc_assert (cl->num_sorted == 0);
254 free (cl);
258 /* Find a matching coalesce pair object in CL for the pair P1 and P2. If
259 one isn't found, return NULL if CREATE is false, otherwise create a new
260 coalesce pair object and return it. */
262 static coalesce_pair *
263 find_coalesce_pair (coalesce_list *cl, int p1, int p2, bool create)
265 struct coalesce_pair p;
266 coalesce_pair **slot;
267 unsigned int hash;
269 /* Normalize so that p1 is the smaller value. */
270 if (p2 < p1)
272 p.first_element = p2;
273 p.second_element = p1;
275 else
277 p.first_element = p1;
278 p.second_element = p2;
281 hash = coalesce_pair_hasher::hash (&p);
282 slot = cl->list->find_slot_with_hash (&p, hash, create ? INSERT : NO_INSERT);
283 if (!slot)
284 return NULL;
286 if (!*slot)
288 struct coalesce_pair * pair = XNEW (struct coalesce_pair);
289 gcc_assert (cl->sorted == NULL);
290 pair->first_element = p.first_element;
291 pair->second_element = p.second_element;
292 pair->cost = 0;
293 *slot = pair;
296 return (struct coalesce_pair *) *slot;
299 static inline void
300 add_cost_one_coalesce (coalesce_list *cl, int p1, int p2)
302 cost_one_pair *pair;
304 pair = XNEW (cost_one_pair);
305 pair->first_element = p1;
306 pair->second_element = p2;
307 pair->next = cl->cost_one_list;
308 cl->cost_one_list = pair;
312 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
314 static inline void
315 add_coalesce (coalesce_list *cl, int p1, int p2, int value)
317 coalesce_pair *node;
319 gcc_assert (cl->sorted == NULL);
320 if (p1 == p2)
321 return;
323 node = find_coalesce_pair (cl, p1, p2, true);
325 /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way. */
326 if (node->cost < MUST_COALESCE_COST - 1)
328 if (value < MUST_COALESCE_COST - 1)
329 node->cost += value;
330 else
331 node->cost = value;
336 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
338 static int
339 compare_pairs (const void *p1, const void *p2)
341 const coalesce_pair *const *const pp1 = (const coalesce_pair *const *) p1;
342 const coalesce_pair *const *const pp2 = (const coalesce_pair *const *) p2;
343 int result;
345 result = (* pp1)->cost - (* pp2)->cost;
346 /* Since qsort does not guarantee stability we use the elements
347 as a secondary key. This provides us with independence from
348 the host's implementation of the sorting algorithm. */
349 if (result == 0)
351 result = (* pp2)->first_element - (* pp1)->first_element;
352 if (result == 0)
353 result = (* pp2)->second_element - (* pp1)->second_element;
356 return result;
360 /* Return the number of unique coalesce pairs in CL. */
362 static inline int
363 num_coalesce_pairs (coalesce_list *cl)
365 return cl->list->elements ();
369 /* Iterate over CL using ITER, returning values in PAIR. */
371 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
372 FOR_EACH_HASH_TABLE_ELEMENT (*(CL)->list, (PAIR), coalesce_pair_p, (ITER))
375 /* Prepare CL for removal of preferred pairs. When finished they are sorted
376 in order from most important coalesce to least important. */
378 static void
379 sort_coalesce_list (coalesce_list *cl)
381 unsigned x, num;
382 coalesce_pair *p;
383 coalesce_iterator_type ppi;
385 gcc_assert (cl->sorted == NULL);
387 num = num_coalesce_pairs (cl);
388 cl->num_sorted = num;
389 if (num == 0)
390 return;
392 /* Allocate a vector for the pair pointers. */
393 cl->sorted = XNEWVEC (coalesce_pair *, num);
395 /* Populate the vector with pointers to the pairs. */
396 x = 0;
397 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
398 cl->sorted[x++] = p;
399 gcc_assert (x == num);
401 /* Already sorted. */
402 if (num == 1)
403 return;
405 /* If there are only 2, just pick swap them if the order isn't correct. */
406 if (num == 2)
408 if (cl->sorted[0]->cost > cl->sorted[1]->cost)
409 std::swap (cl->sorted[0], cl->sorted[1]);
410 return;
413 /* Only call qsort if there are more than 2 items.
414 ??? Maybe std::sort will do better, provided that compare_pairs
415 can be inlined. */
416 if (num > 2)
417 qsort (cl->sorted, num, sizeof (coalesce_pair *), compare_pairs);
421 /* Send debug info for coalesce list CL to file F. */
423 static void
424 dump_coalesce_list (FILE *f, coalesce_list *cl)
426 coalesce_pair *node;
427 coalesce_iterator_type ppi;
429 int x;
430 tree var;
432 if (cl->sorted == NULL)
434 fprintf (f, "Coalesce List:\n");
435 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
437 tree var1 = ssa_name (node->first_element);
438 tree var2 = ssa_name (node->second_element);
439 print_generic_expr (f, var1, TDF_SLIM);
440 fprintf (f, " <-> ");
441 print_generic_expr (f, var2, TDF_SLIM);
442 fprintf (f, " (%1d), ", node->cost);
443 fprintf (f, "\n");
446 else
448 fprintf (f, "Sorted Coalesce list:\n");
449 for (x = cl->num_sorted - 1 ; x >=0; x--)
451 node = cl->sorted[x];
452 fprintf (f, "(%d) ", node->cost);
453 var = ssa_name (node->first_element);
454 print_generic_expr (f, var, TDF_SLIM);
455 fprintf (f, " <-> ");
456 var = ssa_name (node->second_element);
457 print_generic_expr (f, var, TDF_SLIM);
458 fprintf (f, "\n");
464 /* This represents a conflict graph. Implemented as an array of bitmaps.
465 A full matrix is used for conflicts rather than just upper triangular form.
466 this make sit much simpler and faster to perform conflict merges. */
468 struct ssa_conflicts
470 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
471 vec<bitmap> conflicts;
474 /* Return an empty new conflict graph for SIZE elements. */
476 static inline ssa_conflicts *
477 ssa_conflicts_new (unsigned size)
479 ssa_conflicts *ptr;
481 ptr = XNEW (ssa_conflicts);
482 bitmap_obstack_initialize (&ptr->obstack);
483 ptr->conflicts.create (size);
484 ptr->conflicts.safe_grow_cleared (size);
485 return ptr;
489 /* Free storage for conflict graph PTR. */
491 static inline void
492 ssa_conflicts_delete (ssa_conflicts *ptr)
494 bitmap_obstack_release (&ptr->obstack);
495 ptr->conflicts.release ();
496 free (ptr);
500 /* Test if elements X and Y conflict in graph PTR. */
502 static inline bool
503 ssa_conflicts_test_p (ssa_conflicts *ptr, unsigned x, unsigned y)
505 bitmap bx = ptr->conflicts[x];
506 bitmap by = ptr->conflicts[y];
508 gcc_checking_assert (x != y);
510 if (bx)
511 /* Avoid the lookup if Y has no conflicts. */
512 return by ? bitmap_bit_p (bx, y) : false;
513 else
514 return false;
518 /* Add a conflict with Y to the bitmap for X in graph PTR. */
520 static inline void
521 ssa_conflicts_add_one (ssa_conflicts *ptr, unsigned x, unsigned y)
523 bitmap bx = ptr->conflicts[x];
524 /* If there are no conflicts yet, allocate the bitmap and set bit. */
525 if (! bx)
526 bx = ptr->conflicts[x] = BITMAP_ALLOC (&ptr->obstack);
527 bitmap_set_bit (bx, y);
531 /* Add conflicts between X and Y in graph PTR. */
533 static inline void
534 ssa_conflicts_add (ssa_conflicts *ptr, unsigned x, unsigned y)
536 gcc_checking_assert (x != y);
537 ssa_conflicts_add_one (ptr, x, y);
538 ssa_conflicts_add_one (ptr, y, x);
542 /* Merge all Y's conflict into X in graph PTR. */
544 static inline void
545 ssa_conflicts_merge (ssa_conflicts *ptr, unsigned x, unsigned y)
547 unsigned z;
548 bitmap_iterator bi;
549 bitmap bx = ptr->conflicts[x];
550 bitmap by = ptr->conflicts[y];
552 gcc_checking_assert (x != y);
553 if (! by)
554 return;
556 /* Add a conflict between X and every one Y has. If the bitmap doesn't
557 exist, then it has already been coalesced, and we don't need to add a
558 conflict. */
559 EXECUTE_IF_SET_IN_BITMAP (by, 0, z, bi)
561 bitmap bz = ptr->conflicts[z];
562 if (bz)
563 bitmap_set_bit (bz, x);
566 if (bx)
568 /* If X has conflicts, add Y's to X. */
569 bitmap_ior_into (bx, by);
570 BITMAP_FREE (by);
571 ptr->conflicts[y] = NULL;
573 else
575 /* If X has no conflicts, simply use Y's. */
576 ptr->conflicts[x] = by;
577 ptr->conflicts[y] = NULL;
582 /* Dump a conflicts graph. */
584 static void
585 ssa_conflicts_dump (FILE *file, ssa_conflicts *ptr)
587 unsigned x;
588 bitmap b;
590 fprintf (file, "\nConflict graph:\n");
592 FOR_EACH_VEC_ELT (ptr->conflicts, x, b)
593 if (b)
595 fprintf (file, "%d: ", x);
596 dump_bitmap (file, b);
601 /* This structure is used to efficiently record the current status of live
602 SSA_NAMES when building a conflict graph.
603 LIVE_BASE_VAR has a bit set for each base variable which has at least one
604 ssa version live.
605 LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
606 index, and is used to track what partitions of each base variable are
607 live. This makes it easy to add conflicts between just live partitions
608 with the same base variable.
609 The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
610 marked as being live. This delays clearing of these bitmaps until
611 they are actually needed again. */
613 struct live_track
615 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
616 bitmap live_base_var; /* Indicates if a basevar is live. */
617 bitmap *live_base_partitions; /* Live partitions for each basevar. */
618 var_map map; /* Var_map being used for partition mapping. */
622 /* This routine will create a new live track structure based on the partitions
623 in MAP. */
625 static live_track *
626 new_live_track (var_map map)
628 live_track *ptr;
629 int lim, x;
631 /* Make sure there is a partition view in place. */
632 gcc_assert (map->partition_to_base_index != NULL);
634 ptr = (live_track *) xmalloc (sizeof (live_track));
635 ptr->map = map;
636 lim = num_basevars (map);
637 bitmap_obstack_initialize (&ptr->obstack);
638 ptr->live_base_partitions = (bitmap *) xmalloc (sizeof (bitmap *) * lim);
639 ptr->live_base_var = BITMAP_ALLOC (&ptr->obstack);
640 for (x = 0; x < lim; x++)
641 ptr->live_base_partitions[x] = BITMAP_ALLOC (&ptr->obstack);
642 return ptr;
646 /* This routine will free the memory associated with PTR. */
648 static void
649 delete_live_track (live_track *ptr)
651 bitmap_obstack_release (&ptr->obstack);
652 free (ptr->live_base_partitions);
653 free (ptr);
657 /* This function will remove PARTITION from the live list in PTR. */
659 static inline void
660 live_track_remove_partition (live_track *ptr, int partition)
662 int root;
664 root = basevar_index (ptr->map, partition);
665 bitmap_clear_bit (ptr->live_base_partitions[root], partition);
666 /* If the element list is empty, make the base variable not live either. */
667 if (bitmap_empty_p (ptr->live_base_partitions[root]))
668 bitmap_clear_bit (ptr->live_base_var, root);
672 /* This function will adds PARTITION to the live list in PTR. */
674 static inline void
675 live_track_add_partition (live_track *ptr, int partition)
677 int root;
679 root = basevar_index (ptr->map, partition);
680 /* If this base var wasn't live before, it is now. Clear the element list
681 since it was delayed until needed. */
682 if (bitmap_set_bit (ptr->live_base_var, root))
683 bitmap_clear (ptr->live_base_partitions[root]);
684 bitmap_set_bit (ptr->live_base_partitions[root], partition);
689 /* Clear the live bit for VAR in PTR. */
691 static inline void
692 live_track_clear_var (live_track *ptr, tree var)
694 int p;
696 p = var_to_partition (ptr->map, var);
697 if (p != NO_PARTITION)
698 live_track_remove_partition (ptr, p);
702 /* Return TRUE if VAR is live in PTR. */
704 static inline bool
705 live_track_live_p (live_track *ptr, tree var)
707 int p, root;
709 p = var_to_partition (ptr->map, var);
710 if (p != NO_PARTITION)
712 root = basevar_index (ptr->map, p);
713 if (bitmap_bit_p (ptr->live_base_var, root))
714 return bitmap_bit_p (ptr->live_base_partitions[root], p);
716 return false;
720 /* This routine will add USE to PTR. USE will be marked as live in both the
721 ssa live map and the live bitmap for the root of USE. */
723 static inline void
724 live_track_process_use (live_track *ptr, tree use)
726 int p;
728 p = var_to_partition (ptr->map, use);
729 if (p == NO_PARTITION)
730 return;
732 /* Mark as live in the appropriate live list. */
733 live_track_add_partition (ptr, p);
737 /* This routine will process a DEF in PTR. DEF will be removed from the live
738 lists, and if there are any other live partitions with the same base
739 variable, conflicts will be added to GRAPH. */
741 static inline void
742 live_track_process_def (live_track *ptr, tree def, ssa_conflicts *graph)
744 int p, root;
745 bitmap b;
746 unsigned x;
747 bitmap_iterator bi;
749 p = var_to_partition (ptr->map, def);
750 if (p == NO_PARTITION)
751 return;
753 /* Clear the liveness bit. */
754 live_track_remove_partition (ptr, p);
756 /* If the bitmap isn't empty now, conflicts need to be added. */
757 root = basevar_index (ptr->map, p);
758 if (bitmap_bit_p (ptr->live_base_var, root))
760 b = ptr->live_base_partitions[root];
761 EXECUTE_IF_SET_IN_BITMAP (b, 0, x, bi)
762 ssa_conflicts_add (graph, p, x);
767 /* Initialize PTR with the partitions set in INIT. */
769 static inline void
770 live_track_init (live_track *ptr, bitmap init)
772 unsigned p;
773 bitmap_iterator bi;
775 /* Mark all live on exit partitions. */
776 EXECUTE_IF_SET_IN_BITMAP (init, 0, p, bi)
777 live_track_add_partition (ptr, p);
781 /* This routine will clear all live partitions in PTR. */
783 static inline void
784 live_track_clear_base_vars (live_track *ptr)
786 /* Simply clear the live base list. Anything marked as live in the element
787 lists will be cleared later if/when the base variable ever comes alive
788 again. */
789 bitmap_clear (ptr->live_base_var);
793 /* Build a conflict graph based on LIVEINFO. Any partitions which are in the
794 partition view of the var_map liveinfo is based on get entries in the
795 conflict graph. Only conflicts between ssa_name partitions with the same
796 base variable are added. */
798 static ssa_conflicts *
799 build_ssa_conflict_graph (tree_live_info_p liveinfo)
801 ssa_conflicts *graph;
802 var_map map;
803 basic_block bb;
804 ssa_op_iter iter;
805 live_track *live;
806 basic_block entry;
808 /* If inter-variable coalescing is enabled, we may attempt to
809 coalesce variables from different base variables, including
810 different parameters, so we have to make sure default defs live
811 at the entry block conflict with each other. */
812 if (flag_tree_coalesce_vars)
813 entry = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
814 else
815 entry = NULL;
817 map = live_var_map (liveinfo);
818 graph = ssa_conflicts_new (num_var_partitions (map));
820 live = new_live_track (map);
822 FOR_EACH_BB_FN (bb, cfun)
824 /* Start with live on exit temporaries. */
825 live_track_init (live, live_on_exit (liveinfo, bb));
827 for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);
828 gsi_prev (&gsi))
830 tree var;
831 gimple *stmt = gsi_stmt (gsi);
833 /* A copy between 2 partitions does not introduce an interference
834 by itself. If they did, you would never be able to coalesce
835 two things which are copied. If the two variables really do
836 conflict, they will conflict elsewhere in the program.
838 This is handled by simply removing the SRC of the copy from the
839 live list, and processing the stmt normally. */
840 if (is_gimple_assign (stmt))
842 tree lhs = gimple_assign_lhs (stmt);
843 tree rhs1 = gimple_assign_rhs1 (stmt);
844 if (gimple_assign_copy_p (stmt)
845 && TREE_CODE (lhs) == SSA_NAME
846 && TREE_CODE (rhs1) == SSA_NAME)
847 live_track_clear_var (live, rhs1);
849 else if (is_gimple_debug (stmt))
850 continue;
852 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
853 live_track_process_def (live, var, graph);
855 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
856 live_track_process_use (live, var);
859 /* If result of a PHI is unused, looping over the statements will not
860 record any conflicts since the def was never live. Since the PHI node
861 is going to be translated out of SSA form, it will insert a copy.
862 There must be a conflict recorded between the result of the PHI and
863 any variables that are live. Otherwise the out-of-ssa translation
864 may create incorrect code. */
865 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
866 gsi_next (&gsi))
868 gphi *phi = gsi.phi ();
869 tree result = PHI_RESULT (phi);
870 if (live_track_live_p (live, result))
871 live_track_process_def (live, result, graph);
874 /* Pretend there are defs for params' default defs at the start
875 of the (post-)entry block. This will prevent PARM_DECLs from
876 coalescing into the same partition. Although RESULT_DECLs'
877 default defs don't have a useful initial value, we have to
878 prevent them from coalescing with PARM_DECLs' default defs
879 too, otherwise assign_parms would attempt to assign different
880 RTL to the same partition. */
881 if (bb == entry)
883 unsigned i;
884 for (i = 1; i < num_ssa_names; i++)
886 tree var = ssa_name (i);
888 if (!var
889 || !SSA_NAME_IS_DEFAULT_DEF (var)
890 || !SSA_NAME_VAR (var)
891 || VAR_P (SSA_NAME_VAR (var)))
892 continue;
894 live_track_process_def (live, var, graph);
895 /* Process a use too, so that it remains live and
896 conflicts with other parms' default defs, even unused
897 ones. */
898 live_track_process_use (live, var);
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");
939 /* Call CALLBACK for all PARM_DECLs and RESULT_DECLs for which
940 assign_parms may ask for a default partition. */
942 static void
943 for_all_parms (void (*callback)(tree var, void *arg), void *arg)
945 for (tree var = DECL_ARGUMENTS (current_function_decl); var;
946 var = DECL_CHAIN (var))
947 callback (var, arg);
948 if (!VOID_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
949 callback (DECL_RESULT (current_function_decl), arg);
950 if (cfun->static_chain_decl)
951 callback (cfun->static_chain_decl, arg);
954 /* Create a default def for VAR. */
956 static void
957 create_default_def (tree var, void *arg ATTRIBUTE_UNUSED)
959 if (!is_gimple_reg (var))
960 return;
962 tree ssa = get_or_create_ssa_default_def (cfun, var);
963 gcc_assert (ssa);
966 /* Register VAR's default def in MAP. */
968 static void
969 register_default_def (tree var, void *map_)
971 var_map map = (var_map)map_;
973 if (!is_gimple_reg (var))
974 return;
976 tree ssa = ssa_default_def (cfun, var);
977 gcc_assert (ssa);
979 register_ssa_partition (map, ssa);
982 /* If VAR is an SSA_NAME associated with a PARM_DECL or a RESULT_DECL,
983 and the DECL's default def is unused (i.e., it was introduced by
984 create_default_def), mark VAR and the default def for
985 coalescing. */
987 static void
988 coalesce_with_default (tree var, coalesce_list *cl, bitmap used_in_copy)
990 if (SSA_NAME_IS_DEFAULT_DEF (var)
991 || !SSA_NAME_VAR (var)
992 || VAR_P (SSA_NAME_VAR (var)))
993 return;
995 tree ssa = ssa_default_def (cfun, SSA_NAME_VAR (var));
996 if (!has_zero_uses (ssa))
997 return;
999 add_cost_one_coalesce (cl, SSA_NAME_VERSION (ssa), SSA_NAME_VERSION (var));
1000 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1001 /* Default defs will have their used_in_copy bits set at the end of
1002 create_outofssa_var_map. */
1005 /* This function creates a var_map for the current function as well as creating
1006 a coalesce list for use later in the out of ssa process. */
1008 static var_map
1009 create_outofssa_var_map (coalesce_list *cl, bitmap used_in_copy)
1011 gimple_stmt_iterator gsi;
1012 basic_block bb;
1013 tree var;
1014 gimple *stmt;
1015 tree first;
1016 var_map map;
1017 ssa_op_iter iter;
1018 int v1, v2, cost;
1019 unsigned i;
1021 for_all_parms (create_default_def, NULL);
1023 map = init_var_map (num_ssa_names);
1025 for_all_parms (register_default_def, map);
1027 FOR_EACH_BB_FN (bb, cfun)
1029 tree arg;
1031 for (gphi_iterator gpi = gsi_start_phis (bb);
1032 !gsi_end_p (gpi);
1033 gsi_next (&gpi))
1035 gphi *phi = gpi.phi ();
1036 size_t i;
1037 int ver;
1038 tree res;
1039 bool saw_copy = false;
1041 res = gimple_phi_result (phi);
1042 ver = SSA_NAME_VERSION (res);
1043 register_ssa_partition (map, res);
1045 /* Register ssa_names and coalesces between the args and the result
1046 of all PHI. */
1047 for (i = 0; i < gimple_phi_num_args (phi); i++)
1049 edge e = gimple_phi_arg_edge (phi, i);
1050 arg = PHI_ARG_DEF (phi, i);
1051 if (TREE_CODE (arg) != SSA_NAME)
1052 continue;
1054 register_ssa_partition (map, arg);
1055 if (gimple_can_coalesce_p (arg, res)
1056 || (e->flags & EDGE_ABNORMAL))
1058 saw_copy = true;
1059 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (arg));
1060 if ((e->flags & EDGE_ABNORMAL) == 0)
1062 int cost = coalesce_cost_edge (e);
1063 if (cost == 1 && has_single_use (arg))
1064 add_cost_one_coalesce (cl, ver, SSA_NAME_VERSION (arg));
1065 else
1066 add_coalesce (cl, ver, SSA_NAME_VERSION (arg), cost);
1070 if (saw_copy)
1071 bitmap_set_bit (used_in_copy, ver);
1074 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1076 stmt = gsi_stmt (gsi);
1078 if (is_gimple_debug (stmt))
1079 continue;
1081 /* Register USE and DEF operands in each statement. */
1082 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
1083 register_ssa_partition (map, var);
1085 /* Check for copy coalesces. */
1086 switch (gimple_code (stmt))
1088 case GIMPLE_ASSIGN:
1090 tree lhs = gimple_assign_lhs (stmt);
1091 tree rhs1 = gimple_assign_rhs1 (stmt);
1092 if (gimple_assign_ssa_name_copy_p (stmt)
1093 && gimple_can_coalesce_p (lhs, rhs1))
1095 v1 = SSA_NAME_VERSION (lhs);
1096 v2 = SSA_NAME_VERSION (rhs1);
1097 cost = coalesce_cost_bb (bb);
1098 add_coalesce (cl, v1, v2, cost);
1099 bitmap_set_bit (used_in_copy, v1);
1100 bitmap_set_bit (used_in_copy, v2);
1103 break;
1105 case GIMPLE_RETURN:
1107 tree res = DECL_RESULT (current_function_decl);
1108 if (VOID_TYPE_P (TREE_TYPE (res))
1109 || !is_gimple_reg (res))
1110 break;
1111 tree rhs1 = gimple_return_retval (as_a <greturn *> (stmt));
1112 if (!rhs1)
1113 break;
1114 tree lhs = ssa_default_def (cfun, res);
1115 gcc_assert (lhs);
1116 if (TREE_CODE (rhs1) == SSA_NAME
1117 && gimple_can_coalesce_p (lhs, rhs1))
1119 v1 = SSA_NAME_VERSION (lhs);
1120 v2 = SSA_NAME_VERSION (rhs1);
1121 cost = coalesce_cost_bb (bb);
1122 add_coalesce (cl, v1, v2, cost);
1123 bitmap_set_bit (used_in_copy, v1);
1124 bitmap_set_bit (used_in_copy, v2);
1126 break;
1129 case GIMPLE_ASM:
1131 gasm *asm_stmt = as_a <gasm *> (stmt);
1132 unsigned long noutputs, i;
1133 unsigned long ninputs;
1134 tree *outputs, link;
1135 noutputs = gimple_asm_noutputs (asm_stmt);
1136 ninputs = gimple_asm_ninputs (asm_stmt);
1137 outputs = (tree *) alloca (noutputs * sizeof (tree));
1138 for (i = 0; i < noutputs; ++i)
1140 link = gimple_asm_output_op (asm_stmt, i);
1141 outputs[i] = TREE_VALUE (link);
1144 for (i = 0; i < ninputs; ++i)
1146 const char *constraint;
1147 tree input;
1148 char *end;
1149 unsigned long match;
1151 link = gimple_asm_input_op (asm_stmt, i);
1152 constraint
1153 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1154 input = TREE_VALUE (link);
1156 if (TREE_CODE (input) != SSA_NAME)
1157 continue;
1159 match = strtoul (constraint, &end, 10);
1160 if (match >= noutputs || end == constraint)
1161 continue;
1163 if (TREE_CODE (outputs[match]) != SSA_NAME)
1164 continue;
1166 v1 = SSA_NAME_VERSION (outputs[match]);
1167 v2 = SSA_NAME_VERSION (input);
1169 if (gimple_can_coalesce_p (outputs[match], input))
1171 cost = coalesce_cost (REG_BR_PROB_BASE,
1172 optimize_bb_for_size_p (bb));
1173 add_coalesce (cl, v1, v2, cost);
1174 bitmap_set_bit (used_in_copy, v1);
1175 bitmap_set_bit (used_in_copy, v2);
1178 break;
1181 default:
1182 break;
1187 /* Now process result decls and live on entry variables for entry into
1188 the coalesce list. */
1189 first = NULL_TREE;
1190 for (i = 1; i < num_ssa_names; i++)
1192 var = ssa_name (i);
1193 if (var != NULL_TREE && !virtual_operand_p (var))
1195 coalesce_with_default (var, cl, used_in_copy);
1197 /* Add coalesces between all the result decls. */
1198 if (SSA_NAME_VAR (var)
1199 && TREE_CODE (SSA_NAME_VAR (var)) == RESULT_DECL)
1201 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1202 if (first == NULL_TREE)
1203 first = var;
1204 else
1206 gcc_assert (gimple_can_coalesce_p (var, first));
1207 v1 = SSA_NAME_VERSION (first);
1208 v2 = SSA_NAME_VERSION (var);
1209 cost = coalesce_cost_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
1210 add_coalesce (cl, v1, v2, cost);
1213 /* Mark any default_def variables as being in the coalesce list
1214 since they will have to be coalesced with the base variable. If
1215 not marked as present, they won't be in the coalesce view. */
1216 if (SSA_NAME_IS_DEFAULT_DEF (var)
1217 && (!has_zero_uses (var)
1218 || (SSA_NAME_VAR (var)
1219 && !VAR_P (SSA_NAME_VAR (var)))))
1220 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1224 return map;
1228 /* Attempt to coalesce ssa versions X and Y together using the partition
1229 mapping in MAP and checking conflicts in GRAPH. Output any debug info to
1230 DEBUG, if it is nun-NULL. */
1232 static inline bool
1233 attempt_coalesce (var_map map, ssa_conflicts *graph, int x, int y,
1234 FILE *debug)
1236 int z;
1237 tree var1, var2;
1238 int p1, p2;
1240 p1 = var_to_partition (map, ssa_name (x));
1241 p2 = var_to_partition (map, ssa_name (y));
1243 if (debug)
1245 fprintf (debug, "(%d)", x);
1246 print_generic_expr (debug, partition_to_var (map, p1), TDF_SLIM);
1247 fprintf (debug, " & (%d)", y);
1248 print_generic_expr (debug, partition_to_var (map, p2), TDF_SLIM);
1251 if (p1 == p2)
1253 if (debug)
1254 fprintf (debug, ": Already Coalesced.\n");
1255 return true;
1258 if (debug)
1259 fprintf (debug, " [map: %d, %d] ", p1, p2);
1262 if (!ssa_conflicts_test_p (graph, p1, p2))
1264 var1 = partition_to_var (map, p1);
1265 var2 = partition_to_var (map, p2);
1267 z = var_union (map, var1, var2);
1268 if (z == NO_PARTITION)
1270 if (debug)
1271 fprintf (debug, ": Unable to perform partition union.\n");
1272 return false;
1275 /* z is the new combined partition. Remove the other partition from
1276 the list, and merge the conflicts. */
1277 if (z == p1)
1278 ssa_conflicts_merge (graph, p1, p2);
1279 else
1280 ssa_conflicts_merge (graph, p2, p1);
1282 if (debug)
1283 fprintf (debug, ": Success -> %d\n", z);
1285 return true;
1288 if (debug)
1289 fprintf (debug, ": Fail due to conflict\n");
1291 return false;
1295 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1296 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1298 static void
1299 coalesce_partitions (var_map map, ssa_conflicts *graph, coalesce_list *cl,
1300 FILE *debug)
1302 int x = 0, y = 0;
1303 tree var1, var2;
1304 int cost;
1305 basic_block bb;
1306 edge e;
1307 edge_iterator ei;
1309 /* First, coalesce all the copies across abnormal edges. These are not placed
1310 in the coalesce list because they do not need to be sorted, and simply
1311 consume extra memory/compilation time in large programs. */
1313 FOR_EACH_BB_FN (bb, cfun)
1315 FOR_EACH_EDGE (e, ei, bb->preds)
1316 if (e->flags & EDGE_ABNORMAL)
1318 gphi_iterator gsi;
1319 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1320 gsi_next (&gsi))
1322 gphi *phi = gsi.phi ();
1323 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1324 if (SSA_NAME_IS_DEFAULT_DEF (arg)
1325 && (!SSA_NAME_VAR (arg)
1326 || TREE_CODE (SSA_NAME_VAR (arg)) != PARM_DECL))
1327 continue;
1329 tree res = PHI_RESULT (phi);
1330 int v1 = SSA_NAME_VERSION (res);
1331 int v2 = SSA_NAME_VERSION (arg);
1333 if (debug)
1334 fprintf (debug, "Abnormal coalesce: ");
1336 if (!attempt_coalesce (map, graph, v1, v2, debug))
1337 fail_abnormal_edge_coalesce (v1, v2);
1342 /* Now process the items in the coalesce list. */
1344 while ((cost = pop_best_coalesce (cl, &x, &y)) != NO_BEST_COALESCE)
1346 var1 = ssa_name (x);
1347 var2 = ssa_name (y);
1349 /* Assert the coalesces have the same base variable. */
1350 gcc_assert (gimple_can_coalesce_p (var1, var2));
1352 if (debug)
1353 fprintf (debug, "Coalesce list: ");
1354 attempt_coalesce (map, graph, x, y, debug);
1359 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1361 struct ssa_name_var_hash : nofree_ptr_hash <tree_node>
1363 static inline hashval_t hash (const tree_node *);
1364 static inline int equal (const tree_node *, const tree_node *);
1367 inline hashval_t
1368 ssa_name_var_hash::hash (const_tree n)
1370 return DECL_UID (SSA_NAME_VAR (n));
1373 inline int
1374 ssa_name_var_hash::equal (const tree_node *n1, const tree_node *n2)
1376 return SSA_NAME_VAR (n1) == SSA_NAME_VAR (n2);
1380 /* Output partition map MAP with coalescing plan PART to file F. */
1382 void
1383 dump_part_var_map (FILE *f, partition part, var_map map)
1385 int t;
1386 unsigned x, y;
1387 int p;
1389 fprintf (f, "\nCoalescible Partition map \n\n");
1391 for (x = 0; x < map->num_partitions; x++)
1393 if (map->view_to_partition != NULL)
1394 p = map->view_to_partition[x];
1395 else
1396 p = x;
1398 if (ssa_name (p) == NULL_TREE
1399 || virtual_operand_p (ssa_name (p)))
1400 continue;
1402 t = 0;
1403 for (y = 1; y < num_ssa_names; y++)
1405 tree var = version_to_var (map, y);
1406 if (!var)
1407 continue;
1408 int q = var_to_partition (map, var);
1409 p = partition_find (part, q);
1410 gcc_assert (map->partition_to_base_index[q]
1411 == map->partition_to_base_index[p]);
1413 if (p == (int)x)
1415 if (t++ == 0)
1417 fprintf (f, "Partition %d, base %d (", x,
1418 map->partition_to_base_index[q]);
1419 print_generic_expr (f, partition_to_var (map, q), TDF_SLIM);
1420 fprintf (f, " - ");
1422 fprintf (f, "%d ", y);
1425 if (t != 0)
1426 fprintf (f, ")\n");
1428 fprintf (f, "\n");
1431 /* Given SSA_NAMEs NAME1 and NAME2, return true if they are candidates for
1432 coalescing together, false otherwise.
1434 This must stay consistent with var_map_base_init in tree-ssa-live.c. */
1436 bool
1437 gimple_can_coalesce_p (tree name1, tree name2)
1439 /* First check the SSA_NAME's associated DECL. Without
1440 optimization, we only want to coalesce if they have the same DECL
1441 or both have no associated DECL. */
1442 tree var1 = SSA_NAME_VAR (name1);
1443 tree var2 = SSA_NAME_VAR (name2);
1444 var1 = (var1 && (!VAR_P (var1) || !DECL_IGNORED_P (var1))) ? var1 : NULL_TREE;
1445 var2 = (var2 && (!VAR_P (var2) || !DECL_IGNORED_P (var2))) ? var2 : NULL_TREE;
1446 if (var1 != var2 && !flag_tree_coalesce_vars)
1447 return false;
1449 /* Now check the types. If the types are the same, then we should
1450 try to coalesce V1 and V2. */
1451 tree t1 = TREE_TYPE (name1);
1452 tree t2 = TREE_TYPE (name2);
1453 if (t1 == t2)
1455 check_modes:
1456 /* If the base variables are the same, we're good: none of the
1457 other tests below could possibly fail. */
1458 var1 = SSA_NAME_VAR (name1);
1459 var2 = SSA_NAME_VAR (name2);
1460 if (var1 == var2)
1461 return true;
1463 /* We don't want to coalesce two SSA names if one of the base
1464 variables is supposed to be a register while the other is
1465 supposed to be on the stack. Anonymous SSA names most often
1466 take registers, but when not optimizing, user variables
1467 should go on the stack, so coalescing them with the anonymous
1468 variable as the partition leader would end up assigning the
1469 user variable to a register. Don't do that! */
1470 bool reg1 = use_register_for_decl (name1);
1471 bool reg2 = use_register_for_decl (name2);
1472 if (reg1 != reg2)
1473 return false;
1475 /* Check that the promoted modes and unsignedness are the same.
1476 We don't want to coalesce if the promoted modes would be
1477 different, or if they would sign-extend differently. Only
1478 PARM_DECLs and RESULT_DECLs have different promotion rules,
1479 so skip the test if both are variables, or both are anonymous
1480 SSA_NAMEs. */
1481 int unsigned1, unsigned2;
1482 return ((!var1 || VAR_P (var1)) && (!var2 || VAR_P (var2)))
1483 || ((promote_ssa_mode (name1, &unsigned1)
1484 == promote_ssa_mode (name2, &unsigned2))
1485 && unsigned1 == unsigned2);
1488 /* If alignment requirements are different, we can't coalesce. */
1489 if (MINIMUM_ALIGNMENT (t1,
1490 var1 ? DECL_MODE (var1) : TYPE_MODE (t1),
1491 var1 ? LOCAL_DECL_ALIGNMENT (var1) : TYPE_ALIGN (t1))
1492 != MINIMUM_ALIGNMENT (t2,
1493 var2 ? DECL_MODE (var2) : TYPE_MODE (t2),
1494 var2 ? LOCAL_DECL_ALIGNMENT (var2) : TYPE_ALIGN (t2)))
1495 return false;
1497 /* If the types are not the same, check for a canonical type match. This
1498 (for example) allows coalescing when the types are fundamentally the
1499 same, but just have different names.
1501 Note pointer types with different address spaces may have the same
1502 canonical type. Those are rejected for coalescing by the
1503 types_compatible_p check. */
1504 if (TYPE_CANONICAL (t1)
1505 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)
1506 && types_compatible_p (t1, t2))
1507 goto check_modes;
1509 return false;
1512 /* Fill in MAP's partition_to_base_index, with one index for each
1513 partition of SSA names USED_IN_COPIES and related by CL coalesce
1514 possibilities. This must match gimple_can_coalesce_p in the
1515 optimized case. */
1517 static void
1518 compute_optimized_partition_bases (var_map map, bitmap used_in_copies,
1519 coalesce_list *cl)
1521 int parts = num_var_partitions (map);
1522 partition tentative = partition_new (parts);
1524 /* Partition the SSA versions so that, for each coalescible
1525 pair, both of its members are in the same partition in
1526 TENTATIVE. */
1527 gcc_assert (!cl->sorted);
1528 coalesce_pair *node;
1529 coalesce_iterator_type ppi;
1530 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
1532 tree v1 = ssa_name (node->first_element);
1533 int p1 = partition_find (tentative, var_to_partition (map, v1));
1534 tree v2 = ssa_name (node->second_element);
1535 int p2 = partition_find (tentative, var_to_partition (map, v2));
1537 if (p1 == p2)
1538 continue;
1540 partition_union (tentative, p1, p2);
1543 /* We have to deal with cost one pairs too. */
1544 for (cost_one_pair *co = cl->cost_one_list; co; co = co->next)
1546 tree v1 = ssa_name (co->first_element);
1547 int p1 = partition_find (tentative, var_to_partition (map, v1));
1548 tree v2 = ssa_name (co->second_element);
1549 int p2 = partition_find (tentative, var_to_partition (map, v2));
1551 if (p1 == p2)
1552 continue;
1554 partition_union (tentative, p1, p2);
1557 /* And also with abnormal edges. */
1558 basic_block bb;
1559 edge e;
1560 edge_iterator ei;
1561 FOR_EACH_BB_FN (bb, cfun)
1563 FOR_EACH_EDGE (e, ei, bb->preds)
1564 if (e->flags & EDGE_ABNORMAL)
1566 gphi_iterator gsi;
1567 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1568 gsi_next (&gsi))
1570 gphi *phi = gsi.phi ();
1571 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1572 if (SSA_NAME_IS_DEFAULT_DEF (arg)
1573 && (!SSA_NAME_VAR (arg)
1574 || TREE_CODE (SSA_NAME_VAR (arg)) != PARM_DECL))
1575 continue;
1577 tree res = PHI_RESULT (phi);
1579 int p1 = partition_find (tentative, var_to_partition (map, res));
1580 int p2 = partition_find (tentative, var_to_partition (map, arg));
1582 if (p1 == p2)
1583 continue;
1585 partition_union (tentative, p1, p2);
1590 map->partition_to_base_index = XCNEWVEC (int, parts);
1591 auto_vec<unsigned int> index_map (parts);
1592 if (parts)
1593 index_map.quick_grow (parts);
1595 const unsigned no_part = -1;
1596 unsigned count = parts;
1597 while (count)
1598 index_map[--count] = no_part;
1600 /* Initialize MAP's mapping from partition to base index, using
1601 as base indices an enumeration of the TENTATIVE partitions in
1602 which each SSA version ended up, so that we compute conflicts
1603 between all SSA versions that ended up in the same potential
1604 coalesce partition. */
1605 bitmap_iterator bi;
1606 unsigned i;
1607 EXECUTE_IF_SET_IN_BITMAP (used_in_copies, 0, i, bi)
1609 int pidx = var_to_partition (map, ssa_name (i));
1610 int base = partition_find (tentative, pidx);
1611 if (index_map[base] != no_part)
1612 continue;
1613 index_map[base] = count++;
1616 map->num_basevars = count;
1618 EXECUTE_IF_SET_IN_BITMAP (used_in_copies, 0, i, bi)
1620 int pidx = var_to_partition (map, ssa_name (i));
1621 int base = partition_find (tentative, pidx);
1622 gcc_assert (index_map[base] < count);
1623 map->partition_to_base_index[pidx] = index_map[base];
1626 if (dump_file && (dump_flags & TDF_DETAILS))
1627 dump_part_var_map (dump_file, tentative, map);
1629 partition_delete (tentative);
1632 /* Hashtable helpers. */
1634 struct tree_int_map_hasher : nofree_ptr_hash <tree_int_map>
1636 static inline hashval_t hash (const tree_int_map *);
1637 static inline bool equal (const tree_int_map *, const tree_int_map *);
1640 inline hashval_t
1641 tree_int_map_hasher::hash (const tree_int_map *v)
1643 return tree_map_base_hash (v);
1646 inline bool
1647 tree_int_map_hasher::equal (const tree_int_map *v, const tree_int_map *c)
1649 return tree_int_map_eq (v, c);
1652 /* This routine will initialize the basevar fields of MAP with base
1653 names. Partitions will share the same base if they have the same
1654 SSA_NAME_VAR, or, being anonymous variables, the same type. This
1655 must match gimple_can_coalesce_p in the non-optimized case. */
1657 static void
1658 compute_samebase_partition_bases (var_map map)
1660 int x, num_part;
1661 tree var;
1662 struct tree_int_map *m, *mapstorage;
1664 num_part = num_var_partitions (map);
1665 hash_table<tree_int_map_hasher> tree_to_index (num_part);
1666 /* We can have at most num_part entries in the hash tables, so it's
1667 enough to allocate so many map elements once, saving some malloc
1668 calls. */
1669 mapstorage = m = XNEWVEC (struct tree_int_map, num_part);
1671 /* If a base table already exists, clear it, otherwise create it. */
1672 free (map->partition_to_base_index);
1673 map->partition_to_base_index = (int *) xmalloc (sizeof (int) * num_part);
1675 /* Build the base variable list, and point partitions at their bases. */
1676 for (x = 0; x < num_part; x++)
1678 struct tree_int_map **slot;
1679 unsigned baseindex;
1680 var = partition_to_var (map, x);
1681 if (SSA_NAME_VAR (var)
1682 && (!VAR_P (SSA_NAME_VAR (var))
1683 || !DECL_IGNORED_P (SSA_NAME_VAR (var))))
1684 m->base.from = SSA_NAME_VAR (var);
1685 else
1686 /* This restricts what anonymous SSA names we can coalesce
1687 as it restricts the sets we compute conflicts for.
1688 Using TREE_TYPE to generate sets is the easies as
1689 type equivalency also holds for SSA names with the same
1690 underlying decl.
1692 Check gimple_can_coalesce_p when changing this code. */
1693 m->base.from = (TYPE_CANONICAL (TREE_TYPE (var))
1694 ? TYPE_CANONICAL (TREE_TYPE (var))
1695 : TREE_TYPE (var));
1696 /* If base variable hasn't been seen, set it up. */
1697 slot = tree_to_index.find_slot (m, INSERT);
1698 if (!*slot)
1700 baseindex = m - mapstorage;
1701 m->to = baseindex;
1702 *slot = m;
1703 m++;
1705 else
1706 baseindex = (*slot)->to;
1707 map->partition_to_base_index[x] = baseindex;
1710 map->num_basevars = m - mapstorage;
1712 free (mapstorage);
1715 /* Reduce the number of copies by coalescing variables in the function. Return
1716 a partition map with the resulting coalesces. */
1718 extern var_map
1719 coalesce_ssa_name (void)
1721 tree_live_info_p liveinfo;
1722 ssa_conflicts *graph;
1723 coalesce_list *cl;
1724 bitmap used_in_copies = BITMAP_ALLOC (NULL);
1725 var_map map;
1726 unsigned int i;
1728 cl = create_coalesce_list ();
1729 map = create_outofssa_var_map (cl, used_in_copies);
1731 /* If this optimization is disabled, we need to coalesce all the
1732 names originating from the same SSA_NAME_VAR so debug info
1733 remains undisturbed. */
1734 if (!flag_tree_coalesce_vars)
1736 hash_table<ssa_name_var_hash> ssa_name_hash (10);
1738 for (i = 1; i < num_ssa_names; i++)
1740 tree a = ssa_name (i);
1742 if (a
1743 && SSA_NAME_VAR (a)
1744 && !DECL_IGNORED_P (SSA_NAME_VAR (a))
1745 && (!has_zero_uses (a) || !SSA_NAME_IS_DEFAULT_DEF (a)
1746 || !VAR_P (SSA_NAME_VAR (a))))
1748 tree *slot = ssa_name_hash.find_slot (a, INSERT);
1750 if (!*slot)
1751 *slot = a;
1752 else
1754 /* If the variable is a PARM_DECL or a RESULT_DECL, we
1755 _require_ that all the names originating from it be
1756 coalesced, because there must be a single partition
1757 containing all the names so that it can be assigned
1758 the canonical RTL location of the DECL safely.
1759 If in_lto_p, a function could have been compiled
1760 originally with optimizations and only the link
1761 performed at -O0, so we can't actually require it. */
1762 const int cost
1763 = (TREE_CODE (SSA_NAME_VAR (a)) == VAR_DECL || in_lto_p)
1764 ? MUST_COALESCE_COST - 1 : MUST_COALESCE_COST;
1765 add_coalesce (cl, SSA_NAME_VERSION (a),
1766 SSA_NAME_VERSION (*slot), cost);
1767 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (a));
1768 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (*slot));
1773 if (dump_file && (dump_flags & TDF_DETAILS))
1774 dump_var_map (dump_file, map);
1776 partition_view_bitmap (map, used_in_copies);
1778 if (flag_tree_coalesce_vars)
1779 compute_optimized_partition_bases (map, used_in_copies, cl);
1780 else
1781 compute_samebase_partition_bases (map);
1783 BITMAP_FREE (used_in_copies);
1785 if (num_var_partitions (map) < 1)
1787 delete_coalesce_list (cl);
1788 return map;
1791 if (dump_file && (dump_flags & TDF_DETAILS))
1792 dump_var_map (dump_file, map);
1794 liveinfo = calculate_live_ranges (map, false);
1796 if (dump_file && (dump_flags & TDF_DETAILS))
1797 dump_live_info (dump_file, liveinfo, LIVEDUMP_ENTRY);
1799 /* Build a conflict graph. */
1800 graph = build_ssa_conflict_graph (liveinfo);
1801 delete_tree_live_info (liveinfo);
1802 if (dump_file && (dump_flags & TDF_DETAILS))
1803 ssa_conflicts_dump (dump_file, graph);
1805 sort_coalesce_list (cl);
1807 if (dump_file && (dump_flags & TDF_DETAILS))
1809 fprintf (dump_file, "\nAfter sorting:\n");
1810 dump_coalesce_list (dump_file, cl);
1813 /* First, coalesce all live on entry variables to their base variable.
1814 This will ensure the first use is coming from the correct location. */
1816 if (dump_file && (dump_flags & TDF_DETAILS))
1817 dump_var_map (dump_file, map);
1819 /* Now coalesce everything in the list. */
1820 coalesce_partitions (map, graph, cl,
1821 ((dump_flags & TDF_DETAILS) ? dump_file : NULL));
1823 delete_coalesce_list (cl);
1824 ssa_conflicts_delete (graph);
1826 return map;
1829 /* We need to pass two arguments to set_parm_default_def_partition,
1830 but for_all_parms only supports one. Use a pair. */
1832 typedef std::pair<var_map, bitmap> parm_default_def_partition_arg;
1834 /* Set in ARG's PARTS bitmap the bit corresponding to the partition in
1835 ARG's MAP containing VAR's default def. */
1837 static void
1838 set_parm_default_def_partition (tree var, void *arg_)
1840 parm_default_def_partition_arg *arg = (parm_default_def_partition_arg *)arg_;
1841 var_map map = arg->first;
1842 bitmap parts = arg->second;
1844 if (!is_gimple_reg (var))
1845 return;
1847 tree ssa = ssa_default_def (cfun, var);
1848 gcc_assert (ssa);
1850 int version = var_to_partition (map, ssa);
1851 gcc_assert (version != NO_PARTITION);
1853 bool changed = bitmap_set_bit (parts, version);
1854 gcc_assert (changed);
1857 /* Allocate and return a bitmap that has a bit set for each partition
1858 that contains a default def for a parameter. */
1860 extern bitmap
1861 get_parm_default_def_partitions (var_map map)
1863 bitmap parm_default_def_parts = BITMAP_ALLOC (NULL);
1865 parm_default_def_partition_arg
1866 arg = std::make_pair (map, parm_default_def_parts);
1868 for_all_parms (set_parm_default_def_partition, &arg);
1870 return parm_default_def_parts;