1 /* Convert a program in SSA form into Normal form.
2 Copyright (C) 2004, 2005, 2006, 2007 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 add_referenced_var (tmp
);
124 /* add_referenced_var will create the annotation and set up some
125 of the flags in the annotation. However, some flags we need to
126 inherit from our original variable. */
127 set_symbol_mem_tag (tmp
, symbol_mem_tag (t
));
128 if (is_call_clobbered (t
))
129 mark_call_clobbered (tmp
, var_ann (t
)->escape_mask
);
135 /* This helper function fill insert a copy from a constant or variable SRC to
136 variable DEST on edge E. */
139 insert_copy_on_edge (edge e
, tree dest
, tree src
)
143 copy
= build_gimple_modify_stmt (dest
, src
);
146 if (TREE_CODE (src
) == ADDR_EXPR
)
147 src
= TREE_OPERAND (src
, 0);
148 if (TREE_CODE (src
) == VAR_DECL
|| TREE_CODE (src
) == PARM_DECL
)
151 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
154 "Inserting a copy on edge BB%d->BB%d :",
157 print_generic_expr (dump_file
, copy
, dump_flags
);
158 fprintf (dump_file
, "\n");
161 bsi_insert_on_edge (e
, copy
);
165 /* Create an elimination graph with SIZE nodes and associated data
169 new_elim_graph (int size
)
171 elim_graph g
= (elim_graph
) xmalloc (sizeof (struct _elim_graph
));
173 g
->nodes
= VEC_alloc (tree
, heap
, 30);
174 g
->const_copies
= VEC_alloc (tree
, heap
, 20);
175 g
->edge_list
= VEC_alloc (int, heap
, 20);
176 g
->stack
= VEC_alloc (int, heap
, 30);
178 g
->visited
= sbitmap_alloc (size
);
184 /* Empty elimination graph G. */
187 clear_elim_graph (elim_graph g
)
189 VEC_truncate (tree
, g
->nodes
, 0);
190 VEC_truncate (int, g
->edge_list
, 0);
194 /* Delete elimination graph G. */
197 delete_elim_graph (elim_graph g
)
199 sbitmap_free (g
->visited
);
200 VEC_free (int, heap
, g
->stack
);
201 VEC_free (int, heap
, g
->edge_list
);
202 VEC_free (tree
, heap
, g
->const_copies
);
203 VEC_free (tree
, heap
, g
->nodes
);
208 /* Return the number of nodes in graph G. */
211 elim_graph_size (elim_graph g
)
213 return VEC_length (tree
, g
->nodes
);
217 /* Add NODE to graph G, if it doesn't exist already. */
220 elim_graph_add_node (elim_graph g
, tree node
)
225 for (x
= 0; VEC_iterate (tree
, g
->nodes
, x
, t
); x
++)
228 VEC_safe_push (tree
, heap
, g
->nodes
, node
);
232 /* Add the edge PRED->SUCC to graph G. */
235 elim_graph_add_edge (elim_graph g
, int pred
, int succ
)
237 VEC_safe_push (int, heap
, g
->edge_list
, pred
);
238 VEC_safe_push (int, heap
, g
->edge_list
, succ
);
242 /* Remove an edge from graph G for which NODE is the predecessor, and
243 return the successor node. -1 is returned if there is no such edge. */
246 elim_graph_remove_succ_edge (elim_graph g
, int node
)
250 for (x
= 0; x
< VEC_length (int, g
->edge_list
); x
+= 2)
251 if (VEC_index (int, g
->edge_list
, x
) == node
)
253 VEC_replace (int, g
->edge_list
, x
, -1);
254 y
= VEC_index (int, g
->edge_list
, x
+ 1);
255 VEC_replace (int, g
->edge_list
, x
+ 1, -1);
262 /* Find all the nodes in GRAPH which are successors to NODE in the
263 edge list. VAR will hold the partition number found. CODE is the
264 code fragment executed for every node found. */
266 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, CODE) \
270 for (x_ = 0; x_ < VEC_length (int, (GRAPH)->edge_list); x_ += 2) \
272 y_ = VEC_index (int, (GRAPH)->edge_list, x_); \
275 (VAR) = VEC_index (int, (GRAPH)->edge_list, x_ + 1); \
281 /* Find all the nodes which are predecessors of NODE in the edge list for
282 GRAPH. VAR will hold the partition number found. CODE is the
283 code fragment executed for every node found. */
285 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, CODE) \
289 for (x_ = 0; x_ < VEC_length (int, (GRAPH)->edge_list); x_ += 2) \
291 y_ = VEC_index (int, (GRAPH)->edge_list, x_ + 1); \
294 (VAR) = VEC_index (int, (GRAPH)->edge_list, x_); \
300 /* Add T to elimination graph G. */
303 eliminate_name (elim_graph g
, tree T
)
305 elim_graph_add_node (g
, T
);
309 /* Build elimination graph G for basic block BB on incoming PHI edge
313 eliminate_build (elim_graph g
, basic_block B
)
319 clear_elim_graph (g
);
321 for (phi
= phi_nodes (B
); phi
; phi
= PHI_CHAIN (phi
))
323 T0
= var_to_partition_to_var (g
->map
, PHI_RESULT (phi
));
325 /* Ignore results which are not in partitions. */
329 Ti
= PHI_ARG_DEF (phi
, g
->e
->dest_idx
);
331 /* If this argument is a constant, or a SSA_NAME which is being
332 left in SSA form, just queue a copy to be emitted on this
334 if (!phi_ssa_name_p (Ti
)
335 || (TREE_CODE (Ti
) == SSA_NAME
336 && var_to_partition (g
->map
, Ti
) == NO_PARTITION
))
338 /* Save constant copies until all other copies have been emitted
340 VEC_safe_push (tree
, heap
, g
->const_copies
, T0
);
341 VEC_safe_push (tree
, heap
, g
->const_copies
, Ti
);
345 Ti
= var_to_partition_to_var (g
->map
, Ti
);
348 eliminate_name (g
, T0
);
349 eliminate_name (g
, Ti
);
350 p0
= var_to_partition (g
->map
, T0
);
351 pi
= var_to_partition (g
->map
, Ti
);
352 elim_graph_add_edge (g
, p0
, pi
);
359 /* Push successors of T onto the elimination stack for G. */
362 elim_forward (elim_graph g
, int T
)
365 SET_BIT (g
->visited
, T
);
366 FOR_EACH_ELIM_GRAPH_SUCC (g
, T
, S
,
368 if (!TEST_BIT (g
->visited
, S
))
371 VEC_safe_push (int, heap
, g
->stack
, T
);
375 /* Return 1 if there unvisited predecessors of T in graph G. */
378 elim_unvisited_predecessor (elim_graph g
, int T
)
381 FOR_EACH_ELIM_GRAPH_PRED (g
, T
, P
,
383 if (!TEST_BIT (g
->visited
, P
))
389 /* Process predecessors first, and insert a copy. */
392 elim_backward (elim_graph g
, int T
)
395 SET_BIT (g
->visited
, T
);
396 FOR_EACH_ELIM_GRAPH_PRED (g
, T
, P
,
398 if (!TEST_BIT (g
->visited
, P
))
400 elim_backward (g
, P
);
401 insert_copy_on_edge (g
->e
,
402 partition_to_var (g
->map
, P
),
403 partition_to_var (g
->map
, T
));
408 /* Insert required copies for T in graph G. Check for a strongly connected
409 region, and create a temporary to break the cycle if one is found. */
412 elim_create (elim_graph g
, int T
)
417 if (elim_unvisited_predecessor (g
, T
))
419 U
= create_temp (partition_to_var (g
->map
, T
));
420 insert_copy_on_edge (g
->e
, U
, partition_to_var (g
->map
, T
));
421 FOR_EACH_ELIM_GRAPH_PRED (g
, T
, P
,
423 if (!TEST_BIT (g
->visited
, P
))
425 elim_backward (g
, P
);
426 insert_copy_on_edge (g
->e
, partition_to_var (g
->map
, P
), U
);
432 S
= elim_graph_remove_succ_edge (g
, T
);
435 SET_BIT (g
->visited
, T
);
436 insert_copy_on_edge (g
->e
,
437 partition_to_var (g
->map
, T
),
438 partition_to_var (g
->map
, S
));
445 /* Eliminate all the phi nodes on edge E in graph G. */
448 eliminate_phi (edge e
, elim_graph g
)
451 basic_block B
= e
->dest
;
453 gcc_assert (VEC_length (tree
, g
->const_copies
) == 0);
455 /* Abnormal edges already have everything coalesced. */
456 if (e
->flags
& EDGE_ABNORMAL
)
461 eliminate_build (g
, B
);
463 if (elim_graph_size (g
) != 0)
467 sbitmap_zero (g
->visited
);
468 VEC_truncate (int, g
->stack
, 0);
470 for (x
= 0; VEC_iterate (tree
, g
->nodes
, x
, var
); x
++)
472 int p
= var_to_partition (g
->map
, var
);
473 if (!TEST_BIT (g
->visited
, p
))
477 sbitmap_zero (g
->visited
);
478 while (VEC_length (int, g
->stack
) > 0)
480 x
= VEC_pop (int, g
->stack
);
481 if (!TEST_BIT (g
->visited
, x
))
486 /* If there are any pending constant copies, issue them now. */
487 while (VEC_length (tree
, g
->const_copies
) > 0)
490 src
= VEC_pop (tree
, g
->const_copies
);
491 dest
= VEC_pop (tree
, g
->const_copies
);
492 insert_copy_on_edge (e
, dest
, src
);
497 /* Take the ssa-name var_map MAP, and assign real variables to each
501 assign_vars (var_map map
)
507 num
= num_var_partitions (map
);
508 for (x
= 0; x
< num
; x
++)
510 var
= partition_to_var (map
, x
);
511 if (TREE_CODE (var
) != SSA_NAME
)
514 /* It must already be coalesced. */
515 gcc_assert (ann
->out_of_ssa_tag
== 1);
516 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
518 fprintf (dump_file
, "partition %d already has variable ", x
);
519 print_generic_expr (dump_file
, var
, TDF_SLIM
);
520 fprintf (dump_file
, " assigned to it.\n");
525 root
= SSA_NAME_VAR (var
);
526 ann
= var_ann (root
);
527 /* If ROOT is already associated, create a new one. */
528 if (ann
->out_of_ssa_tag
)
530 root
= create_temp (root
);
531 ann
= var_ann (root
);
533 /* ROOT has not been coalesced yet, so use it. */
534 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
536 fprintf (dump_file
, "Partition %d is assigned to var ", x
);
537 print_generic_stmt (dump_file
, root
, TDF_SLIM
);
539 change_partition_var (map
, root
, x
);
545 /* Replace use operand P with whatever variable it has been rewritten to based
546 on the partitions in MAP. EXPR is an optional expression vector over SSA
547 versions which is used to replace P with an expression instead of a variable.
548 If the stmt is changed, return true. */
551 replace_use_variable (var_map map
, use_operand_p p
, tree
*expr
)
554 tree var
= USE_FROM_PTR (p
);
556 /* Check if we are replacing this variable with an expression. */
559 int version
= SSA_NAME_VERSION (var
);
562 tree new_expr
= GIMPLE_STMT_OPERAND (expr
[version
], 1);
563 SET_USE (p
, new_expr
);
565 /* Clear the stmt's RHS, or GC might bite us. */
566 GIMPLE_STMT_OPERAND (expr
[version
], 1) = NULL_TREE
;
571 new_var
= var_to_partition_to_var (map
, var
);
574 SET_USE (p
, new_var
);
575 set_is_used (new_var
);
582 /* Replace def operand DEF_P with whatever variable it has been rewritten to
583 based on the partitions in MAP. EXPR is an optional expression vector over
584 SSA versions which is used to replace DEF_P with an expression instead of a
585 variable. If the stmt is changed, return true. */
588 replace_def_variable (var_map map
, def_operand_p def_p
, tree
*expr
)
591 tree var
= DEF_FROM_PTR (def_p
);
593 /* Do nothing if we are replacing this variable with an expression. */
594 if (expr
&& expr
[SSA_NAME_VERSION (var
)])
597 new_var
= var_to_partition_to_var (map
, var
);
600 SET_DEF (def_p
, new_var
);
601 set_is_used (new_var
);
608 /* Remove any PHI node which is a virtual PHI. */
611 eliminate_virtual_phis (void)
618 for (phi
= phi_nodes (bb
); phi
; phi
= next
)
620 next
= PHI_CHAIN (phi
);
621 if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi
))))
623 #ifdef ENABLE_CHECKING
625 /* There should be no arguments of this PHI which are in
626 the partition list, or we get incorrect results. */
627 for (i
= 0; i
< PHI_NUM_ARGS (phi
); i
++)
629 tree arg
= PHI_ARG_DEF (phi
, i
);
630 if (TREE_CODE (arg
) == SSA_NAME
631 && is_gimple_reg (SSA_NAME_VAR (arg
)))
633 fprintf (stderr
, "Argument of PHI is not virtual (");
634 print_generic_expr (stderr
, arg
, TDF_SLIM
);
635 fprintf (stderr
, "), but the result is :");
636 print_generic_stmt (stderr
, phi
, TDF_SLIM
);
637 internal_error ("SSA corruption");
641 remove_phi_node (phi
, NULL_TREE
, true);
648 /* This function will rewrite the current program using the variable mapping
649 found in MAP. If the replacement vector VALUES is provided, any
650 occurrences of partitions with non-null entries in the vector will be
651 replaced with the expression in the vector instead of its mapped
655 rewrite_trees (var_map map
, tree
*values
)
659 block_stmt_iterator si
;
664 #ifdef ENABLE_CHECKING
665 /* Search for PHIs where the destination has no partition, but one
666 or more arguments has a partition. This should not happen and can
667 create incorrect code. */
671 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
673 tree T0
= var_to_partition_to_var (map
, PHI_RESULT (phi
));
677 for (i
= 0; i
< PHI_NUM_ARGS (phi
); i
++)
679 tree arg
= PHI_ARG_DEF (phi
, i
);
681 if (TREE_CODE (arg
) == SSA_NAME
682 && var_to_partition (map
, arg
) != NO_PARTITION
)
684 fprintf (stderr
, "Argument of PHI is in a partition :(");
685 print_generic_expr (stderr
, arg
, TDF_SLIM
);
686 fprintf (stderr
, "), but the result is not :");
687 print_generic_stmt (stderr
, phi
, TDF_SLIM
);
688 internal_error ("SSA corruption");
696 /* Replace PHI nodes with any required copies. */
697 g
= new_elim_graph (map
->num_partitions
);
701 for (si
= bsi_start (bb
); !bsi_end_p (si
); )
703 tree stmt
= bsi_stmt (si
);
704 use_operand_p use_p
, copy_use_p
;
706 bool remove
= false, is_copy
= false;
711 ann
= stmt_ann (stmt
);
714 if (TREE_CODE (stmt
) == GIMPLE_MODIFY_STMT
715 && (TREE_CODE (GIMPLE_STMT_OPERAND (stmt
, 1)) == SSA_NAME
))
718 copy_use_p
= NULL_USE_OPERAND_P
;
719 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
721 if (replace_use_variable (map
, use_p
, values
))
730 def_p
= SINGLE_SSA_DEF_OPERAND (stmt
, SSA_OP_DEF
);
734 /* Mark this stmt for removal if it is the list of replaceable
736 if (values
&& values
[SSA_NAME_VERSION (DEF_FROM_PTR (def_p
))])
740 if (replace_def_variable (map
, def_p
, NULL
))
742 /* If both SSA_NAMEs coalesce to the same variable,
743 mark the now redundant copy for removal. */
746 gcc_assert (copy_use_p
!= NULL_USE_OPERAND_P
);
747 if (DEF_FROM_PTR (def_p
) == USE_FROM_PTR (copy_use_p
))
753 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_DEF
)
754 if (replace_def_variable (map
, def_p
, NULL
))
757 /* Remove any stmts marked for removal. */
759 bsi_remove (&si
, true);
764 phi
= phi_nodes (bb
);
768 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
769 eliminate_phi (e
, g
);
773 delete_elim_graph (g
);
776 /* These are the local work structures used to determine the best place to
777 insert the copies that were placed on edges by the SSA->normal pass.. */
778 static VEC(edge
,heap
) *edge_leader
;
779 static VEC(tree
,heap
) *stmt_list
;
780 static bitmap leader_has_match
= NULL
;
781 static edge leader_match
= NULL
;
784 /* Pass this function to make_forwarder_block so that all the edges with
785 matching PENDING_STMT lists to 'curr_stmt_list' get redirected. E is the
786 edge to test for a match. */
789 same_stmt_list_p (edge e
)
791 return (e
->aux
== (PTR
) leader_match
) ? true : false;
795 /* Return TRUE if S1 and S2 are equivalent copies. */
798 identical_copies_p (tree s1
, tree s2
)
800 #ifdef ENABLE_CHECKING
801 gcc_assert (TREE_CODE (s1
) == GIMPLE_MODIFY_STMT
);
802 gcc_assert (TREE_CODE (s2
) == GIMPLE_MODIFY_STMT
);
803 gcc_assert (DECL_P (GIMPLE_STMT_OPERAND (s1
, 0)));
804 gcc_assert (DECL_P (GIMPLE_STMT_OPERAND (s2
, 0)));
807 if (GIMPLE_STMT_OPERAND (s1
, 0) != GIMPLE_STMT_OPERAND (s2
, 0))
810 s1
= GIMPLE_STMT_OPERAND (s1
, 1);
811 s2
= GIMPLE_STMT_OPERAND (s2
, 1);
820 /* Compare the PENDING_STMT list for edges E1 and E2. Return true if the lists
821 contain the same sequence of copies. */
824 identical_stmt_lists_p (edge e1
, edge e2
)
826 tree t1
= PENDING_STMT (e1
);
827 tree t2
= PENDING_STMT (e2
);
828 tree_stmt_iterator tsi1
, tsi2
;
830 gcc_assert (TREE_CODE (t1
) == STATEMENT_LIST
);
831 gcc_assert (TREE_CODE (t2
) == STATEMENT_LIST
);
833 for (tsi1
= tsi_start (t1
), tsi2
= tsi_start (t2
);
834 !tsi_end_p (tsi1
) && !tsi_end_p (tsi2
);
835 tsi_next (&tsi1
), tsi_next (&tsi2
))
837 if (!identical_copies_p (tsi_stmt (tsi1
), tsi_stmt (tsi2
)))
841 if (!tsi_end_p (tsi1
) || ! tsi_end_p (tsi2
))
848 /* Allocate data structures used in analyze_edges_for_bb. */
851 init_analyze_edges_for_bb (void)
853 edge_leader
= VEC_alloc (edge
, heap
, 25);
854 stmt_list
= VEC_alloc (tree
, heap
, 25);
855 leader_has_match
= BITMAP_ALLOC (NULL
);
859 /* Free data structures used in analyze_edges_for_bb. */
862 fini_analyze_edges_for_bb (void)
864 VEC_free (edge
, heap
, edge_leader
);
865 VEC_free (tree
, heap
, stmt_list
);
866 BITMAP_FREE (leader_has_match
);
870 /* Look at all the incoming edges to block BB, and decide where the best place
871 to insert the stmts on each edge are, and perform those insertions. */
874 analyze_edges_for_bb (basic_block bb
)
880 bool have_opportunity
;
881 block_stmt_iterator bsi
;
883 edge single_edge
= NULL
;
889 /* Blocks which contain at least one abnormal edge cannot use
890 make_forwarder_block. Look for these blocks, and commit any PENDING_STMTs
891 found on edges in these block. */
892 have_opportunity
= true;
893 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
894 if (e
->flags
& EDGE_ABNORMAL
)
896 have_opportunity
= false;
900 if (!have_opportunity
)
902 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
903 if (PENDING_STMT (e
))
904 bsi_commit_one_edge_insert (e
, NULL
);
908 /* Find out how many edges there are with interesting pending stmts on them.
909 Commit the stmts on edges we are not interested in. */
910 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
912 if (PENDING_STMT (e
))
914 gcc_assert (!(e
->flags
& EDGE_ABNORMAL
));
915 if (e
->flags
& EDGE_FALLTHRU
)
917 bsi
= bsi_start (e
->src
);
918 if (!bsi_end_p (bsi
))
920 stmt
= bsi_stmt (bsi
);
922 gcc_assert (stmt
!= NULL_TREE
);
923 is_label
= (TREE_CODE (stmt
) == LABEL_EXPR
);
924 /* Punt if it has non-label stmts, or isn't local. */
925 if (!is_label
|| DECL_NONLOCAL (TREE_OPERAND (stmt
, 0))
928 bsi_commit_one_edge_insert (e
, NULL
);
938 /* If there aren't at least 2 edges, no sharing will happen. */
942 bsi_commit_one_edge_insert (single_edge
, NULL
);
946 /* Ensure that we have empty worklists. */
947 #ifdef ENABLE_CHECKING
948 gcc_assert (VEC_length (edge
, edge_leader
) == 0);
949 gcc_assert (VEC_length (tree
, stmt_list
) == 0);
950 gcc_assert (bitmap_empty_p (leader_has_match
));
953 /* Find the "leader" block for each set of unique stmt lists. Preference is
954 given to FALLTHRU blocks since they would need a GOTO to arrive at another
955 block. The leader edge destination is the block which all the other edges
956 with the same stmt list will be redirected to. */
957 have_opportunity
= false;
958 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
960 if (PENDING_STMT (e
))
964 /* Look for the same stmt list in edge leaders list. */
965 for (x
= 0; VEC_iterate (edge
, edge_leader
, x
, leader
); x
++)
967 if (identical_stmt_lists_p (leader
, e
))
969 /* Give this edge the same stmt list pointer. */
970 PENDING_STMT (e
) = NULL
;
972 bitmap_set_bit (leader_has_match
, x
);
973 have_opportunity
= found
= true;
978 /* If no similar stmt list, add this edge to the leader list. */
981 VEC_safe_push (edge
, heap
, edge_leader
, e
);
982 VEC_safe_push (tree
, heap
, stmt_list
, PENDING_STMT (e
));
987 /* If there are no similar lists, just issue the stmts. */
988 if (!have_opportunity
)
990 for (x
= 0; VEC_iterate (edge
, edge_leader
, x
, leader
); x
++)
991 bsi_commit_one_edge_insert (leader
, NULL
);
992 VEC_truncate (edge
, edge_leader
, 0);
993 VEC_truncate (tree
, stmt_list
, 0);
994 bitmap_clear (leader_has_match
);
999 fprintf (dump_file
, "\nOpportunities in BB %d for stmt/block reduction:\n",
1002 /* For each common list, create a forwarding block and issue the stmt's
1004 for (x
= 0; VEC_iterate (edge
, edge_leader
, x
, leader
); x
++)
1005 if (bitmap_bit_p (leader_has_match
, x
))
1008 block_stmt_iterator bsi
;
1009 tree curr_stmt_list
;
1011 leader_match
= leader
;
1013 /* The tree_* cfg manipulation routines use the PENDING_EDGE field
1014 for various PHI manipulations, so it gets cleared when calls are
1015 made to make_forwarder_block(). So make sure the edge is clear,
1016 and use the saved stmt list. */
1017 PENDING_STMT (leader
) = NULL
;
1018 leader
->aux
= leader
;
1019 curr_stmt_list
= VEC_index (tree
, stmt_list
, x
);
1021 new_edge
= make_forwarder_block (leader
->dest
, same_stmt_list_p
,
1023 bb
= new_edge
->dest
;
1026 fprintf (dump_file
, "Splitting BB %d for Common stmt list. ",
1027 leader
->dest
->index
);
1028 fprintf (dump_file
, "Original block is now BB%d.\n", bb
->index
);
1029 print_generic_stmt (dump_file
, curr_stmt_list
, TDF_VOPS
);
1032 FOR_EACH_EDGE (e
, ei
, new_edge
->src
->preds
)
1036 fprintf (dump_file
, " Edge (%d->%d) lands here.\n",
1037 e
->src
->index
, e
->dest
->index
);
1040 bsi
= bsi_last (leader
->dest
);
1041 bsi_insert_after (&bsi
, curr_stmt_list
, BSI_NEW_STMT
);
1043 leader_match
= NULL
;
1044 /* We should never get a new block now. */
1048 PENDING_STMT (leader
) = VEC_index (tree
, stmt_list
, x
);
1049 bsi_commit_one_edge_insert (leader
, NULL
);
1053 /* Clear the working data structures. */
1054 VEC_truncate (edge
, edge_leader
, 0);
1055 VEC_truncate (tree
, stmt_list
, 0);
1056 bitmap_clear (leader_has_match
);
1060 /* This function will analyze the insertions which were performed on edges,
1061 and decide whether they should be left on that edge, or whether it is more
1062 efficient to emit some subset of them in a single block. All stmts are
1063 inserted somewhere. */
1066 perform_edge_inserts (void)
1071 fprintf(dump_file
, "Analyzing Edge Insertions.\n");
1073 /* analyze_edges_for_bb calls make_forwarder_block, which tries to
1074 incrementally update the dominator information. Since we don't
1075 need dominator information after this pass, go ahead and free the
1076 dominator information. */
1077 free_dominance_info (CDI_DOMINATORS
);
1078 free_dominance_info (CDI_POST_DOMINATORS
);
1080 /* Allocate data structures used in analyze_edges_for_bb. */
1081 init_analyze_edges_for_bb ();
1084 analyze_edges_for_bb (bb
);
1086 analyze_edges_for_bb (EXIT_BLOCK_PTR
);
1088 /* Free data structures used in analyze_edges_for_bb. */
1089 fini_analyze_edges_for_bb ();
1091 #ifdef ENABLE_CHECKING
1097 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1099 if (PENDING_STMT (e
))
1100 error (" Pending stmts not issued on PRED edge (%d, %d)\n",
1101 e
->src
->index
, e
->dest
->index
);
1103 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1105 if (PENDING_STMT (e
))
1106 error (" Pending stmts not issued on SUCC edge (%d, %d)\n",
1107 e
->src
->index
, e
->dest
->index
);
1110 FOR_EACH_EDGE (e
, ei
, ENTRY_BLOCK_PTR
->succs
)
1112 if (PENDING_STMT (e
))
1113 error (" Pending stmts not issued on ENTRY edge (%d, %d)\n",
1114 e
->src
->index
, e
->dest
->index
);
1116 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR
->preds
)
1118 if (PENDING_STMT (e
))
1119 error (" Pending stmts not issued on EXIT edge (%d, %d)\n",
1120 e
->src
->index
, e
->dest
->index
);
1127 /* Remove the ssa-names in the current function and translate them into normal
1128 compiler variables. PERFORM_TER is true if Temporary Expression Replacement
1129 should also be used. */
1132 remove_ssa_form (bool perform_ter
)
1136 tree
*values
= NULL
;
1139 map
= coalesce_ssa_name ();
1141 /* Return to viewing the variable list as just all reference variables after
1142 coalescing has been performed. */
1143 partition_view_normal (map
, false);
1145 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1147 fprintf (dump_file
, "After Coalescing:\n");
1148 dump_var_map (dump_file
, map
);
1153 values
= find_replaceable_exprs (map
);
1154 if (values
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
1155 dump_replaceable_exprs (dump_file
, values
);
1158 /* Assign real variables to the partitions now. */
1161 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1163 fprintf (dump_file
, "After Base variable replacement:\n");
1164 dump_var_map (dump_file
, map
);
1167 rewrite_trees (map
, values
);
1172 /* Remove PHI nodes which have been translated back to real variables. */
1175 for (phi
= phi_nodes (bb
); phi
; phi
= next
)
1177 next
= PHI_CHAIN (phi
);
1178 remove_phi_node (phi
, NULL_TREE
, true);
1182 /* If any copies were inserted on edges, analyze and insert them now. */
1183 perform_edge_inserts ();
1185 delete_var_map (map
);
1189 /* Search every PHI node for arguments associated with backedges which
1190 we can trivially determine will need a copy (the argument is either
1191 not an SSA_NAME or the argument has a different underlying variable
1192 than the PHI result).
1194 Insert a copy from the PHI argument to a new destination at the
1195 end of the block with the backedge to the top of the loop. Update
1196 the PHI argument to reference this new destination. */
1199 insert_backedge_copies (void)
1207 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
1209 tree result
= PHI_RESULT (phi
);
1213 if (!is_gimple_reg (result
))
1216 result_var
= SSA_NAME_VAR (result
);
1217 for (i
= 0; i
< PHI_NUM_ARGS (phi
); i
++)
1219 tree arg
= PHI_ARG_DEF (phi
, i
);
1220 edge e
= PHI_ARG_EDGE (phi
, i
);
1222 /* If the argument is not an SSA_NAME, then we will need a
1223 constant initialization. If the argument is an SSA_NAME with
1224 a different underlying variable then a copy statement will be
1226 if ((e
->flags
& EDGE_DFS_BACK
)
1227 && (TREE_CODE (arg
) != SSA_NAME
1228 || SSA_NAME_VAR (arg
) != result_var
))
1230 tree stmt
, name
, last
= NULL
;
1231 block_stmt_iterator bsi
;
1233 bsi
= bsi_last (PHI_ARG_EDGE (phi
, i
)->src
);
1234 if (!bsi_end_p (bsi
))
1235 last
= bsi_stmt (bsi
);
1237 /* In theory the only way we ought to get back to the
1238 start of a loop should be with a COND_EXPR or GOTO_EXPR.
1239 However, better safe than sorry.
1240 If the block ends with a control statement or
1241 something that might throw, then we have to
1242 insert this assignment before the last
1243 statement. Else insert it after the last statement. */
1244 if (last
&& stmt_ends_bb_p (last
))
1246 /* If the last statement in the block is the definition
1247 site of the PHI argument, then we can't insert
1248 anything after it. */
1249 if (TREE_CODE (arg
) == SSA_NAME
1250 && SSA_NAME_DEF_STMT (arg
) == last
)
1254 /* Create a new instance of the underlying variable of the
1256 stmt
= build_gimple_modify_stmt (NULL_TREE
,
1257 PHI_ARG_DEF (phi
, i
));
1258 name
= make_ssa_name (result_var
, stmt
);
1259 GIMPLE_STMT_OPERAND (stmt
, 0) = name
;
1261 /* Insert the new statement into the block and update
1263 if (last
&& stmt_ends_bb_p (last
))
1264 bsi_insert_before (&bsi
, stmt
, BSI_NEW_STMT
);
1266 bsi_insert_after (&bsi
, stmt
, BSI_NEW_STMT
);
1267 SET_PHI_ARG_DEF (phi
, i
, name
);
1274 /* Take the current function out of SSA form, translating PHIs as described in
1275 R. Morgan, ``Building an Optimizing Compiler'',
1276 Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
1279 rewrite_out_of_ssa (void)
1281 /* If elimination of a PHI requires inserting a copy on a backedge,
1282 then we will have to split the backedge which has numerous
1283 undesirable performance effects.
1285 A significant number of such cases can be handled here by inserting
1286 copies into the loop itself. */
1287 insert_backedge_copies ();
1289 eliminate_virtual_phis ();
1291 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1292 dump_tree_cfg (dump_file
, dump_flags
& ~TDF_DETAILS
);
1294 remove_ssa_form (flag_tree_ter
&& !flag_mudflap
);
1296 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1297 dump_tree_cfg (dump_file
, dump_flags
& ~TDF_DETAILS
);
1299 cfun
->gimple_df
->in_ssa_p
= false;
1304 /* Define the parameters of the out of SSA pass. */
1306 struct tree_opt_pass pass_del_ssa
=
1308 "optimized", /* name */
1310 rewrite_out_of_ssa
, /* execute */
1313 0, /* static_pass_number */
1314 TV_TREE_SSA_TO_NORMAL
, /* tv_id */
1315 PROP_cfg
| PROP_ssa
| PROP_alias
, /* properties_required */
1316 0, /* properties_provided */
1317 /* ??? If TER is enabled, we also kill gimple. */
1318 PROP_ssa
, /* properties_destroyed */
1319 TODO_verify_ssa
| TODO_verify_flow
1320 | TODO_verify_stmts
, /* todo_flags_start */
1323 | TODO_remove_unused_locals
, /* todo_flags_finish */