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