gcc/
[official-gcc.git] / gcc / tree-outof-ssa.c
blob42c90d49a1807d65de26aad43f8a7510e05c67b7
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 "tree-ssa.h"
31 #include "dumpfile.h"
32 #include "diagnostic-core.h"
33 #include "tree-outof-ssa.h"
35 /* FIXME: A lot of code here deals with expanding to RTL. All that code
36 should be in cfgexpand.c. */
37 #include "expr.h"
39 /* Return TRUE if expression STMT is suitable for replacement. */
41 bool
42 ssa_is_replaceable_p (gimple stmt)
44 use_operand_p use_p;
45 tree def;
46 gimple use_stmt;
48 /* Only consider modify stmts. */
49 if (!is_gimple_assign (stmt))
50 return false;
52 /* If the statement may throw an exception, it cannot be replaced. */
53 if (stmt_could_throw_p (stmt))
54 return false;
56 /* Punt if there is more than 1 def. */
57 def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
58 if (!def)
59 return false;
61 /* Only consider definitions which have a single use. */
62 if (!single_imm_use (def, &use_p, &use_stmt))
63 return false;
65 /* Used in this block, but at the TOP of the block, not the end. */
66 if (gimple_code (use_stmt) == GIMPLE_PHI)
67 return false;
69 /* There must be no VDEFs. */
70 if (gimple_vdef (stmt))
71 return false;
73 /* Float expressions must go through memory if float-store is on. */
74 if (flag_float_store
75 && FLOAT_TYPE_P (gimple_expr_type (stmt)))
76 return false;
78 /* An assignment with a register variable on the RHS is not
79 replaceable. */
80 if (gimple_assign_rhs_code (stmt) == VAR_DECL
81 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt)))
82 return false;
84 /* No function calls can be replaced. */
85 if (is_gimple_call (stmt))
86 return false;
88 /* Leave any stmt with volatile operands alone as well. */
89 if (gimple_has_volatile_ops (stmt))
90 return false;
92 return true;
96 /* Used to hold all the components required to do SSA PHI elimination.
97 The node and pred/succ list is a simple linear list of nodes and
98 edges represented as pairs of nodes.
100 The predecessor and successor list: Nodes are entered in pairs, where
101 [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
102 predecessors, all the odd elements are successors.
104 Rationale:
105 When implemented as bitmaps, very large programs SSA->Normal times were
106 being dominated by clearing the interference graph.
108 Typically this list of edges is extremely small since it only includes
109 PHI results and uses from a single edge which have not coalesced with
110 each other. This means that no virtual PHI nodes are included, and
111 empirical evidence suggests that the number of edges rarely exceed
112 3, and in a bootstrap of GCC, the maximum size encountered was 7.
113 This also limits the number of possible nodes that are involved to
114 rarely more than 6, and in the bootstrap of gcc, the maximum number
115 of nodes encountered was 12. */
117 typedef struct _elim_graph {
118 /* Size of the elimination vectors. */
119 int size;
121 /* List of nodes in the elimination graph. */
122 vec<int> nodes;
124 /* The predecessor and successor edge list. */
125 vec<int> edge_list;
127 /* Source locus on each edge */
128 vec<source_location> edge_locus;
130 /* Visited vector. */
131 sbitmap visited;
133 /* Stack for visited nodes. */
134 vec<int> stack;
136 /* The variable partition map. */
137 var_map map;
139 /* Edge being eliminated by this graph. */
140 edge e;
142 /* List of constant copies to emit. These are pushed on in pairs. */
143 vec<int> const_dests;
144 vec<tree> const_copies;
146 /* Source locations for any constant copies. */
147 vec<source_location> copy_locus;
148 } *elim_graph;
151 /* For an edge E find out a good source location to associate with
152 instructions inserted on edge E. If E has an implicit goto set,
153 use its location. Otherwise search instructions in predecessors
154 of E for a location, and use that one. That makes sense because
155 we insert on edges for PHI nodes, and effects of PHIs happen on
156 the end of the predecessor conceptually. */
158 static void
159 set_location_for_edge (edge e)
161 if (e->goto_locus)
163 set_curr_insn_location (e->goto_locus);
165 else
167 basic_block bb = e->src;
168 gimple_stmt_iterator gsi;
172 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
174 gimple stmt = gsi_stmt (gsi);
175 if (is_gimple_debug (stmt))
176 continue;
177 if (gimple_has_location (stmt) || gimple_block (stmt))
179 set_curr_insn_location (gimple_location (stmt));
180 return;
183 /* Nothing found in this basic block. Make a half-assed attempt
184 to continue with another block. */
185 if (single_pred_p (bb))
186 bb = single_pred (bb);
187 else
188 bb = e->src;
190 while (bb != e->src);
194 /* Emit insns to copy SRC into DEST converting SRC if necessary. As
195 SRC/DEST might be BLKmode memory locations SIZEEXP is a tree from
196 which we deduce the size to copy in that case. */
198 static inline rtx
199 emit_partition_copy (rtx dest, rtx src, int unsignedsrcp, tree sizeexp)
201 rtx seq;
203 start_sequence ();
205 if (GET_MODE (src) != VOIDmode && GET_MODE (src) != GET_MODE (dest))
206 src = convert_to_mode (GET_MODE (dest), src, unsignedsrcp);
207 if (GET_MODE (src) == BLKmode)
209 gcc_assert (GET_MODE (dest) == BLKmode);
210 emit_block_move (dest, src, expr_size (sizeexp), BLOCK_OP_NORMAL);
212 else
213 emit_move_insn (dest, src);
215 seq = get_insns ();
216 end_sequence ();
218 return seq;
221 /* Insert a copy instruction from partition SRC to DEST onto edge E. */
223 static void
224 insert_partition_copy_on_edge (edge e, int dest, int src, source_location locus)
226 tree var;
227 rtx seq;
228 if (dump_file && (dump_flags & TDF_DETAILS))
230 fprintf (dump_file,
231 "Inserting a partition copy on edge BB%d->BB%d :"
232 "PART.%d = PART.%d",
233 e->src->index,
234 e->dest->index, dest, src);
235 fprintf (dump_file, "\n");
238 gcc_assert (SA.partition_to_pseudo[dest]);
239 gcc_assert (SA.partition_to_pseudo[src]);
241 set_location_for_edge (e);
242 /* If a locus is provided, override the default. */
243 if (locus)
244 set_curr_insn_location (locus);
246 var = partition_to_var (SA.map, src);
247 seq = emit_partition_copy (SA.partition_to_pseudo[dest],
248 SA.partition_to_pseudo[src],
249 TYPE_UNSIGNED (TREE_TYPE (var)),
250 var);
252 insert_insn_on_edge (seq, e);
255 /* Insert a copy instruction from expression SRC to partition DEST
256 onto edge E. */
258 static void
259 insert_value_copy_on_edge (edge e, int dest, tree src, source_location locus)
261 rtx seq, x;
262 enum machine_mode dest_mode, src_mode;
263 int unsignedp;
264 tree var;
266 if (dump_file && (dump_flags & TDF_DETAILS))
268 fprintf (dump_file,
269 "Inserting a value copy on edge BB%d->BB%d : PART.%d = ",
270 e->src->index,
271 e->dest->index, dest);
272 print_generic_expr (dump_file, src, TDF_SLIM);
273 fprintf (dump_file, "\n");
276 gcc_assert (SA.partition_to_pseudo[dest]);
278 set_location_for_edge (e);
279 /* If a locus is provided, override the default. */
280 if (locus)
281 set_curr_insn_location (locus);
283 start_sequence ();
285 var = SSA_NAME_VAR (partition_to_var (SA.map, dest));
286 src_mode = TYPE_MODE (TREE_TYPE (src));
287 dest_mode = GET_MODE (SA.partition_to_pseudo[dest]);
288 gcc_assert (src_mode == TYPE_MODE (TREE_TYPE (var)));
289 gcc_assert (!REG_P (SA.partition_to_pseudo[dest])
290 || dest_mode == promote_decl_mode (var, &unsignedp));
292 if (src_mode != dest_mode)
294 x = expand_expr (src, NULL, src_mode, EXPAND_NORMAL);
295 x = convert_modes (dest_mode, src_mode, x, unsignedp);
297 else if (src_mode == BLKmode)
299 x = SA.partition_to_pseudo[dest];
300 store_expr (src, x, 0, false);
302 else
303 x = expand_expr (src, SA.partition_to_pseudo[dest],
304 dest_mode, EXPAND_NORMAL);
306 if (x != SA.partition_to_pseudo[dest])
307 emit_move_insn (SA.partition_to_pseudo[dest], x);
308 seq = get_insns ();
309 end_sequence ();
311 insert_insn_on_edge (seq, e);
314 /* Insert a copy instruction from RTL expression SRC to partition DEST
315 onto edge E. */
317 static void
318 insert_rtx_to_part_on_edge (edge e, int dest, rtx src, int unsignedsrcp,
319 source_location locus)
321 rtx seq;
322 if (dump_file && (dump_flags & TDF_DETAILS))
324 fprintf (dump_file,
325 "Inserting a temp copy on edge BB%d->BB%d : PART.%d = ",
326 e->src->index,
327 e->dest->index, dest);
328 print_simple_rtl (dump_file, src);
329 fprintf (dump_file, "\n");
332 gcc_assert (SA.partition_to_pseudo[dest]);
334 set_location_for_edge (e);
335 /* If a locus is provided, override the default. */
336 if (locus)
337 set_curr_insn_location (locus);
339 /* We give the destination as sizeexp in case src/dest are BLKmode
340 mems. Usually we give the source. As we result from SSA names
341 the left and right size should be the same (and no WITH_SIZE_EXPR
342 involved), so it doesn't matter. */
343 seq = emit_partition_copy (SA.partition_to_pseudo[dest],
344 src, unsignedsrcp,
345 partition_to_var (SA.map, dest));
347 insert_insn_on_edge (seq, e);
350 /* Insert a copy instruction from partition SRC to RTL lvalue DEST
351 onto edge E. */
353 static void
354 insert_part_to_rtx_on_edge (edge e, rtx dest, int src, source_location locus)
356 tree var;
357 rtx seq;
358 if (dump_file && (dump_flags & TDF_DETAILS))
360 fprintf (dump_file,
361 "Inserting a temp copy on edge BB%d->BB%d : ",
362 e->src->index,
363 e->dest->index);
364 print_simple_rtl (dump_file, dest);
365 fprintf (dump_file, "= PART.%d\n", src);
368 gcc_assert (SA.partition_to_pseudo[src]);
370 set_location_for_edge (e);
371 /* If a locus is provided, override the default. */
372 if (locus)
373 set_curr_insn_location (locus);
375 var = partition_to_var (SA.map, src);
376 seq = emit_partition_copy (dest,
377 SA.partition_to_pseudo[src],
378 TYPE_UNSIGNED (TREE_TYPE (var)),
379 var);
381 insert_insn_on_edge (seq, e);
385 /* Create an elimination graph with SIZE nodes and associated data
386 structures. */
388 static elim_graph
389 new_elim_graph (int size)
391 elim_graph g = (elim_graph) xmalloc (sizeof (struct _elim_graph));
393 g->nodes.create (30);
394 g->const_dests.create (20);
395 g->const_copies.create (20);
396 g->copy_locus.create (10);
397 g->edge_list.create (20);
398 g->edge_locus.create (10);
399 g->stack.create (30);
401 g->visited = sbitmap_alloc (size);
403 return g;
407 /* Empty elimination graph G. */
409 static inline void
410 clear_elim_graph (elim_graph g)
412 g->nodes.truncate (0);
413 g->edge_list.truncate (0);
414 g->edge_locus.truncate (0);
418 /* Delete elimination graph G. */
420 static inline void
421 delete_elim_graph (elim_graph g)
423 sbitmap_free (g->visited);
424 g->stack.release ();
425 g->edge_list.release ();
426 g->const_copies.release ();
427 g->const_dests.release ();
428 g->nodes.release ();
429 g->copy_locus.release ();
430 g->edge_locus.release ();
432 free (g);
436 /* Return the number of nodes in graph G. */
438 static inline int
439 elim_graph_size (elim_graph g)
441 return g->nodes.length ();
445 /* Add NODE to graph G, if it doesn't exist already. */
447 static inline void
448 elim_graph_add_node (elim_graph g, int node)
450 int x;
451 int t;
453 FOR_EACH_VEC_ELT (g->nodes, x, t)
454 if (t == node)
455 return;
456 g->nodes.safe_push (node);
460 /* Add the edge PRED->SUCC to graph G. */
462 static inline void
463 elim_graph_add_edge (elim_graph g, int pred, int succ, source_location locus)
465 g->edge_list.safe_push (pred);
466 g->edge_list.safe_push (succ);
467 g->edge_locus.safe_push (locus);
471 /* Remove an edge from graph G for which NODE is the predecessor, and
472 return the successor node. -1 is returned if there is no such edge. */
474 static inline int
475 elim_graph_remove_succ_edge (elim_graph g, int node, source_location *locus)
477 int y;
478 unsigned x;
479 for (x = 0; x < g->edge_list.length (); x += 2)
480 if (g->edge_list[x] == node)
482 g->edge_list[x] = -1;
483 y = g->edge_list[x + 1];
484 g->edge_list[x + 1] = -1;
485 *locus = g->edge_locus[x / 2];
486 g->edge_locus[x / 2] = UNKNOWN_LOCATION;
487 return y;
489 *locus = UNKNOWN_LOCATION;
490 return -1;
494 /* Find all the nodes in GRAPH which are successors to NODE in the
495 edge list. VAR will hold the partition number found. CODE is the
496 code fragment executed for every node found. */
498 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, LOCUS, CODE) \
499 do { \
500 unsigned x_; \
501 int y_; \
502 for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
504 y_ = (GRAPH)->edge_list[x_]; \
505 if (y_ != (NODE)) \
506 continue; \
507 (void) ((VAR) = (GRAPH)->edge_list[x_ + 1]); \
508 (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
509 CODE; \
511 } while (0)
514 /* Find all the nodes which are predecessors of NODE in the edge list for
515 GRAPH. VAR will hold the partition number found. CODE is the
516 code fragment executed for every node found. */
518 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, LOCUS, CODE) \
519 do { \
520 unsigned x_; \
521 int y_; \
522 for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
524 y_ = (GRAPH)->edge_list[x_ + 1]; \
525 if (y_ != (NODE)) \
526 continue; \
527 (void) ((VAR) = (GRAPH)->edge_list[x_]); \
528 (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
529 CODE; \
531 } while (0)
534 /* Add T to elimination graph G. */
536 static inline void
537 eliminate_name (elim_graph g, int T)
539 elim_graph_add_node (g, T);
543 /* Build elimination graph G for basic block BB on incoming PHI edge
544 G->e. */
546 static void
547 eliminate_build (elim_graph g)
549 tree Ti;
550 int p0, pi;
551 gimple_stmt_iterator gsi;
553 clear_elim_graph (g);
555 for (gsi = gsi_start_phis (g->e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
557 gimple phi = gsi_stmt (gsi);
558 source_location locus;
560 p0 = var_to_partition (g->map, gimple_phi_result (phi));
561 /* Ignore results which are not in partitions. */
562 if (p0 == NO_PARTITION)
563 continue;
565 Ti = PHI_ARG_DEF (phi, g->e->dest_idx);
566 locus = gimple_phi_arg_location_from_edge (phi, g->e);
568 /* If this argument is a constant, or a SSA_NAME which is being
569 left in SSA form, just queue a copy to be emitted on this
570 edge. */
571 if (!phi_ssa_name_p (Ti)
572 || (TREE_CODE (Ti) == SSA_NAME
573 && var_to_partition (g->map, Ti) == NO_PARTITION))
575 /* Save constant copies until all other copies have been emitted
576 on this edge. */
577 g->const_dests.safe_push (p0);
578 g->const_copies.safe_push (Ti);
579 g->copy_locus.safe_push (locus);
581 else
583 pi = var_to_partition (g->map, Ti);
584 if (p0 != pi)
586 eliminate_name (g, p0);
587 eliminate_name (g, pi);
588 elim_graph_add_edge (g, p0, pi, locus);
595 /* Push successors of T onto the elimination stack for G. */
597 static void
598 elim_forward (elim_graph g, int T)
600 int S;
601 source_location locus;
603 bitmap_set_bit (g->visited, T);
604 FOR_EACH_ELIM_GRAPH_SUCC (g, T, S, locus,
606 if (!bitmap_bit_p (g->visited, S))
607 elim_forward (g, S);
609 g->stack.safe_push (T);
613 /* Return 1 if there unvisited predecessors of T in graph G. */
615 static int
616 elim_unvisited_predecessor (elim_graph g, int T)
618 int P;
619 source_location locus;
621 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
623 if (!bitmap_bit_p (g->visited, P))
624 return 1;
626 return 0;
629 /* Process predecessors first, and insert a copy. */
631 static void
632 elim_backward (elim_graph g, int T)
634 int P;
635 source_location locus;
637 bitmap_set_bit (g->visited, T);
638 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
640 if (!bitmap_bit_p (g->visited, P))
642 elim_backward (g, P);
643 insert_partition_copy_on_edge (g->e, P, T, locus);
648 /* Allocate a new pseudo register usable for storing values sitting
649 in NAME (a decl or SSA name), i.e. with matching mode and attributes. */
651 static rtx
652 get_temp_reg (tree name)
654 tree var = TREE_CODE (name) == SSA_NAME ? SSA_NAME_VAR (name) : name;
655 tree type = TREE_TYPE (var);
656 int unsignedp;
657 enum machine_mode reg_mode = promote_decl_mode (var, &unsignedp);
658 rtx x = gen_reg_rtx (reg_mode);
659 if (POINTER_TYPE_P (type))
660 mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (var))));
661 return x;
664 /* Insert required copies for T in graph G. Check for a strongly connected
665 region, and create a temporary to break the cycle if one is found. */
667 static void
668 elim_create (elim_graph g, int T)
670 int P, S;
671 source_location locus;
673 if (elim_unvisited_predecessor (g, T))
675 tree var = partition_to_var (g->map, T);
676 rtx U = get_temp_reg (var);
677 int unsignedsrcp = TYPE_UNSIGNED (TREE_TYPE (var));
679 insert_part_to_rtx_on_edge (g->e, U, T, UNKNOWN_LOCATION);
680 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
682 if (!bitmap_bit_p (g->visited, P))
684 elim_backward (g, P);
685 insert_rtx_to_part_on_edge (g->e, P, U, unsignedsrcp, locus);
689 else
691 S = elim_graph_remove_succ_edge (g, T, &locus);
692 if (S != -1)
694 bitmap_set_bit (g->visited, T);
695 insert_partition_copy_on_edge (g->e, T, S, locus);
701 /* Eliminate all the phi nodes on edge E in graph G. */
703 static void
704 eliminate_phi (edge e, elim_graph g)
706 int x;
708 gcc_assert (g->const_copies.length () == 0);
709 gcc_assert (g->copy_locus.length () == 0);
711 /* Abnormal edges already have everything coalesced. */
712 if (e->flags & EDGE_ABNORMAL)
713 return;
715 g->e = e;
717 eliminate_build (g);
719 if (elim_graph_size (g) != 0)
721 int part;
723 bitmap_clear (g->visited);
724 g->stack.truncate (0);
726 FOR_EACH_VEC_ELT (g->nodes, x, part)
728 if (!bitmap_bit_p (g->visited, part))
729 elim_forward (g, part);
732 bitmap_clear (g->visited);
733 while (g->stack.length () > 0)
735 x = g->stack.pop ();
736 if (!bitmap_bit_p (g->visited, x))
737 elim_create (g, x);
741 /* If there are any pending constant copies, issue them now. */
742 while (g->const_copies.length () > 0)
744 int dest;
745 tree src;
746 source_location locus;
748 src = g->const_copies.pop ();
749 dest = g->const_dests.pop ();
750 locus = g->copy_locus.pop ();
751 insert_value_copy_on_edge (e, dest, src, locus);
756 /* Remove each argument from PHI. If an arg was the last use of an SSA_NAME,
757 check to see if this allows another PHI node to be removed. */
759 static void
760 remove_gimple_phi_args (gimple phi)
762 use_operand_p arg_p;
763 ssa_op_iter iter;
765 if (dump_file && (dump_flags & TDF_DETAILS))
767 fprintf (dump_file, "Removing Dead PHI definition: ");
768 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
771 FOR_EACH_PHI_ARG (arg_p, phi, iter, SSA_OP_USE)
773 tree arg = USE_FROM_PTR (arg_p);
774 if (TREE_CODE (arg) == SSA_NAME)
776 /* Remove the reference to the existing argument. */
777 SET_USE (arg_p, NULL_TREE);
778 if (has_zero_uses (arg))
780 gimple stmt;
781 gimple_stmt_iterator gsi;
783 stmt = SSA_NAME_DEF_STMT (arg);
785 /* Also remove the def if it is a PHI node. */
786 if (gimple_code (stmt) == GIMPLE_PHI)
788 remove_gimple_phi_args (stmt);
789 gsi = gsi_for_stmt (stmt);
790 remove_phi_node (&gsi, true);
798 /* Remove any PHI node which is a virtual PHI, or a PHI with no uses. */
800 static void
801 eliminate_useless_phis (void)
803 basic_block bb;
804 gimple_stmt_iterator gsi;
805 tree result;
807 FOR_EACH_BB (bb)
809 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
811 gimple phi = gsi_stmt (gsi);
812 result = gimple_phi_result (phi);
813 if (virtual_operand_p (result))
815 #ifdef ENABLE_CHECKING
816 size_t i;
817 /* There should be no arguments which are not virtual, or the
818 results will be incorrect. */
819 for (i = 0; i < gimple_phi_num_args (phi); i++)
821 tree arg = PHI_ARG_DEF (phi, i);
822 if (TREE_CODE (arg) == SSA_NAME
823 && !virtual_operand_p (arg))
825 fprintf (stderr, "Argument of PHI is not virtual (");
826 print_generic_expr (stderr, arg, TDF_SLIM);
827 fprintf (stderr, "), but the result is :");
828 print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
829 internal_error ("SSA corruption");
832 #endif
833 remove_phi_node (&gsi, true);
835 else
837 /* Also remove real PHIs with no uses. */
838 if (has_zero_uses (result))
840 remove_gimple_phi_args (phi);
841 remove_phi_node (&gsi, true);
843 else
844 gsi_next (&gsi);
851 /* This function will rewrite the current program using the variable mapping
852 found in MAP. If the replacement vector VALUES is provided, any
853 occurrences of partitions with non-null entries in the vector will be
854 replaced with the expression in the vector instead of its mapped
855 variable. */
857 static void
858 rewrite_trees (var_map map ATTRIBUTE_UNUSED)
860 #ifdef ENABLE_CHECKING
861 basic_block bb;
862 /* Search for PHIs where the destination has no partition, but one
863 or more arguments has a partition. This should not happen and can
864 create incorrect code. */
865 FOR_EACH_BB (bb)
867 gimple_stmt_iterator gsi;
868 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
870 gimple phi = gsi_stmt (gsi);
871 tree T0 = var_to_partition_to_var (map, gimple_phi_result (phi));
872 if (T0 == NULL_TREE)
874 size_t i;
875 for (i = 0; i < gimple_phi_num_args (phi); i++)
877 tree arg = PHI_ARG_DEF (phi, i);
879 if (TREE_CODE (arg) == SSA_NAME
880 && var_to_partition (map, arg) != NO_PARTITION)
882 fprintf (stderr, "Argument of PHI is in a partition :(");
883 print_generic_expr (stderr, arg, TDF_SLIM);
884 fprintf (stderr, "), but the result is not :");
885 print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
886 internal_error ("SSA corruption");
892 #endif
895 /* Given the out-of-ssa info object SA (with prepared partitions)
896 eliminate all phi nodes in all basic blocks. Afterwards no
897 basic block will have phi nodes anymore and there are possibly
898 some RTL instructions inserted on edges. */
900 void
901 expand_phi_nodes (struct ssaexpand *sa)
903 basic_block bb;
904 elim_graph g = new_elim_graph (sa->map->num_partitions);
905 g->map = sa->map;
907 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR, next_bb)
908 if (!gimple_seq_empty_p (phi_nodes (bb)))
910 edge e;
911 edge_iterator ei;
912 FOR_EACH_EDGE (e, ei, bb->preds)
913 eliminate_phi (e, g);
914 set_phi_nodes (bb, NULL);
915 /* We can't redirect EH edges in RTL land, so we need to do this
916 here. Redirection happens only when splitting is necessary,
917 which it is only for critical edges, normally. For EH edges
918 it might also be necessary when the successor has more than
919 one predecessor. In that case the edge is either required to
920 be fallthru (which EH edges aren't), or the predecessor needs
921 to end with a jump (which again, isn't the case with EH edges).
922 Hence, split all EH edges on which we inserted instructions
923 and whose successor has multiple predecessors. */
924 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
926 if (e->insns.r && (e->flags & EDGE_EH)
927 && !single_pred_p (e->dest))
929 rtx insns = e->insns.r;
930 basic_block bb;
931 e->insns.r = NULL_RTX;
932 bb = split_edge (e);
933 single_pred_edge (bb)->insns.r = insns;
935 else
936 ei_next (&ei);
940 delete_elim_graph (g);
944 /* Remove the ssa-names in the current function and translate them into normal
945 compiler variables. PERFORM_TER is true if Temporary Expression Replacement
946 should also be used. */
948 static void
949 remove_ssa_form (bool perform_ter, struct ssaexpand *sa)
951 bitmap values = NULL;
952 var_map map;
953 unsigned i;
955 map = coalesce_ssa_name ();
957 /* Return to viewing the variable list as just all reference variables after
958 coalescing has been performed. */
959 partition_view_normal (map, false);
961 if (dump_file && (dump_flags & TDF_DETAILS))
963 fprintf (dump_file, "After Coalescing:\n");
964 dump_var_map (dump_file, map);
967 if (perform_ter)
969 values = find_replaceable_exprs (map);
970 if (values && dump_file && (dump_flags & TDF_DETAILS))
971 dump_replaceable_exprs (dump_file, values);
974 rewrite_trees (map);
976 sa->map = map;
977 sa->values = values;
978 sa->partition_has_default_def = BITMAP_ALLOC (NULL);
979 for (i = 1; i < num_ssa_names; i++)
981 tree t = ssa_name (i);
982 if (t && SSA_NAME_IS_DEFAULT_DEF (t))
984 int p = var_to_partition (map, t);
985 if (p != NO_PARTITION)
986 bitmap_set_bit (sa->partition_has_default_def, p);
992 /* If not already done so for basic block BB, assign increasing uids
993 to each of its instructions. */
995 static void
996 maybe_renumber_stmts_bb (basic_block bb)
998 unsigned i = 0;
999 gimple_stmt_iterator gsi;
1001 if (!bb->aux)
1002 return;
1003 bb->aux = NULL;
1004 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1006 gimple stmt = gsi_stmt (gsi);
1007 gimple_set_uid (stmt, i);
1008 i++;
1013 /* Return true if we can determine that the SSA_NAMEs RESULT (a result
1014 of a PHI node) and ARG (one of its arguments) conflict. Return false
1015 otherwise, also when we simply aren't sure. */
1017 static bool
1018 trivially_conflicts_p (basic_block bb, tree result, tree arg)
1020 use_operand_p use;
1021 imm_use_iterator imm_iter;
1022 gimple defa = SSA_NAME_DEF_STMT (arg);
1024 /* If ARG isn't defined in the same block it's too complicated for
1025 our little mind. */
1026 if (gimple_bb (defa) != bb)
1027 return false;
1029 FOR_EACH_IMM_USE_FAST (use, imm_iter, result)
1031 gimple use_stmt = USE_STMT (use);
1032 if (is_gimple_debug (use_stmt))
1033 continue;
1034 /* Now, if there's a use of RESULT that lies outside this basic block,
1035 then there surely is a conflict with ARG. */
1036 if (gimple_bb (use_stmt) != bb)
1037 return true;
1038 if (gimple_code (use_stmt) == GIMPLE_PHI)
1039 continue;
1040 /* The use now is in a real stmt of BB, so if ARG was defined
1041 in a PHI node (like RESULT) both conflict. */
1042 if (gimple_code (defa) == GIMPLE_PHI)
1043 return true;
1044 maybe_renumber_stmts_bb (bb);
1045 /* If the use of RESULT occurs after the definition of ARG,
1046 the two conflict too. */
1047 if (gimple_uid (defa) < gimple_uid (use_stmt))
1048 return true;
1051 return false;
1055 /* Search every PHI node for arguments associated with backedges which
1056 we can trivially determine will need a copy (the argument is either
1057 not an SSA_NAME or the argument has a different underlying variable
1058 than the PHI result).
1060 Insert a copy from the PHI argument to a new destination at the
1061 end of the block with the backedge to the top of the loop. Update
1062 the PHI argument to reference this new destination. */
1064 static void
1065 insert_backedge_copies (void)
1067 basic_block bb;
1068 gimple_stmt_iterator gsi;
1070 mark_dfs_back_edges ();
1072 FOR_EACH_BB (bb)
1074 /* Mark block as possibly needing calculation of UIDs. */
1075 bb->aux = &bb->aux;
1077 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1079 gimple phi = gsi_stmt (gsi);
1080 tree result = gimple_phi_result (phi);
1081 size_t i;
1083 if (virtual_operand_p (result))
1084 continue;
1086 for (i = 0; i < gimple_phi_num_args (phi); i++)
1088 tree arg = gimple_phi_arg_def (phi, i);
1089 edge e = gimple_phi_arg_edge (phi, i);
1091 /* If the argument is not an SSA_NAME, then we will need a
1092 constant initialization. If the argument is an SSA_NAME with
1093 a different underlying variable then a copy statement will be
1094 needed. */
1095 if ((e->flags & EDGE_DFS_BACK)
1096 && (TREE_CODE (arg) != SSA_NAME
1097 || SSA_NAME_VAR (arg) != SSA_NAME_VAR (result)
1098 || trivially_conflicts_p (bb, result, arg)))
1100 tree name;
1101 gimple stmt, last = NULL;
1102 gimple_stmt_iterator gsi2;
1104 gsi2 = gsi_last_bb (gimple_phi_arg_edge (phi, i)->src);
1105 if (!gsi_end_p (gsi2))
1106 last = gsi_stmt (gsi2);
1108 /* In theory the only way we ought to get back to the
1109 start of a loop should be with a COND_EXPR or GOTO_EXPR.
1110 However, better safe than sorry.
1111 If the block ends with a control statement or
1112 something that might throw, then we have to
1113 insert this assignment before the last
1114 statement. Else insert it after the last statement. */
1115 if (last && stmt_ends_bb_p (last))
1117 /* If the last statement in the block is the definition
1118 site of the PHI argument, then we can't insert
1119 anything after it. */
1120 if (TREE_CODE (arg) == SSA_NAME
1121 && SSA_NAME_DEF_STMT (arg) == last)
1122 continue;
1125 /* Create a new instance of the underlying variable of the
1126 PHI result. */
1127 name = copy_ssa_name (result, NULL);
1128 stmt = gimple_build_assign (name,
1129 gimple_phi_arg_def (phi, i));
1131 /* copy location if present. */
1132 if (gimple_phi_arg_has_location (phi, i))
1133 gimple_set_location (stmt,
1134 gimple_phi_arg_location (phi, i));
1136 /* Insert the new statement into the block and update
1137 the PHI node. */
1138 if (last && stmt_ends_bb_p (last))
1139 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
1140 else
1141 gsi_insert_after (&gsi2, stmt, GSI_NEW_STMT);
1142 SET_PHI_ARG_DEF (phi, i, name);
1147 /* Unmark this block again. */
1148 bb->aux = NULL;
1152 /* Free all memory associated with going out of SSA form. SA is
1153 the outof-SSA info object. */
1155 void
1156 finish_out_of_ssa (struct ssaexpand *sa)
1158 free (sa->partition_to_pseudo);
1159 if (sa->values)
1160 BITMAP_FREE (sa->values);
1161 delete_var_map (sa->map);
1162 BITMAP_FREE (sa->partition_has_default_def);
1163 memset (sa, 0, sizeof *sa);
1166 /* Take the current function out of SSA form, translating PHIs as described in
1167 R. Morgan, ``Building an Optimizing Compiler'',
1168 Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
1170 unsigned int
1171 rewrite_out_of_ssa (struct ssaexpand *sa)
1173 /* If elimination of a PHI requires inserting a copy on a backedge,
1174 then we will have to split the backedge which has numerous
1175 undesirable performance effects.
1177 A significant number of such cases can be handled here by inserting
1178 copies into the loop itself. */
1179 insert_backedge_copies ();
1182 /* Eliminate PHIs which are of no use, such as virtual or dead phis. */
1183 eliminate_useless_phis ();
1185 if (dump_file && (dump_flags & TDF_DETAILS))
1186 gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1188 remove_ssa_form (flag_tree_ter, sa);
1190 if (dump_file && (dump_flags & TDF_DETAILS))
1191 gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1193 return 0;