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