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