1 /* Convert a program in SSA form into Normal form.
2 Copyright (C) 2004 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 2, 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 COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "coretypes.h"
31 #include "langhooks.h"
32 #include "hard-reg-set.h"
33 #include "basic-block.h"
38 #include "diagnostic.h"
40 #include "tree-flow.h"
41 #include "tree-gimple.h"
42 #include "tree-inline.h"
45 #include "tree-alias-common.h"
47 #include "tree-dump.h"
48 #include "tree-ssa-live.h"
49 #include "tree-pass.h"
51 /* Flags to pass to remove_ssa_form. */
53 #define SSANORM_PERFORM_TER 0x1
54 #define SSANORM_COMBINE_TEMPS 0x2
55 #define SSANORM_REMOVE_ALL_PHIS 0x4
56 #define SSANORM_COALESCE_PARTITIONS 0x8
57 #define SSANORM_USE_COALESCE_LIST 0x10
59 /* Used to hold all the components required to do SSA PHI elimination.
60 The node and pred/succ list is a simple linear list of nodes and
61 edges represented as pairs of nodes.
63 The predecessor and successor list: Nodes are entered in pairs, where
64 [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
65 predecessors, all the odd elements are successors.
68 When implemented as bitmaps, very large programs SSA->Normal times were
69 being dominated by clearing the interference graph.
71 Typically this list of edges is extremely small since it only includes
72 PHI results and uses from a single edge which have not coalesced with
73 each other. This means that no virtual PHI nodes are included, and
74 empirical evidence suggests that the number of edges rarely exceed
75 3, and in a bootstrap of GCC, the maximum size encountered was 7.
76 This also limits the number of possible nodes that are involved to
77 rarely more than 6, and in the bootstrap of gcc, the maximum number
78 of nodes encountered was 12. */
80 typedef struct _elim_graph
{
81 /* Size of the elimination vectors. */
84 /* List of nodes in the elimination graph. */
87 /* The predecessor and successor edge list. */
88 varray_type edge_list
;
93 /* Stack for visited nodes. */
96 /* The variable partition map. */
99 /* Edge being eliminated by this graph. */
102 /* List of constant copies to emit. These are pushed on in pairs. */
103 varray_type const_copies
;
107 /* Local functions. */
108 static tree
create_temp (tree
);
109 static void insert_copy_on_edge (edge
, tree
, tree
);
110 static elim_graph
new_elim_graph (int);
111 static inline void delete_elim_graph (elim_graph
);
112 static inline void clear_elim_graph (elim_graph
);
113 static inline int elim_graph_size (elim_graph
);
114 static inline void elim_graph_add_node (elim_graph
, tree
);
115 static inline void elim_graph_add_edge (elim_graph
, int, int);
116 static inline int elim_graph_remove_succ_edge (elim_graph
, int);
118 static inline void eliminate_name (elim_graph
, tree
);
119 static void eliminate_build (elim_graph
, basic_block
, int);
120 static void elim_forward (elim_graph
, int);
121 static int elim_unvisited_predecessor (elim_graph
, int);
122 static void elim_backward (elim_graph
, int);
123 static void elim_create (elim_graph
, int);
124 static void eliminate_phi (edge
, int, elim_graph
);
125 static tree_live_info_p
coalesce_ssa_name (var_map
, int);
126 static void assign_vars (var_map
);
127 static bool replace_use_variable (var_map
, use_operand_p
, tree
*);
128 static bool replace_def_variable (var_map
, def_operand_p
, tree
*);
129 static void eliminate_virtual_phis (void);
130 static void coalesce_abnormal_edges (var_map
, conflict_graph
, root_var_p
);
131 static void print_exprs (FILE *, const char *, tree
, const char *, tree
,
133 static void print_exprs_edge (FILE *, edge
, const char *, tree
, const char *,
137 /* Create a temporary variable based on the type of variable T. Use T's name
144 const char *name
= NULL
;
147 if (TREE_CODE (t
) == SSA_NAME
)
148 t
= SSA_NAME_VAR (t
);
150 if (TREE_CODE (t
) != VAR_DECL
151 && TREE_CODE (t
) != PARM_DECL
)
154 type
= TREE_TYPE (t
);
157 name
= IDENTIFIER_POINTER (tmp
);
161 tmp
= create_tmp_var (type
, name
);
162 DECL_ARTIFICIAL (tmp
) = DECL_ARTIFICIAL (t
);
163 add_referenced_tmp_var (tmp
);
165 /* add_referenced_tmp_var will create the annotation and set up some
166 of the flags in the annotation. However, some flags we need to
167 inherit from our original variable. */
168 var_ann (tmp
)->type_mem_tag
= var_ann (t
)->type_mem_tag
;
169 if (is_call_clobbered (t
))
170 mark_call_clobbered (tmp
);
176 /* This helper function fill insert a copy from a constant or variable SRC to
177 variable DEST on edge E. */
180 insert_copy_on_edge (edge e
, tree dest
, tree src
)
184 copy
= build (MODIFY_EXPR
, TREE_TYPE (dest
), dest
, src
);
187 if (TREE_CODE (src
) == ADDR_EXPR
)
188 src
= TREE_OPERAND (src
, 0);
189 if (TREE_CODE (src
) == VAR_DECL
|| TREE_CODE (src
) == PARM_DECL
)
192 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
195 "Inserting a copy on edge BB%d->BB%d :",
198 print_generic_expr (dump_file
, copy
, dump_flags
);
199 fprintf (dump_file
, "\n");
202 bsi_insert_on_edge (e
, copy
);
206 /* Create an elimination graph with SIZE nodes and associated data
210 new_elim_graph (int size
)
212 elim_graph g
= (elim_graph
) xmalloc (sizeof (struct _elim_graph
));
214 VARRAY_TREE_INIT (g
->nodes
, 30, "Elimination Node List");
215 VARRAY_TREE_INIT (g
->const_copies
, 20, "Elimination Constant Copies");
216 VARRAY_INT_INIT (g
->edge_list
, 20, "Elimination Edge List");
217 VARRAY_INT_INIT (g
->stack
, 30, " Elimination Stack");
219 g
->visited
= sbitmap_alloc (size
);
225 /* Empty elimination graph G. */
228 clear_elim_graph (elim_graph g
)
230 VARRAY_POP_ALL (g
->nodes
);
231 VARRAY_POP_ALL (g
->edge_list
);
235 /* Delete elimination graph G. */
238 delete_elim_graph (elim_graph g
)
240 sbitmap_free (g
->visited
);
245 /* Return the number of nodes in graph G. */
248 elim_graph_size (elim_graph g
)
250 return VARRAY_ACTIVE_SIZE (g
->nodes
);
254 /* Add NODE to graph G, if it doesn't exist already. */
257 elim_graph_add_node (elim_graph g
, tree node
)
260 for (x
= 0; x
< elim_graph_size (g
); x
++)
261 if (VARRAY_TREE (g
->nodes
, x
) == node
)
263 VARRAY_PUSH_TREE (g
->nodes
, node
);
267 /* Add the edge PRED->SUCC to graph G. */
270 elim_graph_add_edge (elim_graph g
, int pred
, int succ
)
272 VARRAY_PUSH_INT (g
->edge_list
, pred
);
273 VARRAY_PUSH_INT (g
->edge_list
, succ
);
277 /* Remove an edge from graph G for which NODE is the predecessor, and
278 return the successor node. -1 is returned if there is no such edge. */
281 elim_graph_remove_succ_edge (elim_graph g
, int node
)
285 for (x
= 0; x
< VARRAY_ACTIVE_SIZE (g
->edge_list
); x
+= 2)
286 if (VARRAY_INT (g
->edge_list
, x
) == node
)
288 VARRAY_INT (g
->edge_list
, x
) = -1;
289 y
= VARRAY_INT (g
->edge_list
, x
+ 1);
290 VARRAY_INT (g
->edge_list
, x
+ 1) = -1;
297 /* Find all the nodes in GRAPH which are successors to NODE in the
298 edge list. VAR will hold the partition number found. CODE is the
299 code fragment executed for every node found. */
301 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, CODE) \
305 for (x_ = 0; x_ < VARRAY_ACTIVE_SIZE ((GRAPH)->edge_list); x_ += 2) \
307 y_ = VARRAY_INT ((GRAPH)->edge_list, x_); \
310 (VAR) = VARRAY_INT ((GRAPH)->edge_list, x_ + 1); \
316 /* Find all the nodes which are predecessors of NODE in the edge list for
317 GRAPH. VAR will hold the partition number found. CODE is the
318 code fragment executed for every node found. */
320 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, CODE) \
324 for (x_ = 0; x_ < VARRAY_ACTIVE_SIZE ((GRAPH)->edge_list); x_ += 2) \
326 y_ = VARRAY_INT ((GRAPH)->edge_list, x_ + 1); \
329 (VAR) = VARRAY_INT ((GRAPH)->edge_list, x_); \
335 /* Add T to elimination graph G. */
338 eliminate_name (elim_graph g
, tree T
)
340 elim_graph_add_node (g
, T
);
344 /* Build elimination graph G for basic block BB on incoming PHI edge I. */
347 eliminate_build (elim_graph g
, basic_block B
, int i
)
353 clear_elim_graph (g
);
355 for (phi
= phi_nodes (B
); phi
; phi
= PHI_CHAIN (phi
))
357 T0
= var_to_partition_to_var (g
->map
, PHI_RESULT (phi
));
359 /* Ignore results which are not in partitions. */
363 if (PHI_ARG_EDGE (phi
, i
) == g
->e
)
364 Ti
= PHI_ARG_DEF (phi
, i
);
367 /* On rare occasions, a PHI node may not have the arguments
368 in the same order as all of the other PHI nodes. If they don't
369 match, find the appropriate index here. */
370 pi
= phi_arg_from_edge (phi
, g
->e
);
373 Ti
= PHI_ARG_DEF (phi
, pi
);
376 /* If this argument is a constant, or a SSA_NAME which is being
377 left in SSA form, just queue a copy to be emitted on this
379 if (!phi_ssa_name_p (Ti
)
380 || (TREE_CODE (Ti
) == SSA_NAME
381 && var_to_partition (g
->map
, Ti
) == NO_PARTITION
))
383 /* Save constant copies until all other copies have been emitted
385 VARRAY_PUSH_TREE (g
->const_copies
, T0
);
386 VARRAY_PUSH_TREE (g
->const_copies
, Ti
);
390 Ti
= var_to_partition_to_var (g
->map
, Ti
);
393 eliminate_name (g
, T0
);
394 eliminate_name (g
, Ti
);
395 p0
= var_to_partition (g
->map
, T0
);
396 pi
= var_to_partition (g
->map
, Ti
);
397 elim_graph_add_edge (g
, p0
, pi
);
404 /* Push successors of T onto the elimination stack for G. */
407 elim_forward (elim_graph g
, int T
)
410 SET_BIT (g
->visited
, T
);
411 FOR_EACH_ELIM_GRAPH_SUCC (g
, T
, S
,
413 if (!TEST_BIT (g
->visited
, S
))
416 VARRAY_PUSH_INT (g
->stack
, T
);
420 /* Return 1 if there unvisited predecessors of T in graph G. */
423 elim_unvisited_predecessor (elim_graph g
, int T
)
426 FOR_EACH_ELIM_GRAPH_PRED (g
, T
, P
,
428 if (!TEST_BIT (g
->visited
, P
))
434 /* Process predecessors first, and insert a copy. */
437 elim_backward (elim_graph g
, int T
)
440 SET_BIT (g
->visited
, T
);
441 FOR_EACH_ELIM_GRAPH_PRED (g
, T
, P
,
443 if (!TEST_BIT (g
->visited
, P
))
445 elim_backward (g
, P
);
446 insert_copy_on_edge (g
->e
,
447 partition_to_var (g
->map
, P
),
448 partition_to_var (g
->map
, T
));
453 /* Insert required copies for T in graph G. Check for a strongly connected
454 region, and create a temporary to break the cycle if one is found. */
457 elim_create (elim_graph g
, int T
)
462 if (elim_unvisited_predecessor (g
, T
))
464 U
= create_temp (partition_to_var (g
->map
, T
));
465 insert_copy_on_edge (g
->e
, U
, partition_to_var (g
->map
, T
));
466 FOR_EACH_ELIM_GRAPH_PRED (g
, T
, P
,
468 if (!TEST_BIT (g
->visited
, P
))
470 elim_backward (g
, P
);
471 insert_copy_on_edge (g
->e
, partition_to_var (g
->map
, P
), U
);
477 S
= elim_graph_remove_succ_edge (g
, T
);
480 SET_BIT (g
->visited
, T
);
481 insert_copy_on_edge (g
->e
,
482 partition_to_var (g
->map
, T
),
483 partition_to_var (g
->map
, S
));
489 /* Eliminate all the phi nodes on edge E in graph G. I is the usual PHI
490 index that edge E's values are found on. */
493 eliminate_phi (edge e
, int i
, elim_graph g
)
497 basic_block B
= e
->dest
;
499 #if defined ENABLE_CHECKING
502 if (VARRAY_ACTIVE_SIZE (g
->const_copies
) != 0)
506 /* Abnormal edges already have everything coalesced, or the coalescer
507 would have aborted. */
508 if (e
->flags
& EDGE_ABNORMAL
)
511 num_nodes
= num_var_partitions (g
->map
);
514 eliminate_build (g
, B
, i
);
516 if (elim_graph_size (g
) != 0)
518 sbitmap_zero (g
->visited
);
519 VARRAY_POP_ALL (g
->stack
);
521 for (x
= 0; x
< elim_graph_size (g
); x
++)
523 tree var
= VARRAY_TREE (g
->nodes
, x
);
524 int p
= var_to_partition (g
->map
, var
);
525 if (!TEST_BIT (g
->visited
, p
))
529 sbitmap_zero (g
->visited
);
530 while (VARRAY_ACTIVE_SIZE (g
->stack
) > 0)
532 x
= VARRAY_TOP_INT (g
->stack
);
533 VARRAY_POP (g
->stack
);
534 if (!TEST_BIT (g
->visited
, x
))
539 /* If there are any pending constant copies, issue them now. */
540 while (VARRAY_ACTIVE_SIZE (g
->const_copies
) > 0)
543 src
= VARRAY_TOP_TREE (g
->const_copies
);
544 VARRAY_POP (g
->const_copies
);
545 dest
= VARRAY_TOP_TREE (g
->const_copies
);
546 VARRAY_POP (g
->const_copies
);
547 insert_copy_on_edge (e
, dest
, src
);
552 /* Shortcut routine to print messages to file F of the form:
553 "STR1 EXPR1 STR2 EXPR2 STR3." */
556 print_exprs (FILE *f
, const char *str1
, tree expr1
, const char *str2
,
557 tree expr2
, const char *str3
)
559 fprintf (f
, "%s", str1
);
560 print_generic_expr (f
, expr1
, TDF_SLIM
);
561 fprintf (f
, "%s", str2
);
562 print_generic_expr (f
, expr2
, TDF_SLIM
);
563 fprintf (f
, "%s", str3
);
567 /* Shortcut routine to print abnormal edge messages to file F of the form:
568 "STR1 EXPR1 STR2 EXPR2 across edge E. */
571 print_exprs_edge (FILE *f
, edge e
, const char *str1
, tree expr1
,
572 const char *str2
, tree expr2
)
574 print_exprs (f
, str1
, expr1
, str2
, expr2
, " across an abnormal edge");
575 fprintf (f
, " from BB%d->BB%d\n", e
->src
->index
,
580 /* Coalesce partitions in MAP which are live across abnormal edges in GRAPH.
581 RV is the root variable groupings of the partitions in MAP. Since code
582 cannot be inserted on these edges, failure to coalesce something across
583 an abnormal edge is an error. */
586 coalesce_abnormal_edges (var_map map
, conflict_graph graph
, root_var_p rv
)
593 /* Code cannot be inserted on abnormal edges. Look for all abnormal
594 edges, and coalesce any PHI results with their arguments across
598 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
599 if (e
->dest
!= EXIT_BLOCK_PTR
&& e
->flags
& EDGE_ABNORMAL
)
600 for (phi
= phi_nodes (e
->dest
); phi
; phi
= PHI_CHAIN (phi
))
602 /* Visit each PHI on the destination side of this abnormal
603 edge, and attempt to coalesce the argument with the result. */
604 var
= PHI_RESULT (phi
);
605 x
= var_to_partition (map
, var
);
607 /* Ignore results which are not relevant. */
608 if (x
== NO_PARTITION
)
611 y
= phi_arg_from_edge (phi
, e
);
615 tmp
= PHI_ARG_DEF (phi
, y
);
616 if (!phi_ssa_name_p (tmp
))
618 print_exprs_edge (stderr
, e
,
619 "\nConstant argument in PHI. Can't insert :",
623 y
= var_to_partition (map
, tmp
);
624 if (x
== NO_PARTITION
|| y
== NO_PARTITION
)
626 if (root_var_find (rv
, x
) != root_var_find (rv
, y
))
628 print_exprs_edge (stderr
, e
, "\nDifferent root vars: ",
629 root_var (rv
, root_var_find (rv
, x
)),
631 root_var (rv
, root_var_find (rv
, y
)));
637 if (!conflict_graph_conflict_p (graph
, x
, y
))
639 /* Now map the partitions back to their real variables. */
640 var
= partition_to_var (map
, x
);
641 tmp
= partition_to_var (map
, y
);
643 && (dump_flags
& TDF_DETAILS
))
645 print_exprs_edge (dump_file
, e
,
646 "ABNORMAL: Coalescing ",
649 if (var_union (map
, var
, tmp
) == NO_PARTITION
)
651 print_exprs_edge (stderr
, e
, "\nUnable to coalesce",
652 partition_to_var (map
, x
), " and ",
653 partition_to_var (map
, y
));
656 conflict_graph_merge_regs (graph
, x
, y
);
660 print_exprs_edge (stderr
, e
, "\n Conflict ",
661 partition_to_var (map
, x
),
662 " and ", partition_to_var (map
, y
));
670 /* Reduce the number of live ranges in MAP. Live range information is
671 returned if FLAGS indicates that we are combining temporaries, otherwise
672 NULL is returned. The only partitions which are associated with actual
673 variables at this point are those which are forced to be coalesced for
674 various reason. (live on entry, live across abnormal edges, etc.). */
676 static tree_live_info_p
677 coalesce_ssa_name (var_map map
, int flags
)
683 tree_live_info_p liveinfo
;
685 conflict_graph graph
;
687 coalesce_list_p cl
= NULL
;
689 if (num_var_partitions (map
) <= 1)
692 /* If no preference given, use cheap coalescing of all partitions. */
693 if ((flags
& (SSANORM_COALESCE_PARTITIONS
| SSANORM_USE_COALESCE_LIST
)) == 0)
694 flags
|= SSANORM_COALESCE_PARTITIONS
;
696 liveinfo
= calculate_live_on_entry (map
);
697 calculate_live_on_exit (liveinfo
);
698 rv
= root_var_init (map
);
700 /* Remove single element variable from the list. */
701 root_var_compact (rv
);
703 if (flags
& SSANORM_USE_COALESCE_LIST
)
705 cl
= create_coalesce_list (map
);
707 /* Add all potential copies via PHI arguments to the list. */
710 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
712 tree res
= PHI_RESULT (phi
);
713 int p
= var_to_partition (map
, res
);
714 if (p
== NO_PARTITION
)
716 for (x
= 0; x
< PHI_NUM_ARGS (phi
); x
++)
718 tree arg
= PHI_ARG_DEF (phi
, x
);
721 if (TREE_CODE (arg
) != SSA_NAME
)
723 if (SSA_NAME_VAR (res
) != SSA_NAME_VAR (arg
))
725 p2
= var_to_partition (map
, PHI_ARG_DEF (phi
, x
));
726 if (p2
!= NO_PARTITION
)
727 add_coalesce (cl
, p
, p2
, 1);
732 /* Coalesce all the result decls together. */
735 for (x
= 0; x
< num_var_partitions (map
); x
++)
737 tree p
= partition_to_var (map
, x
);
738 if (TREE_CODE (SSA_NAME_VAR(p
)) == RESULT_DECL
)
740 if (var
== NULL_TREE
)
746 add_coalesce (cl
, i
, x
, 1);
751 /* Build a conflict graph. */
752 graph
= build_tree_conflict_graph (liveinfo
, rv
, cl
);
756 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
758 fprintf (dump_file
, "Before sorting:\n");
759 dump_coalesce_list (dump_file
, cl
);
762 sort_coalesce_list (cl
);
764 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
766 fprintf (dump_file
, "\nAfter sorting:\n");
767 dump_coalesce_list (dump_file
, cl
);
771 /* Put the single element variables back in. */
772 root_var_decompact (rv
);
774 /* First, coalesce all live on entry variables to their root variable.
775 This will ensure the first use is coming from the correct location. */
777 live
= sbitmap_alloc (num_var_partitions (map
));
780 /* Set 'live' vector to indicate live on entry partitions. */
781 num
= num_var_partitions (map
);
782 for (x
= 0 ; x
< num
; x
++)
784 var
= partition_to_var (map
, x
);
785 if (default_def (SSA_NAME_VAR (var
)) == var
)
789 if ((flags
& SSANORM_COMBINE_TEMPS
) == 0)
791 delete_tree_live_info (liveinfo
);
795 /* Assign root variable as partition representative for each live on entry
797 EXECUTE_IF_SET_IN_SBITMAP (live
, 0, x
,
799 var
= root_var (rv
, root_var_find (rv
, x
));
801 /* If these aren't already coalesced... */
802 if (partition_to_var (map
, x
) != var
)
804 if (ann
->out_of_ssa_tag
)
806 /* This root variable has already been assigned to another
807 partition which is not coalesced with this one. */
811 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
813 print_exprs (dump_file
, "Must coalesce ",
814 partition_to_var (map
, x
),
815 " with the root variable ", var
, ".\n");
818 change_partition_var (map
, var
, x
);
824 /* Coalesce partitions live across abnormal edges. */
825 coalesce_abnormal_edges (map
, graph
, rv
);
827 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
828 dump_var_map (dump_file
, map
);
830 /* Coalesce partitions. */
831 if (flags
& SSANORM_USE_COALESCE_LIST
)
832 coalesce_tpa_members (rv
, graph
, map
, cl
,
833 ((dump_flags
& TDF_DETAILS
) ? dump_file
837 if (flags
& SSANORM_COALESCE_PARTITIONS
)
838 coalesce_tpa_members (rv
, graph
, map
, NULL
,
839 ((dump_flags
& TDF_DETAILS
) ? dump_file
842 delete_coalesce_list (cl
);
843 root_var_delete (rv
);
844 conflict_graph_delete (graph
);
850 /* Take the ssa-name var_map MAP, and assign real variables to each
854 assign_vars (var_map map
)
861 rv
= root_var_init (map
);
865 /* Coalescing may already have forced some partitions to their root
866 variable. Find these and tag them. */
868 num
= num_var_partitions (map
);
869 for (x
= 0; x
< num
; x
++)
871 var
= partition_to_var (map
, x
);
872 if (TREE_CODE (var
) != SSA_NAME
)
874 /* Coalescing will already have verified that more than one
875 partition doesn't have the same root variable. Simply marked
876 the variable as assigned. */
878 ann
->out_of_ssa_tag
= 1;
879 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
881 fprintf (dump_file
, "partition %d has variable ", x
);
882 print_generic_expr (dump_file
, var
, TDF_SLIM
);
883 fprintf (dump_file
, " assigned to it.\n");
889 num
= root_var_num (rv
);
890 for (x
= 0; x
< num
; x
++)
892 var
= root_var (rv
, x
);
894 for (i
= root_var_first_partition (rv
, x
);
896 i
= root_var_next_partition (rv
, i
))
898 t
= partition_to_var (map
, i
);
900 if (t
== var
|| TREE_CODE (t
) != SSA_NAME
)
903 rep
= var_to_partition (map
, t
);
905 if (!ann
->out_of_ssa_tag
)
907 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
908 print_exprs (dump_file
, "", t
, " --> ", var
, "\n");
909 change_partition_var (map
, var
, rep
);
913 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
914 print_exprs (dump_file
, "", t
, " not coalesced with ", var
,
917 var
= create_temp (t
);
918 change_partition_var (map
, var
, rep
);
921 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
923 fprintf (dump_file
, " --> New temp: '");
924 print_generic_expr (dump_file
, var
, TDF_SLIM
);
925 fprintf (dump_file
, "'\n");
930 root_var_delete (rv
);
934 /* Replace use operand P with whatever variable it has been rewritten to based
935 on the partitions in MAP. EXPR is an optional expression vector over SSA
936 versions which is used to replace P with an expression instead of a variable.
937 If the stmt is changed, return true. */
940 replace_use_variable (var_map map
, use_operand_p p
, tree
*expr
)
943 tree var
= USE_FROM_PTR (p
);
945 /* Check if we are replacing this variable with an expression. */
948 int version
= SSA_NAME_VERSION (var
);
951 tree new_expr
= TREE_OPERAND (expr
[version
], 1);
952 SET_USE (p
, new_expr
);
953 /* Clear the stmt's RHS, or GC might bite us. */
954 TREE_OPERAND (expr
[version
], 1) = NULL_TREE
;
959 new_var
= var_to_partition_to_var (map
, var
);
962 SET_USE (p
, new_var
);
963 set_is_used (new_var
);
970 /* Replace def operand DEF_P with whatever variable it has been rewritten to
971 based on the partitions in MAP. EXPR is an optional expression vector over
972 SSA versions which is used to replace DEF_P with an expression instead of a
973 variable. If the stmt is changed, return true. */
976 replace_def_variable (var_map map
, def_operand_p def_p
, tree
*expr
)
979 tree var
= DEF_FROM_PTR (def_p
);
981 /* Check if we are replacing this variable with an expression. */
984 int version
= SSA_NAME_VERSION (var
);
987 tree new_expr
= TREE_OPERAND (expr
[version
], 1);
988 SET_DEF (def_p
, new_expr
);
989 /* Clear the stmt's RHS, or GC might bite us. */
990 TREE_OPERAND (expr
[version
], 1) = NULL_TREE
;
995 new_var
= var_to_partition_to_var (map
, var
);
998 SET_DEF (def_p
, new_var
);
999 set_is_used (new_var
);
1006 /* Remove any PHI node which is a virtual PHI. */
1009 eliminate_virtual_phis (void)
1016 for (phi
= phi_nodes (bb
); phi
; phi
= next
)
1018 next
= PHI_CHAIN (phi
);
1019 if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi
))))
1021 #ifdef ENABLE_CHECKING
1023 /* There should be no arguments of this PHI which are in
1024 the partition list, or we get incorrect results. */
1025 for (i
= 0; i
< PHI_NUM_ARGS (phi
); i
++)
1027 tree arg
= PHI_ARG_DEF (phi
, i
);
1028 if (TREE_CODE (arg
) == SSA_NAME
1029 && is_gimple_reg (SSA_NAME_VAR (arg
)))
1031 fprintf (stderr
, "Argument of PHI is not virtual (");
1032 print_generic_expr (stderr
, arg
, TDF_SLIM
);
1033 fprintf (stderr
, "), but the result is :");
1034 print_generic_stmt (stderr
, phi
, TDF_SLIM
);
1039 remove_phi_node (phi
, NULL_TREE
, bb
);
1046 /* This routine will coalesce variables in MAP of the same type which do not
1047 interfere with each other. LIVEINFO is the live range info for variables
1048 of interest. This will both reduce the memory footprint of the stack, and
1049 allow us to coalesce together local copies of globals and scalarized
1053 coalesce_vars (var_map map
, tree_live_info_p liveinfo
)
1060 conflict_graph graph
;
1062 cl
= create_coalesce_list (map
);
1064 /* Merge all the live on entry vectors for coalesced partitions. */
1065 for (x
= 0; x
< num_var_partitions (map
); x
++)
1067 var
= partition_to_var (map
, x
);
1068 p
= var_to_partition (map
, var
);
1070 live_merge_and_clear (liveinfo
, p
, x
);
1073 /* When PHI nodes are turned into copies, the result of each PHI node
1074 becomes live on entry to the block. Mark these now. */
1079 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
1081 p
= var_to_partition (map
, PHI_RESULT (phi
));
1083 /* Skip virtual PHI nodes. */
1084 if (p
== NO_PARTITION
)
1087 make_live_on_entry (liveinfo
, bb
, p
);
1089 /* Each argument is a potential copy operation. Add any arguments
1090 which are not coalesced to the result to the coalesce list. */
1091 for (x
= 0; x
< PHI_NUM_ARGS (phi
); x
++)
1093 arg
= PHI_ARG_DEF (phi
, x
);
1094 if (!phi_ssa_name_p (arg
))
1096 p2
= var_to_partition (map
, arg
);
1097 if (p2
== NO_PARTITION
)
1100 add_coalesce (cl
, p
, p2
, 1);
1106 /* Re-calculate live on exit info. */
1107 calculate_live_on_exit (liveinfo
);
1109 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1111 fprintf (dump_file
, "Live range info for variable memory coalescing.\n");
1112 dump_live_info (dump_file
, liveinfo
, LIVEDUMP_ALL
);
1114 fprintf (dump_file
, "Coalesce list from phi nodes:\n");
1115 dump_coalesce_list (dump_file
, cl
);
1119 tv
= type_var_init (map
);
1121 type_var_dump (dump_file
, tv
);
1122 type_var_compact (tv
);
1124 type_var_dump (dump_file
, tv
);
1126 graph
= build_tree_conflict_graph (liveinfo
, tv
, cl
);
1128 type_var_decompact (tv
);
1129 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1131 fprintf (dump_file
, "type var list now looks like:n");
1132 type_var_dump (dump_file
, tv
);
1134 fprintf (dump_file
, "Coalesce list after conflict graph build:\n");
1135 dump_coalesce_list (dump_file
, cl
);
1138 sort_coalesce_list (cl
);
1139 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1141 fprintf (dump_file
, "Coalesce list after sorting:\n");
1142 dump_coalesce_list (dump_file
, cl
);
1145 coalesce_tpa_members (tv
, graph
, map
, cl
,
1146 ((dump_flags
& TDF_DETAILS
) ? dump_file
: NULL
));
1148 type_var_delete (tv
);
1149 delete_coalesce_list (cl
);
1153 /* Temporary Expression Replacement (TER)
1155 Replace SSA version variables during out-of-ssa with their defining
1156 expression if there is only one use of the variable.
1158 A pass is made through the function, one block at a time. No cross block
1159 information is tracked.
1161 Variables which only have one use, and whose defining stmt is considered
1162 a replaceable expression (see check_replaceable) are entered into
1163 consideration by adding a list of dependent partitions to the version_info
1164 vector for that ssa_name_version. This information comes from the partition
1165 mapping for each USE. At the same time, the partition_dep_list vector for
1166 these partitions have this version number entered into their lists.
1168 When the use of a replaceable ssa_variable is encountered, the dependence
1169 list in version_info[] is moved to the "pending_dependence" list in case
1170 the current expression is also replaceable. (To be determined later in
1171 processing this stmt.) version_info[] for the version is then updated to
1172 point to the defining stmt and the 'replaceable' bit is set.
1174 Any partition which is defined by a statement 'kills' any expression which
1175 is dependent on this partition. Every ssa version in the partitions'
1176 dependence list is removed from future consideration.
1178 All virtual references are lumped together. Any expression which is
1179 dependent on any virtual variable (via a VUSE) has a dependence added
1180 to the special partition defined by VIRTUAL_PARTITION.
1182 Whenever a V_MAY_DEF is seen, all expressions dependent this
1183 VIRTUAL_PARTITION are removed from consideration.
1185 At the end of a basic block, all expression are removed from consideration
1186 in preparation for the next block.
1188 The end result is a vector over SSA_NAME_VERSION which is passed back to
1189 rewrite_out_of_ssa. As the SSA variables are being rewritten, instead of
1190 replacing the SSA_NAME tree element with the partition it was assigned,
1191 it is replaced with the RHS of the defining expression. */
1194 /* Dependency list element. This can contain either a partition index or a
1195 version number, depending on which list it is in. */
1197 typedef struct value_expr_d
1200 struct value_expr_d
*next
;
1204 /* Temporary Expression Replacement (TER) table information. */
1206 typedef struct temp_expr_table_d
1209 void **version_info
;
1210 value_expr_p
*partition_dep_list
;
1212 bool saw_replaceable
;
1213 int virtual_partition
;
1214 bitmap partition_in_use
;
1215 value_expr_p free_list
;
1216 value_expr_p pending_dependence
;
1217 } *temp_expr_table_p
;
1219 /* Used to indicate a dependency on V_MAY_DEFs. */
1220 #define VIRTUAL_PARTITION(table) (table->virtual_partition)
1222 static temp_expr_table_p
new_temp_expr_table (var_map
);
1223 static tree
*free_temp_expr_table (temp_expr_table_p
);
1224 static inline value_expr_p
new_value_expr (temp_expr_table_p
);
1225 static inline void free_value_expr (temp_expr_table_p
, value_expr_p
);
1226 static inline value_expr_p
find_value_in_list (value_expr_p
, int,
1228 static inline void add_value_to_list (temp_expr_table_p
, value_expr_p
*, int);
1229 static inline void add_info_to_list (temp_expr_table_p
, value_expr_p
*,
1231 static value_expr_p
remove_value_from_list (value_expr_p
*, int);
1232 static void add_dependance (temp_expr_table_p
, int, tree
);
1233 static bool check_replaceable (temp_expr_table_p
, tree
);
1234 static void finish_expr (temp_expr_table_p
, int, bool);
1235 static void mark_replaceable (temp_expr_table_p
, tree
);
1236 static inline void kill_expr (temp_expr_table_p
, int, bool);
1237 static inline void kill_virtual_exprs (temp_expr_table_p
, bool);
1238 static void find_replaceable_in_bb (temp_expr_table_p
, basic_block
);
1239 static tree
*find_replaceable_exprs (var_map
);
1240 static void dump_replaceable_exprs (FILE *, tree
*);
1243 /* Create a new TER table for MAP. */
1245 static temp_expr_table_p
1246 new_temp_expr_table (var_map map
)
1248 temp_expr_table_p t
;
1250 t
= (temp_expr_table_p
) xmalloc (sizeof (struct temp_expr_table_d
));
1253 t
->version_info
= xcalloc (num_ssa_names
+ 1, sizeof (void *));
1254 t
->partition_dep_list
= xcalloc (num_var_partitions (map
) + 1,
1255 sizeof (value_expr_p
));
1257 t
->replaceable
= BITMAP_XMALLOC ();
1258 t
->partition_in_use
= BITMAP_XMALLOC ();
1260 t
->saw_replaceable
= false;
1261 t
->virtual_partition
= num_var_partitions (map
);
1262 t
->free_list
= NULL
;
1263 t
->pending_dependence
= NULL
;
1269 /* Free TER table T. If there are valid replacements, return the expression
1273 free_temp_expr_table (temp_expr_table_p t
)
1278 #ifdef ENABLE_CHECKING
1280 for (x
= 0; x
<= num_var_partitions (t
->map
); x
++)
1281 if (t
->partition_dep_list
[x
] != NULL
)
1285 while ((p
= t
->free_list
))
1287 t
->free_list
= p
->next
;
1291 BITMAP_XFREE (t
->partition_in_use
);
1292 BITMAP_XFREE (t
->replaceable
);
1294 free (t
->partition_dep_list
);
1295 if (t
->saw_replaceable
)
1296 ret
= (tree
*)t
->version_info
;
1298 free (t
->version_info
);
1305 /* Allocate a new value list node. Take it from the free list in TABLE if
1308 static inline value_expr_p
1309 new_value_expr (temp_expr_table_p table
)
1312 if (table
->free_list
)
1314 p
= table
->free_list
;
1315 table
->free_list
= p
->next
;
1318 p
= (value_expr_p
) xmalloc (sizeof (struct value_expr_d
));
1324 /* Add value list node P to the free list in TABLE. */
1327 free_value_expr (temp_expr_table_p table
, value_expr_p p
)
1329 p
->next
= table
->free_list
;
1330 table
->free_list
= p
;
1334 /* Find VALUE if its in LIST. Return a pointer to the list object if found,
1335 else return NULL. If LAST_PTR is provided, it will point to the previous
1336 item upon return, or NULL if this is the first item in the list. */
1338 static inline value_expr_p
1339 find_value_in_list (value_expr_p list
, int value
, value_expr_p
*last_ptr
)
1342 value_expr_p last
= NULL
;
1344 for (curr
= list
; curr
; last
= curr
, curr
= curr
->next
)
1346 if (curr
->value
== value
)
1355 /* Add VALUE to LIST, if it isn't already present. TAB is the expression
1359 add_value_to_list (temp_expr_table_p tab
, value_expr_p
*list
, int value
)
1363 if (!find_value_in_list (*list
, value
, NULL
))
1365 info
= new_value_expr (tab
);
1366 info
->value
= value
;
1373 /* Add value node INFO if it's value isn't already in LIST. Free INFO if
1374 it is already in the list. TAB is the expression table. */
1377 add_info_to_list (temp_expr_table_p tab
, value_expr_p
*list
, value_expr_p info
)
1379 if (find_value_in_list (*list
, info
->value
, NULL
))
1380 free_value_expr (tab
, info
);
1389 /* Look for VALUE in LIST. If found, remove it from the list and return it's
1393 remove_value_from_list (value_expr_p
*list
, int value
)
1395 value_expr_p info
, last
;
1397 info
= find_value_in_list (*list
, value
, &last
);
1403 last
->next
= info
->next
;
1409 /* Add a dependency between the def of ssa VERSION and VAR. If VAR is
1410 replaceable by an expression, add a dependence each of the elements of the
1411 expression. These are contained in the pending list. TAB is the
1412 expression table. */
1415 add_dependance (temp_expr_table_p tab
, int version
, tree var
)
1420 i
= SSA_NAME_VERSION (var
);
1421 if (bitmap_bit_p (tab
->replaceable
, i
))
1423 /* This variable is being substituted, so use whatever dependences
1424 were queued up when we marked this as replaceable earlier. */
1425 while ((info
= tab
->pending_dependence
))
1427 tab
->pending_dependence
= info
->next
;
1428 /* Get the partition this variable was dependent on. Reuse this
1429 object to represent the current expression instead. */
1431 info
->value
= version
;
1432 add_info_to_list (tab
, &(tab
->partition_dep_list
[x
]), info
);
1433 add_value_to_list (tab
,
1434 (value_expr_p
*)&(tab
->version_info
[version
]), x
);
1435 bitmap_set_bit (tab
->partition_in_use
, x
);
1440 i
= var_to_partition (tab
->map
, var
);
1441 #ifdef ENABLE_CHECKING
1442 if (i
== NO_PARTITION
)
1445 add_value_to_list (tab
, &(tab
->partition_dep_list
[i
]), version
);
1446 add_value_to_list (tab
,
1447 (value_expr_p
*)&(tab
->version_info
[version
]), i
);
1448 bitmap_set_bit (tab
->partition_in_use
, i
);
1453 /* Check if expression STMT is suitable for replacement in table TAB. If so,
1454 create an expression entry. Return true if this stmt is replaceable. */
1457 check_replaceable (temp_expr_table_p tab
, tree stmt
)
1460 vuse_optype vuseops
;
1464 int num_use_ops
, version
;
1465 var_map map
= tab
->map
;
1468 if (TREE_CODE (stmt
) != MODIFY_EXPR
)
1471 ann
= stmt_ann (stmt
);
1472 defs
= DEF_OPS (ann
);
1474 /* Punt if there is more than 1 def, or more than 1 use. */
1475 if (NUM_DEFS (defs
) != 1)
1477 def
= DEF_OP (defs
, 0);
1478 if (version_ref_count (map
, def
) != 1)
1481 /* Assignments to variables assigned to hard registers are not
1483 if (DECL_HARD_REGISTER (SSA_NAME_VAR (def
)))
1486 /* There must be no V_MAY_DEFS. */
1487 if (NUM_V_MAY_DEFS (V_MAY_DEF_OPS (ann
)) != 0)
1490 /* There must be no V_MUST_DEFS. */
1491 if (NUM_V_MUST_DEFS (V_MUST_DEF_OPS (ann
)) != 0)
1494 /* Float expressions must go through memory if float-store is on. */
1495 if (flag_float_store
&& FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt
, 1))))
1498 uses
= USE_OPS (ann
);
1499 num_use_ops
= NUM_USES (uses
);
1500 vuseops
= VUSE_OPS (ann
);
1502 /* Any expression which has no virtual operands and no real operands
1503 should have been propagated if it's possible to do anything with them.
1504 If this happens here, it probably exists that way for a reason, so we
1505 won't touch it. An example is:
1507 There are no virtual uses nor any real uses, so we just leave this
1508 alone to be safe. */
1510 if (num_use_ops
== 0 && NUM_VUSES (vuseops
) == 0)
1513 version
= SSA_NAME_VERSION (def
);
1515 /* Add this expression to the dependency list for each use partition. */
1516 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, iter
, SSA_OP_USE
)
1518 add_dependance (tab
, version
, var
);
1521 /* If there are VUSES, add a dependence on virtual defs. */
1522 if (NUM_VUSES (vuseops
) != 0)
1524 add_value_to_list (tab
, (value_expr_p
*)&(tab
->version_info
[version
]),
1525 VIRTUAL_PARTITION (tab
));
1526 add_value_to_list (tab
,
1527 &(tab
->partition_dep_list
[VIRTUAL_PARTITION (tab
)]),
1529 bitmap_set_bit (tab
->partition_in_use
, VIRTUAL_PARTITION (tab
));
1536 /* This function will remove the expression for VERSION from replacement
1537 consideration.n table TAB If 'replace' is true, it is marked as
1538 replaceable, otherwise not. */
1541 finish_expr (temp_expr_table_p tab
, int version
, bool replace
)
1543 value_expr_p info
, tmp
;
1546 /* Remove this expression from its dependent lists. The partition dependence
1547 list is retained and transfered later to whomever uses this version. */
1548 for (info
= (value_expr_p
) tab
->version_info
[version
]; info
; info
= tmp
)
1550 partition
= info
->value
;
1551 #ifdef ENABLE_CHECKING
1552 if (tab
->partition_dep_list
[partition
] == NULL
)
1555 tmp
= remove_value_from_list (&(tab
->partition_dep_list
[partition
]),
1557 #ifdef ENABLE_CHECKING
1561 free_value_expr (tab
, tmp
);
1562 /* Only clear the bit when the dependency list is emptied via
1563 a replacement. Otherwise kill_expr will take care of it. */
1564 if (!(tab
->partition_dep_list
[partition
]) && replace
)
1565 bitmap_clear_bit (tab
->partition_in_use
, partition
);
1568 free_value_expr (tab
, info
);
1573 tab
->saw_replaceable
= true;
1574 bitmap_set_bit (tab
->replaceable
, version
);
1578 #ifdef ENABLE_CHECKING
1579 if (bitmap_bit_p (tab
->replaceable
, version
))
1582 tab
->version_info
[version
] = NULL
;
1587 /* Mark the expression associated with VAR as replaceable, and enter
1588 the defining stmt into the version_info table TAB. */
1591 mark_replaceable (temp_expr_table_p tab
, tree var
)
1594 int version
= SSA_NAME_VERSION (var
);
1595 finish_expr (tab
, version
, true);
1597 /* Move the dependence list to the pending list. */
1598 if (tab
->version_info
[version
])
1600 info
= (value_expr_p
) tab
->version_info
[version
];
1601 for ( ; info
->next
; info
= info
->next
)
1603 info
->next
= tab
->pending_dependence
;
1604 tab
->pending_dependence
= (value_expr_p
)tab
->version_info
[version
];
1607 tab
->version_info
[version
] = SSA_NAME_DEF_STMT (var
);
1611 /* This function marks any expression in TAB which is dependent on PARTITION
1612 as NOT replaceable. CLEAR_BIT is used to determine whether partition_in_use
1613 should have its bit cleared. Since this routine can be called within an
1614 EXECUTE_IF_SET_IN_BITMAP, the bit can't always be cleared. */
1617 kill_expr (temp_expr_table_p tab
, int partition
, bool clear_bit
)
1621 /* Mark every active expr dependent on this var as not replaceable. */
1622 while ((ptr
= tab
->partition_dep_list
[partition
]) != NULL
)
1623 finish_expr (tab
, ptr
->value
, false);
1626 bitmap_clear_bit (tab
->partition_in_use
, partition
);
1630 /* This function kills all expressions in TAB which are dependent on virtual
1631 DEFs. CLEAR_BIT determines whether partition_in_use gets cleared. */
1634 kill_virtual_exprs (temp_expr_table_p tab
, bool clear_bit
)
1636 kill_expr (tab
, VIRTUAL_PARTITION (tab
), clear_bit
);
1640 /* This function processes basic block BB, and looks for variables which can
1641 be replaced by their expressions. Results are stored in TAB. */
1644 find_replaceable_in_bb (temp_expr_table_p tab
, basic_block bb
)
1646 block_stmt_iterator bsi
;
1650 var_map map
= tab
->map
;
1654 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
1656 stmt
= bsi_stmt (bsi
);
1657 ann
= stmt_ann (stmt
);
1659 /* Determine if this stmt finishes an existing expression. */
1660 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_USE
)
1662 if (tab
->version_info
[SSA_NAME_VERSION (def
)])
1664 /* Mark expression as replaceable unless stmt is volatile. */
1665 if (!ann
->has_volatile_ops
)
1666 mark_replaceable (tab
, def
);
1668 finish_expr (tab
, SSA_NAME_VERSION (def
), false);
1672 /* Next, see if this stmt kills off an active expression. */
1673 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
1675 partition
= var_to_partition (map
, def
);
1676 if (partition
!= NO_PARTITION
&& tab
->partition_dep_list
[partition
])
1677 kill_expr (tab
, partition
, true);
1680 /* Now see if we are creating a new expression or not. */
1681 if (!ann
->has_volatile_ops
)
1682 check_replaceable (tab
, stmt
);
1684 /* Free any unused dependency lists. */
1685 while ((p
= tab
->pending_dependence
))
1687 tab
->pending_dependence
= p
->next
;
1688 free_value_expr (tab
, p
);
1691 /* A V_MAY_DEF kills any expression using a virtual operand. */
1692 if (NUM_V_MAY_DEFS (V_MAY_DEF_OPS (ann
)) > 0)
1693 kill_virtual_exprs (tab
, true);
1695 /* A V_MUST_DEF kills any expression using a virtual operand. */
1696 if (NUM_V_MUST_DEFS (V_MUST_DEF_OPS (ann
)) > 0)
1697 kill_virtual_exprs (tab
, true);
1702 /* This function is the driver routine for replacement of temporary expressions
1703 in the SSA->normal phase, operating on MAP. If there are replaceable
1704 expressions, a table is returned which maps SSA versions to the
1705 expressions they should be replaced with. A NULL_TREE indicates no
1706 replacement should take place. If there are no replacements at all,
1707 NULL is returned by the function, otherwise an expression vector indexed
1708 by SSA_NAME version numbers. */
1711 find_replaceable_exprs (var_map map
)
1715 temp_expr_table_p table
;
1718 table
= new_temp_expr_table (map
);
1721 find_replaceable_in_bb (table
, bb
);
1722 EXECUTE_IF_SET_IN_BITMAP ((table
->partition_in_use
), 0, i
,
1724 kill_expr (table
, i
, false);
1728 ret
= free_temp_expr_table (table
);
1733 /* Dump TER expression table EXPR to file F. */
1736 dump_replaceable_exprs (FILE *f
, tree
*expr
)
1740 fprintf (f
, "\nReplacing Expressions\n");
1741 for (x
= 0; x
< (int)num_ssa_names
+ 1; x
++)
1745 var
= DEF_OP (STMT_DEF_OPS (stmt
), 0);
1746 print_generic_expr (f
, var
, TDF_SLIM
);
1747 fprintf (f
, " replace with --> ");
1748 print_generic_expr (f
, TREE_OPERAND (stmt
, 1), TDF_SLIM
);
1755 /* Helper function for discover_nonconstant_array_refs.
1756 Look for ARRAY_REF nodes with non-constant indexes and mark them
1760 discover_nonconstant_array_refs_r (tree
* tp
, int *walk_subtrees
,
1761 void *data ATTRIBUTE_UNUSED
)
1765 if (TYPE_P (t
) || DECL_P (t
))
1767 else if (TREE_CODE (t
) == ARRAY_REF
|| TREE_CODE (t
) == ARRAY_RANGE_REF
)
1769 while (((TREE_CODE (t
) == ARRAY_REF
|| TREE_CODE (t
) == ARRAY_RANGE_REF
)
1770 && is_gimple_min_invariant (TREE_OPERAND (t
, 1))
1771 && (!TREE_OPERAND (t
, 2)
1772 || is_gimple_min_invariant (TREE_OPERAND (t
, 2))))
1773 || (TREE_CODE (t
) == COMPONENT_REF
1774 && (!TREE_OPERAND (t
,2)
1775 || is_gimple_min_invariant (TREE_OPERAND (t
, 2))))
1776 || TREE_CODE (t
) == BIT_FIELD_REF
1777 || TREE_CODE (t
) == REALPART_EXPR
1778 || TREE_CODE (t
) == IMAGPART_EXPR
1779 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
1780 || TREE_CODE (t
) == NOP_EXPR
1781 || TREE_CODE (t
) == CONVERT_EXPR
)
1782 t
= TREE_OPERAND (t
, 0);
1784 if (TREE_CODE (t
) == ARRAY_REF
|| TREE_CODE (t
) == ARRAY_RANGE_REF
)
1786 t
= get_base_address (t
);
1787 if (t
&& DECL_P (t
))
1788 TREE_ADDRESSABLE (t
) = 1;
1798 /* RTL expansion is not able to compile array references with variable
1799 offsets for arrays stored in single register. Discover such
1800 expressions and mark variables as addressable to avoid this
1804 discover_nonconstant_array_refs (void)
1807 block_stmt_iterator bsi
;
1811 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
1812 walk_tree (bsi_stmt_ptr (bsi
), discover_nonconstant_array_refs_r
,
1818 /* This function will rewrite the current program using the variable mapping
1819 found in MAP. If the replacement vector VALUES is provided, any
1820 occurrences of partitions with non-null entries in the vector will be
1821 replaced with the expression in the vector instead of its mapped
1825 rewrite_trees (var_map map
, tree
*values
)
1829 block_stmt_iterator si
;
1834 #ifdef ENABLE_CHECKING
1835 /* Search for PHIs where the destination has no partition, but one
1836 or more arguments has a partition. This should not happen and can
1837 create incorrect code. */
1842 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
1844 tree T0
= var_to_partition_to_var (map
, PHI_RESULT (phi
));
1846 if (T0
== NULL_TREE
)
1850 for (i
= 0; i
< PHI_NUM_ARGS (phi
); i
++)
1852 tree arg
= PHI_ARG_DEF (phi
, i
);
1854 if (TREE_CODE (arg
) == SSA_NAME
1855 && var_to_partition (map
, arg
) != NO_PARTITION
)
1857 fprintf (stderr
, "Argument of PHI is in a partition :(");
1858 print_generic_expr (stderr
, arg
, TDF_SLIM
);
1859 fprintf (stderr
, "), but the result is not :");
1860 print_generic_stmt (stderr
, phi
, TDF_SLIM
);
1869 /* Replace PHI nodes with any required copies. */
1870 g
= new_elim_graph (map
->num_partitions
);
1874 for (si
= bsi_start (bb
); !bsi_end_p (si
); )
1876 size_t num_uses
, num_defs
;
1879 tree stmt
= bsi_stmt (si
);
1880 use_operand_p use_p
;
1881 def_operand_p def_p
;
1882 int remove
= 0, is_copy
= 0;
1886 get_stmt_operands (stmt
);
1887 ann
= stmt_ann (stmt
);
1890 if (TREE_CODE (stmt
) == MODIFY_EXPR
1891 && (TREE_CODE (TREE_OPERAND (stmt
, 1)) == SSA_NAME
))
1894 uses
= USE_OPS (ann
);
1895 num_uses
= NUM_USES (uses
);
1896 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
1898 if (replace_use_variable (map
, use_p
, values
))
1902 defs
= DEF_OPS (ann
);
1903 num_defs
= NUM_DEFS (defs
);
1905 /* Mark this stmt for removal if it is the list of replaceable
1907 if (values
&& num_defs
== 1)
1909 tree def
= DEF_OP (defs
, 0);
1911 val
= values
[SSA_NAME_VERSION (def
)];
1917 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_DEF
)
1919 if (replace_def_variable (map
, def_p
, NULL
))
1922 /* If both SSA_NAMEs coalesce to the same variable,
1923 mark the now redundant copy for removal. */
1926 && (DEF_FROM_PTR (def_p
) == USE_OP (uses
, 0)))
1929 if (changed
& !remove
)
1933 /* Remove any stmts marked for removal. */
1940 phi
= phi_nodes (bb
);
1943 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
1944 eliminate_phi (e
, phi_arg_from_edge (phi
, e
), g
);
1948 delete_elim_graph (g
);
1950 /* If any copies were inserted on edges, actually insert them now. */
1951 bsi_commit_edge_inserts (NULL
);
1955 /* Remove the variables specified in MAP from SSA form. Any debug information
1956 is sent to DUMP. FLAGS indicate what options should be used. */
1959 remove_ssa_form (FILE *dump
, var_map map
, int flags
)
1961 tree_live_info_p liveinfo
;
1965 tree
*values
= NULL
;
1970 /* If we are not combining temps, don't calculate live ranges for variables
1971 with only one SSA version. */
1972 if ((flags
& SSANORM_COMBINE_TEMPS
) == 0)
1973 compact_var_map (map
, VARMAP_NO_SINGLE_DEFS
);
1975 compact_var_map (map
, VARMAP_NORMAL
);
1977 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1978 dump_var_map (dump_file
, map
);
1980 liveinfo
= coalesce_ssa_name (map
, flags
);
1982 /* Make sure even single occurrence variables are in the list now. */
1983 if ((flags
& SSANORM_COMBINE_TEMPS
) == 0)
1984 compact_var_map (map
, VARMAP_NORMAL
);
1986 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1988 fprintf (dump_file
, "After Coalescing:\n");
1989 dump_var_map (dump_file
, map
);
1992 if (flags
& SSANORM_PERFORM_TER
)
1994 values
= find_replaceable_exprs (map
);
1995 if (values
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
1996 dump_replaceable_exprs (dump_file
, values
);
1999 /* Assign real variables to the partitions now. */
2002 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2004 fprintf (dump_file
, "After Root variable replacement:\n");
2005 dump_var_map (dump_file
, map
);
2008 if ((flags
& SSANORM_COMBINE_TEMPS
) && liveinfo
)
2010 coalesce_vars (map
, liveinfo
);
2011 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2013 fprintf (dump_file
, "After variable memory coalescing:\n");
2014 dump_var_map (dump_file
, map
);
2019 delete_tree_live_info (liveinfo
);
2021 rewrite_trees (map
, values
);
2026 /* Remove phi nodes which have been translated back to real variables. */
2029 for (phi
= phi_nodes (bb
); phi
; phi
= next
)
2031 next
= PHI_CHAIN (phi
);
2032 if ((flags
& SSANORM_REMOVE_ALL_PHIS
)
2033 || var_to_partition (map
, PHI_RESULT (phi
)) != NO_PARTITION
)
2034 remove_phi_node (phi
, NULL_TREE
, bb
);
2041 /* Take the current function out of SSA form, as described in
2042 R. Morgan, ``Building an Optimizing Compiler'',
2043 Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
2046 rewrite_out_of_ssa (void)
2050 int ssa_flags
= (SSANORM_REMOVE_ALL_PHIS
| SSANORM_USE_COALESCE_LIST
);
2052 if (!flag_tree_live_range_split
)
2053 ssa_flags
|= SSANORM_COALESCE_PARTITIONS
;
2055 eliminate_virtual_phis ();
2057 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2058 dump_tree_cfg (dump_file
, dump_flags
& ~TDF_DETAILS
);
2060 /* We cannot allow unssa to un-gimplify trees before we instrument them. */
2061 if (flag_tree_ter
&& !flag_mudflap
)
2062 var_flags
= SSA_VAR_MAP_REF_COUNT
;
2064 map
= create_ssa_var_map (var_flags
);
2066 if (flag_tree_combine_temps
)
2067 ssa_flags
|= SSANORM_COMBINE_TEMPS
;
2068 if (flag_tree_ter
&& !flag_mudflap
)
2069 ssa_flags
|= SSANORM_PERFORM_TER
;
2071 remove_ssa_form (dump_file
, map
, ssa_flags
);
2073 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2074 dump_tree_cfg (dump_file
, dump_flags
& ~TDF_DETAILS
);
2076 /* Do some cleanups which reduce the amount of data the
2077 tree->rtl expanders deal with. */
2078 cfg_remove_useless_stmts ();
2080 /* Flush out flow graph and SSA data. */
2081 delete_var_map (map
);
2083 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
2084 discover_nonconstant_array_refs ();
2088 /* Define the parameters of the out of SSA pass. */
2090 struct tree_opt_pass pass_del_ssa
=
2092 "optimized", /* name */
2094 rewrite_out_of_ssa
, /* execute */
2097 0, /* static_pass_number */
2098 TV_TREE_SSA_TO_NORMAL
, /* tv_id */
2099 PROP_cfg
| PROP_ssa
| PROP_alias
, /* properties_required */
2100 0, /* properties_provided */
2101 /* ??? If TER is enabled, we also kill gimple. */
2102 PROP_ssa
, /* properties_destroyed */
2103 TODO_verify_ssa
| TODO_verify_flow
2104 | TODO_verify_stmts
, /* todo_flags_start */
2105 TODO_dump_func
| TODO_ggc_collect
/* todo_flags_finish */