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