[PR64164] Drop copyrename, use coalescible partition as base when optimizing.
[official-gcc.git] / gcc / tree-outof-ssa.c
blobe6ab93206c73366ce4bcf0adeef6b87aaaa9ee26
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 "input.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "stor-layout.h"
31 #include "predict.h"
32 #include "hard-reg-set.h"
33 #include "function.h"
34 #include "dominance.h"
35 #include "cfg.h"
36 #include "cfgrtl.h"
37 #include "cfganal.h"
38 #include "basic-block.h"
39 #include "gimple-pretty-print.h"
40 #include "bitmap.h"
41 #include "sbitmap.h"
42 #include "tree-ssa-alias.h"
43 #include "internal-fn.h"
44 #include "tree-eh.h"
45 #include "gimple-expr.h"
46 #include "is-a.h"
47 #include "gimple.h"
48 #include "gimple-iterator.h"
49 #include "gimple-ssa.h"
50 #include "tree-cfg.h"
51 #include "tree-phinodes.h"
52 #include "ssa-iterators.h"
53 #include "stringpool.h"
54 #include "tree-ssanames.h"
55 #include "dumpfile.h"
56 #include "diagnostic-core.h"
57 #include "tree-ssa-live.h"
58 #include "tree-ssa-ter.h"
59 #include "tree-ssa-coalesce.h"
60 #include "tree-outof-ssa.h"
62 /* FIXME: A lot of code here deals with expanding to RTL. All that code
63 should be in cfgexpand.c. */
64 #include "rtl.h"
65 #include "flags.h"
66 #include "insn-config.h"
67 #include "expmed.h"
68 #include "dojump.h"
69 #include "explow.h"
70 #include "calls.h"
71 #include "emit-rtl.h"
72 #include "varasm.h"
73 #include "stmt.h"
74 #include "expr.h"
76 /* Return TRUE if expression STMT is suitable for replacement. */
78 bool
79 ssa_is_replaceable_p (gimple stmt)
81 use_operand_p use_p;
82 tree def;
83 gimple use_stmt;
85 /* Only consider modify stmts. */
86 if (!is_gimple_assign (stmt))
87 return false;
89 /* If the statement may throw an exception, it cannot be replaced. */
90 if (stmt_could_throw_p (stmt))
91 return false;
93 /* Punt if there is more than 1 def. */
94 def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
95 if (!def)
96 return false;
98 /* Only consider definitions which have a single use. */
99 if (!single_imm_use (def, &use_p, &use_stmt))
100 return false;
102 /* Used in this block, but at the TOP of the block, not the end. */
103 if (gimple_code (use_stmt) == GIMPLE_PHI)
104 return false;
106 /* There must be no VDEFs. */
107 if (gimple_vdef (stmt))
108 return false;
110 /* Float expressions must go through memory if float-store is on. */
111 if (flag_float_store
112 && FLOAT_TYPE_P (gimple_expr_type (stmt)))
113 return false;
115 /* An assignment with a register variable on the RHS is not
116 replaceable. */
117 if (gimple_assign_rhs_code (stmt) == VAR_DECL
118 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt)))
119 return false;
121 /* No function calls can be replaced. */
122 if (is_gimple_call (stmt))
123 return false;
125 /* Leave any stmt with volatile operands alone as well. */
126 if (gimple_has_volatile_ops (stmt))
127 return false;
129 return true;
133 /* Used to hold all the components required to do SSA PHI elimination.
134 The node and pred/succ list is a simple linear list of nodes and
135 edges represented as pairs of nodes.
137 The predecessor and successor list: Nodes are entered in pairs, where
138 [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
139 predecessors, all the odd elements are successors.
141 Rationale:
142 When implemented as bitmaps, very large programs SSA->Normal times were
143 being dominated by clearing the interference graph.
145 Typically this list of edges is extremely small since it only includes
146 PHI results and uses from a single edge which have not coalesced with
147 each other. This means that no virtual PHI nodes are included, and
148 empirical evidence suggests that the number of edges rarely exceed
149 3, and in a bootstrap of GCC, the maximum size encountered was 7.
150 This also limits the number of possible nodes that are involved to
151 rarely more than 6, and in the bootstrap of gcc, the maximum number
152 of nodes encountered was 12. */
154 typedef struct _elim_graph {
155 /* Size of the elimination vectors. */
156 int size;
158 /* List of nodes in the elimination graph. */
159 vec<int> nodes;
161 /* The predecessor and successor edge list. */
162 vec<int> edge_list;
164 /* Source locus on each edge */
165 vec<source_location> edge_locus;
167 /* Visited vector. */
168 sbitmap visited;
170 /* Stack for visited nodes. */
171 vec<int> stack;
173 /* The variable partition map. */
174 var_map map;
176 /* Edge being eliminated by this graph. */
177 edge e;
179 /* List of constant copies to emit. These are pushed on in pairs. */
180 vec<int> const_dests;
181 vec<tree> const_copies;
183 /* Source locations for any constant copies. */
184 vec<source_location> copy_locus;
185 } *elim_graph;
188 /* For an edge E find out a good source location to associate with
189 instructions inserted on edge E. If E has an implicit goto set,
190 use its location. Otherwise search instructions in predecessors
191 of E for a location, and use that one. That makes sense because
192 we insert on edges for PHI nodes, and effects of PHIs happen on
193 the end of the predecessor conceptually. */
195 static void
196 set_location_for_edge (edge e)
198 if (e->goto_locus)
200 set_curr_insn_location (e->goto_locus);
202 else
204 basic_block bb = e->src;
205 gimple_stmt_iterator gsi;
209 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
211 gimple stmt = gsi_stmt (gsi);
212 if (is_gimple_debug (stmt))
213 continue;
214 if (gimple_has_location (stmt) || gimple_block (stmt))
216 set_curr_insn_location (gimple_location (stmt));
217 return;
220 /* Nothing found in this basic block. Make a half-assed attempt
221 to continue with another block. */
222 if (single_pred_p (bb))
223 bb = single_pred (bb);
224 else
225 bb = e->src;
227 while (bb != e->src);
231 /* Emit insns to copy SRC into DEST converting SRC if necessary. As
232 SRC/DEST might be BLKmode memory locations SIZEEXP is a tree from
233 which we deduce the size to copy in that case. */
235 static inline rtx_insn *
236 emit_partition_copy (rtx dest, rtx src, int unsignedsrcp, tree sizeexp)
238 start_sequence ();
240 if (GET_MODE (src) != VOIDmode && GET_MODE (src) != GET_MODE (dest))
241 src = convert_to_mode (GET_MODE (dest), src, unsignedsrcp);
242 if (GET_MODE (src) == BLKmode)
244 gcc_assert (GET_MODE (dest) == BLKmode);
245 emit_block_move (dest, src, expr_size (sizeexp), BLOCK_OP_NORMAL);
247 else
248 emit_move_insn (dest, src);
250 rtx_insn *seq = get_insns ();
251 end_sequence ();
253 return seq;
256 /* Insert a copy instruction from partition SRC to DEST onto edge E. */
258 static void
259 insert_partition_copy_on_edge (edge e, int dest, int src, source_location locus)
261 tree var;
262 if (dump_file && (dump_flags & TDF_DETAILS))
264 fprintf (dump_file,
265 "Inserting a partition copy on edge BB%d->BB%d :"
266 "PART.%d = PART.%d",
267 e->src->index,
268 e->dest->index, dest, src);
269 fprintf (dump_file, "\n");
272 gcc_assert (SA.partition_to_pseudo[dest]);
273 gcc_assert (SA.partition_to_pseudo[src]);
275 set_location_for_edge (e);
276 /* If a locus is provided, override the default. */
277 if (locus)
278 set_curr_insn_location (locus);
280 var = partition_to_var (SA.map, src);
281 rtx_insn *seq = emit_partition_copy (copy_rtx (SA.partition_to_pseudo[dest]),
282 copy_rtx (SA.partition_to_pseudo[src]),
283 TYPE_UNSIGNED (TREE_TYPE (var)),
284 var);
286 insert_insn_on_edge (seq, e);
289 /* Insert a copy instruction from expression SRC to partition DEST
290 onto edge E. */
292 static void
293 insert_value_copy_on_edge (edge e, int dest, tree src, source_location locus)
295 rtx dest_rtx, seq, x;
296 machine_mode dest_mode, src_mode;
297 int unsignedp;
299 if (dump_file && (dump_flags & TDF_DETAILS))
301 fprintf (dump_file,
302 "Inserting a value copy on edge BB%d->BB%d : PART.%d = ",
303 e->src->index,
304 e->dest->index, dest);
305 print_generic_expr (dump_file, src, TDF_SLIM);
306 fprintf (dump_file, "\n");
309 dest_rtx = copy_rtx (SA.partition_to_pseudo[dest]);
310 gcc_assert (dest_rtx);
312 set_location_for_edge (e);
313 /* If a locus is provided, override the default. */
314 if (locus)
315 set_curr_insn_location (locus);
317 start_sequence ();
319 tree name = partition_to_var (SA.map, dest);
320 src_mode = TYPE_MODE (TREE_TYPE (src));
321 dest_mode = GET_MODE (dest_rtx);
322 gcc_assert (src_mode == TYPE_MODE (TREE_TYPE (name)));
323 gcc_assert (!REG_P (dest_rtx)
324 || dest_mode == promote_ssa_mode (name, &unsignedp));
326 if (src_mode != dest_mode)
328 x = expand_expr (src, NULL, src_mode, EXPAND_NORMAL);
329 x = convert_modes (dest_mode, src_mode, x, unsignedp);
331 else if (src_mode == BLKmode)
333 x = dest_rtx;
334 store_expr (src, x, 0, false);
336 else
337 x = expand_expr (src, dest_rtx, dest_mode, EXPAND_NORMAL);
339 if (x != dest_rtx)
340 emit_move_insn (dest_rtx, x);
341 seq = get_insns ();
342 end_sequence ();
344 insert_insn_on_edge (seq, e);
347 /* Insert a copy instruction from RTL expression SRC to partition DEST
348 onto edge E. */
350 static void
351 insert_rtx_to_part_on_edge (edge e, int dest, rtx src, int unsignedsrcp,
352 source_location locus)
354 if (dump_file && (dump_flags & TDF_DETAILS))
356 fprintf (dump_file,
357 "Inserting a temp copy on edge BB%d->BB%d : PART.%d = ",
358 e->src->index,
359 e->dest->index, dest);
360 print_simple_rtl (dump_file, src);
361 fprintf (dump_file, "\n");
364 gcc_assert (SA.partition_to_pseudo[dest]);
366 set_location_for_edge (e);
367 /* If a locus is provided, override the default. */
368 if (locus)
369 set_curr_insn_location (locus);
371 /* We give the destination as sizeexp in case src/dest are BLKmode
372 mems. Usually we give the source. As we result from SSA names
373 the left and right size should be the same (and no WITH_SIZE_EXPR
374 involved), so it doesn't matter. */
375 rtx_insn *seq = emit_partition_copy (copy_rtx (SA.partition_to_pseudo[dest]),
376 src, unsignedsrcp,
377 partition_to_var (SA.map, dest));
379 insert_insn_on_edge (seq, e);
382 /* Insert a copy instruction from partition SRC to RTL lvalue DEST
383 onto edge E. */
385 static void
386 insert_part_to_rtx_on_edge (edge e, rtx dest, int src, source_location locus)
388 tree var;
389 if (dump_file && (dump_flags & TDF_DETAILS))
391 fprintf (dump_file,
392 "Inserting a temp copy on edge BB%d->BB%d : ",
393 e->src->index,
394 e->dest->index);
395 print_simple_rtl (dump_file, dest);
396 fprintf (dump_file, "= PART.%d\n", src);
399 gcc_assert (SA.partition_to_pseudo[src]);
401 set_location_for_edge (e);
402 /* If a locus is provided, override the default. */
403 if (locus)
404 set_curr_insn_location (locus);
406 var = partition_to_var (SA.map, src);
407 rtx_insn *seq = emit_partition_copy (dest,
408 copy_rtx (SA.partition_to_pseudo[src]),
409 TYPE_UNSIGNED (TREE_TYPE (var)),
410 var);
412 insert_insn_on_edge (seq, e);
416 /* Create an elimination graph with SIZE nodes and associated data
417 structures. */
419 static elim_graph
420 new_elim_graph (int size)
422 elim_graph g = (elim_graph) xmalloc (sizeof (struct _elim_graph));
424 g->nodes.create (30);
425 g->const_dests.create (20);
426 g->const_copies.create (20);
427 g->copy_locus.create (10);
428 g->edge_list.create (20);
429 g->edge_locus.create (10);
430 g->stack.create (30);
432 g->visited = sbitmap_alloc (size);
434 return g;
438 /* Empty elimination graph G. */
440 static inline void
441 clear_elim_graph (elim_graph g)
443 g->nodes.truncate (0);
444 g->edge_list.truncate (0);
445 g->edge_locus.truncate (0);
449 /* Delete elimination graph G. */
451 static inline void
452 delete_elim_graph (elim_graph g)
454 sbitmap_free (g->visited);
455 g->stack.release ();
456 g->edge_list.release ();
457 g->const_copies.release ();
458 g->const_dests.release ();
459 g->nodes.release ();
460 g->copy_locus.release ();
461 g->edge_locus.release ();
463 free (g);
467 /* Return the number of nodes in graph G. */
469 static inline int
470 elim_graph_size (elim_graph g)
472 return g->nodes.length ();
476 /* Add NODE to graph G, if it doesn't exist already. */
478 static inline void
479 elim_graph_add_node (elim_graph g, int node)
481 int x;
482 int t;
484 FOR_EACH_VEC_ELT (g->nodes, x, t)
485 if (t == node)
486 return;
487 g->nodes.safe_push (node);
491 /* Add the edge PRED->SUCC to graph G. */
493 static inline void
494 elim_graph_add_edge (elim_graph g, int pred, int succ, source_location locus)
496 g->edge_list.safe_push (pred);
497 g->edge_list.safe_push (succ);
498 g->edge_locus.safe_push (locus);
502 /* Remove an edge from graph G for which NODE is the predecessor, and
503 return the successor node. -1 is returned if there is no such edge. */
505 static inline int
506 elim_graph_remove_succ_edge (elim_graph g, int node, source_location *locus)
508 int y;
509 unsigned x;
510 for (x = 0; x < g->edge_list.length (); x += 2)
511 if (g->edge_list[x] == node)
513 g->edge_list[x] = -1;
514 y = g->edge_list[x + 1];
515 g->edge_list[x + 1] = -1;
516 *locus = g->edge_locus[x / 2];
517 g->edge_locus[x / 2] = UNKNOWN_LOCATION;
518 return y;
520 *locus = UNKNOWN_LOCATION;
521 return -1;
525 /* Find all the nodes in GRAPH which are successors to NODE in the
526 edge list. VAR will hold the partition number found. CODE is the
527 code fragment executed for every node found. */
529 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, LOCUS, CODE) \
530 do { \
531 unsigned x_; \
532 int y_; \
533 for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
535 y_ = (GRAPH)->edge_list[x_]; \
536 if (y_ != (NODE)) \
537 continue; \
538 (void) ((VAR) = (GRAPH)->edge_list[x_ + 1]); \
539 (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
540 CODE; \
542 } while (0)
545 /* Find all the nodes which are predecessors of NODE in the edge list for
546 GRAPH. VAR will hold the partition number found. CODE is the
547 code fragment executed for every node found. */
549 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, LOCUS, CODE) \
550 do { \
551 unsigned x_; \
552 int y_; \
553 for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
555 y_ = (GRAPH)->edge_list[x_ + 1]; \
556 if (y_ != (NODE)) \
557 continue; \
558 (void) ((VAR) = (GRAPH)->edge_list[x_]); \
559 (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
560 CODE; \
562 } while (0)
565 /* Add T to elimination graph G. */
567 static inline void
568 eliminate_name (elim_graph g, int T)
570 elim_graph_add_node (g, T);
573 /* Return true if this phi argument T should have a copy queued when using
574 var_map MAP. PHI nodes should contain only ssa_names and invariants. A
575 test for ssa_name is definitely simpler, but don't let invalid contents
576 slip through in the meantime. */
578 static inline bool
579 queue_phi_copy_p (var_map map, tree t)
581 if (TREE_CODE (t) == SSA_NAME)
583 if (var_to_partition (map, t) == NO_PARTITION)
584 return true;
585 return false;
587 gcc_checking_assert (is_gimple_min_invariant (t));
588 return true;
591 /* Build elimination graph G for basic block BB on incoming PHI edge
592 G->e. */
594 static void
595 eliminate_build (elim_graph g)
597 tree Ti;
598 int p0, pi;
599 gphi_iterator gsi;
601 clear_elim_graph (g);
603 for (gsi = gsi_start_phis (g->e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
605 gphi *phi = gsi.phi ();
606 source_location locus;
608 p0 = var_to_partition (g->map, gimple_phi_result (phi));
609 /* Ignore results which are not in partitions. */
610 if (p0 == NO_PARTITION)
611 continue;
613 Ti = PHI_ARG_DEF (phi, g->e->dest_idx);
614 locus = gimple_phi_arg_location_from_edge (phi, g->e);
616 /* If this argument is a constant, or a SSA_NAME which is being
617 left in SSA form, just queue a copy to be emitted on this
618 edge. */
619 if (queue_phi_copy_p (g->map, Ti))
621 /* Save constant copies until all other copies have been emitted
622 on this edge. */
623 g->const_dests.safe_push (p0);
624 g->const_copies.safe_push (Ti);
625 g->copy_locus.safe_push (locus);
627 else
629 pi = var_to_partition (g->map, Ti);
630 if (p0 != pi)
632 eliminate_name (g, p0);
633 eliminate_name (g, pi);
634 elim_graph_add_edge (g, p0, pi, locus);
641 /* Push successors of T onto the elimination stack for G. */
643 static void
644 elim_forward (elim_graph g, int T)
646 int S;
647 source_location locus;
649 bitmap_set_bit (g->visited, T);
650 FOR_EACH_ELIM_GRAPH_SUCC (g, T, S, locus,
652 if (!bitmap_bit_p (g->visited, S))
653 elim_forward (g, S);
655 g->stack.safe_push (T);
659 /* Return 1 if there unvisited predecessors of T in graph G. */
661 static int
662 elim_unvisited_predecessor (elim_graph g, int T)
664 int P;
665 source_location locus;
667 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
669 if (!bitmap_bit_p (g->visited, P))
670 return 1;
672 return 0;
675 /* Process predecessors first, and insert a copy. */
677 static void
678 elim_backward (elim_graph g, int T)
680 int P;
681 source_location locus;
683 bitmap_set_bit (g->visited, T);
684 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
686 if (!bitmap_bit_p (g->visited, P))
688 elim_backward (g, P);
689 insert_partition_copy_on_edge (g->e, P, T, locus);
694 /* Allocate a new pseudo register usable for storing values sitting
695 in NAME (a decl or SSA name), i.e. with matching mode and attributes. */
697 static rtx
698 get_temp_reg (tree name)
700 tree type = TREE_TYPE (name);
701 int unsignedp;
702 machine_mode reg_mode = promote_ssa_mode (name, &unsignedp);
703 rtx x = gen_reg_rtx (reg_mode);
704 if (POINTER_TYPE_P (type))
705 mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (type)));
706 return x;
709 /* Insert required copies for T in graph G. Check for a strongly connected
710 region, and create a temporary to break the cycle if one is found. */
712 static void
713 elim_create (elim_graph g, int T)
715 int P, S;
716 source_location locus;
718 if (elim_unvisited_predecessor (g, T))
720 tree var = partition_to_var (g->map, T);
721 rtx U = get_temp_reg (var);
722 int unsignedsrcp = TYPE_UNSIGNED (TREE_TYPE (var));
724 insert_part_to_rtx_on_edge (g->e, U, T, UNKNOWN_LOCATION);
725 FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
727 if (!bitmap_bit_p (g->visited, P))
729 elim_backward (g, P);
730 insert_rtx_to_part_on_edge (g->e, P, U, unsignedsrcp, locus);
734 else
736 S = elim_graph_remove_succ_edge (g, T, &locus);
737 if (S != -1)
739 bitmap_set_bit (g->visited, T);
740 insert_partition_copy_on_edge (g->e, T, S, locus);
746 /* Eliminate all the phi nodes on edge E in graph G. */
748 static void
749 eliminate_phi (edge e, elim_graph g)
751 int x;
753 gcc_assert (g->const_copies.length () == 0);
754 gcc_assert (g->copy_locus.length () == 0);
756 /* Abnormal edges already have everything coalesced. */
757 if (e->flags & EDGE_ABNORMAL)
758 return;
760 g->e = e;
762 eliminate_build (g);
764 if (elim_graph_size (g) != 0)
766 int part;
768 bitmap_clear (g->visited);
769 g->stack.truncate (0);
771 FOR_EACH_VEC_ELT (g->nodes, x, part)
773 if (!bitmap_bit_p (g->visited, part))
774 elim_forward (g, part);
777 bitmap_clear (g->visited);
778 while (g->stack.length () > 0)
780 x = g->stack.pop ();
781 if (!bitmap_bit_p (g->visited, x))
782 elim_create (g, x);
786 /* If there are any pending constant copies, issue them now. */
787 while (g->const_copies.length () > 0)
789 int dest;
790 tree src;
791 source_location locus;
793 src = g->const_copies.pop ();
794 dest = g->const_dests.pop ();
795 locus = g->copy_locus.pop ();
796 insert_value_copy_on_edge (e, dest, src, locus);
801 /* Remove each argument from PHI. If an arg was the last use of an SSA_NAME,
802 check to see if this allows another PHI node to be removed. */
804 static void
805 remove_gimple_phi_args (gphi *phi)
807 use_operand_p arg_p;
808 ssa_op_iter iter;
810 if (dump_file && (dump_flags & TDF_DETAILS))
812 fprintf (dump_file, "Removing Dead PHI definition: ");
813 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
816 FOR_EACH_PHI_ARG (arg_p, phi, iter, SSA_OP_USE)
818 tree arg = USE_FROM_PTR (arg_p);
819 if (TREE_CODE (arg) == SSA_NAME)
821 /* Remove the reference to the existing argument. */
822 SET_USE (arg_p, NULL_TREE);
823 if (has_zero_uses (arg))
825 gimple stmt;
826 gimple_stmt_iterator gsi;
828 stmt = SSA_NAME_DEF_STMT (arg);
830 /* Also remove the def if it is a PHI node. */
831 if (gimple_code (stmt) == GIMPLE_PHI)
833 remove_gimple_phi_args (as_a <gphi *> (stmt));
834 gsi = gsi_for_stmt (stmt);
835 remove_phi_node (&gsi, true);
843 /* Remove any PHI node which is a virtual PHI, or a PHI with no uses. */
845 static void
846 eliminate_useless_phis (void)
848 basic_block bb;
849 gphi_iterator gsi;
850 tree result;
852 FOR_EACH_BB_FN (bb, cfun)
854 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
856 gphi *phi = gsi.phi ();
857 result = gimple_phi_result (phi);
858 if (virtual_operand_p (result))
860 #ifdef ENABLE_CHECKING
861 size_t i;
862 /* There should be no arguments which are not virtual, or the
863 results will be incorrect. */
864 for (i = 0; i < gimple_phi_num_args (phi); i++)
866 tree arg = PHI_ARG_DEF (phi, i);
867 if (TREE_CODE (arg) == SSA_NAME
868 && !virtual_operand_p (arg))
870 fprintf (stderr, "Argument of PHI is not virtual (");
871 print_generic_expr (stderr, arg, TDF_SLIM);
872 fprintf (stderr, "), but the result is :");
873 print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
874 internal_error ("SSA corruption");
877 #endif
878 remove_phi_node (&gsi, true);
880 else
882 /* Also remove real PHIs with no uses. */
883 if (has_zero_uses (result))
885 remove_gimple_phi_args (phi);
886 remove_phi_node (&gsi, true);
888 else
889 gsi_next (&gsi);
896 /* This function will rewrite the current program using the variable mapping
897 found in MAP. If the replacement vector VALUES is provided, any
898 occurrences of partitions with non-null entries in the vector will be
899 replaced with the expression in the vector instead of its mapped
900 variable. */
902 static void
903 rewrite_trees (var_map map ATTRIBUTE_UNUSED)
905 #ifdef ENABLE_CHECKING
906 basic_block bb;
907 /* Search for PHIs where the destination has no partition, but one
908 or more arguments has a partition. This should not happen and can
909 create incorrect code. */
910 FOR_EACH_BB_FN (bb, cfun)
912 gphi_iterator gsi;
913 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
915 gphi *phi = gsi.phi ();
916 tree T0 = var_to_partition_to_var (map, gimple_phi_result (phi));
917 if (T0 == NULL_TREE)
919 size_t i;
920 for (i = 0; i < gimple_phi_num_args (phi); i++)
922 tree arg = PHI_ARG_DEF (phi, i);
924 if (TREE_CODE (arg) == SSA_NAME
925 && var_to_partition (map, arg) != NO_PARTITION)
927 fprintf (stderr, "Argument of PHI is in a partition :(");
928 print_generic_expr (stderr, arg, TDF_SLIM);
929 fprintf (stderr, "), but the result is not :");
930 print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
931 internal_error ("SSA corruption");
937 #endif
940 /* Given the out-of-ssa info object SA (with prepared partitions)
941 eliminate all phi nodes in all basic blocks. Afterwards no
942 basic block will have phi nodes anymore and there are possibly
943 some RTL instructions inserted on edges. */
945 void
946 expand_phi_nodes (struct ssaexpand *sa)
948 basic_block bb;
949 elim_graph g = new_elim_graph (sa->map->num_partitions);
950 g->map = sa->map;
952 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb,
953 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
954 if (!gimple_seq_empty_p (phi_nodes (bb)))
956 edge e;
957 edge_iterator ei;
958 FOR_EACH_EDGE (e, ei, bb->preds)
959 eliminate_phi (e, g);
960 set_phi_nodes (bb, NULL);
961 /* We can't redirect EH edges in RTL land, so we need to do this
962 here. Redirection happens only when splitting is necessary,
963 which it is only for critical edges, normally. For EH edges
964 it might also be necessary when the successor has more than
965 one predecessor. In that case the edge is either required to
966 be fallthru (which EH edges aren't), or the predecessor needs
967 to end with a jump (which again, isn't the case with EH edges).
968 Hence, split all EH edges on which we inserted instructions
969 and whose successor has multiple predecessors. */
970 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
972 if (e->insns.r && (e->flags & EDGE_EH)
973 && !single_pred_p (e->dest))
975 rtx_insn *insns = e->insns.r;
976 basic_block bb;
977 e->insns.r = NULL;
978 bb = split_edge (e);
979 single_pred_edge (bb)->insns.r = insns;
981 else
982 ei_next (&ei);
986 delete_elim_graph (g);
990 /* Remove the ssa-names in the current function and translate them into normal
991 compiler variables. PERFORM_TER is true if Temporary Expression Replacement
992 should also be used. */
994 static void
995 remove_ssa_form (bool perform_ter, struct ssaexpand *sa)
997 bitmap values = NULL;
998 var_map map;
999 unsigned i;
1001 map = coalesce_ssa_name ();
1003 /* Return to viewing the variable list as just all reference variables after
1004 coalescing has been performed. */
1005 partition_view_normal (map);
1007 if (dump_file && (dump_flags & TDF_DETAILS))
1009 fprintf (dump_file, "After Coalescing:\n");
1010 dump_var_map (dump_file, map);
1013 if (perform_ter)
1015 values = find_replaceable_exprs (map);
1016 if (values && dump_file && (dump_flags & TDF_DETAILS))
1017 dump_replaceable_exprs (dump_file, values);
1020 rewrite_trees (map);
1022 sa->map = map;
1023 sa->values = values;
1024 sa->partition_has_default_def = BITMAP_ALLOC (NULL);
1025 for (i = 1; i < num_ssa_names; i++)
1027 tree t = ssa_name (i);
1028 if (t && SSA_NAME_IS_DEFAULT_DEF (t))
1030 int p = var_to_partition (map, t);
1031 if (p != NO_PARTITION)
1032 bitmap_set_bit (sa->partition_has_default_def, p);
1038 /* If not already done so for basic block BB, assign increasing uids
1039 to each of its instructions. */
1041 static void
1042 maybe_renumber_stmts_bb (basic_block bb)
1044 unsigned i = 0;
1045 gimple_stmt_iterator gsi;
1047 if (!bb->aux)
1048 return;
1049 bb->aux = NULL;
1050 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1052 gimple stmt = gsi_stmt (gsi);
1053 gimple_set_uid (stmt, i);
1054 i++;
1059 /* Return true if we can determine that the SSA_NAMEs RESULT (a result
1060 of a PHI node) and ARG (one of its arguments) conflict. Return false
1061 otherwise, also when we simply aren't sure. */
1063 static bool
1064 trivially_conflicts_p (basic_block bb, tree result, tree arg)
1066 use_operand_p use;
1067 imm_use_iterator imm_iter;
1068 gimple defa = SSA_NAME_DEF_STMT (arg);
1070 /* If ARG isn't defined in the same block it's too complicated for
1071 our little mind. */
1072 if (gimple_bb (defa) != bb)
1073 return false;
1075 FOR_EACH_IMM_USE_FAST (use, imm_iter, result)
1077 gimple use_stmt = USE_STMT (use);
1078 if (is_gimple_debug (use_stmt))
1079 continue;
1080 /* Now, if there's a use of RESULT that lies outside this basic block,
1081 then there surely is a conflict with ARG. */
1082 if (gimple_bb (use_stmt) != bb)
1083 return true;
1084 if (gimple_code (use_stmt) == GIMPLE_PHI)
1085 continue;
1086 /* The use now is in a real stmt of BB, so if ARG was defined
1087 in a PHI node (like RESULT) both conflict. */
1088 if (gimple_code (defa) == GIMPLE_PHI)
1089 return true;
1090 maybe_renumber_stmts_bb (bb);
1091 /* If the use of RESULT occurs after the definition of ARG,
1092 the two conflict too. */
1093 if (gimple_uid (defa) < gimple_uid (use_stmt))
1094 return true;
1097 return false;
1101 /* Search every PHI node for arguments associated with backedges which
1102 we can trivially determine will need a copy (the argument is either
1103 not an SSA_NAME or the argument has a different underlying variable
1104 than the PHI result).
1106 Insert a copy from the PHI argument to a new destination at the
1107 end of the block with the backedge to the top of the loop. Update
1108 the PHI argument to reference this new destination. */
1110 static void
1111 insert_backedge_copies (void)
1113 basic_block bb;
1114 gphi_iterator gsi;
1116 mark_dfs_back_edges ();
1118 FOR_EACH_BB_FN (bb, cfun)
1120 /* Mark block as possibly needing calculation of UIDs. */
1121 bb->aux = &bb->aux;
1123 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1125 gphi *phi = gsi.phi ();
1126 tree result = gimple_phi_result (phi);
1127 size_t i;
1129 if (virtual_operand_p (result))
1130 continue;
1132 for (i = 0; i < gimple_phi_num_args (phi); i++)
1134 tree arg = gimple_phi_arg_def (phi, i);
1135 edge e = gimple_phi_arg_edge (phi, i);
1137 /* If the argument is not an SSA_NAME, then we will need a
1138 constant initialization. If the argument is an SSA_NAME with
1139 a different underlying variable then a copy statement will be
1140 needed. */
1141 if ((e->flags & EDGE_DFS_BACK)
1142 && (TREE_CODE (arg) != SSA_NAME
1143 || SSA_NAME_VAR (arg) != SSA_NAME_VAR (result)
1144 || trivially_conflicts_p (bb, result, arg)))
1146 tree name;
1147 gassign *stmt;
1148 gimple last = NULL;
1149 gimple_stmt_iterator gsi2;
1151 gsi2 = gsi_last_bb (gimple_phi_arg_edge (phi, i)->src);
1152 if (!gsi_end_p (gsi2))
1153 last = gsi_stmt (gsi2);
1155 /* In theory the only way we ought to get back to the
1156 start of a loop should be with a COND_EXPR or GOTO_EXPR.
1157 However, better safe than sorry.
1158 If the block ends with a control statement or
1159 something that might throw, then we have to
1160 insert this assignment before the last
1161 statement. Else insert it after the last statement. */
1162 if (last && stmt_ends_bb_p (last))
1164 /* If the last statement in the block is the definition
1165 site of the PHI argument, then we can't insert
1166 anything after it. */
1167 if (TREE_CODE (arg) == SSA_NAME
1168 && SSA_NAME_DEF_STMT (arg) == last)
1169 continue;
1172 /* Create a new instance of the underlying variable of the
1173 PHI result. */
1174 name = copy_ssa_name (result);
1175 stmt = gimple_build_assign (name,
1176 gimple_phi_arg_def (phi, i));
1178 /* copy location if present. */
1179 if (gimple_phi_arg_has_location (phi, i))
1180 gimple_set_location (stmt,
1181 gimple_phi_arg_location (phi, i));
1183 /* Insert the new statement into the block and update
1184 the PHI node. */
1185 if (last && stmt_ends_bb_p (last))
1186 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
1187 else
1188 gsi_insert_after (&gsi2, stmt, GSI_NEW_STMT);
1189 SET_PHI_ARG_DEF (phi, i, name);
1194 /* Unmark this block again. */
1195 bb->aux = NULL;
1199 /* Free all memory associated with going out of SSA form. SA is
1200 the outof-SSA info object. */
1202 void
1203 finish_out_of_ssa (struct ssaexpand *sa)
1205 free (sa->partition_to_pseudo);
1206 if (sa->values)
1207 BITMAP_FREE (sa->values);
1208 delete_var_map (sa->map);
1209 BITMAP_FREE (sa->partition_has_default_def);
1210 memset (sa, 0, sizeof *sa);
1213 /* Take the current function out of SSA form, translating PHIs as described in
1214 R. Morgan, ``Building an Optimizing Compiler'',
1215 Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
1217 unsigned int
1218 rewrite_out_of_ssa (struct ssaexpand *sa)
1220 /* If elimination of a PHI requires inserting a copy on a backedge,
1221 then we will have to split the backedge which has numerous
1222 undesirable performance effects.
1224 A significant number of such cases can be handled here by inserting
1225 copies into the loop itself. */
1226 insert_backedge_copies ();
1229 /* Eliminate PHIs which are of no use, such as virtual or dead phis. */
1230 eliminate_useless_phis ();
1232 if (dump_file && (dump_flags & TDF_DETAILS))
1233 gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1235 remove_ssa_form (flag_tree_ter, sa);
1237 if (dump_file && (dump_flags & TDF_DETAILS))
1238 gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1240 return 0;