* df-scan.c (df_collection_rec): Adjust.
[official-gcc.git] / gcc / tree-outof-ssa.c
blob98a0c2ce9e82ee4d8983d587171208cfe62ad1b0
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)
10 any later version.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "ggc.h"
27 #include "basic-block.h"
28 #include "gimple-pretty-print.h"
29 #include "bitmap.h"
30 #include "sbitmap.h"
31 #include "gimple.h"
32 #include "gimple-ssa.h"
33 #include "tree-cfg.h"
34 #include "tree-phinodes.h"
35 #include "ssa-iterators.h"
36 #include "tree-ssanames.h"
37 #include "dumpfile.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. */
43 #include "expr.h"
45 /* Return TRUE if expression STMT is suitable for replacement. */
47 bool
48 ssa_is_replaceable_p (gimple stmt)
50 use_operand_p use_p;
51 tree def;
52 gimple use_stmt;
54 /* Only consider modify stmts. */
55 if (!is_gimple_assign (stmt))
56 return false;
58 /* If the statement may throw an exception, it cannot be replaced. */
59 if (stmt_could_throw_p (stmt))
60 return false;
62 /* Punt if there is more than 1 def. */
63 def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
64 if (!def)
65 return false;
67 /* Only consider definitions which have a single use. */
68 if (!single_imm_use (def, &use_p, &use_stmt))
69 return false;
71 /* Used in this block, but at the TOP of the block, not the end. */
72 if (gimple_code (use_stmt) == GIMPLE_PHI)
73 return false;
75 /* There must be no VDEFs. */
76 if (gimple_vdef (stmt))
77 return false;
79 /* Float expressions must go through memory if float-store is on. */
80 if (flag_float_store
81 && FLOAT_TYPE_P (gimple_expr_type (stmt)))
82 return false;
84 /* An assignment with a register variable on the RHS is not
85 replaceable. */
86 if (gimple_assign_rhs_code (stmt) == VAR_DECL
87 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt)))
88 return false;
90 /* No function calls can be replaced. */
91 if (is_gimple_call (stmt))
92 return false;
94 /* Leave any stmt with volatile operands alone as well. */
95 if (gimple_has_volatile_ops (stmt))
96 return false;
98 return true;
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.
110 Rationale:
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. */
125 int size;
127 /* List of nodes in the elimination graph. */
128 vec<int> nodes;
130 /* The predecessor and successor edge list. */
131 vec<int> edge_list;
133 /* Source locus on each edge */
134 vec<source_location> edge_locus;
136 /* Visited vector. */
137 sbitmap visited;
139 /* Stack for visited nodes. */
140 vec<int> stack;
142 /* The variable partition map. */
143 var_map map;
145 /* Edge being eliminated by this graph. */
146 edge e;
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;
154 } *elim_graph;
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. */
164 static void
165 set_location_for_edge (edge e)
167 if (e->goto_locus)
169 set_curr_insn_location (e->goto_locus);
171 else
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))
182 continue;
183 if (gimple_has_location (stmt) || gimple_block (stmt))
185 set_curr_insn_location (gimple_location (stmt));
186 return;
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);
193 else
194 bb = e->src;
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. */
204 static inline rtx
205 emit_partition_copy (rtx dest, rtx src, int unsignedsrcp, tree sizeexp)
207 rtx seq;
209 start_sequence ();
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);
218 else
219 emit_move_insn (dest, src);
221 seq = get_insns ();
222 end_sequence ();
224 return seq;
227 /* Insert a copy instruction from partition SRC to DEST onto edge E. */
229 static void
230 insert_partition_copy_on_edge (edge e, int dest, int src, source_location locus)
232 tree var;
233 rtx seq;
234 if (dump_file && (dump_flags & TDF_DETAILS))
236 fprintf (dump_file,
237 "Inserting a partition copy on edge BB%d->BB%d :"
238 "PART.%d = PART.%d",
239 e->src->index,
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. */
249 if (locus)
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)),
256 var);
258 insert_insn_on_edge (seq, e);
261 /* Insert a copy instruction from expression SRC to partition DEST
262 onto edge E. */
264 static void
265 insert_value_copy_on_edge (edge e, int dest, tree src, source_location locus)
267 rtx seq, x;
268 enum machine_mode dest_mode, src_mode;
269 int unsignedp;
270 tree var;
272 if (dump_file && (dump_flags & TDF_DETAILS))
274 fprintf (dump_file,
275 "Inserting a value copy on edge BB%d->BB%d : PART.%d = ",
276 e->src->index,
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. */
286 if (locus)
287 set_curr_insn_location (locus);
289 start_sequence ();
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);
308 else
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);
314 seq = get_insns ();
315 end_sequence ();
317 insert_insn_on_edge (seq, e);
320 /* Insert a copy instruction from RTL expression SRC to partition DEST
321 onto edge E. */
323 static void
324 insert_rtx_to_part_on_edge (edge e, int dest, rtx src, int unsignedsrcp,
325 source_location locus)
327 rtx seq;
328 if (dump_file && (dump_flags & TDF_DETAILS))
330 fprintf (dump_file,
331 "Inserting a temp copy on edge BB%d->BB%d : PART.%d = ",
332 e->src->index,
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. */
342 if (locus)
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],
350 src, unsignedsrcp,
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
357 onto edge E. */
359 static void
360 insert_part_to_rtx_on_edge (edge e, rtx dest, int src, source_location locus)
362 tree var;
363 rtx seq;
364 if (dump_file && (dump_flags & TDF_DETAILS))
366 fprintf (dump_file,
367 "Inserting a temp copy on edge BB%d->BB%d : ",
368 e->src->index,
369 e->dest->index);
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. */
378 if (locus)
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)),
385 var);
387 insert_insn_on_edge (seq, e);
391 /* Create an elimination graph with SIZE nodes and associated data
392 structures. */
394 static elim_graph
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);
409 return g;
413 /* Empty elimination graph G. */
415 static inline void
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. */
426 static inline void
427 delete_elim_graph (elim_graph g)
429 sbitmap_free (g->visited);
430 g->stack.release ();
431 g->edge_list.release ();
432 g->const_copies.release ();
433 g->const_dests.release ();
434 g->nodes.release ();
435 g->copy_locus.release ();
436 g->edge_locus.release ();
438 free (g);
442 /* Return the number of nodes in graph G. */
444 static inline int
445 elim_graph_size (elim_graph g)
447 return g->nodes.length ();
451 /* Add NODE to graph G, if it doesn't exist already. */
453 static inline void
454 elim_graph_add_node (elim_graph g, int node)
456 int x;
457 int t;
459 FOR_EACH_VEC_ELT (g->nodes, x, t)
460 if (t == node)
461 return;
462 g->nodes.safe_push (node);
466 /* Add the edge PRED->SUCC to graph G. */
468 static inline void
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. */
480 static inline int
481 elim_graph_remove_succ_edge (elim_graph g, int node, source_location *locus)
483 int y;
484 unsigned x;
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;
493 return y;
495 *locus = UNKNOWN_LOCATION;
496 return -1;
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) \
505 do { \
506 unsigned x_; \
507 int y_; \
508 for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
510 y_ = (GRAPH)->edge_list[x_]; \
511 if (y_ != (NODE)) \
512 continue; \
513 (void) ((VAR) = (GRAPH)->edge_list[x_ + 1]); \
514 (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
515 CODE; \
517 } while (0)
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) \
525 do { \
526 unsigned x_; \
527 int y_; \
528 for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
530 y_ = (GRAPH)->edge_list[x_ + 1]; \
531 if (y_ != (NODE)) \
532 continue; \
533 (void) ((VAR) = (GRAPH)->edge_list[x_]); \
534 (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
535 CODE; \
537 } while (0)
540 /* Add T to elimination graph G. */
542 static inline void
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
550 G->e. */
552 static void
553 eliminate_build (elim_graph g)
555 tree Ti;
556 int p0, pi;
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)
569 continue;
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
576 edge. */
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
582 on this edge. */
583 g->const_dests.safe_push (p0);
584 g->const_copies.safe_push (Ti);
585 g->copy_locus.safe_push (locus);
587 else
589 pi = var_to_partition (g->map, Ti);
590 if (p0 != pi)
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. */
603 static void
604 elim_forward (elim_graph g, int T)
606 int S;
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))
613 elim_forward (g, S);
615 g->stack.safe_push (T);
619 /* Return 1 if there unvisited predecessors of T in graph G. */
621 static int
622 elim_unvisited_predecessor (elim_graph g, int T)
624 int P;
625 source_location locus;
627 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
629 if (!bitmap_bit_p (g->visited, P))
630 return 1;
632 return 0;
635 /* Process predecessors first, and insert a copy. */
637 static void
638 elim_backward (elim_graph g, int T)
640 int P;
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. */
657 static rtx
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);
662 int unsignedp;
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))));
667 return x;
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. */
673 static void
674 elim_create (elim_graph g, int T)
676 int P, S;
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);
695 else
697 S = elim_graph_remove_succ_edge (g, T, &locus);
698 if (S != -1)
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. */
709 static void
710 eliminate_phi (edge e, elim_graph g)
712 int x;
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)
719 return;
721 g->e = e;
723 eliminate_build (g);
725 if (elim_graph_size (g) != 0)
727 int part;
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)
741 x = g->stack.pop ();
742 if (!bitmap_bit_p (g->visited, x))
743 elim_create (g, x);
747 /* If there are any pending constant copies, issue them now. */
748 while (g->const_copies.length () > 0)
750 int dest;
751 tree src;
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. */
765 static void
766 remove_gimple_phi_args (gimple phi)
768 use_operand_p arg_p;
769 ssa_op_iter iter;
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))
786 gimple stmt;
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. */
806 static void
807 eliminate_useless_phis (void)
809 basic_block bb;
810 gimple_stmt_iterator gsi;
811 tree result;
813 FOR_EACH_BB (bb)
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
822 size_t i;
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");
838 #endif
839 remove_phi_node (&gsi, true);
841 else
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);
849 else
850 gsi_next (&gsi);
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
861 variable. */
863 static void
864 rewrite_trees (var_map map ATTRIBUTE_UNUSED)
866 #ifdef ENABLE_CHECKING
867 basic_block bb;
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. */
871 FOR_EACH_BB (bb)
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));
878 if (T0 == NULL_TREE)
880 size_t i;
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");
898 #endif
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. */
906 void
907 expand_phi_nodes (struct ssaexpand *sa)
909 basic_block bb;
910 elim_graph g = new_elim_graph (sa->map->num_partitions);
911 g->map = sa->map;
913 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR, next_bb)
914 if (!gimple_seq_empty_p (phi_nodes (bb)))
916 edge e;
917 edge_iterator ei;
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;
936 basic_block bb;
937 e->insns.r = NULL_RTX;
938 bb = split_edge (e);
939 single_pred_edge (bb)->insns.r = insns;
941 else
942 ei_next (&ei);
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. */
954 static void
955 remove_ssa_form (bool perform_ter, struct ssaexpand *sa)
957 bitmap values = NULL;
958 var_map map;
959 unsigned i;
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);
973 if (perform_ter)
975 values = find_replaceable_exprs (map);
976 if (values && dump_file && (dump_flags & TDF_DETAILS))
977 dump_replaceable_exprs (dump_file, values);
980 rewrite_trees (map);
982 sa->map = map;
983 sa->values = 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. */
1001 static void
1002 maybe_renumber_stmts_bb (basic_block bb)
1004 unsigned i = 0;
1005 gimple_stmt_iterator gsi;
1007 if (!bb->aux)
1008 return;
1009 bb->aux = NULL;
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);
1014 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. */
1023 static bool
1024 trivially_conflicts_p (basic_block bb, tree result, tree arg)
1026 use_operand_p use;
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
1031 our little mind. */
1032 if (gimple_bb (defa) != bb)
1033 return false;
1035 FOR_EACH_IMM_USE_FAST (use, imm_iter, result)
1037 gimple use_stmt = USE_STMT (use);
1038 if (is_gimple_debug (use_stmt))
1039 continue;
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)
1043 return true;
1044 if (gimple_code (use_stmt) == GIMPLE_PHI)
1045 continue;
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)
1049 return true;
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))
1054 return true;
1057 return false;
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. */
1070 static void
1071 insert_backedge_copies (void)
1073 basic_block bb;
1074 gimple_stmt_iterator gsi;
1076 mark_dfs_back_edges ();
1078 FOR_EACH_BB (bb)
1080 /* Mark block as possibly needing calculation of UIDs. */
1081 bb->aux = &bb->aux;
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);
1087 size_t i;
1089 if (virtual_operand_p (result))
1090 continue;
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
1100 needed. */
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)))
1106 tree name;
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)
1128 continue;
1131 /* Create a new instance of the underlying variable of the
1132 PHI result. */
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
1143 the PHI node. */
1144 if (last && stmt_ends_bb_p (last))
1145 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
1146 else
1147 gsi_insert_after (&gsi2, stmt, GSI_NEW_STMT);
1148 SET_PHI_ARG_DEF (phi, i, name);
1153 /* Unmark this block again. */
1154 bb->aux = NULL;
1158 /* Free all memory associated with going out of SSA form. SA is
1159 the outof-SSA info object. */
1161 void
1162 finish_out_of_ssa (struct ssaexpand *sa)
1164 free (sa->partition_to_pseudo);
1165 if (sa->values)
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. */
1176 unsigned int
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);
1199 return 0;