* omp-low.c (MASK_GANG, MASK_WORKER, MASK_VECTOR): Delete.
[official-gcc.git] / gcc / tree-ssa-coalesce.c
blobfd716a6efe9cd28acfa69d6c2587247f4e4138b6
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"
42 #include "tree-dfa.h"
43 #include "tm_p.h"
44 #include "stor-layout.h"
46 /* This set of routines implements a coalesce_list. This is an object which
47 is used to track pairs of ssa_names which are desirable to coalesce
48 together to avoid copies. Costs are associated with each pair, and when
49 all desired information has been collected, the object can be used to
50 order the pairs for processing. */
52 /* This structure defines a pair entry. */
54 struct coalesce_pair
56 int first_element;
57 int second_element;
58 int cost;
61 /* Coalesce pair hashtable helpers. */
63 struct coalesce_pair_hasher : nofree_ptr_hash <coalesce_pair>
65 static inline hashval_t hash (const coalesce_pair *);
66 static inline bool equal (const coalesce_pair *, const coalesce_pair *);
69 /* Hash function for coalesce list. Calculate hash for PAIR. */
71 inline hashval_t
72 coalesce_pair_hasher::hash (const coalesce_pair *pair)
74 hashval_t a = (hashval_t)(pair->first_element);
75 hashval_t b = (hashval_t)(pair->second_element);
77 return b * (b - 1) / 2 + a;
80 /* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
81 returning TRUE if the two pairs are equivalent. */
83 inline bool
84 coalesce_pair_hasher::equal (const coalesce_pair *p1, const coalesce_pair *p2)
86 return (p1->first_element == p2->first_element
87 && p1->second_element == p2->second_element);
90 typedef hash_table<coalesce_pair_hasher> coalesce_table_type;
91 typedef coalesce_table_type::iterator coalesce_iterator_type;
94 struct cost_one_pair
96 int first_element;
97 int second_element;
98 cost_one_pair *next;
101 /* This structure maintains the list of coalesce pairs. */
103 struct coalesce_list
105 coalesce_table_type *list; /* Hash table. */
106 coalesce_pair **sorted; /* List when sorted. */
107 int num_sorted; /* Number in the sorted list. */
108 cost_one_pair *cost_one_list;/* Single use coalesces with cost 1. */
111 #define NO_BEST_COALESCE -1
112 #define MUST_COALESCE_COST INT_MAX
115 /* Return cost of execution of copy instruction with FREQUENCY. */
117 static inline int
118 coalesce_cost (int frequency, bool optimize_for_size)
120 /* Base costs on BB frequencies bounded by 1. */
121 int cost = frequency;
123 if (!cost)
124 cost = 1;
126 if (optimize_for_size)
127 cost = 1;
129 return cost;
133 /* Return the cost of executing a copy instruction in basic block BB. */
135 static inline int
136 coalesce_cost_bb (basic_block bb)
138 return coalesce_cost (bb->frequency, optimize_bb_for_size_p (bb));
142 /* Return the cost of executing a copy instruction on edge E. */
144 static inline int
145 coalesce_cost_edge (edge e)
147 int mult = 1;
149 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
150 if (EDGE_CRITICAL_P (e))
151 mult = 2;
152 if (e->flags & EDGE_ABNORMAL)
153 return MUST_COALESCE_COST;
154 if (e->flags & EDGE_EH)
156 edge e2;
157 edge_iterator ei;
158 FOR_EACH_EDGE (e2, ei, e->dest->preds)
159 if (e2 != e)
161 /* Putting code on EH edge that leads to BB
162 with multiple predecestors imply splitting of
163 edge too. */
164 if (mult < 2)
165 mult = 2;
166 /* If there are multiple EH predecestors, we
167 also copy EH regions and produce separate
168 landing pad. This is expensive. */
169 if (e2->flags & EDGE_EH)
171 mult = 5;
172 break;
177 return coalesce_cost (EDGE_FREQUENCY (e),
178 optimize_edge_for_size_p (e)) * mult;
182 /* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
183 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
184 NO_BEST_COALESCE is returned if there aren't any. */
186 static inline int
187 pop_cost_one_pair (coalesce_list *cl, int *p1, int *p2)
189 cost_one_pair *ptr;
191 ptr = cl->cost_one_list;
192 if (!ptr)
193 return NO_BEST_COALESCE;
195 *p1 = ptr->first_element;
196 *p2 = ptr->second_element;
197 cl->cost_one_list = ptr->next;
199 free (ptr);
201 return 1;
204 /* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
205 2 elements via P1 and P2. Their calculated cost is returned by the function.
206 NO_BEST_COALESCE is returned if the coalesce list is empty. */
208 static inline int
209 pop_best_coalesce (coalesce_list *cl, int *p1, int *p2)
211 coalesce_pair *node;
212 int ret;
214 if (cl->sorted == NULL)
215 return pop_cost_one_pair (cl, p1, p2);
217 if (cl->num_sorted == 0)
218 return pop_cost_one_pair (cl, p1, p2);
220 node = cl->sorted[--(cl->num_sorted)];
221 *p1 = node->first_element;
222 *p2 = node->second_element;
223 ret = node->cost;
224 free (node);
226 return ret;
230 /* Create a new empty coalesce list object and return it. */
232 static inline coalesce_list *
233 create_coalesce_list (void)
235 coalesce_list *list;
236 unsigned size = num_ssa_names * 3;
238 if (size < 40)
239 size = 40;
241 list = (coalesce_list *) xmalloc (sizeof (struct coalesce_list));
242 list->list = new coalesce_table_type (size);
243 list->sorted = NULL;
244 list->num_sorted = 0;
245 list->cost_one_list = NULL;
246 return list;
250 /* Delete coalesce list CL. */
252 static inline void
253 delete_coalesce_list (coalesce_list *cl)
255 gcc_assert (cl->cost_one_list == NULL);
256 delete cl->list;
257 cl->list = NULL;
258 free (cl->sorted);
259 gcc_assert (cl->num_sorted == 0);
260 free (cl);
264 /* Find a matching coalesce pair object in CL for the pair P1 and P2. If
265 one isn't found, return NULL if CREATE is false, otherwise create a new
266 coalesce pair object and return it. */
268 static coalesce_pair *
269 find_coalesce_pair (coalesce_list *cl, int p1, int p2, bool create)
271 struct coalesce_pair p;
272 coalesce_pair **slot;
273 unsigned int hash;
275 /* Normalize so that p1 is the smaller value. */
276 if (p2 < p1)
278 p.first_element = p2;
279 p.second_element = p1;
281 else
283 p.first_element = p1;
284 p.second_element = p2;
287 hash = coalesce_pair_hasher::hash (&p);
288 slot = cl->list->find_slot_with_hash (&p, hash, create ? INSERT : NO_INSERT);
289 if (!slot)
290 return NULL;
292 if (!*slot)
294 struct coalesce_pair * pair = XNEW (struct coalesce_pair);
295 gcc_assert (cl->sorted == NULL);
296 pair->first_element = p.first_element;
297 pair->second_element = p.second_element;
298 pair->cost = 0;
299 *slot = pair;
302 return (struct coalesce_pair *) *slot;
305 static inline void
306 add_cost_one_coalesce (coalesce_list *cl, int p1, int p2)
308 cost_one_pair *pair;
310 pair = XNEW (cost_one_pair);
311 pair->first_element = p1;
312 pair->second_element = p2;
313 pair->next = cl->cost_one_list;
314 cl->cost_one_list = pair;
318 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
320 static inline void
321 add_coalesce (coalesce_list *cl, int p1, int p2, int value)
323 coalesce_pair *node;
325 gcc_assert (cl->sorted == NULL);
326 if (p1 == p2)
327 return;
329 node = find_coalesce_pair (cl, p1, p2, true);
331 /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way. */
332 if (node->cost < MUST_COALESCE_COST - 1)
334 if (value < MUST_COALESCE_COST - 1)
335 node->cost += value;
336 else
337 node->cost = value;
342 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
344 static int
345 compare_pairs (const void *p1, const void *p2)
347 const coalesce_pair *const *const pp1 = (const coalesce_pair *const *) p1;
348 const coalesce_pair *const *const pp2 = (const coalesce_pair *const *) p2;
349 int result;
351 result = (* pp1)->cost - (* pp2)->cost;
352 /* Since qsort does not guarantee stability we use the elements
353 as a secondary key. This provides us with independence from
354 the host's implementation of the sorting algorithm. */
355 if (result == 0)
357 result = (* pp2)->first_element - (* pp1)->first_element;
358 if (result == 0)
359 result = (* pp2)->second_element - (* pp1)->second_element;
362 return result;
366 /* Return the number of unique coalesce pairs in CL. */
368 static inline int
369 num_coalesce_pairs (coalesce_list *cl)
371 return cl->list->elements ();
375 /* Iterate over CL using ITER, returning values in PAIR. */
377 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
378 FOR_EACH_HASH_TABLE_ELEMENT (*(CL)->list, (PAIR), coalesce_pair_p, (ITER))
381 /* Prepare CL for removal of preferred pairs. When finished they are sorted
382 in order from most important coalesce to least important. */
384 static void
385 sort_coalesce_list (coalesce_list *cl)
387 unsigned x, num;
388 coalesce_pair *p;
389 coalesce_iterator_type ppi;
391 gcc_assert (cl->sorted == NULL);
393 num = num_coalesce_pairs (cl);
394 cl->num_sorted = num;
395 if (num == 0)
396 return;
398 /* Allocate a vector for the pair pointers. */
399 cl->sorted = XNEWVEC (coalesce_pair *, num);
401 /* Populate the vector with pointers to the pairs. */
402 x = 0;
403 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
404 cl->sorted[x++] = p;
405 gcc_assert (x == num);
407 /* Already sorted. */
408 if (num == 1)
409 return;
411 /* If there are only 2, just pick swap them if the order isn't correct. */
412 if (num == 2)
414 if (cl->sorted[0]->cost > cl->sorted[1]->cost)
415 std::swap (cl->sorted[0], cl->sorted[1]);
416 return;
419 /* Only call qsort if there are more than 2 items.
420 ??? Maybe std::sort will do better, provided that compare_pairs
421 can be inlined. */
422 if (num > 2)
423 qsort (cl->sorted, num, sizeof (coalesce_pair *), compare_pairs);
427 /* Send debug info for coalesce list CL to file F. */
429 static void
430 dump_coalesce_list (FILE *f, coalesce_list *cl)
432 coalesce_pair *node;
433 coalesce_iterator_type ppi;
435 int x;
436 tree var;
438 if (cl->sorted == NULL)
440 fprintf (f, "Coalesce List:\n");
441 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
443 tree var1 = ssa_name (node->first_element);
444 tree var2 = ssa_name (node->second_element);
445 print_generic_expr (f, var1, TDF_SLIM);
446 fprintf (f, " <-> ");
447 print_generic_expr (f, var2, TDF_SLIM);
448 fprintf (f, " (%1d), ", node->cost);
449 fprintf (f, "\n");
452 else
454 fprintf (f, "Sorted Coalesce list:\n");
455 for (x = cl->num_sorted - 1 ; x >=0; x--)
457 node = cl->sorted[x];
458 fprintf (f, "(%d) ", node->cost);
459 var = ssa_name (node->first_element);
460 print_generic_expr (f, var, TDF_SLIM);
461 fprintf (f, " <-> ");
462 var = ssa_name (node->second_element);
463 print_generic_expr (f, var, TDF_SLIM);
464 fprintf (f, "\n");
470 /* This represents a conflict graph. Implemented as an array of bitmaps.
471 A full matrix is used for conflicts rather than just upper triangular form.
472 this make sit much simpler and faster to perform conflict merges. */
474 struct ssa_conflicts
476 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
477 vec<bitmap> conflicts;
480 /* Return an empty new conflict graph for SIZE elements. */
482 static inline ssa_conflicts *
483 ssa_conflicts_new (unsigned size)
485 ssa_conflicts *ptr;
487 ptr = XNEW (ssa_conflicts);
488 bitmap_obstack_initialize (&ptr->obstack);
489 ptr->conflicts.create (size);
490 ptr->conflicts.safe_grow_cleared (size);
491 return ptr;
495 /* Free storage for conflict graph PTR. */
497 static inline void
498 ssa_conflicts_delete (ssa_conflicts *ptr)
500 bitmap_obstack_release (&ptr->obstack);
501 ptr->conflicts.release ();
502 free (ptr);
506 /* Test if elements X and Y conflict in graph PTR. */
508 static inline bool
509 ssa_conflicts_test_p (ssa_conflicts *ptr, unsigned x, unsigned y)
511 bitmap bx = ptr->conflicts[x];
512 bitmap by = ptr->conflicts[y];
514 gcc_checking_assert (x != y);
516 if (bx)
517 /* Avoid the lookup if Y has no conflicts. */
518 return by ? bitmap_bit_p (bx, y) : false;
519 else
520 return false;
524 /* Add a conflict with Y to the bitmap for X in graph PTR. */
526 static inline void
527 ssa_conflicts_add_one (ssa_conflicts *ptr, unsigned x, unsigned y)
529 bitmap bx = ptr->conflicts[x];
530 /* If there are no conflicts yet, allocate the bitmap and set bit. */
531 if (! bx)
532 bx = ptr->conflicts[x] = BITMAP_ALLOC (&ptr->obstack);
533 bitmap_set_bit (bx, y);
537 /* Add conflicts between X and Y in graph PTR. */
539 static inline void
540 ssa_conflicts_add (ssa_conflicts *ptr, unsigned x, unsigned y)
542 gcc_checking_assert (x != y);
543 ssa_conflicts_add_one (ptr, x, y);
544 ssa_conflicts_add_one (ptr, y, x);
548 /* Merge all Y's conflict into X in graph PTR. */
550 static inline void
551 ssa_conflicts_merge (ssa_conflicts *ptr, unsigned x, unsigned y)
553 unsigned z;
554 bitmap_iterator bi;
555 bitmap bx = ptr->conflicts[x];
556 bitmap by = ptr->conflicts[y];
558 gcc_checking_assert (x != y);
559 if (! by)
560 return;
562 /* Add a conflict between X and every one Y has. If the bitmap doesn't
563 exist, then it has already been coalesced, and we don't need to add a
564 conflict. */
565 EXECUTE_IF_SET_IN_BITMAP (by, 0, z, bi)
567 bitmap bz = ptr->conflicts[z];
568 if (bz)
569 bitmap_set_bit (bz, x);
572 if (bx)
574 /* If X has conflicts, add Y's to X. */
575 bitmap_ior_into (bx, by);
576 BITMAP_FREE (by);
577 ptr->conflicts[y] = NULL;
579 else
581 /* If X has no conflicts, simply use Y's. */
582 ptr->conflicts[x] = by;
583 ptr->conflicts[y] = NULL;
588 /* Dump a conflicts graph. */
590 static void
591 ssa_conflicts_dump (FILE *file, ssa_conflicts *ptr)
593 unsigned x;
594 bitmap b;
596 fprintf (file, "\nConflict graph:\n");
598 FOR_EACH_VEC_ELT (ptr->conflicts, x, b)
599 if (b)
601 fprintf (file, "%d: ", x);
602 dump_bitmap (file, b);
607 /* This structure is used to efficiently record the current status of live
608 SSA_NAMES when building a conflict graph.
609 LIVE_BASE_VAR has a bit set for each base variable which has at least one
610 ssa version live.
611 LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
612 index, and is used to track what partitions of each base variable are
613 live. This makes it easy to add conflicts between just live partitions
614 with the same base variable.
615 The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
616 marked as being live. This delays clearing of these bitmaps until
617 they are actually needed again. */
619 struct live_track
621 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
622 bitmap live_base_var; /* Indicates if a basevar is live. */
623 bitmap *live_base_partitions; /* Live partitions for each basevar. */
624 var_map map; /* Var_map being used for partition mapping. */
628 /* This routine will create a new live track structure based on the partitions
629 in MAP. */
631 static live_track *
632 new_live_track (var_map map)
634 live_track *ptr;
635 int lim, x;
637 /* Make sure there is a partition view in place. */
638 gcc_assert (map->partition_to_base_index != NULL);
640 ptr = (live_track *) xmalloc (sizeof (live_track));
641 ptr->map = map;
642 lim = num_basevars (map);
643 bitmap_obstack_initialize (&ptr->obstack);
644 ptr->live_base_partitions = (bitmap *) xmalloc (sizeof (bitmap *) * lim);
645 ptr->live_base_var = BITMAP_ALLOC (&ptr->obstack);
646 for (x = 0; x < lim; x++)
647 ptr->live_base_partitions[x] = BITMAP_ALLOC (&ptr->obstack);
648 return ptr;
652 /* This routine will free the memory associated with PTR. */
654 static void
655 delete_live_track (live_track *ptr)
657 bitmap_obstack_release (&ptr->obstack);
658 free (ptr->live_base_partitions);
659 free (ptr);
663 /* This function will remove PARTITION from the live list in PTR. */
665 static inline void
666 live_track_remove_partition (live_track *ptr, int partition)
668 int root;
670 root = basevar_index (ptr->map, partition);
671 bitmap_clear_bit (ptr->live_base_partitions[root], partition);
672 /* If the element list is empty, make the base variable not live either. */
673 if (bitmap_empty_p (ptr->live_base_partitions[root]))
674 bitmap_clear_bit (ptr->live_base_var, root);
678 /* This function will adds PARTITION to the live list in PTR. */
680 static inline void
681 live_track_add_partition (live_track *ptr, int partition)
683 int root;
685 root = basevar_index (ptr->map, partition);
686 /* If this base var wasn't live before, it is now. Clear the element list
687 since it was delayed until needed. */
688 if (bitmap_set_bit (ptr->live_base_var, root))
689 bitmap_clear (ptr->live_base_partitions[root]);
690 bitmap_set_bit (ptr->live_base_partitions[root], partition);
695 /* Clear the live bit for VAR in PTR. */
697 static inline void
698 live_track_clear_var (live_track *ptr, tree var)
700 int p;
702 p = var_to_partition (ptr->map, var);
703 if (p != NO_PARTITION)
704 live_track_remove_partition (ptr, p);
708 /* Return TRUE if VAR is live in PTR. */
710 static inline bool
711 live_track_live_p (live_track *ptr, tree var)
713 int p, root;
715 p = var_to_partition (ptr->map, var);
716 if (p != NO_PARTITION)
718 root = basevar_index (ptr->map, p);
719 if (bitmap_bit_p (ptr->live_base_var, root))
720 return bitmap_bit_p (ptr->live_base_partitions[root], p);
722 return false;
726 /* This routine will add USE to PTR. USE will be marked as live in both the
727 ssa live map and the live bitmap for the root of USE. */
729 static inline void
730 live_track_process_use (live_track *ptr, tree use)
732 int p;
734 p = var_to_partition (ptr->map, use);
735 if (p == NO_PARTITION)
736 return;
738 /* Mark as live in the appropriate live list. */
739 live_track_add_partition (ptr, p);
743 /* This routine will process a DEF in PTR. DEF will be removed from the live
744 lists, and if there are any other live partitions with the same base
745 variable, conflicts will be added to GRAPH. */
747 static inline void
748 live_track_process_def (live_track *ptr, tree def, ssa_conflicts *graph)
750 int p, root;
751 bitmap b;
752 unsigned x;
753 bitmap_iterator bi;
755 p = var_to_partition (ptr->map, def);
756 if (p == NO_PARTITION)
757 return;
759 /* Clear the liveness bit. */
760 live_track_remove_partition (ptr, p);
762 /* If the bitmap isn't empty now, conflicts need to be added. */
763 root = basevar_index (ptr->map, p);
764 if (bitmap_bit_p (ptr->live_base_var, root))
766 b = ptr->live_base_partitions[root];
767 EXECUTE_IF_SET_IN_BITMAP (b, 0, x, bi)
768 ssa_conflicts_add (graph, p, x);
773 /* Initialize PTR with the partitions set in INIT. */
775 static inline void
776 live_track_init (live_track *ptr, bitmap init)
778 unsigned p;
779 bitmap_iterator bi;
781 /* Mark all live on exit partitions. */
782 EXECUTE_IF_SET_IN_BITMAP (init, 0, p, bi)
783 live_track_add_partition (ptr, p);
787 /* This routine will clear all live partitions in PTR. */
789 static inline void
790 live_track_clear_base_vars (live_track *ptr)
792 /* Simply clear the live base list. Anything marked as live in the element
793 lists will be cleared later if/when the base variable ever comes alive
794 again. */
795 bitmap_clear (ptr->live_base_var);
799 /* Build a conflict graph based on LIVEINFO. Any partitions which are in the
800 partition view of the var_map liveinfo is based on get entries in the
801 conflict graph. Only conflicts between ssa_name partitions with the same
802 base variable are added. */
804 static ssa_conflicts *
805 build_ssa_conflict_graph (tree_live_info_p liveinfo)
807 ssa_conflicts *graph;
808 var_map map;
809 basic_block bb;
810 ssa_op_iter iter;
811 live_track *live;
812 basic_block entry;
814 /* If inter-variable coalescing is enabled, we may attempt to
815 coalesce variables from different base variables, including
816 different parameters, so we have to make sure default defs live
817 at the entry block conflict with each other. */
818 if (flag_tree_coalesce_vars)
819 entry = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
820 else
821 entry = NULL;
823 map = live_var_map (liveinfo);
824 graph = ssa_conflicts_new (num_var_partitions (map));
826 live = new_live_track (map);
828 FOR_EACH_BB_FN (bb, cfun)
830 /* Start with live on exit temporaries. */
831 live_track_init (live, live_on_exit (liveinfo, bb));
833 for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);
834 gsi_prev (&gsi))
836 tree var;
837 gimple *stmt = gsi_stmt (gsi);
839 /* A copy between 2 partitions does not introduce an interference
840 by itself. If they did, you would never be able to coalesce
841 two things which are copied. If the two variables really do
842 conflict, they will conflict elsewhere in the program.
844 This is handled by simply removing the SRC of the copy from the
845 live list, and processing the stmt normally. */
846 if (is_gimple_assign (stmt))
848 tree lhs = gimple_assign_lhs (stmt);
849 tree rhs1 = gimple_assign_rhs1 (stmt);
850 if (gimple_assign_copy_p (stmt)
851 && TREE_CODE (lhs) == SSA_NAME
852 && TREE_CODE (rhs1) == SSA_NAME)
853 live_track_clear_var (live, rhs1);
855 else if (is_gimple_debug (stmt))
856 continue;
858 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
859 live_track_process_def (live, var, graph);
861 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
862 live_track_process_use (live, var);
865 /* If result of a PHI is unused, looping over the statements will not
866 record any conflicts since the def was never live. Since the PHI node
867 is going to be translated out of SSA form, it will insert a copy.
868 There must be a conflict recorded between the result of the PHI and
869 any variables that are live. Otherwise the out-of-ssa translation
870 may create incorrect code. */
871 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
872 gsi_next (&gsi))
874 gphi *phi = gsi.phi ();
875 tree result = PHI_RESULT (phi);
876 if (live_track_live_p (live, result))
877 live_track_process_def (live, result, graph);
880 /* Pretend there are defs for params' default defs at the start
881 of the (post-)entry block. This will prevent PARM_DECLs from
882 coalescing into the same partition. Although RESULT_DECLs'
883 default defs don't have a useful initial value, we have to
884 prevent them from coalescing with PARM_DECLs' default defs
885 too, otherwise assign_parms would attempt to assign different
886 RTL to the same partition. */
887 if (bb == entry)
889 unsigned i;
890 for (i = 1; i < num_ssa_names; i++)
892 tree var = ssa_name (i);
894 if (!var
895 || !SSA_NAME_IS_DEFAULT_DEF (var)
896 || !SSA_NAME_VAR (var)
897 || VAR_P (SSA_NAME_VAR (var)))
898 continue;
900 live_track_process_def (live, var, graph);
901 /* Process a use too, so that it remains live and
902 conflicts with other parms' default defs, even unused
903 ones. */
904 live_track_process_use (live, var);
908 live_track_clear_base_vars (live);
911 delete_live_track (live);
912 return graph;
916 /* Shortcut routine to print messages to file F of the form:
917 "STR1 EXPR1 STR2 EXPR2 STR3." */
919 static inline void
920 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
921 tree expr2, const char *str3)
923 fprintf (f, "%s", str1);
924 print_generic_expr (f, expr1, TDF_SLIM);
925 fprintf (f, "%s", str2);
926 print_generic_expr (f, expr2, TDF_SLIM);
927 fprintf (f, "%s", str3);
931 /* Print a failure to coalesce a MUST_COALESCE pair X and Y. */
933 static inline void
934 fail_abnormal_edge_coalesce (int x, int y)
936 fprintf (stderr, "\nUnable to coalesce ssa_names %d and %d",x, y);
937 fprintf (stderr, " which are marked as MUST COALESCE.\n");
938 print_generic_expr (stderr, ssa_name (x), TDF_SLIM);
939 fprintf (stderr, " and ");
940 print_generic_stmt (stderr, ssa_name (y), TDF_SLIM);
942 internal_error ("SSA corruption");
945 /* Call CALLBACK for all PARM_DECLs and RESULT_DECLs for which
946 assign_parms may ask for a default partition. */
948 static void
949 for_all_parms (void (*callback)(tree var, void *arg), void *arg)
951 for (tree var = DECL_ARGUMENTS (current_function_decl); var;
952 var = DECL_CHAIN (var))
953 callback (var, arg);
954 if (!VOID_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
955 callback (DECL_RESULT (current_function_decl), arg);
956 if (cfun->static_chain_decl)
957 callback (cfun->static_chain_decl, arg);
960 /* Create a default def for VAR. */
962 static void
963 create_default_def (tree var, void *arg ATTRIBUTE_UNUSED)
965 if (!is_gimple_reg (var))
966 return;
968 tree ssa = get_or_create_ssa_default_def (cfun, var);
969 gcc_assert (ssa);
972 /* Register VAR's default def in MAP. */
974 static void
975 register_default_def (tree var, void *map_)
977 var_map map = (var_map)map_;
979 if (!is_gimple_reg (var))
980 return;
982 tree ssa = ssa_default_def (cfun, var);
983 gcc_assert (ssa);
985 register_ssa_partition (map, ssa);
988 /* If VAR is an SSA_NAME associated with a PARM_DECL or a RESULT_DECL,
989 and the DECL's default def is unused (i.e., it was introduced by
990 create_default_def), mark VAR and the default def for
991 coalescing. */
993 static void
994 coalesce_with_default (tree var, coalesce_list *cl, bitmap used_in_copy)
996 if (SSA_NAME_IS_DEFAULT_DEF (var)
997 || !SSA_NAME_VAR (var)
998 || VAR_P (SSA_NAME_VAR (var)))
999 return;
1001 tree ssa = ssa_default_def (cfun, SSA_NAME_VAR (var));
1002 if (!has_zero_uses (ssa))
1003 return;
1005 add_cost_one_coalesce (cl, SSA_NAME_VERSION (ssa), SSA_NAME_VERSION (var));
1006 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1007 /* Default defs will have their used_in_copy bits set at the end of
1008 create_outofssa_var_map. */
1011 /* This function creates a var_map for the current function as well as creating
1012 a coalesce list for use later in the out of ssa process. */
1014 static var_map
1015 create_outofssa_var_map (coalesce_list *cl, bitmap used_in_copy)
1017 gimple_stmt_iterator gsi;
1018 basic_block bb;
1019 tree var;
1020 gimple *stmt;
1021 tree first;
1022 var_map map;
1023 ssa_op_iter iter;
1024 int v1, v2, cost;
1025 unsigned i;
1027 for_all_parms (create_default_def, NULL);
1029 map = init_var_map (num_ssa_names);
1031 for_all_parms (register_default_def, map);
1033 FOR_EACH_BB_FN (bb, cfun)
1035 tree arg;
1037 for (gphi_iterator gpi = gsi_start_phis (bb);
1038 !gsi_end_p (gpi);
1039 gsi_next (&gpi))
1041 gphi *phi = gpi.phi ();
1042 size_t i;
1043 int ver;
1044 tree res;
1045 bool saw_copy = false;
1047 res = gimple_phi_result (phi);
1048 ver = SSA_NAME_VERSION (res);
1049 register_ssa_partition (map, res);
1051 /* Register ssa_names and coalesces between the args and the result
1052 of all PHI. */
1053 for (i = 0; i < gimple_phi_num_args (phi); i++)
1055 edge e = gimple_phi_arg_edge (phi, i);
1056 arg = PHI_ARG_DEF (phi, i);
1057 if (TREE_CODE (arg) != SSA_NAME)
1058 continue;
1060 register_ssa_partition (map, arg);
1061 if (gimple_can_coalesce_p (arg, res)
1062 || (e->flags & EDGE_ABNORMAL))
1064 saw_copy = true;
1065 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (arg));
1066 if ((e->flags & EDGE_ABNORMAL) == 0)
1068 int cost = coalesce_cost_edge (e);
1069 if (cost == 1 && has_single_use (arg))
1070 add_cost_one_coalesce (cl, ver, SSA_NAME_VERSION (arg));
1071 else
1072 add_coalesce (cl, ver, SSA_NAME_VERSION (arg), cost);
1076 if (saw_copy)
1077 bitmap_set_bit (used_in_copy, ver);
1080 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1082 stmt = gsi_stmt (gsi);
1084 if (is_gimple_debug (stmt))
1085 continue;
1087 /* Register USE and DEF operands in each statement. */
1088 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
1089 register_ssa_partition (map, var);
1091 /* Check for copy coalesces. */
1092 switch (gimple_code (stmt))
1094 case GIMPLE_ASSIGN:
1096 tree lhs = gimple_assign_lhs (stmt);
1097 tree rhs1 = gimple_assign_rhs1 (stmt);
1098 if (gimple_assign_ssa_name_copy_p (stmt)
1099 && gimple_can_coalesce_p (lhs, rhs1))
1101 v1 = SSA_NAME_VERSION (lhs);
1102 v2 = SSA_NAME_VERSION (rhs1);
1103 cost = coalesce_cost_bb (bb);
1104 add_coalesce (cl, v1, v2, cost);
1105 bitmap_set_bit (used_in_copy, v1);
1106 bitmap_set_bit (used_in_copy, v2);
1109 break;
1111 case GIMPLE_RETURN:
1113 tree res = DECL_RESULT (current_function_decl);
1114 if (VOID_TYPE_P (TREE_TYPE (res))
1115 || !is_gimple_reg (res))
1116 break;
1117 tree rhs1 = gimple_return_retval (as_a <greturn *> (stmt));
1118 if (!rhs1)
1119 break;
1120 tree lhs = ssa_default_def (cfun, res);
1121 gcc_assert (lhs);
1122 if (TREE_CODE (rhs1) == SSA_NAME
1123 && gimple_can_coalesce_p (lhs, rhs1))
1125 v1 = SSA_NAME_VERSION (lhs);
1126 v2 = SSA_NAME_VERSION (rhs1);
1127 cost = coalesce_cost_bb (bb);
1128 add_coalesce (cl, v1, v2, cost);
1129 bitmap_set_bit (used_in_copy, v1);
1130 bitmap_set_bit (used_in_copy, v2);
1132 break;
1135 case GIMPLE_ASM:
1137 gasm *asm_stmt = as_a <gasm *> (stmt);
1138 unsigned long noutputs, i;
1139 unsigned long ninputs;
1140 tree *outputs, link;
1141 noutputs = gimple_asm_noutputs (asm_stmt);
1142 ninputs = gimple_asm_ninputs (asm_stmt);
1143 outputs = (tree *) alloca (noutputs * sizeof (tree));
1144 for (i = 0; i < noutputs; ++i)
1146 link = gimple_asm_output_op (asm_stmt, i);
1147 outputs[i] = TREE_VALUE (link);
1150 for (i = 0; i < ninputs; ++i)
1152 const char *constraint;
1153 tree input;
1154 char *end;
1155 unsigned long match;
1157 link = gimple_asm_input_op (asm_stmt, i);
1158 constraint
1159 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1160 input = TREE_VALUE (link);
1162 if (TREE_CODE (input) != SSA_NAME)
1163 continue;
1165 match = strtoul (constraint, &end, 10);
1166 if (match >= noutputs || end == constraint)
1167 continue;
1169 if (TREE_CODE (outputs[match]) != SSA_NAME)
1170 continue;
1172 v1 = SSA_NAME_VERSION (outputs[match]);
1173 v2 = SSA_NAME_VERSION (input);
1175 if (gimple_can_coalesce_p (outputs[match], input))
1177 cost = coalesce_cost (REG_BR_PROB_BASE,
1178 optimize_bb_for_size_p (bb));
1179 add_coalesce (cl, v1, v2, cost);
1180 bitmap_set_bit (used_in_copy, v1);
1181 bitmap_set_bit (used_in_copy, v2);
1184 break;
1187 default:
1188 break;
1193 /* Now process result decls and live on entry variables for entry into
1194 the coalesce list. */
1195 first = NULL_TREE;
1196 for (i = 1; i < num_ssa_names; i++)
1198 var = ssa_name (i);
1199 if (var != NULL_TREE && !virtual_operand_p (var))
1201 coalesce_with_default (var, cl, used_in_copy);
1203 /* Add coalesces between all the result decls. */
1204 if (SSA_NAME_VAR (var)
1205 && TREE_CODE (SSA_NAME_VAR (var)) == RESULT_DECL)
1207 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1208 if (first == NULL_TREE)
1209 first = var;
1210 else
1212 gcc_assert (gimple_can_coalesce_p (var, first));
1213 v1 = SSA_NAME_VERSION (first);
1214 v2 = SSA_NAME_VERSION (var);
1215 cost = coalesce_cost_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
1216 add_coalesce (cl, v1, v2, cost);
1219 /* Mark any default_def variables as being in the coalesce list
1220 since they will have to be coalesced with the base variable. If
1221 not marked as present, they won't be in the coalesce view. */
1222 if (SSA_NAME_IS_DEFAULT_DEF (var)
1223 && (!has_zero_uses (var)
1224 || (SSA_NAME_VAR (var)
1225 && !VAR_P (SSA_NAME_VAR (var)))))
1226 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1230 return map;
1234 /* Attempt to coalesce ssa versions X and Y together using the partition
1235 mapping in MAP and checking conflicts in GRAPH. Output any debug info to
1236 DEBUG, if it is nun-NULL. */
1238 static inline bool
1239 attempt_coalesce (var_map map, ssa_conflicts *graph, int x, int y,
1240 FILE *debug)
1242 int z;
1243 tree var1, var2;
1244 int p1, p2;
1246 p1 = var_to_partition (map, ssa_name (x));
1247 p2 = var_to_partition (map, ssa_name (y));
1249 if (debug)
1251 fprintf (debug, "(%d)", x);
1252 print_generic_expr (debug, partition_to_var (map, p1), TDF_SLIM);
1253 fprintf (debug, " & (%d)", y);
1254 print_generic_expr (debug, partition_to_var (map, p2), TDF_SLIM);
1257 if (p1 == p2)
1259 if (debug)
1260 fprintf (debug, ": Already Coalesced.\n");
1261 return true;
1264 if (debug)
1265 fprintf (debug, " [map: %d, %d] ", p1, p2);
1268 if (!ssa_conflicts_test_p (graph, p1, p2))
1270 var1 = partition_to_var (map, p1);
1271 var2 = partition_to_var (map, p2);
1273 z = var_union (map, var1, var2);
1274 if (z == NO_PARTITION)
1276 if (debug)
1277 fprintf (debug, ": Unable to perform partition union.\n");
1278 return false;
1281 /* z is the new combined partition. Remove the other partition from
1282 the list, and merge the conflicts. */
1283 if (z == p1)
1284 ssa_conflicts_merge (graph, p1, p2);
1285 else
1286 ssa_conflicts_merge (graph, p2, p1);
1288 if (debug)
1289 fprintf (debug, ": Success -> %d\n", z);
1291 return true;
1294 if (debug)
1295 fprintf (debug, ": Fail due to conflict\n");
1297 return false;
1301 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1302 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1304 static void
1305 coalesce_partitions (var_map map, ssa_conflicts *graph, coalesce_list *cl,
1306 FILE *debug)
1308 int x = 0, y = 0;
1309 tree var1, var2;
1310 int cost;
1311 basic_block bb;
1312 edge e;
1313 edge_iterator ei;
1315 /* First, coalesce all the copies across abnormal edges. These are not placed
1316 in the coalesce list because they do not need to be sorted, and simply
1317 consume extra memory/compilation time in large programs. */
1319 FOR_EACH_BB_FN (bb, cfun)
1321 FOR_EACH_EDGE (e, ei, bb->preds)
1322 if (e->flags & EDGE_ABNORMAL)
1324 gphi_iterator gsi;
1325 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1326 gsi_next (&gsi))
1328 gphi *phi = gsi.phi ();
1329 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1330 if (SSA_NAME_IS_DEFAULT_DEF (arg)
1331 && (!SSA_NAME_VAR (arg)
1332 || TREE_CODE (SSA_NAME_VAR (arg)) != PARM_DECL))
1333 continue;
1335 tree res = PHI_RESULT (phi);
1336 int v1 = SSA_NAME_VERSION (res);
1337 int v2 = SSA_NAME_VERSION (arg);
1339 if (debug)
1340 fprintf (debug, "Abnormal coalesce: ");
1342 if (!attempt_coalesce (map, graph, v1, v2, debug))
1343 fail_abnormal_edge_coalesce (v1, v2);
1348 /* Now process the items in the coalesce list. */
1350 while ((cost = pop_best_coalesce (cl, &x, &y)) != NO_BEST_COALESCE)
1352 var1 = ssa_name (x);
1353 var2 = ssa_name (y);
1355 /* Assert the coalesces have the same base variable. */
1356 gcc_assert (gimple_can_coalesce_p (var1, var2));
1358 if (debug)
1359 fprintf (debug, "Coalesce list: ");
1360 attempt_coalesce (map, graph, x, y, debug);
1365 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1367 struct ssa_name_var_hash : nofree_ptr_hash <tree_node>
1369 static inline hashval_t hash (const tree_node *);
1370 static inline int equal (const tree_node *, const tree_node *);
1373 inline hashval_t
1374 ssa_name_var_hash::hash (const_tree n)
1376 return DECL_UID (SSA_NAME_VAR (n));
1379 inline int
1380 ssa_name_var_hash::equal (const tree_node *n1, const tree_node *n2)
1382 return SSA_NAME_VAR (n1) == SSA_NAME_VAR (n2);
1386 /* Output partition map MAP with coalescing plan PART to file F. */
1388 void
1389 dump_part_var_map (FILE *f, partition part, var_map map)
1391 int t;
1392 unsigned x, y;
1393 int p;
1395 fprintf (f, "\nCoalescible Partition map \n\n");
1397 for (x = 0; x < map->num_partitions; x++)
1399 if (map->view_to_partition != NULL)
1400 p = map->view_to_partition[x];
1401 else
1402 p = x;
1404 if (ssa_name (p) == NULL_TREE
1405 || virtual_operand_p (ssa_name (p)))
1406 continue;
1408 t = 0;
1409 for (y = 1; y < num_ssa_names; y++)
1411 tree var = version_to_var (map, y);
1412 if (!var)
1413 continue;
1414 int q = var_to_partition (map, var);
1415 p = partition_find (part, q);
1416 gcc_assert (map->partition_to_base_index[q]
1417 == map->partition_to_base_index[p]);
1419 if (p == (int)x)
1421 if (t++ == 0)
1423 fprintf (f, "Partition %d, base %d (", x,
1424 map->partition_to_base_index[q]);
1425 print_generic_expr (f, partition_to_var (map, q), TDF_SLIM);
1426 fprintf (f, " - ");
1428 fprintf (f, "%d ", y);
1431 if (t != 0)
1432 fprintf (f, ")\n");
1434 fprintf (f, "\n");
1437 /* Given SSA_NAMEs NAME1 and NAME2, return true if they are candidates for
1438 coalescing together, false otherwise.
1440 This must stay consistent with var_map_base_init in tree-ssa-live.c. */
1442 bool
1443 gimple_can_coalesce_p (tree name1, tree name2)
1445 /* First check the SSA_NAME's associated DECL. Without
1446 optimization, we only want to coalesce if they have the same DECL
1447 or both have no associated DECL. */
1448 tree var1 = SSA_NAME_VAR (name1);
1449 tree var2 = SSA_NAME_VAR (name2);
1450 var1 = (var1 && (!VAR_P (var1) || !DECL_IGNORED_P (var1))) ? var1 : NULL_TREE;
1451 var2 = (var2 && (!VAR_P (var2) || !DECL_IGNORED_P (var2))) ? var2 : NULL_TREE;
1452 if (var1 != var2 && !flag_tree_coalesce_vars)
1453 return false;
1455 /* Now check the types. If the types are the same, then we should
1456 try to coalesce V1 and V2. */
1457 tree t1 = TREE_TYPE (name1);
1458 tree t2 = TREE_TYPE (name2);
1459 if (t1 == t2)
1461 check_modes:
1462 /* If the base variables are the same, we're good: none of the
1463 other tests below could possibly fail. */
1464 var1 = SSA_NAME_VAR (name1);
1465 var2 = SSA_NAME_VAR (name2);
1466 if (var1 == var2)
1467 return true;
1469 /* We don't want to coalesce two SSA names if one of the base
1470 variables is supposed to be a register while the other is
1471 supposed to be on the stack. Anonymous SSA names most often
1472 take registers, but when not optimizing, user variables
1473 should go on the stack, so coalescing them with the anonymous
1474 variable as the partition leader would end up assigning the
1475 user variable to a register. Don't do that! */
1476 bool reg1 = use_register_for_decl (name1);
1477 bool reg2 = use_register_for_decl (name2);
1478 if (reg1 != reg2)
1479 return false;
1481 /* Check that the promoted modes and unsignedness are the same.
1482 We don't want to coalesce if the promoted modes would be
1483 different, or if they would sign-extend differently. Only
1484 PARM_DECLs and RESULT_DECLs have different promotion rules,
1485 so skip the test if both are variables, or both are anonymous
1486 SSA_NAMEs. */
1487 int unsigned1, unsigned2;
1488 return ((!var1 || VAR_P (var1)) && (!var2 || VAR_P (var2)))
1489 || ((promote_ssa_mode (name1, &unsigned1)
1490 == promote_ssa_mode (name2, &unsigned2))
1491 && unsigned1 == unsigned2);
1494 /* If alignment requirements are different, we can't coalesce. */
1495 if (MINIMUM_ALIGNMENT (t1,
1496 var1 ? DECL_MODE (var1) : TYPE_MODE (t1),
1497 var1 ? LOCAL_DECL_ALIGNMENT (var1) : TYPE_ALIGN (t1))
1498 != MINIMUM_ALIGNMENT (t2,
1499 var2 ? DECL_MODE (var2) : TYPE_MODE (t2),
1500 var2 ? LOCAL_DECL_ALIGNMENT (var2) : TYPE_ALIGN (t2)))
1501 return false;
1503 /* If the types are not the same, check for a canonical type match. This
1504 (for example) allows coalescing when the types are fundamentally the
1505 same, but just have different names.
1507 Note pointer types with different address spaces may have the same
1508 canonical type. Those are rejected for coalescing by the
1509 types_compatible_p check. */
1510 if (TYPE_CANONICAL (t1)
1511 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)
1512 && types_compatible_p (t1, t2))
1513 goto check_modes;
1515 return false;
1518 /* Fill in MAP's partition_to_base_index, with one index for each
1519 partition of SSA names USED_IN_COPIES and related by CL coalesce
1520 possibilities. This must match gimple_can_coalesce_p in the
1521 optimized case. */
1523 static void
1524 compute_optimized_partition_bases (var_map map, bitmap used_in_copies,
1525 coalesce_list *cl)
1527 int parts = num_var_partitions (map);
1528 partition tentative = partition_new (parts);
1530 /* Partition the SSA versions so that, for each coalescible
1531 pair, both of its members are in the same partition in
1532 TENTATIVE. */
1533 gcc_assert (!cl->sorted);
1534 coalesce_pair *node;
1535 coalesce_iterator_type ppi;
1536 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
1538 tree v1 = ssa_name (node->first_element);
1539 int p1 = partition_find (tentative, var_to_partition (map, v1));
1540 tree v2 = ssa_name (node->second_element);
1541 int p2 = partition_find (tentative, var_to_partition (map, v2));
1543 if (p1 == p2)
1544 continue;
1546 partition_union (tentative, p1, p2);
1549 /* We have to deal with cost one pairs too. */
1550 for (cost_one_pair *co = cl->cost_one_list; co; co = co->next)
1552 tree v1 = ssa_name (co->first_element);
1553 int p1 = partition_find (tentative, var_to_partition (map, v1));
1554 tree v2 = ssa_name (co->second_element);
1555 int p2 = partition_find (tentative, var_to_partition (map, v2));
1557 if (p1 == p2)
1558 continue;
1560 partition_union (tentative, p1, p2);
1563 /* And also with abnormal edges. */
1564 basic_block bb;
1565 edge e;
1566 edge_iterator ei;
1567 FOR_EACH_BB_FN (bb, cfun)
1569 FOR_EACH_EDGE (e, ei, bb->preds)
1570 if (e->flags & EDGE_ABNORMAL)
1572 gphi_iterator gsi;
1573 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1574 gsi_next (&gsi))
1576 gphi *phi = gsi.phi ();
1577 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1578 if (SSA_NAME_IS_DEFAULT_DEF (arg)
1579 && (!SSA_NAME_VAR (arg)
1580 || TREE_CODE (SSA_NAME_VAR (arg)) != PARM_DECL))
1581 continue;
1583 tree res = PHI_RESULT (phi);
1585 int p1 = partition_find (tentative, var_to_partition (map, res));
1586 int p2 = partition_find (tentative, var_to_partition (map, arg));
1588 if (p1 == p2)
1589 continue;
1591 partition_union (tentative, p1, p2);
1596 map->partition_to_base_index = XCNEWVEC (int, parts);
1597 auto_vec<unsigned int> index_map (parts);
1598 if (parts)
1599 index_map.quick_grow (parts);
1601 const unsigned no_part = -1;
1602 unsigned count = parts;
1603 while (count)
1604 index_map[--count] = no_part;
1606 /* Initialize MAP's mapping from partition to base index, using
1607 as base indices an enumeration of the TENTATIVE partitions in
1608 which each SSA version ended up, so that we compute conflicts
1609 between all SSA versions that ended up in the same potential
1610 coalesce partition. */
1611 bitmap_iterator bi;
1612 unsigned i;
1613 EXECUTE_IF_SET_IN_BITMAP (used_in_copies, 0, i, bi)
1615 int pidx = var_to_partition (map, ssa_name (i));
1616 int base = partition_find (tentative, pidx);
1617 if (index_map[base] != no_part)
1618 continue;
1619 index_map[base] = count++;
1622 map->num_basevars = count;
1624 EXECUTE_IF_SET_IN_BITMAP (used_in_copies, 0, i, bi)
1626 int pidx = var_to_partition (map, ssa_name (i));
1627 int base = partition_find (tentative, pidx);
1628 gcc_assert (index_map[base] < count);
1629 map->partition_to_base_index[pidx] = index_map[base];
1632 if (dump_file && (dump_flags & TDF_DETAILS))
1633 dump_part_var_map (dump_file, tentative, map);
1635 partition_delete (tentative);
1638 /* Hashtable helpers. */
1640 struct tree_int_map_hasher : nofree_ptr_hash <tree_int_map>
1642 static inline hashval_t hash (const tree_int_map *);
1643 static inline bool equal (const tree_int_map *, const tree_int_map *);
1646 inline hashval_t
1647 tree_int_map_hasher::hash (const tree_int_map *v)
1649 return tree_map_base_hash (v);
1652 inline bool
1653 tree_int_map_hasher::equal (const tree_int_map *v, const tree_int_map *c)
1655 return tree_int_map_eq (v, c);
1658 /* This routine will initialize the basevar fields of MAP with base
1659 names. Partitions will share the same base if they have the same
1660 SSA_NAME_VAR, or, being anonymous variables, the same type. This
1661 must match gimple_can_coalesce_p in the non-optimized case. */
1663 static void
1664 compute_samebase_partition_bases (var_map map)
1666 int x, num_part;
1667 tree var;
1668 struct tree_int_map *m, *mapstorage;
1670 num_part = num_var_partitions (map);
1671 hash_table<tree_int_map_hasher> tree_to_index (num_part);
1672 /* We can have at most num_part entries in the hash tables, so it's
1673 enough to allocate so many map elements once, saving some malloc
1674 calls. */
1675 mapstorage = m = XNEWVEC (struct tree_int_map, num_part);
1677 /* If a base table already exists, clear it, otherwise create it. */
1678 free (map->partition_to_base_index);
1679 map->partition_to_base_index = (int *) xmalloc (sizeof (int) * num_part);
1681 /* Build the base variable list, and point partitions at their bases. */
1682 for (x = 0; x < num_part; x++)
1684 struct tree_int_map **slot;
1685 unsigned baseindex;
1686 var = partition_to_var (map, x);
1687 if (SSA_NAME_VAR (var)
1688 && (!VAR_P (SSA_NAME_VAR (var))
1689 || !DECL_IGNORED_P (SSA_NAME_VAR (var))))
1690 m->base.from = SSA_NAME_VAR (var);
1691 else
1692 /* This restricts what anonymous SSA names we can coalesce
1693 as it restricts the sets we compute conflicts for.
1694 Using TREE_TYPE to generate sets is the easies as
1695 type equivalency also holds for SSA names with the same
1696 underlying decl.
1698 Check gimple_can_coalesce_p when changing this code. */
1699 m->base.from = (TYPE_CANONICAL (TREE_TYPE (var))
1700 ? TYPE_CANONICAL (TREE_TYPE (var))
1701 : TREE_TYPE (var));
1702 /* If base variable hasn't been seen, set it up. */
1703 slot = tree_to_index.find_slot (m, INSERT);
1704 if (!*slot)
1706 baseindex = m - mapstorage;
1707 m->to = baseindex;
1708 *slot = m;
1709 m++;
1711 else
1712 baseindex = (*slot)->to;
1713 map->partition_to_base_index[x] = baseindex;
1716 map->num_basevars = m - mapstorage;
1718 free (mapstorage);
1721 /* Reduce the number of copies by coalescing variables in the function. Return
1722 a partition map with the resulting coalesces. */
1724 extern var_map
1725 coalesce_ssa_name (void)
1727 tree_live_info_p liveinfo;
1728 ssa_conflicts *graph;
1729 coalesce_list *cl;
1730 bitmap used_in_copies = BITMAP_ALLOC (NULL);
1731 var_map map;
1732 unsigned int i;
1734 cl = create_coalesce_list ();
1735 map = create_outofssa_var_map (cl, used_in_copies);
1737 /* If this optimization is disabled, we need to coalesce all the
1738 names originating from the same SSA_NAME_VAR so debug info
1739 remains undisturbed. */
1740 if (!flag_tree_coalesce_vars)
1742 hash_table<ssa_name_var_hash> ssa_name_hash (10);
1744 for (i = 1; i < num_ssa_names; i++)
1746 tree a = ssa_name (i);
1748 if (a
1749 && SSA_NAME_VAR (a)
1750 && !DECL_IGNORED_P (SSA_NAME_VAR (a))
1751 && (!has_zero_uses (a) || !SSA_NAME_IS_DEFAULT_DEF (a)
1752 || !VAR_P (SSA_NAME_VAR (a))))
1754 tree *slot = ssa_name_hash.find_slot (a, INSERT);
1756 if (!*slot)
1757 *slot = a;
1758 else
1760 /* If the variable is a PARM_DECL or a RESULT_DECL, we
1761 _require_ that all the names originating from it be
1762 coalesced, because there must be a single partition
1763 containing all the names so that it can be assigned
1764 the canonical RTL location of the DECL safely.
1765 If in_lto_p, a function could have been compiled
1766 originally with optimizations and only the link
1767 performed at -O0, so we can't actually require it. */
1768 const int cost
1769 = (TREE_CODE (SSA_NAME_VAR (a)) == VAR_DECL || in_lto_p)
1770 ? MUST_COALESCE_COST - 1 : MUST_COALESCE_COST;
1771 add_coalesce (cl, SSA_NAME_VERSION (a),
1772 SSA_NAME_VERSION (*slot), cost);
1773 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (a));
1774 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (*slot));
1779 if (dump_file && (dump_flags & TDF_DETAILS))
1780 dump_var_map (dump_file, map);
1782 partition_view_bitmap (map, used_in_copies);
1784 if (flag_tree_coalesce_vars)
1785 compute_optimized_partition_bases (map, used_in_copies, cl);
1786 else
1787 compute_samebase_partition_bases (map);
1789 BITMAP_FREE (used_in_copies);
1791 if (num_var_partitions (map) < 1)
1793 delete_coalesce_list (cl);
1794 return map;
1797 if (dump_file && (dump_flags & TDF_DETAILS))
1798 dump_var_map (dump_file, map);
1800 liveinfo = calculate_live_ranges (map, false);
1802 if (dump_file && (dump_flags & TDF_DETAILS))
1803 dump_live_info (dump_file, liveinfo, LIVEDUMP_ENTRY);
1805 /* Build a conflict graph. */
1806 graph = build_ssa_conflict_graph (liveinfo);
1807 delete_tree_live_info (liveinfo);
1808 if (dump_file && (dump_flags & TDF_DETAILS))
1809 ssa_conflicts_dump (dump_file, graph);
1811 sort_coalesce_list (cl);
1813 if (dump_file && (dump_flags & TDF_DETAILS))
1815 fprintf (dump_file, "\nAfter sorting:\n");
1816 dump_coalesce_list (dump_file, cl);
1819 /* First, coalesce all live on entry variables to their base variable.
1820 This will ensure the first use is coming from the correct location. */
1822 if (dump_file && (dump_flags & TDF_DETAILS))
1823 dump_var_map (dump_file, map);
1825 /* Now coalesce everything in the list. */
1826 coalesce_partitions (map, graph, cl,
1827 ((dump_flags & TDF_DETAILS) ? dump_file : NULL));
1829 delete_coalesce_list (cl);
1830 ssa_conflicts_delete (graph);
1832 return map;
1835 /* We need to pass two arguments to set_parm_default_def_partition,
1836 but for_all_parms only supports one. Use a pair. */
1838 typedef std::pair<var_map, bitmap> parm_default_def_partition_arg;
1840 /* Set in ARG's PARTS bitmap the bit corresponding to the partition in
1841 ARG's MAP containing VAR's default def. */
1843 static void
1844 set_parm_default_def_partition (tree var, void *arg_)
1846 parm_default_def_partition_arg *arg = (parm_default_def_partition_arg *)arg_;
1847 var_map map = arg->first;
1848 bitmap parts = arg->second;
1850 if (!is_gimple_reg (var))
1851 return;
1853 tree ssa = ssa_default_def (cfun, var);
1854 gcc_assert (ssa);
1856 int version = var_to_partition (map, ssa);
1857 gcc_assert (version != NO_PARTITION);
1859 bool changed = bitmap_set_bit (parts, version);
1860 gcc_assert (changed);
1863 /* Allocate and return a bitmap that has a bit set for each partition
1864 that contains a default def for a parameter. */
1866 extern bitmap
1867 get_parm_default_def_partitions (var_map map)
1869 bitmap parm_default_def_parts = BITMAP_ALLOC (NULL);
1871 parm_default_def_partition_arg
1872 arg = std::make_pair (map, parm_default_def_parts);
1874 for_all_parms (set_parm_default_def_partition, &arg);
1876 return parm_default_def_parts;