PR bootstrap/66085
[official-gcc.git] / gcc / tree-outof-ssa.c
blobe23bc0bfc87fb756650657b40e67b1bc58af05c2
1 /* Convert a program in SSA form into Normal form.
2 Copyright (C) 2004-2015 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 "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "stor-layout.h"
37 #include "predict.h"
38 #include "hard-reg-set.h"
39 #include "function.h"
40 #include "dominance.h"
41 #include "cfg.h"
42 #include "cfgrtl.h"
43 #include "cfganal.h"
44 #include "basic-block.h"
45 #include "gimple-pretty-print.h"
46 #include "bitmap.h"
47 #include "sbitmap.h"
48 #include "tree-ssa-alias.h"
49 #include "internal-fn.h"
50 #include "tree-eh.h"
51 #include "gimple-expr.h"
52 #include "is-a.h"
53 #include "gimple.h"
54 #include "gimple-iterator.h"
55 #include "gimple-ssa.h"
56 #include "tree-cfg.h"
57 #include "tree-phinodes.h"
58 #include "ssa-iterators.h"
59 #include "stringpool.h"
60 #include "tree-ssanames.h"
61 #include "dumpfile.h"
62 #include "diagnostic-core.h"
63 #include "tree-ssa-live.h"
64 #include "tree-ssa-ter.h"
65 #include "tree-ssa-coalesce.h"
66 #include "tree-outof-ssa.h"
68 /* FIXME: A lot of code here deals with expanding to RTL. All that code
69 should be in cfgexpand.c. */
70 #include "hashtab.h"
71 #include "rtl.h"
72 #include "flags.h"
73 #include "statistics.h"
74 #include "real.h"
75 #include "fixed-value.h"
76 #include "insn-config.h"
77 #include "expmed.h"
78 #include "dojump.h"
79 #include "explow.h"
80 #include "calls.h"
81 #include "emit-rtl.h"
82 #include "varasm.h"
83 #include "stmt.h"
84 #include "expr.h"
86 /* Return TRUE if expression STMT is suitable for replacement. */
88 bool
89 ssa_is_replaceable_p (gimple stmt)
91 use_operand_p use_p;
92 tree def;
93 gimple use_stmt;
95 /* Only consider modify stmts. */
96 if (!is_gimple_assign (stmt))
97 return false;
99 /* If the statement may throw an exception, it cannot be replaced. */
100 if (stmt_could_throw_p (stmt))
101 return false;
103 /* Punt if there is more than 1 def. */
104 def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
105 if (!def)
106 return false;
108 /* Only consider definitions which have a single use. */
109 if (!single_imm_use (def, &use_p, &use_stmt))
110 return false;
112 /* Used in this block, but at the TOP of the block, not the end. */
113 if (gimple_code (use_stmt) == GIMPLE_PHI)
114 return false;
116 /* There must be no VDEFs. */
117 if (gimple_vdef (stmt))
118 return false;
120 /* Float expressions must go through memory if float-store is on. */
121 if (flag_float_store
122 && FLOAT_TYPE_P (gimple_expr_type (stmt)))
123 return false;
125 /* An assignment with a register variable on the RHS is not
126 replaceable. */
127 if (gimple_assign_rhs_code (stmt) == VAR_DECL
128 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt)))
129 return false;
131 /* No function calls can be replaced. */
132 if (is_gimple_call (stmt))
133 return false;
135 /* Leave any stmt with volatile operands alone as well. */
136 if (gimple_has_volatile_ops (stmt))
137 return false;
139 return true;
143 /* Used to hold all the components required to do SSA PHI elimination.
144 The node and pred/succ list is a simple linear list of nodes and
145 edges represented as pairs of nodes.
147 The predecessor and successor list: Nodes are entered in pairs, where
148 [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
149 predecessors, all the odd elements are successors.
151 Rationale:
152 When implemented as bitmaps, very large programs SSA->Normal times were
153 being dominated by clearing the interference graph.
155 Typically this list of edges is extremely small since it only includes
156 PHI results and uses from a single edge which have not coalesced with
157 each other. This means that no virtual PHI nodes are included, and
158 empirical evidence suggests that the number of edges rarely exceed
159 3, and in a bootstrap of GCC, the maximum size encountered was 7.
160 This also limits the number of possible nodes that are involved to
161 rarely more than 6, and in the bootstrap of gcc, the maximum number
162 of nodes encountered was 12. */
164 typedef struct _elim_graph {
165 /* Size of the elimination vectors. */
166 int size;
168 /* List of nodes in the elimination graph. */
169 vec<int> nodes;
171 /* The predecessor and successor edge list. */
172 vec<int> edge_list;
174 /* Source locus on each edge */
175 vec<source_location> edge_locus;
177 /* Visited vector. */
178 sbitmap visited;
180 /* Stack for visited nodes. */
181 vec<int> stack;
183 /* The variable partition map. */
184 var_map map;
186 /* Edge being eliminated by this graph. */
187 edge e;
189 /* List of constant copies to emit. These are pushed on in pairs. */
190 vec<int> const_dests;
191 vec<tree> const_copies;
193 /* Source locations for any constant copies. */
194 vec<source_location> copy_locus;
195 } *elim_graph;
198 /* For an edge E find out a good source location to associate with
199 instructions inserted on edge E. If E has an implicit goto set,
200 use its location. Otherwise search instructions in predecessors
201 of E for a location, and use that one. That makes sense because
202 we insert on edges for PHI nodes, and effects of PHIs happen on
203 the end of the predecessor conceptually. */
205 static void
206 set_location_for_edge (edge e)
208 if (e->goto_locus)
210 set_curr_insn_location (e->goto_locus);
212 else
214 basic_block bb = e->src;
215 gimple_stmt_iterator gsi;
219 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
221 gimple stmt = gsi_stmt (gsi);
222 if (is_gimple_debug (stmt))
223 continue;
224 if (gimple_has_location (stmt) || gimple_block (stmt))
226 set_curr_insn_location (gimple_location (stmt));
227 return;
230 /* Nothing found in this basic block. Make a half-assed attempt
231 to continue with another block. */
232 if (single_pred_p (bb))
233 bb = single_pred (bb);
234 else
235 bb = e->src;
237 while (bb != e->src);
241 /* Emit insns to copy SRC into DEST converting SRC if necessary. As
242 SRC/DEST might be BLKmode memory locations SIZEEXP is a tree from
243 which we deduce the size to copy in that case. */
245 static inline rtx_insn *
246 emit_partition_copy (rtx dest, rtx src, int unsignedsrcp, tree sizeexp)
248 start_sequence ();
250 if (GET_MODE (src) != VOIDmode && GET_MODE (src) != GET_MODE (dest))
251 src = convert_to_mode (GET_MODE (dest), src, unsignedsrcp);
252 if (GET_MODE (src) == BLKmode)
254 gcc_assert (GET_MODE (dest) == BLKmode);
255 emit_block_move (dest, src, expr_size (sizeexp), BLOCK_OP_NORMAL);
257 else
258 emit_move_insn (dest, src);
260 rtx_insn *seq = get_insns ();
261 end_sequence ();
263 return seq;
266 /* Insert a copy instruction from partition SRC to DEST onto edge E. */
268 static void
269 insert_partition_copy_on_edge (edge e, int dest, int src, source_location locus)
271 tree var;
272 if (dump_file && (dump_flags & TDF_DETAILS))
274 fprintf (dump_file,
275 "Inserting a partition copy on edge BB%d->BB%d :"
276 "PART.%d = PART.%d",
277 e->src->index,
278 e->dest->index, dest, src);
279 fprintf (dump_file, "\n");
282 gcc_assert (SA.partition_to_pseudo[dest]);
283 gcc_assert (SA.partition_to_pseudo[src]);
285 set_location_for_edge (e);
286 /* If a locus is provided, override the default. */
287 if (locus)
288 set_curr_insn_location (locus);
290 var = partition_to_var (SA.map, src);
291 rtx_insn *seq = emit_partition_copy (copy_rtx (SA.partition_to_pseudo[dest]),
292 copy_rtx (SA.partition_to_pseudo[src]),
293 TYPE_UNSIGNED (TREE_TYPE (var)),
294 var);
296 insert_insn_on_edge (seq, e);
299 /* Insert a copy instruction from expression SRC to partition DEST
300 onto edge E. */
302 static void
303 insert_value_copy_on_edge (edge e, int dest, tree src, source_location locus)
305 rtx dest_rtx, seq, x;
306 machine_mode dest_mode, src_mode;
307 int unsignedp;
308 tree var;
310 if (dump_file && (dump_flags & TDF_DETAILS))
312 fprintf (dump_file,
313 "Inserting a value copy on edge BB%d->BB%d : PART.%d = ",
314 e->src->index,
315 e->dest->index, dest);
316 print_generic_expr (dump_file, src, TDF_SLIM);
317 fprintf (dump_file, "\n");
320 dest_rtx = copy_rtx (SA.partition_to_pseudo[dest]);
321 gcc_assert (dest_rtx);
323 set_location_for_edge (e);
324 /* If a locus is provided, override the default. */
325 if (locus)
326 set_curr_insn_location (locus);
328 start_sequence ();
330 var = SSA_NAME_VAR (partition_to_var (SA.map, dest));
331 src_mode = TYPE_MODE (TREE_TYPE (src));
332 dest_mode = GET_MODE (dest_rtx);
333 gcc_assert (src_mode == TYPE_MODE (TREE_TYPE (var)));
334 gcc_assert (!REG_P (dest_rtx)
335 || dest_mode == promote_decl_mode (var, &unsignedp));
337 if (src_mode != dest_mode)
339 x = expand_expr (src, NULL, src_mode, EXPAND_NORMAL);
340 x = convert_modes (dest_mode, src_mode, x, unsignedp);
342 else if (src_mode == BLKmode)
344 x = dest_rtx;
345 store_expr (src, x, 0, false);
347 else
348 x = expand_expr (src, dest_rtx, dest_mode, EXPAND_NORMAL);
350 if (x != dest_rtx)
351 emit_move_insn (dest_rtx, x);
352 seq = get_insns ();
353 end_sequence ();
355 insert_insn_on_edge (seq, e);
358 /* Insert a copy instruction from RTL expression SRC to partition DEST
359 onto edge E. */
361 static void
362 insert_rtx_to_part_on_edge (edge e, int dest, rtx src, int unsignedsrcp,
363 source_location locus)
365 if (dump_file && (dump_flags & TDF_DETAILS))
367 fprintf (dump_file,
368 "Inserting a temp copy on edge BB%d->BB%d : PART.%d = ",
369 e->src->index,
370 e->dest->index, dest);
371 print_simple_rtl (dump_file, src);
372 fprintf (dump_file, "\n");
375 gcc_assert (SA.partition_to_pseudo[dest]);
377 set_location_for_edge (e);
378 /* If a locus is provided, override the default. */
379 if (locus)
380 set_curr_insn_location (locus);
382 /* We give the destination as sizeexp in case src/dest are BLKmode
383 mems. Usually we give the source. As we result from SSA names
384 the left and right size should be the same (and no WITH_SIZE_EXPR
385 involved), so it doesn't matter. */
386 rtx_insn *seq = emit_partition_copy (copy_rtx (SA.partition_to_pseudo[dest]),
387 src, unsignedsrcp,
388 partition_to_var (SA.map, dest));
390 insert_insn_on_edge (seq, e);
393 /* Insert a copy instruction from partition SRC to RTL lvalue DEST
394 onto edge E. */
396 static void
397 insert_part_to_rtx_on_edge (edge e, rtx dest, int src, source_location locus)
399 tree var;
400 if (dump_file && (dump_flags & TDF_DETAILS))
402 fprintf (dump_file,
403 "Inserting a temp copy on edge BB%d->BB%d : ",
404 e->src->index,
405 e->dest->index);
406 print_simple_rtl (dump_file, dest);
407 fprintf (dump_file, "= PART.%d\n", src);
410 gcc_assert (SA.partition_to_pseudo[src]);
412 set_location_for_edge (e);
413 /* If a locus is provided, override the default. */
414 if (locus)
415 set_curr_insn_location (locus);
417 var = partition_to_var (SA.map, src);
418 rtx_insn *seq = emit_partition_copy (dest,
419 copy_rtx (SA.partition_to_pseudo[src]),
420 TYPE_UNSIGNED (TREE_TYPE (var)),
421 var);
423 insert_insn_on_edge (seq, e);
427 /* Create an elimination graph with SIZE nodes and associated data
428 structures. */
430 static elim_graph
431 new_elim_graph (int size)
433 elim_graph g = (elim_graph) xmalloc (sizeof (struct _elim_graph));
435 g->nodes.create (30);
436 g->const_dests.create (20);
437 g->const_copies.create (20);
438 g->copy_locus.create (10);
439 g->edge_list.create (20);
440 g->edge_locus.create (10);
441 g->stack.create (30);
443 g->visited = sbitmap_alloc (size);
445 return g;
449 /* Empty elimination graph G. */
451 static inline void
452 clear_elim_graph (elim_graph g)
454 g->nodes.truncate (0);
455 g->edge_list.truncate (0);
456 g->edge_locus.truncate (0);
460 /* Delete elimination graph G. */
462 static inline void
463 delete_elim_graph (elim_graph g)
465 sbitmap_free (g->visited);
466 g->stack.release ();
467 g->edge_list.release ();
468 g->const_copies.release ();
469 g->const_dests.release ();
470 g->nodes.release ();
471 g->copy_locus.release ();
472 g->edge_locus.release ();
474 free (g);
478 /* Return the number of nodes in graph G. */
480 static inline int
481 elim_graph_size (elim_graph g)
483 return g->nodes.length ();
487 /* Add NODE to graph G, if it doesn't exist already. */
489 static inline void
490 elim_graph_add_node (elim_graph g, int node)
492 int x;
493 int t;
495 FOR_EACH_VEC_ELT (g->nodes, x, t)
496 if (t == node)
497 return;
498 g->nodes.safe_push (node);
502 /* Add the edge PRED->SUCC to graph G. */
504 static inline void
505 elim_graph_add_edge (elim_graph g, int pred, int succ, source_location locus)
507 g->edge_list.safe_push (pred);
508 g->edge_list.safe_push (succ);
509 g->edge_locus.safe_push (locus);
513 /* Remove an edge from graph G for which NODE is the predecessor, and
514 return the successor node. -1 is returned if there is no such edge. */
516 static inline int
517 elim_graph_remove_succ_edge (elim_graph g, int node, source_location *locus)
519 int y;
520 unsigned x;
521 for (x = 0; x < g->edge_list.length (); x += 2)
522 if (g->edge_list[x] == node)
524 g->edge_list[x] = -1;
525 y = g->edge_list[x + 1];
526 g->edge_list[x + 1] = -1;
527 *locus = g->edge_locus[x / 2];
528 g->edge_locus[x / 2] = UNKNOWN_LOCATION;
529 return y;
531 *locus = UNKNOWN_LOCATION;
532 return -1;
536 /* Find all the nodes in GRAPH which are successors to NODE in the
537 edge list. VAR will hold the partition number found. CODE is the
538 code fragment executed for every node found. */
540 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, LOCUS, CODE) \
541 do { \
542 unsigned x_; \
543 int y_; \
544 for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
546 y_ = (GRAPH)->edge_list[x_]; \
547 if (y_ != (NODE)) \
548 continue; \
549 (void) ((VAR) = (GRAPH)->edge_list[x_ + 1]); \
550 (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
551 CODE; \
553 } while (0)
556 /* Find all the nodes which are predecessors of NODE in the edge list for
557 GRAPH. VAR will hold the partition number found. CODE is the
558 code fragment executed for every node found. */
560 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, LOCUS, CODE) \
561 do { \
562 unsigned x_; \
563 int y_; \
564 for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
566 y_ = (GRAPH)->edge_list[x_ + 1]; \
567 if (y_ != (NODE)) \
568 continue; \
569 (void) ((VAR) = (GRAPH)->edge_list[x_]); \
570 (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
571 CODE; \
573 } while (0)
576 /* Add T to elimination graph G. */
578 static inline void
579 eliminate_name (elim_graph g, int T)
581 elim_graph_add_node (g, T);
584 /* Return true if this phi argument T should have a copy queued when using
585 var_map MAP. PHI nodes should contain only ssa_names and invariants. A
586 test for ssa_name is definitely simpler, but don't let invalid contents
587 slip through in the meantime. */
589 static inline bool
590 queue_phi_copy_p (var_map map, tree t)
592 if (TREE_CODE (t) == SSA_NAME)
594 if (var_to_partition (map, t) == NO_PARTITION)
595 return true;
596 return false;
598 gcc_checking_assert (is_gimple_min_invariant (t));
599 return true;
602 /* Build elimination graph G for basic block BB on incoming PHI edge
603 G->e. */
605 static void
606 eliminate_build (elim_graph g)
608 tree Ti;
609 int p0, pi;
610 gphi_iterator gsi;
612 clear_elim_graph (g);
614 for (gsi = gsi_start_phis (g->e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
616 gphi *phi = gsi.phi ();
617 source_location locus;
619 p0 = var_to_partition (g->map, gimple_phi_result (phi));
620 /* Ignore results which are not in partitions. */
621 if (p0 == NO_PARTITION)
622 continue;
624 Ti = PHI_ARG_DEF (phi, g->e->dest_idx);
625 locus = gimple_phi_arg_location_from_edge (phi, g->e);
627 /* If this argument is a constant, or a SSA_NAME which is being
628 left in SSA form, just queue a copy to be emitted on this
629 edge. */
630 if (queue_phi_copy_p (g->map, Ti))
632 /* Save constant copies until all other copies have been emitted
633 on this edge. */
634 g->const_dests.safe_push (p0);
635 g->const_copies.safe_push (Ti);
636 g->copy_locus.safe_push (locus);
638 else
640 pi = var_to_partition (g->map, Ti);
641 if (p0 != pi)
643 eliminate_name (g, p0);
644 eliminate_name (g, pi);
645 elim_graph_add_edge (g, p0, pi, locus);
652 /* Push successors of T onto the elimination stack for G. */
654 static void
655 elim_forward (elim_graph g, int T)
657 int S;
658 source_location locus;
660 bitmap_set_bit (g->visited, T);
661 FOR_EACH_ELIM_GRAPH_SUCC (g, T, S, locus,
663 if (!bitmap_bit_p (g->visited, S))
664 elim_forward (g, S);
666 g->stack.safe_push (T);
670 /* Return 1 if there unvisited predecessors of T in graph G. */
672 static int
673 elim_unvisited_predecessor (elim_graph g, int T)
675 int P;
676 source_location locus;
678 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
680 if (!bitmap_bit_p (g->visited, P))
681 return 1;
683 return 0;
686 /* Process predecessors first, and insert a copy. */
688 static void
689 elim_backward (elim_graph g, int T)
691 int P;
692 source_location locus;
694 bitmap_set_bit (g->visited, T);
695 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
697 if (!bitmap_bit_p (g->visited, P))
699 elim_backward (g, P);
700 insert_partition_copy_on_edge (g->e, P, T, locus);
705 /* Allocate a new pseudo register usable for storing values sitting
706 in NAME (a decl or SSA name), i.e. with matching mode and attributes. */
708 static rtx
709 get_temp_reg (tree name)
711 tree var = TREE_CODE (name) == SSA_NAME ? SSA_NAME_VAR (name) : name;
712 tree type = TREE_TYPE (var);
713 int unsignedp;
714 machine_mode reg_mode = promote_decl_mode (var, &unsignedp);
715 rtx x = gen_reg_rtx (reg_mode);
716 if (POINTER_TYPE_P (type))
717 mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (var))));
718 return x;
721 /* Insert required copies for T in graph G. Check for a strongly connected
722 region, and create a temporary to break the cycle if one is found. */
724 static void
725 elim_create (elim_graph g, int T)
727 int P, S;
728 source_location locus;
730 if (elim_unvisited_predecessor (g, T))
732 tree var = partition_to_var (g->map, T);
733 rtx U = get_temp_reg (var);
734 int unsignedsrcp = TYPE_UNSIGNED (TREE_TYPE (var));
736 insert_part_to_rtx_on_edge (g->e, U, T, UNKNOWN_LOCATION);
737 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
739 if (!bitmap_bit_p (g->visited, P))
741 elim_backward (g, P);
742 insert_rtx_to_part_on_edge (g->e, P, U, unsignedsrcp, locus);
746 else
748 S = elim_graph_remove_succ_edge (g, T, &locus);
749 if (S != -1)
751 bitmap_set_bit (g->visited, T);
752 insert_partition_copy_on_edge (g->e, T, S, locus);
758 /* Eliminate all the phi nodes on edge E in graph G. */
760 static void
761 eliminate_phi (edge e, elim_graph g)
763 int x;
765 gcc_assert (g->const_copies.length () == 0);
766 gcc_assert (g->copy_locus.length () == 0);
768 /* Abnormal edges already have everything coalesced. */
769 if (e->flags & EDGE_ABNORMAL)
770 return;
772 g->e = e;
774 eliminate_build (g);
776 if (elim_graph_size (g) != 0)
778 int part;
780 bitmap_clear (g->visited);
781 g->stack.truncate (0);
783 FOR_EACH_VEC_ELT (g->nodes, x, part)
785 if (!bitmap_bit_p (g->visited, part))
786 elim_forward (g, part);
789 bitmap_clear (g->visited);
790 while (g->stack.length () > 0)
792 x = g->stack.pop ();
793 if (!bitmap_bit_p (g->visited, x))
794 elim_create (g, x);
798 /* If there are any pending constant copies, issue them now. */
799 while (g->const_copies.length () > 0)
801 int dest;
802 tree src;
803 source_location locus;
805 src = g->const_copies.pop ();
806 dest = g->const_dests.pop ();
807 locus = g->copy_locus.pop ();
808 insert_value_copy_on_edge (e, dest, src, locus);
813 /* Remove each argument from PHI. If an arg was the last use of an SSA_NAME,
814 check to see if this allows another PHI node to be removed. */
816 static void
817 remove_gimple_phi_args (gphi *phi)
819 use_operand_p arg_p;
820 ssa_op_iter iter;
822 if (dump_file && (dump_flags & TDF_DETAILS))
824 fprintf (dump_file, "Removing Dead PHI definition: ");
825 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
828 FOR_EACH_PHI_ARG (arg_p, phi, iter, SSA_OP_USE)
830 tree arg = USE_FROM_PTR (arg_p);
831 if (TREE_CODE (arg) == SSA_NAME)
833 /* Remove the reference to the existing argument. */
834 SET_USE (arg_p, NULL_TREE);
835 if (has_zero_uses (arg))
837 gimple stmt;
838 gimple_stmt_iterator gsi;
840 stmt = SSA_NAME_DEF_STMT (arg);
842 /* Also remove the def if it is a PHI node. */
843 if (gimple_code (stmt) == GIMPLE_PHI)
845 remove_gimple_phi_args (as_a <gphi *> (stmt));
846 gsi = gsi_for_stmt (stmt);
847 remove_phi_node (&gsi, true);
855 /* Remove any PHI node which is a virtual PHI, or a PHI with no uses. */
857 static void
858 eliminate_useless_phis (void)
860 basic_block bb;
861 gphi_iterator gsi;
862 tree result;
864 FOR_EACH_BB_FN (bb, cfun)
866 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
868 gphi *phi = gsi.phi ();
869 result = gimple_phi_result (phi);
870 if (virtual_operand_p (result))
872 #ifdef ENABLE_CHECKING
873 size_t i;
874 /* There should be no arguments which are not virtual, or the
875 results will be incorrect. */
876 for (i = 0; i < gimple_phi_num_args (phi); i++)
878 tree arg = PHI_ARG_DEF (phi, i);
879 if (TREE_CODE (arg) == SSA_NAME
880 && !virtual_operand_p (arg))
882 fprintf (stderr, "Argument of PHI is not virtual (");
883 print_generic_expr (stderr, arg, TDF_SLIM);
884 fprintf (stderr, "), but the result is :");
885 print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
886 internal_error ("SSA corruption");
889 #endif
890 remove_phi_node (&gsi, true);
892 else
894 /* Also remove real PHIs with no uses. */
895 if (has_zero_uses (result))
897 remove_gimple_phi_args (phi);
898 remove_phi_node (&gsi, true);
900 else
901 gsi_next (&gsi);
908 /* This function will rewrite the current program using the variable mapping
909 found in MAP. If the replacement vector VALUES is provided, any
910 occurrences of partitions with non-null entries in the vector will be
911 replaced with the expression in the vector instead of its mapped
912 variable. */
914 static void
915 rewrite_trees (var_map map ATTRIBUTE_UNUSED)
917 #ifdef ENABLE_CHECKING
918 basic_block bb;
919 /* Search for PHIs where the destination has no partition, but one
920 or more arguments has a partition. This should not happen and can
921 create incorrect code. */
922 FOR_EACH_BB_FN (bb, cfun)
924 gphi_iterator gsi;
925 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
927 gphi *phi = gsi.phi ();
928 tree T0 = var_to_partition_to_var (map, gimple_phi_result (phi));
929 if (T0 == NULL_TREE)
931 size_t i;
932 for (i = 0; i < gimple_phi_num_args (phi); i++)
934 tree arg = PHI_ARG_DEF (phi, i);
936 if (TREE_CODE (arg) == SSA_NAME
937 && var_to_partition (map, arg) != NO_PARTITION)
939 fprintf (stderr, "Argument of PHI is in a partition :(");
940 print_generic_expr (stderr, arg, TDF_SLIM);
941 fprintf (stderr, "), but the result is not :");
942 print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
943 internal_error ("SSA corruption");
949 #endif
952 /* Given the out-of-ssa info object SA (with prepared partitions)
953 eliminate all phi nodes in all basic blocks. Afterwards no
954 basic block will have phi nodes anymore and there are possibly
955 some RTL instructions inserted on edges. */
957 void
958 expand_phi_nodes (struct ssaexpand *sa)
960 basic_block bb;
961 elim_graph g = new_elim_graph (sa->map->num_partitions);
962 g->map = sa->map;
964 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb,
965 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
966 if (!gimple_seq_empty_p (phi_nodes (bb)))
968 edge e;
969 edge_iterator ei;
970 FOR_EACH_EDGE (e, ei, bb->preds)
971 eliminate_phi (e, g);
972 set_phi_nodes (bb, NULL);
973 /* We can't redirect EH edges in RTL land, so we need to do this
974 here. Redirection happens only when splitting is necessary,
975 which it is only for critical edges, normally. For EH edges
976 it might also be necessary when the successor has more than
977 one predecessor. In that case the edge is either required to
978 be fallthru (which EH edges aren't), or the predecessor needs
979 to end with a jump (which again, isn't the case with EH edges).
980 Hence, split all EH edges on which we inserted instructions
981 and whose successor has multiple predecessors. */
982 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
984 if (e->insns.r && (e->flags & EDGE_EH)
985 && !single_pred_p (e->dest))
987 rtx_insn *insns = e->insns.r;
988 basic_block bb;
989 e->insns.r = NULL;
990 bb = split_edge (e);
991 single_pred_edge (bb)->insns.r = insns;
993 else
994 ei_next (&ei);
998 delete_elim_graph (g);
1002 /* Remove the ssa-names in the current function and translate them into normal
1003 compiler variables. PERFORM_TER is true if Temporary Expression Replacement
1004 should also be used. */
1006 static void
1007 remove_ssa_form (bool perform_ter, struct ssaexpand *sa)
1009 bitmap values = NULL;
1010 var_map map;
1011 unsigned i;
1013 map = coalesce_ssa_name ();
1015 /* Return to viewing the variable list as just all reference variables after
1016 coalescing has been performed. */
1017 partition_view_normal (map, false);
1019 if (dump_file && (dump_flags & TDF_DETAILS))
1021 fprintf (dump_file, "After Coalescing:\n");
1022 dump_var_map (dump_file, map);
1025 if (perform_ter)
1027 values = find_replaceable_exprs (map);
1028 if (values && dump_file && (dump_flags & TDF_DETAILS))
1029 dump_replaceable_exprs (dump_file, values);
1032 rewrite_trees (map);
1034 sa->map = map;
1035 sa->values = values;
1036 sa->partition_has_default_def = BITMAP_ALLOC (NULL);
1037 for (i = 1; i < num_ssa_names; i++)
1039 tree t = ssa_name (i);
1040 if (t && SSA_NAME_IS_DEFAULT_DEF (t))
1042 int p = var_to_partition (map, t);
1043 if (p != NO_PARTITION)
1044 bitmap_set_bit (sa->partition_has_default_def, p);
1050 /* If not already done so for basic block BB, assign increasing uids
1051 to each of its instructions. */
1053 static void
1054 maybe_renumber_stmts_bb (basic_block bb)
1056 unsigned i = 0;
1057 gimple_stmt_iterator gsi;
1059 if (!bb->aux)
1060 return;
1061 bb->aux = NULL;
1062 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1064 gimple stmt = gsi_stmt (gsi);
1065 gimple_set_uid (stmt, i);
1066 i++;
1071 /* Return true if we can determine that the SSA_NAMEs RESULT (a result
1072 of a PHI node) and ARG (one of its arguments) conflict. Return false
1073 otherwise, also when we simply aren't sure. */
1075 static bool
1076 trivially_conflicts_p (basic_block bb, tree result, tree arg)
1078 use_operand_p use;
1079 imm_use_iterator imm_iter;
1080 gimple defa = SSA_NAME_DEF_STMT (arg);
1082 /* If ARG isn't defined in the same block it's too complicated for
1083 our little mind. */
1084 if (gimple_bb (defa) != bb)
1085 return false;
1087 FOR_EACH_IMM_USE_FAST (use, imm_iter, result)
1089 gimple use_stmt = USE_STMT (use);
1090 if (is_gimple_debug (use_stmt))
1091 continue;
1092 /* Now, if there's a use of RESULT that lies outside this basic block,
1093 then there surely is a conflict with ARG. */
1094 if (gimple_bb (use_stmt) != bb)
1095 return true;
1096 if (gimple_code (use_stmt) == GIMPLE_PHI)
1097 continue;
1098 /* The use now is in a real stmt of BB, so if ARG was defined
1099 in a PHI node (like RESULT) both conflict. */
1100 if (gimple_code (defa) == GIMPLE_PHI)
1101 return true;
1102 maybe_renumber_stmts_bb (bb);
1103 /* If the use of RESULT occurs after the definition of ARG,
1104 the two conflict too. */
1105 if (gimple_uid (defa) < gimple_uid (use_stmt))
1106 return true;
1109 return false;
1113 /* Search every PHI node for arguments associated with backedges which
1114 we can trivially determine will need a copy (the argument is either
1115 not an SSA_NAME or the argument has a different underlying variable
1116 than the PHI result).
1118 Insert a copy from the PHI argument to a new destination at the
1119 end of the block with the backedge to the top of the loop. Update
1120 the PHI argument to reference this new destination. */
1122 static void
1123 insert_backedge_copies (void)
1125 basic_block bb;
1126 gphi_iterator gsi;
1128 mark_dfs_back_edges ();
1130 FOR_EACH_BB_FN (bb, cfun)
1132 /* Mark block as possibly needing calculation of UIDs. */
1133 bb->aux = &bb->aux;
1135 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1137 gphi *phi = gsi.phi ();
1138 tree result = gimple_phi_result (phi);
1139 size_t i;
1141 if (virtual_operand_p (result))
1142 continue;
1144 for (i = 0; i < gimple_phi_num_args (phi); i++)
1146 tree arg = gimple_phi_arg_def (phi, i);
1147 edge e = gimple_phi_arg_edge (phi, i);
1149 /* If the argument is not an SSA_NAME, then we will need a
1150 constant initialization. If the argument is an SSA_NAME with
1151 a different underlying variable then a copy statement will be
1152 needed. */
1153 if ((e->flags & EDGE_DFS_BACK)
1154 && (TREE_CODE (arg) != SSA_NAME
1155 || SSA_NAME_VAR (arg) != SSA_NAME_VAR (result)
1156 || trivially_conflicts_p (bb, result, arg)))
1158 tree name;
1159 gassign *stmt;
1160 gimple last = NULL;
1161 gimple_stmt_iterator gsi2;
1163 gsi2 = gsi_last_bb (gimple_phi_arg_edge (phi, i)->src);
1164 if (!gsi_end_p (gsi2))
1165 last = gsi_stmt (gsi2);
1167 /* In theory the only way we ought to get back to the
1168 start of a loop should be with a COND_EXPR or GOTO_EXPR.
1169 However, better safe than sorry.
1170 If the block ends with a control statement or
1171 something that might throw, then we have to
1172 insert this assignment before the last
1173 statement. Else insert it after the last statement. */
1174 if (last && stmt_ends_bb_p (last))
1176 /* If the last statement in the block is the definition
1177 site of the PHI argument, then we can't insert
1178 anything after it. */
1179 if (TREE_CODE (arg) == SSA_NAME
1180 && SSA_NAME_DEF_STMT (arg) == last)
1181 continue;
1184 /* Create a new instance of the underlying variable of the
1185 PHI result. */
1186 name = copy_ssa_name (result);
1187 stmt = gimple_build_assign (name,
1188 gimple_phi_arg_def (phi, i));
1190 /* copy location if present. */
1191 if (gimple_phi_arg_has_location (phi, i))
1192 gimple_set_location (stmt,
1193 gimple_phi_arg_location (phi, i));
1195 /* Insert the new statement into the block and update
1196 the PHI node. */
1197 if (last && stmt_ends_bb_p (last))
1198 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
1199 else
1200 gsi_insert_after (&gsi2, stmt, GSI_NEW_STMT);
1201 SET_PHI_ARG_DEF (phi, i, name);
1206 /* Unmark this block again. */
1207 bb->aux = NULL;
1211 /* Free all memory associated with going out of SSA form. SA is
1212 the outof-SSA info object. */
1214 void
1215 finish_out_of_ssa (struct ssaexpand *sa)
1217 free (sa->partition_to_pseudo);
1218 if (sa->values)
1219 BITMAP_FREE (sa->values);
1220 delete_var_map (sa->map);
1221 BITMAP_FREE (sa->partition_has_default_def);
1222 memset (sa, 0, sizeof *sa);
1225 /* Take the current function out of SSA form, translating PHIs as described in
1226 R. Morgan, ``Building an Optimizing Compiler'',
1227 Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
1229 unsigned int
1230 rewrite_out_of_ssa (struct ssaexpand *sa)
1232 /* If elimination of a PHI requires inserting a copy on a backedge,
1233 then we will have to split the backedge which has numerous
1234 undesirable performance effects.
1236 A significant number of such cases can be handled here by inserting
1237 copies into the loop itself. */
1238 insert_backedge_copies ();
1241 /* Eliminate PHIs which are of no use, such as virtual or dead phis. */
1242 eliminate_useless_phis ();
1244 if (dump_file && (dump_flags & TDF_DETAILS))
1245 gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1247 remove_ssa_form (flag_tree_ter, sa);
1249 if (dump_file && (dump_flags & TDF_DETAILS))
1250 gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1252 return 0;