xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/tree-ssa-coalesce.c (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
1 /* Coalesce SSA_NAMES together for the out-of-ssa pass.
2    Copyright (C) 2004-2017 Free Software Foundation, Inc.
3    Contributed by Andrew MacLeod <amacleod@redhat.com>
4 
5 This file is part of GCC.
6 
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.
11 
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.
16 
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/>.  */
20 
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "predict.h"
28 #include "memmodel.h"
29 #include "tm_p.h"
30 #include "ssa.h"
31 #include "tree-pretty-print.h"
32 #include "diagnostic-core.h"
33 #include "dumpfile.h"
34 #include "gimple-iterator.h"
35 #include "tree-ssa-live.h"
36 #include "tree-ssa-coalesce.h"
37 #include "explow.h"
38 #include "tree-dfa.h"
39 #include "stor-layout.h"
40 
41 /* This set of routines implements a coalesce_list.  This is an object which
42    is used to track pairs of ssa_names which are desirable to coalesce
43    together to avoid copies.  Costs are associated with each pair, and when
44    all desired information has been collected, the object can be used to
45    order the pairs for processing.  */
46 
47 /* This structure defines a pair entry.  */
48 
49 struct coalesce_pair
50 {
51   int first_element;
52   int second_element;
53   int cost;
54 
55   /* A count of the number of unique partitions this pair would conflict
56      with if coalescing was successful.  This is the secondary sort key,
57      given two pairs with equal costs, we will prefer the pair with a smaller
58      conflict set.
59 
60      This is lazily initialized when we discover two coalescing pairs have
61      the same primary cost.
62 
63      Note this is not updated and propagated as pairs are coalesced.  */
64   int conflict_count;
65 
66   /* The order in which coalescing pairs are discovered is recorded in this
67      field, which is used as the final tie breaker when sorting coalesce
68      pairs.  */
69   int index;
70 };
71 
72 /* This represents a conflict graph.  Implemented as an array of bitmaps.
73    A full matrix is used for conflicts rather than just upper triangular form.
74    this makes it much simpler and faster to perform conflict merges.  */
75 
76 struct ssa_conflicts
77 {
78   bitmap_obstack obstack;	/* A place to allocate our bitmaps.  */
79   vec<bitmap> conflicts;
80 };
81 
82 /* The narrow API of the qsort comparison function doesn't allow easy
83    access to additional arguments.  So we have two globals (ick) to hold
84    the data we need.  They're initialized before the call to qsort and
85    wiped immediately after.  */
86 static ssa_conflicts *conflicts_;
87 static var_map map_;
88 
89 /* Coalesce pair hashtable helpers.  */
90 
91 struct coalesce_pair_hasher : nofree_ptr_hash <coalesce_pair>
92 {
93   static inline hashval_t hash (const coalesce_pair *);
94   static inline bool equal (const coalesce_pair *, const coalesce_pair *);
95 };
96 
97 /* Hash function for coalesce list.  Calculate hash for PAIR.   */
98 
99 inline hashval_t
100 coalesce_pair_hasher::hash (const coalesce_pair *pair)
101 {
102   hashval_t a = (hashval_t)(pair->first_element);
103   hashval_t b = (hashval_t)(pair->second_element);
104 
105   return b * (b - 1) / 2 + a;
106 }
107 
108 /* Equality function for coalesce list hash table.  Compare PAIR1 and PAIR2,
109    returning TRUE if the two pairs are equivalent.  */
110 
111 inline bool
112 coalesce_pair_hasher::equal (const coalesce_pair *p1, const coalesce_pair *p2)
113 {
114   return (p1->first_element == p2->first_element
115 	  && p1->second_element == p2->second_element);
116 }
117 
118 typedef hash_table<coalesce_pair_hasher> coalesce_table_type;
119 typedef coalesce_table_type::iterator coalesce_iterator_type;
120 
121 
122 struct cost_one_pair
123 {
124   int first_element;
125   int second_element;
126   cost_one_pair *next;
127 };
128 
129 /* This structure maintains the list of coalesce pairs.  */
130 
131 struct coalesce_list
132 {
133   coalesce_table_type *list;	/* Hash table.  */
134   coalesce_pair **sorted;	/* List when sorted.  */
135   int num_sorted;		/* Number in the sorted list.  */
136   cost_one_pair *cost_one_list;/* Single use coalesces with cost 1.  */
137 };
138 
139 #define NO_BEST_COALESCE	-1
140 #define MUST_COALESCE_COST	INT_MAX
141 
142 
143 /* Return cost of execution of copy instruction with FREQUENCY.  */
144 
145 static inline int
146 coalesce_cost (int frequency, bool optimize_for_size)
147 {
148   /* Base costs on BB frequencies bounded by 1.  */
149   int cost = frequency;
150 
151   if (!cost)
152     cost = 1;
153 
154   if (optimize_for_size)
155     cost = 1;
156 
157   return cost;
158 }
159 
160 
161 /* Return the cost of executing a copy instruction in basic block BB.  */
162 
163 static inline int
164 coalesce_cost_bb (basic_block bb)
165 {
166   return coalesce_cost (bb->frequency, optimize_bb_for_size_p (bb));
167 }
168 
169 
170 /* Return the cost of executing a copy instruction on edge E.  */
171 
172 static inline int
173 coalesce_cost_edge (edge e)
174 {
175   int mult = 1;
176 
177   /* Inserting copy on critical edge costs more than inserting it elsewhere.  */
178   if (EDGE_CRITICAL_P (e))
179     mult = 2;
180   if (e->flags & EDGE_ABNORMAL)
181     return MUST_COALESCE_COST;
182   if (e->flags & EDGE_EH)
183     {
184       edge e2;
185       edge_iterator ei;
186       FOR_EACH_EDGE (e2, ei, e->dest->preds)
187 	if (e2 != e)
188 	  {
189 	    /* Putting code on EH edge that leads to BB
190 	       with multiple predecestors imply splitting of
191 	       edge too.  */
192 	    if (mult < 2)
193 	      mult = 2;
194 	    /* If there are multiple EH predecestors, we
195 	       also copy EH regions and produce separate
196 	       landing pad.  This is expensive.  */
197 	    if (e2->flags & EDGE_EH)
198 	      {
199 	        mult = 5;
200 	        break;
201 	      }
202 	  }
203     }
204 
205   return coalesce_cost (EDGE_FREQUENCY (e),
206 			optimize_edge_for_size_p (e)) * mult;
207 }
208 
209 
210 /* Retrieve a pair to coalesce from the cost_one_list in CL.  Returns the
211    2 elements via P1 and P2.  1 is returned by the function if there is a pair,
212    NO_BEST_COALESCE is returned if there aren't any.  */
213 
214 static inline int
215 pop_cost_one_pair (coalesce_list *cl, int *p1, int *p2)
216 {
217   cost_one_pair *ptr;
218 
219   ptr = cl->cost_one_list;
220   if (!ptr)
221     return NO_BEST_COALESCE;
222 
223   *p1 = ptr->first_element;
224   *p2 = ptr->second_element;
225   cl->cost_one_list = ptr->next;
226 
227   free (ptr);
228 
229   return 1;
230 }
231 
232 /* Retrieve the most expensive remaining pair to coalesce from CL.  Returns the
233    2 elements via P1 and P2.  Their calculated cost is returned by the function.
234    NO_BEST_COALESCE is returned if the coalesce list is empty.  */
235 
236 static inline int
237 pop_best_coalesce (coalesce_list *cl, int *p1, int *p2)
238 {
239   coalesce_pair *node;
240   int ret;
241 
242   if (cl->sorted == NULL)
243     return pop_cost_one_pair (cl, p1, p2);
244 
245   if (cl->num_sorted == 0)
246     return pop_cost_one_pair (cl, p1, p2);
247 
248   node = cl->sorted[--(cl->num_sorted)];
249   *p1 = node->first_element;
250   *p2 = node->second_element;
251   ret = node->cost;
252   free (node);
253 
254   return ret;
255 }
256 
257 
258 /* Create a new empty coalesce list object and return it.  */
259 
260 static inline coalesce_list *
261 create_coalesce_list (void)
262 {
263   coalesce_list *list;
264   unsigned size = num_ssa_names * 3;
265 
266   if (size < 40)
267     size = 40;
268 
269   list = (coalesce_list *) xmalloc (sizeof (struct coalesce_list));
270   list->list = new coalesce_table_type (size);
271   list->sorted = NULL;
272   list->num_sorted = 0;
273   list->cost_one_list = NULL;
274   return list;
275 }
276 
277 
278 /* Delete coalesce list CL.  */
279 
280 static inline void
281 delete_coalesce_list (coalesce_list *cl)
282 {
283   gcc_assert (cl->cost_one_list == NULL);
284   delete cl->list;
285   cl->list = NULL;
286   free (cl->sorted);
287   gcc_assert (cl->num_sorted == 0);
288   free (cl);
289 }
290 
291 /* Return the number of unique coalesce pairs in CL.  */
292 
293 static inline int
294 num_coalesce_pairs (coalesce_list *cl)
295 {
296   return cl->list->elements ();
297 }
298 
299 /* Find a matching coalesce pair object in CL for the pair P1 and P2.  If
300    one isn't found, return NULL if CREATE is false, otherwise create a new
301    coalesce pair object and return it.  */
302 
303 static coalesce_pair *
304 find_coalesce_pair (coalesce_list *cl, int p1, int p2, bool create)
305 {
306   struct coalesce_pair p;
307   coalesce_pair **slot;
308   unsigned int hash;
309 
310   /* Normalize so that p1 is the smaller value.  */
311   if (p2 < p1)
312     {
313       p.first_element = p2;
314       p.second_element = p1;
315     }
316   else
317     {
318       p.first_element = p1;
319       p.second_element = p2;
320     }
321 
322   hash = coalesce_pair_hasher::hash (&p);
323   slot = cl->list->find_slot_with_hash (&p, hash, create ? INSERT : NO_INSERT);
324   if (!slot)
325     return NULL;
326 
327   if (!*slot)
328     {
329       struct coalesce_pair * pair = XNEW (struct coalesce_pair);
330       gcc_assert (cl->sorted == NULL);
331       pair->first_element = p.first_element;
332       pair->second_element = p.second_element;
333       pair->cost = 0;
334       pair->index = num_coalesce_pairs (cl);
335       pair->conflict_count = 0;
336       *slot = pair;
337     }
338 
339   return (struct coalesce_pair *) *slot;
340 }
341 
342 static inline void
343 add_cost_one_coalesce (coalesce_list *cl, int p1, int p2)
344 {
345   cost_one_pair *pair;
346 
347   pair = XNEW (cost_one_pair);
348   pair->first_element = p1;
349   pair->second_element = p2;
350   pair->next = cl->cost_one_list;
351   cl->cost_one_list = pair;
352 }
353 
354 
355 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE.  */
356 
357 static inline void
358 add_coalesce (coalesce_list *cl, int p1, int p2, int value)
359 {
360   coalesce_pair *node;
361 
362   gcc_assert (cl->sorted == NULL);
363   if (p1 == p2)
364     return;
365 
366   node = find_coalesce_pair (cl, p1, p2, true);
367 
368   /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way.  */
369   if (node->cost < MUST_COALESCE_COST - 1)
370     {
371       if (value < MUST_COALESCE_COST - 1)
372 	node->cost += value;
373       else
374 	node->cost = value;
375     }
376 }
377 
378 /* Compute and record how many unique conflicts would exist for the
379    representative partition for each coalesce pair in CL.
380 
381    CONFLICTS is the conflict graph and MAP is the current partition view.  */
382 
383 static void
384 initialize_conflict_count (coalesce_pair *p,
385 			   ssa_conflicts *conflicts,
386 			   var_map map)
387 {
388   int p1 = var_to_partition (map, ssa_name (p->first_element));
389   int p2 = var_to_partition (map, ssa_name (p->second_element));
390 
391   /* 4 cases.  If both P1 and P2 have conflicts, then build their
392      union and count the members.  Else handle the degenerate cases
393      in the obvious ways.  */
394   if (conflicts->conflicts[p1] && conflicts->conflicts[p2])
395     p->conflict_count = bitmap_count_unique_bits (conflicts->conflicts[p1],
396 						  conflicts->conflicts[p2]);
397   else if (conflicts->conflicts[p1])
398     p->conflict_count = bitmap_count_bits (conflicts->conflicts[p1]);
399   else if (conflicts->conflicts[p2])
400     p->conflict_count = bitmap_count_bits (conflicts->conflicts[p2]);
401   else
402     p->conflict_count = 0;
403 }
404 
405 
406 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order.  */
407 
408 static int
409 compare_pairs (const void *p1, const void *p2)
410 {
411   coalesce_pair *const *const pp1 = (coalesce_pair *const *) p1;
412   coalesce_pair *const *const pp2 = (coalesce_pair *const *) p2;
413   int result;
414 
415   result = (* pp1)->cost - (* pp2)->cost;
416   /* We use the size of the resulting conflict set as the secondary sort key.
417      Given two equal costing coalesce pairs, we want to prefer the pair that
418      has the smaller conflict set.  */
419   if (result == 0)
420     {
421       if (flag_expensive_optimizations)
422 	{
423 	  /* Lazily initialize the conflict counts as it's fairly expensive
424 	     to compute.  */
425 	  if ((*pp2)->conflict_count == 0)
426 	    initialize_conflict_count (*pp2, conflicts_, map_);
427 	  if ((*pp1)->conflict_count == 0)
428 	    initialize_conflict_count (*pp1, conflicts_, map_);
429 
430 	  result = (*pp2)->conflict_count - (*pp1)->conflict_count;
431 	}
432 
433       /* And if everything else is equal, then sort based on which
434 	 coalesce pair was found first.  */
435       if (result == 0)
436 	result = (*pp2)->index - (*pp1)->index;
437     }
438 
439   return result;
440 }
441 
442 /* Iterate over CL using ITER, returning values in PAIR.  */
443 
444 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL)		\
445   FOR_EACH_HASH_TABLE_ELEMENT (*(CL)->list, (PAIR), coalesce_pair_p, (ITER))
446 
447 
448 /* Prepare CL for removal of preferred pairs.  When finished they are sorted
449    in order from most important coalesce to least important.  */
450 
451 static void
452 sort_coalesce_list (coalesce_list *cl, ssa_conflicts *conflicts, var_map map)
453 {
454   unsigned x, num;
455   coalesce_pair *p;
456   coalesce_iterator_type ppi;
457 
458   gcc_assert (cl->sorted == NULL);
459 
460   num = num_coalesce_pairs (cl);
461   cl->num_sorted = num;
462   if (num == 0)
463     return;
464 
465   /* Allocate a vector for the pair pointers.  */
466   cl->sorted = XNEWVEC (coalesce_pair *, num);
467 
468   /* Populate the vector with pointers to the pairs.  */
469   x = 0;
470   FOR_EACH_PARTITION_PAIR (p, ppi, cl)
471     cl->sorted[x++] = p;
472   gcc_assert (x == num);
473 
474   /* Already sorted.  */
475   if (num == 1)
476     return;
477 
478   /* We don't want to depend on qsort_r, so we have to stuff away
479      additional data into globals so it can be referenced in
480      compare_pairs.  */
481   conflicts_ = conflicts;
482   map_ = map;
483   qsort (cl->sorted, num, sizeof (coalesce_pair *), compare_pairs);
484   conflicts_ = NULL;
485   map_ = NULL;
486 }
487 
488 
489 /* Send debug info for coalesce list CL to file F.  */
490 
491 static void
492 dump_coalesce_list (FILE *f, coalesce_list *cl)
493 {
494   coalesce_pair *node;
495   coalesce_iterator_type ppi;
496 
497   int x;
498   tree var;
499 
500   if (cl->sorted == NULL)
501     {
502       fprintf (f, "Coalesce List:\n");
503       FOR_EACH_PARTITION_PAIR (node, ppi, cl)
504         {
505 	  tree var1 = ssa_name (node->first_element);
506 	  tree var2 = ssa_name (node->second_element);
507 	  print_generic_expr (f, var1, TDF_SLIM);
508 	  fprintf (f, " <-> ");
509 	  print_generic_expr (f, var2, TDF_SLIM);
510 	  fprintf (f, "  (%1d, %1d), ", node->cost, node->conflict_count);
511 	  fprintf (f, "\n");
512 	}
513     }
514   else
515     {
516       fprintf (f, "Sorted Coalesce list:\n");
517       for (x = cl->num_sorted - 1 ; x >=0; x--)
518         {
519 	  node = cl->sorted[x];
520 	  fprintf (f, "(%d, %d) ", node->cost, node->conflict_count);
521 	  var = ssa_name (node->first_element);
522 	  print_generic_expr (f, var, TDF_SLIM);
523 	  fprintf (f, " <-> ");
524 	  var = ssa_name (node->second_element);
525 	  print_generic_expr (f, var, TDF_SLIM);
526 	  fprintf (f, "\n");
527 	}
528     }
529 }
530 
531 
532 /* Return an empty new conflict graph for SIZE elements.  */
533 
534 static inline ssa_conflicts *
535 ssa_conflicts_new (unsigned size)
536 {
537   ssa_conflicts *ptr;
538 
539   ptr = XNEW (ssa_conflicts);
540   bitmap_obstack_initialize (&ptr->obstack);
541   ptr->conflicts.create (size);
542   ptr->conflicts.safe_grow_cleared (size);
543   return ptr;
544 }
545 
546 
547 /* Free storage for conflict graph PTR.  */
548 
549 static inline void
550 ssa_conflicts_delete (ssa_conflicts *ptr)
551 {
552   bitmap_obstack_release (&ptr->obstack);
553   ptr->conflicts.release ();
554   free (ptr);
555 }
556 
557 
558 /* Test if elements X and Y conflict in graph PTR.  */
559 
560 static inline bool
561 ssa_conflicts_test_p (ssa_conflicts *ptr, unsigned x, unsigned y)
562 {
563   bitmap bx = ptr->conflicts[x];
564   bitmap by = ptr->conflicts[y];
565 
566   gcc_checking_assert (x != y);
567 
568   if (bx)
569     /* Avoid the lookup if Y has no conflicts.  */
570     return by ? bitmap_bit_p (bx, y) : false;
571   else
572     return false;
573 }
574 
575 
576 /* Add a conflict with Y to the bitmap for X in graph PTR.  */
577 
578 static inline void
579 ssa_conflicts_add_one (ssa_conflicts *ptr, unsigned x, unsigned y)
580 {
581   bitmap bx = ptr->conflicts[x];
582   /* If there are no conflicts yet, allocate the bitmap and set bit.  */
583   if (! bx)
584     bx = ptr->conflicts[x] = BITMAP_ALLOC (&ptr->obstack);
585   bitmap_set_bit (bx, y);
586 }
587 
588 
589 /* Add conflicts between X and Y in graph PTR.  */
590 
591 static inline void
592 ssa_conflicts_add (ssa_conflicts *ptr, unsigned x, unsigned y)
593 {
594   gcc_checking_assert (x != y);
595   ssa_conflicts_add_one (ptr, x, y);
596   ssa_conflicts_add_one (ptr, y, x);
597 }
598 
599 
600 /* Merge all Y's conflict into X in graph PTR.  */
601 
602 static inline void
603 ssa_conflicts_merge (ssa_conflicts *ptr, unsigned x, unsigned y)
604 {
605   unsigned z;
606   bitmap_iterator bi;
607   bitmap bx = ptr->conflicts[x];
608   bitmap by = ptr->conflicts[y];
609 
610   gcc_checking_assert (x != y);
611   if (! by)
612     return;
613 
614   /* Add a conflict between X and every one Y has.  If the bitmap doesn't
615      exist, then it has already been coalesced, and we don't need to add a
616      conflict.  */
617   EXECUTE_IF_SET_IN_BITMAP (by, 0, z, bi)
618     {
619       bitmap bz = ptr->conflicts[z];
620       if (bz)
621 	bitmap_set_bit (bz, x);
622     }
623 
624   if (bx)
625     {
626       /* If X has conflicts, add Y's to X.  */
627       bitmap_ior_into (bx, by);
628       BITMAP_FREE (by);
629       ptr->conflicts[y] = NULL;
630     }
631   else
632     {
633       /* If X has no conflicts, simply use Y's.  */
634       ptr->conflicts[x] = by;
635       ptr->conflicts[y] = NULL;
636     }
637 }
638 
639 
640 /* Dump a conflicts graph.  */
641 
642 static void
643 ssa_conflicts_dump (FILE *file, ssa_conflicts *ptr)
644 {
645   unsigned x;
646   bitmap b;
647 
648   fprintf (file, "\nConflict graph:\n");
649 
650   FOR_EACH_VEC_ELT (ptr->conflicts, x, b)
651     if (b)
652       {
653 	fprintf (file, "%d: ", x);
654 	dump_bitmap (file, b);
655       }
656 }
657 
658 
659 /* This structure is used to efficiently record the current status of live
660    SSA_NAMES when building a conflict graph.
661    LIVE_BASE_VAR has a bit set for each base variable which has at least one
662    ssa version live.
663    LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
664    index, and is used to track what partitions of each base variable are
665    live.  This makes it easy to add conflicts between just live partitions
666    with the same base variable.
667    The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
668    marked as being live.  This delays clearing of these bitmaps until
669    they are actually needed again.  */
670 
671 struct live_track
672 {
673   bitmap_obstack obstack;	/* A place to allocate our bitmaps.  */
674   bitmap live_base_var;		/* Indicates if a basevar is live.  */
675   bitmap *live_base_partitions;	/* Live partitions for each basevar.  */
676   var_map map;			/* Var_map being used for partition mapping.  */
677 };
678 
679 
680 /* This routine will create a new live track structure based on the partitions
681    in MAP.  */
682 
683 static live_track *
684 new_live_track (var_map map)
685 {
686   live_track *ptr;
687   int lim, x;
688 
689   /* Make sure there is a partition view in place.  */
690   gcc_assert (map->partition_to_base_index != NULL);
691 
692   ptr = (live_track *) xmalloc (sizeof (live_track));
693   ptr->map = map;
694   lim = num_basevars (map);
695   bitmap_obstack_initialize (&ptr->obstack);
696   ptr->live_base_partitions = (bitmap *) xmalloc (sizeof (bitmap *) * lim);
697   ptr->live_base_var = BITMAP_ALLOC (&ptr->obstack);
698   for (x = 0; x < lim; x++)
699     ptr->live_base_partitions[x] = BITMAP_ALLOC (&ptr->obstack);
700   return ptr;
701 }
702 
703 
704 /* This routine will free the memory associated with PTR.  */
705 
706 static void
707 delete_live_track (live_track *ptr)
708 {
709   bitmap_obstack_release (&ptr->obstack);
710   free (ptr->live_base_partitions);
711   free (ptr);
712 }
713 
714 
715 /* This function will remove PARTITION from the live list in PTR.  */
716 
717 static inline void
718 live_track_remove_partition (live_track *ptr, int partition)
719 {
720   int root;
721 
722   root = basevar_index (ptr->map, partition);
723   bitmap_clear_bit (ptr->live_base_partitions[root], partition);
724   /* If the element list is empty, make the base variable not live either.  */
725   if (bitmap_empty_p (ptr->live_base_partitions[root]))
726     bitmap_clear_bit (ptr->live_base_var, root);
727 }
728 
729 
730 /* This function will adds PARTITION to the live list in PTR.  */
731 
732 static inline void
733 live_track_add_partition (live_track *ptr, int partition)
734 {
735   int root;
736 
737   root = basevar_index (ptr->map, partition);
738   /* If this base var wasn't live before, it is now.  Clear the element list
739      since it was delayed until needed.  */
740   if (bitmap_set_bit (ptr->live_base_var, root))
741     bitmap_clear (ptr->live_base_partitions[root]);
742   bitmap_set_bit (ptr->live_base_partitions[root], partition);
743 
744 }
745 
746 
747 /* Clear the live bit for VAR in PTR.  */
748 
749 static inline void
750 live_track_clear_var (live_track *ptr, tree var)
751 {
752   int p;
753 
754   p = var_to_partition (ptr->map, var);
755   if (p != NO_PARTITION)
756     live_track_remove_partition (ptr, p);
757 }
758 
759 
760 /* Return TRUE if VAR is live in PTR.  */
761 
762 static inline bool
763 live_track_live_p (live_track *ptr, tree var)
764 {
765   int p, root;
766 
767   p = var_to_partition (ptr->map, var);
768   if (p != NO_PARTITION)
769     {
770       root = basevar_index (ptr->map, p);
771       if (bitmap_bit_p (ptr->live_base_var, root))
772 	return bitmap_bit_p (ptr->live_base_partitions[root], p);
773     }
774   return false;
775 }
776 
777 
778 /* This routine will add USE to PTR.  USE will be marked as live in both the
779    ssa live map and the live bitmap for the root of USE.  */
780 
781 static inline void
782 live_track_process_use (live_track *ptr, tree use)
783 {
784   int p;
785 
786   p = var_to_partition (ptr->map, use);
787   if (p == NO_PARTITION)
788     return;
789 
790   /* Mark as live in the appropriate live list.  */
791   live_track_add_partition (ptr, p);
792 }
793 
794 
795 /* This routine will process a DEF in PTR.  DEF will be removed from the live
796    lists, and if there are any other live partitions with the same base
797    variable, conflicts will be added to GRAPH.  */
798 
799 static inline void
800 live_track_process_def (live_track *ptr, tree def, ssa_conflicts *graph)
801 {
802   int p, root;
803   bitmap b;
804   unsigned x;
805   bitmap_iterator bi;
806 
807   p = var_to_partition (ptr->map, def);
808   if (p == NO_PARTITION)
809     return;
810 
811   /* Clear the liveness bit.  */
812   live_track_remove_partition (ptr, p);
813 
814   /* If the bitmap isn't empty now, conflicts need to be added.  */
815   root = basevar_index (ptr->map, p);
816   if (bitmap_bit_p (ptr->live_base_var, root))
817     {
818       b = ptr->live_base_partitions[root];
819       EXECUTE_IF_SET_IN_BITMAP (b, 0, x, bi)
820         ssa_conflicts_add (graph, p, x);
821     }
822 }
823 
824 
825 /* Initialize PTR with the partitions set in INIT.  */
826 
827 static inline void
828 live_track_init (live_track *ptr, bitmap init)
829 {
830   unsigned p;
831   bitmap_iterator bi;
832 
833   /* Mark all live on exit partitions.  */
834   EXECUTE_IF_SET_IN_BITMAP (init, 0, p, bi)
835     live_track_add_partition (ptr, p);
836 }
837 
838 
839 /* This routine will clear all live partitions in PTR.   */
840 
841 static inline void
842 live_track_clear_base_vars (live_track *ptr)
843 {
844   /* Simply clear the live base list.  Anything marked as live in the element
845      lists will be cleared later if/when the base variable ever comes alive
846      again.  */
847   bitmap_clear (ptr->live_base_var);
848 }
849 
850 
851 /* Build a conflict graph based on LIVEINFO.  Any partitions which are in the
852    partition view of the var_map liveinfo is based on get entries in the
853    conflict graph.  Only conflicts between ssa_name partitions with the same
854    base variable are added.  */
855 
856 static ssa_conflicts *
857 build_ssa_conflict_graph (tree_live_info_p liveinfo)
858 {
859   ssa_conflicts *graph;
860   var_map map;
861   basic_block bb;
862   ssa_op_iter iter;
863   live_track *live;
864   basic_block entry;
865 
866   /* If inter-variable coalescing is enabled, we may attempt to
867      coalesce variables from different base variables, including
868      different parameters, so we have to make sure default defs live
869      at the entry block conflict with each other.  */
870   if (flag_tree_coalesce_vars)
871     entry = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
872   else
873     entry = NULL;
874 
875   map = live_var_map (liveinfo);
876   graph = ssa_conflicts_new (num_var_partitions (map));
877 
878   live = new_live_track (map);
879 
880   FOR_EACH_BB_FN (bb, cfun)
881     {
882       /* Start with live on exit temporaries.  */
883       live_track_init (live, live_on_exit (liveinfo, bb));
884 
885       for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);
886 	   gsi_prev (&gsi))
887         {
888 	  tree var;
889 	  gimple *stmt = gsi_stmt (gsi);
890 
891 	  /* A copy between 2 partitions does not introduce an interference
892 	     by itself.  If they did, you would never be able to coalesce
893 	     two things which are copied.  If the two variables really do
894 	     conflict, they will conflict elsewhere in the program.
895 
896 	     This is handled by simply removing the SRC of the copy from the
897 	     live list, and processing the stmt normally.  */
898 	  if (is_gimple_assign (stmt))
899 	    {
900 	      tree lhs = gimple_assign_lhs (stmt);
901 	      tree rhs1 = gimple_assign_rhs1 (stmt);
902 	      if (gimple_assign_copy_p (stmt)
903                   && TREE_CODE (lhs) == SSA_NAME
904                   && TREE_CODE (rhs1) == SSA_NAME)
905 		live_track_clear_var (live, rhs1);
906 	    }
907 	  else if (is_gimple_debug (stmt))
908 	    continue;
909 
910 	  /* For stmts with more than one SSA_NAME definition pretend all the
911 	     SSA_NAME outputs but the first one are live at this point, so
912 	     that conflicts are added in between all those even when they are
913 	     actually not really live after the asm, because expansion might
914 	     copy those into pseudos after the asm and if multiple outputs
915 	     share the same partition, it might overwrite those that should
916 	     be live.  E.g.
917 	     asm volatile (".." : "=r" (a) : "=r" (b) : "0" (a), "1" (a));
918 	     return a;
919 	     See PR70593.  */
920 	  bool first = true;
921 	  FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
922 	    if (first)
923 	      first = false;
924 	    else
925 	      live_track_process_use (live, var);
926 
927 	  FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
928 	    live_track_process_def (live, var, graph);
929 
930 	  FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
931 	    live_track_process_use (live, var);
932 	}
933 
934       /* If result of a PHI is unused, looping over the statements will not
935 	 record any conflicts since the def was never live.  Since the PHI node
936 	 is going to be translated out of SSA form, it will insert a copy.
937 	 There must be a conflict recorded between the result of the PHI and
938 	 any variables that are live.  Otherwise the out-of-ssa translation
939 	 may create incorrect code.  */
940       for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
941 	   gsi_next (&gsi))
942 	{
943 	  gphi *phi = gsi.phi ();
944 	  tree result = PHI_RESULT (phi);
945 	  if (live_track_live_p (live, result))
946 	    live_track_process_def (live, result, graph);
947 	}
948 
949       /* Pretend there are defs for params' default defs at the start
950 	 of the (post-)entry block.  This will prevent PARM_DECLs from
951 	 coalescing into the same partition.  Although RESULT_DECLs'
952 	 default defs don't have a useful initial value, we have to
953 	 prevent them from coalescing with PARM_DECLs' default defs
954 	 too, otherwise assign_parms would attempt to assign different
955 	 RTL to the same partition.  */
956       if (bb == entry)
957 	{
958 	  unsigned i;
959 	  tree var;
960 
961 	  FOR_EACH_SSA_NAME (i, var, cfun)
962 	    {
963 	      if (!SSA_NAME_IS_DEFAULT_DEF (var)
964 		  || !SSA_NAME_VAR (var)
965 		  || VAR_P (SSA_NAME_VAR (var)))
966 		continue;
967 
968 	      live_track_process_def (live, var, graph);
969 	      /* Process a use too, so that it remains live and
970 		 conflicts with other parms' default defs, even unused
971 		 ones.  */
972 	      live_track_process_use (live, var);
973 	    }
974 	}
975 
976      live_track_clear_base_vars (live);
977     }
978 
979   delete_live_track (live);
980   return graph;
981 }
982 
983 
984 /* Shortcut routine to print messages to file F of the form:
985    "STR1 EXPR1 STR2 EXPR2 STR3."  */
986 
987 static inline void
988 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
989 	     tree expr2, const char *str3)
990 {
991   fprintf (f, "%s", str1);
992   print_generic_expr (f, expr1, TDF_SLIM);
993   fprintf (f, "%s", str2);
994   print_generic_expr (f, expr2, TDF_SLIM);
995   fprintf (f, "%s", str3);
996 }
997 
998 
999 /* Print a failure to coalesce a MUST_COALESCE pair X and Y.  */
1000 
1001 static inline void
1002 fail_abnormal_edge_coalesce (int x, int y)
1003 {
1004   fprintf (stderr, "\nUnable to coalesce ssa_names %d and %d",x, y);
1005   fprintf (stderr, " which are marked as MUST COALESCE.\n");
1006   print_generic_expr (stderr, ssa_name (x), TDF_SLIM);
1007   fprintf (stderr, " and  ");
1008   print_generic_stmt (stderr, ssa_name (y), TDF_SLIM);
1009 
1010   internal_error ("SSA corruption");
1011 }
1012 
1013 /* Call CALLBACK for all PARM_DECLs and RESULT_DECLs for which
1014    assign_parms may ask for a default partition.  */
1015 
1016 static void
1017 for_all_parms (void (*callback)(tree var, void *arg), void *arg)
1018 {
1019   for (tree var = DECL_ARGUMENTS (current_function_decl); var;
1020        var = DECL_CHAIN (var))
1021     callback (var, arg);
1022   if (!VOID_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
1023     callback (DECL_RESULT (current_function_decl), arg);
1024   if (cfun->static_chain_decl)
1025     callback (cfun->static_chain_decl, arg);
1026 }
1027 
1028 /* Create a default def for VAR.  */
1029 
1030 static void
1031 create_default_def (tree var, void *arg ATTRIBUTE_UNUSED)
1032 {
1033   if (!is_gimple_reg (var))
1034     return;
1035 
1036   tree ssa = get_or_create_ssa_default_def (cfun, var);
1037   gcc_assert (ssa);
1038 }
1039 
1040 /* Register VAR's default def in MAP.  */
1041 
1042 static void
1043 register_default_def (tree var, void *arg ATTRIBUTE_UNUSED)
1044 {
1045   if (!is_gimple_reg (var))
1046     return;
1047 
1048   tree ssa = ssa_default_def (cfun, var);
1049   gcc_assert (ssa);
1050 }
1051 
1052 /* If VAR is an SSA_NAME associated with a PARM_DECL or a RESULT_DECL,
1053    and the DECL's default def is unused (i.e., it was introduced by
1054    create_default_def), mark VAR and the default def for
1055    coalescing.  */
1056 
1057 static void
1058 coalesce_with_default (tree var, coalesce_list *cl, bitmap used_in_copy)
1059 {
1060   if (SSA_NAME_IS_DEFAULT_DEF (var)
1061       || !SSA_NAME_VAR (var)
1062       || VAR_P (SSA_NAME_VAR (var)))
1063     return;
1064 
1065   tree ssa = ssa_default_def (cfun, SSA_NAME_VAR (var));
1066   if (!has_zero_uses (ssa))
1067     return;
1068 
1069   add_cost_one_coalesce (cl, SSA_NAME_VERSION (ssa), SSA_NAME_VERSION (var));
1070   bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1071   /* Default defs will have their used_in_copy bits set at the end of
1072      create_outofssa_var_map.  */
1073 }
1074 
1075 /* This function creates a var_map for the current function as well as creating
1076    a coalesce list for use later in the out of ssa process.  */
1077 
1078 static var_map
1079 create_outofssa_var_map (coalesce_list *cl, bitmap used_in_copy)
1080 {
1081   gimple_stmt_iterator gsi;
1082   basic_block bb;
1083   tree var;
1084   gimple *stmt;
1085   tree first;
1086   var_map map;
1087   int v1, v2, cost;
1088   unsigned i;
1089 
1090   for_all_parms (create_default_def, NULL);
1091 
1092   map = init_var_map (num_ssa_names);
1093 
1094   for_all_parms (register_default_def, NULL);
1095 
1096   FOR_EACH_BB_FN (bb, cfun)
1097     {
1098       tree arg;
1099 
1100       for (gphi_iterator gpi = gsi_start_phis (bb);
1101 	   !gsi_end_p (gpi);
1102 	   gsi_next (&gpi))
1103 	{
1104 	  gphi *phi = gpi.phi ();
1105 	  size_t i;
1106 	  int ver;
1107 	  tree res;
1108 	  bool saw_copy = false;
1109 
1110 	  res = gimple_phi_result (phi);
1111 	  ver = SSA_NAME_VERSION (res);
1112 
1113 	  /* Register ssa_names and coalesces between the args and the result
1114 	     of all PHI.  */
1115 	  for (i = 0; i < gimple_phi_num_args (phi); i++)
1116 	    {
1117 	      edge e = gimple_phi_arg_edge (phi, i);
1118 	      arg = PHI_ARG_DEF (phi, i);
1119 	      if (TREE_CODE (arg) != SSA_NAME)
1120 		continue;
1121 
1122 	      if (gimple_can_coalesce_p (arg, res)
1123 		  || (e->flags & EDGE_ABNORMAL))
1124 		{
1125 		  saw_copy = true;
1126 		  bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (arg));
1127 		  if ((e->flags & EDGE_ABNORMAL) == 0)
1128 		    {
1129 		      int cost = coalesce_cost_edge (e);
1130 		      if (cost == 1 && has_single_use (arg))
1131 			add_cost_one_coalesce (cl, ver, SSA_NAME_VERSION (arg));
1132 		      else
1133 			add_coalesce (cl, ver, SSA_NAME_VERSION (arg), cost);
1134 		    }
1135 		}
1136 	    }
1137 	  if (saw_copy)
1138 	    bitmap_set_bit (used_in_copy, ver);
1139 	}
1140 
1141       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1142         {
1143 	  stmt = gsi_stmt (gsi);
1144 
1145 	  if (is_gimple_debug (stmt))
1146 	    continue;
1147 
1148 	  /* Check for copy coalesces.  */
1149 	  switch (gimple_code (stmt))
1150 	    {
1151 	    case GIMPLE_ASSIGN:
1152 	      {
1153 		tree lhs = gimple_assign_lhs (stmt);
1154 		tree rhs1 = gimple_assign_rhs1 (stmt);
1155 		if (gimple_assign_ssa_name_copy_p (stmt)
1156 		    && gimple_can_coalesce_p (lhs, rhs1))
1157 		  {
1158 		    v1 = SSA_NAME_VERSION (lhs);
1159 		    v2 = SSA_NAME_VERSION (rhs1);
1160 		    cost = coalesce_cost_bb (bb);
1161 		    add_coalesce (cl, v1, v2, cost);
1162 		    bitmap_set_bit (used_in_copy, v1);
1163 		    bitmap_set_bit (used_in_copy, v2);
1164 		  }
1165 	      }
1166 	      break;
1167 
1168 	    case GIMPLE_RETURN:
1169 	      {
1170 		tree res = DECL_RESULT (current_function_decl);
1171 		if (VOID_TYPE_P (TREE_TYPE (res))
1172 		    || !is_gimple_reg (res))
1173 		  break;
1174 		tree rhs1 = gimple_return_retval (as_a <greturn *> (stmt));
1175 		if (!rhs1)
1176 		  break;
1177 		tree lhs = ssa_default_def (cfun, res);
1178 		gcc_assert (lhs);
1179 		if (TREE_CODE (rhs1) == SSA_NAME
1180 		    && gimple_can_coalesce_p (lhs, rhs1))
1181 		  {
1182 		    v1 = SSA_NAME_VERSION (lhs);
1183 		    v2 = SSA_NAME_VERSION (rhs1);
1184 		    cost = coalesce_cost_bb (bb);
1185 		    add_coalesce (cl, v1, v2, cost);
1186 		    bitmap_set_bit (used_in_copy, v1);
1187 		    bitmap_set_bit (used_in_copy, v2);
1188 		  }
1189 		break;
1190 	      }
1191 
1192 	    case GIMPLE_ASM:
1193 	      {
1194 		gasm *asm_stmt = as_a <gasm *> (stmt);
1195 		unsigned long noutputs, i;
1196 		unsigned long ninputs;
1197 		tree *outputs, link;
1198 		noutputs = gimple_asm_noutputs (asm_stmt);
1199 		ninputs = gimple_asm_ninputs (asm_stmt);
1200 		outputs = (tree *) alloca (noutputs * sizeof (tree));
1201 		for (i = 0; i < noutputs; ++i)
1202 		  {
1203 		    link = gimple_asm_output_op (asm_stmt, i);
1204 		    outputs[i] = TREE_VALUE (link);
1205 		  }
1206 
1207 		for (i = 0; i < ninputs; ++i)
1208 		  {
1209                     const char *constraint;
1210                     tree input;
1211 		    char *end;
1212 		    unsigned long match;
1213 
1214 		    link = gimple_asm_input_op (asm_stmt, i);
1215 		    constraint
1216 		      = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1217 		    input = TREE_VALUE (link);
1218 
1219 		    if (TREE_CODE (input) != SSA_NAME)
1220 		      continue;
1221 
1222 		    match = strtoul (constraint, &end, 10);
1223 		    if (match >= noutputs || end == constraint)
1224 		      continue;
1225 
1226 		    if (TREE_CODE (outputs[match]) != SSA_NAME)
1227 		      continue;
1228 
1229 		    v1 = SSA_NAME_VERSION (outputs[match]);
1230 		    v2 = SSA_NAME_VERSION (input);
1231 
1232 		    if (gimple_can_coalesce_p (outputs[match], input))
1233 		      {
1234 			cost = coalesce_cost (REG_BR_PROB_BASE,
1235 					      optimize_bb_for_size_p (bb));
1236 			add_coalesce (cl, v1, v2, cost);
1237 			bitmap_set_bit (used_in_copy, v1);
1238 			bitmap_set_bit (used_in_copy, v2);
1239 		      }
1240 		  }
1241 		break;
1242 	      }
1243 
1244 	    default:
1245 	      break;
1246 	    }
1247 	}
1248     }
1249 
1250   /* Now process result decls and live on entry variables for entry into
1251      the coalesce list.  */
1252   first = NULL_TREE;
1253   FOR_EACH_SSA_NAME (i, var, cfun)
1254     {
1255       if (!virtual_operand_p (var))
1256         {
1257 	  coalesce_with_default (var, cl, used_in_copy);
1258 
1259 	  /* Add coalesces between all the result decls.  */
1260 	  if (SSA_NAME_VAR (var)
1261 	      && TREE_CODE (SSA_NAME_VAR (var)) == RESULT_DECL)
1262 	    {
1263 	      bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1264 	      if (first == NULL_TREE)
1265 		first = var;
1266 	      else
1267 		{
1268 		  gcc_assert (gimple_can_coalesce_p (var, first));
1269 		  v1 = SSA_NAME_VERSION (first);
1270 		  v2 = SSA_NAME_VERSION (var);
1271 		  cost = coalesce_cost_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
1272 		  add_coalesce (cl, v1, v2, cost);
1273 		}
1274 	    }
1275 	  /* Mark any default_def variables as being in the coalesce list
1276 	     since they will have to be coalesced with the base variable.  If
1277 	     not marked as present, they won't be in the coalesce view. */
1278 	  if (SSA_NAME_IS_DEFAULT_DEF (var)
1279 	      && (!has_zero_uses (var)
1280 		  || (SSA_NAME_VAR (var)
1281 		      && !VAR_P (SSA_NAME_VAR (var)))))
1282 	    bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1283 	}
1284     }
1285 
1286   return map;
1287 }
1288 
1289 
1290 /* Attempt to coalesce ssa versions X and Y together using the partition
1291    mapping in MAP and checking conflicts in GRAPH.  Output any debug info to
1292    DEBUG, if it is nun-NULL.  */
1293 
1294 static inline bool
1295 attempt_coalesce (var_map map, ssa_conflicts *graph, int x, int y,
1296 		  FILE *debug)
1297 {
1298   int z;
1299   tree var1, var2;
1300   int p1, p2;
1301 
1302   p1 = var_to_partition (map, ssa_name (x));
1303   p2 = var_to_partition (map, ssa_name (y));
1304 
1305   if (debug)
1306     {
1307       fprintf (debug, "(%d)", x);
1308       print_generic_expr (debug, partition_to_var (map, p1), TDF_SLIM);
1309       fprintf (debug, " & (%d)", y);
1310       print_generic_expr (debug, partition_to_var (map, p2), TDF_SLIM);
1311     }
1312 
1313   if (p1 == p2)
1314     {
1315       if (debug)
1316 	fprintf (debug, ": Already Coalesced.\n");
1317       return true;
1318     }
1319 
1320   if (debug)
1321     fprintf (debug, " [map: %d, %d] ", p1, p2);
1322 
1323 
1324   if (!ssa_conflicts_test_p (graph, p1, p2))
1325     {
1326       var1 = partition_to_var (map, p1);
1327       var2 = partition_to_var (map, p2);
1328 
1329       z = var_union (map, var1, var2);
1330       if (z == NO_PARTITION)
1331 	{
1332 	  if (debug)
1333 	    fprintf (debug, ": Unable to perform partition union.\n");
1334 	  return false;
1335 	}
1336 
1337       /* z is the new combined partition.  Remove the other partition from
1338 	 the list, and merge the conflicts.  */
1339       if (z == p1)
1340 	ssa_conflicts_merge (graph, p1, p2);
1341       else
1342 	ssa_conflicts_merge (graph, p2, p1);
1343 
1344       if (debug)
1345 	fprintf (debug, ": Success -> %d\n", z);
1346 
1347       return true;
1348     }
1349 
1350   if (debug)
1351     fprintf (debug, ": Fail due to conflict\n");
1352 
1353   return false;
1354 }
1355 
1356 
1357 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1358    GRAPH.  Debug output is sent to DEBUG if it is non-NULL.  */
1359 
1360 static void
1361 coalesce_partitions (var_map map, ssa_conflicts *graph, coalesce_list *cl,
1362 		     FILE *debug)
1363 {
1364   int x = 0, y = 0;
1365   tree var1, var2;
1366   int cost;
1367   basic_block bb;
1368   edge e;
1369   edge_iterator ei;
1370 
1371   /* First, coalesce all the copies across abnormal edges.  These are not placed
1372      in the coalesce list because they do not need to be sorted, and simply
1373      consume extra memory/compilation time in large programs.  */
1374 
1375   FOR_EACH_BB_FN (bb, cfun)
1376     {
1377       FOR_EACH_EDGE (e, ei, bb->preds)
1378 	if (e->flags & EDGE_ABNORMAL)
1379 	  {
1380 	    gphi_iterator gsi;
1381 	    for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1382 		 gsi_next (&gsi))
1383 	      {
1384 		gphi *phi = gsi.phi ();
1385 		tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1386 		if (SSA_NAME_IS_DEFAULT_DEF (arg)
1387 		    && (!SSA_NAME_VAR (arg)
1388 			|| TREE_CODE (SSA_NAME_VAR (arg)) != PARM_DECL))
1389 		  continue;
1390 
1391 		tree res = PHI_RESULT (phi);
1392 		int v1 = SSA_NAME_VERSION (res);
1393 		int v2 = SSA_NAME_VERSION (arg);
1394 
1395 		if (debug)
1396 		  fprintf (debug, "Abnormal coalesce: ");
1397 
1398 		if (!attempt_coalesce (map, graph, v1, v2, debug))
1399 		  fail_abnormal_edge_coalesce (v1, v2);
1400 	      }
1401 	  }
1402     }
1403 
1404   /* Now process the items in the coalesce list.  */
1405 
1406   while ((cost = pop_best_coalesce (cl, &x, &y)) != NO_BEST_COALESCE)
1407     {
1408       var1 = ssa_name (x);
1409       var2 = ssa_name (y);
1410 
1411       /* Assert the coalesces have the same base variable.  */
1412       gcc_assert (gimple_can_coalesce_p (var1, var2));
1413 
1414       if (debug)
1415 	fprintf (debug, "Coalesce list: ");
1416       attempt_coalesce (map, graph, x, y, debug);
1417     }
1418 }
1419 
1420 
1421 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR.  */
1422 
1423 struct ssa_name_var_hash : nofree_ptr_hash <tree_node>
1424 {
1425   static inline hashval_t hash (const tree_node *);
1426   static inline int equal (const tree_node *, const tree_node *);
1427 };
1428 
1429 inline hashval_t
1430 ssa_name_var_hash::hash (const_tree n)
1431 {
1432   return DECL_UID (SSA_NAME_VAR (n));
1433 }
1434 
1435 inline int
1436 ssa_name_var_hash::equal (const tree_node *n1, const tree_node *n2)
1437 {
1438   return SSA_NAME_VAR (n1) == SSA_NAME_VAR (n2);
1439 }
1440 
1441 
1442 /* Output partition map MAP with coalescing plan PART to file F.  */
1443 
1444 void
1445 dump_part_var_map (FILE *f, partition part, var_map map)
1446 {
1447   int t;
1448   unsigned x, y;
1449   int p;
1450 
1451   fprintf (f, "\nCoalescible Partition map \n\n");
1452 
1453   for (x = 0; x < map->num_partitions; x++)
1454     {
1455       if (map->view_to_partition != NULL)
1456 	p = map->view_to_partition[x];
1457       else
1458 	p = x;
1459 
1460       if (ssa_name (p) == NULL_TREE
1461 	  || virtual_operand_p (ssa_name (p)))
1462         continue;
1463 
1464       t = 0;
1465       for (y = 1; y < num_ssa_names; y++)
1466         {
1467 	  tree var = version_to_var (map, y);
1468 	  if (!var)
1469 	    continue;
1470 	  int q = var_to_partition (map, var);
1471 	  p = partition_find (part, q);
1472 	  gcc_assert (map->partition_to_base_index[q]
1473 		      == map->partition_to_base_index[p]);
1474 
1475 	  if (p == (int)x)
1476 	    {
1477 	      if (t++ == 0)
1478 	        {
1479 		  fprintf (f, "Partition %d, base %d (", x,
1480 			   map->partition_to_base_index[q]);
1481 		  print_generic_expr (f, partition_to_var (map, q), TDF_SLIM);
1482 		  fprintf (f, " - ");
1483 		}
1484 	      fprintf (f, "%d ", y);
1485 	    }
1486 	}
1487       if (t != 0)
1488 	fprintf (f, ")\n");
1489     }
1490   fprintf (f, "\n");
1491 }
1492 
1493 /* Given SSA_NAMEs NAME1 and NAME2, return true if they are candidates for
1494    coalescing together, false otherwise.
1495 
1496    This must stay consistent with compute_samebase_partition_bases and
1497    compute_optimized_partition_bases.  */
1498 
1499 bool
1500 gimple_can_coalesce_p (tree name1, tree name2)
1501 {
1502   /* First check the SSA_NAME's associated DECL.  Without
1503      optimization, we only want to coalesce if they have the same DECL
1504      or both have no associated DECL.  */
1505   tree var1 = SSA_NAME_VAR (name1);
1506   tree var2 = SSA_NAME_VAR (name2);
1507   var1 = (var1 && (!VAR_P (var1) || !DECL_IGNORED_P (var1))) ? var1 : NULL_TREE;
1508   var2 = (var2 && (!VAR_P (var2) || !DECL_IGNORED_P (var2))) ? var2 : NULL_TREE;
1509   if (var1 != var2 && !flag_tree_coalesce_vars)
1510     return false;
1511 
1512   /* Now check the types.  If the types are the same, then we should
1513      try to coalesce V1 and V2.  */
1514   tree t1 = TREE_TYPE (name1);
1515   tree t2 = TREE_TYPE (name2);
1516   if (t1 == t2)
1517     {
1518     check_modes:
1519       /* If the base variables are the same, we're good: none of the
1520 	 other tests below could possibly fail.  */
1521       var1 = SSA_NAME_VAR (name1);
1522       var2 = SSA_NAME_VAR (name2);
1523       if (var1 == var2)
1524 	return true;
1525 
1526       /* We don't want to coalesce two SSA names if one of the base
1527 	 variables is supposed to be a register while the other is
1528 	 supposed to be on the stack.  Anonymous SSA names most often
1529 	 take registers, but when not optimizing, user variables
1530 	 should go on the stack, so coalescing them with the anonymous
1531 	 variable as the partition leader would end up assigning the
1532 	 user variable to a register.  Don't do that!  */
1533       bool reg1 = use_register_for_decl (name1);
1534       bool reg2 = use_register_for_decl (name2);
1535       if (reg1 != reg2)
1536 	return false;
1537 
1538       /* Check that the promoted modes and unsignedness are the same.
1539 	 We don't want to coalesce if the promoted modes would be
1540 	 different, or if they would sign-extend differently.  Only
1541 	 PARM_DECLs and RESULT_DECLs have different promotion rules,
1542 	 so skip the test if both are variables, or both are anonymous
1543 	 SSA_NAMEs.  */
1544       int unsigned1, unsigned2;
1545       return ((!var1 || VAR_P (var1)) && (!var2 || VAR_P (var2)))
1546 	|| ((promote_ssa_mode (name1, &unsigned1)
1547 	     == promote_ssa_mode (name2, &unsigned2))
1548 	    && unsigned1 == unsigned2);
1549     }
1550 
1551   /* If alignment requirements are different, we can't coalesce.  */
1552   if (MINIMUM_ALIGNMENT (t1,
1553 			 var1 ? DECL_MODE (var1) : TYPE_MODE (t1),
1554 			 var1 ? LOCAL_DECL_ALIGNMENT (var1) : TYPE_ALIGN (t1))
1555       != MINIMUM_ALIGNMENT (t2,
1556 			    var2 ? DECL_MODE (var2) : TYPE_MODE (t2),
1557 			    var2 ? LOCAL_DECL_ALIGNMENT (var2) : TYPE_ALIGN (t2)))
1558     return false;
1559 
1560   /* If the types are not the same, see whether they are compatible.  This
1561      (for example) allows coalescing when the types are fundamentally the
1562      same, but just have different names.
1563 
1564      In the non-optimized case, we must first test TYPE_CANONICAL because
1565      we use it to compute the partition_to_base_index of the map.  */
1566   if (flag_tree_coalesce_vars)
1567     {
1568       if (types_compatible_p (t1, t2))
1569 	goto check_modes;
1570     }
1571   else
1572     {
1573       if (TYPE_CANONICAL (t1)
1574 	  && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)
1575 	  && types_compatible_p (t1, t2))
1576 	goto check_modes;
1577     }
1578 
1579   return false;
1580 }
1581 
1582 /* Fill in MAP's partition_to_base_index, with one index for each
1583    partition of SSA names USED_IN_COPIES and related by CL coalesce
1584    possibilities.  This must match gimple_can_coalesce_p in the
1585    optimized case.  */
1586 
1587 static void
1588 compute_optimized_partition_bases (var_map map, bitmap used_in_copies,
1589 				   coalesce_list *cl)
1590 {
1591   int parts = num_var_partitions (map);
1592   partition tentative = partition_new (parts);
1593 
1594   /* Partition the SSA versions so that, for each coalescible
1595      pair, both of its members are in the same partition in
1596      TENTATIVE.  */
1597   gcc_assert (!cl->sorted);
1598   coalesce_pair *node;
1599   coalesce_iterator_type ppi;
1600   FOR_EACH_PARTITION_PAIR (node, ppi, cl)
1601     {
1602       tree v1 = ssa_name (node->first_element);
1603       int p1 = partition_find (tentative, var_to_partition (map, v1));
1604       tree v2 = ssa_name (node->second_element);
1605       int p2 = partition_find (tentative, var_to_partition (map, v2));
1606 
1607       if (p1 == p2)
1608 	continue;
1609 
1610       partition_union (tentative, p1, p2);
1611     }
1612 
1613   /* We have to deal with cost one pairs too.  */
1614   for (cost_one_pair *co = cl->cost_one_list; co; co = co->next)
1615     {
1616       tree v1 = ssa_name (co->first_element);
1617       int p1 = partition_find (tentative, var_to_partition (map, v1));
1618       tree v2 = ssa_name (co->second_element);
1619       int p2 = partition_find (tentative, var_to_partition (map, v2));
1620 
1621       if (p1 == p2)
1622 	continue;
1623 
1624       partition_union (tentative, p1, p2);
1625     }
1626 
1627   /* And also with abnormal edges.  */
1628   basic_block bb;
1629   edge e;
1630   edge_iterator ei;
1631   FOR_EACH_BB_FN (bb, cfun)
1632     {
1633       FOR_EACH_EDGE (e, ei, bb->preds)
1634 	if (e->flags & EDGE_ABNORMAL)
1635 	  {
1636 	    gphi_iterator gsi;
1637 	    for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1638 		 gsi_next (&gsi))
1639 	      {
1640 		gphi *phi = gsi.phi ();
1641 		tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1642 		if (SSA_NAME_IS_DEFAULT_DEF (arg)
1643 		    && (!SSA_NAME_VAR (arg)
1644 			|| TREE_CODE (SSA_NAME_VAR (arg)) != PARM_DECL))
1645 		  continue;
1646 
1647 		tree res = PHI_RESULT (phi);
1648 
1649 		int p1 = partition_find (tentative, var_to_partition (map, res));
1650 		int p2 = partition_find (tentative, var_to_partition (map, arg));
1651 
1652 		if (p1 == p2)
1653 		  continue;
1654 
1655 		partition_union (tentative, p1, p2);
1656 	      }
1657 	  }
1658     }
1659 
1660   map->partition_to_base_index = XCNEWVEC (int, parts);
1661   auto_vec<unsigned int> index_map (parts);
1662   if (parts)
1663     index_map.quick_grow (parts);
1664 
1665   const unsigned no_part = -1;
1666   unsigned count = parts;
1667   while (count)
1668     index_map[--count] = no_part;
1669 
1670   /* Initialize MAP's mapping from partition to base index, using
1671      as base indices an enumeration of the TENTATIVE partitions in
1672      which each SSA version ended up, so that we compute conflicts
1673      between all SSA versions that ended up in the same potential
1674      coalesce partition.  */
1675   bitmap_iterator bi;
1676   unsigned i;
1677   EXECUTE_IF_SET_IN_BITMAP (used_in_copies, 0, i, bi)
1678     {
1679       int pidx = var_to_partition (map, ssa_name (i));
1680       int base = partition_find (tentative, pidx);
1681       if (index_map[base] != no_part)
1682 	continue;
1683       index_map[base] = count++;
1684     }
1685 
1686   map->num_basevars = count;
1687 
1688   EXECUTE_IF_SET_IN_BITMAP (used_in_copies, 0, i, bi)
1689     {
1690       int pidx = var_to_partition (map, ssa_name (i));
1691       int base = partition_find (tentative, pidx);
1692       gcc_assert (index_map[base] < count);
1693       map->partition_to_base_index[pidx] = index_map[base];
1694     }
1695 
1696   if (dump_file && (dump_flags & TDF_DETAILS))
1697     dump_part_var_map (dump_file, tentative, map);
1698 
1699   partition_delete (tentative);
1700 }
1701 
1702 /* Hashtable helpers.  */
1703 
1704 struct tree_int_map_hasher : nofree_ptr_hash <tree_int_map>
1705 {
1706   static inline hashval_t hash (const tree_int_map *);
1707   static inline bool equal (const tree_int_map *, const tree_int_map *);
1708 };
1709 
1710 inline hashval_t
1711 tree_int_map_hasher::hash (const tree_int_map *v)
1712 {
1713   return tree_map_base_hash (v);
1714 }
1715 
1716 inline bool
1717 tree_int_map_hasher::equal (const tree_int_map *v, const tree_int_map *c)
1718 {
1719   return tree_int_map_eq (v, c);
1720 }
1721 
1722 /* This routine will initialize the basevar fields of MAP with base
1723    names.  Partitions will share the same base if they have the same
1724    SSA_NAME_VAR, or, being anonymous variables, the same type.  This
1725    must match gimple_can_coalesce_p in the non-optimized case.  */
1726 
1727 static void
1728 compute_samebase_partition_bases (var_map map)
1729 {
1730   int x, num_part;
1731   tree var;
1732   struct tree_int_map *m, *mapstorage;
1733 
1734   num_part = num_var_partitions (map);
1735   hash_table<tree_int_map_hasher> tree_to_index (num_part);
1736   /* We can have at most num_part entries in the hash tables, so it's
1737      enough to allocate so many map elements once, saving some malloc
1738      calls.  */
1739   mapstorage = m = XNEWVEC (struct tree_int_map, num_part);
1740 
1741   /* If a base table already exists, clear it, otherwise create it.  */
1742   free (map->partition_to_base_index);
1743   map->partition_to_base_index = (int *) xmalloc (sizeof (int) * num_part);
1744 
1745   /* Build the base variable list, and point partitions at their bases.  */
1746   for (x = 0; x < num_part; x++)
1747     {
1748       struct tree_int_map **slot;
1749       unsigned baseindex;
1750       var = partition_to_var (map, x);
1751       if (SSA_NAME_VAR (var)
1752 	  && (!VAR_P (SSA_NAME_VAR (var))
1753 	      || !DECL_IGNORED_P (SSA_NAME_VAR (var))))
1754 	m->base.from = SSA_NAME_VAR (var);
1755       else
1756 	/* This restricts what anonymous SSA names we can coalesce
1757 	   as it restricts the sets we compute conflicts for.
1758 	   Using TREE_TYPE to generate sets is the easiest as
1759 	   type equivalency also holds for SSA names with the same
1760 	   underlying decl.
1761 
1762 	   Check gimple_can_coalesce_p when changing this code.  */
1763 	m->base.from = (TYPE_CANONICAL (TREE_TYPE (var))
1764 			? TYPE_CANONICAL (TREE_TYPE (var))
1765 			: TREE_TYPE (var));
1766       /* If base variable hasn't been seen, set it up.  */
1767       slot = tree_to_index.find_slot (m, INSERT);
1768       if (!*slot)
1769 	{
1770 	  baseindex = m - mapstorage;
1771 	  m->to = baseindex;
1772 	  *slot = m;
1773 	  m++;
1774 	}
1775       else
1776 	baseindex = (*slot)->to;
1777       map->partition_to_base_index[x] = baseindex;
1778     }
1779 
1780   map->num_basevars = m - mapstorage;
1781 
1782   free (mapstorage);
1783 }
1784 
1785 /* Reduce the number of copies by coalescing variables in the function.  Return
1786    a partition map with the resulting coalesces.  */
1787 
1788 extern var_map
1789 coalesce_ssa_name (void)
1790 {
1791   tree_live_info_p liveinfo;
1792   ssa_conflicts *graph;
1793   coalesce_list *cl;
1794   bitmap used_in_copies = BITMAP_ALLOC (NULL);
1795   var_map map;
1796   unsigned int i;
1797   tree a;
1798 
1799   cl = create_coalesce_list ();
1800   map = create_outofssa_var_map (cl, used_in_copies);
1801 
1802   /* If this optimization is disabled, we need to coalesce all the
1803      names originating from the same SSA_NAME_VAR so debug info
1804      remains undisturbed.  */
1805   if (!flag_tree_coalesce_vars)
1806     {
1807       hash_table<ssa_name_var_hash> ssa_name_hash (10);
1808 
1809       FOR_EACH_SSA_NAME (i, a, cfun)
1810 	{
1811 	  if (SSA_NAME_VAR (a)
1812 	      && !DECL_IGNORED_P (SSA_NAME_VAR (a))
1813 	      && (!has_zero_uses (a) || !SSA_NAME_IS_DEFAULT_DEF (a)
1814 		  || !VAR_P (SSA_NAME_VAR (a))))
1815 	    {
1816 	      tree *slot = ssa_name_hash.find_slot (a, INSERT);
1817 
1818 	      if (!*slot)
1819 		*slot = a;
1820 	      else
1821 		{
1822 		  /* If the variable is a PARM_DECL or a RESULT_DECL, we
1823 		     _require_ that all the names originating from it be
1824 		     coalesced, because there must be a single partition
1825 		     containing all the names so that it can be assigned
1826 		     the canonical RTL location of the DECL safely.
1827 		     If in_lto_p, a function could have been compiled
1828 		     originally with optimizations and only the link
1829 		     performed at -O0, so we can't actually require it.  */
1830 		  const int cost
1831 		    = (TREE_CODE (SSA_NAME_VAR (a)) == VAR_DECL || in_lto_p)
1832 		      ? MUST_COALESCE_COST - 1 : MUST_COALESCE_COST;
1833 		  add_coalesce (cl, SSA_NAME_VERSION (a),
1834 				SSA_NAME_VERSION (*slot), cost);
1835 		  bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (a));
1836 		  bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (*slot));
1837 		}
1838 	    }
1839 	}
1840     }
1841   if (dump_file && (dump_flags & TDF_DETAILS))
1842     dump_var_map (dump_file, map);
1843 
1844   partition_view_bitmap (map, used_in_copies);
1845 
1846   if (flag_tree_coalesce_vars)
1847     compute_optimized_partition_bases (map, used_in_copies, cl);
1848   else
1849     compute_samebase_partition_bases (map);
1850 
1851   BITMAP_FREE (used_in_copies);
1852 
1853   if (num_var_partitions (map) < 1)
1854     {
1855       delete_coalesce_list (cl);
1856       return map;
1857     }
1858 
1859   if (dump_file && (dump_flags & TDF_DETAILS))
1860     dump_var_map (dump_file, map);
1861 
1862   liveinfo = calculate_live_ranges (map, false);
1863 
1864   if (dump_file && (dump_flags & TDF_DETAILS))
1865     dump_live_info (dump_file, liveinfo, LIVEDUMP_ENTRY);
1866 
1867   /* Build a conflict graph.  */
1868   graph = build_ssa_conflict_graph (liveinfo);
1869   delete_tree_live_info (liveinfo);
1870   if (dump_file && (dump_flags & TDF_DETAILS))
1871     ssa_conflicts_dump (dump_file, graph);
1872 
1873   sort_coalesce_list (cl, graph, map);
1874 
1875   if (dump_file && (dump_flags & TDF_DETAILS))
1876     {
1877       fprintf (dump_file, "\nAfter sorting:\n");
1878       dump_coalesce_list (dump_file, cl);
1879     }
1880 
1881   /* First, coalesce all live on entry variables to their base variable.
1882      This will ensure the first use is coming from the correct location.  */
1883 
1884   if (dump_file && (dump_flags & TDF_DETAILS))
1885     dump_var_map (dump_file, map);
1886 
1887   /* Now coalesce everything in the list.  */
1888   coalesce_partitions (map, graph, cl,
1889 		       ((dump_flags & TDF_DETAILS) ? dump_file : NULL));
1890 
1891   delete_coalesce_list (cl);
1892   ssa_conflicts_delete (graph);
1893 
1894   return map;
1895 }
1896 
1897 /* We need to pass two arguments to set_parm_default_def_partition,
1898    but for_all_parms only supports one.  Use a pair.  */
1899 
1900 typedef std::pair<var_map, bitmap> parm_default_def_partition_arg;
1901 
1902 /* Set in ARG's PARTS bitmap the bit corresponding to the partition in
1903    ARG's MAP containing VAR's default def.  */
1904 
1905 static void
1906 set_parm_default_def_partition (tree var, void *arg_)
1907 {
1908   parm_default_def_partition_arg *arg = (parm_default_def_partition_arg *)arg_;
1909   var_map map = arg->first;
1910   bitmap parts = arg->second;
1911 
1912   if (!is_gimple_reg (var))
1913     return;
1914 
1915   tree ssa = ssa_default_def (cfun, var);
1916   gcc_assert (ssa);
1917 
1918   int version = var_to_partition (map, ssa);
1919   gcc_assert (version != NO_PARTITION);
1920 
1921   bool changed = bitmap_set_bit (parts, version);
1922   gcc_assert (changed);
1923 }
1924 
1925 /* Allocate and return a bitmap that has a bit set for each partition
1926    that contains a default def for a parameter.  */
1927 
1928 extern bitmap
1929 get_parm_default_def_partitions (var_map map)
1930 {
1931   bitmap parm_default_def_parts = BITMAP_ALLOC (NULL);
1932 
1933   parm_default_def_partition_arg
1934     arg = std::make_pair (map, parm_default_def_parts);
1935 
1936   for_all_parms (set_parm_default_def_partition, &arg);
1937 
1938   return parm_default_def_parts;
1939 }
1940