1 /* Convert a program in SSA form into Normal form.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 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 "basic-block.h"
28 #include "diagnostic.h"
30 #include "tree-flow.h"
32 #include "tree-dump.h"
33 #include "tree-ssa-live.h"
34 #include "tree-pass.h"
38 /* Used to hold all the components required to do SSA PHI elimination.
39 The node and pred/succ list is a simple linear list of nodes and
40 edges represented as pairs of nodes.
42 The predecessor and successor list: Nodes are entered in pairs, where
43 [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
44 predecessors, all the odd elements are successors.
47 When implemented as bitmaps, very large programs SSA->Normal times were
48 being dominated by clearing the interference graph.
50 Typically this list of edges is extremely small since it only includes
51 PHI results and uses from a single edge which have not coalesced with
52 each other. This means that no virtual PHI nodes are included, and
53 empirical evidence suggests that the number of edges rarely exceed
54 3, and in a bootstrap of GCC, the maximum size encountered was 7.
55 This also limits the number of possible nodes that are involved to
56 rarely more than 6, and in the bootstrap of gcc, the maximum number
57 of nodes encountered was 12. */
59 typedef struct _elim_graph
{
60 /* Size of the elimination vectors. */
63 /* List of nodes in the elimination graph. */
64 VEC(tree
,heap
) *nodes
;
66 /* The predecessor and successor edge list. */
67 VEC(int,heap
) *edge_list
;
72 /* Stack for visited nodes. */
75 /* The variable partition map. */
78 /* Edge being eliminated by this graph. */
81 /* List of constant copies to emit. These are pushed on in pairs. */
82 VEC(tree
,heap
) *const_copies
;
86 /* Create a temporary variable based on the type of variable T. Use T's name
93 const char *name
= NULL
;
96 if (TREE_CODE (t
) == SSA_NAME
)
99 gcc_assert (TREE_CODE (t
) == VAR_DECL
|| TREE_CODE (t
) == PARM_DECL
);
101 type
= TREE_TYPE (t
);
104 name
= IDENTIFIER_POINTER (tmp
);
108 tmp
= create_tmp_var (type
, name
);
110 if (DECL_DEBUG_EXPR_IS_FROM (t
) && DECL_DEBUG_EXPR (t
))
112 SET_DECL_DEBUG_EXPR (tmp
, DECL_DEBUG_EXPR (t
));
113 DECL_DEBUG_EXPR_IS_FROM (tmp
) = 1;
115 else if (!DECL_IGNORED_P (t
))
117 SET_DECL_DEBUG_EXPR (tmp
, t
);
118 DECL_DEBUG_EXPR_IS_FROM (tmp
) = 1;
120 DECL_ARTIFICIAL (tmp
) = DECL_ARTIFICIAL (t
);
121 DECL_IGNORED_P (tmp
) = DECL_IGNORED_P (t
);
122 DECL_GIMPLE_REG_P (tmp
) = DECL_GIMPLE_REG_P (t
);
123 add_referenced_var (tmp
);
125 /* add_referenced_var will create the annotation and set up some
126 of the flags in the annotation. However, some flags we need to
127 inherit from our original variable. */
128 set_symbol_mem_tag (tmp
, symbol_mem_tag (t
));
129 if (is_call_clobbered (t
))
130 mark_call_clobbered (tmp
, var_ann (t
)->escape_mask
);
136 /* This helper function fill insert a copy from a constant or variable SRC to
137 variable DEST on edge E. */
140 insert_copy_on_edge (edge e
, tree dest
, tree src
)
144 copy
= build_gimple_modify_stmt (dest
, src
);
147 if (TREE_CODE (src
) == ADDR_EXPR
)
148 src
= TREE_OPERAND (src
, 0);
149 if (TREE_CODE (src
) == VAR_DECL
|| TREE_CODE (src
) == PARM_DECL
)
152 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
155 "Inserting a copy on edge BB%d->BB%d :",
158 print_generic_expr (dump_file
, copy
, dump_flags
);
159 fprintf (dump_file
, "\n");
162 bsi_insert_on_edge (e
, copy
);
166 /* Create an elimination graph with SIZE nodes and associated data
170 new_elim_graph (int size
)
172 elim_graph g
= (elim_graph
) xmalloc (sizeof (struct _elim_graph
));
174 g
->nodes
= VEC_alloc (tree
, heap
, 30);
175 g
->const_copies
= VEC_alloc (tree
, heap
, 20);
176 g
->edge_list
= VEC_alloc (int, heap
, 20);
177 g
->stack
= VEC_alloc (int, heap
, 30);
179 g
->visited
= sbitmap_alloc (size
);
185 /* Empty elimination graph G. */
188 clear_elim_graph (elim_graph g
)
190 VEC_truncate (tree
, g
->nodes
, 0);
191 VEC_truncate (int, g
->edge_list
, 0);
195 /* Delete elimination graph G. */
198 delete_elim_graph (elim_graph g
)
200 sbitmap_free (g
->visited
);
201 VEC_free (int, heap
, g
->stack
);
202 VEC_free (int, heap
, g
->edge_list
);
203 VEC_free (tree
, heap
, g
->const_copies
);
204 VEC_free (tree
, heap
, g
->nodes
);
209 /* Return the number of nodes in graph G. */
212 elim_graph_size (elim_graph g
)
214 return VEC_length (tree
, g
->nodes
);
218 /* Add NODE to graph G, if it doesn't exist already. */
221 elim_graph_add_node (elim_graph g
, tree node
)
226 for (x
= 0; VEC_iterate (tree
, g
->nodes
, x
, t
); x
++)
229 VEC_safe_push (tree
, heap
, g
->nodes
, node
);
233 /* Add the edge PRED->SUCC to graph G. */
236 elim_graph_add_edge (elim_graph g
, int pred
, int succ
)
238 VEC_safe_push (int, heap
, g
->edge_list
, pred
);
239 VEC_safe_push (int, heap
, g
->edge_list
, succ
);
243 /* Remove an edge from graph G for which NODE is the predecessor, and
244 return the successor node. -1 is returned if there is no such edge. */
247 elim_graph_remove_succ_edge (elim_graph g
, int node
)
251 for (x
= 0; x
< VEC_length (int, g
->edge_list
); x
+= 2)
252 if (VEC_index (int, g
->edge_list
, x
) == node
)
254 VEC_replace (int, g
->edge_list
, x
, -1);
255 y
= VEC_index (int, g
->edge_list
, x
+ 1);
256 VEC_replace (int, g
->edge_list
, x
+ 1, -1);
263 /* Find all the nodes in GRAPH which are successors to NODE in the
264 edge list. VAR will hold the partition number found. CODE is the
265 code fragment executed for every node found. */
267 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, CODE) \
271 for (x_ = 0; x_ < VEC_length (int, (GRAPH)->edge_list); x_ += 2) \
273 y_ = VEC_index (int, (GRAPH)->edge_list, x_); \
276 (VAR) = VEC_index (int, (GRAPH)->edge_list, x_ + 1); \
282 /* Find all the nodes which are predecessors of NODE in the edge list for
283 GRAPH. VAR will hold the partition number found. CODE is the
284 code fragment executed for every node found. */
286 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, CODE) \
290 for (x_ = 0; x_ < VEC_length (int, (GRAPH)->edge_list); x_ += 2) \
292 y_ = VEC_index (int, (GRAPH)->edge_list, x_ + 1); \
295 (VAR) = VEC_index (int, (GRAPH)->edge_list, x_); \
301 /* Add T to elimination graph G. */
304 eliminate_name (elim_graph g
, tree T
)
306 elim_graph_add_node (g
, T
);
310 /* Build elimination graph G for basic block BB on incoming PHI edge
314 eliminate_build (elim_graph g
, basic_block B
)
320 clear_elim_graph (g
);
322 for (phi
= phi_nodes (B
); phi
; phi
= PHI_CHAIN (phi
))
324 T0
= var_to_partition_to_var (g
->map
, PHI_RESULT (phi
));
326 /* Ignore results which are not in partitions. */
330 Ti
= PHI_ARG_DEF (phi
, g
->e
->dest_idx
);
332 /* If this argument is a constant, or a SSA_NAME which is being
333 left in SSA form, just queue a copy to be emitted on this
335 if (!phi_ssa_name_p (Ti
)
336 || (TREE_CODE (Ti
) == SSA_NAME
337 && var_to_partition (g
->map
, Ti
) == NO_PARTITION
))
339 /* Save constant copies until all other copies have been emitted
341 VEC_safe_push (tree
, heap
, g
->const_copies
, T0
);
342 VEC_safe_push (tree
, heap
, g
->const_copies
, Ti
);
346 Ti
= var_to_partition_to_var (g
->map
, Ti
);
349 eliminate_name (g
, T0
);
350 eliminate_name (g
, Ti
);
351 p0
= var_to_partition (g
->map
, T0
);
352 pi
= var_to_partition (g
->map
, Ti
);
353 elim_graph_add_edge (g
, p0
, pi
);
360 /* Push successors of T onto the elimination stack for G. */
363 elim_forward (elim_graph g
, int T
)
366 SET_BIT (g
->visited
, T
);
367 FOR_EACH_ELIM_GRAPH_SUCC (g
, T
, S
,
369 if (!TEST_BIT (g
->visited
, S
))
372 VEC_safe_push (int, heap
, g
->stack
, T
);
376 /* Return 1 if there unvisited predecessors of T in graph G. */
379 elim_unvisited_predecessor (elim_graph g
, int T
)
382 FOR_EACH_ELIM_GRAPH_PRED (g
, T
, P
,
384 if (!TEST_BIT (g
->visited
, P
))
390 /* Process predecessors first, and insert a copy. */
393 elim_backward (elim_graph g
, int T
)
396 SET_BIT (g
->visited
, T
);
397 FOR_EACH_ELIM_GRAPH_PRED (g
, T
, P
,
399 if (!TEST_BIT (g
->visited
, P
))
401 elim_backward (g
, P
);
402 insert_copy_on_edge (g
->e
,
403 partition_to_var (g
->map
, P
),
404 partition_to_var (g
->map
, T
));
409 /* Insert required copies for T in graph G. Check for a strongly connected
410 region, and create a temporary to break the cycle if one is found. */
413 elim_create (elim_graph g
, int T
)
418 if (elim_unvisited_predecessor (g
, T
))
420 U
= create_temp (partition_to_var (g
->map
, T
));
421 insert_copy_on_edge (g
->e
, U
, partition_to_var (g
->map
, T
));
422 FOR_EACH_ELIM_GRAPH_PRED (g
, T
, P
,
424 if (!TEST_BIT (g
->visited
, P
))
426 elim_backward (g
, P
);
427 insert_copy_on_edge (g
->e
, partition_to_var (g
->map
, P
), U
);
433 S
= elim_graph_remove_succ_edge (g
, T
);
436 SET_BIT (g
->visited
, T
);
437 insert_copy_on_edge (g
->e
,
438 partition_to_var (g
->map
, T
),
439 partition_to_var (g
->map
, S
));
446 /* Eliminate all the phi nodes on edge E in graph G. */
449 eliminate_phi (edge e
, elim_graph g
)
452 basic_block B
= e
->dest
;
454 gcc_assert (VEC_length (tree
, g
->const_copies
) == 0);
456 /* Abnormal edges already have everything coalesced. */
457 if (e
->flags
& EDGE_ABNORMAL
)
462 eliminate_build (g
, B
);
464 if (elim_graph_size (g
) != 0)
468 sbitmap_zero (g
->visited
);
469 VEC_truncate (int, g
->stack
, 0);
471 for (x
= 0; VEC_iterate (tree
, g
->nodes
, x
, var
); x
++)
473 int p
= var_to_partition (g
->map
, var
);
474 if (!TEST_BIT (g
->visited
, p
))
478 sbitmap_zero (g
->visited
);
479 while (VEC_length (int, g
->stack
) > 0)
481 x
= VEC_pop (int, g
->stack
);
482 if (!TEST_BIT (g
->visited
, x
))
487 /* If there are any pending constant copies, issue them now. */
488 while (VEC_length (tree
, g
->const_copies
) > 0)
491 src
= VEC_pop (tree
, g
->const_copies
);
492 dest
= VEC_pop (tree
, g
->const_copies
);
493 insert_copy_on_edge (e
, dest
, src
);
498 /* Take the ssa-name var_map MAP, and assign real variables to each
502 assign_vars (var_map map
)
508 num
= num_var_partitions (map
);
509 for (x
= 0; x
< num
; x
++)
511 var
= partition_to_var (map
, x
);
512 if (TREE_CODE (var
) != SSA_NAME
)
515 /* It must already be coalesced. */
516 gcc_assert (ann
->out_of_ssa_tag
== 1);
517 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
519 fprintf (dump_file
, "partition %d already has variable ", x
);
520 print_generic_expr (dump_file
, var
, TDF_SLIM
);
521 fprintf (dump_file
, " assigned to it.\n");
526 root
= SSA_NAME_VAR (var
);
527 ann
= var_ann (root
);
528 /* If ROOT is already associated, create a new one. */
529 if (ann
->out_of_ssa_tag
)
531 root
= create_temp (root
);
532 ann
= var_ann (root
);
534 /* ROOT has not been coalesced yet, so use it. */
535 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
537 fprintf (dump_file
, "Partition %d is assigned to var ", x
);
538 print_generic_stmt (dump_file
, root
, TDF_SLIM
);
540 change_partition_var (map
, root
, x
);
546 /* Replace use operand P with whatever variable it has been rewritten to based
547 on the partitions in MAP. EXPR is an optional expression vector over SSA
548 versions which is used to replace P with an expression instead of a variable.
549 If the stmt is changed, return true. */
552 replace_use_variable (var_map map
, use_operand_p p
, tree
*expr
)
555 tree var
= USE_FROM_PTR (p
);
557 /* Check if we are replacing this variable with an expression. */
560 int version
= SSA_NAME_VERSION (var
);
563 tree new_expr
= GIMPLE_STMT_OPERAND (expr
[version
], 1);
564 SET_USE (p
, new_expr
);
566 /* Clear the stmt's RHS, or GC might bite us. */
567 GIMPLE_STMT_OPERAND (expr
[version
], 1) = NULL_TREE
;
572 new_var
= var_to_partition_to_var (map
, var
);
575 SET_USE (p
, new_var
);
576 set_is_used (new_var
);
583 /* Replace def operand DEF_P with whatever variable it has been rewritten to
584 based on the partitions in MAP. EXPR is an optional expression vector over
585 SSA versions which is used to replace DEF_P with an expression instead of a
586 variable. If the stmt is changed, return true. */
589 replace_def_variable (var_map map
, def_operand_p def_p
, tree
*expr
)
592 tree var
= DEF_FROM_PTR (def_p
);
594 /* Do nothing if we are replacing this variable with an expression. */
595 if (expr
&& expr
[SSA_NAME_VERSION (var
)])
598 new_var
= var_to_partition_to_var (map
, var
);
601 SET_DEF (def_p
, new_var
);
602 set_is_used (new_var
);
609 /* Remove any PHI node which is a virtual PHI. */
612 eliminate_virtual_phis (void)
619 for (phi
= phi_nodes (bb
); phi
; phi
= next
)
621 next
= PHI_CHAIN (phi
);
622 if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi
))))
624 #ifdef ENABLE_CHECKING
626 /* There should be no arguments of this PHI which are in
627 the partition list, or we get incorrect results. */
628 for (i
= 0; i
< PHI_NUM_ARGS (phi
); i
++)
630 tree arg
= PHI_ARG_DEF (phi
, i
);
631 if (TREE_CODE (arg
) == SSA_NAME
632 && is_gimple_reg (SSA_NAME_VAR (arg
)))
634 fprintf (stderr
, "Argument of PHI is not virtual (");
635 print_generic_expr (stderr
, arg
, TDF_SLIM
);
636 fprintf (stderr
, "), but the result is :");
637 print_generic_stmt (stderr
, phi
, TDF_SLIM
);
638 internal_error ("SSA corruption");
642 remove_phi_node (phi
, NULL_TREE
, true);
649 /* This function will rewrite the current program using the variable mapping
650 found in MAP. If the replacement vector VALUES is provided, any
651 occurrences of partitions with non-null entries in the vector will be
652 replaced with the expression in the vector instead of its mapped
656 rewrite_trees (var_map map
, tree
*values
)
660 block_stmt_iterator si
;
665 #ifdef ENABLE_CHECKING
666 /* Search for PHIs where the destination has no partition, but one
667 or more arguments has a partition. This should not happen and can
668 create incorrect code. */
672 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
674 tree T0
= var_to_partition_to_var (map
, PHI_RESULT (phi
));
678 for (i
= 0; i
< PHI_NUM_ARGS (phi
); i
++)
680 tree arg
= PHI_ARG_DEF (phi
, i
);
682 if (TREE_CODE (arg
) == SSA_NAME
683 && var_to_partition (map
, arg
) != NO_PARTITION
)
685 fprintf (stderr
, "Argument of PHI is in a partition :(");
686 print_generic_expr (stderr
, arg
, TDF_SLIM
);
687 fprintf (stderr
, "), but the result is not :");
688 print_generic_stmt (stderr
, phi
, TDF_SLIM
);
689 internal_error ("SSA corruption");
697 /* Replace PHI nodes with any required copies. */
698 g
= new_elim_graph (map
->num_partitions
);
702 for (si
= bsi_start (bb
); !bsi_end_p (si
); )
704 tree stmt
= bsi_stmt (si
);
705 use_operand_p use_p
, copy_use_p
;
707 bool remove
= false, is_copy
= false;
712 ann
= stmt_ann (stmt
);
715 if (TREE_CODE (stmt
) == GIMPLE_MODIFY_STMT
716 && (TREE_CODE (GIMPLE_STMT_OPERAND (stmt
, 1)) == SSA_NAME
))
719 copy_use_p
= NULL_USE_OPERAND_P
;
720 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
722 if (replace_use_variable (map
, use_p
, values
))
731 def_p
= SINGLE_SSA_DEF_OPERAND (stmt
, SSA_OP_DEF
);
735 /* Mark this stmt for removal if it is the list of replaceable
737 if (values
&& values
[SSA_NAME_VERSION (DEF_FROM_PTR (def_p
))])
741 if (replace_def_variable (map
, def_p
, NULL
))
743 /* If both SSA_NAMEs coalesce to the same variable,
744 mark the now redundant copy for removal. */
747 gcc_assert (copy_use_p
!= NULL_USE_OPERAND_P
);
748 if (DEF_FROM_PTR (def_p
) == USE_FROM_PTR (copy_use_p
))
754 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_DEF
)
755 if (replace_def_variable (map
, def_p
, NULL
))
758 /* Remove any stmts marked for removal. */
760 bsi_remove (&si
, true);
764 if (maybe_clean_or_replace_eh_stmt (stmt
, stmt
))
765 tree_purge_dead_eh_edges (bb
);
770 phi
= phi_nodes (bb
);
774 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
775 eliminate_phi (e
, g
);
779 delete_elim_graph (g
);
782 /* These are the local work structures used to determine the best place to
783 insert the copies that were placed on edges by the SSA->normal pass.. */
784 static VEC(edge
,heap
) *edge_leader
;
785 static VEC(tree
,heap
) *stmt_list
;
786 static bitmap leader_has_match
= NULL
;
787 static edge leader_match
= NULL
;
790 /* Pass this function to make_forwarder_block so that all the edges with
791 matching PENDING_STMT lists to 'curr_stmt_list' get redirected. E is the
792 edge to test for a match. */
795 same_stmt_list_p (edge e
)
797 return (e
->aux
== (PTR
) leader_match
) ? true : false;
801 /* Return TRUE if S1 and S2 are equivalent copies. */
804 identical_copies_p (const_tree s1
, const_tree s2
)
806 #ifdef ENABLE_CHECKING
807 gcc_assert (TREE_CODE (s1
) == GIMPLE_MODIFY_STMT
);
808 gcc_assert (TREE_CODE (s2
) == GIMPLE_MODIFY_STMT
);
809 gcc_assert (DECL_P (GIMPLE_STMT_OPERAND (s1
, 0)));
810 gcc_assert (DECL_P (GIMPLE_STMT_OPERAND (s2
, 0)));
813 if (GIMPLE_STMT_OPERAND (s1
, 0) != GIMPLE_STMT_OPERAND (s2
, 0))
816 s1
= GIMPLE_STMT_OPERAND (s1
, 1);
817 s2
= GIMPLE_STMT_OPERAND (s2
, 1);
826 /* Compare the PENDING_STMT list for edges E1 and E2. Return true if the lists
827 contain the same sequence of copies. */
830 identical_stmt_lists_p (const_edge e1
, const_edge e2
)
832 tree t1
= PENDING_STMT (e1
);
833 tree t2
= PENDING_STMT (e2
);
834 tree_stmt_iterator tsi1
, tsi2
;
836 gcc_assert (TREE_CODE (t1
) == STATEMENT_LIST
);
837 gcc_assert (TREE_CODE (t2
) == STATEMENT_LIST
);
839 for (tsi1
= tsi_start (t1
), tsi2
= tsi_start (t2
);
840 !tsi_end_p (tsi1
) && !tsi_end_p (tsi2
);
841 tsi_next (&tsi1
), tsi_next (&tsi2
))
843 if (!identical_copies_p (tsi_stmt (tsi1
), tsi_stmt (tsi2
)))
847 if (!tsi_end_p (tsi1
) || ! tsi_end_p (tsi2
))
854 /* Allocate data structures used in analyze_edges_for_bb. */
857 init_analyze_edges_for_bb (void)
859 edge_leader
= VEC_alloc (edge
, heap
, 25);
860 stmt_list
= VEC_alloc (tree
, heap
, 25);
861 leader_has_match
= BITMAP_ALLOC (NULL
);
865 /* Free data structures used in analyze_edges_for_bb. */
868 fini_analyze_edges_for_bb (void)
870 VEC_free (edge
, heap
, edge_leader
);
871 VEC_free (tree
, heap
, stmt_list
);
872 BITMAP_FREE (leader_has_match
);
875 /* A helper function to be called via walk_tree. Return DATA if it is
876 contained in subtree TP. */
879 contains_tree_r (tree
* tp
, int *walk_subtrees
, void *data
)
890 /* A threshold for the number of insns contained in the latch block.
891 It is used to prevent blowing the loop with too many copies from
893 #define MAX_STMTS_IN_LATCH 2
895 /* Return TRUE if the stmts on SINGLE-EDGE can be moved to the
896 body of the loop. This should be permitted only if SINGLE-EDGE is a
897 single-basic-block latch edge and thus cleaning the latch will help
898 to create a single-basic-block loop. Otherwise return FALSE. */
901 process_single_block_loop_latch (edge single_edge
)
904 basic_block b_exit
, b_pheader
, b_loop
= single_edge
->src
;
907 block_stmt_iterator bsi
, bsi_exit
;
908 tree_stmt_iterator tsi
;
910 unsigned int count
= 0;
912 if (single_edge
== NULL
|| (single_edge
->dest
!= single_edge
->src
)
913 || (EDGE_COUNT (b_loop
->succs
) != 2)
914 || (EDGE_COUNT (b_loop
->preds
) != 2))
917 /* Get the stmts on the latch edge. */
918 stmts
= PENDING_STMT (single_edge
);
920 /* Find the successor edge which is not the latch edge. */
921 FOR_EACH_EDGE (e
, ei
, b_loop
->succs
)
922 if (e
->dest
!= b_loop
)
927 /* Check that the exit block has only the loop as a predecessor,
928 and that there are no pending stmts on that edge as well. */
929 if (EDGE_COUNT (b_exit
->preds
) != 1 || PENDING_STMT (e
))
932 /* Find the predecessor edge which is not the latch edge. */
933 FOR_EACH_EDGE (e
, ei
, b_loop
->preds
)
934 if (e
->src
!= b_loop
)
939 if (b_exit
== b_pheader
|| b_exit
== b_loop
|| b_pheader
== b_loop
)
942 bsi_exit
= bsi_after_labels (b_exit
);
944 /* Get the last stmt in the loop body. */
945 bsi
= bsi_last (single_edge
->src
);
946 stmt
= bsi_stmt (bsi
);
948 if (TREE_CODE (stmt
) != COND_EXPR
)
951 expr
= COND_EXPR_COND (stmt
);
952 /* Iterate over the insns on the latch and count them. */
953 for (tsi
= tsi_start (stmts
); !tsi_end_p (tsi
); tsi_next (&tsi
))
955 tree stmt1
= tsi_stmt (tsi
);
959 /* Check that the condition does not contain any new definition
960 created in the latch as the stmts from the latch intended
962 if (TREE_CODE (stmt1
) != GIMPLE_MODIFY_STMT
)
964 var
= GIMPLE_STMT_OPERAND (stmt1
, 0);
965 if (TREE_THIS_VOLATILE (var
)
966 || TYPE_VOLATILE (TREE_TYPE (var
))
967 || walk_tree (&expr
, contains_tree_r
, var
, NULL
))
970 /* Check that the latch does not contain more than MAX_STMTS_IN_LATCH
971 insns. The purpose of this restriction is to prevent blowing the
972 loop with too many copies from the latch. */
973 if (count
> MAX_STMTS_IN_LATCH
)
976 /* Apply the transformation - clean up the latch block:
981 if (cond) goto L2 else goto L3;
995 if (cond) goto L1 else goto L2;
1000 for (tsi
= tsi_start (stmts
); !tsi_end_p (tsi
); tsi_next (&tsi
))
1002 tree stmt1
= tsi_stmt (tsi
);
1003 tree var
, tmp_var
, copy
;
1005 /* Create a new variable to load back the value of var in case
1006 we exit the loop. */
1007 var
= GIMPLE_STMT_OPERAND (stmt1
, 0);
1008 tmp_var
= create_temp (var
);
1009 copy
= build2 (GIMPLE_MODIFY_STMT
, TREE_TYPE (tmp_var
), tmp_var
, var
);
1010 set_is_used (tmp_var
);
1011 bsi_insert_before (&bsi
, copy
, BSI_SAME_STMT
);
1012 copy
= build2 (GIMPLE_MODIFY_STMT
, TREE_TYPE (tmp_var
), var
, tmp_var
);
1013 bsi_insert_before (&bsi_exit
, copy
, BSI_SAME_STMT
);
1016 PENDING_STMT (single_edge
) = 0;
1017 /* Insert the new stmts to the loop body. */
1018 bsi_insert_before (&bsi
, stmts
, BSI_NEW_STMT
);
1022 "\nCleaned-up latch block of loop with single BB: %d\n\n",
1023 single_edge
->dest
->index
);
1028 /* Look at all the incoming edges to block BB, and decide where the best place
1029 to insert the stmts on each edge are, and perform those insertions. */
1032 analyze_edges_for_bb (basic_block bb
)
1038 bool have_opportunity
;
1039 block_stmt_iterator bsi
;
1041 edge single_edge
= NULL
;
1047 /* Blocks which contain at least one abnormal edge cannot use
1048 make_forwarder_block. Look for these blocks, and commit any PENDING_STMTs
1049 found on edges in these block. */
1050 have_opportunity
= true;
1051 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1052 if (e
->flags
& EDGE_ABNORMAL
)
1054 have_opportunity
= false;
1058 if (!have_opportunity
)
1060 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1061 if (PENDING_STMT (e
))
1062 bsi_commit_one_edge_insert (e
, NULL
);
1066 /* Find out how many edges there are with interesting pending stmts on them.
1067 Commit the stmts on edges we are not interested in. */
1068 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1070 if (PENDING_STMT (e
))
1072 gcc_assert (!(e
->flags
& EDGE_ABNORMAL
));
1073 if (e
->flags
& EDGE_FALLTHRU
)
1075 bsi
= bsi_start (e
->src
);
1076 if (!bsi_end_p (bsi
))
1078 stmt
= bsi_stmt (bsi
);
1080 gcc_assert (stmt
!= NULL_TREE
);
1081 is_label
= (TREE_CODE (stmt
) == LABEL_EXPR
);
1082 /* Punt if it has non-label stmts, or isn't local. */
1083 if (!is_label
|| DECL_NONLOCAL (TREE_OPERAND (stmt
, 0))
1084 || !bsi_end_p (bsi
))
1086 bsi_commit_one_edge_insert (e
, NULL
);
1096 /* If there aren't at least 2 edges, no sharing will happen. */
1101 /* Add stmts to the edge unless processed specially as a
1102 single-block loop latch edge. */
1103 if (!process_single_block_loop_latch (single_edge
))
1104 bsi_commit_one_edge_insert (single_edge
, NULL
);
1109 /* Ensure that we have empty worklists. */
1110 #ifdef ENABLE_CHECKING
1111 gcc_assert (VEC_length (edge
, edge_leader
) == 0);
1112 gcc_assert (VEC_length (tree
, stmt_list
) == 0);
1113 gcc_assert (bitmap_empty_p (leader_has_match
));
1116 /* Find the "leader" block for each set of unique stmt lists. Preference is
1117 given to FALLTHRU blocks since they would need a GOTO to arrive at another
1118 block. The leader edge destination is the block which all the other edges
1119 with the same stmt list will be redirected to. */
1120 have_opportunity
= false;
1121 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1123 if (PENDING_STMT (e
))
1127 /* Look for the same stmt list in edge leaders list. */
1128 for (x
= 0; VEC_iterate (edge
, edge_leader
, x
, leader
); x
++)
1130 if (identical_stmt_lists_p (leader
, e
))
1132 /* Give this edge the same stmt list pointer. */
1133 PENDING_STMT (e
) = NULL
;
1135 bitmap_set_bit (leader_has_match
, x
);
1136 have_opportunity
= found
= true;
1141 /* If no similar stmt list, add this edge to the leader list. */
1144 VEC_safe_push (edge
, heap
, edge_leader
, e
);
1145 VEC_safe_push (tree
, heap
, stmt_list
, PENDING_STMT (e
));
1150 /* If there are no similar lists, just issue the stmts. */
1151 if (!have_opportunity
)
1153 for (x
= 0; VEC_iterate (edge
, edge_leader
, x
, leader
); x
++)
1154 bsi_commit_one_edge_insert (leader
, NULL
);
1155 VEC_truncate (edge
, edge_leader
, 0);
1156 VEC_truncate (tree
, stmt_list
, 0);
1157 bitmap_clear (leader_has_match
);
1162 fprintf (dump_file
, "\nOpportunities in BB %d for stmt/block reduction:\n",
1165 /* For each common list, create a forwarding block and issue the stmt's
1167 for (x
= 0; VEC_iterate (edge
, edge_leader
, x
, leader
); x
++)
1168 if (bitmap_bit_p (leader_has_match
, x
))
1171 block_stmt_iterator bsi
;
1172 tree curr_stmt_list
;
1174 leader_match
= leader
;
1176 /* The tree_* cfg manipulation routines use the PENDING_EDGE field
1177 for various PHI manipulations, so it gets cleared when calls are
1178 made to make_forwarder_block(). So make sure the edge is clear,
1179 and use the saved stmt list. */
1180 PENDING_STMT (leader
) = NULL
;
1181 leader
->aux
= leader
;
1182 curr_stmt_list
= VEC_index (tree
, stmt_list
, x
);
1184 new_edge
= make_forwarder_block (leader
->dest
, same_stmt_list_p
,
1186 bb
= new_edge
->dest
;
1189 fprintf (dump_file
, "Splitting BB %d for Common stmt list. ",
1190 leader
->dest
->index
);
1191 fprintf (dump_file
, "Original block is now BB%d.\n", bb
->index
);
1192 print_generic_stmt (dump_file
, curr_stmt_list
, TDF_VOPS
);
1195 FOR_EACH_EDGE (e
, ei
, new_edge
->src
->preds
)
1199 fprintf (dump_file
, " Edge (%d->%d) lands here.\n",
1200 e
->src
->index
, e
->dest
->index
);
1203 bsi
= bsi_last (leader
->dest
);
1204 bsi_insert_after (&bsi
, curr_stmt_list
, BSI_NEW_STMT
);
1206 leader_match
= NULL
;
1207 /* We should never get a new block now. */
1211 PENDING_STMT (leader
) = VEC_index (tree
, stmt_list
, x
);
1212 bsi_commit_one_edge_insert (leader
, NULL
);
1216 /* Clear the working data structures. */
1217 VEC_truncate (edge
, edge_leader
, 0);
1218 VEC_truncate (tree
, stmt_list
, 0);
1219 bitmap_clear (leader_has_match
);
1223 /* This function will analyze the insertions which were performed on edges,
1224 and decide whether they should be left on that edge, or whether it is more
1225 efficient to emit some subset of them in a single block. All stmts are
1226 inserted somewhere. */
1229 perform_edge_inserts (void)
1234 fprintf(dump_file
, "Analyzing Edge Insertions.\n");
1236 /* analyze_edges_for_bb calls make_forwarder_block, which tries to
1237 incrementally update the dominator information. Since we don't
1238 need dominator information after this pass, go ahead and free the
1239 dominator information. */
1240 free_dominance_info (CDI_DOMINATORS
);
1241 free_dominance_info (CDI_POST_DOMINATORS
);
1243 /* Allocate data structures used in analyze_edges_for_bb. */
1244 init_analyze_edges_for_bb ();
1247 analyze_edges_for_bb (bb
);
1249 analyze_edges_for_bb (EXIT_BLOCK_PTR
);
1251 /* Free data structures used in analyze_edges_for_bb. */
1252 fini_analyze_edges_for_bb ();
1254 #ifdef ENABLE_CHECKING
1260 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1262 if (PENDING_STMT (e
))
1263 error (" Pending stmts not issued on PRED edge (%d, %d)\n",
1264 e
->src
->index
, e
->dest
->index
);
1266 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1268 if (PENDING_STMT (e
))
1269 error (" Pending stmts not issued on SUCC edge (%d, %d)\n",
1270 e
->src
->index
, e
->dest
->index
);
1273 FOR_EACH_EDGE (e
, ei
, ENTRY_BLOCK_PTR
->succs
)
1275 if (PENDING_STMT (e
))
1276 error (" Pending stmts not issued on ENTRY edge (%d, %d)\n",
1277 e
->src
->index
, e
->dest
->index
);
1279 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR
->preds
)
1281 if (PENDING_STMT (e
))
1282 error (" Pending stmts not issued on EXIT edge (%d, %d)\n",
1283 e
->src
->index
, e
->dest
->index
);
1290 /* Remove the ssa-names in the current function and translate them into normal
1291 compiler variables. PERFORM_TER is true if Temporary Expression Replacement
1292 should also be used. */
1295 remove_ssa_form (bool perform_ter
)
1299 tree
*values
= NULL
;
1302 map
= coalesce_ssa_name ();
1304 /* Return to viewing the variable list as just all reference variables after
1305 coalescing has been performed. */
1306 partition_view_normal (map
, false);
1308 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1310 fprintf (dump_file
, "After Coalescing:\n");
1311 dump_var_map (dump_file
, map
);
1316 values
= find_replaceable_exprs (map
);
1317 if (values
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
1318 dump_replaceable_exprs (dump_file
, values
);
1321 /* Assign real variables to the partitions now. */
1324 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1326 fprintf (dump_file
, "After Base variable replacement:\n");
1327 dump_var_map (dump_file
, map
);
1330 rewrite_trees (map
, values
);
1335 /* Remove PHI nodes which have been translated back to real variables. */
1338 for (phi
= phi_nodes (bb
); phi
; phi
= next
)
1340 next
= PHI_CHAIN (phi
);
1341 remove_phi_node (phi
, NULL_TREE
, true);
1345 /* If any copies were inserted on edges, analyze and insert them now. */
1346 perform_edge_inserts ();
1348 delete_var_map (map
);
1352 /* Search every PHI node for arguments associated with backedges which
1353 we can trivially determine will need a copy (the argument is either
1354 not an SSA_NAME or the argument has a different underlying variable
1355 than the PHI result).
1357 Insert a copy from the PHI argument to a new destination at the
1358 end of the block with the backedge to the top of the loop. Update
1359 the PHI argument to reference this new destination. */
1362 insert_backedge_copies (void)
1370 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
1372 tree result
= PHI_RESULT (phi
);
1376 if (!is_gimple_reg (result
))
1379 result_var
= SSA_NAME_VAR (result
);
1380 for (i
= 0; i
< PHI_NUM_ARGS (phi
); i
++)
1382 tree arg
= PHI_ARG_DEF (phi
, i
);
1383 edge e
= PHI_ARG_EDGE (phi
, i
);
1385 /* If the argument is not an SSA_NAME, then we will need a
1386 constant initialization. If the argument is an SSA_NAME with
1387 a different underlying variable then a copy statement will be
1389 if ((e
->flags
& EDGE_DFS_BACK
)
1390 && (TREE_CODE (arg
) != SSA_NAME
1391 || SSA_NAME_VAR (arg
) != result_var
))
1393 tree stmt
, name
, last
= NULL
;
1394 block_stmt_iterator bsi
;
1396 bsi
= bsi_last (PHI_ARG_EDGE (phi
, i
)->src
);
1397 if (!bsi_end_p (bsi
))
1398 last
= bsi_stmt (bsi
);
1400 /* In theory the only way we ought to get back to the
1401 start of a loop should be with a COND_EXPR or GOTO_EXPR.
1402 However, better safe than sorry.
1403 If the block ends with a control statement or
1404 something that might throw, then we have to
1405 insert this assignment before the last
1406 statement. Else insert it after the last statement. */
1407 if (last
&& stmt_ends_bb_p (last
))
1409 /* If the last statement in the block is the definition
1410 site of the PHI argument, then we can't insert
1411 anything after it. */
1412 if (TREE_CODE (arg
) == SSA_NAME
1413 && SSA_NAME_DEF_STMT (arg
) == last
)
1417 /* Create a new instance of the underlying variable of the
1419 stmt
= build_gimple_modify_stmt (NULL_TREE
,
1420 PHI_ARG_DEF (phi
, i
));
1421 name
= make_ssa_name (result_var
, stmt
);
1422 GIMPLE_STMT_OPERAND (stmt
, 0) = name
;
1424 /* Insert the new statement into the block and update
1426 if (last
&& stmt_ends_bb_p (last
))
1427 bsi_insert_before (&bsi
, stmt
, BSI_NEW_STMT
);
1429 bsi_insert_after (&bsi
, stmt
, BSI_NEW_STMT
);
1430 SET_PHI_ARG_DEF (phi
, i
, name
);
1437 /* Take the current function out of SSA form, translating PHIs as described in
1438 R. Morgan, ``Building an Optimizing Compiler'',
1439 Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
1442 rewrite_out_of_ssa (void)
1444 /* If elimination of a PHI requires inserting a copy on a backedge,
1445 then we will have to split the backedge which has numerous
1446 undesirable performance effects.
1448 A significant number of such cases can be handled here by inserting
1449 copies into the loop itself. */
1450 insert_backedge_copies ();
1452 eliminate_virtual_phis ();
1454 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1455 dump_tree_cfg (dump_file
, dump_flags
& ~TDF_DETAILS
);
1457 remove_ssa_form (flag_tree_ter
&& !flag_mudflap
);
1459 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1460 dump_tree_cfg (dump_file
, dump_flags
& ~TDF_DETAILS
);
1462 cfun
->gimple_df
->in_ssa_p
= false;
1467 /* Define the parameters of the out of SSA pass. */
1469 struct gimple_opt_pass pass_del_ssa
=
1473 "optimized", /* name */
1475 rewrite_out_of_ssa
, /* execute */
1478 0, /* static_pass_number */
1479 TV_TREE_SSA_TO_NORMAL
, /* tv_id */
1480 PROP_cfg
| PROP_ssa
, /* properties_required */
1481 0, /* properties_provided */
1482 /* ??? If TER is enabled, we also kill gimple. */
1483 PROP_ssa
, /* properties_destroyed */
1484 TODO_verify_ssa
| TODO_verify_flow
1485 | TODO_verify_stmts
, /* todo_flags_start */
1488 | TODO_remove_unused_locals
/* todo_flags_finish */