1 /* Coalesce SSA_NAMES together for the out-of-ssa pass.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
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"
27 #include "tree-pretty-print.h"
31 #include "gimple-iterator.h"
32 #include "gimple-ssa.h"
33 #include "tree-phinodes.h"
34 #include "ssa-iterators.h"
35 #include "stringpool.h"
36 #include "tree-ssanames.h"
37 #include "hash-table.h"
38 #include "tree-ssa-live.h"
39 #include "tree-ssa-coalesce.h"
40 #include "diagnostic-core.h"
43 /* This set of routines implements a coalesce_list. This is an object which
44 is used to track pairs of ssa_names which are desirable to coalesce
45 together to avoid copies. Costs are associated with each pair, and when
46 all desired information has been collected, the object can be used to
47 order the pairs for processing. */
49 /* This structure defines a pair entry. */
51 typedef struct coalesce_pair
57 typedef const struct coalesce_pair
*const_coalesce_pair_p
;
59 /* Coalesce pair hashtable helpers. */
61 struct coalesce_pair_hasher
: typed_noop_remove
<coalesce_pair
>
63 typedef coalesce_pair value_type
;
64 typedef coalesce_pair compare_type
;
65 static inline hashval_t
hash (const value_type
*);
66 static inline bool equal (const value_type
*, const compare_type
*);
69 /* Hash function for coalesce list. Calculate hash for PAIR. */
72 coalesce_pair_hasher::hash (const value_type
*pair
)
74 hashval_t a
= (hashval_t
)(pair
->first_element
);
75 hashval_t b
= (hashval_t
)(pair
->second_element
);
77 return b
* (b
- 1) / 2 + a
;
80 /* Equality function for coalesce list hash table. Compare PAIR1 and PAIR2,
81 returning TRUE if the two pairs are equivalent. */
84 coalesce_pair_hasher::equal (const value_type
*p1
, const compare_type
*p2
)
86 return (p1
->first_element
== p2
->first_element
87 && p1
->second_element
== p2
->second_element
);
90 typedef hash_table
<coalesce_pair_hasher
> coalesce_table_type
;
91 typedef coalesce_table_type::iterator coalesce_iterator_type
;
94 typedef struct cost_one_pair_d
98 struct cost_one_pair_d
*next
;
101 /* This structure maintains the list of coalesce pairs. */
103 typedef struct coalesce_list_d
105 coalesce_table_type list
; /* Hash table. */
106 coalesce_pair_p
*sorted
; /* List when sorted. */
107 int num_sorted
; /* Number in the sorted list. */
108 cost_one_pair_p cost_one_list
;/* Single use coalesces with cost 1. */
111 #define NO_BEST_COALESCE -1
112 #define MUST_COALESCE_COST INT_MAX
115 /* Return cost of execution of copy instruction with FREQUENCY. */
118 coalesce_cost (int frequency
, bool optimize_for_size
)
120 /* Base costs on BB frequencies bounded by 1. */
121 int cost
= frequency
;
126 if (optimize_for_size
)
133 /* Return the cost of executing a copy instruction in basic block BB. */
136 coalesce_cost_bb (basic_block bb
)
138 return coalesce_cost (bb
->frequency
, optimize_bb_for_size_p (bb
));
142 /* Return the cost of executing a copy instruction on edge E. */
145 coalesce_cost_edge (edge e
)
149 /* Inserting copy on critical edge costs more than inserting it elsewhere. */
150 if (EDGE_CRITICAL_P (e
))
152 if (e
->flags
& EDGE_ABNORMAL
)
153 return MUST_COALESCE_COST
;
154 if (e
->flags
& EDGE_EH
)
158 FOR_EACH_EDGE (e2
, ei
, e
->dest
->preds
)
161 /* Putting code on EH edge that leads to BB
162 with multiple predecestors imply splitting of
166 /* If there are multiple EH predecestors, we
167 also copy EH regions and produce separate
168 landing pad. This is expensive. */
169 if (e2
->flags
& EDGE_EH
)
177 return coalesce_cost (EDGE_FREQUENCY (e
),
178 optimize_edge_for_size_p (e
)) * mult
;
182 /* Retrieve a pair to coalesce from the cost_one_list in CL. Returns the
183 2 elements via P1 and P2. 1 is returned by the function if there is a pair,
184 NO_BEST_COALESCE is returned if there aren't any. */
187 pop_cost_one_pair (coalesce_list_p cl
, int *p1
, int *p2
)
191 ptr
= cl
->cost_one_list
;
193 return NO_BEST_COALESCE
;
195 *p1
= ptr
->first_element
;
196 *p2
= ptr
->second_element
;
197 cl
->cost_one_list
= ptr
->next
;
204 /* Retrieve the most expensive remaining pair to coalesce from CL. Returns the
205 2 elements via P1 and P2. Their calculated cost is returned by the function.
206 NO_BEST_COALESCE is returned if the coalesce list is empty. */
209 pop_best_coalesce (coalesce_list_p cl
, int *p1
, int *p2
)
211 coalesce_pair_p node
;
214 if (cl
->sorted
== NULL
)
215 return pop_cost_one_pair (cl
, p1
, p2
);
217 if (cl
->num_sorted
== 0)
218 return pop_cost_one_pair (cl
, p1
, p2
);
220 node
= cl
->sorted
[--(cl
->num_sorted
)];
221 *p1
= node
->first_element
;
222 *p2
= node
->second_element
;
230 /* Create a new empty coalesce list object and return it. */
232 static inline coalesce_list_p
233 create_coalesce_list (void)
235 coalesce_list_p list
;
236 unsigned size
= num_ssa_names
* 3;
241 list
= (coalesce_list_p
) xmalloc (sizeof (struct coalesce_list_d
));
242 list
->list
.create (size
);
244 list
->num_sorted
= 0;
245 list
->cost_one_list
= NULL
;
250 /* Delete coalesce list CL. */
253 delete_coalesce_list (coalesce_list_p cl
)
255 gcc_assert (cl
->cost_one_list
== NULL
);
258 gcc_assert (cl
->num_sorted
== 0);
263 /* Find a matching coalesce pair object in CL for the pair P1 and P2. If
264 one isn't found, return NULL if CREATE is false, otherwise create a new
265 coalesce pair object and return it. */
267 static coalesce_pair_p
268 find_coalesce_pair (coalesce_list_p cl
, int p1
, int p2
, bool create
)
270 struct coalesce_pair p
;
271 coalesce_pair
**slot
;
274 /* Normalize so that p1 is the smaller value. */
277 p
.first_element
= p2
;
278 p
.second_element
= p1
;
282 p
.first_element
= p1
;
283 p
.second_element
= p2
;
286 hash
= coalesce_pair_hasher::hash (&p
);
287 slot
= cl
->list
.find_slot_with_hash (&p
, hash
, create
? INSERT
: NO_INSERT
);
293 struct coalesce_pair
* pair
= XNEW (struct coalesce_pair
);
294 gcc_assert (cl
->sorted
== NULL
);
295 pair
->first_element
= p
.first_element
;
296 pair
->second_element
= p
.second_element
;
301 return (struct coalesce_pair
*) *slot
;
305 add_cost_one_coalesce (coalesce_list_p cl
, int p1
, int p2
)
307 cost_one_pair_p pair
;
309 pair
= XNEW (struct cost_one_pair_d
);
310 pair
->first_element
= p1
;
311 pair
->second_element
= p2
;
312 pair
->next
= cl
->cost_one_list
;
313 cl
->cost_one_list
= pair
;
317 /* Add a coalesce between P1 and P2 in list CL with a cost of VALUE. */
320 add_coalesce (coalesce_list_p cl
, int p1
, int p2
, int value
)
322 coalesce_pair_p node
;
324 gcc_assert (cl
->sorted
== NULL
);
328 node
= find_coalesce_pair (cl
, p1
, p2
, true);
330 /* Once the value is at least MUST_COALESCE_COST - 1, leave it that way. */
331 if (node
->cost
< MUST_COALESCE_COST
- 1)
333 if (value
< MUST_COALESCE_COST
- 1)
341 /* Comparison function to allow qsort to sort P1 and P2 in Ascending order. */
344 compare_pairs (const void *p1
, const void *p2
)
346 const_coalesce_pair_p
const *const pp1
= (const_coalesce_pair_p
const *) p1
;
347 const_coalesce_pair_p
const *const pp2
= (const_coalesce_pair_p
const *) p2
;
350 result
= (* pp1
)->cost
- (* pp2
)->cost
;
351 /* Since qsort does not guarantee stability we use the elements
352 as a secondary key. This provides us with independence from
353 the host's implementation of the sorting algorithm. */
356 result
= (* pp2
)->first_element
- (* pp1
)->first_element
;
358 result
= (* pp2
)->second_element
- (* pp1
)->second_element
;
365 /* Return the number of unique coalesce pairs in CL. */
368 num_coalesce_pairs (coalesce_list_p cl
)
370 return cl
->list
.elements ();
374 /* Iterate over CL using ITER, returning values in PAIR. */
376 #define FOR_EACH_PARTITION_PAIR(PAIR, ITER, CL) \
377 FOR_EACH_HASH_TABLE_ELEMENT ((CL)->list, (PAIR), coalesce_pair_p, (ITER))
380 /* Prepare CL for removal of preferred pairs. When finished they are sorted
381 in order from most important coalesce to least important. */
384 sort_coalesce_list (coalesce_list_p cl
)
388 coalesce_iterator_type ppi
;
390 gcc_assert (cl
->sorted
== NULL
);
392 num
= num_coalesce_pairs (cl
);
393 cl
->num_sorted
= num
;
397 /* Allocate a vector for the pair pointers. */
398 cl
->sorted
= XNEWVEC (coalesce_pair_p
, num
);
400 /* Populate the vector with pointers to the pairs. */
402 FOR_EACH_PARTITION_PAIR (p
, ppi
, cl
)
404 gcc_assert (x
== num
);
406 /* Already sorted. */
410 /* If there are only 2, just pick swap them if the order isn't correct. */
413 if (cl
->sorted
[0]->cost
> cl
->sorted
[1]->cost
)
416 cl
->sorted
[0] = cl
->sorted
[1];
422 /* Only call qsort if there are more than 2 items. */
424 qsort (cl
->sorted
, num
, sizeof (coalesce_pair_p
), compare_pairs
);
428 /* Send debug info for coalesce list CL to file F. */
431 dump_coalesce_list (FILE *f
, coalesce_list_p cl
)
433 coalesce_pair_p node
;
434 coalesce_iterator_type ppi
;
439 if (cl
->sorted
== NULL
)
441 fprintf (f
, "Coalesce List:\n");
442 FOR_EACH_PARTITION_PAIR (node
, ppi
, cl
)
444 tree var1
= ssa_name (node
->first_element
);
445 tree var2
= ssa_name (node
->second_element
);
446 print_generic_expr (f
, var1
, TDF_SLIM
);
447 fprintf (f
, " <-> ");
448 print_generic_expr (f
, var2
, TDF_SLIM
);
449 fprintf (f
, " (%1d), ", node
->cost
);
455 fprintf (f
, "Sorted Coalesce list:\n");
456 for (x
= cl
->num_sorted
- 1 ; x
>=0; x
--)
458 node
= cl
->sorted
[x
];
459 fprintf (f
, "(%d) ", node
->cost
);
460 var
= ssa_name (node
->first_element
);
461 print_generic_expr (f
, var
, TDF_SLIM
);
462 fprintf (f
, " <-> ");
463 var
= ssa_name (node
->second_element
);
464 print_generic_expr (f
, var
, TDF_SLIM
);
471 /* This represents a conflict graph. Implemented as an array of bitmaps.
472 A full matrix is used for conflicts rather than just upper triangular form.
473 this make sit much simpler and faster to perform conflict merges. */
475 typedef struct ssa_conflicts_d
477 bitmap_obstack obstack
; /* A place to allocate our bitmaps. */
478 vec
<bitmap
> conflicts
;
481 /* Return an empty new conflict graph for SIZE elements. */
483 static inline ssa_conflicts_p
484 ssa_conflicts_new (unsigned size
)
488 ptr
= XNEW (struct ssa_conflicts_d
);
489 bitmap_obstack_initialize (&ptr
->obstack
);
490 ptr
->conflicts
.create (size
);
491 ptr
->conflicts
.safe_grow_cleared (size
);
496 /* Free storage for conflict graph PTR. */
499 ssa_conflicts_delete (ssa_conflicts_p ptr
)
501 bitmap_obstack_release (&ptr
->obstack
);
502 ptr
->conflicts
.release ();
507 /* Test if elements X and Y conflict in graph PTR. */
510 ssa_conflicts_test_p (ssa_conflicts_p ptr
, unsigned x
, unsigned y
)
512 bitmap bx
= ptr
->conflicts
[x
];
513 bitmap by
= ptr
->conflicts
[y
];
515 gcc_checking_assert (x
!= y
);
518 /* Avoid the lookup if Y has no conflicts. */
519 return by
? bitmap_bit_p (bx
, y
) : false;
525 /* Add a conflict with Y to the bitmap for X in graph PTR. */
528 ssa_conflicts_add_one (ssa_conflicts_p ptr
, unsigned x
, unsigned y
)
530 bitmap bx
= ptr
->conflicts
[x
];
531 /* If there are no conflicts yet, allocate the bitmap and set bit. */
533 bx
= ptr
->conflicts
[x
] = BITMAP_ALLOC (&ptr
->obstack
);
534 bitmap_set_bit (bx
, y
);
538 /* Add conflicts between X and Y in graph PTR. */
541 ssa_conflicts_add (ssa_conflicts_p ptr
, unsigned x
, unsigned y
)
543 gcc_checking_assert (x
!= y
);
544 ssa_conflicts_add_one (ptr
, x
, y
);
545 ssa_conflicts_add_one (ptr
, y
, x
);
549 /* Merge all Y's conflict into X in graph PTR. */
552 ssa_conflicts_merge (ssa_conflicts_p ptr
, unsigned x
, unsigned y
)
556 bitmap bx
= ptr
->conflicts
[x
];
557 bitmap by
= ptr
->conflicts
[y
];
559 gcc_checking_assert (x
!= y
);
563 /* Add a conflict between X and every one Y has. If the bitmap doesn't
564 exist, then it has already been coalesced, and we don't need to add a
566 EXECUTE_IF_SET_IN_BITMAP (by
, 0, z
, bi
)
568 bitmap bz
= ptr
->conflicts
[z
];
570 bitmap_set_bit (bz
, x
);
575 /* If X has conflicts, add Y's to X. */
576 bitmap_ior_into (bx
, by
);
578 ptr
->conflicts
[y
] = NULL
;
582 /* If X has no conflicts, simply use Y's. */
583 ptr
->conflicts
[x
] = by
;
584 ptr
->conflicts
[y
] = NULL
;
589 /* Dump a conflicts graph. */
592 ssa_conflicts_dump (FILE *file
, ssa_conflicts_p ptr
)
597 fprintf (file
, "\nConflict graph:\n");
599 FOR_EACH_VEC_ELT (ptr
->conflicts
, x
, b
)
602 fprintf (file
, "%d: ", x
);
603 dump_bitmap (file
, b
);
608 /* This structure is used to efficiently record the current status of live
609 SSA_NAMES when building a conflict graph.
610 LIVE_BASE_VAR has a bit set for each base variable which has at least one
612 LIVE_BASE_PARTITIONS is an array of bitmaps using the basevar table as an
613 index, and is used to track what partitions of each base variable are
614 live. This makes it easy to add conflicts between just live partitions
615 with the same base variable.
616 The values in LIVE_BASE_PARTITIONS are only valid if the base variable is
617 marked as being live. This delays clearing of these bitmaps until
618 they are actually needed again. */
620 typedef struct live_track_d
622 bitmap_obstack obstack
; /* A place to allocate our bitmaps. */
623 bitmap live_base_var
; /* Indicates if a basevar is live. */
624 bitmap
*live_base_partitions
; /* Live partitions for each basevar. */
625 var_map map
; /* Var_map being used for partition mapping. */
629 /* This routine will create a new live track structure based on the partitions
633 new_live_track (var_map map
)
638 /* Make sure there is a partition view in place. */
639 gcc_assert (map
->partition_to_base_index
!= NULL
);
641 ptr
= (live_track_p
) xmalloc (sizeof (struct live_track_d
));
643 lim
= num_basevars (map
);
644 bitmap_obstack_initialize (&ptr
->obstack
);
645 ptr
->live_base_partitions
= (bitmap
*) xmalloc (sizeof (bitmap
*) * lim
);
646 ptr
->live_base_var
= BITMAP_ALLOC (&ptr
->obstack
);
647 for (x
= 0; x
< lim
; x
++)
648 ptr
->live_base_partitions
[x
] = BITMAP_ALLOC (&ptr
->obstack
);
653 /* This routine will free the memory associated with PTR. */
656 delete_live_track (live_track_p ptr
)
658 bitmap_obstack_release (&ptr
->obstack
);
659 free (ptr
->live_base_partitions
);
664 /* This function will remove PARTITION from the live list in PTR. */
667 live_track_remove_partition (live_track_p ptr
, int partition
)
671 root
= basevar_index (ptr
->map
, partition
);
672 bitmap_clear_bit (ptr
->live_base_partitions
[root
], partition
);
673 /* If the element list is empty, make the base variable not live either. */
674 if (bitmap_empty_p (ptr
->live_base_partitions
[root
]))
675 bitmap_clear_bit (ptr
->live_base_var
, root
);
679 /* This function will adds PARTITION to the live list in PTR. */
682 live_track_add_partition (live_track_p ptr
, int partition
)
686 root
= basevar_index (ptr
->map
, partition
);
687 /* If this base var wasn't live before, it is now. Clear the element list
688 since it was delayed until needed. */
689 if (bitmap_set_bit (ptr
->live_base_var
, root
))
690 bitmap_clear (ptr
->live_base_partitions
[root
]);
691 bitmap_set_bit (ptr
->live_base_partitions
[root
], partition
);
696 /* Clear the live bit for VAR in PTR. */
699 live_track_clear_var (live_track_p ptr
, tree var
)
703 p
= var_to_partition (ptr
->map
, var
);
704 if (p
!= NO_PARTITION
)
705 live_track_remove_partition (ptr
, p
);
709 /* Return TRUE if VAR is live in PTR. */
712 live_track_live_p (live_track_p ptr
, tree var
)
716 p
= var_to_partition (ptr
->map
, var
);
717 if (p
!= NO_PARTITION
)
719 root
= basevar_index (ptr
->map
, p
);
720 if (bitmap_bit_p (ptr
->live_base_var
, root
))
721 return bitmap_bit_p (ptr
->live_base_partitions
[root
], p
);
727 /* This routine will add USE to PTR. USE will be marked as live in both the
728 ssa live map and the live bitmap for the root of USE. */
731 live_track_process_use (live_track_p ptr
, tree use
)
735 p
= var_to_partition (ptr
->map
, use
);
736 if (p
== NO_PARTITION
)
739 /* Mark as live in the appropriate live list. */
740 live_track_add_partition (ptr
, p
);
744 /* This routine will process a DEF in PTR. DEF will be removed from the live
745 lists, and if there are any other live partitions with the same base
746 variable, conflicts will be added to GRAPH. */
749 live_track_process_def (live_track_p ptr
, tree def
, ssa_conflicts_p graph
)
756 p
= var_to_partition (ptr
->map
, def
);
757 if (p
== NO_PARTITION
)
760 /* Clear the liveness bit. */
761 live_track_remove_partition (ptr
, p
);
763 /* If the bitmap isn't empty now, conflicts need to be added. */
764 root
= basevar_index (ptr
->map
, p
);
765 if (bitmap_bit_p (ptr
->live_base_var
, root
))
767 b
= ptr
->live_base_partitions
[root
];
768 EXECUTE_IF_SET_IN_BITMAP (b
, 0, x
, bi
)
769 ssa_conflicts_add (graph
, p
, x
);
774 /* Initialize PTR with the partitions set in INIT. */
777 live_track_init (live_track_p ptr
, bitmap init
)
782 /* Mark all live on exit partitions. */
783 EXECUTE_IF_SET_IN_BITMAP (init
, 0, p
, bi
)
784 live_track_add_partition (ptr
, p
);
788 /* This routine will clear all live partitions in PTR. */
791 live_track_clear_base_vars (live_track_p ptr
)
793 /* Simply clear the live base list. Anything marked as live in the element
794 lists will be cleared later if/when the base variable ever comes alive
796 bitmap_clear (ptr
->live_base_var
);
800 /* Build a conflict graph based on LIVEINFO. Any partitions which are in the
801 partition view of the var_map liveinfo is based on get entries in the
802 conflict graph. Only conflicts between ssa_name partitions with the same
803 base variable are added. */
805 static ssa_conflicts_p
806 build_ssa_conflict_graph (tree_live_info_p liveinfo
)
808 ssa_conflicts_p graph
;
814 map
= live_var_map (liveinfo
);
815 graph
= ssa_conflicts_new (num_var_partitions (map
));
817 live
= new_live_track (map
);
821 gimple_stmt_iterator gsi
;
823 /* Start with live on exit temporaries. */
824 live_track_init (live
, live_on_exit (liveinfo
, bb
));
826 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
829 gimple stmt
= gsi_stmt (gsi
);
831 /* A copy between 2 partitions does not introduce an interference
832 by itself. If they did, you would never be able to coalesce
833 two things which are copied. If the two variables really do
834 conflict, they will conflict elsewhere in the program.
836 This is handled by simply removing the SRC of the copy from the
837 live list, and processing the stmt normally. */
838 if (is_gimple_assign (stmt
))
840 tree lhs
= gimple_assign_lhs (stmt
);
841 tree rhs1
= gimple_assign_rhs1 (stmt
);
842 if (gimple_assign_copy_p (stmt
)
843 && TREE_CODE (lhs
) == SSA_NAME
844 && TREE_CODE (rhs1
) == SSA_NAME
)
845 live_track_clear_var (live
, rhs1
);
847 else if (is_gimple_debug (stmt
))
850 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, iter
, SSA_OP_DEF
)
851 live_track_process_def (live
, var
, graph
);
853 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, iter
, SSA_OP_USE
)
854 live_track_process_use (live
, var
);
857 /* If result of a PHI is unused, looping over the statements will not
858 record any conflicts since the def was never live. Since the PHI node
859 is going to be translated out of SSA form, it will insert a copy.
860 There must be a conflict recorded between the result of the PHI and
861 any variables that are live. Otherwise the out-of-ssa translation
862 may create incorrect code. */
863 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
865 gimple phi
= gsi_stmt (gsi
);
866 tree result
= PHI_RESULT (phi
);
867 if (live_track_live_p (live
, result
))
868 live_track_process_def (live
, result
, graph
);
871 live_track_clear_base_vars (live
);
874 delete_live_track (live
);
879 /* Shortcut routine to print messages to file F of the form:
880 "STR1 EXPR1 STR2 EXPR2 STR3." */
883 print_exprs (FILE *f
, const char *str1
, tree expr1
, const char *str2
,
884 tree expr2
, const char *str3
)
886 fprintf (f
, "%s", str1
);
887 print_generic_expr (f
, expr1
, TDF_SLIM
);
888 fprintf (f
, "%s", str2
);
889 print_generic_expr (f
, expr2
, TDF_SLIM
);
890 fprintf (f
, "%s", str3
);
894 /* Print a failure to coalesce a MUST_COALESCE pair X and Y. */
897 fail_abnormal_edge_coalesce (int x
, int y
)
899 fprintf (stderr
, "\nUnable to coalesce ssa_names %d and %d",x
, y
);
900 fprintf (stderr
, " which are marked as MUST COALESCE.\n");
901 print_generic_expr (stderr
, ssa_name (x
), TDF_SLIM
);
902 fprintf (stderr
, " and ");
903 print_generic_stmt (stderr
, ssa_name (y
), TDF_SLIM
);
905 internal_error ("SSA corruption");
909 /* This function creates a var_map for the current function as well as creating
910 a coalesce list for use later in the out of ssa process. */
913 create_outofssa_var_map (coalesce_list_p cl
, bitmap used_in_copy
)
915 gimple_stmt_iterator gsi
;
925 map
= init_var_map (num_ssa_names
);
931 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
933 gimple phi
= gsi_stmt (gsi
);
937 bool saw_copy
= false;
939 res
= gimple_phi_result (phi
);
940 ver
= SSA_NAME_VERSION (res
);
941 register_ssa_partition (map
, res
);
943 /* Register ssa_names and coalesces between the args and the result
945 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
947 edge e
= gimple_phi_arg_edge (phi
, i
);
948 arg
= PHI_ARG_DEF (phi
, i
);
949 if (TREE_CODE (arg
) != SSA_NAME
)
952 register_ssa_partition (map
, arg
);
953 if (gimple_can_coalesce_p (arg
, res
)
954 || (e
->flags
& EDGE_ABNORMAL
))
957 bitmap_set_bit (used_in_copy
, SSA_NAME_VERSION (arg
));
958 if ((e
->flags
& EDGE_ABNORMAL
) == 0)
960 int cost
= coalesce_cost_edge (e
);
961 if (cost
== 1 && has_single_use (arg
))
962 add_cost_one_coalesce (cl
, ver
, SSA_NAME_VERSION (arg
));
964 add_coalesce (cl
, ver
, SSA_NAME_VERSION (arg
), cost
);
969 bitmap_set_bit (used_in_copy
, ver
);
972 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
974 stmt
= gsi_stmt (gsi
);
976 if (is_gimple_debug (stmt
))
979 /* Register USE and DEF operands in each statement. */
980 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, iter
, (SSA_OP_DEF
|SSA_OP_USE
))
981 register_ssa_partition (map
, var
);
983 /* Check for copy coalesces. */
984 switch (gimple_code (stmt
))
988 tree lhs
= gimple_assign_lhs (stmt
);
989 tree rhs1
= gimple_assign_rhs1 (stmt
);
990 if (gimple_assign_ssa_name_copy_p (stmt
)
991 && gimple_can_coalesce_p (lhs
, rhs1
))
993 v1
= SSA_NAME_VERSION (lhs
);
994 v2
= SSA_NAME_VERSION (rhs1
);
995 cost
= coalesce_cost_bb (bb
);
996 add_coalesce (cl
, v1
, v2
, cost
);
997 bitmap_set_bit (used_in_copy
, v1
);
998 bitmap_set_bit (used_in_copy
, v2
);
1005 unsigned long noutputs
, i
;
1006 unsigned long ninputs
;
1007 tree
*outputs
, link
;
1008 noutputs
= gimple_asm_noutputs (stmt
);
1009 ninputs
= gimple_asm_ninputs (stmt
);
1010 outputs
= (tree
*) alloca (noutputs
* sizeof (tree
));
1011 for (i
= 0; i
< noutputs
; ++i
)
1013 link
= gimple_asm_output_op (stmt
, i
);
1014 outputs
[i
] = TREE_VALUE (link
);
1017 for (i
= 0; i
< ninputs
; ++i
)
1019 const char *constraint
;
1022 unsigned long match
;
1024 link
= gimple_asm_input_op (stmt
, i
);
1026 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
1027 input
= TREE_VALUE (link
);
1029 if (TREE_CODE (input
) != SSA_NAME
)
1032 match
= strtoul (constraint
, &end
, 10);
1033 if (match
>= noutputs
|| end
== constraint
)
1036 if (TREE_CODE (outputs
[match
]) != SSA_NAME
)
1039 v1
= SSA_NAME_VERSION (outputs
[match
]);
1040 v2
= SSA_NAME_VERSION (input
);
1042 if (gimple_can_coalesce_p (outputs
[match
], input
))
1044 cost
= coalesce_cost (REG_BR_PROB_BASE
,
1045 optimize_bb_for_size_p (bb
));
1046 add_coalesce (cl
, v1
, v2
, cost
);
1047 bitmap_set_bit (used_in_copy
, v1
);
1048 bitmap_set_bit (used_in_copy
, v2
);
1060 /* Now process result decls and live on entry variables for entry into
1061 the coalesce list. */
1063 for (i
= 1; i
< num_ssa_names
; i
++)
1066 if (var
!= NULL_TREE
&& !virtual_operand_p (var
))
1068 /* Add coalesces between all the result decls. */
1069 if (SSA_NAME_VAR (var
)
1070 && TREE_CODE (SSA_NAME_VAR (var
)) == RESULT_DECL
)
1072 if (first
== NULL_TREE
)
1076 gcc_assert (gimple_can_coalesce_p (var
, first
));
1077 v1
= SSA_NAME_VERSION (first
);
1078 v2
= SSA_NAME_VERSION (var
);
1079 bitmap_set_bit (used_in_copy
, v1
);
1080 bitmap_set_bit (used_in_copy
, v2
);
1081 cost
= coalesce_cost_bb (EXIT_BLOCK_PTR
);
1082 add_coalesce (cl
, v1
, v2
, cost
);
1085 /* Mark any default_def variables as being in the coalesce list
1086 since they will have to be coalesced with the base variable. If
1087 not marked as present, they won't be in the coalesce view. */
1088 if (SSA_NAME_IS_DEFAULT_DEF (var
)
1089 && !has_zero_uses (var
))
1090 bitmap_set_bit (used_in_copy
, SSA_NAME_VERSION (var
));
1098 /* Attempt to coalesce ssa versions X and Y together using the partition
1099 mapping in MAP and checking conflicts in GRAPH. Output any debug info to
1100 DEBUG, if it is nun-NULL. */
1103 attempt_coalesce (var_map map
, ssa_conflicts_p graph
, int x
, int y
,
1110 p1
= var_to_partition (map
, ssa_name (x
));
1111 p2
= var_to_partition (map
, ssa_name (y
));
1115 fprintf (debug
, "(%d)", x
);
1116 print_generic_expr (debug
, partition_to_var (map
, p1
), TDF_SLIM
);
1117 fprintf (debug
, " & (%d)", y
);
1118 print_generic_expr (debug
, partition_to_var (map
, p2
), TDF_SLIM
);
1124 fprintf (debug
, ": Already Coalesced.\n");
1129 fprintf (debug
, " [map: %d, %d] ", p1
, p2
);
1132 if (!ssa_conflicts_test_p (graph
, p1
, p2
))
1134 var1
= partition_to_var (map
, p1
);
1135 var2
= partition_to_var (map
, p2
);
1136 z
= var_union (map
, var1
, var2
);
1137 if (z
== NO_PARTITION
)
1140 fprintf (debug
, ": Unable to perform partition union.\n");
1144 /* z is the new combined partition. Remove the other partition from
1145 the list, and merge the conflicts. */
1147 ssa_conflicts_merge (graph
, p1
, p2
);
1149 ssa_conflicts_merge (graph
, p2
, p1
);
1152 fprintf (debug
, ": Success -> %d\n", z
);
1157 fprintf (debug
, ": Fail due to conflict\n");
1163 /* Attempt to Coalesce partitions in MAP which occur in the list CL using
1164 GRAPH. Debug output is sent to DEBUG if it is non-NULL. */
1167 coalesce_partitions (var_map map
, ssa_conflicts_p graph
, coalesce_list_p cl
,
1177 /* First, coalesce all the copies across abnormal edges. These are not placed
1178 in the coalesce list because they do not need to be sorted, and simply
1179 consume extra memory/compilation time in large programs. */
1183 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1184 if (e
->flags
& EDGE_ABNORMAL
)
1186 gimple_stmt_iterator gsi
;
1187 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
1190 gimple phi
= gsi_stmt (gsi
);
1191 tree res
= PHI_RESULT (phi
);
1192 tree arg
= PHI_ARG_DEF (phi
, e
->dest_idx
);
1193 int v1
= SSA_NAME_VERSION (res
);
1194 int v2
= SSA_NAME_VERSION (arg
);
1197 fprintf (debug
, "Abnormal coalesce: ");
1199 if (!attempt_coalesce (map
, graph
, v1
, v2
, debug
))
1200 fail_abnormal_edge_coalesce (v1
, v2
);
1205 /* Now process the items in the coalesce list. */
1207 while ((cost
= pop_best_coalesce (cl
, &x
, &y
)) != NO_BEST_COALESCE
)
1209 var1
= ssa_name (x
);
1210 var2
= ssa_name (y
);
1212 /* Assert the coalesces have the same base variable. */
1213 gcc_assert (gimple_can_coalesce_p (var1
, var2
));
1216 fprintf (debug
, "Coalesce list: ");
1217 attempt_coalesce (map
, graph
, x
, y
, debug
);
1222 /* Hashtable support for storing SSA names hashed by their SSA_NAME_VAR. */
1224 struct ssa_name_var_hash
: typed_noop_remove
<tree_node
>
1226 typedef union tree_node value_type
;
1227 typedef union tree_node compare_type
;
1228 static inline hashval_t
hash (const value_type
*);
1229 static inline int equal (const value_type
*, const compare_type
*);
1233 ssa_name_var_hash::hash (const_tree n
)
1235 return DECL_UID (SSA_NAME_VAR (n
));
1239 ssa_name_var_hash::equal (const value_type
*n1
, const compare_type
*n2
)
1241 return SSA_NAME_VAR (n1
) == SSA_NAME_VAR (n2
);
1245 /* Reduce the number of copies by coalescing variables in the function. Return
1246 a partition map with the resulting coalesces. */
1249 coalesce_ssa_name (void)
1251 tree_live_info_p liveinfo
;
1252 ssa_conflicts_p graph
;
1254 bitmap used_in_copies
= BITMAP_ALLOC (NULL
);
1258 cl
= create_coalesce_list ();
1259 map
= create_outofssa_var_map (cl
, used_in_copies
);
1261 /* If optimization is disabled, we need to coalesce all the names originating
1262 from the same SSA_NAME_VAR so debug info remains undisturbed. */
1265 hash_table
<ssa_name_var_hash
> ssa_name_hash
;
1267 ssa_name_hash
.create (10);
1268 for (i
= 1; i
< num_ssa_names
; i
++)
1270 tree a
= ssa_name (i
);
1274 && !DECL_IGNORED_P (SSA_NAME_VAR (a
))
1275 && (!has_zero_uses (a
) || !SSA_NAME_IS_DEFAULT_DEF (a
)))
1277 tree
*slot
= ssa_name_hash
.find_slot (a
, INSERT
);
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. */
1289 = TREE_CODE (SSA_NAME_VAR (a
)) == VAR_DECL
1290 ? MUST_COALESCE_COST
- 1 : MUST_COALESCE_COST
;
1291 add_coalesce (cl
, SSA_NAME_VERSION (a
),
1292 SSA_NAME_VERSION (*slot
), cost
);
1293 bitmap_set_bit (used_in_copies
, SSA_NAME_VERSION (a
));
1294 bitmap_set_bit (used_in_copies
, SSA_NAME_VERSION (*slot
));
1298 ssa_name_hash
.dispose ();
1300 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1301 dump_var_map (dump_file
, map
);
1303 /* Don't calculate live ranges for variables not in the coalesce list. */
1304 partition_view_bitmap (map
, used_in_copies
, true);
1305 BITMAP_FREE (used_in_copies
);
1307 if (num_var_partitions (map
) < 1)
1309 delete_coalesce_list (cl
);
1313 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1314 dump_var_map (dump_file
, map
);
1316 liveinfo
= calculate_live_ranges (map
);
1318 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1319 dump_live_info (dump_file
, liveinfo
, LIVEDUMP_ENTRY
);
1321 /* Build a conflict graph. */
1322 graph
= build_ssa_conflict_graph (liveinfo
);
1323 delete_tree_live_info (liveinfo
);
1324 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1325 ssa_conflicts_dump (dump_file
, graph
);
1327 sort_coalesce_list (cl
);
1329 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1331 fprintf (dump_file
, "\nAfter sorting:\n");
1332 dump_coalesce_list (dump_file
, cl
);
1335 /* First, coalesce all live on entry variables to their base variable.
1336 This will ensure the first use is coming from the correct location. */
1338 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1339 dump_var_map (dump_file
, map
);
1341 /* Now coalesce everything in the list. */
1342 coalesce_partitions (map
, graph
, cl
,
1343 ((dump_flags
& TDF_DETAILS
) ? dump_file
1346 delete_coalesce_list (cl
);
1347 ssa_conflicts_delete (graph
);