2013-11-21 Edward Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / tree-outof-ssa.c
blob9a7a73f407994d713f15c71b690350a970ba45b6
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 "stor-layout.h"
27 #include "ggc.h"
28 #include "basic-block.h"
29 #include "gimple-pretty-print.h"
30 #include "bitmap.h"
31 #include "sbitmap.h"
32 #include "gimple.h"
33 #include "gimple-iterator.h"
34 #include "gimple-ssa.h"
35 #include "tree-cfg.h"
36 #include "tree-phinodes.h"
37 #include "ssa-iterators.h"
38 #include "stringpool.h"
39 #include "tree-ssanames.h"
40 #include "dumpfile.h"
41 #include "diagnostic-core.h"
42 #include "tree-ssa-live.h"
43 #include "tree-ssa-ter.h"
44 #include "tree-ssa-coalesce.h"
45 #include "tree-outof-ssa.h"
47 /* FIXME: A lot of code here deals with expanding to RTL. All that code
48 should be in cfgexpand.c. */
49 #include "expr.h"
51 /* Return TRUE if expression STMT is suitable for replacement. */
53 bool
54 ssa_is_replaceable_p (gimple stmt)
56 use_operand_p use_p;
57 tree def;
58 gimple use_stmt;
60 /* Only consider modify stmts. */
61 if (!is_gimple_assign (stmt))
62 return false;
64 /* If the statement may throw an exception, it cannot be replaced. */
65 if (stmt_could_throw_p (stmt))
66 return false;
68 /* Punt if there is more than 1 def. */
69 def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
70 if (!def)
71 return false;
73 /* Only consider definitions which have a single use. */
74 if (!single_imm_use (def, &use_p, &use_stmt))
75 return false;
77 /* Used in this block, but at the TOP of the block, not the end. */
78 if (gimple_code (use_stmt) == GIMPLE_PHI)
79 return false;
81 /* There must be no VDEFs. */
82 if (gimple_vdef (stmt))
83 return false;
85 /* Float expressions must go through memory if float-store is on. */
86 if (flag_float_store
87 && FLOAT_TYPE_P (gimple_expr_type (stmt)))
88 return false;
90 /* An assignment with a register variable on the RHS is not
91 replaceable. */
92 if (gimple_assign_rhs_code (stmt) == VAR_DECL
93 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt)))
94 return false;
96 /* No function calls can be replaced. */
97 if (is_gimple_call (stmt))
98 return false;
100 /* Leave any stmt with volatile operands alone as well. */
101 if (gimple_has_volatile_ops (stmt))
102 return false;
104 return true;
108 /* Used to hold all the components required to do SSA PHI elimination.
109 The node and pred/succ list is a simple linear list of nodes and
110 edges represented as pairs of nodes.
112 The predecessor and successor list: Nodes are entered in pairs, where
113 [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
114 predecessors, all the odd elements are successors.
116 Rationale:
117 When implemented as bitmaps, very large programs SSA->Normal times were
118 being dominated by clearing the interference graph.
120 Typically this list of edges is extremely small since it only includes
121 PHI results and uses from a single edge which have not coalesced with
122 each other. This means that no virtual PHI nodes are included, and
123 empirical evidence suggests that the number of edges rarely exceed
124 3, and in a bootstrap of GCC, the maximum size encountered was 7.
125 This also limits the number of possible nodes that are involved to
126 rarely more than 6, and in the bootstrap of gcc, the maximum number
127 of nodes encountered was 12. */
129 typedef struct _elim_graph {
130 /* Size of the elimination vectors. */
131 int size;
133 /* List of nodes in the elimination graph. */
134 vec<int> nodes;
136 /* The predecessor and successor edge list. */
137 vec<int> edge_list;
139 /* Source locus on each edge */
140 vec<source_location> edge_locus;
142 /* Visited vector. */
143 sbitmap visited;
145 /* Stack for visited nodes. */
146 vec<int> stack;
148 /* The variable partition map. */
149 var_map map;
151 /* Edge being eliminated by this graph. */
152 edge e;
154 /* List of constant copies to emit. These are pushed on in pairs. */
155 vec<int> const_dests;
156 vec<tree> const_copies;
158 /* Source locations for any constant copies. */
159 vec<source_location> copy_locus;
160 } *elim_graph;
163 /* For an edge E find out a good source location to associate with
164 instructions inserted on edge E. If E has an implicit goto set,
165 use its location. Otherwise search instructions in predecessors
166 of E for a location, and use that one. That makes sense because
167 we insert on edges for PHI nodes, and effects of PHIs happen on
168 the end of the predecessor conceptually. */
170 static void
171 set_location_for_edge (edge e)
173 if (e->goto_locus)
175 set_curr_insn_location (e->goto_locus);
177 else
179 basic_block bb = e->src;
180 gimple_stmt_iterator gsi;
184 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
186 gimple stmt = gsi_stmt (gsi);
187 if (is_gimple_debug (stmt))
188 continue;
189 if (gimple_has_location (stmt) || gimple_block (stmt))
191 set_curr_insn_location (gimple_location (stmt));
192 return;
195 /* Nothing found in this basic block. Make a half-assed attempt
196 to continue with another block. */
197 if (single_pred_p (bb))
198 bb = single_pred (bb);
199 else
200 bb = e->src;
202 while (bb != e->src);
206 /* Emit insns to copy SRC into DEST converting SRC if necessary. As
207 SRC/DEST might be BLKmode memory locations SIZEEXP is a tree from
208 which we deduce the size to copy in that case. */
210 static inline rtx
211 emit_partition_copy (rtx dest, rtx src, int unsignedsrcp, tree sizeexp)
213 rtx seq;
215 start_sequence ();
217 if (GET_MODE (src) != VOIDmode && GET_MODE (src) != GET_MODE (dest))
218 src = convert_to_mode (GET_MODE (dest), src, unsignedsrcp);
219 if (GET_MODE (src) == BLKmode)
221 gcc_assert (GET_MODE (dest) == BLKmode);
222 emit_block_move (dest, src, expr_size (sizeexp), BLOCK_OP_NORMAL);
224 else
225 emit_move_insn (dest, src);
227 seq = get_insns ();
228 end_sequence ();
230 return seq;
233 /* Insert a copy instruction from partition SRC to DEST onto edge E. */
235 static void
236 insert_partition_copy_on_edge (edge e, int dest, int src, source_location locus)
238 tree var;
239 rtx seq;
240 if (dump_file && (dump_flags & TDF_DETAILS))
242 fprintf (dump_file,
243 "Inserting a partition copy on edge BB%d->BB%d :"
244 "PART.%d = PART.%d",
245 e->src->index,
246 e->dest->index, dest, src);
247 fprintf (dump_file, "\n");
250 gcc_assert (SA.partition_to_pseudo[dest]);
251 gcc_assert (SA.partition_to_pseudo[src]);
253 set_location_for_edge (e);
254 /* If a locus is provided, override the default. */
255 if (locus)
256 set_curr_insn_location (locus);
258 var = partition_to_var (SA.map, src);
259 seq = emit_partition_copy (SA.partition_to_pseudo[dest],
260 SA.partition_to_pseudo[src],
261 TYPE_UNSIGNED (TREE_TYPE (var)),
262 var);
264 insert_insn_on_edge (seq, e);
267 /* Insert a copy instruction from expression SRC to partition DEST
268 onto edge E. */
270 static void
271 insert_value_copy_on_edge (edge e, int dest, tree src, source_location locus)
273 rtx seq, x;
274 enum machine_mode dest_mode, src_mode;
275 int unsignedp;
276 tree var;
278 if (dump_file && (dump_flags & TDF_DETAILS))
280 fprintf (dump_file,
281 "Inserting a value copy on edge BB%d->BB%d : PART.%d = ",
282 e->src->index,
283 e->dest->index, dest);
284 print_generic_expr (dump_file, src, TDF_SLIM);
285 fprintf (dump_file, "\n");
288 gcc_assert (SA.partition_to_pseudo[dest]);
290 set_location_for_edge (e);
291 /* If a locus is provided, override the default. */
292 if (locus)
293 set_curr_insn_location (locus);
295 start_sequence ();
297 var = SSA_NAME_VAR (partition_to_var (SA.map, dest));
298 src_mode = TYPE_MODE (TREE_TYPE (src));
299 dest_mode = GET_MODE (SA.partition_to_pseudo[dest]);
300 gcc_assert (src_mode == TYPE_MODE (TREE_TYPE (var)));
301 gcc_assert (!REG_P (SA.partition_to_pseudo[dest])
302 || dest_mode == promote_decl_mode (var, &unsignedp));
304 if (src_mode != dest_mode)
306 x = expand_expr (src, NULL, src_mode, EXPAND_NORMAL);
307 x = convert_modes (dest_mode, src_mode, x, unsignedp);
309 else if (src_mode == BLKmode)
311 x = SA.partition_to_pseudo[dest];
312 store_expr (src, x, 0, false);
314 else
315 x = expand_expr (src, SA.partition_to_pseudo[dest],
316 dest_mode, EXPAND_NORMAL);
318 if (x != SA.partition_to_pseudo[dest])
319 emit_move_insn (SA.partition_to_pseudo[dest], x);
320 seq = get_insns ();
321 end_sequence ();
323 insert_insn_on_edge (seq, e);
326 /* Insert a copy instruction from RTL expression SRC to partition DEST
327 onto edge E. */
329 static void
330 insert_rtx_to_part_on_edge (edge e, int dest, rtx src, int unsignedsrcp,
331 source_location locus)
333 rtx seq;
334 if (dump_file && (dump_flags & TDF_DETAILS))
336 fprintf (dump_file,
337 "Inserting a temp copy on edge BB%d->BB%d : PART.%d = ",
338 e->src->index,
339 e->dest->index, dest);
340 print_simple_rtl (dump_file, src);
341 fprintf (dump_file, "\n");
344 gcc_assert (SA.partition_to_pseudo[dest]);
346 set_location_for_edge (e);
347 /* If a locus is provided, override the default. */
348 if (locus)
349 set_curr_insn_location (locus);
351 /* We give the destination as sizeexp in case src/dest are BLKmode
352 mems. Usually we give the source. As we result from SSA names
353 the left and right size should be the same (and no WITH_SIZE_EXPR
354 involved), so it doesn't matter. */
355 seq = emit_partition_copy (SA.partition_to_pseudo[dest],
356 src, unsignedsrcp,
357 partition_to_var (SA.map, dest));
359 insert_insn_on_edge (seq, e);
362 /* Insert a copy instruction from partition SRC to RTL lvalue DEST
363 onto edge E. */
365 static void
366 insert_part_to_rtx_on_edge (edge e, rtx dest, int src, source_location locus)
368 tree var;
369 rtx seq;
370 if (dump_file && (dump_flags & TDF_DETAILS))
372 fprintf (dump_file,
373 "Inserting a temp copy on edge BB%d->BB%d : ",
374 e->src->index,
375 e->dest->index);
376 print_simple_rtl (dump_file, dest);
377 fprintf (dump_file, "= PART.%d\n", src);
380 gcc_assert (SA.partition_to_pseudo[src]);
382 set_location_for_edge (e);
383 /* If a locus is provided, override the default. */
384 if (locus)
385 set_curr_insn_location (locus);
387 var = partition_to_var (SA.map, src);
388 seq = emit_partition_copy (dest,
389 SA.partition_to_pseudo[src],
390 TYPE_UNSIGNED (TREE_TYPE (var)),
391 var);
393 insert_insn_on_edge (seq, e);
397 /* Create an elimination graph with SIZE nodes and associated data
398 structures. */
400 static elim_graph
401 new_elim_graph (int size)
403 elim_graph g = (elim_graph) xmalloc (sizeof (struct _elim_graph));
405 g->nodes.create (30);
406 g->const_dests.create (20);
407 g->const_copies.create (20);
408 g->copy_locus.create (10);
409 g->edge_list.create (20);
410 g->edge_locus.create (10);
411 g->stack.create (30);
413 g->visited = sbitmap_alloc (size);
415 return g;
419 /* Empty elimination graph G. */
421 static inline void
422 clear_elim_graph (elim_graph g)
424 g->nodes.truncate (0);
425 g->edge_list.truncate (0);
426 g->edge_locus.truncate (0);
430 /* Delete elimination graph G. */
432 static inline void
433 delete_elim_graph (elim_graph g)
435 sbitmap_free (g->visited);
436 g->stack.release ();
437 g->edge_list.release ();
438 g->const_copies.release ();
439 g->const_dests.release ();
440 g->nodes.release ();
441 g->copy_locus.release ();
442 g->edge_locus.release ();
444 free (g);
448 /* Return the number of nodes in graph G. */
450 static inline int
451 elim_graph_size (elim_graph g)
453 return g->nodes.length ();
457 /* Add NODE to graph G, if it doesn't exist already. */
459 static inline void
460 elim_graph_add_node (elim_graph g, int node)
462 int x;
463 int t;
465 FOR_EACH_VEC_ELT (g->nodes, x, t)
466 if (t == node)
467 return;
468 g->nodes.safe_push (node);
472 /* Add the edge PRED->SUCC to graph G. */
474 static inline void
475 elim_graph_add_edge (elim_graph g, int pred, int succ, source_location locus)
477 g->edge_list.safe_push (pred);
478 g->edge_list.safe_push (succ);
479 g->edge_locus.safe_push (locus);
483 /* Remove an edge from graph G for which NODE is the predecessor, and
484 return the successor node. -1 is returned if there is no such edge. */
486 static inline int
487 elim_graph_remove_succ_edge (elim_graph g, int node, source_location *locus)
489 int y;
490 unsigned x;
491 for (x = 0; x < g->edge_list.length (); x += 2)
492 if (g->edge_list[x] == node)
494 g->edge_list[x] = -1;
495 y = g->edge_list[x + 1];
496 g->edge_list[x + 1] = -1;
497 *locus = g->edge_locus[x / 2];
498 g->edge_locus[x / 2] = UNKNOWN_LOCATION;
499 return y;
501 *locus = UNKNOWN_LOCATION;
502 return -1;
506 /* Find all the nodes in GRAPH which are successors to NODE in the
507 edge list. VAR will hold the partition number found. CODE is the
508 code fragment executed for every node found. */
510 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, LOCUS, CODE) \
511 do { \
512 unsigned x_; \
513 int y_; \
514 for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
516 y_ = (GRAPH)->edge_list[x_]; \
517 if (y_ != (NODE)) \
518 continue; \
519 (void) ((VAR) = (GRAPH)->edge_list[x_ + 1]); \
520 (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
521 CODE; \
523 } while (0)
526 /* Find all the nodes which are predecessors of NODE in the edge list for
527 GRAPH. VAR will hold the partition number found. CODE is the
528 code fragment executed for every node found. */
530 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, LOCUS, CODE) \
531 do { \
532 unsigned x_; \
533 int y_; \
534 for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
536 y_ = (GRAPH)->edge_list[x_ + 1]; \
537 if (y_ != (NODE)) \
538 continue; \
539 (void) ((VAR) = (GRAPH)->edge_list[x_]); \
540 (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
541 CODE; \
543 } while (0)
546 /* Add T to elimination graph G. */
548 static inline void
549 eliminate_name (elim_graph g, int T)
551 elim_graph_add_node (g, T);
554 /* Return true if this phi argument T should have a copy queued when using
555 var_map MAP. PHI nodes should contain only ssa_names and invariants. A
556 test for ssa_name is definitely simpler, but don't let invalid contents
557 slip through in the meantime. */
559 static inline bool
560 queue_phi_copy_p (var_map map, tree t)
562 if (TREE_CODE (t) == SSA_NAME)
564 if (var_to_partition (map, t) == NO_PARTITION)
565 return true;
566 return false;
568 gcc_checking_assert (is_gimple_min_invariant (t));
569 return true;
572 /* Build elimination graph G for basic block BB on incoming PHI edge
573 G->e. */
575 static void
576 eliminate_build (elim_graph g)
578 tree Ti;
579 int p0, pi;
580 gimple_stmt_iterator gsi;
582 clear_elim_graph (g);
584 for (gsi = gsi_start_phis (g->e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
586 gimple phi = gsi_stmt (gsi);
587 source_location locus;
589 p0 = var_to_partition (g->map, gimple_phi_result (phi));
590 /* Ignore results which are not in partitions. */
591 if (p0 == NO_PARTITION)
592 continue;
594 Ti = PHI_ARG_DEF (phi, g->e->dest_idx);
595 locus = gimple_phi_arg_location_from_edge (phi, g->e);
597 /* If this argument is a constant, or a SSA_NAME which is being
598 left in SSA form, just queue a copy to be emitted on this
599 edge. */
600 if (queue_phi_copy_p (g->map, Ti))
602 /* Save constant copies until all other copies have been emitted
603 on this edge. */
604 g->const_dests.safe_push (p0);
605 g->const_copies.safe_push (Ti);
606 g->copy_locus.safe_push (locus);
608 else
610 pi = var_to_partition (g->map, Ti);
611 if (p0 != pi)
613 eliminate_name (g, p0);
614 eliminate_name (g, pi);
615 elim_graph_add_edge (g, p0, pi, locus);
622 /* Push successors of T onto the elimination stack for G. */
624 static void
625 elim_forward (elim_graph g, int T)
627 int S;
628 source_location locus;
630 bitmap_set_bit (g->visited, T);
631 FOR_EACH_ELIM_GRAPH_SUCC (g, T, S, locus,
633 if (!bitmap_bit_p (g->visited, S))
634 elim_forward (g, S);
636 g->stack.safe_push (T);
640 /* Return 1 if there unvisited predecessors of T in graph G. */
642 static int
643 elim_unvisited_predecessor (elim_graph g, int T)
645 int P;
646 source_location locus;
648 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
650 if (!bitmap_bit_p (g->visited, P))
651 return 1;
653 return 0;
656 /* Process predecessors first, and insert a copy. */
658 static void
659 elim_backward (elim_graph g, int T)
661 int P;
662 source_location locus;
664 bitmap_set_bit (g->visited, T);
665 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
667 if (!bitmap_bit_p (g->visited, P))
669 elim_backward (g, P);
670 insert_partition_copy_on_edge (g->e, P, T, locus);
675 /* Allocate a new pseudo register usable for storing values sitting
676 in NAME (a decl or SSA name), i.e. with matching mode and attributes. */
678 static rtx
679 get_temp_reg (tree name)
681 tree var = TREE_CODE (name) == SSA_NAME ? SSA_NAME_VAR (name) : name;
682 tree type = TREE_TYPE (var);
683 int unsignedp;
684 enum machine_mode reg_mode = promote_decl_mode (var, &unsignedp);
685 rtx x = gen_reg_rtx (reg_mode);
686 if (POINTER_TYPE_P (type))
687 mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (var))));
688 return x;
691 /* Insert required copies for T in graph G. Check for a strongly connected
692 region, and create a temporary to break the cycle if one is found. */
694 static void
695 elim_create (elim_graph g, int T)
697 int P, S;
698 source_location locus;
700 if (elim_unvisited_predecessor (g, T))
702 tree var = partition_to_var (g->map, T);
703 rtx U = get_temp_reg (var);
704 int unsignedsrcp = TYPE_UNSIGNED (TREE_TYPE (var));
706 insert_part_to_rtx_on_edge (g->e, U, T, UNKNOWN_LOCATION);
707 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
709 if (!bitmap_bit_p (g->visited, P))
711 elim_backward (g, P);
712 insert_rtx_to_part_on_edge (g->e, P, U, unsignedsrcp, locus);
716 else
718 S = elim_graph_remove_succ_edge (g, T, &locus);
719 if (S != -1)
721 bitmap_set_bit (g->visited, T);
722 insert_partition_copy_on_edge (g->e, T, S, locus);
728 /* Eliminate all the phi nodes on edge E in graph G. */
730 static void
731 eliminate_phi (edge e, elim_graph g)
733 int x;
735 gcc_assert (g->const_copies.length () == 0);
736 gcc_assert (g->copy_locus.length () == 0);
738 /* Abnormal edges already have everything coalesced. */
739 if (e->flags & EDGE_ABNORMAL)
740 return;
742 g->e = e;
744 eliminate_build (g);
746 if (elim_graph_size (g) != 0)
748 int part;
750 bitmap_clear (g->visited);
751 g->stack.truncate (0);
753 FOR_EACH_VEC_ELT (g->nodes, x, part)
755 if (!bitmap_bit_p (g->visited, part))
756 elim_forward (g, part);
759 bitmap_clear (g->visited);
760 while (g->stack.length () > 0)
762 x = g->stack.pop ();
763 if (!bitmap_bit_p (g->visited, x))
764 elim_create (g, x);
768 /* If there are any pending constant copies, issue them now. */
769 while (g->const_copies.length () > 0)
771 int dest;
772 tree src;
773 source_location locus;
775 src = g->const_copies.pop ();
776 dest = g->const_dests.pop ();
777 locus = g->copy_locus.pop ();
778 insert_value_copy_on_edge (e, dest, src, locus);
783 /* Remove each argument from PHI. If an arg was the last use of an SSA_NAME,
784 check to see if this allows another PHI node to be removed. */
786 static void
787 remove_gimple_phi_args (gimple phi)
789 use_operand_p arg_p;
790 ssa_op_iter iter;
792 if (dump_file && (dump_flags & TDF_DETAILS))
794 fprintf (dump_file, "Removing Dead PHI definition: ");
795 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
798 FOR_EACH_PHI_ARG (arg_p, phi, iter, SSA_OP_USE)
800 tree arg = USE_FROM_PTR (arg_p);
801 if (TREE_CODE (arg) == SSA_NAME)
803 /* Remove the reference to the existing argument. */
804 SET_USE (arg_p, NULL_TREE);
805 if (has_zero_uses (arg))
807 gimple stmt;
808 gimple_stmt_iterator gsi;
810 stmt = SSA_NAME_DEF_STMT (arg);
812 /* Also remove the def if it is a PHI node. */
813 if (gimple_code (stmt) == GIMPLE_PHI)
815 remove_gimple_phi_args (stmt);
816 gsi = gsi_for_stmt (stmt);
817 remove_phi_node (&gsi, true);
825 /* Remove any PHI node which is a virtual PHI, or a PHI with no uses. */
827 static void
828 eliminate_useless_phis (void)
830 basic_block bb;
831 gimple_stmt_iterator gsi;
832 tree result;
834 FOR_EACH_BB (bb)
836 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
838 gimple phi = gsi_stmt (gsi);
839 result = gimple_phi_result (phi);
840 if (virtual_operand_p (result))
842 #ifdef ENABLE_CHECKING
843 size_t i;
844 /* There should be no arguments which are not virtual, or the
845 results will be incorrect. */
846 for (i = 0; i < gimple_phi_num_args (phi); i++)
848 tree arg = PHI_ARG_DEF (phi, i);
849 if (TREE_CODE (arg) == SSA_NAME
850 && !virtual_operand_p (arg))
852 fprintf (stderr, "Argument of PHI is not virtual (");
853 print_generic_expr (stderr, arg, TDF_SLIM);
854 fprintf (stderr, "), but the result is :");
855 print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
856 internal_error ("SSA corruption");
859 #endif
860 remove_phi_node (&gsi, true);
862 else
864 /* Also remove real PHIs with no uses. */
865 if (has_zero_uses (result))
867 remove_gimple_phi_args (phi);
868 remove_phi_node (&gsi, true);
870 else
871 gsi_next (&gsi);
878 /* This function will rewrite the current program using the variable mapping
879 found in MAP. If the replacement vector VALUES is provided, any
880 occurrences of partitions with non-null entries in the vector will be
881 replaced with the expression in the vector instead of its mapped
882 variable. */
884 static void
885 rewrite_trees (var_map map ATTRIBUTE_UNUSED)
887 #ifdef ENABLE_CHECKING
888 basic_block bb;
889 /* Search for PHIs where the destination has no partition, but one
890 or more arguments has a partition. This should not happen and can
891 create incorrect code. */
892 FOR_EACH_BB (bb)
894 gimple_stmt_iterator gsi;
895 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
897 gimple phi = gsi_stmt (gsi);
898 tree T0 = var_to_partition_to_var (map, gimple_phi_result (phi));
899 if (T0 == NULL_TREE)
901 size_t i;
902 for (i = 0; i < gimple_phi_num_args (phi); i++)
904 tree arg = PHI_ARG_DEF (phi, i);
906 if (TREE_CODE (arg) == SSA_NAME
907 && var_to_partition (map, arg) != NO_PARTITION)
909 fprintf (stderr, "Argument of PHI is in a partition :(");
910 print_generic_expr (stderr, arg, TDF_SLIM);
911 fprintf (stderr, "), but the result is not :");
912 print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
913 internal_error ("SSA corruption");
919 #endif
922 /* Given the out-of-ssa info object SA (with prepared partitions)
923 eliminate all phi nodes in all basic blocks. Afterwards no
924 basic block will have phi nodes anymore and there are possibly
925 some RTL instructions inserted on edges. */
927 void
928 expand_phi_nodes (struct ssaexpand *sa)
930 basic_block bb;
931 elim_graph g = new_elim_graph (sa->map->num_partitions);
932 g->map = sa->map;
934 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb,
935 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
936 if (!gimple_seq_empty_p (phi_nodes (bb)))
938 edge e;
939 edge_iterator ei;
940 FOR_EACH_EDGE (e, ei, bb->preds)
941 eliminate_phi (e, g);
942 set_phi_nodes (bb, NULL);
943 /* We can't redirect EH edges in RTL land, so we need to do this
944 here. Redirection happens only when splitting is necessary,
945 which it is only for critical edges, normally. For EH edges
946 it might also be necessary when the successor has more than
947 one predecessor. In that case the edge is either required to
948 be fallthru (which EH edges aren't), or the predecessor needs
949 to end with a jump (which again, isn't the case with EH edges).
950 Hence, split all EH edges on which we inserted instructions
951 and whose successor has multiple predecessors. */
952 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
954 if (e->insns.r && (e->flags & EDGE_EH)
955 && !single_pred_p (e->dest))
957 rtx insns = e->insns.r;
958 basic_block bb;
959 e->insns.r = NULL_RTX;
960 bb = split_edge (e);
961 single_pred_edge (bb)->insns.r = insns;
963 else
964 ei_next (&ei);
968 delete_elim_graph (g);
972 /* Remove the ssa-names in the current function and translate them into normal
973 compiler variables. PERFORM_TER is true if Temporary Expression Replacement
974 should also be used. */
976 static void
977 remove_ssa_form (bool perform_ter, struct ssaexpand *sa)
979 bitmap values = NULL;
980 var_map map;
981 unsigned i;
983 map = coalesce_ssa_name ();
985 /* Return to viewing the variable list as just all reference variables after
986 coalescing has been performed. */
987 partition_view_normal (map, false);
989 if (dump_file && (dump_flags & TDF_DETAILS))
991 fprintf (dump_file, "After Coalescing:\n");
992 dump_var_map (dump_file, map);
995 if (perform_ter)
997 values = find_replaceable_exprs (map);
998 if (values && dump_file && (dump_flags & TDF_DETAILS))
999 dump_replaceable_exprs (dump_file, values);
1002 rewrite_trees (map);
1004 sa->map = map;
1005 sa->values = values;
1006 sa->partition_has_default_def = BITMAP_ALLOC (NULL);
1007 for (i = 1; i < num_ssa_names; i++)
1009 tree t = ssa_name (i);
1010 if (t && SSA_NAME_IS_DEFAULT_DEF (t))
1012 int p = var_to_partition (map, t);
1013 if (p != NO_PARTITION)
1014 bitmap_set_bit (sa->partition_has_default_def, p);
1020 /* If not already done so for basic block BB, assign increasing uids
1021 to each of its instructions. */
1023 static void
1024 maybe_renumber_stmts_bb (basic_block bb)
1026 unsigned i = 0;
1027 gimple_stmt_iterator gsi;
1029 if (!bb->aux)
1030 return;
1031 bb->aux = NULL;
1032 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1034 gimple stmt = gsi_stmt (gsi);
1035 gimple_set_uid (stmt, i);
1036 i++;
1041 /* Return true if we can determine that the SSA_NAMEs RESULT (a result
1042 of a PHI node) and ARG (one of its arguments) conflict. Return false
1043 otherwise, also when we simply aren't sure. */
1045 static bool
1046 trivially_conflicts_p (basic_block bb, tree result, tree arg)
1048 use_operand_p use;
1049 imm_use_iterator imm_iter;
1050 gimple defa = SSA_NAME_DEF_STMT (arg);
1052 /* If ARG isn't defined in the same block it's too complicated for
1053 our little mind. */
1054 if (gimple_bb (defa) != bb)
1055 return false;
1057 FOR_EACH_IMM_USE_FAST (use, imm_iter, result)
1059 gimple use_stmt = USE_STMT (use);
1060 if (is_gimple_debug (use_stmt))
1061 continue;
1062 /* Now, if there's a use of RESULT that lies outside this basic block,
1063 then there surely is a conflict with ARG. */
1064 if (gimple_bb (use_stmt) != bb)
1065 return true;
1066 if (gimple_code (use_stmt) == GIMPLE_PHI)
1067 continue;
1068 /* The use now is in a real stmt of BB, so if ARG was defined
1069 in a PHI node (like RESULT) both conflict. */
1070 if (gimple_code (defa) == GIMPLE_PHI)
1071 return true;
1072 maybe_renumber_stmts_bb (bb);
1073 /* If the use of RESULT occurs after the definition of ARG,
1074 the two conflict too. */
1075 if (gimple_uid (defa) < gimple_uid (use_stmt))
1076 return true;
1079 return false;
1083 /* Search every PHI node for arguments associated with backedges which
1084 we can trivially determine will need a copy (the argument is either
1085 not an SSA_NAME or the argument has a different underlying variable
1086 than the PHI result).
1088 Insert a copy from the PHI argument to a new destination at the
1089 end of the block with the backedge to the top of the loop. Update
1090 the PHI argument to reference this new destination. */
1092 static void
1093 insert_backedge_copies (void)
1095 basic_block bb;
1096 gimple_stmt_iterator gsi;
1098 mark_dfs_back_edges ();
1100 FOR_EACH_BB (bb)
1102 /* Mark block as possibly needing calculation of UIDs. */
1103 bb->aux = &bb->aux;
1105 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1107 gimple phi = gsi_stmt (gsi);
1108 tree result = gimple_phi_result (phi);
1109 size_t i;
1111 if (virtual_operand_p (result))
1112 continue;
1114 for (i = 0; i < gimple_phi_num_args (phi); i++)
1116 tree arg = gimple_phi_arg_def (phi, i);
1117 edge e = gimple_phi_arg_edge (phi, i);
1119 /* If the argument is not an SSA_NAME, then we will need a
1120 constant initialization. If the argument is an SSA_NAME with
1121 a different underlying variable then a copy statement will be
1122 needed. */
1123 if ((e->flags & EDGE_DFS_BACK)
1124 && (TREE_CODE (arg) != SSA_NAME
1125 || SSA_NAME_VAR (arg) != SSA_NAME_VAR (result)
1126 || trivially_conflicts_p (bb, result, arg)))
1128 tree name;
1129 gimple stmt, last = NULL;
1130 gimple_stmt_iterator gsi2;
1132 gsi2 = gsi_last_bb (gimple_phi_arg_edge (phi, i)->src);
1133 if (!gsi_end_p (gsi2))
1134 last = gsi_stmt (gsi2);
1136 /* In theory the only way we ought to get back to the
1137 start of a loop should be with a COND_EXPR or GOTO_EXPR.
1138 However, better safe than sorry.
1139 If the block ends with a control statement or
1140 something that might throw, then we have to
1141 insert this assignment before the last
1142 statement. Else insert it after the last statement. */
1143 if (last && stmt_ends_bb_p (last))
1145 /* If the last statement in the block is the definition
1146 site of the PHI argument, then we can't insert
1147 anything after it. */
1148 if (TREE_CODE (arg) == SSA_NAME
1149 && SSA_NAME_DEF_STMT (arg) == last)
1150 continue;
1153 /* Create a new instance of the underlying variable of the
1154 PHI result. */
1155 name = copy_ssa_name (result, NULL);
1156 stmt = gimple_build_assign (name,
1157 gimple_phi_arg_def (phi, i));
1159 /* copy location if present. */
1160 if (gimple_phi_arg_has_location (phi, i))
1161 gimple_set_location (stmt,
1162 gimple_phi_arg_location (phi, i));
1164 /* Insert the new statement into the block and update
1165 the PHI node. */
1166 if (last && stmt_ends_bb_p (last))
1167 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
1168 else
1169 gsi_insert_after (&gsi2, stmt, GSI_NEW_STMT);
1170 SET_PHI_ARG_DEF (phi, i, name);
1175 /* Unmark this block again. */
1176 bb->aux = NULL;
1180 /* Free all memory associated with going out of SSA form. SA is
1181 the outof-SSA info object. */
1183 void
1184 finish_out_of_ssa (struct ssaexpand *sa)
1186 free (sa->partition_to_pseudo);
1187 if (sa->values)
1188 BITMAP_FREE (sa->values);
1189 delete_var_map (sa->map);
1190 BITMAP_FREE (sa->partition_has_default_def);
1191 memset (sa, 0, sizeof *sa);
1194 /* Take the current function out of SSA form, translating PHIs as described in
1195 R. Morgan, ``Building an Optimizing Compiler'',
1196 Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
1198 unsigned int
1199 rewrite_out_of_ssa (struct ssaexpand *sa)
1201 /* If elimination of a PHI requires inserting a copy on a backedge,
1202 then we will have to split the backedge which has numerous
1203 undesirable performance effects.
1205 A significant number of such cases can be handled here by inserting
1206 copies into the loop itself. */
1207 insert_backedge_copies ();
1210 /* Eliminate PHIs which are of no use, such as virtual or dead phis. */
1211 eliminate_useless_phis ();
1213 if (dump_file && (dump_flags & TDF_DETAILS))
1214 gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1216 remove_ssa_form (flag_tree_ter, sa);
1218 if (dump_file && (dump_flags & TDF_DETAILS))
1219 gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1221 return 0;