Daily bump.
[official-gcc.git] / gcc / tree-outof-ssa.c
blob1e982857e14122e55751ddb8dd569ecc05268602
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-ssa-live.h"
40 #include "tree-ssa-ter.h"
41 #include "tree-ssa-coalesce.h"
42 #include "tree-outof-ssa.h"
44 /* FIXME: A lot of code here deals with expanding to RTL. All that code
45 should be in cfgexpand.c. */
46 #include "expr.h"
48 /* Return TRUE if expression STMT is suitable for replacement. */
50 bool
51 ssa_is_replaceable_p (gimple stmt)
53 use_operand_p use_p;
54 tree def;
55 gimple use_stmt;
57 /* Only consider modify stmts. */
58 if (!is_gimple_assign (stmt))
59 return false;
61 /* If the statement may throw an exception, it cannot be replaced. */
62 if (stmt_could_throw_p (stmt))
63 return false;
65 /* Punt if there is more than 1 def. */
66 def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
67 if (!def)
68 return false;
70 /* Only consider definitions which have a single use. */
71 if (!single_imm_use (def, &use_p, &use_stmt))
72 return false;
74 /* Used in this block, but at the TOP of the block, not the end. */
75 if (gimple_code (use_stmt) == GIMPLE_PHI)
76 return false;
78 /* There must be no VDEFs. */
79 if (gimple_vdef (stmt))
80 return false;
82 /* Float expressions must go through memory if float-store is on. */
83 if (flag_float_store
84 && FLOAT_TYPE_P (gimple_expr_type (stmt)))
85 return false;
87 /* An assignment with a register variable on the RHS is not
88 replaceable. */
89 if (gimple_assign_rhs_code (stmt) == VAR_DECL
90 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt)))
91 return false;
93 /* No function calls can be replaced. */
94 if (is_gimple_call (stmt))
95 return false;
97 /* Leave any stmt with volatile operands alone as well. */
98 if (gimple_has_volatile_ops (stmt))
99 return false;
101 return true;
105 /* Used to hold all the components required to do SSA PHI elimination.
106 The node and pred/succ list is a simple linear list of nodes and
107 edges represented as pairs of nodes.
109 The predecessor and successor list: Nodes are entered in pairs, where
110 [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
111 predecessors, all the odd elements are successors.
113 Rationale:
114 When implemented as bitmaps, very large programs SSA->Normal times were
115 being dominated by clearing the interference graph.
117 Typically this list of edges is extremely small since it only includes
118 PHI results and uses from a single edge which have not coalesced with
119 each other. This means that no virtual PHI nodes are included, and
120 empirical evidence suggests that the number of edges rarely exceed
121 3, and in a bootstrap of GCC, the maximum size encountered was 7.
122 This also limits the number of possible nodes that are involved to
123 rarely more than 6, and in the bootstrap of gcc, the maximum number
124 of nodes encountered was 12. */
126 typedef struct _elim_graph {
127 /* Size of the elimination vectors. */
128 int size;
130 /* List of nodes in the elimination graph. */
131 vec<int> nodes;
133 /* The predecessor and successor edge list. */
134 vec<int> edge_list;
136 /* Source locus on each edge */
137 vec<source_location> edge_locus;
139 /* Visited vector. */
140 sbitmap visited;
142 /* Stack for visited nodes. */
143 vec<int> stack;
145 /* The variable partition map. */
146 var_map map;
148 /* Edge being eliminated by this graph. */
149 edge e;
151 /* List of constant copies to emit. These are pushed on in pairs. */
152 vec<int> const_dests;
153 vec<tree> const_copies;
155 /* Source locations for any constant copies. */
156 vec<source_location> copy_locus;
157 } *elim_graph;
160 /* For an edge E find out a good source location to associate with
161 instructions inserted on edge E. If E has an implicit goto set,
162 use its location. Otherwise search instructions in predecessors
163 of E for a location, and use that one. That makes sense because
164 we insert on edges for PHI nodes, and effects of PHIs happen on
165 the end of the predecessor conceptually. */
167 static void
168 set_location_for_edge (edge e)
170 if (e->goto_locus)
172 set_curr_insn_location (e->goto_locus);
174 else
176 basic_block bb = e->src;
177 gimple_stmt_iterator gsi;
181 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
183 gimple stmt = gsi_stmt (gsi);
184 if (is_gimple_debug (stmt))
185 continue;
186 if (gimple_has_location (stmt) || gimple_block (stmt))
188 set_curr_insn_location (gimple_location (stmt));
189 return;
192 /* Nothing found in this basic block. Make a half-assed attempt
193 to continue with another block. */
194 if (single_pred_p (bb))
195 bb = single_pred (bb);
196 else
197 bb = e->src;
199 while (bb != e->src);
203 /* Emit insns to copy SRC into DEST converting SRC if necessary. As
204 SRC/DEST might be BLKmode memory locations SIZEEXP is a tree from
205 which we deduce the size to copy in that case. */
207 static inline rtx
208 emit_partition_copy (rtx dest, rtx src, int unsignedsrcp, tree sizeexp)
210 rtx seq;
212 start_sequence ();
214 if (GET_MODE (src) != VOIDmode && GET_MODE (src) != GET_MODE (dest))
215 src = convert_to_mode (GET_MODE (dest), src, unsignedsrcp);
216 if (GET_MODE (src) == BLKmode)
218 gcc_assert (GET_MODE (dest) == BLKmode);
219 emit_block_move (dest, src, expr_size (sizeexp), BLOCK_OP_NORMAL);
221 else
222 emit_move_insn (dest, src);
224 seq = get_insns ();
225 end_sequence ();
227 return seq;
230 /* Insert a copy instruction from partition SRC to DEST onto edge E. */
232 static void
233 insert_partition_copy_on_edge (edge e, int dest, int src, source_location locus)
235 tree var;
236 rtx seq;
237 if (dump_file && (dump_flags & TDF_DETAILS))
239 fprintf (dump_file,
240 "Inserting a partition copy on edge BB%d->BB%d :"
241 "PART.%d = PART.%d",
242 e->src->index,
243 e->dest->index, dest, src);
244 fprintf (dump_file, "\n");
247 gcc_assert (SA.partition_to_pseudo[dest]);
248 gcc_assert (SA.partition_to_pseudo[src]);
250 set_location_for_edge (e);
251 /* If a locus is provided, override the default. */
252 if (locus)
253 set_curr_insn_location (locus);
255 var = partition_to_var (SA.map, src);
256 seq = emit_partition_copy (SA.partition_to_pseudo[dest],
257 SA.partition_to_pseudo[src],
258 TYPE_UNSIGNED (TREE_TYPE (var)),
259 var);
261 insert_insn_on_edge (seq, e);
264 /* Insert a copy instruction from expression SRC to partition DEST
265 onto edge E. */
267 static void
268 insert_value_copy_on_edge (edge e, int dest, tree src, source_location locus)
270 rtx seq, x;
271 enum machine_mode dest_mode, src_mode;
272 int unsignedp;
273 tree var;
275 if (dump_file && (dump_flags & TDF_DETAILS))
277 fprintf (dump_file,
278 "Inserting a value copy on edge BB%d->BB%d : PART.%d = ",
279 e->src->index,
280 e->dest->index, dest);
281 print_generic_expr (dump_file, src, TDF_SLIM);
282 fprintf (dump_file, "\n");
285 gcc_assert (SA.partition_to_pseudo[dest]);
287 set_location_for_edge (e);
288 /* If a locus is provided, override the default. */
289 if (locus)
290 set_curr_insn_location (locus);
292 start_sequence ();
294 var = SSA_NAME_VAR (partition_to_var (SA.map, dest));
295 src_mode = TYPE_MODE (TREE_TYPE (src));
296 dest_mode = GET_MODE (SA.partition_to_pseudo[dest]);
297 gcc_assert (src_mode == TYPE_MODE (TREE_TYPE (var)));
298 gcc_assert (!REG_P (SA.partition_to_pseudo[dest])
299 || dest_mode == promote_decl_mode (var, &unsignedp));
301 if (src_mode != dest_mode)
303 x = expand_expr (src, NULL, src_mode, EXPAND_NORMAL);
304 x = convert_modes (dest_mode, src_mode, x, unsignedp);
306 else if (src_mode == BLKmode)
308 x = SA.partition_to_pseudo[dest];
309 store_expr (src, x, 0, false);
311 else
312 x = expand_expr (src, SA.partition_to_pseudo[dest],
313 dest_mode, EXPAND_NORMAL);
315 if (x != SA.partition_to_pseudo[dest])
316 emit_move_insn (SA.partition_to_pseudo[dest], x);
317 seq = get_insns ();
318 end_sequence ();
320 insert_insn_on_edge (seq, e);
323 /* Insert a copy instruction from RTL expression SRC to partition DEST
324 onto edge E. */
326 static void
327 insert_rtx_to_part_on_edge (edge e, int dest, rtx src, int unsignedsrcp,
328 source_location locus)
330 rtx seq;
331 if (dump_file && (dump_flags & TDF_DETAILS))
333 fprintf (dump_file,
334 "Inserting a temp copy on edge BB%d->BB%d : PART.%d = ",
335 e->src->index,
336 e->dest->index, dest);
337 print_simple_rtl (dump_file, src);
338 fprintf (dump_file, "\n");
341 gcc_assert (SA.partition_to_pseudo[dest]);
343 set_location_for_edge (e);
344 /* If a locus is provided, override the default. */
345 if (locus)
346 set_curr_insn_location (locus);
348 /* We give the destination as sizeexp in case src/dest are BLKmode
349 mems. Usually we give the source. As we result from SSA names
350 the left and right size should be the same (and no WITH_SIZE_EXPR
351 involved), so it doesn't matter. */
352 seq = emit_partition_copy (SA.partition_to_pseudo[dest],
353 src, unsignedsrcp,
354 partition_to_var (SA.map, dest));
356 insert_insn_on_edge (seq, e);
359 /* Insert a copy instruction from partition SRC to RTL lvalue DEST
360 onto edge E. */
362 static void
363 insert_part_to_rtx_on_edge (edge e, rtx dest, int src, source_location locus)
365 tree var;
366 rtx seq;
367 if (dump_file && (dump_flags & TDF_DETAILS))
369 fprintf (dump_file,
370 "Inserting a temp copy on edge BB%d->BB%d : ",
371 e->src->index,
372 e->dest->index);
373 print_simple_rtl (dump_file, dest);
374 fprintf (dump_file, "= PART.%d\n", src);
377 gcc_assert (SA.partition_to_pseudo[src]);
379 set_location_for_edge (e);
380 /* If a locus is provided, override the default. */
381 if (locus)
382 set_curr_insn_location (locus);
384 var = partition_to_var (SA.map, src);
385 seq = emit_partition_copy (dest,
386 SA.partition_to_pseudo[src],
387 TYPE_UNSIGNED (TREE_TYPE (var)),
388 var);
390 insert_insn_on_edge (seq, e);
394 /* Create an elimination graph with SIZE nodes and associated data
395 structures. */
397 static elim_graph
398 new_elim_graph (int size)
400 elim_graph g = (elim_graph) xmalloc (sizeof (struct _elim_graph));
402 g->nodes.create (30);
403 g->const_dests.create (20);
404 g->const_copies.create (20);
405 g->copy_locus.create (10);
406 g->edge_list.create (20);
407 g->edge_locus.create (10);
408 g->stack.create (30);
410 g->visited = sbitmap_alloc (size);
412 return g;
416 /* Empty elimination graph G. */
418 static inline void
419 clear_elim_graph (elim_graph g)
421 g->nodes.truncate (0);
422 g->edge_list.truncate (0);
423 g->edge_locus.truncate (0);
427 /* Delete elimination graph G. */
429 static inline void
430 delete_elim_graph (elim_graph g)
432 sbitmap_free (g->visited);
433 g->stack.release ();
434 g->edge_list.release ();
435 g->const_copies.release ();
436 g->const_dests.release ();
437 g->nodes.release ();
438 g->copy_locus.release ();
439 g->edge_locus.release ();
441 free (g);
445 /* Return the number of nodes in graph G. */
447 static inline int
448 elim_graph_size (elim_graph g)
450 return g->nodes.length ();
454 /* Add NODE to graph G, if it doesn't exist already. */
456 static inline void
457 elim_graph_add_node (elim_graph g, int node)
459 int x;
460 int t;
462 FOR_EACH_VEC_ELT (g->nodes, x, t)
463 if (t == node)
464 return;
465 g->nodes.safe_push (node);
469 /* Add the edge PRED->SUCC to graph G. */
471 static inline void
472 elim_graph_add_edge (elim_graph g, int pred, int succ, source_location locus)
474 g->edge_list.safe_push (pred);
475 g->edge_list.safe_push (succ);
476 g->edge_locus.safe_push (locus);
480 /* Remove an edge from graph G for which NODE is the predecessor, and
481 return the successor node. -1 is returned if there is no such edge. */
483 static inline int
484 elim_graph_remove_succ_edge (elim_graph g, int node, source_location *locus)
486 int y;
487 unsigned x;
488 for (x = 0; x < g->edge_list.length (); x += 2)
489 if (g->edge_list[x] == node)
491 g->edge_list[x] = -1;
492 y = g->edge_list[x + 1];
493 g->edge_list[x + 1] = -1;
494 *locus = g->edge_locus[x / 2];
495 g->edge_locus[x / 2] = UNKNOWN_LOCATION;
496 return y;
498 *locus = UNKNOWN_LOCATION;
499 return -1;
503 /* Find all the nodes in GRAPH which are successors to NODE in the
504 edge list. VAR will hold the partition number found. CODE is the
505 code fragment executed for every node found. */
507 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, LOCUS, CODE) \
508 do { \
509 unsigned x_; \
510 int y_; \
511 for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
513 y_ = (GRAPH)->edge_list[x_]; \
514 if (y_ != (NODE)) \
515 continue; \
516 (void) ((VAR) = (GRAPH)->edge_list[x_ + 1]); \
517 (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
518 CODE; \
520 } while (0)
523 /* Find all the nodes which are predecessors of NODE in the edge list for
524 GRAPH. VAR will hold the partition number found. CODE is the
525 code fragment executed for every node found. */
527 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, LOCUS, CODE) \
528 do { \
529 unsigned x_; \
530 int y_; \
531 for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
533 y_ = (GRAPH)->edge_list[x_ + 1]; \
534 if (y_ != (NODE)) \
535 continue; \
536 (void) ((VAR) = (GRAPH)->edge_list[x_]); \
537 (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
538 CODE; \
540 } while (0)
543 /* Add T to elimination graph G. */
545 static inline void
546 eliminate_name (elim_graph g, int T)
548 elim_graph_add_node (g, T);
552 /* Build elimination graph G for basic block BB on incoming PHI edge
553 G->e. */
555 static void
556 eliminate_build (elim_graph g)
558 tree Ti;
559 int p0, pi;
560 gimple_stmt_iterator gsi;
562 clear_elim_graph (g);
564 for (gsi = gsi_start_phis (g->e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
566 gimple phi = gsi_stmt (gsi);
567 source_location locus;
569 p0 = var_to_partition (g->map, gimple_phi_result (phi));
570 /* Ignore results which are not in partitions. */
571 if (p0 == NO_PARTITION)
572 continue;
574 Ti = PHI_ARG_DEF (phi, g->e->dest_idx);
575 locus = gimple_phi_arg_location_from_edge (phi, g->e);
577 /* If this argument is a constant, or a SSA_NAME which is being
578 left in SSA form, just queue a copy to be emitted on this
579 edge. */
580 if (!phi_ssa_name_p (Ti)
581 || (TREE_CODE (Ti) == SSA_NAME
582 && var_to_partition (g->map, Ti) == NO_PARTITION))
584 /* Save constant copies until all other copies have been emitted
585 on this edge. */
586 g->const_dests.safe_push (p0);
587 g->const_copies.safe_push (Ti);
588 g->copy_locus.safe_push (locus);
590 else
592 pi = var_to_partition (g->map, Ti);
593 if (p0 != pi)
595 eliminate_name (g, p0);
596 eliminate_name (g, pi);
597 elim_graph_add_edge (g, p0, pi, locus);
604 /* Push successors of T onto the elimination stack for G. */
606 static void
607 elim_forward (elim_graph g, int T)
609 int S;
610 source_location locus;
612 bitmap_set_bit (g->visited, T);
613 FOR_EACH_ELIM_GRAPH_SUCC (g, T, S, locus,
615 if (!bitmap_bit_p (g->visited, S))
616 elim_forward (g, S);
618 g->stack.safe_push (T);
622 /* Return 1 if there unvisited predecessors of T in graph G. */
624 static int
625 elim_unvisited_predecessor (elim_graph g, int T)
627 int P;
628 source_location locus;
630 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
632 if (!bitmap_bit_p (g->visited, P))
633 return 1;
635 return 0;
638 /* Process predecessors first, and insert a copy. */
640 static void
641 elim_backward (elim_graph g, int T)
643 int P;
644 source_location locus;
646 bitmap_set_bit (g->visited, T);
647 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
649 if (!bitmap_bit_p (g->visited, P))
651 elim_backward (g, P);
652 insert_partition_copy_on_edge (g->e, P, T, locus);
657 /* Allocate a new pseudo register usable for storing values sitting
658 in NAME (a decl or SSA name), i.e. with matching mode and attributes. */
660 static rtx
661 get_temp_reg (tree name)
663 tree var = TREE_CODE (name) == SSA_NAME ? SSA_NAME_VAR (name) : name;
664 tree type = TREE_TYPE (var);
665 int unsignedp;
666 enum machine_mode reg_mode = promote_decl_mode (var, &unsignedp);
667 rtx x = gen_reg_rtx (reg_mode);
668 if (POINTER_TYPE_P (type))
669 mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (var))));
670 return x;
673 /* Insert required copies for T in graph G. Check for a strongly connected
674 region, and create a temporary to break the cycle if one is found. */
676 static void
677 elim_create (elim_graph g, int T)
679 int P, S;
680 source_location locus;
682 if (elim_unvisited_predecessor (g, T))
684 tree var = partition_to_var (g->map, T);
685 rtx U = get_temp_reg (var);
686 int unsignedsrcp = TYPE_UNSIGNED (TREE_TYPE (var));
688 insert_part_to_rtx_on_edge (g->e, U, T, UNKNOWN_LOCATION);
689 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
691 if (!bitmap_bit_p (g->visited, P))
693 elim_backward (g, P);
694 insert_rtx_to_part_on_edge (g->e, P, U, unsignedsrcp, locus);
698 else
700 S = elim_graph_remove_succ_edge (g, T, &locus);
701 if (S != -1)
703 bitmap_set_bit (g->visited, T);
704 insert_partition_copy_on_edge (g->e, T, S, locus);
710 /* Eliminate all the phi nodes on edge E in graph G. */
712 static void
713 eliminate_phi (edge e, elim_graph g)
715 int x;
717 gcc_assert (g->const_copies.length () == 0);
718 gcc_assert (g->copy_locus.length () == 0);
720 /* Abnormal edges already have everything coalesced. */
721 if (e->flags & EDGE_ABNORMAL)
722 return;
724 g->e = e;
726 eliminate_build (g);
728 if (elim_graph_size (g) != 0)
730 int part;
732 bitmap_clear (g->visited);
733 g->stack.truncate (0);
735 FOR_EACH_VEC_ELT (g->nodes, x, part)
737 if (!bitmap_bit_p (g->visited, part))
738 elim_forward (g, part);
741 bitmap_clear (g->visited);
742 while (g->stack.length () > 0)
744 x = g->stack.pop ();
745 if (!bitmap_bit_p (g->visited, x))
746 elim_create (g, x);
750 /* If there are any pending constant copies, issue them now. */
751 while (g->const_copies.length () > 0)
753 int dest;
754 tree src;
755 source_location locus;
757 src = g->const_copies.pop ();
758 dest = g->const_dests.pop ();
759 locus = g->copy_locus.pop ();
760 insert_value_copy_on_edge (e, dest, src, locus);
765 /* Remove each argument from PHI. If an arg was the last use of an SSA_NAME,
766 check to see if this allows another PHI node to be removed. */
768 static void
769 remove_gimple_phi_args (gimple phi)
771 use_operand_p arg_p;
772 ssa_op_iter iter;
774 if (dump_file && (dump_flags & TDF_DETAILS))
776 fprintf (dump_file, "Removing Dead PHI definition: ");
777 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
780 FOR_EACH_PHI_ARG (arg_p, phi, iter, SSA_OP_USE)
782 tree arg = USE_FROM_PTR (arg_p);
783 if (TREE_CODE (arg) == SSA_NAME)
785 /* Remove the reference to the existing argument. */
786 SET_USE (arg_p, NULL_TREE);
787 if (has_zero_uses (arg))
789 gimple stmt;
790 gimple_stmt_iterator gsi;
792 stmt = SSA_NAME_DEF_STMT (arg);
794 /* Also remove the def if it is a PHI node. */
795 if (gimple_code (stmt) == GIMPLE_PHI)
797 remove_gimple_phi_args (stmt);
798 gsi = gsi_for_stmt (stmt);
799 remove_phi_node (&gsi, true);
807 /* Remove any PHI node which is a virtual PHI, or a PHI with no uses. */
809 static void
810 eliminate_useless_phis (void)
812 basic_block bb;
813 gimple_stmt_iterator gsi;
814 tree result;
816 FOR_EACH_BB (bb)
818 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
820 gimple phi = gsi_stmt (gsi);
821 result = gimple_phi_result (phi);
822 if (virtual_operand_p (result))
824 #ifdef ENABLE_CHECKING
825 size_t i;
826 /* There should be no arguments which are not virtual, or the
827 results will be incorrect. */
828 for (i = 0; i < gimple_phi_num_args (phi); i++)
830 tree arg = PHI_ARG_DEF (phi, i);
831 if (TREE_CODE (arg) == SSA_NAME
832 && !virtual_operand_p (arg))
834 fprintf (stderr, "Argument of PHI is not virtual (");
835 print_generic_expr (stderr, arg, TDF_SLIM);
836 fprintf (stderr, "), but the result is :");
837 print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
838 internal_error ("SSA corruption");
841 #endif
842 remove_phi_node (&gsi, true);
844 else
846 /* Also remove real PHIs with no uses. */
847 if (has_zero_uses (result))
849 remove_gimple_phi_args (phi);
850 remove_phi_node (&gsi, true);
852 else
853 gsi_next (&gsi);
860 /* This function will rewrite the current program using the variable mapping
861 found in MAP. If the replacement vector VALUES is provided, any
862 occurrences of partitions with non-null entries in the vector will be
863 replaced with the expression in the vector instead of its mapped
864 variable. */
866 static void
867 rewrite_trees (var_map map ATTRIBUTE_UNUSED)
869 #ifdef ENABLE_CHECKING
870 basic_block bb;
871 /* Search for PHIs where the destination has no partition, but one
872 or more arguments has a partition. This should not happen and can
873 create incorrect code. */
874 FOR_EACH_BB (bb)
876 gimple_stmt_iterator gsi;
877 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
879 gimple phi = gsi_stmt (gsi);
880 tree T0 = var_to_partition_to_var (map, gimple_phi_result (phi));
881 if (T0 == NULL_TREE)
883 size_t i;
884 for (i = 0; i < gimple_phi_num_args (phi); i++)
886 tree arg = PHI_ARG_DEF (phi, i);
888 if (TREE_CODE (arg) == SSA_NAME
889 && var_to_partition (map, arg) != NO_PARTITION)
891 fprintf (stderr, "Argument of PHI is in a partition :(");
892 print_generic_expr (stderr, arg, TDF_SLIM);
893 fprintf (stderr, "), but the result is not :");
894 print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
895 internal_error ("SSA corruption");
901 #endif
904 /* Given the out-of-ssa info object SA (with prepared partitions)
905 eliminate all phi nodes in all basic blocks. Afterwards no
906 basic block will have phi nodes anymore and there are possibly
907 some RTL instructions inserted on edges. */
909 void
910 expand_phi_nodes (struct ssaexpand *sa)
912 basic_block bb;
913 elim_graph g = new_elim_graph (sa->map->num_partitions);
914 g->map = sa->map;
916 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR, next_bb)
917 if (!gimple_seq_empty_p (phi_nodes (bb)))
919 edge e;
920 edge_iterator ei;
921 FOR_EACH_EDGE (e, ei, bb->preds)
922 eliminate_phi (e, g);
923 set_phi_nodes (bb, NULL);
924 /* We can't redirect EH edges in RTL land, so we need to do this
925 here. Redirection happens only when splitting is necessary,
926 which it is only for critical edges, normally. For EH edges
927 it might also be necessary when the successor has more than
928 one predecessor. In that case the edge is either required to
929 be fallthru (which EH edges aren't), or the predecessor needs
930 to end with a jump (which again, isn't the case with EH edges).
931 Hence, split all EH edges on which we inserted instructions
932 and whose successor has multiple predecessors. */
933 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
935 if (e->insns.r && (e->flags & EDGE_EH)
936 && !single_pred_p (e->dest))
938 rtx insns = e->insns.r;
939 basic_block bb;
940 e->insns.r = NULL_RTX;
941 bb = split_edge (e);
942 single_pred_edge (bb)->insns.r = insns;
944 else
945 ei_next (&ei);
949 delete_elim_graph (g);
953 /* Remove the ssa-names in the current function and translate them into normal
954 compiler variables. PERFORM_TER is true if Temporary Expression Replacement
955 should also be used. */
957 static void
958 remove_ssa_form (bool perform_ter, struct ssaexpand *sa)
960 bitmap values = NULL;
961 var_map map;
962 unsigned i;
964 map = coalesce_ssa_name ();
966 /* Return to viewing the variable list as just all reference variables after
967 coalescing has been performed. */
968 partition_view_normal (map, false);
970 if (dump_file && (dump_flags & TDF_DETAILS))
972 fprintf (dump_file, "After Coalescing:\n");
973 dump_var_map (dump_file, map);
976 if (perform_ter)
978 values = find_replaceable_exprs (map);
979 if (values && dump_file && (dump_flags & TDF_DETAILS))
980 dump_replaceable_exprs (dump_file, values);
983 rewrite_trees (map);
985 sa->map = map;
986 sa->values = values;
987 sa->partition_has_default_def = BITMAP_ALLOC (NULL);
988 for (i = 1; i < num_ssa_names; i++)
990 tree t = ssa_name (i);
991 if (t && SSA_NAME_IS_DEFAULT_DEF (t))
993 int p = var_to_partition (map, t);
994 if (p != NO_PARTITION)
995 bitmap_set_bit (sa->partition_has_default_def, p);
1001 /* If not already done so for basic block BB, assign increasing uids
1002 to each of its instructions. */
1004 static void
1005 maybe_renumber_stmts_bb (basic_block bb)
1007 unsigned i = 0;
1008 gimple_stmt_iterator gsi;
1010 if (!bb->aux)
1011 return;
1012 bb->aux = NULL;
1013 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1015 gimple stmt = gsi_stmt (gsi);
1016 gimple_set_uid (stmt, i);
1017 i++;
1022 /* Return true if we can determine that the SSA_NAMEs RESULT (a result
1023 of a PHI node) and ARG (one of its arguments) conflict. Return false
1024 otherwise, also when we simply aren't sure. */
1026 static bool
1027 trivially_conflicts_p (basic_block bb, tree result, tree arg)
1029 use_operand_p use;
1030 imm_use_iterator imm_iter;
1031 gimple defa = SSA_NAME_DEF_STMT (arg);
1033 /* If ARG isn't defined in the same block it's too complicated for
1034 our little mind. */
1035 if (gimple_bb (defa) != bb)
1036 return false;
1038 FOR_EACH_IMM_USE_FAST (use, imm_iter, result)
1040 gimple use_stmt = USE_STMT (use);
1041 if (is_gimple_debug (use_stmt))
1042 continue;
1043 /* Now, if there's a use of RESULT that lies outside this basic block,
1044 then there surely is a conflict with ARG. */
1045 if (gimple_bb (use_stmt) != bb)
1046 return true;
1047 if (gimple_code (use_stmt) == GIMPLE_PHI)
1048 continue;
1049 /* The use now is in a real stmt of BB, so if ARG was defined
1050 in a PHI node (like RESULT) both conflict. */
1051 if (gimple_code (defa) == GIMPLE_PHI)
1052 return true;
1053 maybe_renumber_stmts_bb (bb);
1054 /* If the use of RESULT occurs after the definition of ARG,
1055 the two conflict too. */
1056 if (gimple_uid (defa) < gimple_uid (use_stmt))
1057 return true;
1060 return false;
1064 /* Search every PHI node for arguments associated with backedges which
1065 we can trivially determine will need a copy (the argument is either
1066 not an SSA_NAME or the argument has a different underlying variable
1067 than the PHI result).
1069 Insert a copy from the PHI argument to a new destination at the
1070 end of the block with the backedge to the top of the loop. Update
1071 the PHI argument to reference this new destination. */
1073 static void
1074 insert_backedge_copies (void)
1076 basic_block bb;
1077 gimple_stmt_iterator gsi;
1079 mark_dfs_back_edges ();
1081 FOR_EACH_BB (bb)
1083 /* Mark block as possibly needing calculation of UIDs. */
1084 bb->aux = &bb->aux;
1086 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1088 gimple phi = gsi_stmt (gsi);
1089 tree result = gimple_phi_result (phi);
1090 size_t i;
1092 if (virtual_operand_p (result))
1093 continue;
1095 for (i = 0; i < gimple_phi_num_args (phi); i++)
1097 tree arg = gimple_phi_arg_def (phi, i);
1098 edge e = gimple_phi_arg_edge (phi, i);
1100 /* If the argument is not an SSA_NAME, then we will need a
1101 constant initialization. If the argument is an SSA_NAME with
1102 a different underlying variable then a copy statement will be
1103 needed. */
1104 if ((e->flags & EDGE_DFS_BACK)
1105 && (TREE_CODE (arg) != SSA_NAME
1106 || SSA_NAME_VAR (arg) != SSA_NAME_VAR (result)
1107 || trivially_conflicts_p (bb, result, arg)))
1109 tree name;
1110 gimple stmt, last = NULL;
1111 gimple_stmt_iterator gsi2;
1113 gsi2 = gsi_last_bb (gimple_phi_arg_edge (phi, i)->src);
1114 if (!gsi_end_p (gsi2))
1115 last = gsi_stmt (gsi2);
1117 /* In theory the only way we ought to get back to the
1118 start of a loop should be with a COND_EXPR or GOTO_EXPR.
1119 However, better safe than sorry.
1120 If the block ends with a control statement or
1121 something that might throw, then we have to
1122 insert this assignment before the last
1123 statement. Else insert it after the last statement. */
1124 if (last && stmt_ends_bb_p (last))
1126 /* If the last statement in the block is the definition
1127 site of the PHI argument, then we can't insert
1128 anything after it. */
1129 if (TREE_CODE (arg) == SSA_NAME
1130 && SSA_NAME_DEF_STMT (arg) == last)
1131 continue;
1134 /* Create a new instance of the underlying variable of the
1135 PHI result. */
1136 name = copy_ssa_name (result, NULL);
1137 stmt = gimple_build_assign (name,
1138 gimple_phi_arg_def (phi, i));
1140 /* copy location if present. */
1141 if (gimple_phi_arg_has_location (phi, i))
1142 gimple_set_location (stmt,
1143 gimple_phi_arg_location (phi, i));
1145 /* Insert the new statement into the block and update
1146 the PHI node. */
1147 if (last && stmt_ends_bb_p (last))
1148 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
1149 else
1150 gsi_insert_after (&gsi2, stmt, GSI_NEW_STMT);
1151 SET_PHI_ARG_DEF (phi, i, name);
1156 /* Unmark this block again. */
1157 bb->aux = NULL;
1161 /* Free all memory associated with going out of SSA form. SA is
1162 the outof-SSA info object. */
1164 void
1165 finish_out_of_ssa (struct ssaexpand *sa)
1167 free (sa->partition_to_pseudo);
1168 if (sa->values)
1169 BITMAP_FREE (sa->values);
1170 delete_var_map (sa->map);
1171 BITMAP_FREE (sa->partition_has_default_def);
1172 memset (sa, 0, sizeof *sa);
1175 /* Take the current function out of SSA form, translating PHIs as described in
1176 R. Morgan, ``Building an Optimizing Compiler'',
1177 Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
1179 unsigned int
1180 rewrite_out_of_ssa (struct ssaexpand *sa)
1182 /* If elimination of a PHI requires inserting a copy on a backedge,
1183 then we will have to split the backedge which has numerous
1184 undesirable performance effects.
1186 A significant number of such cases can be handled here by inserting
1187 copies into the loop itself. */
1188 insert_backedge_copies ();
1191 /* Eliminate PHIs which are of no use, such as virtual or dead phis. */
1192 eliminate_useless_phis ();
1194 if (dump_file && (dump_flags & TDF_DETAILS))
1195 gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1197 remove_ssa_form (flag_tree_ter, sa);
1199 if (dump_file && (dump_flags & TDF_DETAILS))
1200 gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1202 return 0;