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