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