1 /* Convert a program in SSA form into Normal form.
2 Copyright (C) 2004-2016 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"
31 #include "gimple-pretty-print.h"
32 #include "diagnostic-core.h"
33 #include "stor-layout.h"
37 #include "gimple-iterator.h"
40 #include "tree-ssa-live.h"
41 #include "tree-ssa-ter.h"
42 #include "tree-ssa-coalesce.h"
43 #include "tree-outof-ssa.h"
46 /* FIXME: A lot of code here deals with expanding to RTL. All that code
47 should be in cfgexpand.c. */
51 /* Return TRUE if expression STMT is suitable for replacement. */
54 ssa_is_replaceable_p (gimple
*stmt
)
60 /* Only consider modify stmts. */
61 if (!is_gimple_assign (stmt
))
64 /* If the statement may throw an exception, it cannot be replaced. */
65 if (stmt_could_throw_p (stmt
))
68 /* Punt if there is more than 1 def. */
69 def
= SINGLE_SSA_TREE_OPERAND (stmt
, SSA_OP_DEF
);
73 /* Only consider definitions which have a single use. */
74 if (!single_imm_use (def
, &use_p
, &use_stmt
))
77 /* Used in this block, but at the TOP of the block, not the end. */
78 if (gimple_code (use_stmt
) == GIMPLE_PHI
)
81 /* There must be no VDEFs. */
82 if (gimple_vdef (stmt
))
85 /* Float expressions must go through memory if float-store is on. */
87 && FLOAT_TYPE_P (gimple_expr_type (stmt
)))
90 /* An assignment with a register variable on the RHS is not
92 if (gimple_assign_rhs_code (stmt
) == VAR_DECL
93 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt
)))
96 /* No function calls can be replaced. */
97 if (is_gimple_call (stmt
))
100 /* Leave any stmt with volatile operands alone as well. */
101 if (gimple_has_volatile_ops (stmt
))
108 /* Used to hold all the components required to do SSA PHI elimination.
109 The node and pred/succ list is a simple linear list of nodes and
110 edges represented as pairs of nodes.
112 The predecessor and successor list: Nodes are entered in pairs, where
113 [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
114 predecessors, all the odd elements are successors.
117 When implemented as bitmaps, very large programs SSA->Normal times were
118 being dominated by clearing the interference graph.
120 Typically this list of edges is extremely small since it only includes
121 PHI results and uses from a single edge which have not coalesced with
122 each other. This means that no virtual PHI nodes are included, and
123 empirical evidence suggests that the number of edges rarely exceed
124 3, and in a bootstrap of GCC, the maximum size encountered was 7.
125 This also limits the number of possible nodes that are involved to
126 rarely more than 6, and in the bootstrap of gcc, the maximum number
127 of nodes encountered was 12. */
131 elim_graph (var_map map
);
133 /* Size of the elimination vectors. */
136 /* List of nodes in the elimination graph. */
139 /* The predecessor and successor edge list. */
140 auto_vec
<int> edge_list
;
142 /* Source locus on each edge */
143 auto_vec
<source_location
> edge_locus
;
145 /* Visited vector. */
146 auto_sbitmap visited
;
148 /* Stack for visited nodes. */
151 /* The variable partition map. */
154 /* Edge being eliminated by this graph. */
157 /* List of constant copies to emit. These are pushed on in pairs. */
158 auto_vec
<int> const_dests
;
159 auto_vec
<tree
> const_copies
;
161 /* Source locations for any constant copies. */
162 auto_vec
<source_location
> copy_locus
;
166 /* For an edge E find out a good source location to associate with
167 instructions inserted on edge E. If E has an implicit goto set,
168 use its location. Otherwise search instructions in predecessors
169 of E for a location, and use that one. That makes sense because
170 we insert on edges for PHI nodes, and effects of PHIs happen on
171 the end of the predecessor conceptually. */
174 set_location_for_edge (edge e
)
178 set_curr_insn_location (e
->goto_locus
);
182 basic_block bb
= e
->src
;
183 gimple_stmt_iterator gsi
;
187 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
189 gimple
*stmt
= gsi_stmt (gsi
);
190 if (is_gimple_debug (stmt
))
192 if (gimple_has_location (stmt
) || gimple_block (stmt
))
194 set_curr_insn_location (gimple_location (stmt
));
198 /* Nothing found in this basic block. Make a half-assed attempt
199 to continue with another block. */
200 if (single_pred_p (bb
))
201 bb
= single_pred (bb
);
205 while (bb
!= e
->src
);
209 /* Emit insns to copy SRC into DEST converting SRC if necessary. As
210 SRC/DEST might be BLKmode memory locations SIZEEXP is a tree from
211 which we deduce the size to copy in that case. */
213 static inline rtx_insn
*
214 emit_partition_copy (rtx dest
, rtx src
, int unsignedsrcp
, tree sizeexp
)
218 if (GET_MODE (src
) != VOIDmode
&& GET_MODE (src
) != GET_MODE (dest
))
219 src
= convert_to_mode (GET_MODE (dest
), src
, unsignedsrcp
);
220 if (GET_MODE (src
) == BLKmode
)
222 gcc_assert (GET_MODE (dest
) == BLKmode
);
223 emit_block_move (dest
, src
, expr_size (sizeexp
), BLOCK_OP_NORMAL
);
226 emit_move_insn (dest
, src
);
227 do_pending_stack_adjust ();
229 rtx_insn
*seq
= get_insns ();
235 /* Insert a copy instruction from partition SRC to DEST onto edge E. */
238 insert_partition_copy_on_edge (edge e
, int dest
, int src
, source_location locus
)
241 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
244 "Inserting a partition copy on edge BB%d->BB%d :"
247 e
->dest
->index
, dest
, src
);
248 fprintf (dump_file
, "\n");
251 gcc_assert (SA
.partition_to_pseudo
[dest
]);
252 gcc_assert (SA
.partition_to_pseudo
[src
]);
254 set_location_for_edge (e
);
255 /* If a locus is provided, override the default. */
257 set_curr_insn_location (locus
);
259 var
= partition_to_var (SA
.map
, src
);
260 rtx_insn
*seq
= emit_partition_copy (copy_rtx (SA
.partition_to_pseudo
[dest
]),
261 copy_rtx (SA
.partition_to_pseudo
[src
]),
262 TYPE_UNSIGNED (TREE_TYPE (var
)),
265 insert_insn_on_edge (seq
, e
);
268 /* Insert a copy instruction from expression SRC to partition DEST
272 insert_value_copy_on_edge (edge e
, int dest
, tree src
, source_location locus
)
274 rtx dest_rtx
, seq
, x
;
275 machine_mode dest_mode
, src_mode
;
278 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
281 "Inserting a value copy on edge BB%d->BB%d : PART.%d = ",
283 e
->dest
->index
, dest
);
284 print_generic_expr (dump_file
, src
, TDF_SLIM
);
285 fprintf (dump_file
, "\n");
288 dest_rtx
= copy_rtx (SA
.partition_to_pseudo
[dest
]);
289 gcc_assert (dest_rtx
);
291 set_location_for_edge (e
);
292 /* If a locus is provided, override the default. */
294 set_curr_insn_location (locus
);
298 tree name
= partition_to_var (SA
.map
, dest
);
299 src_mode
= TYPE_MODE (TREE_TYPE (src
));
300 dest_mode
= GET_MODE (dest_rtx
);
301 gcc_assert (src_mode
== TYPE_MODE (TREE_TYPE (name
)));
302 gcc_assert (!REG_P (dest_rtx
)
303 || dest_mode
== promote_ssa_mode (name
, &unsignedp
));
305 if (src_mode
!= dest_mode
)
307 x
= expand_expr (src
, NULL
, src_mode
, EXPAND_NORMAL
);
308 x
= convert_modes (dest_mode
, src_mode
, x
, unsignedp
);
310 else if (src_mode
== BLKmode
)
313 store_expr (src
, x
, 0, false, false);
316 x
= expand_expr (src
, dest_rtx
, dest_mode
, EXPAND_NORMAL
);
319 emit_move_insn (dest_rtx
, x
);
320 do_pending_stack_adjust ();
325 insert_insn_on_edge (seq
, e
);
328 /* Insert a copy instruction from RTL expression SRC to partition DEST
332 insert_rtx_to_part_on_edge (edge e
, int dest
, rtx src
, int unsignedsrcp
,
333 source_location locus
)
335 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
338 "Inserting a temp copy on edge BB%d->BB%d : PART.%d = ",
340 e
->dest
->index
, dest
);
341 print_simple_rtl (dump_file
, src
);
342 fprintf (dump_file
, "\n");
345 gcc_assert (SA
.partition_to_pseudo
[dest
]);
347 set_location_for_edge (e
);
348 /* If a locus is provided, override the default. */
350 set_curr_insn_location (locus
);
352 /* We give the destination as sizeexp in case src/dest are BLKmode
353 mems. Usually we give the source. As we result from SSA names
354 the left and right size should be the same (and no WITH_SIZE_EXPR
355 involved), so it doesn't matter. */
356 rtx_insn
*seq
= emit_partition_copy (copy_rtx (SA
.partition_to_pseudo
[dest
]),
358 partition_to_var (SA
.map
, dest
));
360 insert_insn_on_edge (seq
, e
);
363 /* Insert a copy instruction from partition SRC to RTL lvalue DEST
367 insert_part_to_rtx_on_edge (edge e
, rtx dest
, int src
, source_location locus
)
370 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
373 "Inserting a temp copy on edge BB%d->BB%d : ",
376 print_simple_rtl (dump_file
, dest
);
377 fprintf (dump_file
, "= PART.%d\n", src
);
380 gcc_assert (SA
.partition_to_pseudo
[src
]);
382 set_location_for_edge (e
);
383 /* If a locus is provided, override the default. */
385 set_curr_insn_location (locus
);
387 var
= partition_to_var (SA
.map
, src
);
388 rtx_insn
*seq
= emit_partition_copy (dest
,
389 copy_rtx (SA
.partition_to_pseudo
[src
]),
390 TYPE_UNSIGNED (TREE_TYPE (var
)),
393 insert_insn_on_edge (seq
, e
);
397 /* Create an elimination graph for map. */
399 elim_graph::elim_graph (var_map map
) :
400 nodes (30), edge_list (20), edge_locus (10), visited (map
->num_partitions
),
401 stack (30), map (map
), const_dests (20), const_copies (20), copy_locus (10)
406 /* Empty elimination graph G. */
409 clear_elim_graph (elim_graph
*g
)
411 g
->nodes
.truncate (0);
412 g
->edge_list
.truncate (0);
413 g
->edge_locus
.truncate (0);
417 /* Return the number of nodes in graph G. */
420 elim_graph_size (elim_graph
*g
)
422 return g
->nodes
.length ();
426 /* Add NODE to graph G, if it doesn't exist already. */
429 elim_graph_add_node (elim_graph
*g
, int node
)
434 FOR_EACH_VEC_ELT (g
->nodes
, x
, t
)
437 g
->nodes
.safe_push (node
);
441 /* Add the edge PRED->SUCC to graph G. */
444 elim_graph_add_edge (elim_graph
*g
, int pred
, int succ
, source_location locus
)
446 g
->edge_list
.safe_push (pred
);
447 g
->edge_list
.safe_push (succ
);
448 g
->edge_locus
.safe_push (locus
);
452 /* Remove an edge from graph G for which NODE is the predecessor, and
453 return the successor node. -1 is returned if there is no such edge. */
456 elim_graph_remove_succ_edge (elim_graph
*g
, int node
, source_location
*locus
)
460 for (x
= 0; x
< g
->edge_list
.length (); x
+= 2)
461 if (g
->edge_list
[x
] == node
)
463 g
->edge_list
[x
] = -1;
464 y
= g
->edge_list
[x
+ 1];
465 g
->edge_list
[x
+ 1] = -1;
466 *locus
= g
->edge_locus
[x
/ 2];
467 g
->edge_locus
[x
/ 2] = UNKNOWN_LOCATION
;
470 *locus
= UNKNOWN_LOCATION
;
475 /* Find all the nodes in GRAPH which are successors to NODE in the
476 edge list. VAR will hold the partition number found. CODE is the
477 code fragment executed for every node found. */
479 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, LOCUS, CODE) \
483 for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
485 y_ = (GRAPH)->edge_list[x_]; \
488 (void) ((VAR) = (GRAPH)->edge_list[x_ + 1]); \
489 (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
495 /* Find all the nodes which are predecessors of NODE in the edge list for
496 GRAPH. VAR will hold the partition number found. CODE is the
497 code fragment executed for every node found. */
499 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, LOCUS, CODE) \
503 for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
505 y_ = (GRAPH)->edge_list[x_ + 1]; \
508 (void) ((VAR) = (GRAPH)->edge_list[x_]); \
509 (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
515 /* Add T to elimination graph G. */
518 eliminate_name (elim_graph
*g
, int T
)
520 elim_graph_add_node (g
, T
);
523 /* Return true if this phi argument T should have a copy queued when using
524 var_map MAP. PHI nodes should contain only ssa_names and invariants. A
525 test for ssa_name is definitely simpler, but don't let invalid contents
526 slip through in the meantime. */
529 queue_phi_copy_p (var_map map
, tree t
)
531 if (TREE_CODE (t
) == SSA_NAME
)
533 if (var_to_partition (map
, t
) == NO_PARTITION
)
537 gcc_checking_assert (is_gimple_min_invariant (t
));
541 /* Build elimination graph G for basic block BB on incoming PHI edge
545 eliminate_build (elim_graph
*g
)
551 clear_elim_graph (g
);
553 for (gsi
= gsi_start_phis (g
->e
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
555 gphi
*phi
= gsi
.phi ();
556 source_location locus
;
558 p0
= var_to_partition (g
->map
, gimple_phi_result (phi
));
559 /* Ignore results which are not in partitions. */
560 if (p0
== NO_PARTITION
)
563 Ti
= PHI_ARG_DEF (phi
, g
->e
->dest_idx
);
564 locus
= gimple_phi_arg_location_from_edge (phi
, g
->e
);
566 /* If this argument is a constant, or a SSA_NAME which is being
567 left in SSA form, just queue a copy to be emitted on this
569 if (queue_phi_copy_p (g
->map
, Ti
))
571 /* Save constant copies until all other copies have been emitted
573 g
->const_dests
.safe_push (p0
);
574 g
->const_copies
.safe_push (Ti
);
575 g
->copy_locus
.safe_push (locus
);
579 pi
= var_to_partition (g
->map
, Ti
);
582 eliminate_name (g
, p0
);
583 eliminate_name (g
, pi
);
584 elim_graph_add_edge (g
, p0
, pi
, locus
);
591 /* Push successors of T onto the elimination stack for G. */
594 elim_forward (elim_graph
*g
, int T
)
597 source_location locus
;
599 bitmap_set_bit (g
->visited
, T
);
600 FOR_EACH_ELIM_GRAPH_SUCC (g
, T
, S
, locus
,
602 if (!bitmap_bit_p (g
->visited
, S
))
605 g
->stack
.safe_push (T
);
609 /* Return 1 if there unvisited predecessors of T in graph G. */
612 elim_unvisited_predecessor (elim_graph
*g
, int T
)
615 source_location locus
;
617 FOR_EACH_ELIM_GRAPH_PRED (g
, T
, P
, locus
,
619 if (!bitmap_bit_p (g
->visited
, P
))
625 /* Process predecessors first, and insert a copy. */
628 elim_backward (elim_graph
*g
, int T
)
631 source_location locus
;
633 bitmap_set_bit (g
->visited
, T
);
634 FOR_EACH_ELIM_GRAPH_PRED (g
, T
, P
, locus
,
636 if (!bitmap_bit_p (g
->visited
, P
))
638 elim_backward (g
, P
);
639 insert_partition_copy_on_edge (g
->e
, P
, T
, locus
);
644 /* Allocate a new pseudo register usable for storing values sitting
645 in NAME (a decl or SSA name), i.e. with matching mode and attributes. */
648 get_temp_reg (tree name
)
650 tree type
= TREE_TYPE (name
);
652 machine_mode reg_mode
= promote_ssa_mode (name
, &unsignedp
);
653 rtx x
= gen_reg_rtx (reg_mode
);
654 if (POINTER_TYPE_P (type
))
655 mark_reg_pointer (x
, TYPE_ALIGN (TREE_TYPE (type
)));
659 /* Insert required copies for T in graph G. Check for a strongly connected
660 region, and create a temporary to break the cycle if one is found. */
663 elim_create (elim_graph
*g
, int T
)
666 source_location locus
;
668 if (elim_unvisited_predecessor (g
, T
))
670 tree var
= partition_to_var (g
->map
, T
);
671 rtx U
= get_temp_reg (var
);
672 int unsignedsrcp
= TYPE_UNSIGNED (TREE_TYPE (var
));
674 insert_part_to_rtx_on_edge (g
->e
, U
, T
, UNKNOWN_LOCATION
);
675 FOR_EACH_ELIM_GRAPH_PRED (g
, T
, P
, locus
,
677 if (!bitmap_bit_p (g
->visited
, P
))
679 elim_backward (g
, P
);
680 insert_rtx_to_part_on_edge (g
->e
, P
, U
, unsignedsrcp
, locus
);
686 S
= elim_graph_remove_succ_edge (g
, T
, &locus
);
689 bitmap_set_bit (g
->visited
, T
);
690 insert_partition_copy_on_edge (g
->e
, T
, S
, locus
);
696 /* Eliminate all the phi nodes on edge E in graph G. */
699 eliminate_phi (edge e
, elim_graph
*g
)
703 gcc_assert (g
->const_copies
.length () == 0);
704 gcc_assert (g
->copy_locus
.length () == 0);
706 /* Abnormal edges already have everything coalesced. */
707 if (e
->flags
& EDGE_ABNORMAL
)
714 if (elim_graph_size (g
) != 0)
718 bitmap_clear (g
->visited
);
719 g
->stack
.truncate (0);
721 FOR_EACH_VEC_ELT (g
->nodes
, x
, part
)
723 if (!bitmap_bit_p (g
->visited
, part
))
724 elim_forward (g
, part
);
727 bitmap_clear (g
->visited
);
728 while (g
->stack
.length () > 0)
731 if (!bitmap_bit_p (g
->visited
, x
))
736 /* If there are any pending constant copies, issue them now. */
737 while (g
->const_copies
.length () > 0)
741 source_location locus
;
743 src
= g
->const_copies
.pop ();
744 dest
= g
->const_dests
.pop ();
745 locus
= g
->copy_locus
.pop ();
746 insert_value_copy_on_edge (e
, dest
, src
, locus
);
751 /* Remove each argument from PHI. If an arg was the last use of an SSA_NAME,
752 check to see if this allows another PHI node to be removed. */
755 remove_gimple_phi_args (gphi
*phi
)
760 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
762 fprintf (dump_file
, "Removing Dead PHI definition: ");
763 print_gimple_stmt (dump_file
, phi
, 0, TDF_SLIM
);
766 FOR_EACH_PHI_ARG (arg_p
, phi
, iter
, SSA_OP_USE
)
768 tree arg
= USE_FROM_PTR (arg_p
);
769 if (TREE_CODE (arg
) == SSA_NAME
)
771 /* Remove the reference to the existing argument. */
772 SET_USE (arg_p
, NULL_TREE
);
773 if (has_zero_uses (arg
))
776 gimple_stmt_iterator gsi
;
778 stmt
= SSA_NAME_DEF_STMT (arg
);
780 /* Also remove the def if it is a PHI node. */
781 if (gimple_code (stmt
) == GIMPLE_PHI
)
783 remove_gimple_phi_args (as_a
<gphi
*> (stmt
));
784 gsi
= gsi_for_stmt (stmt
);
785 remove_phi_node (&gsi
, true);
793 /* Remove any PHI node which is a virtual PHI, or a PHI with no uses. */
796 eliminate_useless_phis (void)
802 FOR_EACH_BB_FN (bb
, cfun
)
804 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); )
806 gphi
*phi
= gsi
.phi ();
807 result
= gimple_phi_result (phi
);
808 if (virtual_operand_p (result
))
810 /* There should be no arguments which are not virtual, or the
811 results will be incorrect. */
813 for (size_t i
= 0; i
< gimple_phi_num_args (phi
); i
++)
815 tree arg
= PHI_ARG_DEF (phi
, i
);
816 if (TREE_CODE (arg
) == SSA_NAME
817 && !virtual_operand_p (arg
))
819 fprintf (stderr
, "Argument of PHI is not virtual (");
820 print_generic_expr (stderr
, arg
, TDF_SLIM
);
821 fprintf (stderr
, "), but the result is :");
822 print_gimple_stmt (stderr
, phi
, 0, TDF_SLIM
);
823 internal_error ("SSA corruption");
827 remove_phi_node (&gsi
, true);
831 /* Also remove real PHIs with no uses. */
832 if (has_zero_uses (result
))
834 remove_gimple_phi_args (phi
);
835 remove_phi_node (&gsi
, true);
845 /* This function will rewrite the current program using the variable mapping
846 found in MAP. If the replacement vector VALUES is provided, any
847 occurrences of partitions with non-null entries in the vector will be
848 replaced with the expression in the vector instead of its mapped
852 rewrite_trees (var_map map
)
858 /* Search for PHIs where the destination has no partition, but one
859 or more arguments has a partition. This should not happen and can
860 create incorrect code. */
861 FOR_EACH_BB_FN (bb
, cfun
)
864 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
866 gphi
*phi
= gsi
.phi ();
867 tree T0
= var_to_partition_to_var (map
, gimple_phi_result (phi
));
871 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
873 tree arg
= PHI_ARG_DEF (phi
, i
);
875 if (TREE_CODE (arg
) == SSA_NAME
876 && var_to_partition (map
, arg
) != NO_PARTITION
)
878 fprintf (stderr
, "Argument of PHI is in a partition :(");
879 print_generic_expr (stderr
, arg
, TDF_SLIM
);
880 fprintf (stderr
, "), but the result is not :");
881 print_gimple_stmt (stderr
, phi
, 0, TDF_SLIM
);
882 internal_error ("SSA corruption");
890 /* Given the out-of-ssa info object SA (with prepared partitions)
891 eliminate all phi nodes in all basic blocks. Afterwards no
892 basic block will have phi nodes anymore and there are possibly
893 some RTL instructions inserted on edges. */
896 expand_phi_nodes (struct ssaexpand
*sa
)
899 elim_graph
g (sa
->map
);
901 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
)->next_bb
,
902 EXIT_BLOCK_PTR_FOR_FN (cfun
), next_bb
)
903 if (!gimple_seq_empty_p (phi_nodes (bb
)))
907 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
908 eliminate_phi (e
, &g
);
909 set_phi_nodes (bb
, NULL
);
910 /* We can't redirect EH edges in RTL land, so we need to do this
911 here. Redirection happens only when splitting is necessary,
912 which it is only for critical edges, normally. For EH edges
913 it might also be necessary when the successor has more than
914 one predecessor. In that case the edge is either required to
915 be fallthru (which EH edges aren't), or the predecessor needs
916 to end with a jump (which again, isn't the case with EH edges).
917 Hence, split all EH edges on which we inserted instructions
918 and whose successor has multiple predecessors. */
919 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
921 if (e
->insns
.r
&& (e
->flags
& EDGE_EH
)
922 && !single_pred_p (e
->dest
))
924 rtx_insn
*insns
= e
->insns
.r
;
928 single_pred_edge (bb
)->insns
.r
= insns
;
937 /* Remove the ssa-names in the current function and translate them into normal
938 compiler variables. PERFORM_TER is true if Temporary Expression Replacement
939 should also be used. */
942 remove_ssa_form (bool perform_ter
, struct ssaexpand
*sa
)
944 bitmap values
= NULL
;
947 map
= coalesce_ssa_name ();
949 /* Return to viewing the variable list as just all reference variables after
950 coalescing has been performed. */
951 partition_view_normal (map
);
953 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
955 fprintf (dump_file
, "After Coalescing:\n");
956 dump_var_map (dump_file
, map
);
961 values
= find_replaceable_exprs (map
);
962 if (values
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
963 dump_replaceable_exprs (dump_file
, values
);
970 sa
->partitions_for_parm_default_defs
= get_parm_default_def_partitions (map
);
974 /* If not already done so for basic block BB, assign increasing uids
975 to each of its instructions. */
978 maybe_renumber_stmts_bb (basic_block bb
)
981 gimple_stmt_iterator gsi
;
986 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
988 gimple
*stmt
= gsi_stmt (gsi
);
989 gimple_set_uid (stmt
, i
);
995 /* Return true if we can determine that the SSA_NAMEs RESULT (a result
996 of a PHI node) and ARG (one of its arguments) conflict. Return false
997 otherwise, also when we simply aren't sure. */
1000 trivially_conflicts_p (basic_block bb
, tree result
, tree arg
)
1003 imm_use_iterator imm_iter
;
1004 gimple
*defa
= SSA_NAME_DEF_STMT (arg
);
1006 /* If ARG isn't defined in the same block it's too complicated for
1008 if (gimple_bb (defa
) != bb
)
1011 FOR_EACH_IMM_USE_FAST (use
, imm_iter
, result
)
1013 gimple
*use_stmt
= USE_STMT (use
);
1014 if (is_gimple_debug (use_stmt
))
1016 /* Now, if there's a use of RESULT that lies outside this basic block,
1017 then there surely is a conflict with ARG. */
1018 if (gimple_bb (use_stmt
) != bb
)
1020 if (gimple_code (use_stmt
) == GIMPLE_PHI
)
1022 /* The use now is in a real stmt of BB, so if ARG was defined
1023 in a PHI node (like RESULT) both conflict. */
1024 if (gimple_code (defa
) == GIMPLE_PHI
)
1026 maybe_renumber_stmts_bb (bb
);
1027 /* If the use of RESULT occurs after the definition of ARG,
1028 the two conflict too. */
1029 if (gimple_uid (defa
) < gimple_uid (use_stmt
))
1037 /* Search every PHI node for arguments associated with backedges which
1038 we can trivially determine will need a copy (the argument is either
1039 not an SSA_NAME or the argument has a different underlying variable
1040 than the PHI result).
1042 Insert a copy from the PHI argument to a new destination at the
1043 end of the block with the backedge to the top of the loop. Update
1044 the PHI argument to reference this new destination. */
1047 insert_backedge_copies (void)
1052 mark_dfs_back_edges ();
1054 FOR_EACH_BB_FN (bb
, cfun
)
1056 /* Mark block as possibly needing calculation of UIDs. */
1059 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1061 gphi
*phi
= gsi
.phi ();
1062 tree result
= gimple_phi_result (phi
);
1065 if (virtual_operand_p (result
))
1068 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
1070 tree arg
= gimple_phi_arg_def (phi
, i
);
1071 edge e
= gimple_phi_arg_edge (phi
, i
);
1073 /* If the argument is not an SSA_NAME, then we will need a
1074 constant initialization. If the argument is an SSA_NAME with
1075 a different underlying variable then a copy statement will be
1077 if ((e
->flags
& EDGE_DFS_BACK
)
1078 && (TREE_CODE (arg
) != SSA_NAME
1079 || SSA_NAME_VAR (arg
) != SSA_NAME_VAR (result
)
1080 || trivially_conflicts_p (bb
, result
, arg
)))
1084 gimple
*last
= NULL
;
1085 gimple_stmt_iterator gsi2
;
1087 gsi2
= gsi_last_bb (gimple_phi_arg_edge (phi
, i
)->src
);
1088 if (!gsi_end_p (gsi2
))
1089 last
= gsi_stmt (gsi2
);
1091 /* In theory the only way we ought to get back to the
1092 start of a loop should be with a COND_EXPR or GOTO_EXPR.
1093 However, better safe than sorry.
1094 If the block ends with a control statement or
1095 something that might throw, then we have to
1096 insert this assignment before the last
1097 statement. Else insert it after the last statement. */
1098 if (last
&& stmt_ends_bb_p (last
))
1100 /* If the last statement in the block is the definition
1101 site of the PHI argument, then we can't insert
1102 anything after it. */
1103 if (TREE_CODE (arg
) == SSA_NAME
1104 && SSA_NAME_DEF_STMT (arg
) == last
)
1108 /* Create a new instance of the underlying variable of the
1110 name
= copy_ssa_name (result
);
1111 stmt
= gimple_build_assign (name
,
1112 gimple_phi_arg_def (phi
, i
));
1114 /* copy location if present. */
1115 if (gimple_phi_arg_has_location (phi
, i
))
1116 gimple_set_location (stmt
,
1117 gimple_phi_arg_location (phi
, i
));
1119 /* Insert the new statement into the block and update
1121 if (last
&& stmt_ends_bb_p (last
))
1122 gsi_insert_before (&gsi2
, stmt
, GSI_NEW_STMT
);
1124 gsi_insert_after (&gsi2
, stmt
, GSI_NEW_STMT
);
1125 SET_PHI_ARG_DEF (phi
, i
, name
);
1130 /* Unmark this block again. */
1135 /* Free all memory associated with going out of SSA form. SA is
1136 the outof-SSA info object. */
1139 finish_out_of_ssa (struct ssaexpand
*sa
)
1141 free (sa
->partition_to_pseudo
);
1143 BITMAP_FREE (sa
->values
);
1144 delete_var_map (sa
->map
);
1145 BITMAP_FREE (sa
->partitions_for_parm_default_defs
);
1146 memset (sa
, 0, sizeof *sa
);
1149 /* Take the current function out of SSA form, translating PHIs as described in
1150 R. Morgan, ``Building an Optimizing Compiler'',
1151 Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
1154 rewrite_out_of_ssa (struct ssaexpand
*sa
)
1156 /* If elimination of a PHI requires inserting a copy on a backedge,
1157 then we will have to split the backedge which has numerous
1158 undesirable performance effects.
1160 A significant number of such cases can be handled here by inserting
1161 copies into the loop itself. */
1162 insert_backedge_copies ();
1165 /* Eliminate PHIs which are of no use, such as virtual or dead phis. */
1166 eliminate_useless_phis ();
1168 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1169 gimple_dump_cfg (dump_file
, dump_flags
& ~TDF_DETAILS
);
1171 remove_ssa_form (flag_tree_ter
, sa
);
1173 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1174 gimple_dump_cfg (dump_file
, dump_flags
& ~TDF_DETAILS
);