2007-03-01 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / tree-outof-ssa.c
blobad8c5ae029836322f45730cab72dfed2220d86b3
1 /* Convert a program in SSA form into Normal form.
2 Copyright (C) 2004, 2005, 2006 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 2, 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 COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "ggc.h"
28 #include "basic-block.h"
29 #include "diagnostic.h"
30 #include "bitmap.h"
31 #include "tree-flow.h"
32 #include "timevar.h"
33 #include "tree-dump.h"
34 #include "tree-ssa-live.h"
35 #include "tree-pass.h"
36 #include "toplev.h"
39 /* Used to hold all the components required to do SSA PHI elimination.
40 The node and pred/succ list is a simple linear list of nodes and
41 edges represented as pairs of nodes.
43 The predecessor and successor list: Nodes are entered in pairs, where
44 [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
45 predecessors, all the odd elements are successors.
47 Rationale:
48 When implemented as bitmaps, very large programs SSA->Normal times were
49 being dominated by clearing the interference graph.
51 Typically this list of edges is extremely small since it only includes
52 PHI results and uses from a single edge which have not coalesced with
53 each other. This means that no virtual PHI nodes are included, and
54 empirical evidence suggests that the number of edges rarely exceed
55 3, and in a bootstrap of GCC, the maximum size encountered was 7.
56 This also limits the number of possible nodes that are involved to
57 rarely more than 6, and in the bootstrap of gcc, the maximum number
58 of nodes encountered was 12. */
60 typedef struct _elim_graph {
61 /* Size of the elimination vectors. */
62 int size;
64 /* List of nodes in the elimination graph. */
65 VEC(tree,heap) *nodes;
67 /* The predecessor and successor edge list. */
68 VEC(int,heap) *edge_list;
70 /* Visited vector. */
71 sbitmap visited;
73 /* Stack for visited nodes. */
74 VEC(int,heap) *stack;
76 /* The variable partition map. */
77 var_map map;
79 /* Edge being eliminated by this graph. */
80 edge e;
82 /* List of constant copies to emit. These are pushed on in pairs. */
83 VEC(tree,heap) *const_copies;
84 } *elim_graph;
87 /* Create a temporary variable based on the type of variable T. Use T's name
88 as the prefix. */
90 static tree
91 create_temp (tree t)
93 tree tmp;
94 const char *name = NULL;
95 tree type;
97 if (TREE_CODE (t) == SSA_NAME)
98 t = SSA_NAME_VAR (t);
100 gcc_assert (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL);
102 type = TREE_TYPE (t);
103 tmp = DECL_NAME (t);
104 if (tmp)
105 name = IDENTIFIER_POINTER (tmp);
107 if (name == NULL)
108 name = "temp";
109 tmp = create_tmp_var (type, name);
111 if (DECL_DEBUG_EXPR_IS_FROM (t) && DECL_DEBUG_EXPR (t))
113 SET_DECL_DEBUG_EXPR (tmp, DECL_DEBUG_EXPR (t));
114 DECL_DEBUG_EXPR_IS_FROM (tmp) = 1;
116 else if (!DECL_IGNORED_P (t))
118 SET_DECL_DEBUG_EXPR (tmp, t);
119 DECL_DEBUG_EXPR_IS_FROM (tmp) = 1;
121 DECL_ARTIFICIAL (tmp) = DECL_ARTIFICIAL (t);
122 DECL_IGNORED_P (tmp) = DECL_IGNORED_P (t);
123 add_referenced_var (tmp);
125 /* add_referenced_var will create the annotation and set up some
126 of the flags in the annotation. However, some flags we need to
127 inherit from our original variable. */
128 set_symbol_mem_tag (tmp, symbol_mem_tag (t));
129 if (is_call_clobbered (t))
130 mark_call_clobbered (tmp, var_ann (t)->escape_mask);
132 return tmp;
136 /* This helper function fill insert a copy from a constant or variable SRC to
137 variable DEST on edge E. */
139 static void
140 insert_copy_on_edge (edge e, tree dest, tree src)
142 tree copy;
144 copy = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (dest), dest, src);
145 set_is_used (dest);
147 if (TREE_CODE (src) == ADDR_EXPR)
148 src = TREE_OPERAND (src, 0);
149 if (TREE_CODE (src) == VAR_DECL || TREE_CODE (src) == PARM_DECL)
150 set_is_used (src);
152 if (dump_file && (dump_flags & TDF_DETAILS))
154 fprintf (dump_file,
155 "Inserting a copy on edge BB%d->BB%d :",
156 e->src->index,
157 e->dest->index);
158 print_generic_expr (dump_file, copy, dump_flags);
159 fprintf (dump_file, "\n");
162 bsi_insert_on_edge (e, copy);
166 /* Create an elimination graph with SIZE nodes and associated data
167 structures. */
169 static elim_graph
170 new_elim_graph (int size)
172 elim_graph g = (elim_graph) xmalloc (sizeof (struct _elim_graph));
174 g->nodes = VEC_alloc (tree, heap, 30);
175 g->const_copies = VEC_alloc (tree, heap, 20);
176 g->edge_list = VEC_alloc (int, heap, 20);
177 g->stack = VEC_alloc (int, heap, 30);
179 g->visited = sbitmap_alloc (size);
181 return g;
185 /* Empty elimination graph G. */
187 static inline void
188 clear_elim_graph (elim_graph g)
190 VEC_truncate (tree, g->nodes, 0);
191 VEC_truncate (int, g->edge_list, 0);
195 /* Delete elimination graph G. */
197 static inline void
198 delete_elim_graph (elim_graph g)
200 sbitmap_free (g->visited);
201 VEC_free (int, heap, g->stack);
202 VEC_free (int, heap, g->edge_list);
203 VEC_free (tree, heap, g->const_copies);
204 VEC_free (tree, heap, g->nodes);
205 free (g);
209 /* Return the number of nodes in graph G. */
211 static inline int
212 elim_graph_size (elim_graph g)
214 return VEC_length (tree, g->nodes);
218 /* Add NODE to graph G, if it doesn't exist already. */
220 static inline void
221 elim_graph_add_node (elim_graph g, tree node)
223 int x;
224 tree t;
226 for (x = 0; VEC_iterate (tree, g->nodes, x, t); x++)
227 if (t == node)
228 return;
229 VEC_safe_push (tree, heap, g->nodes, node);
233 /* Add the edge PRED->SUCC to graph G. */
235 static inline void
236 elim_graph_add_edge (elim_graph g, int pred, int succ)
238 VEC_safe_push (int, heap, g->edge_list, pred);
239 VEC_safe_push (int, heap, g->edge_list, succ);
243 /* Remove an edge from graph G for which NODE is the predecessor, and
244 return the successor node. -1 is returned if there is no such edge. */
246 static inline int
247 elim_graph_remove_succ_edge (elim_graph g, int node)
249 int y;
250 unsigned x;
251 for (x = 0; x < VEC_length (int, g->edge_list); x += 2)
252 if (VEC_index (int, g->edge_list, x) == node)
254 VEC_replace (int, g->edge_list, x, -1);
255 y = VEC_index (int, g->edge_list, x + 1);
256 VEC_replace (int, g->edge_list, x + 1, -1);
257 return y;
259 return -1;
263 /* Find all the nodes in GRAPH which are successors to NODE in the
264 edge list. VAR will hold the partition number found. CODE is the
265 code fragment executed for every node found. */
267 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, CODE) \
268 do { \
269 unsigned x_; \
270 int y_; \
271 for (x_ = 0; x_ < VEC_length (int, (GRAPH)->edge_list); x_ += 2) \
273 y_ = VEC_index (int, (GRAPH)->edge_list, x_); \
274 if (y_ != (NODE)) \
275 continue; \
276 (VAR) = VEC_index (int, (GRAPH)->edge_list, x_ + 1); \
277 CODE; \
279 } while (0)
282 /* Find all the nodes which are predecessors of NODE in the edge list for
283 GRAPH. VAR will hold the partition number found. CODE is the
284 code fragment executed for every node found. */
286 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, CODE) \
287 do { \
288 unsigned x_; \
289 int y_; \
290 for (x_ = 0; x_ < VEC_length (int, (GRAPH)->edge_list); x_ += 2) \
292 y_ = VEC_index (int, (GRAPH)->edge_list, x_ + 1); \
293 if (y_ != (NODE)) \
294 continue; \
295 (VAR) = VEC_index (int, (GRAPH)->edge_list, x_); \
296 CODE; \
298 } while (0)
301 /* Add T to elimination graph G. */
303 static inline void
304 eliminate_name (elim_graph g, tree T)
306 elim_graph_add_node (g, T);
310 /* Build elimination graph G for basic block BB on incoming PHI edge
311 G->e. */
313 static void
314 eliminate_build (elim_graph g, basic_block B)
316 tree phi;
317 tree T0, Ti;
318 int p0, pi;
320 clear_elim_graph (g);
322 for (phi = phi_nodes (B); phi; phi = PHI_CHAIN (phi))
324 T0 = var_to_partition_to_var (g->map, PHI_RESULT (phi));
326 /* Ignore results which are not in partitions. */
327 if (T0 == NULL_TREE)
328 continue;
330 Ti = PHI_ARG_DEF (phi, g->e->dest_idx);
332 /* If this argument is a constant, or a SSA_NAME which is being
333 left in SSA form, just queue a copy to be emitted on this
334 edge. */
335 if (!phi_ssa_name_p (Ti)
336 || (TREE_CODE (Ti) == SSA_NAME
337 && var_to_partition (g->map, Ti) == NO_PARTITION))
339 /* Save constant copies until all other copies have been emitted
340 on this edge. */
341 VEC_safe_push (tree, heap, g->const_copies, T0);
342 VEC_safe_push (tree, heap, g->const_copies, Ti);
344 else
346 Ti = var_to_partition_to_var (g->map, Ti);
347 if (T0 != Ti)
349 eliminate_name (g, T0);
350 eliminate_name (g, Ti);
351 p0 = var_to_partition (g->map, T0);
352 pi = var_to_partition (g->map, Ti);
353 elim_graph_add_edge (g, p0, pi);
360 /* Push successors of T onto the elimination stack for G. */
362 static void
363 elim_forward (elim_graph g, int T)
365 int S;
366 SET_BIT (g->visited, T);
367 FOR_EACH_ELIM_GRAPH_SUCC (g, T, S,
369 if (!TEST_BIT (g->visited, S))
370 elim_forward (g, S);
372 VEC_safe_push (int, heap, g->stack, T);
376 /* Return 1 if there unvisited predecessors of T in graph G. */
378 static int
379 elim_unvisited_predecessor (elim_graph g, int T)
381 int P;
382 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
384 if (!TEST_BIT (g->visited, P))
385 return 1;
387 return 0;
390 /* Process predecessors first, and insert a copy. */
392 static void
393 elim_backward (elim_graph g, int T)
395 int P;
396 SET_BIT (g->visited, T);
397 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
399 if (!TEST_BIT (g->visited, P))
401 elim_backward (g, P);
402 insert_copy_on_edge (g->e,
403 partition_to_var (g->map, P),
404 partition_to_var (g->map, T));
409 /* Insert required copies for T in graph G. Check for a strongly connected
410 region, and create a temporary to break the cycle if one is found. */
412 static void
413 elim_create (elim_graph g, int T)
415 tree U;
416 int P, S;
418 if (elim_unvisited_predecessor (g, T))
420 U = create_temp (partition_to_var (g->map, T));
421 insert_copy_on_edge (g->e, U, partition_to_var (g->map, T));
422 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
424 if (!TEST_BIT (g->visited, P))
426 elim_backward (g, P);
427 insert_copy_on_edge (g->e, partition_to_var (g->map, P), U);
431 else
433 S = elim_graph_remove_succ_edge (g, T);
434 if (S != -1)
436 SET_BIT (g->visited, T);
437 insert_copy_on_edge (g->e,
438 partition_to_var (g->map, T),
439 partition_to_var (g->map, S));
446 /* Eliminate all the phi nodes on edge E in graph G. */
448 static void
449 eliminate_phi (edge e, elim_graph g)
451 int x;
452 basic_block B = e->dest;
454 gcc_assert (VEC_length (tree, g->const_copies) == 0);
456 /* Abnormal edges already have everything coalesced. */
457 if (e->flags & EDGE_ABNORMAL)
458 return;
460 g->e = e;
462 eliminate_build (g, B);
464 if (elim_graph_size (g) != 0)
466 tree var;
468 sbitmap_zero (g->visited);
469 VEC_truncate (int, g->stack, 0);
471 for (x = 0; VEC_iterate (tree, g->nodes, x, var); x++)
473 int p = var_to_partition (g->map, var);
474 if (!TEST_BIT (g->visited, p))
475 elim_forward (g, p);
478 sbitmap_zero (g->visited);
479 while (VEC_length (int, g->stack) > 0)
481 x = VEC_pop (int, g->stack);
482 if (!TEST_BIT (g->visited, x))
483 elim_create (g, x);
487 /* If there are any pending constant copies, issue them now. */
488 while (VEC_length (tree, g->const_copies) > 0)
490 tree src, dest;
491 src = VEC_pop (tree, g->const_copies);
492 dest = VEC_pop (tree, g->const_copies);
493 insert_copy_on_edge (e, dest, src);
498 /* Take the ssa-name var_map MAP, and assign real variables to each
499 partition. */
501 static void
502 assign_vars (var_map map)
504 int x, num;
505 tree var, root;
506 var_ann_t ann;
508 num = num_var_partitions (map);
509 for (x = 0; x < num; x++)
511 var = partition_to_var (map, x);
512 if (TREE_CODE (var) != SSA_NAME)
514 ann = var_ann (var);
515 /* It must already be coalesced. */
516 gcc_assert (ann->out_of_ssa_tag == 1);
517 if (dump_file && (dump_flags & TDF_DETAILS))
519 fprintf (dump_file, "partition %d already has variable ", x);
520 print_generic_expr (dump_file, var, TDF_SLIM);
521 fprintf (dump_file, " assigned to it.\n");
524 else
526 root = SSA_NAME_VAR (var);
527 ann = var_ann (root);
528 /* If ROOT is already associated, create a new one. */
529 if (ann->out_of_ssa_tag)
531 root = create_temp (root);
532 ann = var_ann (root);
534 /* ROOT has not been coalesced yet, so use it. */
535 if (dump_file && (dump_flags & TDF_DETAILS))
537 fprintf (dump_file, "Partition %d is assigned to var ", x);
538 print_generic_stmt (dump_file, root, TDF_SLIM);
540 change_partition_var (map, root, x);
546 /* Replace use operand P with whatever variable it has been rewritten to based
547 on the partitions in MAP. EXPR is an optional expression vector over SSA
548 versions which is used to replace P with an expression instead of a variable.
549 If the stmt is changed, return true. */
551 static inline bool
552 replace_use_variable (var_map map, use_operand_p p, tree *expr)
554 tree new_var;
555 tree var = USE_FROM_PTR (p);
557 /* Check if we are replacing this variable with an expression. */
558 if (expr)
560 int version = SSA_NAME_VERSION (var);
561 if (expr[version])
563 tree new_expr = GIMPLE_STMT_OPERAND (expr[version], 1);
564 SET_USE (p, new_expr);
566 /* Clear the stmt's RHS, or GC might bite us. */
567 GIMPLE_STMT_OPERAND (expr[version], 1) = NULL_TREE;
568 return true;
572 new_var = var_to_partition_to_var (map, var);
573 if (new_var)
575 SET_USE (p, new_var);
576 set_is_used (new_var);
577 return true;
579 return false;
583 /* Replace def operand DEF_P with whatever variable it has been rewritten to
584 based on the partitions in MAP. EXPR is an optional expression vector over
585 SSA versions which is used to replace DEF_P with an expression instead of a
586 variable. If the stmt is changed, return true. */
588 static inline bool
589 replace_def_variable (var_map map, def_operand_p def_p, tree *expr)
591 tree new_var;
592 tree var = DEF_FROM_PTR (def_p);
594 /* Do nothing if we are replacing this variable with an expression. */
595 if (expr && expr[SSA_NAME_VERSION (var)])
596 return true;
598 new_var = var_to_partition_to_var (map, var);
599 if (new_var)
601 SET_DEF (def_p, new_var);
602 set_is_used (new_var);
603 return true;
605 return false;
609 /* Remove any PHI node which is a virtual PHI. */
611 static void
612 eliminate_virtual_phis (void)
614 basic_block bb;
615 tree phi, next;
617 FOR_EACH_BB (bb)
619 for (phi = phi_nodes (bb); phi; phi = next)
621 next = PHI_CHAIN (phi);
622 if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi))))
624 #ifdef ENABLE_CHECKING
625 int i;
626 /* There should be no arguments of this PHI which are in
627 the partition list, or we get incorrect results. */
628 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
630 tree arg = PHI_ARG_DEF (phi, i);
631 if (TREE_CODE (arg) == SSA_NAME
632 && is_gimple_reg (SSA_NAME_VAR (arg)))
634 fprintf (stderr, "Argument of PHI is not virtual (");
635 print_generic_expr (stderr, arg, TDF_SLIM);
636 fprintf (stderr, "), but the result is :");
637 print_generic_stmt (stderr, phi, TDF_SLIM);
638 internal_error ("SSA corruption");
641 #endif
642 remove_phi_node (phi, NULL_TREE, true);
649 /* This function will rewrite the current program using the variable mapping
650 found in MAP. If the replacement vector VALUES is provided, any
651 occurrences of partitions with non-null entries in the vector will be
652 replaced with the expression in the vector instead of its mapped
653 variable. */
655 static void
656 rewrite_trees (var_map map, tree *values)
658 elim_graph g;
659 basic_block bb;
660 block_stmt_iterator si;
661 edge e;
662 tree phi;
663 bool changed;
665 #ifdef ENABLE_CHECKING
666 /* Search for PHIs where the destination has no partition, but one
667 or more arguments has a partition. This should not happen and can
668 create incorrect code. */
669 FOR_EACH_BB (bb)
671 tree phi;
672 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
674 tree T0 = var_to_partition_to_var (map, PHI_RESULT (phi));
675 if (T0 == NULL_TREE)
677 int i;
678 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
680 tree arg = PHI_ARG_DEF (phi, i);
682 if (TREE_CODE (arg) == SSA_NAME
683 && var_to_partition (map, arg) != NO_PARTITION)
685 fprintf (stderr, "Argument of PHI is in a partition :(");
686 print_generic_expr (stderr, arg, TDF_SLIM);
687 fprintf (stderr, "), but the result is not :");
688 print_generic_stmt (stderr, phi, TDF_SLIM);
689 internal_error ("SSA corruption");
695 #endif
697 /* Replace PHI nodes with any required copies. */
698 g = new_elim_graph (map->num_partitions);
699 g->map = map;
700 FOR_EACH_BB (bb)
702 for (si = bsi_start (bb); !bsi_end_p (si); )
704 tree stmt = bsi_stmt (si);
705 use_operand_p use_p, copy_use_p;
706 def_operand_p def_p;
707 bool remove = false, is_copy = false;
708 int num_uses = 0;
709 stmt_ann_t ann;
710 ssa_op_iter iter;
712 ann = stmt_ann (stmt);
713 changed = false;
715 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
716 && (TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == SSA_NAME))
717 is_copy = true;
719 copy_use_p = NULL_USE_OPERAND_P;
720 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
722 if (replace_use_variable (map, use_p, values))
723 changed = true;
724 copy_use_p = use_p;
725 num_uses++;
728 if (num_uses != 1)
729 is_copy = false;
731 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
733 if (def_p != NULL)
735 /* Mark this stmt for removal if it is the list of replaceable
736 expressions. */
737 if (values && values[SSA_NAME_VERSION (DEF_FROM_PTR (def_p))])
738 remove = true;
739 else
741 if (replace_def_variable (map, def_p, NULL))
742 changed = true;
743 /* If both SSA_NAMEs coalesce to the same variable,
744 mark the now redundant copy for removal. */
745 if (is_copy)
747 gcc_assert (copy_use_p != NULL_USE_OPERAND_P);
748 if (DEF_FROM_PTR (def_p) == USE_FROM_PTR (copy_use_p))
749 remove = true;
753 else
754 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_DEF)
755 if (replace_def_variable (map, def_p, NULL))
756 changed = true;
758 /* Remove any stmts marked for removal. */
759 if (remove)
760 bsi_remove (&si, true);
761 else
762 bsi_next (&si);
765 phi = phi_nodes (bb);
766 if (phi)
768 edge_iterator ei;
769 FOR_EACH_EDGE (e, ei, bb->preds)
770 eliminate_phi (e, g);
774 delete_elim_graph (g);
777 /* These are the local work structures used to determine the best place to
778 insert the copies that were placed on edges by the SSA->normal pass.. */
779 static VEC(edge,heap) *edge_leader;
780 static VEC(tree,heap) *stmt_list;
781 static bitmap leader_has_match = NULL;
782 static edge leader_match = NULL;
785 /* Pass this function to make_forwarder_block so that all the edges with
786 matching PENDING_STMT lists to 'curr_stmt_list' get redirected. E is the
787 edge to test for a match. */
789 static inline bool
790 same_stmt_list_p (edge e)
792 return (e->aux == (PTR) leader_match) ? true : false;
796 /* Return TRUE if S1 and S2 are equivalent copies. */
798 static inline bool
799 identical_copies_p (tree s1, tree s2)
801 #ifdef ENABLE_CHECKING
802 gcc_assert (TREE_CODE (s1) == GIMPLE_MODIFY_STMT);
803 gcc_assert (TREE_CODE (s2) == GIMPLE_MODIFY_STMT);
804 gcc_assert (DECL_P (GIMPLE_STMT_OPERAND (s1, 0)));
805 gcc_assert (DECL_P (GIMPLE_STMT_OPERAND (s2, 0)));
806 #endif
808 if (GIMPLE_STMT_OPERAND (s1, 0) != GIMPLE_STMT_OPERAND (s2, 0))
809 return false;
811 s1 = GIMPLE_STMT_OPERAND (s1, 1);
812 s2 = GIMPLE_STMT_OPERAND (s2, 1);
814 if (s1 != s2)
815 return false;
817 return true;
821 /* Compare the PENDING_STMT list for edges E1 and E2. Return true if the lists
822 contain the same sequence of copies. */
824 static inline bool
825 identical_stmt_lists_p (edge e1, edge e2)
827 tree t1 = PENDING_STMT (e1);
828 tree t2 = PENDING_STMT (e2);
829 tree_stmt_iterator tsi1, tsi2;
831 gcc_assert (TREE_CODE (t1) == STATEMENT_LIST);
832 gcc_assert (TREE_CODE (t2) == STATEMENT_LIST);
834 for (tsi1 = tsi_start (t1), tsi2 = tsi_start (t2);
835 !tsi_end_p (tsi1) && !tsi_end_p (tsi2);
836 tsi_next (&tsi1), tsi_next (&tsi2))
838 if (!identical_copies_p (tsi_stmt (tsi1), tsi_stmt (tsi2)))
839 break;
842 if (!tsi_end_p (tsi1) || ! tsi_end_p (tsi2))
843 return false;
845 return true;
849 /* Allocate data structures used in analyze_edges_for_bb. */
851 static void
852 init_analyze_edges_for_bb (void)
854 edge_leader = VEC_alloc (edge, heap, 25);
855 stmt_list = VEC_alloc (tree, heap, 25);
856 leader_has_match = BITMAP_ALLOC (NULL);
860 /* Free data structures used in analyze_edges_for_bb. */
862 static void
863 fini_analyze_edges_for_bb (void)
865 VEC_free (edge, heap, edge_leader);
866 VEC_free (tree, heap, stmt_list);
867 BITMAP_FREE (leader_has_match);
871 /* Look at all the incoming edges to block BB, and decide where the best place
872 to insert the stmts on each edge are, and perform those insertions. */
874 static void
875 analyze_edges_for_bb (basic_block bb)
877 edge e;
878 edge_iterator ei;
879 int count;
880 unsigned int x;
881 bool have_opportunity;
882 block_stmt_iterator bsi;
883 tree stmt;
884 edge single_edge = NULL;
885 bool is_label;
886 edge leader;
888 count = 0;
890 /* Blocks which contain at least one abnormal edge cannot use
891 make_forwarder_block. Look for these blocks, and commit any PENDING_STMTs
892 found on edges in these block. */
893 have_opportunity = true;
894 FOR_EACH_EDGE (e, ei, bb->preds)
895 if (e->flags & EDGE_ABNORMAL)
897 have_opportunity = false;
898 break;
901 if (!have_opportunity)
903 FOR_EACH_EDGE (e, ei, bb->preds)
904 if (PENDING_STMT (e))
905 bsi_commit_one_edge_insert (e, NULL);
906 return;
909 /* Find out how many edges there are with interesting pending stmts on them.
910 Commit the stmts on edges we are not interested in. */
911 FOR_EACH_EDGE (e, ei, bb->preds)
913 if (PENDING_STMT (e))
915 gcc_assert (!(e->flags & EDGE_ABNORMAL));
916 if (e->flags & EDGE_FALLTHRU)
918 bsi = bsi_start (e->src);
919 if (!bsi_end_p (bsi))
921 stmt = bsi_stmt (bsi);
922 bsi_next (&bsi);
923 gcc_assert (stmt != NULL_TREE);
924 is_label = (TREE_CODE (stmt) == LABEL_EXPR);
925 /* Punt if it has non-label stmts, or isn't local. */
926 if (!is_label || DECL_NONLOCAL (TREE_OPERAND (stmt, 0))
927 || !bsi_end_p (bsi))
929 bsi_commit_one_edge_insert (e, NULL);
930 continue;
934 single_edge = e;
935 count++;
939 /* If there aren't at least 2 edges, no sharing will happen. */
940 if (count < 2)
942 if (single_edge)
943 bsi_commit_one_edge_insert (single_edge, NULL);
944 return;
947 /* Ensure that we have empty worklists. */
948 #ifdef ENABLE_CHECKING
949 gcc_assert (VEC_length (edge, edge_leader) == 0);
950 gcc_assert (VEC_length (tree, stmt_list) == 0);
951 gcc_assert (bitmap_empty_p (leader_has_match));
952 #endif
954 /* Find the "leader" block for each set of unique stmt lists. Preference is
955 given to FALLTHRU blocks since they would need a GOTO to arrive at another
956 block. The leader edge destination is the block which all the other edges
957 with the same stmt list will be redirected to. */
958 have_opportunity = false;
959 FOR_EACH_EDGE (e, ei, bb->preds)
961 if (PENDING_STMT (e))
963 bool found = false;
965 /* Look for the same stmt list in edge leaders list. */
966 for (x = 0; VEC_iterate (edge, edge_leader, x, leader); x++)
968 if (identical_stmt_lists_p (leader, e))
970 /* Give this edge the same stmt list pointer. */
971 PENDING_STMT (e) = NULL;
972 e->aux = leader;
973 bitmap_set_bit (leader_has_match, x);
974 have_opportunity = found = true;
975 break;
979 /* If no similar stmt list, add this edge to the leader list. */
980 if (!found)
982 VEC_safe_push (edge, heap, edge_leader, e);
983 VEC_safe_push (tree, heap, stmt_list, PENDING_STMT (e));
988 /* If there are no similar lists, just issue the stmts. */
989 if (!have_opportunity)
991 for (x = 0; VEC_iterate (edge, edge_leader, x, leader); x++)
992 bsi_commit_one_edge_insert (leader, NULL);
993 VEC_truncate (edge, edge_leader, 0);
994 VEC_truncate (tree, stmt_list, 0);
995 bitmap_clear (leader_has_match);
996 return;
999 if (dump_file)
1000 fprintf (dump_file, "\nOpportunities in BB %d for stmt/block reduction:\n",
1001 bb->index);
1003 /* For each common list, create a forwarding block and issue the stmt's
1004 in that block. */
1005 for (x = 0; VEC_iterate (edge, edge_leader, x, leader); x++)
1006 if (bitmap_bit_p (leader_has_match, x))
1008 edge new_edge;
1009 block_stmt_iterator bsi;
1010 tree curr_stmt_list;
1012 leader_match = leader;
1014 /* The tree_* cfg manipulation routines use the PENDING_EDGE field
1015 for various PHI manipulations, so it gets cleared when calls are
1016 made to make_forwarder_block(). So make sure the edge is clear,
1017 and use the saved stmt list. */
1018 PENDING_STMT (leader) = NULL;
1019 leader->aux = leader;
1020 curr_stmt_list = VEC_index (tree, stmt_list, x);
1022 new_edge = make_forwarder_block (leader->dest, same_stmt_list_p,
1023 NULL);
1024 bb = new_edge->dest;
1025 if (dump_file)
1027 fprintf (dump_file, "Splitting BB %d for Common stmt list. ",
1028 leader->dest->index);
1029 fprintf (dump_file, "Original block is now BB%d.\n", bb->index);
1030 print_generic_stmt (dump_file, curr_stmt_list, TDF_VOPS);
1033 FOR_EACH_EDGE (e, ei, new_edge->src->preds)
1035 e->aux = NULL;
1036 if (dump_file)
1037 fprintf (dump_file, " Edge (%d->%d) lands here.\n",
1038 e->src->index, e->dest->index);
1041 bsi = bsi_last (leader->dest);
1042 bsi_insert_after (&bsi, curr_stmt_list, BSI_NEW_STMT);
1044 leader_match = NULL;
1045 /* We should never get a new block now. */
1047 else
1049 PENDING_STMT (leader) = VEC_index (tree, stmt_list, x);
1050 bsi_commit_one_edge_insert (leader, NULL);
1054 /* Clear the working data structures. */
1055 VEC_truncate (edge, edge_leader, 0);
1056 VEC_truncate (tree, stmt_list, 0);
1057 bitmap_clear (leader_has_match);
1061 /* This function will analyze the insertions which were performed on edges,
1062 and decide whether they should be left on that edge, or whether it is more
1063 efficient to emit some subset of them in a single block. All stmts are
1064 inserted somewhere. */
1066 static void
1067 perform_edge_inserts (void)
1069 basic_block bb;
1071 if (dump_file)
1072 fprintf(dump_file, "Analyzing Edge Insertions.\n");
1074 /* analyze_edges_for_bb calls make_forwarder_block, which tries to
1075 incrementally update the dominator information. Since we don't
1076 need dominator information after this pass, go ahead and free the
1077 dominator information. */
1078 free_dominance_info (CDI_DOMINATORS);
1079 free_dominance_info (CDI_POST_DOMINATORS);
1081 /* Allocate data structures used in analyze_edges_for_bb. */
1082 init_analyze_edges_for_bb ();
1084 FOR_EACH_BB (bb)
1085 analyze_edges_for_bb (bb);
1087 analyze_edges_for_bb (EXIT_BLOCK_PTR);
1089 /* Free data structures used in analyze_edges_for_bb. */
1090 fini_analyze_edges_for_bb ();
1092 #ifdef ENABLE_CHECKING
1094 edge_iterator ei;
1095 edge e;
1096 FOR_EACH_BB (bb)
1098 FOR_EACH_EDGE (e, ei, bb->preds)
1100 if (PENDING_STMT (e))
1101 error (" Pending stmts not issued on PRED edge (%d, %d)\n",
1102 e->src->index, e->dest->index);
1104 FOR_EACH_EDGE (e, ei, bb->succs)
1106 if (PENDING_STMT (e))
1107 error (" Pending stmts not issued on SUCC edge (%d, %d)\n",
1108 e->src->index, e->dest->index);
1111 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
1113 if (PENDING_STMT (e))
1114 error (" Pending stmts not issued on ENTRY edge (%d, %d)\n",
1115 e->src->index, e->dest->index);
1117 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
1119 if (PENDING_STMT (e))
1120 error (" Pending stmts not issued on EXIT edge (%d, %d)\n",
1121 e->src->index, e->dest->index);
1124 #endif
1128 /* Remove the ssa-names in the current function and translate them into normal
1129 compiler variables. PERFORM_TER is true if Temporary Expression Replacement
1130 should also be used. */
1132 static void
1133 remove_ssa_form (bool perform_ter)
1135 basic_block bb;
1136 tree phi, next;
1137 tree *values = NULL;
1138 var_map map;
1140 map = coalesce_ssa_name ();
1142 /* Return to viewing the variable list as just all reference variables after
1143 coalescing has been performed. */
1144 partition_view_normal (map, false);
1146 if (dump_file && (dump_flags & TDF_DETAILS))
1148 fprintf (dump_file, "After Coalescing:\n");
1149 dump_var_map (dump_file, map);
1152 if (perform_ter)
1154 values = find_replaceable_exprs (map);
1155 if (values && dump_file && (dump_flags & TDF_DETAILS))
1156 dump_replaceable_exprs (dump_file, values);
1159 /* Assign real variables to the partitions now. */
1160 assign_vars (map);
1162 if (dump_file && (dump_flags & TDF_DETAILS))
1164 fprintf (dump_file, "After Base variable replacement:\n");
1165 dump_var_map (dump_file, map);
1168 rewrite_trees (map, values);
1170 if (values)
1171 free (values);
1173 /* Remove PHI nodes which have been translated back to real variables. */
1174 FOR_EACH_BB (bb)
1176 for (phi = phi_nodes (bb); phi; phi = next)
1178 next = PHI_CHAIN (phi);
1179 remove_phi_node (phi, NULL_TREE, true);
1183 /* If any copies were inserted on edges, analyze and insert them now. */
1184 perform_edge_inserts ();
1186 delete_var_map (map);
1190 /* Search every PHI node for arguments associated with backedges which
1191 we can trivially determine will need a copy (the argument is either
1192 not an SSA_NAME or the argument has a different underlying variable
1193 than the PHI result).
1195 Insert a copy from the PHI argument to a new destination at the
1196 end of the block with the backedge to the top of the loop. Update
1197 the PHI argument to reference this new destination. */
1199 static void
1200 insert_backedge_copies (void)
1202 basic_block bb;
1204 FOR_EACH_BB (bb)
1206 tree phi;
1208 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1210 tree result = PHI_RESULT (phi);
1211 tree result_var;
1212 int i;
1214 if (!is_gimple_reg (result))
1215 continue;
1217 result_var = SSA_NAME_VAR (result);
1218 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1220 tree arg = PHI_ARG_DEF (phi, i);
1221 edge e = PHI_ARG_EDGE (phi, i);
1223 /* If the argument is not an SSA_NAME, then we will need a
1224 constant initialization. If the argument is an SSA_NAME with
1225 a different underlying variable then a copy statement will be
1226 needed. */
1227 if ((e->flags & EDGE_DFS_BACK)
1228 && (TREE_CODE (arg) != SSA_NAME
1229 || SSA_NAME_VAR (arg) != result_var))
1231 tree stmt, name, last = NULL;
1232 block_stmt_iterator bsi;
1234 bsi = bsi_last (PHI_ARG_EDGE (phi, i)->src);
1235 if (!bsi_end_p (bsi))
1236 last = bsi_stmt (bsi);
1238 /* In theory the only way we ought to get back to the
1239 start of a loop should be with a COND_EXPR or GOTO_EXPR.
1240 However, better safe than sorry.
1241 If the block ends with a control statement or
1242 something that might throw, then we have to
1243 insert this assignment before the last
1244 statement. Else insert it after the last statement. */
1245 if (last && stmt_ends_bb_p (last))
1247 /* If the last statement in the block is the definition
1248 site of the PHI argument, then we can't insert
1249 anything after it. */
1250 if (TREE_CODE (arg) == SSA_NAME
1251 && SSA_NAME_DEF_STMT (arg) == last)
1252 continue;
1255 /* Create a new instance of the underlying variable of the
1256 PHI result. */
1257 stmt = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (result_var),
1258 NULL_TREE, PHI_ARG_DEF (phi, i));
1259 name = make_ssa_name (result_var, stmt);
1260 GIMPLE_STMT_OPERAND (stmt, 0) = name;
1262 /* Insert the new statement into the block and update
1263 the PHI node. */
1264 if (last && stmt_ends_bb_p (last))
1265 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
1266 else
1267 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
1268 SET_PHI_ARG_DEF (phi, i, name);
1275 /* Take the current function out of SSA form, translating PHIs as described in
1276 R. Morgan, ``Building an Optimizing Compiler'',
1277 Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
1279 static unsigned int
1280 rewrite_out_of_ssa (void)
1282 /* If elimination of a PHI requires inserting a copy on a backedge,
1283 then we will have to split the backedge which has numerous
1284 undesirable performance effects.
1286 A significant number of such cases can be handled here by inserting
1287 copies into the loop itself. */
1288 insert_backedge_copies ();
1290 eliminate_virtual_phis ();
1292 if (dump_file && (dump_flags & TDF_DETAILS))
1293 dump_tree_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1295 remove_ssa_form (flag_tree_ter && !flag_mudflap);
1297 if (dump_file && (dump_flags & TDF_DETAILS))
1298 dump_tree_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1300 cfun->gimple_df->in_ssa_p = false;
1301 return 0;
1305 /* Define the parameters of the out of SSA pass. */
1307 struct tree_opt_pass pass_del_ssa =
1309 "optimized", /* name */
1310 NULL, /* gate */
1311 rewrite_out_of_ssa, /* execute */
1312 NULL, /* sub */
1313 NULL, /* next */
1314 0, /* static_pass_number */
1315 TV_TREE_SSA_TO_NORMAL, /* tv_id */
1316 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
1317 0, /* properties_provided */
1318 /* ??? If TER is enabled, we also kill gimple. */
1319 PROP_ssa, /* properties_destroyed */
1320 TODO_verify_ssa | TODO_verify_flow
1321 | TODO_verify_stmts, /* todo_flags_start */
1322 TODO_dump_func
1323 | TODO_ggc_collect
1324 | TODO_remove_unused_locals, /* todo_flags_finish */
1325 0 /* letter */