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>
5 This file is part of GCC.
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)
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.
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/>. */
23 #include "coretypes.h"
28 #include "double-int.h"
35 #include "fold-const.h"
37 #include "tree-pretty-print.h"
40 #include "hash-table.h"
42 #include "hard-reg-set.h"
45 #include "dominance.h"
47 #include "basic-block.h"
48 #include "tree-ssa-alias.h"
49 #include "internal-fn.h"
50 #include "gimple-expr.h"
53 #include "gimple-iterator.h"
54 #include "gimple-ssa.h"
55 #include "tree-phinodes.h"
56 #include "ssa-iterators.h"
57 #include "stringpool.h"
58 #include "tree-ssanames.h"
59 #include "tree-ssa-live.h"
60 #include "tree-ssa-coalesce.h"
61 #include "diagnostic-core.h"
65 /* This set of routines implements a coalesce_list. This is an object which
66 is used to track pairs of ssa_names which are desirable to coalesce
67 together to avoid copies. Costs are associated with each pair, and when
68 all desired information has been collected, the object can be used to
69 order the pairs for processing. */
71 /* This structure defines a pair entry. */
73 typedef struct coalesce_pair
79 typedef const struct coalesce_pair
*const_coalesce_pair_p
;
81 /* Coalesce pair hashtable helpers. */
83 struct coalesce_pair_hasher
: typed_noop_remove
<coalesce_pair
>
85 typedef coalesce_pair value_type
;
86 typedef coalesce_pair compare_type
;
87 static inline hashval_t
hash (const value_type
*);
88 static inline bool equal (const value_type
*, const compare_type
*);
91 /* Hash function for coalesce list. Calculate hash for PAIR. */
94 coalesce_pair_hasher::hash (const value_type
*pair
)
96 hashval_t a
= (hashval_t
)(pair
->first_element
);
97 hashval_t b
= (hashval_t
)(pair
->second_element
);
99 return b
* (b
- 1) / 2 + a
;
102 /* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
103 returning TRUE if the two pairs are equivalent. */
106 coalesce_pair_hasher::equal (const value_type
*p1
, const compare_type
*p2
)
108 return (p1
->first_element
== p2
->first_element
109 && p1
->second_element
== p2
->second_element
);
112 typedef hash_table
<coalesce_pair_hasher
> coalesce_table_type
;
113 typedef coalesce_table_type::iterator coalesce_iterator_type
;
116 typedef struct cost_one_pair_d
120 struct cost_one_pair_d
*next
;
123 /* This structure maintains the list of coalesce pairs. */
125 typedef struct coalesce_list_d
127 coalesce_table_type
*list
; /* Hash table. */
128 coalesce_pair_p
*sorted
; /* List when sorted. */
129 int num_sorted
; /* Number in the sorted list. */
130 cost_one_pair_p cost_one_list
;/* Single use coalesces with cost 1. */
133 #define NO_BEST_COALESCE -1
134 #define MUST_COALESCE_COST INT_MAX
137 /* Return cost of execution of copy instruction with FREQUENCY. */
140 coalesce_cost (int frequency
, bool optimize_for_size
)
142 /* Base costs on BB frequencies bounded by 1. */
143 int cost
= frequency
;
148 if (optimize_for_size
)
155 /* Return the cost of executing a copy instruction in basic block BB. */
158 coalesce_cost_bb (basic_block bb
)
160 return coalesce_cost (bb
->frequency
, optimize_bb_for_size_p (bb
));
164 /* Return the cost of executing a copy instruction on edge E. */
167 coalesce_cost_edge (edge e
)
171 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
172 if (EDGE_CRITICAL_P (e
))
174 if (e
->flags
& EDGE_ABNORMAL
)
175 return MUST_COALESCE_COST
;
176 if (e
->flags
& EDGE_EH
)
180 FOR_EACH_EDGE (e2
, ei
, e
->dest
->preds
)
183 /* Putting code on EH edge that leads to BB
184 with multiple predecestors imply splitting of
188 /* If there are multiple EH predecestors, we
189 also copy EH regions and produce separate
190 landing pad. This is expensive. */
191 if (e2
->flags
& EDGE_EH
)
199 return coalesce_cost (EDGE_FREQUENCY (e
),
200 optimize_edge_for_size_p (e
)) * mult
;
204 /* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
205 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
206 NO_BEST_COALESCE is returned if there aren't any. */
209 pop_cost_one_pair (coalesce_list_p cl
, int *p1
, int *p2
)
213 ptr
= cl
->cost_one_list
;
215 return NO_BEST_COALESCE
;
217 *p1
= ptr
->first_element
;
218 *p2
= ptr
->second_element
;
219 cl
->cost_one_list
= ptr
->next
;
226 /* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
227 2 elements via P1 and P2. Their calculated cost is returned by the function.
228 NO_BEST_COALESCE is returned if the coalesce list is empty. */
231 pop_best_coalesce (coalesce_list_p cl
, int *p1
, int *p2
)
233 coalesce_pair_p node
;
236 if (cl
->sorted
== NULL
)
237 return pop_cost_one_pair (cl
, p1
, p2
);
239 if (cl
->num_sorted
== 0)
240 return pop_cost_one_pair (cl
, p1
, p2
);
242 node
= cl
->sorted
[--(cl
->num_sorted
)];
243 *p1
= node
->first_element
;
244 *p2
= node
->second_element
;
252 /* Create a new empty coalesce list object and return it. */
254 static inline coalesce_list_p
255 create_coalesce_list (void)
257 coalesce_list_p list
;
258 unsigned size
= num_ssa_names
* 3;
263 list
= (coalesce_list_p
) xmalloc (sizeof (struct coalesce_list_d
));
264 list
->list
= new coalesce_table_type (size
);
266 list
->num_sorted
= 0;
267 list
->cost_one_list
= NULL
;
272 /* Delete coalesce list CL. */
275 delete_coalesce_list (coalesce_list_p cl
)
277 gcc_assert (cl
->cost_one_list
== NULL
);
281 gcc_assert (cl
->num_sorted
== 0);
286 /* Find a matching coalesce pair object in CL for the pair P1 and P2. If
287 one isn't found, return NULL if CREATE is false, otherwise create a new
288 coalesce pair object and return it. */
290 static coalesce_pair_p
291 find_coalesce_pair (coalesce_list_p cl
, int p1
, int p2
, bool create
)
293 struct coalesce_pair p
;
294 coalesce_pair
**slot
;
297 /* Normalize so that p1 is the smaller value. */
300 p
.first_element
= p2
;
301 p
.second_element
= p1
;
305 p
.first_element
= p1
;
306 p
.second_element
= p2
;
309 hash
= coalesce_pair_hasher::hash (&p
);
310 slot
= cl
->list
->find_slot_with_hash (&p
, hash
, create
? INSERT
: NO_INSERT
);
316 struct coalesce_pair
* pair
= XNEW (struct coalesce_pair
);
317 gcc_assert (cl
->sorted
== NULL
);
318 pair
->first_element
= p
.first_element
;
319 pair
->second_element
= p
.second_element
;
324 return (struct coalesce_pair
*) *slot
;
328 add_cost_one_coalesce (coalesce_list_p cl
, int p1
, int p2
)
330 cost_one_pair_p pair
;
332 pair
= XNEW (struct cost_one_pair_d
);
333 pair
->first_element
= p1
;
334 pair
->second_element
= p2
;
335 pair
->next
= cl
->cost_one_list
;
336 cl
->cost_one_list
= pair
;
340 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
343 add_coalesce (coalesce_list_p cl
, int p1
, int p2
, int value
)
345 coalesce_pair_p node
;
347 gcc_assert (cl
->sorted
== NULL
);
351 node
= find_coalesce_pair (cl
, p1
, p2
, true);
353 /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way. */
354 if (node
->cost
< MUST_COALESCE_COST
- 1)
356 if (value
< MUST_COALESCE_COST
- 1)
364 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
367 compare_pairs (const void *p1
, const void *p2
)
369 const_coalesce_pair_p
const *const pp1
= (const_coalesce_pair_p
const *) p1
;
370 const_coalesce_pair_p
const *const pp2
= (const_coalesce_pair_p
const *) p2
;
373 result
= (* pp1
)->cost
- (* pp2
)->cost
;
374 /* Since qsort does not guarantee stability we use the elements
375 as a secondary key. This provides us with independence from
376 the host's implementation of the sorting algorithm. */
379 result
= (* pp2
)->first_element
- (* pp1
)->first_element
;
381 result
= (* pp2
)->second_element
- (* pp1
)->second_element
;
388 /* Return the number of unique coalesce pairs in CL. */
391 num_coalesce_pairs (coalesce_list_p cl
)
393 return cl
->list
->elements ();
397 /* Iterate over CL using ITER, returning values in PAIR. */
399 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
400 FOR_EACH_HASH_TABLE_ELEMENT (*(CL)->list, (PAIR), coalesce_pair_p, (ITER))
403 /* Prepare CL for removal of preferred pairs. When finished they are sorted
404 in order from most important coalesce to least important. */
407 sort_coalesce_list (coalesce_list_p cl
)
411 coalesce_iterator_type ppi
;
413 gcc_assert (cl
->sorted
== NULL
);
415 num
= num_coalesce_pairs (cl
);
416 cl
->num_sorted
= num
;
420 /* Allocate a vector for the pair pointers. */
421 cl
->sorted
= XNEWVEC (coalesce_pair_p
, num
);
423 /* Populate the vector with pointers to the pairs. */
425 FOR_EACH_PARTITION_PAIR (p
, ppi
, cl
)
427 gcc_assert (x
== num
);
429 /* Already sorted. */
433 /* If there are only 2, just pick swap them if the order isn't correct. */
436 if (cl
->sorted
[0]->cost
> cl
->sorted
[1]->cost
)
439 cl
->sorted
[0] = cl
->sorted
[1];
445 /* Only call qsort if there are more than 2 items. */
447 qsort (cl
->sorted
, num
, sizeof (coalesce_pair_p
), compare_pairs
);
451 /* Send debug info for coalesce list CL to file F. */
454 dump_coalesce_list (FILE *f
, coalesce_list_p cl
)
456 coalesce_pair_p node
;
457 coalesce_iterator_type ppi
;
462 if (cl
->sorted
== NULL
)
464 fprintf (f
, "Coalesce List:\n");
465 FOR_EACH_PARTITION_PAIR (node
, ppi
, cl
)
467 tree var1
= ssa_name (node
->first_element
);
468 tree var2
= ssa_name (node
->second_element
);
469 print_generic_expr (f
, var1
, TDF_SLIM
);
470 fprintf (f
, " <-> ");
471 print_generic_expr (f
, var2
, TDF_SLIM
);
472 fprintf (f
, " (%1d), ", node
->cost
);
478 fprintf (f
, "Sorted Coalesce list:\n");
479 for (x
= cl
->num_sorted
- 1 ; x
>=0; x
--)
481 node
= cl
->sorted
[x
];
482 fprintf (f
, "(%d) ", node
->cost
);
483 var
= ssa_name (node
->first_element
);
484 print_generic_expr (f
, var
, TDF_SLIM
);
485 fprintf (f
, " <-> ");
486 var
= ssa_name (node
->second_element
);
487 print_generic_expr (f
, var
, TDF_SLIM
);
494 /* This represents a conflict graph. Implemented as an array of bitmaps.
495 A full matrix is used for conflicts rather than just upper triangular form.
496 this make sit much simpler and faster to perform conflict merges. */
498 typedef struct ssa_conflicts_d
500 bitmap_obstack obstack
; /* A place to allocate our bitmaps. */
501 vec
<bitmap
> conflicts
;
504 /* Return an empty new conflict graph for SIZE elements. */
506 static inline ssa_conflicts_p
507 ssa_conflicts_new (unsigned size
)
511 ptr
= XNEW (struct ssa_conflicts_d
);
512 bitmap_obstack_initialize (&ptr
->obstack
);
513 ptr
->conflicts
.create (size
);
514 ptr
->conflicts
.safe_grow_cleared (size
);
519 /* Free storage for conflict graph PTR. */
522 ssa_conflicts_delete (ssa_conflicts_p ptr
)
524 bitmap_obstack_release (&ptr
->obstack
);
525 ptr
->conflicts
.release ();
530 /* Test if elements X and Y conflict in graph PTR. */
533 ssa_conflicts_test_p (ssa_conflicts_p ptr
, unsigned x
, unsigned y
)
535 bitmap bx
= ptr
->conflicts
[x
];
536 bitmap by
= ptr
->conflicts
[y
];
538 gcc_checking_assert (x
!= y
);
541 /* Avoid the lookup if Y has no conflicts. */
542 return by
? bitmap_bit_p (bx
, y
) : false;
548 /* Add a conflict with Y to the bitmap for X in graph PTR. */
551 ssa_conflicts_add_one (ssa_conflicts_p ptr
, unsigned x
, unsigned y
)
553 bitmap bx
= ptr
->conflicts
[x
];
554 /* If there are no conflicts yet, allocate the bitmap and set bit. */
556 bx
= ptr
->conflicts
[x
] = BITMAP_ALLOC (&ptr
->obstack
);
557 bitmap_set_bit (bx
, y
);
561 /* Add conflicts between X and Y in graph PTR. */
564 ssa_conflicts_add (ssa_conflicts_p ptr
, unsigned x
, unsigned y
)
566 gcc_checking_assert (x
!= y
);
567 ssa_conflicts_add_one (ptr
, x
, y
);
568 ssa_conflicts_add_one (ptr
, y
, x
);
572 /* Merge all Y's conflict into X in graph PTR. */
575 ssa_conflicts_merge (ssa_conflicts_p ptr
, unsigned x
, unsigned y
)
579 bitmap bx
= ptr
->conflicts
[x
];
580 bitmap by
= ptr
->conflicts
[y
];
582 gcc_checking_assert (x
!= y
);
586 /* Add a conflict between X and every one Y has. If the bitmap doesn't
587 exist, then it has already been coalesced, and we don't need to add a
589 EXECUTE_IF_SET_IN_BITMAP (by
, 0, z
, bi
)
591 bitmap bz
= ptr
->conflicts
[z
];
593 bitmap_set_bit (bz
, x
);
598 /* If X has conflicts, add Y's to X. */
599 bitmap_ior_into (bx
, by
);
601 ptr
->conflicts
[y
] = NULL
;
605 /* If X has no conflicts, simply use Y's. */
606 ptr
->conflicts
[x
] = by
;
607 ptr
->conflicts
[y
] = NULL
;
612 /* Dump a conflicts graph. */
615 ssa_conflicts_dump (FILE *file
, ssa_conflicts_p ptr
)
620 fprintf (file
, "\nConflict graph:\n");
622 FOR_EACH_VEC_ELT (ptr
->conflicts
, x
, b
)
625 fprintf (file
, "%d: ", x
);
626 dump_bitmap (file
, b
);
631 /* This structure is used to efficiently record the current status of live
632 SSA_NAMES when building a conflict graph.
633 LIVE_BASE_VAR has a bit set for each base variable which has at least one
635 LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
636 index, and is used to track what partitions of each base variable are
637 live. This makes it easy to add conflicts between just live partitions
638 with the same base variable.
639 The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
640 marked as being live. This delays clearing of these bitmaps until
641 they are actually needed again. */
643 typedef struct live_track_d
645 bitmap_obstack obstack
; /* A place to allocate our bitmaps. */
646 bitmap live_base_var
; /* Indicates if a basevar is live. */
647 bitmap
*live_base_partitions
; /* Live partitions for each basevar. */
648 var_map map
; /* Var_map being used for partition mapping. */
652 /* This routine will create a new live track structure based on the partitions
656 new_live_track (var_map map
)
661 /* Make sure there is a partition view in place. */
662 gcc_assert (map
->partition_to_base_index
!= NULL
);
664 ptr
= (live_track_p
) xmalloc (sizeof (struct live_track_d
));
666 lim
= num_basevars (map
);
667 bitmap_obstack_initialize (&ptr
->obstack
);
668 ptr
->live_base_partitions
= (bitmap
*) xmalloc (sizeof (bitmap
*) * lim
);
669 ptr
->live_base_var
= BITMAP_ALLOC (&ptr
->obstack
);
670 for (x
= 0; x
< lim
; x
++)
671 ptr
->live_base_partitions
[x
] = BITMAP_ALLOC (&ptr
->obstack
);
676 /* This routine will free the memory associated with PTR. */
679 delete_live_track (live_track_p ptr
)
681 bitmap_obstack_release (&ptr
->obstack
);
682 free (ptr
->live_base_partitions
);
687 /* This function will remove PARTITION from the live list in PTR. */
690 live_track_remove_partition (live_track_p ptr
, int partition
)
694 root
= basevar_index (ptr
->map
, partition
);
695 bitmap_clear_bit (ptr
->live_base_partitions
[root
], partition
);
696 /* If the element list is empty, make the base variable not live either. */
697 if (bitmap_empty_p (ptr
->live_base_partitions
[root
]))
698 bitmap_clear_bit (ptr
->live_base_var
, root
);
702 /* This function will adds PARTITION to the live list in PTR. */
705 live_track_add_partition (live_track_p ptr
, int partition
)
709 root
= basevar_index (ptr
->map
, partition
);
710 /* If this base var wasn't live before, it is now. Clear the element list
711 since it was delayed until needed. */
712 if (bitmap_set_bit (ptr
->live_base_var
, root
))
713 bitmap_clear (ptr
->live_base_partitions
[root
]);
714 bitmap_set_bit (ptr
->live_base_partitions
[root
], partition
);
719 /* Clear the live bit for VAR in PTR. */
722 live_track_clear_var (live_track_p ptr
, tree var
)
726 p
= var_to_partition (ptr
->map
, var
);
727 if (p
!= NO_PARTITION
)
728 live_track_remove_partition (ptr
, p
);
732 /* Return TRUE if VAR is live in PTR. */
735 live_track_live_p (live_track_p ptr
, tree var
)
739 p
= var_to_partition (ptr
->map
, var
);
740 if (p
!= NO_PARTITION
)
742 root
= basevar_index (ptr
->map
, p
);
743 if (bitmap_bit_p (ptr
->live_base_var
, root
))
744 return bitmap_bit_p (ptr
->live_base_partitions
[root
], p
);
750 /* This routine will add USE to PTR. USE will be marked as live in both the
751 ssa live map and the live bitmap for the root of USE. */
754 live_track_process_use (live_track_p ptr
, tree use
)
758 p
= var_to_partition (ptr
->map
, use
);
759 if (p
== NO_PARTITION
)
762 /* Mark as live in the appropriate live list. */
763 live_track_add_partition (ptr
, p
);
767 /* This routine will process a DEF in PTR. DEF will be removed from the live
768 lists, and if there are any other live partitions with the same base
769 variable, conflicts will be added to GRAPH. */
772 live_track_process_def (live_track_p ptr
, tree def
, ssa_conflicts_p graph
)
779 p
= var_to_partition (ptr
->map
, def
);
780 if (p
== NO_PARTITION
)
783 /* Clear the liveness bit. */
784 live_track_remove_partition (ptr
, p
);
786 /* If the bitmap isn't empty now, conflicts need to be added. */
787 root
= basevar_index (ptr
->map
, p
);
788 if (bitmap_bit_p (ptr
->live_base_var
, root
))
790 b
= ptr
->live_base_partitions
[root
];
791 EXECUTE_IF_SET_IN_BITMAP (b
, 0, x
, bi
)
792 ssa_conflicts_add (graph
, p
, x
);
797 /* Initialize PTR with the partitions set in INIT. */
800 live_track_init (live_track_p ptr
, bitmap init
)
805 /* Mark all live on exit partitions. */
806 EXECUTE_IF_SET_IN_BITMAP (init
, 0, p
, bi
)
807 live_track_add_partition (ptr
, p
);
811 /* This routine will clear all live partitions in PTR. */
814 live_track_clear_base_vars (live_track_p ptr
)
816 /* Simply clear the live base list. Anything marked as live in the element
817 lists will be cleared later if/when the base variable ever comes alive
819 bitmap_clear (ptr
->live_base_var
);
823 /* Build a conflict graph based on LIVEINFO. Any partitions which are in the
824 partition view of the var_map liveinfo is based on get entries in the
825 conflict graph. Only conflicts between ssa_name partitions with the same
826 base variable are added. */
828 static ssa_conflicts_p
829 build_ssa_conflict_graph (tree_live_info_p liveinfo
)
831 ssa_conflicts_p graph
;
837 map
= live_var_map (liveinfo
);
838 graph
= ssa_conflicts_new (num_var_partitions (map
));
840 live
= new_live_track (map
);
842 FOR_EACH_BB_FN (bb
, cfun
)
844 /* Start with live on exit temporaries. */
845 live_track_init (live
, live_on_exit (liveinfo
, bb
));
847 for (gimple_stmt_iterator gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
);
851 gimple stmt
= gsi_stmt (gsi
);
853 /* A copy between 2 partitions does not introduce an interference
854 by itself. If they did, you would never be able to coalesce
855 two things which are copied. If the two variables really do
856 conflict, they will conflict elsewhere in the program.
858 This is handled by simply removing the SRC of the copy from the
859 live list, and processing the stmt normally. */
860 if (is_gimple_assign (stmt
))
862 tree lhs
= gimple_assign_lhs (stmt
);
863 tree rhs1
= gimple_assign_rhs1 (stmt
);
864 if (gimple_assign_copy_p (stmt
)
865 && TREE_CODE (lhs
) == SSA_NAME
866 && TREE_CODE (rhs1
) == SSA_NAME
)
867 live_track_clear_var (live
, rhs1
);
869 else if (is_gimple_debug (stmt
))
872 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, iter
, SSA_OP_DEF
)
873 live_track_process_def (live
, var
, graph
);
875 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, iter
, SSA_OP_USE
)
876 live_track_process_use (live
, var
);
879 /* If result of a PHI is unused, looping over the statements will not
880 record any conflicts since the def was never live. Since the PHI node
881 is going to be translated out of SSA form, it will insert a copy.
882 There must be a conflict recorded between the result of the PHI and
883 any variables that are live. Otherwise the out-of-ssa translation
884 may create incorrect code. */
885 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
888 gphi
*phi
= gsi
.phi ();
889 tree result
= PHI_RESULT (phi
);
890 if (live_track_live_p (live
, result
))
891 live_track_process_def (live
, result
, graph
);
894 live_track_clear_base_vars (live
);
897 delete_live_track (live
);
902 /* Shortcut routine to print messages to file F of the form:
903 "STR1 EXPR1 STR2 EXPR2 STR3." */
906 print_exprs (FILE *f
, const char *str1
, tree expr1
, const char *str2
,
907 tree expr2
, const char *str3
)
909 fprintf (f
, "%s", str1
);
910 print_generic_expr (f
, expr1
, TDF_SLIM
);
911 fprintf (f
, "%s", str2
);
912 print_generic_expr (f
, expr2
, TDF_SLIM
);
913 fprintf (f
, "%s", str3
);
917 /* Print a failure to coalesce a MUST_COALESCE pair X and Y. */
920 fail_abnormal_edge_coalesce (int x
, int y
)
922 fprintf (stderr
, "\nUnable to coalesce ssa_names %d and %d",x
, y
);
923 fprintf (stderr
, " which are marked as MUST COALESCE.\n");
924 print_generic_expr (stderr
, ssa_name (x
), TDF_SLIM
);
925 fprintf (stderr
, " and ");
926 print_generic_stmt (stderr
, ssa_name (y
), TDF_SLIM
);
928 internal_error ("SSA corruption");
932 /* This function creates a var_map for the current function as well as creating
933 a coalesce list for use later in the out of ssa process. */
936 create_outofssa_var_map (coalesce_list_p cl
, bitmap used_in_copy
)
938 gimple_stmt_iterator gsi
;
948 map
= init_var_map (num_ssa_names
);
950 FOR_EACH_BB_FN (bb
, cfun
)
954 for (gphi_iterator gpi
= gsi_start_phis (bb
);
958 gphi
*phi
= gpi
.phi ();
962 bool saw_copy
= false;
964 res
= gimple_phi_result (phi
);
965 ver
= SSA_NAME_VERSION (res
);
966 register_ssa_partition (map
, res
);
968 /* Register ssa_names and coalesces between the args and the result
970 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
972 edge e
= gimple_phi_arg_edge (phi
, i
);
973 arg
= PHI_ARG_DEF (phi
, i
);
974 if (TREE_CODE (arg
) != SSA_NAME
)
977 register_ssa_partition (map
, arg
);
978 if (gimple_can_coalesce_p (arg
, res
)
979 || (e
->flags
& EDGE_ABNORMAL
))
982 bitmap_set_bit (used_in_copy
, SSA_NAME_VERSION (arg
));
983 if ((e
->flags
& EDGE_ABNORMAL
) == 0)
985 int cost
= coalesce_cost_edge (e
);
986 if (cost
== 1 && has_single_use (arg
))
987 add_cost_one_coalesce (cl
, ver
, SSA_NAME_VERSION (arg
));
989 add_coalesce (cl
, ver
, SSA_NAME_VERSION (arg
), cost
);
994 bitmap_set_bit (used_in_copy
, ver
);
997 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
999 stmt
= gsi_stmt (gsi
);
1001 if (is_gimple_debug (stmt
))
1004 /* Register USE and DEF operands in each statement. */
1005 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, iter
, (SSA_OP_DEF
|SSA_OP_USE
))
1006 register_ssa_partition (map
, var
);
1008 /* Check for copy coalesces. */
1009 switch (gimple_code (stmt
))
1013 tree lhs
= gimple_assign_lhs (stmt
);
1014 tree rhs1
= gimple_assign_rhs1 (stmt
);
1015 if (gimple_assign_ssa_name_copy_p (stmt
)
1016 && gimple_can_coalesce_p (lhs
, rhs1
))
1018 v1
= SSA_NAME_VERSION (lhs
);
1019 v2
= SSA_NAME_VERSION (rhs1
);
1020 cost
= coalesce_cost_bb (bb
);
1021 add_coalesce (cl
, v1
, v2
, cost
);
1022 bitmap_set_bit (used_in_copy
, v1
);
1023 bitmap_set_bit (used_in_copy
, v2
);
1030 gasm
*asm_stmt
= as_a
<gasm
*> (stmt
);
1031 unsigned long noutputs
, i
;
1032 unsigned long ninputs
;
1033 tree
*outputs
, link
;
1034 noutputs
= gimple_asm_noutputs (asm_stmt
);
1035 ninputs
= gimple_asm_ninputs (asm_stmt
);
1036 outputs
= (tree
*) alloca (noutputs
* sizeof (tree
));
1037 for (i
= 0; i
< noutputs
; ++i
)
1039 link
= gimple_asm_output_op (asm_stmt
, i
);
1040 outputs
[i
] = TREE_VALUE (link
);
1043 for (i
= 0; i
< ninputs
; ++i
)
1045 const char *constraint
;
1048 unsigned long match
;
1050 link
= gimple_asm_input_op (asm_stmt
, i
);
1052 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
1053 input
= TREE_VALUE (link
);
1055 if (TREE_CODE (input
) != SSA_NAME
)
1058 match
= strtoul (constraint
, &end
, 10);
1059 if (match
>= noutputs
|| end
== constraint
)
1062 if (TREE_CODE (outputs
[match
]) != SSA_NAME
)
1065 v1
= SSA_NAME_VERSION (outputs
[match
]);
1066 v2
= SSA_NAME_VERSION (input
);
1068 if (gimple_can_coalesce_p (outputs
[match
], input
))
1070 cost
= coalesce_cost (REG_BR_PROB_BASE
,
1071 optimize_bb_for_size_p (bb
));
1072 add_coalesce (cl
, v1
, v2
, cost
);
1073 bitmap_set_bit (used_in_copy
, v1
);
1074 bitmap_set_bit (used_in_copy
, v2
);
1086 /* Now process result decls and live on entry variables for entry into
1087 the coalesce list. */
1089 for (i
= 1; i
< num_ssa_names
; i
++)
1092 if (var
!= NULL_TREE
&& !virtual_operand_p (var
))
1094 /* Add coalesces between all the result decls. */
1095 if (SSA_NAME_VAR (var
)
1096 && TREE_CODE (SSA_NAME_VAR (var
)) == RESULT_DECL
)
1098 if (first
== NULL_TREE
)
1102 gcc_assert (gimple_can_coalesce_p (var
, first
));
1103 v1
= SSA_NAME_VERSION (first
);
1104 v2
= SSA_NAME_VERSION (var
);
1105 bitmap_set_bit (used_in_copy
, v1
);
1106 bitmap_set_bit (used_in_copy
, v2
);
1107 cost
= coalesce_cost_bb (EXIT_BLOCK_PTR_FOR_FN (cfun
));
1108 add_coalesce (cl
, v1
, v2
, cost
);
1111 /* Mark any default_def variables as being in the coalesce list
1112 since they will have to be coalesced with the base variable. If
1113 not marked as present, they won't be in the coalesce view. */
1114 if (SSA_NAME_IS_DEFAULT_DEF (var
)
1115 && !has_zero_uses (var
))
1116 bitmap_set_bit (used_in_copy
, SSA_NAME_VERSION (var
));
1124 /* Attempt to coalesce ssa versions X and Y together using the partition
1125 mapping in MAP and checking conflicts in GRAPH if not NULL.
1126 Output any debug info to DEBUG, if it is nun-NULL. */
1129 attempt_coalesce (var_map map
, ssa_conflicts_p graph
, int x
, int y
,
1136 p1
= var_to_partition (map
, ssa_name (x
));
1137 p2
= var_to_partition (map
, ssa_name (y
));
1141 fprintf (debug
, "(%d)", x
);
1142 print_generic_expr (debug
, partition_to_var (map
, p1
), TDF_SLIM
);
1143 fprintf (debug
, " & (%d)", y
);
1144 print_generic_expr (debug
, partition_to_var (map
, p2
), TDF_SLIM
);
1150 fprintf (debug
, ": Already Coalesced.\n");
1155 fprintf (debug
, " [map: %d, %d] ", p1
, p2
);
1159 || !ssa_conflicts_test_p (graph
, p1
, p2
))
1161 var1
= partition_to_var (map
, p1
);
1162 var2
= partition_to_var (map
, p2
);
1163 z
= var_union (map
, var1
, var2
);
1164 if (z
== NO_PARTITION
)
1167 fprintf (debug
, ": Unable to perform partition union.\n");
1171 /* z is the new combined partition. Remove the other partition from
1172 the list, and merge the conflicts. */
1176 ssa_conflicts_merge (graph
, p1
, p2
);
1178 ssa_conflicts_merge (graph
, p2
, p1
);
1182 fprintf (debug
, ": Success -> %d\n", z
);
1187 fprintf (debug
, ": Fail due to conflict\n");
1193 /* Perform all abnormal coalescing on MAP.
1194 Debug output is sent to DEBUG if it is non-NULL. */
1197 perform_abnormal_coalescing (var_map map
, FILE *debug
)
1203 FOR_EACH_BB_FN (bb
, cfun
)
1205 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1206 if (e
->flags
& EDGE_ABNORMAL
)
1209 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
1212 gphi
*phi
= gsi
.phi ();
1213 tree arg
= PHI_ARG_DEF (phi
, e
->dest_idx
);
1214 if (SSA_NAME_IS_DEFAULT_DEF (arg
)
1215 && (!SSA_NAME_VAR (arg
)
1216 || TREE_CODE (SSA_NAME_VAR (arg
)) != PARM_DECL
))
1219 tree res
= PHI_RESULT (phi
);
1220 int v1
= SSA_NAME_VERSION (res
);
1221 int v2
= SSA_NAME_VERSION (arg
);
1224 fprintf (debug
, "Abnormal coalesce: ");
1226 if (!attempt_coalesce (map
, NULL
, v1
, v2
, debug
))
1227 fail_abnormal_edge_coalesce (v1
, v2
);
1233 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1234 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1237 coalesce_partitions (var_map map
, ssa_conflicts_p graph
, coalesce_list_p cl
,
1244 /* Now process the items in the coalesce list. */
1246 while ((cost
= pop_best_coalesce (cl
, &x
, &y
)) != NO_BEST_COALESCE
)
1248 var1
= ssa_name (x
);
1249 var2
= ssa_name (y
);
1251 /* Assert the coalesces have the same base variable. */
1252 gcc_assert (gimple_can_coalesce_p (var1
, var2
));
1255 fprintf (debug
, "Coalesce list: ");
1256 attempt_coalesce (map
, graph
, x
, y
, debug
);
1261 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1263 struct ssa_name_var_hash
: typed_noop_remove
<tree_node
>
1265 typedef union tree_node value_type
;
1266 typedef union tree_node compare_type
;
1267 static inline hashval_t
hash (const value_type
*);
1268 static inline int equal (const value_type
*, const compare_type
*);
1272 ssa_name_var_hash::hash (const_tree n
)
1274 return DECL_UID (SSA_NAME_VAR (n
));
1278 ssa_name_var_hash::equal (const value_type
*n1
, const compare_type
*n2
)
1280 return SSA_NAME_VAR (n1
) == SSA_NAME_VAR (n2
);
1284 /* Reduce the number of copies by coalescing variables in the function. Return
1285 a partition map with the resulting coalesces. */
1288 coalesce_ssa_name (void)
1290 tree_live_info_p liveinfo
;
1291 ssa_conflicts_p graph
;
1293 bitmap used_in_copies
= BITMAP_ALLOC (NULL
);
1297 #ifdef ENABLE_CHECKING
1298 /* Verify we can perform all must coalesces. */
1299 verify_ssa_coalescing ();
1302 cl
= create_coalesce_list ();
1303 map
= create_outofssa_var_map (cl
, used_in_copies
);
1305 /* If optimization is disabled, we need to coalesce all the names originating
1306 from the same SSA_NAME_VAR so debug info remains undisturbed. */
1309 hash_table
<ssa_name_var_hash
> ssa_name_hash (10);
1311 for (i
= 1; i
< num_ssa_names
; i
++)
1313 tree a
= ssa_name (i
);
1317 && !DECL_IGNORED_P (SSA_NAME_VAR (a
))
1318 && (!has_zero_uses (a
) || !SSA_NAME_IS_DEFAULT_DEF (a
)))
1320 tree
*slot
= ssa_name_hash
.find_slot (a
, INSERT
);
1326 /* If the variable is a PARM_DECL or a RESULT_DECL, we
1327 _require_ that all the names originating from it be
1328 coalesced, because there must be a single partition
1329 containing all the names so that it can be assigned
1330 the canonical RTL location of the DECL safely.
1331 If in_lto_p, a function could have been compiled
1332 originally with optimizations and only the link
1333 performed at -O0, so we can't actually require it. */
1335 = (TREE_CODE (SSA_NAME_VAR (a
)) == VAR_DECL
|| in_lto_p
)
1336 ? MUST_COALESCE_COST
- 1 : MUST_COALESCE_COST
;
1337 add_coalesce (cl
, SSA_NAME_VERSION (a
),
1338 SSA_NAME_VERSION (*slot
), cost
);
1339 bitmap_set_bit (used_in_copies
, SSA_NAME_VERSION (a
));
1340 bitmap_set_bit (used_in_copies
, SSA_NAME_VERSION (*slot
));
1345 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1346 dump_var_map (dump_file
, map
);
1348 /* Don't calculate live ranges for variables not in the coalesce list. */
1349 partition_view_bitmap (map
, used_in_copies
, true);
1350 BITMAP_FREE (used_in_copies
);
1352 if (num_var_partitions (map
) < 1)
1354 delete_coalesce_list (cl
);
1358 /* First, coalesce all the copies across abnormal edges. These are not placed
1359 in the coalesce list because they do not need to be sorted, and simply
1360 consume extra memory/compilation time in large programs.
1361 Performing abnormal coalescing also needs no live/conflict computation
1362 because it must succeed (but we lose checking that it indeed does).
1363 Still for PR63155 this reduces memory usage from 10GB to zero. */
1364 perform_abnormal_coalescing (map
,
1365 ((dump_flags
& TDF_DETAILS
) ? dump_file
: NULL
));
1367 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1368 dump_var_map (dump_file
, map
);
1370 liveinfo
= calculate_live_ranges (map
, false);
1372 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1373 dump_live_info (dump_file
, liveinfo
, LIVEDUMP_ENTRY
);
1375 /* Build a conflict graph. */
1376 graph
= build_ssa_conflict_graph (liveinfo
);
1377 delete_tree_live_info (liveinfo
);
1378 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1379 ssa_conflicts_dump (dump_file
, graph
);
1381 sort_coalesce_list (cl
);
1383 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1385 fprintf (dump_file
, "\nAfter sorting:\n");
1386 dump_coalesce_list (dump_file
, cl
);
1389 /* First, coalesce all live on entry variables to their base variable.
1390 This will ensure the first use is coming from the correct location. */
1392 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1393 dump_var_map (dump_file
, map
);
1395 /* Now coalesce everything in the list. */
1396 coalesce_partitions (map
, graph
, cl
,
1397 ((dump_flags
& TDF_DETAILS
) ? dump_file
: NULL
));
1399 delete_coalesce_list (cl
);
1400 ssa_conflicts_delete (graph
);
1406 /* Helper for verify_ssa_coalescing. Operates in two modes:
1407 1) scan the function for coalesces we must perform and store the
1408 SSA names participating in USED_IN_COPIES
1409 2) scan the function for coalesces and verify they can be performed
1410 under the constraints of GRAPH updating MAP in the process
1411 FIXME: This can be extended to verify that the virtual operands
1412 form a factored use-def chain (coalescing the active virtual use
1413 with the virtual def at virtual def point). */
1416 verify_ssa_coalescing_worker (bitmap used_in_copies
,
1417 var_map map
, ssa_conflicts_p graph
)
1421 FOR_EACH_BB_FN (bb
, cfun
)
1426 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1427 if (e
->flags
& EDGE_ABNORMAL
)
1430 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
1433 gphi
*phi
= gsi
.phi ();
1434 tree arg
= PHI_ARG_DEF (phi
, e
->dest_idx
);
1435 if (SSA_NAME_IS_DEFAULT_DEF (arg
)
1436 && (!SSA_NAME_VAR (arg
)
1437 || TREE_CODE (SSA_NAME_VAR (arg
)) != PARM_DECL
))
1440 tree res
= PHI_RESULT (phi
);
1442 int v1
= SSA_NAME_VERSION (res
);
1443 int v2
= SSA_NAME_VERSION (arg
);
1446 bitmap_set_bit (used_in_copies
, v1
);
1447 bitmap_set_bit (used_in_copies
, v2
);
1451 int p1
= var_to_partition (map
, res
);
1452 int p2
= var_to_partition (map
, arg
);
1455 if (ssa_conflicts_test_p (graph
, p1
, p2
))
1456 fail_abnormal_edge_coalesce (v1
, v2
);
1457 int z
= var_union (map
,
1458 partition_to_var (map
, p1
),
1459 partition_to_var (map
, p2
));
1461 ssa_conflicts_merge (graph
, p1
, p2
);
1463 ssa_conflicts_merge (graph
, p2
, p1
);
1471 /* Verify that we can coalesce SSA names we must coalesce. */
1474 verify_ssa_coalescing (void)
1476 auto_timevar
tv (TV_TREE_SSA_VERIFY
);
1477 bitmap used_in_copies
= BITMAP_ALLOC (NULL
);
1478 verify_ssa_coalescing_worker (used_in_copies
, NULL
, NULL
);
1479 if (bitmap_empty_p (used_in_copies
))
1481 BITMAP_FREE (used_in_copies
);
1484 var_map map
= init_var_map (num_ssa_names
);
1485 partition_view_bitmap (map
, used_in_copies
, true);
1486 BITMAP_FREE (used_in_copies
);
1487 tree_live_info_p liveinfo
= calculate_live_ranges (map
, false);
1488 ssa_conflicts_p graph
= build_ssa_conflict_graph (liveinfo
);
1489 delete_tree_live_info (liveinfo
);
1490 verify_ssa_coalescing_worker (NULL
, map
, graph
);
1491 ssa_conflicts_delete (graph
);
1492 delete_var_map (map
);