[AArch64 Testsuite] vld1_lane.c: Remove unused test data
[platform/upstream/gcc.git] / gcc / tree-outof-ssa.c
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>
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "cfghooks.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "rtl.h"
29 #include "ssa.h"
30 #include "alias.h"
31 #include "fold-const.h"
32 #include "stor-layout.h"
33 #include "cfgrtl.h"
34 #include "cfganal.h"
35 #include "gimple-pretty-print.h"
36 #include "internal-fn.h"
37 #include "tree-eh.h"
38 #include "gimple-iterator.h"
39 #include "tree-cfg.h"
40 #include "dumpfile.h"
41 #include "diagnostic-core.h"
42 #include "tree-ssa-live.h"
43 #include "tree-ssa-ter.h"
44 #include "tree-ssa-coalesce.h"
45 #include "tree-outof-ssa.h"
46
47 /* FIXME: A lot of code here deals with expanding to RTL.  All that code
48    should be in cfgexpand.c.  */
49 #include "flags.h"
50 #include "insn-config.h"
51 #include "expmed.h"
52 #include "dojump.h"
53 #include "explow.h"
54 #include "calls.h"
55 #include "emit-rtl.h"
56 #include "varasm.h"
57 #include "stmt.h"
58 #include "expr.h"
59
60 /* Return TRUE if expression STMT is suitable for replacement.  */
61
62 bool
63 ssa_is_replaceable_p (gimple stmt)
64 {
65   use_operand_p use_p;
66   tree def;
67   gimple use_stmt;
68
69   /* Only consider modify stmts.  */
70   if (!is_gimple_assign (stmt))
71     return false;
72
73   /* If the statement may throw an exception, it cannot be replaced.  */
74   if (stmt_could_throw_p (stmt))
75     return false;
76
77   /* Punt if there is more than 1 def.  */
78   def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
79   if (!def)
80     return false;
81
82   /* Only consider definitions which have a single use.  */
83   if (!single_imm_use (def, &use_p, &use_stmt))
84     return false;
85
86   /* Used in this block, but at the TOP of the block, not the end.  */
87   if (gimple_code (use_stmt) == GIMPLE_PHI)
88     return false;
89
90   /* There must be no VDEFs.  */
91   if (gimple_vdef (stmt))
92     return false;
93
94   /* Float expressions must go through memory if float-store is on.  */
95   if (flag_float_store
96       && FLOAT_TYPE_P (gimple_expr_type (stmt)))
97     return false;
98
99   /* An assignment with a register variable on the RHS is not
100      replaceable.  */
101   if (gimple_assign_rhs_code (stmt) == VAR_DECL
102       && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt)))
103     return false;
104
105   /* No function calls can be replaced.  */
106   if (is_gimple_call (stmt))
107     return false;
108
109   /* Leave any stmt with volatile operands alone as well.  */
110   if (gimple_has_volatile_ops (stmt))
111     return false;
112
113   return true;
114 }
115
116
117 /* Used to hold all the components required to do SSA PHI elimination.
118    The node and pred/succ list is a simple linear list of nodes and
119    edges represented as pairs of nodes.
120
121    The predecessor and successor list:  Nodes are entered in pairs, where
122    [0] ->PRED, [1]->SUCC.  All the even indexes in the array represent
123    predecessors, all the odd elements are successors.
124
125    Rationale:
126    When implemented as bitmaps, very large programs SSA->Normal times were
127    being dominated by clearing the interference graph.
128
129    Typically this list of edges is extremely small since it only includes
130    PHI results and uses from a single edge which have not coalesced with
131    each other.  This means that no virtual PHI nodes are included, and
132    empirical evidence suggests that the number of edges rarely exceed
133    3, and in a bootstrap of GCC, the maximum size encountered was 7.
134    This also limits the number of possible nodes that are involved to
135    rarely more than 6, and in the bootstrap of gcc, the maximum number
136    of nodes encountered was 12.  */
137
138 typedef struct _elim_graph {
139   /* Size of the elimination vectors.  */
140   int size;
141
142   /* List of nodes in the elimination graph.  */
143   vec<int> nodes;
144
145   /*  The predecessor and successor edge list.  */
146   vec<int> edge_list;
147
148   /* Source locus on each edge */
149   vec<source_location> edge_locus;
150
151   /* Visited vector.  */
152   sbitmap visited;
153
154   /* Stack for visited nodes.  */
155   vec<int> stack;
156
157   /* The variable partition map.  */
158   var_map map;
159
160   /* Edge being eliminated by this graph.  */
161   edge e;
162
163   /* List of constant copies to emit.  These are pushed on in pairs.  */
164   vec<int> const_dests;
165   vec<tree> const_copies;
166
167   /* Source locations for any constant copies.  */
168   vec<source_location> copy_locus;
169 } *elim_graph;
170
171
172 /* For an edge E find out a good source location to associate with
173    instructions inserted on edge E.  If E has an implicit goto set,
174    use its location.  Otherwise search instructions in predecessors
175    of E for a location, and use that one.  That makes sense because
176    we insert on edges for PHI nodes, and effects of PHIs happen on
177    the end of the predecessor conceptually.  */
178
179 static void
180 set_location_for_edge (edge e)
181 {
182   if (e->goto_locus)
183     {
184       set_curr_insn_location (e->goto_locus);
185     }
186   else
187     {
188       basic_block bb = e->src;
189       gimple_stmt_iterator gsi;
190
191       do
192         {
193           for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
194             {
195               gimple stmt = gsi_stmt (gsi);
196               if (is_gimple_debug (stmt))
197                 continue;
198               if (gimple_has_location (stmt) || gimple_block (stmt))
199                 {
200                   set_curr_insn_location (gimple_location (stmt));
201                   return;
202                 }
203             }
204           /* Nothing found in this basic block.  Make a half-assed attempt
205              to continue with another block.  */
206           if (single_pred_p (bb))
207             bb = single_pred (bb);
208           else
209             bb = e->src;
210         }
211       while (bb != e->src);
212     }
213 }
214
215 /* Emit insns to copy SRC into DEST converting SRC if necessary.  As
216    SRC/DEST might be BLKmode memory locations SIZEEXP is a tree from
217    which we deduce the size to copy in that case.  */
218
219 static inline rtx_insn *
220 emit_partition_copy (rtx dest, rtx src, int unsignedsrcp, tree sizeexp)
221 {
222   start_sequence ();
223
224   if (GET_MODE (src) != VOIDmode && GET_MODE (src) != GET_MODE (dest))
225     src = convert_to_mode (GET_MODE (dest), src, unsignedsrcp);
226   if (GET_MODE (src) == BLKmode)
227     {
228       gcc_assert (GET_MODE (dest) == BLKmode);
229       emit_block_move (dest, src, expr_size (sizeexp), BLOCK_OP_NORMAL);
230     }
231   else
232     emit_move_insn (dest, src);
233
234   rtx_insn *seq = get_insns ();
235   end_sequence ();
236
237   return seq;
238 }
239
240 /* Insert a copy instruction from partition SRC to DEST onto edge E.  */
241
242 static void
243 insert_partition_copy_on_edge (edge e, int dest, int src, source_location locus)
244 {
245   tree var;
246   if (dump_file && (dump_flags & TDF_DETAILS))
247     {
248       fprintf (dump_file,
249                "Inserting a partition copy on edge BB%d->BB%d :"
250                "PART.%d = PART.%d",
251                e->src->index,
252                e->dest->index, dest, src);
253       fprintf (dump_file, "\n");
254     }
255
256   gcc_assert (SA.partition_to_pseudo[dest]);
257   gcc_assert (SA.partition_to_pseudo[src]);
258
259   set_location_for_edge (e);
260   /* If a locus is provided, override the default.  */
261   if (locus)
262     set_curr_insn_location (locus);
263
264   var = partition_to_var (SA.map, src);
265   rtx_insn *seq = emit_partition_copy (copy_rtx (SA.partition_to_pseudo[dest]),
266                                        copy_rtx (SA.partition_to_pseudo[src]),
267                                        TYPE_UNSIGNED (TREE_TYPE (var)),
268                                        var);
269
270   insert_insn_on_edge (seq, e);
271 }
272
273 /* Insert a copy instruction from expression SRC to partition DEST
274    onto edge E.  */
275
276 static void
277 insert_value_copy_on_edge (edge e, int dest, tree src, source_location locus)
278 {
279   rtx dest_rtx, seq, x;
280   machine_mode dest_mode, src_mode;
281   int unsignedp;
282   tree var;
283
284   if (dump_file && (dump_flags & TDF_DETAILS))
285     {
286       fprintf (dump_file,
287                "Inserting a value copy on edge BB%d->BB%d : PART.%d = ",
288                e->src->index,
289                e->dest->index, dest);
290       print_generic_expr (dump_file, src, TDF_SLIM);
291       fprintf (dump_file, "\n");
292     }
293
294   dest_rtx = copy_rtx (SA.partition_to_pseudo[dest]);
295   gcc_assert (dest_rtx);
296
297   set_location_for_edge (e);
298   /* If a locus is provided, override the default.  */
299   if (locus)
300     set_curr_insn_location (locus);
301
302   start_sequence ();
303
304   var = SSA_NAME_VAR (partition_to_var (SA.map, dest));
305   src_mode = TYPE_MODE (TREE_TYPE (src));
306   dest_mode = GET_MODE (dest_rtx);
307   gcc_assert (src_mode == TYPE_MODE (TREE_TYPE (var)));
308   gcc_assert (!REG_P (dest_rtx)
309               || dest_mode == promote_decl_mode (var, &unsignedp));
310
311   if (src_mode != dest_mode)
312     {
313       x = expand_expr (src, NULL, src_mode, EXPAND_NORMAL);
314       x = convert_modes (dest_mode, src_mode, x, unsignedp);
315     }
316   else if (src_mode == BLKmode)
317     {
318       x = dest_rtx;
319       store_expr (src, x, 0, false);
320     }
321   else
322     x = expand_expr (src, dest_rtx, dest_mode, EXPAND_NORMAL);
323
324   if (x != dest_rtx)
325     emit_move_insn (dest_rtx, x);
326   seq = get_insns ();
327   end_sequence ();
328
329   insert_insn_on_edge (seq, e);
330 }
331
332 /* Insert a copy instruction from RTL expression SRC to partition DEST
333    onto edge E.  */
334
335 static void
336 insert_rtx_to_part_on_edge (edge e, int dest, rtx src, int unsignedsrcp,
337                             source_location locus)
338 {
339   if (dump_file && (dump_flags & TDF_DETAILS))
340     {
341       fprintf (dump_file,
342                "Inserting a temp copy on edge BB%d->BB%d : PART.%d = ",
343                e->src->index,
344                e->dest->index, dest);
345       print_simple_rtl (dump_file, src);
346       fprintf (dump_file, "\n");
347     }
348
349   gcc_assert (SA.partition_to_pseudo[dest]);
350
351   set_location_for_edge (e);
352   /* If a locus is provided, override the default.  */
353   if (locus)
354     set_curr_insn_location (locus);
355
356   /* We give the destination as sizeexp in case src/dest are BLKmode
357      mems.  Usually we give the source.  As we result from SSA names
358      the left and right size should be the same (and no WITH_SIZE_EXPR
359      involved), so it doesn't matter.  */
360   rtx_insn *seq = emit_partition_copy (copy_rtx (SA.partition_to_pseudo[dest]),
361                                        src, unsignedsrcp,
362                                        partition_to_var (SA.map, dest));
363
364   insert_insn_on_edge (seq, e);
365 }
366
367 /* Insert a copy instruction from partition SRC to RTL lvalue DEST
368    onto edge E.  */
369
370 static void
371 insert_part_to_rtx_on_edge (edge e, rtx dest, int src, source_location locus)
372 {
373   tree var;
374   if (dump_file && (dump_flags & TDF_DETAILS))
375     {
376       fprintf (dump_file,
377                "Inserting a temp copy on edge BB%d->BB%d : ",
378                e->src->index,
379                e->dest->index);
380       print_simple_rtl (dump_file, dest);
381       fprintf (dump_file, "= PART.%d\n", src);
382     }
383
384   gcc_assert (SA.partition_to_pseudo[src]);
385
386   set_location_for_edge (e);
387   /* If a locus is provided, override the default.  */
388   if (locus)
389     set_curr_insn_location (locus);
390
391   var = partition_to_var (SA.map, src);
392   rtx_insn *seq = emit_partition_copy (dest,
393                                        copy_rtx (SA.partition_to_pseudo[src]),
394                                        TYPE_UNSIGNED (TREE_TYPE (var)),
395                                        var);
396
397   insert_insn_on_edge (seq, e);
398 }
399
400
401 /* Create an elimination graph with SIZE nodes and associated data
402    structures.  */
403
404 static elim_graph
405 new_elim_graph (int size)
406 {
407   elim_graph g = (elim_graph) xmalloc (sizeof (struct _elim_graph));
408
409   g->nodes.create (30);
410   g->const_dests.create (20);
411   g->const_copies.create (20);
412   g->copy_locus.create (10);
413   g->edge_list.create (20);
414   g->edge_locus.create (10);
415   g->stack.create (30);
416
417   g->visited = sbitmap_alloc (size);
418
419   return g;
420 }
421
422
423 /* Empty elimination graph G.  */
424
425 static inline void
426 clear_elim_graph (elim_graph g)
427 {
428   g->nodes.truncate (0);
429   g->edge_list.truncate (0);
430   g->edge_locus.truncate (0);
431 }
432
433
434 /* Delete elimination graph G.  */
435
436 static inline void
437 delete_elim_graph (elim_graph g)
438 {
439   sbitmap_free (g->visited);
440   g->stack.release ();
441   g->edge_list.release ();
442   g->const_copies.release ();
443   g->const_dests.release ();
444   g->nodes.release ();
445   g->copy_locus.release ();
446   g->edge_locus.release ();
447
448   free (g);
449 }
450
451
452 /* Return the number of nodes in graph G.  */
453
454 static inline int
455 elim_graph_size (elim_graph g)
456 {
457   return g->nodes.length ();
458 }
459
460
461 /* Add NODE to graph G, if it doesn't exist already.  */
462
463 static inline void
464 elim_graph_add_node (elim_graph g, int node)
465 {
466   int x;
467   int t;
468
469   FOR_EACH_VEC_ELT (g->nodes, x, t)
470     if (t == node)
471       return;
472   g->nodes.safe_push (node);
473 }
474
475
476 /* Add the edge PRED->SUCC to graph G.  */
477
478 static inline void
479 elim_graph_add_edge (elim_graph g, int pred, int succ, source_location locus)
480 {
481   g->edge_list.safe_push (pred);
482   g->edge_list.safe_push (succ);
483   g->edge_locus.safe_push (locus);
484 }
485
486
487 /* Remove an edge from graph G for which NODE is the predecessor, and
488    return the successor node.  -1 is returned if there is no such edge.  */
489
490 static inline int
491 elim_graph_remove_succ_edge (elim_graph g, int node, source_location *locus)
492 {
493   int y;
494   unsigned x;
495   for (x = 0; x < g->edge_list.length (); x += 2)
496     if (g->edge_list[x] == node)
497       {
498         g->edge_list[x] = -1;
499         y = g->edge_list[x + 1];
500         g->edge_list[x + 1] = -1;
501         *locus = g->edge_locus[x / 2];
502         g->edge_locus[x / 2] = UNKNOWN_LOCATION;
503         return y;
504       }
505   *locus = UNKNOWN_LOCATION;
506   return -1;
507 }
508
509
510 /* Find all the nodes in GRAPH which are successors to NODE in the
511    edge list.  VAR will hold the partition number found.  CODE is the
512    code fragment executed for every node found.  */
513
514 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, LOCUS, CODE)         \
515 do {                                                                    \
516   unsigned x_;                                                          \
517   int y_;                                                               \
518   for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2)      \
519     {                                                                   \
520       y_ = (GRAPH)->edge_list[x_];                                      \
521       if (y_ != (NODE))                                                 \
522         continue;                                                       \
523       (void) ((VAR) = (GRAPH)->edge_list[x_ + 1]);                      \
524       (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]);                   \
525       CODE;                                                             \
526     }                                                                   \
527 } while (0)
528
529
530 /* Find all the nodes which are predecessors of NODE in the edge list for
531    GRAPH.  VAR will hold the partition number found.  CODE is the
532    code fragment executed for every node found.  */
533
534 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, LOCUS, CODE)         \
535 do {                                                                    \
536   unsigned x_;                                                          \
537   int y_;                                                               \
538   for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2)      \
539     {                                                                   \
540       y_ = (GRAPH)->edge_list[x_ + 1];                                  \
541       if (y_ != (NODE))                                                 \
542         continue;                                                       \
543       (void) ((VAR) = (GRAPH)->edge_list[x_]);                          \
544       (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]);                   \
545       CODE;                                                             \
546     }                                                                   \
547 } while (0)
548
549
550 /* Add T to elimination graph G.  */
551
552 static inline void
553 eliminate_name (elim_graph g, int T)
554 {
555   elim_graph_add_node (g, T);
556 }
557
558 /* Return true if this phi argument T should have a copy queued when using
559    var_map MAP.  PHI nodes should contain only ssa_names and invariants.  A
560    test for ssa_name is definitely simpler, but don't let invalid contents
561    slip through in the meantime.  */
562
563 static inline bool
564 queue_phi_copy_p (var_map map, tree t)
565 {
566   if (TREE_CODE (t) == SSA_NAME)
567     { 
568       if (var_to_partition (map, t) == NO_PARTITION)
569         return true;
570       return false;
571     }
572   gcc_checking_assert (is_gimple_min_invariant (t));
573   return true;
574 }
575
576 /* Build elimination graph G for basic block BB on incoming PHI edge
577    G->e.  */
578
579 static void
580 eliminate_build (elim_graph g)
581 {
582   tree Ti;
583   int p0, pi;
584   gphi_iterator gsi;
585
586   clear_elim_graph (g);
587
588   for (gsi = gsi_start_phis (g->e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
589     {
590       gphi *phi = gsi.phi ();
591       source_location locus;
592
593       p0 = var_to_partition (g->map, gimple_phi_result (phi));
594       /* Ignore results which are not in partitions.  */
595       if (p0 == NO_PARTITION)
596         continue;
597
598       Ti = PHI_ARG_DEF (phi, g->e->dest_idx);
599       locus = gimple_phi_arg_location_from_edge (phi, g->e);
600
601       /* If this argument is a constant, or a SSA_NAME which is being
602          left in SSA form, just queue a copy to be emitted on this
603          edge.  */
604       if (queue_phi_copy_p (g->map, Ti))
605         {
606           /* Save constant copies until all other copies have been emitted
607              on this edge.  */
608           g->const_dests.safe_push (p0);
609           g->const_copies.safe_push (Ti);
610           g->copy_locus.safe_push (locus);
611         }
612       else
613         {
614           pi = var_to_partition (g->map, Ti);
615           if (p0 != pi)
616             {
617               eliminate_name (g, p0);
618               eliminate_name (g, pi);
619               elim_graph_add_edge (g, p0, pi, locus);
620             }
621         }
622     }
623 }
624
625
626 /* Push successors of T onto the elimination stack for G.  */
627
628 static void
629 elim_forward (elim_graph g, int T)
630 {
631   int S;
632   source_location locus;
633
634   bitmap_set_bit (g->visited, T);
635   FOR_EACH_ELIM_GRAPH_SUCC (g, T, S, locus,
636     {
637       if (!bitmap_bit_p (g->visited, S))
638         elim_forward (g, S);
639     });
640   g->stack.safe_push (T);
641 }
642
643
644 /* Return 1 if there unvisited predecessors of T in graph G.  */
645
646 static int
647 elim_unvisited_predecessor (elim_graph g, int T)
648 {
649   int P;
650   source_location locus;
651
652   FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
653     {
654       if (!bitmap_bit_p (g->visited, P))
655         return 1;
656     });
657   return 0;
658 }
659
660 /* Process predecessors first, and insert a copy.  */
661
662 static void
663 elim_backward (elim_graph g, int T)
664 {
665   int P;
666   source_location locus;
667
668   bitmap_set_bit (g->visited, T);
669   FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
670     {
671       if (!bitmap_bit_p (g->visited, P))
672         {
673           elim_backward (g, P);
674           insert_partition_copy_on_edge (g->e, P, T, locus);
675         }
676     });
677 }
678
679 /* Allocate a new pseudo register usable for storing values sitting
680    in NAME (a decl or SSA name), i.e. with matching mode and attributes.  */
681
682 static rtx
683 get_temp_reg (tree name)
684 {
685   tree var = TREE_CODE (name) == SSA_NAME ? SSA_NAME_VAR (name) : name;
686   tree type = TREE_TYPE (var);
687   int unsignedp;
688   machine_mode reg_mode = promote_decl_mode (var, &unsignedp);
689   rtx x = gen_reg_rtx (reg_mode);
690   if (POINTER_TYPE_P (type))
691     mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (var))));
692   return x;
693 }
694
695 /* Insert required copies for T in graph G.  Check for a strongly connected
696    region, and create a temporary to break the cycle if one is found.  */
697
698 static void
699 elim_create (elim_graph g, int T)
700 {
701   int P, S;
702   source_location locus;
703
704   if (elim_unvisited_predecessor (g, T))
705     {
706       tree var = partition_to_var (g->map, T);
707       rtx U = get_temp_reg (var);
708       int unsignedsrcp = TYPE_UNSIGNED (TREE_TYPE (var));
709
710       insert_part_to_rtx_on_edge (g->e, U, T, UNKNOWN_LOCATION);
711       FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
712         {
713           if (!bitmap_bit_p (g->visited, P))
714             {
715               elim_backward (g, P);
716               insert_rtx_to_part_on_edge (g->e, P, U, unsignedsrcp, locus);
717             }
718         });
719     }
720   else
721     {
722       S = elim_graph_remove_succ_edge (g, T, &locus);
723       if (S != -1)
724         {
725           bitmap_set_bit (g->visited, T);
726           insert_partition_copy_on_edge (g->e, T, S, locus);
727         }
728     }
729 }
730
731
732 /* Eliminate all the phi nodes on edge E in graph G.  */
733
734 static void
735 eliminate_phi (edge e, elim_graph g)
736 {
737   int x;
738
739   gcc_assert (g->const_copies.length () == 0);
740   gcc_assert (g->copy_locus.length () == 0);
741
742   /* Abnormal edges already have everything coalesced.  */
743   if (e->flags & EDGE_ABNORMAL)
744     return;
745
746   g->e = e;
747
748   eliminate_build (g);
749
750   if (elim_graph_size (g) != 0)
751     {
752       int part;
753
754       bitmap_clear (g->visited);
755       g->stack.truncate (0);
756
757       FOR_EACH_VEC_ELT (g->nodes, x, part)
758         {
759           if (!bitmap_bit_p (g->visited, part))
760             elim_forward (g, part);
761         }
762
763       bitmap_clear (g->visited);
764       while (g->stack.length () > 0)
765         {
766           x = g->stack.pop ();
767           if (!bitmap_bit_p (g->visited, x))
768             elim_create (g, x);
769         }
770     }
771
772   /* If there are any pending constant copies, issue them now.  */
773   while (g->const_copies.length () > 0)
774     {
775       int dest;
776       tree src;
777       source_location locus;
778
779       src = g->const_copies.pop ();
780       dest = g->const_dests.pop ();
781       locus = g->copy_locus.pop ();
782       insert_value_copy_on_edge (e, dest, src, locus);
783     }
784 }
785
786
787 /* Remove each argument from PHI.  If an arg was the last use of an SSA_NAME,
788    check to see if this allows another PHI node to be removed.  */
789
790 static void
791 remove_gimple_phi_args (gphi *phi)
792 {
793   use_operand_p arg_p;
794   ssa_op_iter iter;
795
796   if (dump_file && (dump_flags & TDF_DETAILS))
797     {
798       fprintf (dump_file, "Removing Dead PHI definition: ");
799       print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
800     }
801
802   FOR_EACH_PHI_ARG (arg_p, phi, iter, SSA_OP_USE)
803     {
804       tree arg = USE_FROM_PTR (arg_p);
805       if (TREE_CODE (arg) == SSA_NAME)
806         {
807           /* Remove the reference to the existing argument.  */
808           SET_USE (arg_p, NULL_TREE);
809           if (has_zero_uses (arg))
810             {
811               gimple stmt;
812               gimple_stmt_iterator gsi;
813
814               stmt = SSA_NAME_DEF_STMT (arg);
815
816               /* Also remove the def if it is a PHI node.  */
817               if (gimple_code (stmt) == GIMPLE_PHI)
818                 {
819                   remove_gimple_phi_args (as_a <gphi *> (stmt));
820                   gsi = gsi_for_stmt (stmt);
821                   remove_phi_node (&gsi, true);
822                 }
823
824             }
825         }
826     }
827 }
828
829 /* Remove any PHI node which is a virtual PHI, or a PHI with no uses.  */
830
831 static void
832 eliminate_useless_phis (void)
833 {
834   basic_block bb;
835   gphi_iterator gsi;
836   tree result;
837
838   FOR_EACH_BB_FN (bb, cfun)
839     {
840       for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
841         {
842           gphi *phi = gsi.phi ();
843           result = gimple_phi_result (phi);
844           if (virtual_operand_p (result))
845             {
846 #ifdef ENABLE_CHECKING
847               size_t i;
848               /* There should be no arguments which are not virtual, or the
849                  results will be incorrect.  */
850               for (i = 0; i < gimple_phi_num_args (phi); i++)
851                 {
852                   tree arg = PHI_ARG_DEF (phi, i);
853                   if (TREE_CODE (arg) == SSA_NAME
854                       && !virtual_operand_p (arg))
855                     {
856                       fprintf (stderr, "Argument of PHI is not virtual (");
857                       print_generic_expr (stderr, arg, TDF_SLIM);
858                       fprintf (stderr, "), but the result is :");
859                       print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
860                       internal_error ("SSA corruption");
861                     }
862                 }
863 #endif
864               remove_phi_node (&gsi, true);
865             }
866           else
867             {
868               /* Also remove real PHIs with no uses.  */
869               if (has_zero_uses (result))
870                 {
871                   remove_gimple_phi_args (phi);
872                   remove_phi_node (&gsi, true);
873                 }
874               else
875                 gsi_next (&gsi);
876             }
877         }
878     }
879 }
880
881
882 /* This function will rewrite the current program using the variable mapping
883    found in MAP.  If the replacement vector VALUES is provided, any
884    occurrences of partitions with non-null entries in the vector will be
885    replaced with the expression in the vector instead of its mapped
886    variable.  */
887
888 static void
889 rewrite_trees (var_map map ATTRIBUTE_UNUSED)
890 {
891 #ifdef ENABLE_CHECKING
892   basic_block bb;
893   /* Search for PHIs where the destination has no partition, but one
894      or more arguments has a partition.  This should not happen and can
895      create incorrect code.  */
896   FOR_EACH_BB_FN (bb, cfun)
897     {
898       gphi_iterator gsi;
899       for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
900         {
901           gphi *phi = gsi.phi ();
902           tree T0 = var_to_partition_to_var (map, gimple_phi_result (phi));
903           if (T0 == NULL_TREE)
904             {
905               size_t i;
906               for (i = 0; i < gimple_phi_num_args (phi); i++)
907                 {
908                   tree arg = PHI_ARG_DEF (phi, i);
909
910                   if (TREE_CODE (arg) == SSA_NAME
911                       && var_to_partition (map, arg) != NO_PARTITION)
912                     {
913                       fprintf (stderr, "Argument of PHI is in a partition :(");
914                       print_generic_expr (stderr, arg, TDF_SLIM);
915                       fprintf (stderr, "), but the result is not :");
916                       print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
917                       internal_error ("SSA corruption");
918                     }
919                 }
920             }
921         }
922     }
923 #endif
924 }
925
926 /* Given the out-of-ssa info object SA (with prepared partitions)
927    eliminate all phi nodes in all basic blocks.  Afterwards no
928    basic block will have phi nodes anymore and there are possibly
929    some RTL instructions inserted on edges.  */
930
931 void
932 expand_phi_nodes (struct ssaexpand *sa)
933 {
934   basic_block bb;
935   elim_graph g = new_elim_graph (sa->map->num_partitions);
936   g->map = sa->map;
937
938   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb,
939                   EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
940     if (!gimple_seq_empty_p (phi_nodes (bb)))
941       {
942         edge e;
943         edge_iterator ei;
944         FOR_EACH_EDGE (e, ei, bb->preds)
945           eliminate_phi (e, g);
946         set_phi_nodes (bb, NULL);
947         /* We can't redirect EH edges in RTL land, so we need to do this
948            here.  Redirection happens only when splitting is necessary,
949            which it is only for critical edges, normally.  For EH edges
950            it might also be necessary when the successor has more than
951            one predecessor.  In that case the edge is either required to
952            be fallthru (which EH edges aren't), or the predecessor needs
953            to end with a jump (which again, isn't the case with EH edges).
954            Hence, split all EH edges on which we inserted instructions
955            and whose successor has multiple predecessors.  */
956         for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
957           {
958             if (e->insns.r && (e->flags & EDGE_EH)
959                 && !single_pred_p (e->dest))
960               {
961                 rtx_insn *insns = e->insns.r;
962                 basic_block bb;
963                 e->insns.r = NULL;
964                 bb = split_edge (e);
965                 single_pred_edge (bb)->insns.r = insns;
966               }
967             else
968               ei_next (&ei);
969           }
970       }
971
972   delete_elim_graph (g);
973 }
974
975
976 /* Remove the ssa-names in the current function and translate them into normal
977    compiler variables.  PERFORM_TER is true if Temporary Expression Replacement
978    should also be used.  */
979
980 static void
981 remove_ssa_form (bool perform_ter, struct ssaexpand *sa)
982 {
983   bitmap values = NULL;
984   var_map map;
985   unsigned i;
986
987   map = coalesce_ssa_name ();
988
989   /* Return to viewing the variable list as just all reference variables after
990      coalescing has been performed.  */
991   partition_view_normal (map, false);
992
993   if (dump_file && (dump_flags & TDF_DETAILS))
994     {
995       fprintf (dump_file, "After Coalescing:\n");
996       dump_var_map (dump_file, map);
997     }
998
999   if (perform_ter)
1000     {
1001       values = find_replaceable_exprs (map);
1002       if (values && dump_file && (dump_flags & TDF_DETAILS))
1003         dump_replaceable_exprs (dump_file, values);
1004     }
1005
1006   rewrite_trees (map);
1007
1008   sa->map = map;
1009   sa->values = values;
1010   sa->partition_has_default_def = BITMAP_ALLOC (NULL);
1011   for (i = 1; i < num_ssa_names; i++)
1012     {
1013       tree t = ssa_name (i);
1014       if (t && SSA_NAME_IS_DEFAULT_DEF (t))
1015         {
1016           int p = var_to_partition (map, t);
1017           if (p != NO_PARTITION)
1018             bitmap_set_bit (sa->partition_has_default_def, p);
1019         }
1020     }
1021 }
1022
1023
1024 /* If not already done so for basic block BB, assign increasing uids
1025    to each of its instructions.  */
1026
1027 static void
1028 maybe_renumber_stmts_bb (basic_block bb)
1029 {
1030   unsigned i = 0;
1031   gimple_stmt_iterator gsi;
1032
1033   if (!bb->aux)
1034     return;
1035   bb->aux = NULL;
1036   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1037     {
1038       gimple stmt = gsi_stmt (gsi);
1039       gimple_set_uid (stmt, i);
1040       i++;
1041     }
1042 }
1043
1044
1045 /* Return true if we can determine that the SSA_NAMEs RESULT (a result
1046    of a PHI node) and ARG (one of its arguments) conflict.  Return false
1047    otherwise, also when we simply aren't sure.  */
1048
1049 static bool
1050 trivially_conflicts_p (basic_block bb, tree result, tree arg)
1051 {
1052   use_operand_p use;
1053   imm_use_iterator imm_iter;
1054   gimple defa = SSA_NAME_DEF_STMT (arg);
1055
1056   /* If ARG isn't defined in the same block it's too complicated for
1057      our little mind.  */
1058   if (gimple_bb (defa) != bb)
1059     return false;
1060
1061   FOR_EACH_IMM_USE_FAST (use, imm_iter, result)
1062     {
1063       gimple use_stmt = USE_STMT (use);
1064       if (is_gimple_debug (use_stmt))
1065         continue;
1066       /* Now, if there's a use of RESULT that lies outside this basic block,
1067          then there surely is a conflict with ARG.  */
1068       if (gimple_bb (use_stmt) != bb)
1069         return true;
1070       if (gimple_code (use_stmt) == GIMPLE_PHI)
1071         continue;
1072       /* The use now is in a real stmt of BB, so if ARG was defined
1073          in a PHI node (like RESULT) both conflict.  */
1074       if (gimple_code (defa) == GIMPLE_PHI)
1075         return true;
1076       maybe_renumber_stmts_bb (bb);
1077       /* If the use of RESULT occurs after the definition of ARG,
1078          the two conflict too.  */
1079       if (gimple_uid (defa) < gimple_uid (use_stmt))
1080         return true;
1081     }
1082
1083   return false;
1084 }
1085
1086
1087 /* Search every PHI node for arguments associated with backedges which
1088    we can trivially determine will need a copy (the argument is either
1089    not an SSA_NAME or the argument has a different underlying variable
1090    than the PHI result).
1091
1092    Insert a copy from the PHI argument to a new destination at the
1093    end of the block with the backedge to the top of the loop.  Update
1094    the PHI argument to reference this new destination.  */
1095
1096 static void
1097 insert_backedge_copies (void)
1098 {
1099   basic_block bb;
1100   gphi_iterator gsi;
1101
1102   mark_dfs_back_edges ();
1103
1104   FOR_EACH_BB_FN (bb, cfun)
1105     {
1106       /* Mark block as possibly needing calculation of UIDs.  */
1107       bb->aux = &bb->aux;
1108
1109       for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1110         {
1111           gphi *phi = gsi.phi ();
1112           tree result = gimple_phi_result (phi);
1113           size_t i;
1114
1115           if (virtual_operand_p (result))
1116             continue;
1117
1118           for (i = 0; i < gimple_phi_num_args (phi); i++)
1119             {
1120               tree arg = gimple_phi_arg_def (phi, i);
1121               edge e = gimple_phi_arg_edge (phi, i);
1122
1123               /* If the argument is not an SSA_NAME, then we will need a
1124                  constant initialization.  If the argument is an SSA_NAME with
1125                  a different underlying variable then a copy statement will be
1126                  needed.  */
1127               if ((e->flags & EDGE_DFS_BACK)
1128                   && (TREE_CODE (arg) != SSA_NAME
1129                       || SSA_NAME_VAR (arg) != SSA_NAME_VAR (result)
1130                       || trivially_conflicts_p (bb, result, arg)))
1131                 {
1132                   tree name;
1133                   gassign *stmt;
1134                   gimple last = NULL;
1135                   gimple_stmt_iterator gsi2;
1136
1137                   gsi2 = gsi_last_bb (gimple_phi_arg_edge (phi, i)->src);
1138                   if (!gsi_end_p (gsi2))
1139                     last = gsi_stmt (gsi2);
1140
1141                   /* In theory the only way we ought to get back to the
1142                      start of a loop should be with a COND_EXPR or GOTO_EXPR.
1143                      However, better safe than sorry.
1144                      If the block ends with a control statement or
1145                      something that might throw, then we have to
1146                      insert this assignment before the last
1147                      statement.  Else insert it after the last statement.  */
1148                   if (last && stmt_ends_bb_p (last))
1149                     {
1150                       /* If the last statement in the block is the definition
1151                          site of the PHI argument, then we can't insert
1152                          anything after it.  */
1153                       if (TREE_CODE (arg) == SSA_NAME
1154                           && SSA_NAME_DEF_STMT (arg) == last)
1155                         continue;
1156                     }
1157
1158                   /* Create a new instance of the underlying variable of the
1159                      PHI result.  */
1160                   name = copy_ssa_name (result);
1161                   stmt = gimple_build_assign (name,
1162                                               gimple_phi_arg_def (phi, i));
1163
1164                   /* copy location if present.  */
1165                   if (gimple_phi_arg_has_location (phi, i))
1166                     gimple_set_location (stmt,
1167                                          gimple_phi_arg_location (phi, i));
1168
1169                   /* Insert the new statement into the block and update
1170                      the PHI node.  */
1171                   if (last && stmt_ends_bb_p (last))
1172                     gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
1173                   else
1174                     gsi_insert_after (&gsi2, stmt, GSI_NEW_STMT);
1175                   SET_PHI_ARG_DEF (phi, i, name);
1176                 }
1177             }
1178         }
1179
1180       /* Unmark this block again.  */
1181       bb->aux = NULL;
1182     }
1183 }
1184
1185 /* Free all memory associated with going out of SSA form.  SA is
1186    the outof-SSA info object.  */
1187
1188 void
1189 finish_out_of_ssa (struct ssaexpand *sa)
1190 {
1191   free (sa->partition_to_pseudo);
1192   if (sa->values)
1193     BITMAP_FREE (sa->values);
1194   delete_var_map (sa->map);
1195   BITMAP_FREE (sa->partition_has_default_def);
1196   memset (sa, 0, sizeof *sa);
1197 }
1198
1199 /* Take the current function out of SSA form, translating PHIs as described in
1200    R. Morgan, ``Building an Optimizing Compiler'',
1201    Butterworth-Heinemann, Boston, MA, 1998. pp 176-186.  */
1202
1203 unsigned int
1204 rewrite_out_of_ssa (struct ssaexpand *sa)
1205 {
1206   /* If elimination of a PHI requires inserting a copy on a backedge,
1207      then we will have to split the backedge which has numerous
1208      undesirable performance effects.
1209
1210      A significant number of such cases can be handled here by inserting
1211      copies into the loop itself.  */
1212   insert_backedge_copies ();
1213
1214
1215   /* Eliminate PHIs which are of no use, such as virtual or dead phis.  */
1216   eliminate_useless_phis ();
1217
1218   if (dump_file && (dump_flags & TDF_DETAILS))
1219     gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1220
1221   remove_ssa_form (flag_tree_ter, sa);
1222
1223   if (dump_file && (dump_flags & TDF_DETAILS))
1224     gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1225
1226   return 0;
1227 }