c-family/
[official-gcc.git] / gcc / tree-outof-ssa.c
blob4d0f32083203090143de34dfa714bb80f4183b28
1 /* Convert a program in SSA form into Normal form.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Andrew Macleod <amacleod@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "ggc.h"
28 #include "basic-block.h"
29 #include "gimple-pretty-print.h"
30 #include "bitmap.h"
31 #include "tree-flow.h"
32 #include "dumpfile.h"
33 #include "diagnostic-core.h"
34 #include "ssaexpand.h"
36 /* FIXME: A lot of code here deals with expanding to RTL. All that code
37 should be in cfgexpand.c. */
38 #include "expr.h"
41 DEF_VEC_I(source_location);
42 DEF_VEC_ALLOC_I(source_location,heap);
44 /* Used to hold all the components required to do SSA PHI elimination.
45 The node and pred/succ list is a simple linear list of nodes and
46 edges represented as pairs of nodes.
48 The predecessor and successor list: Nodes are entered in pairs, where
49 [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
50 predecessors, all the odd elements are successors.
52 Rationale:
53 When implemented as bitmaps, very large programs SSA->Normal times were
54 being dominated by clearing the interference graph.
56 Typically this list of edges is extremely small since it only includes
57 PHI results and uses from a single edge which have not coalesced with
58 each other. This means that no virtual PHI nodes are included, and
59 empirical evidence suggests that the number of edges rarely exceed
60 3, and in a bootstrap of GCC, the maximum size encountered was 7.
61 This also limits the number of possible nodes that are involved to
62 rarely more than 6, and in the bootstrap of gcc, the maximum number
63 of nodes encountered was 12. */
65 typedef struct _elim_graph {
66 /* Size of the elimination vectors. */
67 int size;
69 /* List of nodes in the elimination graph. */
70 VEC(int,heap) *nodes;
72 /* The predecessor and successor edge list. */
73 VEC(int,heap) *edge_list;
75 /* Source locus on each edge */
76 VEC(source_location,heap) *edge_locus;
78 /* Visited vector. */
79 sbitmap visited;
81 /* Stack for visited nodes. */
82 VEC(int,heap) *stack;
84 /* The variable partition map. */
85 var_map map;
87 /* Edge being eliminated by this graph. */
88 edge e;
90 /* List of constant copies to emit. These are pushed on in pairs. */
91 VEC(int,heap) *const_dests;
92 VEC(tree,heap) *const_copies;
94 /* Source locations for any constant copies. */
95 VEC(source_location,heap) *copy_locus;
96 } *elim_graph;
99 /* For an edge E find out a good source location to associate with
100 instructions inserted on edge E. If E has an implicit goto set,
101 use its location. Otherwise search instructions in predecessors
102 of E for a location, and use that one. That makes sense because
103 we insert on edges for PHI nodes, and effects of PHIs happen on
104 the end of the predecessor conceptually. */
106 static void
107 set_location_for_edge (edge e)
109 if (e->goto_locus)
111 set_curr_insn_location (e->goto_locus);
113 else
115 basic_block bb = e->src;
116 gimple_stmt_iterator gsi;
120 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
122 gimple stmt = gsi_stmt (gsi);
123 if (is_gimple_debug (stmt))
124 continue;
125 if (gimple_has_location (stmt) || gimple_block (stmt))
127 set_curr_insn_location (gimple_location (stmt));
128 return;
131 /* Nothing found in this basic block. Make a half-assed attempt
132 to continue with another block. */
133 if (single_pred_p (bb))
134 bb = single_pred (bb);
135 else
136 bb = e->src;
138 while (bb != e->src);
142 /* Emit insns to copy SRC into DEST converting SRC if necessary. As
143 SRC/DEST might be BLKmode memory locations SIZEEXP is a tree from
144 which we deduce the size to copy in that case. */
146 static inline rtx
147 emit_partition_copy (rtx dest, rtx src, int unsignedsrcp, tree sizeexp)
149 rtx seq;
151 start_sequence ();
153 if (GET_MODE (src) != VOIDmode && GET_MODE (src) != GET_MODE (dest))
154 src = convert_to_mode (GET_MODE (dest), src, unsignedsrcp);
155 if (GET_MODE (src) == BLKmode)
157 gcc_assert (GET_MODE (dest) == BLKmode);
158 emit_block_move (dest, src, expr_size (sizeexp), BLOCK_OP_NORMAL);
160 else
161 emit_move_insn (dest, src);
163 seq = get_insns ();
164 end_sequence ();
166 return seq;
169 /* Insert a copy instruction from partition SRC to DEST onto edge E. */
171 static void
172 insert_partition_copy_on_edge (edge e, int dest, int src, source_location locus)
174 tree var;
175 rtx seq;
176 if (dump_file && (dump_flags & TDF_DETAILS))
178 fprintf (dump_file,
179 "Inserting a partition copy on edge BB%d->BB%d :"
180 "PART.%d = PART.%d",
181 e->src->index,
182 e->dest->index, dest, src);
183 fprintf (dump_file, "\n");
186 gcc_assert (SA.partition_to_pseudo[dest]);
187 gcc_assert (SA.partition_to_pseudo[src]);
189 set_location_for_edge (e);
190 /* If a locus is provided, override the default. */
191 if (locus)
192 set_curr_insn_location (locus);
194 var = partition_to_var (SA.map, src);
195 seq = emit_partition_copy (SA.partition_to_pseudo[dest],
196 SA.partition_to_pseudo[src],
197 TYPE_UNSIGNED (TREE_TYPE (var)),
198 var);
200 insert_insn_on_edge (seq, e);
203 /* Insert a copy instruction from expression SRC to partition DEST
204 onto edge E. */
206 static void
207 insert_value_copy_on_edge (edge e, int dest, tree src, source_location locus)
209 rtx seq, x;
210 enum machine_mode dest_mode, src_mode;
211 int unsignedp;
212 tree var;
214 if (dump_file && (dump_flags & TDF_DETAILS))
216 fprintf (dump_file,
217 "Inserting a value copy on edge BB%d->BB%d : PART.%d = ",
218 e->src->index,
219 e->dest->index, dest);
220 print_generic_expr (dump_file, src, TDF_SLIM);
221 fprintf (dump_file, "\n");
224 gcc_assert (SA.partition_to_pseudo[dest]);
226 set_location_for_edge (e);
227 /* If a locus is provided, override the default. */
228 if (locus)
229 set_curr_insn_location (locus);
231 start_sequence ();
233 var = SSA_NAME_VAR (partition_to_var (SA.map, dest));
234 src_mode = TYPE_MODE (TREE_TYPE (src));
235 dest_mode = GET_MODE (SA.partition_to_pseudo[dest]);
236 gcc_assert (src_mode == TYPE_MODE (TREE_TYPE (var)));
237 gcc_assert (!REG_P (SA.partition_to_pseudo[dest])
238 || dest_mode == promote_decl_mode (var, &unsignedp));
240 if (src_mode != dest_mode)
242 x = expand_expr (src, NULL, src_mode, EXPAND_NORMAL);
243 x = convert_modes (dest_mode, src_mode, x, unsignedp);
245 else if (src_mode == BLKmode)
247 x = SA.partition_to_pseudo[dest];
248 store_expr (src, x, 0, false);
250 else
251 x = expand_expr (src, SA.partition_to_pseudo[dest],
252 dest_mode, EXPAND_NORMAL);
254 if (x != SA.partition_to_pseudo[dest])
255 emit_move_insn (SA.partition_to_pseudo[dest], x);
256 seq = get_insns ();
257 end_sequence ();
259 insert_insn_on_edge (seq, e);
262 /* Insert a copy instruction from RTL expression SRC to partition DEST
263 onto edge E. */
265 static void
266 insert_rtx_to_part_on_edge (edge e, int dest, rtx src, int unsignedsrcp,
267 source_location locus)
269 rtx seq;
270 if (dump_file && (dump_flags & TDF_DETAILS))
272 fprintf (dump_file,
273 "Inserting a temp copy on edge BB%d->BB%d : PART.%d = ",
274 e->src->index,
275 e->dest->index, dest);
276 print_simple_rtl (dump_file, src);
277 fprintf (dump_file, "\n");
280 gcc_assert (SA.partition_to_pseudo[dest]);
282 set_location_for_edge (e);
283 /* If a locus is provided, override the default. */
284 if (locus)
285 set_curr_insn_location (locus);
287 /* We give the destination as sizeexp in case src/dest are BLKmode
288 mems. Usually we give the source. As we result from SSA names
289 the left and right size should be the same (and no WITH_SIZE_EXPR
290 involved), so it doesn't matter. */
291 seq = emit_partition_copy (SA.partition_to_pseudo[dest],
292 src, unsignedsrcp,
293 partition_to_var (SA.map, dest));
295 insert_insn_on_edge (seq, e);
298 /* Insert a copy instruction from partition SRC to RTL lvalue DEST
299 onto edge E. */
301 static void
302 insert_part_to_rtx_on_edge (edge e, rtx dest, int src, source_location locus)
304 tree var;
305 rtx seq;
306 if (dump_file && (dump_flags & TDF_DETAILS))
308 fprintf (dump_file,
309 "Inserting a temp copy on edge BB%d->BB%d : ",
310 e->src->index,
311 e->dest->index);
312 print_simple_rtl (dump_file, dest);
313 fprintf (dump_file, "= PART.%d\n", src);
316 gcc_assert (SA.partition_to_pseudo[src]);
318 set_location_for_edge (e);
319 /* If a locus is provided, override the default. */
320 if (locus)
321 set_curr_insn_location (locus);
323 var = partition_to_var (SA.map, src);
324 seq = emit_partition_copy (dest,
325 SA.partition_to_pseudo[src],
326 TYPE_UNSIGNED (TREE_TYPE (var)),
327 var);
329 insert_insn_on_edge (seq, e);
333 /* Create an elimination graph with SIZE nodes and associated data
334 structures. */
336 static elim_graph
337 new_elim_graph (int size)
339 elim_graph g = (elim_graph) xmalloc (sizeof (struct _elim_graph));
341 g->nodes = VEC_alloc (int, heap, 30);
342 g->const_dests = VEC_alloc (int, heap, 20);
343 g->const_copies = VEC_alloc (tree, heap, 20);
344 g->copy_locus = VEC_alloc (source_location, heap, 10);
345 g->edge_list = VEC_alloc (int, heap, 20);
346 g->edge_locus = VEC_alloc (source_location, heap, 10);
347 g->stack = VEC_alloc (int, heap, 30);
349 g->visited = sbitmap_alloc (size);
351 return g;
355 /* Empty elimination graph G. */
357 static inline void
358 clear_elim_graph (elim_graph g)
360 VEC_truncate (int, g->nodes, 0);
361 VEC_truncate (int, g->edge_list, 0);
362 VEC_truncate (source_location, g->edge_locus, 0);
366 /* Delete elimination graph G. */
368 static inline void
369 delete_elim_graph (elim_graph g)
371 sbitmap_free (g->visited);
372 VEC_free (int, heap, g->stack);
373 VEC_free (int, heap, g->edge_list);
374 VEC_free (tree, heap, g->const_copies);
375 VEC_free (int, heap, g->const_dests);
376 VEC_free (int, heap, g->nodes);
377 VEC_free (source_location, heap, g->copy_locus);
378 VEC_free (source_location, heap, g->edge_locus);
380 free (g);
384 /* Return the number of nodes in graph G. */
386 static inline int
387 elim_graph_size (elim_graph g)
389 return VEC_length (int, g->nodes);
393 /* Add NODE to graph G, if it doesn't exist already. */
395 static inline void
396 elim_graph_add_node (elim_graph g, int node)
398 int x;
399 int t;
401 FOR_EACH_VEC_ELT (int, g->nodes, x, t)
402 if (t == node)
403 return;
404 VEC_safe_push (int, heap, g->nodes, node);
408 /* Add the edge PRED->SUCC to graph G. */
410 static inline void
411 elim_graph_add_edge (elim_graph g, int pred, int succ, source_location locus)
413 VEC_safe_push (int, heap, g->edge_list, pred);
414 VEC_safe_push (int, heap, g->edge_list, succ);
415 VEC_safe_push (source_location, heap, g->edge_locus, locus);
419 /* Remove an edge from graph G for which NODE is the predecessor, and
420 return the successor node. -1 is returned if there is no such edge. */
422 static inline int
423 elim_graph_remove_succ_edge (elim_graph g, int node, source_location *locus)
425 int y;
426 unsigned x;
427 for (x = 0; x < VEC_length (int, g->edge_list); x += 2)
428 if (VEC_index (int, g->edge_list, x) == node)
430 VEC_replace (int, g->edge_list, x, -1);
431 y = VEC_index (int, g->edge_list, x + 1);
432 VEC_replace (int, g->edge_list, x + 1, -1);
433 *locus = VEC_index (source_location, g->edge_locus, x / 2);
434 VEC_replace (source_location, g->edge_locus, x / 2, UNKNOWN_LOCATION);
435 return y;
437 *locus = UNKNOWN_LOCATION;
438 return -1;
442 /* Find all the nodes in GRAPH which are successors to NODE in the
443 edge list. VAR will hold the partition number found. CODE is the
444 code fragment executed for every node found. */
446 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, LOCUS, CODE) \
447 do { \
448 unsigned x_; \
449 int y_; \
450 for (x_ = 0; x_ < VEC_length (int, (GRAPH)->edge_list); x_ += 2) \
452 y_ = VEC_index (int, (GRAPH)->edge_list, x_); \
453 if (y_ != (NODE)) \
454 continue; \
455 (void) ((VAR) = VEC_index (int, (GRAPH)->edge_list, x_ + 1)); \
456 (void) ((LOCUS) = VEC_index (source_location, \
457 (GRAPH)->edge_locus, x_ / 2)); \
458 CODE; \
460 } while (0)
463 /* Find all the nodes which are predecessors of NODE in the edge list for
464 GRAPH. VAR will hold the partition number found. CODE is the
465 code fragment executed for every node found. */
467 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, LOCUS, CODE) \
468 do { \
469 unsigned x_; \
470 int y_; \
471 for (x_ = 0; x_ < VEC_length (int, (GRAPH)->edge_list); x_ += 2) \
473 y_ = VEC_index (int, (GRAPH)->edge_list, x_ + 1); \
474 if (y_ != (NODE)) \
475 continue; \
476 (void) ((VAR) = VEC_index (int, (GRAPH)->edge_list, x_)); \
477 (void) ((LOCUS) = VEC_index (source_location, \
478 (GRAPH)->edge_locus, x_ / 2)); \
479 CODE; \
481 } while (0)
484 /* Add T to elimination graph G. */
486 static inline void
487 eliminate_name (elim_graph g, int T)
489 elim_graph_add_node (g, T);
493 /* Build elimination graph G for basic block BB on incoming PHI edge
494 G->e. */
496 static void
497 eliminate_build (elim_graph g)
499 tree Ti;
500 int p0, pi;
501 gimple_stmt_iterator gsi;
503 clear_elim_graph (g);
505 for (gsi = gsi_start_phis (g->e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
507 gimple phi = gsi_stmt (gsi);
508 source_location locus;
510 p0 = var_to_partition (g->map, gimple_phi_result (phi));
511 /* Ignore results which are not in partitions. */
512 if (p0 == NO_PARTITION)
513 continue;
515 Ti = PHI_ARG_DEF (phi, g->e->dest_idx);
516 locus = gimple_phi_arg_location_from_edge (phi, g->e);
518 /* If this argument is a constant, or a SSA_NAME which is being
519 left in SSA form, just queue a copy to be emitted on this
520 edge. */
521 if (!phi_ssa_name_p (Ti)
522 || (TREE_CODE (Ti) == SSA_NAME
523 && var_to_partition (g->map, Ti) == NO_PARTITION))
525 /* Save constant copies until all other copies have been emitted
526 on this edge. */
527 VEC_safe_push (int, heap, g->const_dests, p0);
528 VEC_safe_push (tree, heap, g->const_copies, Ti);
529 VEC_safe_push (source_location, heap, g->copy_locus, locus);
531 else
533 pi = var_to_partition (g->map, Ti);
534 if (p0 != pi)
536 eliminate_name (g, p0);
537 eliminate_name (g, pi);
538 elim_graph_add_edge (g, p0, pi, locus);
545 /* Push successors of T onto the elimination stack for G. */
547 static void
548 elim_forward (elim_graph g, int T)
550 int S;
551 source_location locus;
553 SET_BIT (g->visited, T);
554 FOR_EACH_ELIM_GRAPH_SUCC (g, T, S, locus,
556 if (!TEST_BIT (g->visited, S))
557 elim_forward (g, S);
559 VEC_safe_push (int, heap, g->stack, T);
563 /* Return 1 if there unvisited predecessors of T in graph G. */
565 static int
566 elim_unvisited_predecessor (elim_graph g, int T)
568 int P;
569 source_location locus;
571 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
573 if (!TEST_BIT (g->visited, P))
574 return 1;
576 return 0;
579 /* Process predecessors first, and insert a copy. */
581 static void
582 elim_backward (elim_graph g, int T)
584 int P;
585 source_location locus;
587 SET_BIT (g->visited, T);
588 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
590 if (!TEST_BIT (g->visited, P))
592 elim_backward (g, P);
593 insert_partition_copy_on_edge (g->e, P, T, locus);
598 /* Allocate a new pseudo register usable for storing values sitting
599 in NAME (a decl or SSA name), i.e. with matching mode and attributes. */
601 static rtx
602 get_temp_reg (tree name)
604 tree var = TREE_CODE (name) == SSA_NAME ? SSA_NAME_VAR (name) : name;
605 tree type = TREE_TYPE (var);
606 int unsignedp;
607 enum machine_mode reg_mode = promote_decl_mode (var, &unsignedp);
608 rtx x = gen_reg_rtx (reg_mode);
609 if (POINTER_TYPE_P (type))
610 mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (var))));
611 return x;
614 /* Insert required copies for T in graph G. Check for a strongly connected
615 region, and create a temporary to break the cycle if one is found. */
617 static void
618 elim_create (elim_graph g, int T)
620 int P, S;
621 source_location locus;
623 if (elim_unvisited_predecessor (g, T))
625 tree var = partition_to_var (g->map, T);
626 rtx U = get_temp_reg (var);
627 int unsignedsrcp = TYPE_UNSIGNED (TREE_TYPE (var));
629 insert_part_to_rtx_on_edge (g->e, U, T, UNKNOWN_LOCATION);
630 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
632 if (!TEST_BIT (g->visited, P))
634 elim_backward (g, P);
635 insert_rtx_to_part_on_edge (g->e, P, U, unsignedsrcp, locus);
639 else
641 S = elim_graph_remove_succ_edge (g, T, &locus);
642 if (S != -1)
644 SET_BIT (g->visited, T);
645 insert_partition_copy_on_edge (g->e, T, S, locus);
651 /* Eliminate all the phi nodes on edge E in graph G. */
653 static void
654 eliminate_phi (edge e, elim_graph g)
656 int x;
658 gcc_assert (VEC_length (tree, g->const_copies) == 0);
659 gcc_assert (VEC_length (source_location, g->copy_locus) == 0);
661 /* Abnormal edges already have everything coalesced. */
662 if (e->flags & EDGE_ABNORMAL)
663 return;
665 g->e = e;
667 eliminate_build (g);
669 if (elim_graph_size (g) != 0)
671 int part;
673 sbitmap_zero (g->visited);
674 VEC_truncate (int, g->stack, 0);
676 FOR_EACH_VEC_ELT (int, g->nodes, x, part)
678 if (!TEST_BIT (g->visited, part))
679 elim_forward (g, part);
682 sbitmap_zero (g->visited);
683 while (VEC_length (int, g->stack) > 0)
685 x = VEC_pop (int, g->stack);
686 if (!TEST_BIT (g->visited, x))
687 elim_create (g, x);
691 /* If there are any pending constant copies, issue them now. */
692 while (VEC_length (tree, g->const_copies) > 0)
694 int dest;
695 tree src;
696 source_location locus;
698 src = VEC_pop (tree, g->const_copies);
699 dest = VEC_pop (int, g->const_dests);
700 locus = VEC_pop (source_location, g->copy_locus);
701 insert_value_copy_on_edge (e, dest, src, locus);
706 /* Remove each argument from PHI. If an arg was the last use of an SSA_NAME,
707 check to see if this allows another PHI node to be removed. */
709 static void
710 remove_gimple_phi_args (gimple phi)
712 use_operand_p arg_p;
713 ssa_op_iter iter;
715 if (dump_file && (dump_flags & TDF_DETAILS))
717 fprintf (dump_file, "Removing Dead PHI definition: ");
718 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
721 FOR_EACH_PHI_ARG (arg_p, phi, iter, SSA_OP_USE)
723 tree arg = USE_FROM_PTR (arg_p);
724 if (TREE_CODE (arg) == SSA_NAME)
726 /* Remove the reference to the existing argument. */
727 SET_USE (arg_p, NULL_TREE);
728 if (has_zero_uses (arg))
730 gimple stmt;
731 gimple_stmt_iterator gsi;
733 stmt = SSA_NAME_DEF_STMT (arg);
735 /* Also remove the def if it is a PHI node. */
736 if (gimple_code (stmt) == GIMPLE_PHI)
738 remove_gimple_phi_args (stmt);
739 gsi = gsi_for_stmt (stmt);
740 remove_phi_node (&gsi, true);
748 /* Remove any PHI node which is a virtual PHI, or a PHI with no uses. */
750 static void
751 eliminate_useless_phis (void)
753 basic_block bb;
754 gimple_stmt_iterator gsi;
755 tree result;
757 FOR_EACH_BB (bb)
759 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
761 gimple phi = gsi_stmt (gsi);
762 result = gimple_phi_result (phi);
763 if (virtual_operand_p (result))
765 #ifdef ENABLE_CHECKING
766 size_t i;
767 /* There should be no arguments which are not virtual, or the
768 results will be incorrect. */
769 for (i = 0; i < gimple_phi_num_args (phi); i++)
771 tree arg = PHI_ARG_DEF (phi, i);
772 if (TREE_CODE (arg) == SSA_NAME
773 && !virtual_operand_p (arg))
775 fprintf (stderr, "Argument of PHI is not virtual (");
776 print_generic_expr (stderr, arg, TDF_SLIM);
777 fprintf (stderr, "), but the result is :");
778 print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
779 internal_error ("SSA corruption");
782 #endif
783 remove_phi_node (&gsi, true);
785 else
787 /* Also remove real PHIs with no uses. */
788 if (has_zero_uses (result))
790 remove_gimple_phi_args (phi);
791 remove_phi_node (&gsi, true);
793 else
794 gsi_next (&gsi);
801 /* This function will rewrite the current program using the variable mapping
802 found in MAP. If the replacement vector VALUES is provided, any
803 occurrences of partitions with non-null entries in the vector will be
804 replaced with the expression in the vector instead of its mapped
805 variable. */
807 static void
808 rewrite_trees (var_map map ATTRIBUTE_UNUSED)
810 #ifdef ENABLE_CHECKING
811 basic_block bb;
812 /* Search for PHIs where the destination has no partition, but one
813 or more arguments has a partition. This should not happen and can
814 create incorrect code. */
815 FOR_EACH_BB (bb)
817 gimple_stmt_iterator gsi;
818 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
820 gimple phi = gsi_stmt (gsi);
821 tree T0 = var_to_partition_to_var (map, gimple_phi_result (phi));
822 if (T0 == NULL_TREE)
824 size_t i;
825 for (i = 0; i < gimple_phi_num_args (phi); i++)
827 tree arg = PHI_ARG_DEF (phi, i);
829 if (TREE_CODE (arg) == SSA_NAME
830 && var_to_partition (map, arg) != NO_PARTITION)
832 fprintf (stderr, "Argument of PHI is in a partition :(");
833 print_generic_expr (stderr, arg, TDF_SLIM);
834 fprintf (stderr, "), but the result is not :");
835 print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
836 internal_error ("SSA corruption");
842 #endif
845 /* Given the out-of-ssa info object SA (with prepared partitions)
846 eliminate all phi nodes in all basic blocks. Afterwards no
847 basic block will have phi nodes anymore and there are possibly
848 some RTL instructions inserted on edges. */
850 void
851 expand_phi_nodes (struct ssaexpand *sa)
853 basic_block bb;
854 elim_graph g = new_elim_graph (sa->map->num_partitions);
855 g->map = sa->map;
857 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR, next_bb)
858 if (!gimple_seq_empty_p (phi_nodes (bb)))
860 edge e;
861 edge_iterator ei;
862 FOR_EACH_EDGE (e, ei, bb->preds)
863 eliminate_phi (e, g);
864 set_phi_nodes (bb, NULL);
865 /* We can't redirect EH edges in RTL land, so we need to do this
866 here. Redirection happens only when splitting is necessary,
867 which it is only for critical edges, normally. For EH edges
868 it might also be necessary when the successor has more than
869 one predecessor. In that case the edge is either required to
870 be fallthru (which EH edges aren't), or the predecessor needs
871 to end with a jump (which again, isn't the case with EH edges).
872 Hence, split all EH edges on which we inserted instructions
873 and whose successor has multiple predecessors. */
874 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
876 if (e->insns.r && (e->flags & EDGE_EH)
877 && !single_pred_p (e->dest))
879 rtx insns = e->insns.r;
880 basic_block bb;
881 e->insns.r = NULL_RTX;
882 bb = split_edge (e);
883 single_pred_edge (bb)->insns.r = insns;
885 else
886 ei_next (&ei);
890 delete_elim_graph (g);
894 /* Remove the ssa-names in the current function and translate them into normal
895 compiler variables. PERFORM_TER is true if Temporary Expression Replacement
896 should also be used. */
898 static void
899 remove_ssa_form (bool perform_ter, struct ssaexpand *sa)
901 bitmap values = NULL;
902 var_map map;
903 unsigned i;
905 map = coalesce_ssa_name ();
907 /* Return to viewing the variable list as just all reference variables after
908 coalescing has been performed. */
909 partition_view_normal (map, false);
911 if (dump_file && (dump_flags & TDF_DETAILS))
913 fprintf (dump_file, "After Coalescing:\n");
914 dump_var_map (dump_file, map);
917 if (perform_ter)
919 values = find_replaceable_exprs (map);
920 if (values && dump_file && (dump_flags & TDF_DETAILS))
921 dump_replaceable_exprs (dump_file, values);
924 rewrite_trees (map);
926 sa->map = map;
927 sa->values = values;
928 sa->partition_has_default_def = BITMAP_ALLOC (NULL);
929 for (i = 1; i < num_ssa_names; i++)
931 tree t = ssa_name (i);
932 if (t && SSA_NAME_IS_DEFAULT_DEF (t))
934 int p = var_to_partition (map, t);
935 if (p != NO_PARTITION)
936 bitmap_set_bit (sa->partition_has_default_def, p);
942 /* If not already done so for basic block BB, assign increasing uids
943 to each of its instructions. */
945 static void
946 maybe_renumber_stmts_bb (basic_block bb)
948 unsigned i = 0;
949 gimple_stmt_iterator gsi;
951 if (!bb->aux)
952 return;
953 bb->aux = NULL;
954 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
956 gimple stmt = gsi_stmt (gsi);
957 gimple_set_uid (stmt, i);
958 i++;
963 /* Return true if we can determine that the SSA_NAMEs RESULT (a result
964 of a PHI node) and ARG (one of its arguments) conflict. Return false
965 otherwise, also when we simply aren't sure. */
967 static bool
968 trivially_conflicts_p (basic_block bb, tree result, tree arg)
970 use_operand_p use;
971 imm_use_iterator imm_iter;
972 gimple defa = SSA_NAME_DEF_STMT (arg);
974 /* If ARG isn't defined in the same block it's too complicated for
975 our little mind. */
976 if (gimple_bb (defa) != bb)
977 return false;
979 FOR_EACH_IMM_USE_FAST (use, imm_iter, result)
981 gimple use_stmt = USE_STMT (use);
982 if (is_gimple_debug (use_stmt))
983 continue;
984 /* Now, if there's a use of RESULT that lies outside this basic block,
985 then there surely is a conflict with ARG. */
986 if (gimple_bb (use_stmt) != bb)
987 return true;
988 if (gimple_code (use_stmt) == GIMPLE_PHI)
989 continue;
990 /* The use now is in a real stmt of BB, so if ARG was defined
991 in a PHI node (like RESULT) both conflict. */
992 if (gimple_code (defa) == GIMPLE_PHI)
993 return true;
994 maybe_renumber_stmts_bb (bb);
995 /* If the use of RESULT occurs after the definition of ARG,
996 the two conflict too. */
997 if (gimple_uid (defa) < gimple_uid (use_stmt))
998 return true;
1001 return false;
1005 /* Search every PHI node for arguments associated with backedges which
1006 we can trivially determine will need a copy (the argument is either
1007 not an SSA_NAME or the argument has a different underlying variable
1008 than the PHI result).
1010 Insert a copy from the PHI argument to a new destination at the
1011 end of the block with the backedge to the top of the loop. Update
1012 the PHI argument to reference this new destination. */
1014 static void
1015 insert_backedge_copies (void)
1017 basic_block bb;
1018 gimple_stmt_iterator gsi;
1020 mark_dfs_back_edges ();
1022 FOR_EACH_BB (bb)
1024 /* Mark block as possibly needing calculation of UIDs. */
1025 bb->aux = &bb->aux;
1027 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1029 gimple phi = gsi_stmt (gsi);
1030 tree result = gimple_phi_result (phi);
1031 size_t i;
1033 if (virtual_operand_p (result))
1034 continue;
1036 for (i = 0; i < gimple_phi_num_args (phi); i++)
1038 tree arg = gimple_phi_arg_def (phi, i);
1039 edge e = gimple_phi_arg_edge (phi, i);
1041 /* If the argument is not an SSA_NAME, then we will need a
1042 constant initialization. If the argument is an SSA_NAME with
1043 a different underlying variable then a copy statement will be
1044 needed. */
1045 if ((e->flags & EDGE_DFS_BACK)
1046 && (TREE_CODE (arg) != SSA_NAME
1047 || SSA_NAME_VAR (arg) != SSA_NAME_VAR (result)
1048 || trivially_conflicts_p (bb, result, arg)))
1050 tree name;
1051 gimple stmt, last = NULL;
1052 gimple_stmt_iterator gsi2;
1054 gsi2 = gsi_last_bb (gimple_phi_arg_edge (phi, i)->src);
1055 if (!gsi_end_p (gsi2))
1056 last = gsi_stmt (gsi2);
1058 /* In theory the only way we ought to get back to the
1059 start of a loop should be with a COND_EXPR or GOTO_EXPR.
1060 However, better safe than sorry.
1061 If the block ends with a control statement or
1062 something that might throw, then we have to
1063 insert this assignment before the last
1064 statement. Else insert it after the last statement. */
1065 if (last && stmt_ends_bb_p (last))
1067 /* If the last statement in the block is the definition
1068 site of the PHI argument, then we can't insert
1069 anything after it. */
1070 if (TREE_CODE (arg) == SSA_NAME
1071 && SSA_NAME_DEF_STMT (arg) == last)
1072 continue;
1075 /* Create a new instance of the underlying variable of the
1076 PHI result. */
1077 name = copy_ssa_name (result, NULL);
1078 stmt = gimple_build_assign (name,
1079 gimple_phi_arg_def (phi, i));
1081 /* copy location if present. */
1082 if (gimple_phi_arg_has_location (phi, i))
1083 gimple_set_location (stmt,
1084 gimple_phi_arg_location (phi, i));
1086 /* Insert the new statement into the block and update
1087 the PHI node. */
1088 if (last && stmt_ends_bb_p (last))
1089 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
1090 else
1091 gsi_insert_after (&gsi2, stmt, GSI_NEW_STMT);
1092 SET_PHI_ARG_DEF (phi, i, name);
1097 /* Unmark this block again. */
1098 bb->aux = NULL;
1102 /* Free all memory associated with going out of SSA form. SA is
1103 the outof-SSA info object. */
1105 void
1106 finish_out_of_ssa (struct ssaexpand *sa)
1108 free (sa->partition_to_pseudo);
1109 if (sa->values)
1110 BITMAP_FREE (sa->values);
1111 delete_var_map (sa->map);
1112 BITMAP_FREE (sa->partition_has_default_def);
1113 memset (sa, 0, sizeof *sa);
1116 /* Take the current function out of SSA form, translating PHIs as described in
1117 R. Morgan, ``Building an Optimizing Compiler'',
1118 Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
1120 unsigned int
1121 rewrite_out_of_ssa (struct ssaexpand *sa)
1123 /* If elimination of a PHI requires inserting a copy on a backedge,
1124 then we will have to split the backedge which has numerous
1125 undesirable performance effects.
1127 A significant number of such cases can be handled here by inserting
1128 copies into the loop itself. */
1129 insert_backedge_copies ();
1132 /* Eliminate PHIs which are of no use, such as virtual or dead phis. */
1133 eliminate_useless_phis ();
1135 if (dump_file && (dump_flags & TDF_DETAILS))
1136 gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1138 remove_ssa_form (flag_tree_ter, sa);
1140 if (dump_file && (dump_flags & TDF_DETAILS))
1141 gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1143 return 0;