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