]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-coalesce.c
tree-ssa.h: New.
[thirdparty/gcc.git] / gcc / tree-ssa-coalesce.c
CommitLineData
7290d709 1/* Coalesce SSA_NAMES together for the out-of-ssa pass.
d1e082c2 2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
7290d709
AM
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9dcd6f09 9the Free Software Foundation; either version 3, or (at your option)
7290d709
AM
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
7290d709
AM
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "tm.h"
25#include "tree.h"
26#include "flags.h"
cf835838 27#include "tree-pretty-print.h"
7290d709 28#include "bitmap.h"
7ee2468b 29#include "dumpfile.h"
7a300452 30#include "tree-ssa.h"
0823efed 31#include "hash-table.h"
7290d709 32#include "tree-ssa-live.h"
718f9c0f 33#include "diagnostic-core.h"
7290d709
AM
34
35
36/* This set of routines implements a coalesce_list. This is an object which
37 is used to track pairs of ssa_names which are desirable to coalesce
b8698a0f
L
38 together to avoid copies. Costs are associated with each pair, and when
39 all desired information has been collected, the object can be used to
7290d709
AM
40 order the pairs for processing. */
41
42/* This structure defines a pair entry. */
43
44typedef struct coalesce_pair
45{
46 int first_element;
47 int second_element;
48 int cost;
49} * coalesce_pair_p;
741ac903 50typedef const struct coalesce_pair *const_coalesce_pair_p;
7290d709 51
bf190e8d
LC
52/* Coalesce pair hashtable helpers. */
53
54struct coalesce_pair_hasher : typed_noop_remove <coalesce_pair>
55{
56 typedef coalesce_pair value_type;
57 typedef coalesce_pair compare_type;
58 static inline hashval_t hash (const value_type *);
59 static inline bool equal (const value_type *, const compare_type *);
60};
61
62/* Hash function for coalesce list. Calculate hash for PAIR. */
63
64inline hashval_t
65coalesce_pair_hasher::hash (const value_type *pair)
66{
67 hashval_t a = (hashval_t)(pair->first_element);
68 hashval_t b = (hashval_t)(pair->second_element);
69
70 return b * (b - 1) / 2 + a;
71}
72
73/* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
74 returning TRUE if the two pairs are equivalent. */
75
76inline bool
77coalesce_pair_hasher::equal (const value_type *p1, const compare_type *p2)
78{
79 return (p1->first_element == p2->first_element
80 && p1->second_element == p2->second_element);
81}
82
83typedef hash_table <coalesce_pair_hasher> coalesce_table_type;
84typedef coalesce_table_type::iterator coalesce_iterator_type;
85
86
7290d709
AM
87typedef struct cost_one_pair_d
88{
89 int first_element;
90 int second_element;
91 struct cost_one_pair_d *next;
92} * cost_one_pair_p;
93
94/* This structure maintains the list of coalesce pairs. */
95
b8698a0f 96typedef struct coalesce_list_d
7290d709 97{
bf190e8d 98 coalesce_table_type list; /* Hash table. */
7290d709
AM
99 coalesce_pair_p *sorted; /* List when sorted. */
100 int num_sorted; /* Number in the sorted list. */
101 cost_one_pair_p cost_one_list;/* Single use coalesces with cost 1. */
102} *coalesce_list_p;
103
104#define NO_BEST_COALESCE -1
105#define MUST_COALESCE_COST INT_MAX
106
107
048bf48b 108/* Return cost of execution of copy instruction with FREQUENCY. */
7290d709
AM
109
110static inline int
048bf48b 111coalesce_cost (int frequency, bool optimize_for_size)
7290d709
AM
112{
113 /* Base costs on BB frequencies bounded by 1. */
114 int cost = frequency;
115
116 if (!cost)
117 cost = 1;
118
efd8f750 119 if (optimize_for_size)
7290d709 120 cost = 1;
7290d709 121
7290d709
AM
122 return cost;
123}
124
125
126/* Return the cost of executing a copy instruction in basic block BB. */
127
b8698a0f 128static inline int
7290d709
AM
129coalesce_cost_bb (basic_block bb)
130{
048bf48b 131 return coalesce_cost (bb->frequency, optimize_bb_for_size_p (bb));
7290d709
AM
132}
133
134
135/* Return the cost of executing a copy instruction on edge E. */
136
b8698a0f 137static inline int
7290d709
AM
138coalesce_cost_edge (edge e)
139{
048bf48b
JH
140 int mult = 1;
141
142 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
143 if (EDGE_CRITICAL_P (e))
144 mult = 2;
7290d709
AM
145 if (e->flags & EDGE_ABNORMAL)
146 return MUST_COALESCE_COST;
048bf48b
JH
147 if (e->flags & EDGE_EH)
148 {
149 edge e2;
150 edge_iterator ei;
151 FOR_EACH_EDGE (e2, ei, e->dest->preds)
152 if (e2 != e)
153 {
154 /* Putting code on EH edge that leads to BB
155 with multiple predecestors imply splitting of
156 edge too. */
157 if (mult < 2)
158 mult = 2;
159 /* If there are multiple EH predecestors, we
160 also copy EH regions and produce separate
161 landing pad. This is expensive. */
162 if (e2->flags & EDGE_EH)
163 {
164 mult = 5;
165 break;
166 }
167 }
168 }
7290d709 169
b8698a0f 170 return coalesce_cost (EDGE_FREQUENCY (e),
048bf48b 171 optimize_edge_for_size_p (e)) * mult;
7290d709
AM
172}
173
174
b8698a0f 175/* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
7290d709
AM
176 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
177 NO_BEST_COALESCE is returned if there aren't any. */
178
179static inline int
180pop_cost_one_pair (coalesce_list_p cl, int *p1, int *p2)
181{
182 cost_one_pair_p ptr;
183
184 ptr = cl->cost_one_list;
185 if (!ptr)
186 return NO_BEST_COALESCE;
187
188 *p1 = ptr->first_element;
189 *p2 = ptr->second_element;
190 cl->cost_one_list = ptr->next;
191
192 free (ptr);
193
194 return 1;
195}
196
b8698a0f 197/* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
7290d709
AM
198 2 elements via P1 and P2. Their calculated cost is returned by the function.
199 NO_BEST_COALESCE is returned if the coalesce list is empty. */
200
201static inline int
202pop_best_coalesce (coalesce_list_p cl, int *p1, int *p2)
203{
204 coalesce_pair_p node;
205 int ret;
206
207 if (cl->sorted == NULL)
208 return pop_cost_one_pair (cl, p1, p2);
209
210 if (cl->num_sorted == 0)
211 return pop_cost_one_pair (cl, p1, p2);
212
213 node = cl->sorted[--(cl->num_sorted)];
214 *p1 = node->first_element;
215 *p2 = node->second_element;
216 ret = node->cost;
217 free (node);
218
219 return ret;
220}
221
222
7290d709
AM
223/* Create a new empty coalesce list object and return it. */
224
b8698a0f 225static inline coalesce_list_p
7290d709
AM
226create_coalesce_list (void)
227{
228 coalesce_list_p list;
229 unsigned size = num_ssa_names * 3;
230
b8698a0f 231 if (size < 40)
7290d709
AM
232 size = 40;
233
234 list = (coalesce_list_p) xmalloc (sizeof (struct coalesce_list_d));
bf190e8d 235 list->list.create (size);
7290d709
AM
236 list->sorted = NULL;
237 list->num_sorted = 0;
238 list->cost_one_list = NULL;
239 return list;
240}
241
242
243/* Delete coalesce list CL. */
244
b8698a0f 245static inline void
7290d709
AM
246delete_coalesce_list (coalesce_list_p cl)
247{
248 gcc_assert (cl->cost_one_list == NULL);
bf190e8d 249 cl->list.dispose ();
04695783 250 free (cl->sorted);
7290d709
AM
251 gcc_assert (cl->num_sorted == 0);
252 free (cl);
253}
254
255
b8698a0f
L
256/* Find a matching coalesce pair object in CL for the pair P1 and P2. If
257 one isn't found, return NULL if CREATE is false, otherwise create a new
7290d709
AM
258 coalesce pair object and return it. */
259
260static coalesce_pair_p
261find_coalesce_pair (coalesce_list_p cl, int p1, int p2, bool create)
262{
a7d04a53 263 struct coalesce_pair p;
bf190e8d 264 coalesce_pair **slot;
7290d709 265 unsigned int hash;
b8698a0f 266
7290d709
AM
267 /* Normalize so that p1 is the smaller value. */
268 if (p2 < p1)
269 {
270 p.first_element = p2;
271 p.second_element = p1;
272 }
273 else
274 {
275 p.first_element = p1;
276 p.second_element = p2;
277 }
b8698a0f 278
bf190e8d
LC
279 hash = coalesce_pair_hasher::hash (&p);
280 slot = cl->list.find_slot_with_hash (&p, hash, create ? INSERT : NO_INSERT);
a7d04a53
RG
281 if (!slot)
282 return NULL;
7290d709 283
a7d04a53 284 if (!*slot)
7290d709 285 {
a7d04a53 286 struct coalesce_pair * pair = XNEW (struct coalesce_pair);
7290d709 287 gcc_assert (cl->sorted == NULL);
7290d709
AM
288 pair->first_element = p.first_element;
289 pair->second_element = p.second_element;
290 pair->cost = 0;
bf190e8d 291 *slot = pair;
7290d709
AM
292 }
293
a7d04a53 294 return (struct coalesce_pair *) *slot;
7290d709
AM
295}
296
297static inline void
298add_cost_one_coalesce (coalesce_list_p cl, int p1, int p2)
299{
300 cost_one_pair_p pair;
301
c22940cd 302 pair = XNEW (struct cost_one_pair_d);
7290d709
AM
303 pair->first_element = p1;
304 pair->second_element = p2;
305 pair->next = cl->cost_one_list;
306 cl->cost_one_list = pair;
307}
308
309
310/* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
311
b8698a0f 312static inline void
4bb07c5d 313add_coalesce (coalesce_list_p cl, int p1, int p2, int value)
7290d709
AM
314{
315 coalesce_pair_p node;
316
317 gcc_assert (cl->sorted == NULL);
318 if (p1 == p2)
319 return;
320
321 node = find_coalesce_pair (cl, p1, p2, true);
322
4bb07c5d
EB
323 /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way. */
324 if (node->cost < MUST_COALESCE_COST - 1)
7290d709 325 {
4bb07c5d 326 if (value < MUST_COALESCE_COST - 1)
7290d709 327 node->cost += value;
4bb07c5d
EB
328 else
329 node->cost = value;
7290d709
AM
330 }
331}
332
333
2e226e66 334/* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
7290d709 335
b8698a0f 336static int
7290d709
AM
337compare_pairs (const void *p1, const void *p2)
338{
3d9a9f94
KG
339 const_coalesce_pair_p const *const pp1 = (const_coalesce_pair_p const *) p1;
340 const_coalesce_pair_p const *const pp2 = (const_coalesce_pair_p const *) p2;
7562df81
NC
341 int result;
342
4e3825db 343 result = (* pp1)->cost - (* pp2)->cost;
7562df81
NC
344 /* Since qsort does not guarantee stability we use the elements
345 as a secondary key. This provides us with independence from
346 the host's implementation of the sorting algorithm. */
347 if (result == 0)
348 {
349 result = (* pp2)->first_element - (* pp1)->first_element;
350 if (result == 0)
351 result = (* pp2)->second_element - (* pp1)->second_element;
352 }
353
354 return result;
7290d709
AM
355}
356
357
358/* Return the number of unique coalesce pairs in CL. */
359
360static inline int
361num_coalesce_pairs (coalesce_list_p cl)
362{
bf190e8d 363 return cl->list.elements ();
7290d709
AM
364}
365
366
367/* Iterate over CL using ITER, returning values in PAIR. */
368
369#define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
bf190e8d 370 FOR_EACH_HASH_TABLE_ELEMENT ((CL)->list, (PAIR), coalesce_pair_p, (ITER))
7290d709
AM
371
372
373/* Prepare CL for removal of preferred pairs. When finished they are sorted
374 in order from most important coalesce to least important. */
375
376static void
377sort_coalesce_list (coalesce_list_p cl)
378{
379 unsigned x, num;
380 coalesce_pair_p p;
bf190e8d 381 coalesce_iterator_type ppi;
7290d709
AM
382
383 gcc_assert (cl->sorted == NULL);
384
385 num = num_coalesce_pairs (cl);
386 cl->num_sorted = num;
387 if (num == 0)
388 return;
389
390 /* Allocate a vector for the pair pointers. */
391 cl->sorted = XNEWVEC (coalesce_pair_p, num);
392
393 /* Populate the vector with pointers to the pairs. */
394 x = 0;
395 FOR_EACH_PARTITION_PAIR (p, ppi, cl)
396 cl->sorted[x++] = p;
397 gcc_assert (x == num);
398
399 /* Already sorted. */
400 if (num == 1)
401 return;
402
403 /* If there are only 2, just pick swap them if the order isn't correct. */
404 if (num == 2)
405 {
406 if (cl->sorted[0]->cost > cl->sorted[1]->cost)
407 {
408 p = cl->sorted[0];
409 cl->sorted[0] = cl->sorted[1];
410 cl->sorted[1] = p;
411 }
412 return;
413 }
414
415 /* Only call qsort if there are more than 2 items. */
416 if (num > 2)
417 qsort (cl->sorted, num, sizeof (coalesce_pair_p), compare_pairs);
418}
419
420
421/* Send debug info for coalesce list CL to file F. */
422
b8698a0f 423static void
7290d709
AM
424dump_coalesce_list (FILE *f, coalesce_list_p cl)
425{
426 coalesce_pair_p node;
bf190e8d
LC
427 coalesce_iterator_type ppi;
428
7290d709
AM
429 int x;
430 tree var;
431
432 if (cl->sorted == NULL)
433 {
434 fprintf (f, "Coalesce List:\n");
435 FOR_EACH_PARTITION_PAIR (node, ppi, cl)
436 {
437 tree var1 = ssa_name (node->first_element);
438 tree var2 = ssa_name (node->second_element);
439 print_generic_expr (f, var1, TDF_SLIM);
440 fprintf (f, " <-> ");
441 print_generic_expr (f, var2, TDF_SLIM);
442 fprintf (f, " (%1d), ", node->cost);
443 fprintf (f, "\n");
444 }
445 }
446 else
447 {
448 fprintf (f, "Sorted Coalesce list:\n");
449 for (x = cl->num_sorted - 1 ; x >=0; x--)
450 {
451 node = cl->sorted[x];
452 fprintf (f, "(%d) ", node->cost);
453 var = ssa_name (node->first_element);
454 print_generic_expr (f, var, TDF_SLIM);
455 fprintf (f, " <-> ");
456 var = ssa_name (node->second_element);
457 print_generic_expr (f, var, TDF_SLIM);
458 fprintf (f, "\n");
459 }
460 }
461}
462
463
b8698a0f 464/* This represents a conflict graph. Implemented as an array of bitmaps.
2e226e66 465 A full matrix is used for conflicts rather than just upper triangular form.
7290d709
AM
466 this make sit much simpler and faster to perform conflict merges. */
467
468typedef struct ssa_conflicts_d
469{
06d43afd 470 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
9771b263 471 vec<bitmap> conflicts;
7290d709
AM
472} * ssa_conflicts_p;
473
110abdbc 474/* Return an empty new conflict graph for SIZE elements. */
7290d709
AM
475
476static inline ssa_conflicts_p
477ssa_conflicts_new (unsigned size)
478{
479 ssa_conflicts_p ptr;
480
481 ptr = XNEW (struct ssa_conflicts_d);
06d43afd 482 bitmap_obstack_initialize (&ptr->obstack);
9771b263
DN
483 ptr->conflicts.create (size);
484 ptr->conflicts.safe_grow_cleared (size);
7290d709
AM
485 return ptr;
486}
487
488
489/* Free storage for conflict graph PTR. */
490
491static inline void
492ssa_conflicts_delete (ssa_conflicts_p ptr)
493{
06d43afd 494 bitmap_obstack_release (&ptr->obstack);
9771b263 495 ptr->conflicts.release ();
7290d709
AM
496 free (ptr);
497}
498
499
500/* Test if elements X and Y conflict in graph PTR. */
501
502static inline bool
503ssa_conflicts_test_p (ssa_conflicts_p ptr, unsigned x, unsigned y)
504{
9771b263
DN
505 bitmap bx = ptr->conflicts[x];
506 bitmap by = ptr->conflicts[y];
7290d709 507
77a74ed7 508 gcc_checking_assert (x != y);
7290d709 509
06d43afd 510 if (bx)
7290d709 511 /* Avoid the lookup if Y has no conflicts. */
06d43afd 512 return by ? bitmap_bit_p (bx, y) : false;
7290d709
AM
513 else
514 return false;
515}
516
517
518/* Add a conflict with Y to the bitmap for X in graph PTR. */
519
520static inline void
521ssa_conflicts_add_one (ssa_conflicts_p ptr, unsigned x, unsigned y)
522{
9771b263 523 bitmap bx = ptr->conflicts[x];
7290d709 524 /* If there are no conflicts yet, allocate the bitmap and set bit. */
06d43afd 525 if (! bx)
9771b263 526 bx = ptr->conflicts[x] = BITMAP_ALLOC (&ptr->obstack);
06d43afd 527 bitmap_set_bit (bx, y);
7290d709
AM
528}
529
530
531/* Add conflicts between X and Y in graph PTR. */
532
533static inline void
534ssa_conflicts_add (ssa_conflicts_p ptr, unsigned x, unsigned y)
535{
77a74ed7 536 gcc_checking_assert (x != y);
7290d709
AM
537 ssa_conflicts_add_one (ptr, x, y);
538 ssa_conflicts_add_one (ptr, y, x);
539}
540
541
542/* Merge all Y's conflict into X in graph PTR. */
543
544static inline void
545ssa_conflicts_merge (ssa_conflicts_p ptr, unsigned x, unsigned y)
546{
547 unsigned z;
548 bitmap_iterator bi;
9771b263
DN
549 bitmap bx = ptr->conflicts[x];
550 bitmap by = ptr->conflicts[y];
7290d709 551
06d43afd
SB
552 gcc_checking_assert (x != y);
553 if (! by)
7290d709
AM
554 return;
555
556 /* Add a conflict between X and every one Y has. If the bitmap doesn't
fa10beec 557 exist, then it has already been coalesced, and we don't need to add a
7290d709 558 conflict. */
06d43afd
SB
559 EXECUTE_IF_SET_IN_BITMAP (by, 0, z, bi)
560 {
9771b263 561 bitmap bz = ptr->conflicts[z];
06d43afd
SB
562 if (bz)
563 bitmap_set_bit (bz, x);
564 }
7290d709 565
06d43afd 566 if (bx)
7290d709
AM
567 {
568 /* If X has conflicts, add Y's to X. */
06d43afd
SB
569 bitmap_ior_into (bx, by);
570 BITMAP_FREE (by);
9771b263 571 ptr->conflicts[y] = NULL;
7290d709
AM
572 }
573 else
574 {
575 /* If X has no conflicts, simply use Y's. */
9771b263
DN
576 ptr->conflicts[x] = by;
577 ptr->conflicts[y] = NULL;
7290d709
AM
578 }
579}
580
581
62b0d9ec
DJ
582/* Dump a conflicts graph. */
583
584static void
585ssa_conflicts_dump (FILE *file, ssa_conflicts_p ptr)
586{
587 unsigned x;
06d43afd 588 bitmap b;
62b0d9ec
DJ
589
590 fprintf (file, "\nConflict graph:\n");
591
9771b263 592 FOR_EACH_VEC_ELT (ptr->conflicts, x, b)
06d43afd 593 if (b)
62b0d9ec 594 {
d630245f 595 fprintf (file, "%d: ", x);
06d43afd 596 dump_bitmap (file, b);
62b0d9ec
DJ
597 }
598}
599
600
b8698a0f
L
601/* This structure is used to efficiently record the current status of live
602 SSA_NAMES when building a conflict graph.
7290d709
AM
603 LIVE_BASE_VAR has a bit set for each base variable which has at least one
604 ssa version live.
b8698a0f
L
605 LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
606 index, and is used to track what partitions of each base variable are
607 live. This makes it easy to add conflicts between just live partitions
608 with the same base variable.
609 The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
7290d709
AM
610 marked as being live. This delays clearing of these bitmaps until
611 they are actually needed again. */
612
613typedef struct live_track_d
614{
06d43afd 615 bitmap_obstack obstack; /* A place to allocate our bitmaps. */
7290d709
AM
616 bitmap live_base_var; /* Indicates if a basevar is live. */
617 bitmap *live_base_partitions; /* Live partitions for each basevar. */
618 var_map map; /* Var_map being used for partition mapping. */
619} * live_track_p;
620
621
622/* This routine will create a new live track structure based on the partitions
623 in MAP. */
624
625static live_track_p
626new_live_track (var_map map)
627{
628 live_track_p ptr;
629 int lim, x;
630
631 /* Make sure there is a partition view in place. */
632 gcc_assert (map->partition_to_base_index != NULL);
633
634 ptr = (live_track_p) xmalloc (sizeof (struct live_track_d));
635 ptr->map = map;
636 lim = num_basevars (map);
06d43afd 637 bitmap_obstack_initialize (&ptr->obstack);
7290d709 638 ptr->live_base_partitions = (bitmap *) xmalloc(sizeof (bitmap *) * lim);
06d43afd 639 ptr->live_base_var = BITMAP_ALLOC (&ptr->obstack);
7290d709 640 for (x = 0; x < lim; x++)
06d43afd 641 ptr->live_base_partitions[x] = BITMAP_ALLOC (&ptr->obstack);
7290d709
AM
642 return ptr;
643}
644
645
646/* This routine will free the memory associated with PTR. */
647
648static void
649delete_live_track (live_track_p ptr)
650{
06d43afd 651 bitmap_obstack_release (&ptr->obstack);
7290d709
AM
652 free (ptr->live_base_partitions);
653 free (ptr);
654}
655
656
657/* This function will remove PARTITION from the live list in PTR. */
658
659static inline void
660live_track_remove_partition (live_track_p ptr, int partition)
661{
662 int root;
663
664 root = basevar_index (ptr->map, partition);
665 bitmap_clear_bit (ptr->live_base_partitions[root], partition);
666 /* If the element list is empty, make the base variable not live either. */
667 if (bitmap_empty_p (ptr->live_base_partitions[root]))
668 bitmap_clear_bit (ptr->live_base_var, root);
669}
670
671
672/* This function will adds PARTITION to the live list in PTR. */
673
674static inline void
675live_track_add_partition (live_track_p ptr, int partition)
676{
677 int root;
678
679 root = basevar_index (ptr->map, partition);
b8698a0f 680 /* If this base var wasn't live before, it is now. Clear the element list
7290d709 681 since it was delayed until needed. */
fcaa4ca4
NF
682 if (bitmap_set_bit (ptr->live_base_var, root))
683 bitmap_clear (ptr->live_base_partitions[root]);
7290d709 684 bitmap_set_bit (ptr->live_base_partitions[root], partition);
b8698a0f 685
7290d709
AM
686}
687
688
689/* Clear the live bit for VAR in PTR. */
690
691static inline void
692live_track_clear_var (live_track_p ptr, tree var)
693{
694 int p;
695
696 p = var_to_partition (ptr->map, var);
697 if (p != NO_PARTITION)
698 live_track_remove_partition (ptr, p);
699}
700
701
702/* Return TRUE if VAR is live in PTR. */
703
704static inline bool
705live_track_live_p (live_track_p ptr, tree var)
706{
707 int p, root;
708
709 p = var_to_partition (ptr->map, var);
710 if (p != NO_PARTITION)
711 {
712 root = basevar_index (ptr->map, p);
713 if (bitmap_bit_p (ptr->live_base_var, root))
714 return bitmap_bit_p (ptr->live_base_partitions[root], p);
715 }
716 return false;
717}
718
719
b8698a0f 720/* This routine will add USE to PTR. USE will be marked as live in both the
7290d709
AM
721 ssa live map and the live bitmap for the root of USE. */
722
723static inline void
724live_track_process_use (live_track_p ptr, tree use)
725{
726 int p;
727
728 p = var_to_partition (ptr->map, use);
729 if (p == NO_PARTITION)
730 return;
731
732 /* Mark as live in the appropriate live list. */
733 live_track_add_partition (ptr, p);
734}
735
736
737/* This routine will process a DEF in PTR. DEF will be removed from the live
b8698a0f 738 lists, and if there are any other live partitions with the same base
7290d709
AM
739 variable, conflicts will be added to GRAPH. */
740
741static inline void
742live_track_process_def (live_track_p ptr, tree def, ssa_conflicts_p graph)
743{
744 int p, root;
745 bitmap b;
746 unsigned x;
747 bitmap_iterator bi;
748
749 p = var_to_partition (ptr->map, def);
750 if (p == NO_PARTITION)
751 return;
752
753 /* Clear the liveness bit. */
754 live_track_remove_partition (ptr, p);
755
756 /* If the bitmap isn't empty now, conflicts need to be added. */
757 root = basevar_index (ptr->map, p);
758 if (bitmap_bit_p (ptr->live_base_var, root))
759 {
760 b = ptr->live_base_partitions[root];
761 EXECUTE_IF_SET_IN_BITMAP (b, 0, x, bi)
762 ssa_conflicts_add (graph, p, x);
763 }
764}
765
766
767/* Initialize PTR with the partitions set in INIT. */
768
769static inline void
770live_track_init (live_track_p ptr, bitmap init)
771{
772 unsigned p;
773 bitmap_iterator bi;
774
775 /* Mark all live on exit partitions. */
776 EXECUTE_IF_SET_IN_BITMAP (init, 0, p, bi)
777 live_track_add_partition (ptr, p);
778}
779
780
781/* This routine will clear all live partitions in PTR. */
782
783static inline void
784live_track_clear_base_vars (live_track_p ptr)
785{
786 /* Simply clear the live base list. Anything marked as live in the element
787 lists will be cleared later if/when the base variable ever comes alive
788 again. */
789 bitmap_clear (ptr->live_base_var);
790}
791
792
793/* Build a conflict graph based on LIVEINFO. Any partitions which are in the
b8698a0f
L
794 partition view of the var_map liveinfo is based on get entries in the
795 conflict graph. Only conflicts between ssa_name partitions with the same
2e226e66 796 base variable are added. */
7290d709
AM
797
798static ssa_conflicts_p
799build_ssa_conflict_graph (tree_live_info_p liveinfo)
800{
801 ssa_conflicts_p graph;
802 var_map map;
803 basic_block bb;
804 ssa_op_iter iter;
805 live_track_p live;
806
807 map = live_var_map (liveinfo);
808 graph = ssa_conflicts_new (num_var_partitions (map));
809
810 live = new_live_track (map);
811
812 FOR_EACH_BB (bb)
813 {
726a989a 814 gimple_stmt_iterator gsi;
7290d709
AM
815
816 /* Start with live on exit temporaries. */
817 live_track_init (live, live_on_exit (liveinfo, bb));
818
726a989a 819 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
7290d709
AM
820 {
821 tree var;
726a989a 822 gimple stmt = gsi_stmt (gsi);
7290d709 823
b8698a0f
L
824 /* A copy between 2 partitions does not introduce an interference
825 by itself. If they did, you would never be able to coalesce
826 two things which are copied. If the two variables really do
827 conflict, they will conflict elsewhere in the program.
828
829 This is handled by simply removing the SRC of the copy from the
7290d709 830 live list, and processing the stmt normally. */
726a989a 831 if (is_gimple_assign (stmt))
7290d709 832 {
726a989a
RB
833 tree lhs = gimple_assign_lhs (stmt);
834 tree rhs1 = gimple_assign_rhs1 (stmt);
835 if (gimple_assign_copy_p (stmt)
836 && TREE_CODE (lhs) == SSA_NAME
837 && TREE_CODE (rhs1) == SSA_NAME)
838 live_track_clear_var (live, rhs1);
7290d709 839 }
b5b8b0ac
AO
840 else if (is_gimple_debug (stmt))
841 continue;
7290d709
AM
842
843 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
844 live_track_process_def (live, var, graph);
845
846 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
847 live_track_process_use (live, var);
848 }
849
b8698a0f 850 /* If result of a PHI is unused, looping over the statements will not
7290d709
AM
851 record any conflicts since the def was never live. Since the PHI node
852 is going to be translated out of SSA form, it will insert a copy.
b8698a0f
L
853 There must be a conflict recorded between the result of the PHI and
854 any variables that are live. Otherwise the out-of-ssa translation
7290d709 855 may create incorrect code. */
726a989a 856 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7290d709 857 {
726a989a 858 gimple phi = gsi_stmt (gsi);
7290d709
AM
859 tree result = PHI_RESULT (phi);
860 if (live_track_live_p (live, result))
861 live_track_process_def (live, result, graph);
862 }
863
864 live_track_clear_base_vars (live);
865 }
866
867 delete_live_track (live);
868 return graph;
869}
870
871
872/* Shortcut routine to print messages to file F of the form:
873 "STR1 EXPR1 STR2 EXPR2 STR3." */
874
875static inline void
876print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
877 tree expr2, const char *str3)
878{
879 fprintf (f, "%s", str1);
880 print_generic_expr (f, expr1, TDF_SLIM);
881 fprintf (f, "%s", str2);
882 print_generic_expr (f, expr2, TDF_SLIM);
883 fprintf (f, "%s", str3);
884}
885
886
7290d709
AM
887/* Print a failure to coalesce a MUST_COALESCE pair X and Y. */
888
889static inline void
890fail_abnormal_edge_coalesce (int x, int y)
891{
068c623d 892 fprintf (stderr, "\nUnable to coalesce ssa_names %d and %d",x, y);
7290d709
AM
893 fprintf (stderr, " which are marked as MUST COALESCE.\n");
894 print_generic_expr (stderr, ssa_name (x), TDF_SLIM);
895 fprintf (stderr, " and ");
896 print_generic_stmt (stderr, ssa_name (y), TDF_SLIM);
897
898 internal_error ("SSA corruption");
899}
900
901
902/* This function creates a var_map for the current function as well as creating
903 a coalesce list for use later in the out of ssa process. */
904
905static var_map
906create_outofssa_var_map (coalesce_list_p cl, bitmap used_in_copy)
907{
726a989a 908 gimple_stmt_iterator gsi;
7290d709
AM
909 basic_block bb;
910 tree var;
726a989a 911 gimple stmt;
7290d709
AM
912 tree first;
913 var_map map;
914 ssa_op_iter iter;
915 int v1, v2, cost;
916 unsigned i;
917
17c665a9 918 map = init_var_map (num_ssa_names);
7290d709
AM
919
920 FOR_EACH_BB (bb)
921 {
726a989a 922 tree arg;
7290d709 923
726a989a 924 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7290d709 925 {
726a989a
RB
926 gimple phi = gsi_stmt (gsi);
927 size_t i;
7290d709
AM
928 int ver;
929 tree res;
930 bool saw_copy = false;
931
726a989a 932 res = gimple_phi_result (phi);
7290d709
AM
933 ver = SSA_NAME_VERSION (res);
934 register_ssa_partition (map, res);
935
b8698a0f 936 /* Register ssa_names and coalesces between the args and the result
7290d709 937 of all PHI. */
726a989a 938 for (i = 0; i < gimple_phi_num_args (phi); i++)
7290d709 939 {
726a989a 940 edge e = gimple_phi_arg_edge (phi, i);
7290d709 941 arg = PHI_ARG_DEF (phi, i);
70b5e7dc
RG
942 if (TREE_CODE (arg) != SSA_NAME)
943 continue;
944
945 register_ssa_partition (map, arg);
e91d0adb 946 if (gimple_can_coalesce_p (arg, res)
70b5e7dc
RG
947 || (e->flags & EDGE_ABNORMAL))
948 {
7290d709
AM
949 saw_copy = true;
950 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (arg));
951 if ((e->flags & EDGE_ABNORMAL) == 0)
952 {
953 int cost = coalesce_cost_edge (e);
40b448ef 954 if (cost == 1 && has_single_use (arg))
70b5e7dc 955 add_cost_one_coalesce (cl, ver, SSA_NAME_VERSION (arg));
7290d709
AM
956 else
957 add_coalesce (cl, ver, SSA_NAME_VERSION (arg), cost);
958 }
959 }
7290d709
AM
960 }
961 if (saw_copy)
962 bitmap_set_bit (used_in_copy, ver);
963 }
964
726a989a 965 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7290d709 966 {
726a989a 967 stmt = gsi_stmt (gsi);
7290d709 968
b5b8b0ac
AO
969 if (is_gimple_debug (stmt))
970 continue;
971
7290d709
AM
972 /* Register USE and DEF operands in each statement. */
973 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, (SSA_OP_DEF|SSA_OP_USE))
974 register_ssa_partition (map, var);
975
976 /* Check for copy coalesces. */
726a989a 977 switch (gimple_code (stmt))
7290d709 978 {
726a989a 979 case GIMPLE_ASSIGN:
7290d709 980 {
726a989a
RB
981 tree lhs = gimple_assign_lhs (stmt);
982 tree rhs1 = gimple_assign_rhs1 (stmt);
983
984 if (gimple_assign_copy_p (stmt)
985 && TREE_CODE (lhs) == SSA_NAME
986 && TREE_CODE (rhs1) == SSA_NAME
e91d0adb 987 && gimple_can_coalesce_p (lhs, rhs1))
7290d709 988 {
726a989a
RB
989 v1 = SSA_NAME_VERSION (lhs);
990 v2 = SSA_NAME_VERSION (rhs1);
7290d709
AM
991 cost = coalesce_cost_bb (bb);
992 add_coalesce (cl, v1, v2, cost);
993 bitmap_set_bit (used_in_copy, v1);
994 bitmap_set_bit (used_in_copy, v2);
995 }
996 }
997 break;
998
726a989a 999 case GIMPLE_ASM:
7290d709
AM
1000 {
1001 unsigned long noutputs, i;
726a989a 1002 unsigned long ninputs;
7290d709 1003 tree *outputs, link;
726a989a
RB
1004 noutputs = gimple_asm_noutputs (stmt);
1005 ninputs = gimple_asm_ninputs (stmt);
7290d709 1006 outputs = (tree *) alloca (noutputs * sizeof (tree));
06d43afd
SB
1007 for (i = 0; i < noutputs; ++i)
1008 {
1009 link = gimple_asm_output_op (stmt, i);
1010 outputs[i] = TREE_VALUE (link);
1011 }
7290d709 1012
726a989a 1013 for (i = 0; i < ninputs; ++i)
7290d709 1014 {
726a989a
RB
1015 const char *constraint;
1016 tree input;
7290d709
AM
1017 char *end;
1018 unsigned long match;
1019
726a989a
RB
1020 link = gimple_asm_input_op (stmt, i);
1021 constraint
1022 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1023 input = TREE_VALUE (link);
1024
3521f3cc 1025 if (TREE_CODE (input) != SSA_NAME)
7290d709
AM
1026 continue;
1027
1028 match = strtoul (constraint, &end, 10);
1029 if (match >= noutputs || end == constraint)
1030 continue;
1031
1032 if (TREE_CODE (outputs[match]) != SSA_NAME)
1033 continue;
1034
1035 v1 = SSA_NAME_VERSION (outputs[match]);
1036 v2 = SSA_NAME_VERSION (input);
1037
e91d0adb 1038 if (gimple_can_coalesce_p (outputs[match], input))
7290d709 1039 {
b8698a0f 1040 cost = coalesce_cost (REG_BR_PROB_BASE,
048bf48b 1041 optimize_bb_for_size_p (bb));
7290d709
AM
1042 add_coalesce (cl, v1, v2, cost);
1043 bitmap_set_bit (used_in_copy, v1);
1044 bitmap_set_bit (used_in_copy, v2);
1045 }
1046 }
1047 break;
1048 }
1049
1050 default:
1051 break;
1052 }
7290d709
AM
1053 }
1054 }
1055
1056 /* Now process result decls and live on entry variables for entry into
1057 the coalesce list. */
1058 first = NULL_TREE;
1059 for (i = 1; i < num_ssa_names; i++)
1060 {
4e3825db 1061 var = ssa_name (i);
ea057359 1062 if (var != NULL_TREE && !virtual_operand_p (var))
7290d709
AM
1063 {
1064 /* Add coalesces between all the result decls. */
70b5e7dc
RG
1065 if (SSA_NAME_VAR (var)
1066 && TREE_CODE (SSA_NAME_VAR (var)) == RESULT_DECL)
7290d709
AM
1067 {
1068 if (first == NULL_TREE)
1069 first = var;
1070 else
1071 {
e91d0adb 1072 gcc_assert (gimple_can_coalesce_p (var, first));
7290d709
AM
1073 v1 = SSA_NAME_VERSION (first);
1074 v2 = SSA_NAME_VERSION (var);
1075 bitmap_set_bit (used_in_copy, v1);
1076 bitmap_set_bit (used_in_copy, v2);
1077 cost = coalesce_cost_bb (EXIT_BLOCK_PTR);
1078 add_coalesce (cl, v1, v2, cost);
1079 }
1080 }
1081 /* Mark any default_def variables as being in the coalesce list
1082 since they will have to be coalesced with the base variable. If
1083 not marked as present, they won't be in the coalesce view. */
32244553 1084 if (SSA_NAME_IS_DEFAULT_DEF (var)
4e3825db 1085 && !has_zero_uses (var))
7290d709
AM
1086 bitmap_set_bit (used_in_copy, SSA_NAME_VERSION (var));
1087 }
1088 }
1089
7290d709
AM
1090 return map;
1091}
1092
1093
2e226e66 1094/* Attempt to coalesce ssa versions X and Y together using the partition
7290d709
AM
1095 mapping in MAP and checking conflicts in GRAPH. Output any debug info to
1096 DEBUG, if it is nun-NULL. */
1097
1098static inline bool
1099attempt_coalesce (var_map map, ssa_conflicts_p graph, int x, int y,
1100 FILE *debug)
1101{
1102 int z;
1103 tree var1, var2;
1104 int p1, p2;
1105
1106 p1 = var_to_partition (map, ssa_name (x));
1107 p2 = var_to_partition (map, ssa_name (y));
1108
1109 if (debug)
1110 {
1111 fprintf (debug, "(%d)", x);
1112 print_generic_expr (debug, partition_to_var (map, p1), TDF_SLIM);
1113 fprintf (debug, " & (%d)", y);
1114 print_generic_expr (debug, partition_to_var (map, p2), TDF_SLIM);
1115 }
1116
b8698a0f 1117 if (p1 == p2)
7290d709
AM
1118 {
1119 if (debug)
1120 fprintf (debug, ": Already Coalesced.\n");
1121 return true;
1122 }
1123
1124 if (debug)
1125 fprintf (debug, " [map: %d, %d] ", p1, p2);
1126
1127
1128 if (!ssa_conflicts_test_p (graph, p1, p2))
1129 {
1130 var1 = partition_to_var (map, p1);
1131 var2 = partition_to_var (map, p2);
1132 z = var_union (map, var1, var2);
1133 if (z == NO_PARTITION)
1134 {
1135 if (debug)
1136 fprintf (debug, ": Unable to perform partition union.\n");
1137 return false;
1138 }
1139
b8698a0f 1140 /* z is the new combined partition. Remove the other partition from
7290d709
AM
1141 the list, and merge the conflicts. */
1142 if (z == p1)
1143 ssa_conflicts_merge (graph, p1, p2);
1144 else
1145 ssa_conflicts_merge (graph, p2, p1);
1146
1147 if (debug)
1148 fprintf (debug, ": Success -> %d\n", z);
1149 return true;
1150 }
1151
1152 if (debug)
1153 fprintf (debug, ": Fail due to conflict\n");
1154
1155 return false;
1156}
1157
1158
b8698a0f 1159/* Attempt to Coalesce partitions in MAP which occur in the list CL using
7290d709
AM
1160 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1161
1162static void
b8698a0f 1163coalesce_partitions (var_map map, ssa_conflicts_p graph, coalesce_list_p cl,
7290d709
AM
1164 FILE *debug)
1165{
1166 int x = 0, y = 0;
726a989a 1167 tree var1, var2;
7290d709
AM
1168 int cost;
1169 basic_block bb;
1170 edge e;
1171 edge_iterator ei;
1172
2e226e66 1173 /* First, coalesce all the copies across abnormal edges. These are not placed
b8698a0f 1174 in the coalesce list because they do not need to be sorted, and simply
7290d709
AM
1175 consume extra memory/compilation time in large programs. */
1176
1177 FOR_EACH_BB (bb)
1178 {
1179 FOR_EACH_EDGE (e, ei, bb->preds)
1180 if (e->flags & EDGE_ABNORMAL)
1181 {
726a989a
RB
1182 gimple_stmt_iterator gsi;
1183 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1184 gsi_next (&gsi))
7290d709 1185 {
726a989a 1186 gimple phi = gsi_stmt (gsi);
7290d709
AM
1187 tree res = PHI_RESULT (phi);
1188 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
1189 int v1 = SSA_NAME_VERSION (res);
1190 int v2 = SSA_NAME_VERSION (arg);
1191
7290d709
AM
1192 if (debug)
1193 fprintf (debug, "Abnormal coalesce: ");
1194
1195 if (!attempt_coalesce (map, graph, v1, v2, debug))
1196 fail_abnormal_edge_coalesce (v1, v2);
1197 }
1198 }
1199 }
1200
1201 /* Now process the items in the coalesce list. */
1202
1203 while ((cost = pop_best_coalesce (cl, &x, &y)) != NO_BEST_COALESCE)
1204 {
1205 var1 = ssa_name (x);
1206 var2 = ssa_name (y);
1207
1208 /* Assert the coalesces have the same base variable. */
e91d0adb 1209 gcc_assert (gimple_can_coalesce_p (var1, var2));
7290d709
AM
1210
1211 if (debug)
1212 fprintf (debug, "Coalesce list: ");
1213 attempt_coalesce (map, graph, x, y, debug);
1214 }
1215}
1216
5deac340
RG
1217
1218/* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1219
e492fb92 1220struct ssa_name_var_hash : typed_noop_remove <tree_node>
5deac340 1221{
5831a5f0
LC
1222 typedef union tree_node value_type;
1223 typedef union tree_node compare_type;
1224 static inline hashval_t hash (const value_type *);
1225 static inline int equal (const value_type *, const compare_type *);
5deac340 1226};
c72321c9 1227
0823efed 1228inline hashval_t
5deac340 1229ssa_name_var_hash::hash (const_tree n)
c72321c9 1230{
5deac340 1231 return DECL_UID (SSA_NAME_VAR (n));
c72321c9
JH
1232}
1233
0823efed 1234inline int
5831a5f0 1235ssa_name_var_hash::equal (const value_type *n1, const compare_type *n2)
c72321c9 1236{
c72321c9
JH
1237 return SSA_NAME_VAR (n1) == SSA_NAME_VAR (n2);
1238}
7290d709 1239
5deac340 1240
7290d709
AM
1241/* Reduce the number of copies by coalescing variables in the function. Return
1242 a partition map with the resulting coalesces. */
1243
1244extern var_map
1245coalesce_ssa_name (void)
1246{
7290d709
AM
1247 tree_live_info_p liveinfo;
1248 ssa_conflicts_p graph;
1249 coalesce_list_p cl;
1250 bitmap used_in_copies = BITMAP_ALLOC (NULL);
1251 var_map map;
c72321c9 1252 unsigned int i;
7290d709
AM
1253
1254 cl = create_coalesce_list ();
1255 map = create_outofssa_var_map (cl, used_in_copies);
1256
c72321c9
JH
1257 /* We need to coalesce all names originating same SSA_NAME_VAR
1258 so debug info remains undisturbed. */
1259 if (!optimize)
1260 {
abcc192b
JJ
1261 hash_table <ssa_name_var_hash> ssa_name_hash;
1262
0823efed 1263 ssa_name_hash.create (10);
c72321c9
JH
1264 for (i = 1; i < num_ssa_names; i++)
1265 {
1266 tree a = ssa_name (i);
1267
0ffd2048
MM
1268 if (a
1269 && SSA_NAME_VAR (a)
0c1e1df8 1270 && !DECL_IGNORED_P (SSA_NAME_VAR (a))
0ffd2048 1271 && (!has_zero_uses (a) || !SSA_NAME_IS_DEFAULT_DEF (a)))
c72321c9 1272 {
0823efed 1273 tree *slot = ssa_name_hash.find_slot (a, INSERT);
c72321c9
JH
1274
1275 if (!*slot)
1276 *slot = a;
1277 else
1278 {
1279 add_coalesce (cl, SSA_NAME_VERSION (a), SSA_NAME_VERSION (*slot),
1280 MUST_COALESCE_COST - 1);
1281 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (a));
1282 bitmap_set_bit (used_in_copies, SSA_NAME_VERSION (*slot));
1283 }
1284 }
1285 }
0823efed 1286 ssa_name_hash.dispose ();
c72321c9
JH
1287 }
1288 if (dump_file && (dump_flags & TDF_DETAILS))
1289 dump_var_map (dump_file, map);
1290
7290d709
AM
1291 /* Don't calculate live ranges for variables not in the coalesce list. */
1292 partition_view_bitmap (map, used_in_copies, true);
1293 BITMAP_FREE (used_in_copies);
1294
0d700450 1295 if (num_var_partitions (map) < 1)
7290d709
AM
1296 {
1297 delete_coalesce_list (cl);
1298 return map;
1299 }
1300
1301 if (dump_file && (dump_flags & TDF_DETAILS))
1302 dump_var_map (dump_file, map);
1303
1304 liveinfo = calculate_live_ranges (map);
1305
1306 if (dump_file && (dump_flags & TDF_DETAILS))
1307 dump_live_info (dump_file, liveinfo, LIVEDUMP_ENTRY);
1308
1309 /* Build a conflict graph. */
1310 graph = build_ssa_conflict_graph (liveinfo);
1311 delete_tree_live_info (liveinfo);
62b0d9ec
DJ
1312 if (dump_file && (dump_flags & TDF_DETAILS))
1313 ssa_conflicts_dump (dump_file, graph);
7290d709
AM
1314
1315 sort_coalesce_list (cl);
1316
1317 if (dump_file && (dump_flags & TDF_DETAILS))
1318 {
1319 fprintf (dump_file, "\nAfter sorting:\n");
1320 dump_coalesce_list (dump_file, cl);
1321 }
1322
b8698a0f 1323 /* First, coalesce all live on entry variables to their base variable.
7290d709
AM
1324 This will ensure the first use is coming from the correct location. */
1325
7290d709
AM
1326 if (dump_file && (dump_flags & TDF_DETAILS))
1327 dump_var_map (dump_file, map);
1328
1329 /* Now coalesce everything in the list. */
b8698a0f 1330 coalesce_partitions (map, graph, cl,
7290d709
AM
1331 ((dump_flags & TDF_DETAILS) ? dump_file
1332 : NULL));
1333
1334 delete_coalesce_list (cl);
1335 ssa_conflicts_delete (graph);
1336
1337 return map;
1338}
e91d0adb
JL
1339
1340/* Given SSA_NAMEs NAME1 and NAME2, return true if they are candidates for
1341 coalescing together, false otherwise.
1342
1343 This must stay consistent with var_map_base_init in tree-ssa-live.c. */
1344
1345bool
1346gimple_can_coalesce_p (tree name1, tree name2)
1347{
1348 /* First check the SSA_NAME's associated DECL. We only want to
1349 coalesce if they have the same DECL or both have no associated DECL. */
1350 if (SSA_NAME_VAR (name1) != SSA_NAME_VAR (name2))
1351 return false;
1352
1353 /* Now check the types. If the types are the same, then we should
1354 try to coalesce V1 and V2. */
1355 tree t1 = TREE_TYPE (name1);
1356 tree t2 = TREE_TYPE (name2);
1357 if (t1 == t2)
1358 return true;
1359
1360 /* If the types are not the same, check for a canonical type match. This
1361 (for example) allows coalescing when the types are fundamentally the
1362 same, but just have different names.
1363
1364 Note pointer types with different address spaces may have the same
1365 canonical type. Those are rejected for coalescing by the
1366 types_compatible_p check. */
1367 if (TYPE_CANONICAL (t1)
1368 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)
1369 && types_compatible_p (t1, t2))
1370 return true;
1371
1372 return false;
1373}