1 /* Convert a program in SSA form into Normal form.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3 Contributed by Andrew Macleod <amacleod@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "basic-block.h"
28 #include "gimple-pretty-print.h"
32 #include "gimple-ssa.h"
34 #include "tree-phinodes.h"
35 #include "ssa-iterators.h"
36 #include "tree-ssanames.h"
38 #include "diagnostic-core.h"
39 #include "tree-outof-ssa.h"
41 /* FIXME: A lot of code here deals with expanding to RTL. All that code
42 should be in cfgexpand.c. */
45 /* Return TRUE if expression STMT is suitable for replacement. */
48 ssa_is_replaceable_p (gimple stmt
)
54 /* Only consider modify stmts. */
55 if (!is_gimple_assign (stmt
))
58 /* If the statement may throw an exception, it cannot be replaced. */
59 if (stmt_could_throw_p (stmt
))
62 /* Punt if there is more than 1 def. */
63 def
= SINGLE_SSA_TREE_OPERAND (stmt
, SSA_OP_DEF
);
67 /* Only consider definitions which have a single use. */
68 if (!single_imm_use (def
, &use_p
, &use_stmt
))
71 /* Used in this block, but at the TOP of the block, not the end. */
72 if (gimple_code (use_stmt
) == GIMPLE_PHI
)
75 /* There must be no VDEFs. */
76 if (gimple_vdef (stmt
))
79 /* Float expressions must go through memory if float-store is on. */
81 && FLOAT_TYPE_P (gimple_expr_type (stmt
)))
84 /* An assignment with a register variable on the RHS is not
86 if (gimple_assign_rhs_code (stmt
) == VAR_DECL
87 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt
)))
90 /* No function calls can be replaced. */
91 if (is_gimple_call (stmt
))
94 /* Leave any stmt with volatile operands alone as well. */
95 if (gimple_has_volatile_ops (stmt
))
102 /* Used to hold all the components required to do SSA PHI elimination.
103 The node and pred/succ list is a simple linear list of nodes and
104 edges represented as pairs of nodes.
106 The predecessor and successor list: Nodes are entered in pairs, where
107 [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
108 predecessors, all the odd elements are successors.
111 When implemented as bitmaps, very large programs SSA->Normal times were
112 being dominated by clearing the interference graph.
114 Typically this list of edges is extremely small since it only includes
115 PHI results and uses from a single edge which have not coalesced with
116 each other. This means that no virtual PHI nodes are included, and
117 empirical evidence suggests that the number of edges rarely exceed
118 3, and in a bootstrap of GCC, the maximum size encountered was 7.
119 This also limits the number of possible nodes that are involved to
120 rarely more than 6, and in the bootstrap of gcc, the maximum number
121 of nodes encountered was 12. */
123 typedef struct _elim_graph
{
124 /* Size of the elimination vectors. */
127 /* List of nodes in the elimination graph. */
130 /* The predecessor and successor edge list. */
133 /* Source locus on each edge */
134 vec
<source_location
> edge_locus
;
136 /* Visited vector. */
139 /* Stack for visited nodes. */
142 /* The variable partition map. */
145 /* Edge being eliminated by this graph. */
148 /* List of constant copies to emit. These are pushed on in pairs. */
149 vec
<int> const_dests
;
150 vec
<tree
> const_copies
;
152 /* Source locations for any constant copies. */
153 vec
<source_location
> copy_locus
;
157 /* For an edge E find out a good source location to associate with
158 instructions inserted on edge E. If E has an implicit goto set,
159 use its location. Otherwise search instructions in predecessors
160 of E for a location, and use that one. That makes sense because
161 we insert on edges for PHI nodes, and effects of PHIs happen on
162 the end of the predecessor conceptually. */
165 set_location_for_edge (edge e
)
169 set_curr_insn_location (e
->goto_locus
);
173 basic_block bb
= e
->src
;
174 gimple_stmt_iterator gsi
;
178 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
180 gimple stmt
= gsi_stmt (gsi
);
181 if (is_gimple_debug (stmt
))
183 if (gimple_has_location (stmt
) || gimple_block (stmt
))
185 set_curr_insn_location (gimple_location (stmt
));
189 /* Nothing found in this basic block. Make a half-assed attempt
190 to continue with another block. */
191 if (single_pred_p (bb
))
192 bb
= single_pred (bb
);
196 while (bb
!= e
->src
);
200 /* Emit insns to copy SRC into DEST converting SRC if necessary. As
201 SRC/DEST might be BLKmode memory locations SIZEEXP is a tree from
202 which we deduce the size to copy in that case. */
205 emit_partition_copy (rtx dest
, rtx src
, int unsignedsrcp
, tree sizeexp
)
211 if (GET_MODE (src
) != VOIDmode
&& GET_MODE (src
) != GET_MODE (dest
))
212 src
= convert_to_mode (GET_MODE (dest
), src
, unsignedsrcp
);
213 if (GET_MODE (src
) == BLKmode
)
215 gcc_assert (GET_MODE (dest
) == BLKmode
);
216 emit_block_move (dest
, src
, expr_size (sizeexp
), BLOCK_OP_NORMAL
);
219 emit_move_insn (dest
, src
);
227 /* Insert a copy instruction from partition SRC to DEST onto edge E. */
230 insert_partition_copy_on_edge (edge e
, int dest
, int src
, source_location locus
)
234 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
237 "Inserting a partition copy on edge BB%d->BB%d :"
240 e
->dest
->index
, dest
, src
);
241 fprintf (dump_file
, "\n");
244 gcc_assert (SA
.partition_to_pseudo
[dest
]);
245 gcc_assert (SA
.partition_to_pseudo
[src
]);
247 set_location_for_edge (e
);
248 /* If a locus is provided, override the default. */
250 set_curr_insn_location (locus
);
252 var
= partition_to_var (SA
.map
, src
);
253 seq
= emit_partition_copy (SA
.partition_to_pseudo
[dest
],
254 SA
.partition_to_pseudo
[src
],
255 TYPE_UNSIGNED (TREE_TYPE (var
)),
258 insert_insn_on_edge (seq
, e
);
261 /* Insert a copy instruction from expression SRC to partition DEST
265 insert_value_copy_on_edge (edge e
, int dest
, tree src
, source_location locus
)
268 enum machine_mode dest_mode
, src_mode
;
272 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
275 "Inserting a value copy on edge BB%d->BB%d : PART.%d = ",
277 e
->dest
->index
, dest
);
278 print_generic_expr (dump_file
, src
, TDF_SLIM
);
279 fprintf (dump_file
, "\n");
282 gcc_assert (SA
.partition_to_pseudo
[dest
]);
284 set_location_for_edge (e
);
285 /* If a locus is provided, override the default. */
287 set_curr_insn_location (locus
);
291 var
= SSA_NAME_VAR (partition_to_var (SA
.map
, dest
));
292 src_mode
= TYPE_MODE (TREE_TYPE (src
));
293 dest_mode
= GET_MODE (SA
.partition_to_pseudo
[dest
]);
294 gcc_assert (src_mode
== TYPE_MODE (TREE_TYPE (var
)));
295 gcc_assert (!REG_P (SA
.partition_to_pseudo
[dest
])
296 || dest_mode
== promote_decl_mode (var
, &unsignedp
));
298 if (src_mode
!= dest_mode
)
300 x
= expand_expr (src
, NULL
, src_mode
, EXPAND_NORMAL
);
301 x
= convert_modes (dest_mode
, src_mode
, x
, unsignedp
);
303 else if (src_mode
== BLKmode
)
305 x
= SA
.partition_to_pseudo
[dest
];
306 store_expr (src
, x
, 0, false);
309 x
= expand_expr (src
, SA
.partition_to_pseudo
[dest
],
310 dest_mode
, EXPAND_NORMAL
);
312 if (x
!= SA
.partition_to_pseudo
[dest
])
313 emit_move_insn (SA
.partition_to_pseudo
[dest
], x
);
317 insert_insn_on_edge (seq
, e
);
320 /* Insert a copy instruction from RTL expression SRC to partition DEST
324 insert_rtx_to_part_on_edge (edge e
, int dest
, rtx src
, int unsignedsrcp
,
325 source_location locus
)
328 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
331 "Inserting a temp copy on edge BB%d->BB%d : PART.%d = ",
333 e
->dest
->index
, dest
);
334 print_simple_rtl (dump_file
, src
);
335 fprintf (dump_file
, "\n");
338 gcc_assert (SA
.partition_to_pseudo
[dest
]);
340 set_location_for_edge (e
);
341 /* If a locus is provided, override the default. */
343 set_curr_insn_location (locus
);
345 /* We give the destination as sizeexp in case src/dest are BLKmode
346 mems. Usually we give the source. As we result from SSA names
347 the left and right size should be the same (and no WITH_SIZE_EXPR
348 involved), so it doesn't matter. */
349 seq
= emit_partition_copy (SA
.partition_to_pseudo
[dest
],
351 partition_to_var (SA
.map
, dest
));
353 insert_insn_on_edge (seq
, e
);
356 /* Insert a copy instruction from partition SRC to RTL lvalue DEST
360 insert_part_to_rtx_on_edge (edge e
, rtx dest
, int src
, source_location locus
)
364 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
367 "Inserting a temp copy on edge BB%d->BB%d : ",
370 print_simple_rtl (dump_file
, dest
);
371 fprintf (dump_file
, "= PART.%d\n", src
);
374 gcc_assert (SA
.partition_to_pseudo
[src
]);
376 set_location_for_edge (e
);
377 /* If a locus is provided, override the default. */
379 set_curr_insn_location (locus
);
381 var
= partition_to_var (SA
.map
, src
);
382 seq
= emit_partition_copy (dest
,
383 SA
.partition_to_pseudo
[src
],
384 TYPE_UNSIGNED (TREE_TYPE (var
)),
387 insert_insn_on_edge (seq
, e
);
391 /* Create an elimination graph with SIZE nodes and associated data
395 new_elim_graph (int size
)
397 elim_graph g
= (elim_graph
) xmalloc (sizeof (struct _elim_graph
));
399 g
->nodes
.create (30);
400 g
->const_dests
.create (20);
401 g
->const_copies
.create (20);
402 g
->copy_locus
.create (10);
403 g
->edge_list
.create (20);
404 g
->edge_locus
.create (10);
405 g
->stack
.create (30);
407 g
->visited
= sbitmap_alloc (size
);
413 /* Empty elimination graph G. */
416 clear_elim_graph (elim_graph g
)
418 g
->nodes
.truncate (0);
419 g
->edge_list
.truncate (0);
420 g
->edge_locus
.truncate (0);
424 /* Delete elimination graph G. */
427 delete_elim_graph (elim_graph g
)
429 sbitmap_free (g
->visited
);
431 g
->edge_list
.release ();
432 g
->const_copies
.release ();
433 g
->const_dests
.release ();
435 g
->copy_locus
.release ();
436 g
->edge_locus
.release ();
442 /* Return the number of nodes in graph G. */
445 elim_graph_size (elim_graph g
)
447 return g
->nodes
.length ();
451 /* Add NODE to graph G, if it doesn't exist already. */
454 elim_graph_add_node (elim_graph g
, int node
)
459 FOR_EACH_VEC_ELT (g
->nodes
, x
, t
)
462 g
->nodes
.safe_push (node
);
466 /* Add the edge PRED->SUCC to graph G. */
469 elim_graph_add_edge (elim_graph g
, int pred
, int succ
, source_location locus
)
471 g
->edge_list
.safe_push (pred
);
472 g
->edge_list
.safe_push (succ
);
473 g
->edge_locus
.safe_push (locus
);
477 /* Remove an edge from graph G for which NODE is the predecessor, and
478 return the successor node. -1 is returned if there is no such edge. */
481 elim_graph_remove_succ_edge (elim_graph g
, int node
, source_location
*locus
)
485 for (x
= 0; x
< g
->edge_list
.length (); x
+= 2)
486 if (g
->edge_list
[x
] == node
)
488 g
->edge_list
[x
] = -1;
489 y
= g
->edge_list
[x
+ 1];
490 g
->edge_list
[x
+ 1] = -1;
491 *locus
= g
->edge_locus
[x
/ 2];
492 g
->edge_locus
[x
/ 2] = UNKNOWN_LOCATION
;
495 *locus
= UNKNOWN_LOCATION
;
500 /* Find all the nodes in GRAPH which are successors to NODE in the
501 edge list. VAR will hold the partition number found. CODE is the
502 code fragment executed for every node found. */
504 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, LOCUS, CODE) \
508 for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
510 y_ = (GRAPH)->edge_list[x_]; \
513 (void) ((VAR) = (GRAPH)->edge_list[x_ + 1]); \
514 (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
520 /* Find all the nodes which are predecessors of NODE in the edge list for
521 GRAPH. VAR will hold the partition number found. CODE is the
522 code fragment executed for every node found. */
524 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, LOCUS, CODE) \
528 for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
530 y_ = (GRAPH)->edge_list[x_ + 1]; \
533 (void) ((VAR) = (GRAPH)->edge_list[x_]); \
534 (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
540 /* Add T to elimination graph G. */
543 eliminate_name (elim_graph g
, int T
)
545 elim_graph_add_node (g
, T
);
549 /* Build elimination graph G for basic block BB on incoming PHI edge
553 eliminate_build (elim_graph g
)
557 gimple_stmt_iterator gsi
;
559 clear_elim_graph (g
);
561 for (gsi
= gsi_start_phis (g
->e
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
563 gimple phi
= gsi_stmt (gsi
);
564 source_location locus
;
566 p0
= var_to_partition (g
->map
, gimple_phi_result (phi
));
567 /* Ignore results which are not in partitions. */
568 if (p0
== NO_PARTITION
)
571 Ti
= PHI_ARG_DEF (phi
, g
->e
->dest_idx
);
572 locus
= gimple_phi_arg_location_from_edge (phi
, g
->e
);
574 /* If this argument is a constant, or a SSA_NAME which is being
575 left in SSA form, just queue a copy to be emitted on this
577 if (!phi_ssa_name_p (Ti
)
578 || (TREE_CODE (Ti
) == SSA_NAME
579 && var_to_partition (g
->map
, Ti
) == NO_PARTITION
))
581 /* Save constant copies until all other copies have been emitted
583 g
->const_dests
.safe_push (p0
);
584 g
->const_copies
.safe_push (Ti
);
585 g
->copy_locus
.safe_push (locus
);
589 pi
= var_to_partition (g
->map
, Ti
);
592 eliminate_name (g
, p0
);
593 eliminate_name (g
, pi
);
594 elim_graph_add_edge (g
, p0
, pi
, locus
);
601 /* Push successors of T onto the elimination stack for G. */
604 elim_forward (elim_graph g
, int T
)
607 source_location locus
;
609 bitmap_set_bit (g
->visited
, T
);
610 FOR_EACH_ELIM_GRAPH_SUCC (g
, T
, S
, locus
,
612 if (!bitmap_bit_p (g
->visited
, S
))
615 g
->stack
.safe_push (T
);
619 /* Return 1 if there unvisited predecessors of T in graph G. */
622 elim_unvisited_predecessor (elim_graph g
, int T
)
625 source_location locus
;
627 FOR_EACH_ELIM_GRAPH_PRED (g
, T
, P
, locus
,
629 if (!bitmap_bit_p (g
->visited
, P
))
635 /* Process predecessors first, and insert a copy. */
638 elim_backward (elim_graph g
, int T
)
641 source_location locus
;
643 bitmap_set_bit (g
->visited
, T
);
644 FOR_EACH_ELIM_GRAPH_PRED (g
, T
, P
, locus
,
646 if (!bitmap_bit_p (g
->visited
, P
))
648 elim_backward (g
, P
);
649 insert_partition_copy_on_edge (g
->e
, P
, T
, locus
);
654 /* Allocate a new pseudo register usable for storing values sitting
655 in NAME (a decl or SSA name), i.e. with matching mode and attributes. */
658 get_temp_reg (tree name
)
660 tree var
= TREE_CODE (name
) == SSA_NAME
? SSA_NAME_VAR (name
) : name
;
661 tree type
= TREE_TYPE (var
);
663 enum machine_mode reg_mode
= promote_decl_mode (var
, &unsignedp
);
664 rtx x
= gen_reg_rtx (reg_mode
);
665 if (POINTER_TYPE_P (type
))
666 mark_reg_pointer (x
, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (var
))));
670 /* Insert required copies for T in graph G. Check for a strongly connected
671 region, and create a temporary to break the cycle if one is found. */
674 elim_create (elim_graph g
, int T
)
677 source_location locus
;
679 if (elim_unvisited_predecessor (g
, T
))
681 tree var
= partition_to_var (g
->map
, T
);
682 rtx U
= get_temp_reg (var
);
683 int unsignedsrcp
= TYPE_UNSIGNED (TREE_TYPE (var
));
685 insert_part_to_rtx_on_edge (g
->e
, U
, T
, UNKNOWN_LOCATION
);
686 FOR_EACH_ELIM_GRAPH_PRED (g
, T
, P
, locus
,
688 if (!bitmap_bit_p (g
->visited
, P
))
690 elim_backward (g
, P
);
691 insert_rtx_to_part_on_edge (g
->e
, P
, U
, unsignedsrcp
, locus
);
697 S
= elim_graph_remove_succ_edge (g
, T
, &locus
);
700 bitmap_set_bit (g
->visited
, T
);
701 insert_partition_copy_on_edge (g
->e
, T
, S
, locus
);
707 /* Eliminate all the phi nodes on edge E in graph G. */
710 eliminate_phi (edge e
, elim_graph g
)
714 gcc_assert (g
->const_copies
.length () == 0);
715 gcc_assert (g
->copy_locus
.length () == 0);
717 /* Abnormal edges already have everything coalesced. */
718 if (e
->flags
& EDGE_ABNORMAL
)
725 if (elim_graph_size (g
) != 0)
729 bitmap_clear (g
->visited
);
730 g
->stack
.truncate (0);
732 FOR_EACH_VEC_ELT (g
->nodes
, x
, part
)
734 if (!bitmap_bit_p (g
->visited
, part
))
735 elim_forward (g
, part
);
738 bitmap_clear (g
->visited
);
739 while (g
->stack
.length () > 0)
742 if (!bitmap_bit_p (g
->visited
, x
))
747 /* If there are any pending constant copies, issue them now. */
748 while (g
->const_copies
.length () > 0)
752 source_location locus
;
754 src
= g
->const_copies
.pop ();
755 dest
= g
->const_dests
.pop ();
756 locus
= g
->copy_locus
.pop ();
757 insert_value_copy_on_edge (e
, dest
, src
, locus
);
762 /* Remove each argument from PHI. If an arg was the last use of an SSA_NAME,
763 check to see if this allows another PHI node to be removed. */
766 remove_gimple_phi_args (gimple phi
)
771 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
773 fprintf (dump_file
, "Removing Dead PHI definition: ");
774 print_gimple_stmt (dump_file
, phi
, 0, TDF_SLIM
);
777 FOR_EACH_PHI_ARG (arg_p
, phi
, iter
, SSA_OP_USE
)
779 tree arg
= USE_FROM_PTR (arg_p
);
780 if (TREE_CODE (arg
) == SSA_NAME
)
782 /* Remove the reference to the existing argument. */
783 SET_USE (arg_p
, NULL_TREE
);
784 if (has_zero_uses (arg
))
787 gimple_stmt_iterator gsi
;
789 stmt
= SSA_NAME_DEF_STMT (arg
);
791 /* Also remove the def if it is a PHI node. */
792 if (gimple_code (stmt
) == GIMPLE_PHI
)
794 remove_gimple_phi_args (stmt
);
795 gsi
= gsi_for_stmt (stmt
);
796 remove_phi_node (&gsi
, true);
804 /* Remove any PHI node which is a virtual PHI, or a PHI with no uses. */
807 eliminate_useless_phis (void)
810 gimple_stmt_iterator gsi
;
815 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); )
817 gimple phi
= gsi_stmt (gsi
);
818 result
= gimple_phi_result (phi
);
819 if (virtual_operand_p (result
))
821 #ifdef ENABLE_CHECKING
823 /* There should be no arguments which are not virtual, or the
824 results will be incorrect. */
825 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
827 tree arg
= PHI_ARG_DEF (phi
, i
);
828 if (TREE_CODE (arg
) == SSA_NAME
829 && !virtual_operand_p (arg
))
831 fprintf (stderr
, "Argument of PHI is not virtual (");
832 print_generic_expr (stderr
, arg
, TDF_SLIM
);
833 fprintf (stderr
, "), but the result is :");
834 print_gimple_stmt (stderr
, phi
, 0, TDF_SLIM
);
835 internal_error ("SSA corruption");
839 remove_phi_node (&gsi
, true);
843 /* Also remove real PHIs with no uses. */
844 if (has_zero_uses (result
))
846 remove_gimple_phi_args (phi
);
847 remove_phi_node (&gsi
, true);
857 /* This function will rewrite the current program using the variable mapping
858 found in MAP. If the replacement vector VALUES is provided, any
859 occurrences of partitions with non-null entries in the vector will be
860 replaced with the expression in the vector instead of its mapped
864 rewrite_trees (var_map map ATTRIBUTE_UNUSED
)
866 #ifdef ENABLE_CHECKING
868 /* Search for PHIs where the destination has no partition, but one
869 or more arguments has a partition. This should not happen and can
870 create incorrect code. */
873 gimple_stmt_iterator gsi
;
874 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
876 gimple phi
= gsi_stmt (gsi
);
877 tree T0
= var_to_partition_to_var (map
, gimple_phi_result (phi
));
881 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
883 tree arg
= PHI_ARG_DEF (phi
, i
);
885 if (TREE_CODE (arg
) == SSA_NAME
886 && var_to_partition (map
, arg
) != NO_PARTITION
)
888 fprintf (stderr
, "Argument of PHI is in a partition :(");
889 print_generic_expr (stderr
, arg
, TDF_SLIM
);
890 fprintf (stderr
, "), but the result is not :");
891 print_gimple_stmt (stderr
, phi
, 0, TDF_SLIM
);
892 internal_error ("SSA corruption");
901 /* Given the out-of-ssa info object SA (with prepared partitions)
902 eliminate all phi nodes in all basic blocks. Afterwards no
903 basic block will have phi nodes anymore and there are possibly
904 some RTL instructions inserted on edges. */
907 expand_phi_nodes (struct ssaexpand
*sa
)
910 elim_graph g
= new_elim_graph (sa
->map
->num_partitions
);
913 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR
->next_bb
, EXIT_BLOCK_PTR
, next_bb
)
914 if (!gimple_seq_empty_p (phi_nodes (bb
)))
918 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
919 eliminate_phi (e
, g
);
920 set_phi_nodes (bb
, NULL
);
921 /* We can't redirect EH edges in RTL land, so we need to do this
922 here. Redirection happens only when splitting is necessary,
923 which it is only for critical edges, normally. For EH edges
924 it might also be necessary when the successor has more than
925 one predecessor. In that case the edge is either required to
926 be fallthru (which EH edges aren't), or the predecessor needs
927 to end with a jump (which again, isn't the case with EH edges).
928 Hence, split all EH edges on which we inserted instructions
929 and whose successor has multiple predecessors. */
930 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
932 if (e
->insns
.r
&& (e
->flags
& EDGE_EH
)
933 && !single_pred_p (e
->dest
))
935 rtx insns
= e
->insns
.r
;
937 e
->insns
.r
= NULL_RTX
;
939 single_pred_edge (bb
)->insns
.r
= insns
;
946 delete_elim_graph (g
);
950 /* Remove the ssa-names in the current function and translate them into normal
951 compiler variables. PERFORM_TER is true if Temporary Expression Replacement
952 should also be used. */
955 remove_ssa_form (bool perform_ter
, struct ssaexpand
*sa
)
957 bitmap values
= NULL
;
961 map
= coalesce_ssa_name ();
963 /* Return to viewing the variable list as just all reference variables after
964 coalescing has been performed. */
965 partition_view_normal (map
, false);
967 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
969 fprintf (dump_file
, "After Coalescing:\n");
970 dump_var_map (dump_file
, map
);
975 values
= find_replaceable_exprs (map
);
976 if (values
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
977 dump_replaceable_exprs (dump_file
, values
);
984 sa
->partition_has_default_def
= BITMAP_ALLOC (NULL
);
985 for (i
= 1; i
< num_ssa_names
; i
++)
987 tree t
= ssa_name (i
);
988 if (t
&& SSA_NAME_IS_DEFAULT_DEF (t
))
990 int p
= var_to_partition (map
, t
);
991 if (p
!= NO_PARTITION
)
992 bitmap_set_bit (sa
->partition_has_default_def
, p
);
998 /* If not already done so for basic block BB, assign increasing uids
999 to each of its instructions. */
1002 maybe_renumber_stmts_bb (basic_block bb
)
1005 gimple_stmt_iterator gsi
;
1010 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1012 gimple stmt
= gsi_stmt (gsi
);
1013 gimple_set_uid (stmt
, i
);
1019 /* Return true if we can determine that the SSA_NAMEs RESULT (a result
1020 of a PHI node) and ARG (one of its arguments) conflict. Return false
1021 otherwise, also when we simply aren't sure. */
1024 trivially_conflicts_p (basic_block bb
, tree result
, tree arg
)
1027 imm_use_iterator imm_iter
;
1028 gimple defa
= SSA_NAME_DEF_STMT (arg
);
1030 /* If ARG isn't defined in the same block it's too complicated for
1032 if (gimple_bb (defa
) != bb
)
1035 FOR_EACH_IMM_USE_FAST (use
, imm_iter
, result
)
1037 gimple use_stmt
= USE_STMT (use
);
1038 if (is_gimple_debug (use_stmt
))
1040 /* Now, if there's a use of RESULT that lies outside this basic block,
1041 then there surely is a conflict with ARG. */
1042 if (gimple_bb (use_stmt
) != bb
)
1044 if (gimple_code (use_stmt
) == GIMPLE_PHI
)
1046 /* The use now is in a real stmt of BB, so if ARG was defined
1047 in a PHI node (like RESULT) both conflict. */
1048 if (gimple_code (defa
) == GIMPLE_PHI
)
1050 maybe_renumber_stmts_bb (bb
);
1051 /* If the use of RESULT occurs after the definition of ARG,
1052 the two conflict too. */
1053 if (gimple_uid (defa
) < gimple_uid (use_stmt
))
1061 /* Search every PHI node for arguments associated with backedges which
1062 we can trivially determine will need a copy (the argument is either
1063 not an SSA_NAME or the argument has a different underlying variable
1064 than the PHI result).
1066 Insert a copy from the PHI argument to a new destination at the
1067 end of the block with the backedge to the top of the loop. Update
1068 the PHI argument to reference this new destination. */
1071 insert_backedge_copies (void)
1074 gimple_stmt_iterator gsi
;
1076 mark_dfs_back_edges ();
1080 /* Mark block as possibly needing calculation of UIDs. */
1083 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1085 gimple phi
= gsi_stmt (gsi
);
1086 tree result
= gimple_phi_result (phi
);
1089 if (virtual_operand_p (result
))
1092 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
1094 tree arg
= gimple_phi_arg_def (phi
, i
);
1095 edge e
= gimple_phi_arg_edge (phi
, i
);
1097 /* If the argument is not an SSA_NAME, then we will need a
1098 constant initialization. If the argument is an SSA_NAME with
1099 a different underlying variable then a copy statement will be
1101 if ((e
->flags
& EDGE_DFS_BACK
)
1102 && (TREE_CODE (arg
) != SSA_NAME
1103 || SSA_NAME_VAR (arg
) != SSA_NAME_VAR (result
)
1104 || trivially_conflicts_p (bb
, result
, arg
)))
1107 gimple stmt
, last
= NULL
;
1108 gimple_stmt_iterator gsi2
;
1110 gsi2
= gsi_last_bb (gimple_phi_arg_edge (phi
, i
)->src
);
1111 if (!gsi_end_p (gsi2
))
1112 last
= gsi_stmt (gsi2
);
1114 /* In theory the only way we ought to get back to the
1115 start of a loop should be with a COND_EXPR or GOTO_EXPR.
1116 However, better safe than sorry.
1117 If the block ends with a control statement or
1118 something that might throw, then we have to
1119 insert this assignment before the last
1120 statement. Else insert it after the last statement. */
1121 if (last
&& stmt_ends_bb_p (last
))
1123 /* If the last statement in the block is the definition
1124 site of the PHI argument, then we can't insert
1125 anything after it. */
1126 if (TREE_CODE (arg
) == SSA_NAME
1127 && SSA_NAME_DEF_STMT (arg
) == last
)
1131 /* Create a new instance of the underlying variable of the
1133 name
= copy_ssa_name (result
, NULL
);
1134 stmt
= gimple_build_assign (name
,
1135 gimple_phi_arg_def (phi
, i
));
1137 /* copy location if present. */
1138 if (gimple_phi_arg_has_location (phi
, i
))
1139 gimple_set_location (stmt
,
1140 gimple_phi_arg_location (phi
, i
));
1142 /* Insert the new statement into the block and update
1144 if (last
&& stmt_ends_bb_p (last
))
1145 gsi_insert_before (&gsi2
, stmt
, GSI_NEW_STMT
);
1147 gsi_insert_after (&gsi2
, stmt
, GSI_NEW_STMT
);
1148 SET_PHI_ARG_DEF (phi
, i
, name
);
1153 /* Unmark this block again. */
1158 /* Free all memory associated with going out of SSA form. SA is
1159 the outof-SSA info object. */
1162 finish_out_of_ssa (struct ssaexpand
*sa
)
1164 free (sa
->partition_to_pseudo
);
1166 BITMAP_FREE (sa
->values
);
1167 delete_var_map (sa
->map
);
1168 BITMAP_FREE (sa
->partition_has_default_def
);
1169 memset (sa
, 0, sizeof *sa
);
1172 /* Take the current function out of SSA form, translating PHIs as described in
1173 R. Morgan, ``Building an Optimizing Compiler'',
1174 Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
1177 rewrite_out_of_ssa (struct ssaexpand
*sa
)
1179 /* If elimination of a PHI requires inserting a copy on a backedge,
1180 then we will have to split the backedge which has numerous
1181 undesirable performance effects.
1183 A significant number of such cases can be handled here by inserting
1184 copies into the loop itself. */
1185 insert_backedge_copies ();
1188 /* Eliminate PHIs which are of no use, such as virtual or dead phis. */
1189 eliminate_useless_phis ();
1191 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1192 gimple_dump_cfg (dump_file
, dump_flags
& ~TDF_DETAILS
);
1194 remove_ssa_form (flag_tree_ter
, sa
);
1196 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1197 gimple_dump_cfg (dump_file
, dump_flags
& ~TDF_DETAILS
);