re PR libgcj/18116 (JNI uses dot instead of slash as the package separator)
[official-gcc.git] / gcc / tree-outof-ssa.c
blobc6aa812bc1754d004496e7d33658a9d31b78c535
1 /* Convert a program in SSA form into Normal form.
2 Copyright (C) 2004, 2005 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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "ggc.h"
31 #include "langhooks.h"
32 #include "hard-reg-set.h"
33 #include "basic-block.h"
34 #include "output.h"
35 #include "errors.h"
36 #include "expr.h"
37 #include "function.h"
38 #include "diagnostic.h"
39 #include "bitmap.h"
40 #include "tree-flow.h"
41 #include "tree-gimple.h"
42 #include "tree-inline.h"
43 #include "varray.h"
44 #include "timevar.h"
45 #include "hashtab.h"
46 #include "tree-dump.h"
47 #include "tree-ssa-live.h"
48 #include "tree-pass.h"
50 /* Flags to pass to remove_ssa_form. */
52 #define SSANORM_PERFORM_TER 0x1
53 #define SSANORM_COMBINE_TEMPS 0x2
54 #define SSANORM_REMOVE_ALL_PHIS 0x4
55 #define SSANORM_COALESCE_PARTITIONS 0x8
56 #define SSANORM_USE_COALESCE_LIST 0x10
58 /* Used to hold all the components required to do SSA PHI elimination.
59 The node and pred/succ list is a simple linear list of nodes and
60 edges represented as pairs of nodes.
62 The predecessor and successor list: Nodes are entered in pairs, where
63 [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
64 predecessors, all the odd elements are successors.
66 Rationale:
67 When implemented as bitmaps, very large programs SSA->Normal times were
68 being dominated by clearing the interference graph.
70 Typically this list of edges is extremely small since it only includes
71 PHI results and uses from a single edge which have not coalesced with
72 each other. This means that no virtual PHI nodes are included, and
73 empirical evidence suggests that the number of edges rarely exceed
74 3, and in a bootstrap of GCC, the maximum size encountered was 7.
75 This also limits the number of possible nodes that are involved to
76 rarely more than 6, and in the bootstrap of gcc, the maximum number
77 of nodes encountered was 12. */
79 typedef struct _elim_graph {
80 /* Size of the elimination vectors. */
81 int size;
83 /* List of nodes in the elimination graph. */
84 varray_type nodes;
86 /* The predecessor and successor edge list. */
87 varray_type edge_list;
89 /* Visited vector. */
90 sbitmap visited;
92 /* Stack for visited nodes. */
93 varray_type stack;
95 /* The variable partition map. */
96 var_map map;
98 /* Edge being eliminated by this graph. */
99 edge e;
101 /* List of constant copies to emit. These are pushed on in pairs. */
102 varray_type const_copies;
103 } *elim_graph;
106 /* Local functions. */
107 static tree create_temp (tree);
108 static void insert_copy_on_edge (edge, tree, tree);
109 static elim_graph new_elim_graph (int);
110 static inline void delete_elim_graph (elim_graph);
111 static inline void clear_elim_graph (elim_graph);
112 static inline int elim_graph_size (elim_graph);
113 static inline void elim_graph_add_node (elim_graph, tree);
114 static inline void elim_graph_add_edge (elim_graph, int, int);
115 static inline int elim_graph_remove_succ_edge (elim_graph, int);
117 static inline void eliminate_name (elim_graph, tree);
118 static void eliminate_build (elim_graph, basic_block);
119 static void elim_forward (elim_graph, int);
120 static int elim_unvisited_predecessor (elim_graph, int);
121 static void elim_backward (elim_graph, int);
122 static void elim_create (elim_graph, int);
123 static void eliminate_phi (edge, elim_graph);
124 static tree_live_info_p coalesce_ssa_name (var_map, int);
125 static void assign_vars (var_map);
126 static bool replace_use_variable (var_map, use_operand_p, tree *);
127 static bool replace_def_variable (var_map, def_operand_p, tree *);
128 static void eliminate_virtual_phis (void);
129 static void coalesce_abnormal_edges (var_map, conflict_graph, root_var_p);
130 static void print_exprs (FILE *, const char *, tree, const char *, tree,
131 const char *);
132 static void print_exprs_edge (FILE *, edge, const char *, tree, const char *,
133 tree);
136 /* Create a temporary variable based on the type of variable T. Use T's name
137 as the prefix. */
139 static tree
140 create_temp (tree t)
142 tree tmp;
143 const char *name = NULL;
144 tree type;
146 if (TREE_CODE (t) == SSA_NAME)
147 t = SSA_NAME_VAR (t);
149 gcc_assert (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL);
151 type = TREE_TYPE (t);
152 tmp = DECL_NAME (t);
153 if (tmp)
154 name = IDENTIFIER_POINTER (tmp);
156 if (name == NULL)
157 name = "temp";
158 tmp = create_tmp_var (type, name);
160 if (DECL_DEBUG_EXPR (t) && DECL_DEBUG_EXPR_IS_FROM (t))
162 DECL_DEBUG_EXPR (tmp) = DECL_DEBUG_EXPR (t);
163 DECL_DEBUG_EXPR_IS_FROM (tmp) = 1;
165 else if (!DECL_IGNORED_P (t))
167 DECL_DEBUG_EXPR (tmp) = t;
168 DECL_DEBUG_EXPR_IS_FROM (tmp) = 1;
170 DECL_ARTIFICIAL (tmp) = DECL_ARTIFICIAL (t);
171 DECL_IGNORED_P (tmp) = DECL_IGNORED_P (t);
172 add_referenced_tmp_var (tmp);
174 /* add_referenced_tmp_var will create the annotation and set up some
175 of the flags in the annotation. However, some flags we need to
176 inherit from our original variable. */
177 var_ann (tmp)->type_mem_tag = var_ann (t)->type_mem_tag;
178 if (is_call_clobbered (t))
179 mark_call_clobbered (tmp);
181 return tmp;
185 /* This helper function fill insert a copy from a constant or variable SRC to
186 variable DEST on edge E. */
188 static void
189 insert_copy_on_edge (edge e, tree dest, tree src)
191 tree copy;
193 copy = build (MODIFY_EXPR, TREE_TYPE (dest), dest, src);
194 set_is_used (dest);
196 if (TREE_CODE (src) == ADDR_EXPR)
197 src = TREE_OPERAND (src, 0);
198 if (TREE_CODE (src) == VAR_DECL || TREE_CODE (src) == PARM_DECL)
199 set_is_used (src);
201 if (dump_file && (dump_flags & TDF_DETAILS))
203 fprintf (dump_file,
204 "Inserting a copy on edge BB%d->BB%d :",
205 e->src->index,
206 e->dest->index);
207 print_generic_expr (dump_file, copy, dump_flags);
208 fprintf (dump_file, "\n");
211 bsi_insert_on_edge (e, copy);
215 /* Create an elimination graph with SIZE nodes and associated data
216 structures. */
218 static elim_graph
219 new_elim_graph (int size)
221 elim_graph g = (elim_graph) xmalloc (sizeof (struct _elim_graph));
223 VARRAY_TREE_INIT (g->nodes, 30, "Elimination Node List");
224 VARRAY_TREE_INIT (g->const_copies, 20, "Elimination Constant Copies");
225 VARRAY_INT_INIT (g->edge_list, 20, "Elimination Edge List");
226 VARRAY_INT_INIT (g->stack, 30, " Elimination Stack");
228 g->visited = sbitmap_alloc (size);
230 return g;
234 /* Empty elimination graph G. */
236 static inline void
237 clear_elim_graph (elim_graph g)
239 VARRAY_POP_ALL (g->nodes);
240 VARRAY_POP_ALL (g->edge_list);
244 /* Delete elimination graph G. */
246 static inline void
247 delete_elim_graph (elim_graph g)
249 sbitmap_free (g->visited);
250 free (g);
254 /* Return the number of nodes in graph G. */
256 static inline int
257 elim_graph_size (elim_graph g)
259 return VARRAY_ACTIVE_SIZE (g->nodes);
263 /* Add NODE to graph G, if it doesn't exist already. */
265 static inline void
266 elim_graph_add_node (elim_graph g, tree node)
268 int x;
269 for (x = 0; x < elim_graph_size (g); x++)
270 if (VARRAY_TREE (g->nodes, x) == node)
271 return;
272 VARRAY_PUSH_TREE (g->nodes, node);
276 /* Add the edge PRED->SUCC to graph G. */
278 static inline void
279 elim_graph_add_edge (elim_graph g, int pred, int succ)
281 VARRAY_PUSH_INT (g->edge_list, pred);
282 VARRAY_PUSH_INT (g->edge_list, succ);
286 /* Remove an edge from graph G for which NODE is the predecessor, and
287 return the successor node. -1 is returned if there is no such edge. */
289 static inline int
290 elim_graph_remove_succ_edge (elim_graph g, int node)
292 int y;
293 unsigned x;
294 for (x = 0; x < VARRAY_ACTIVE_SIZE (g->edge_list); x += 2)
295 if (VARRAY_INT (g->edge_list, x) == node)
297 VARRAY_INT (g->edge_list, x) = -1;
298 y = VARRAY_INT (g->edge_list, x + 1);
299 VARRAY_INT (g->edge_list, x + 1) = -1;
300 return y;
302 return -1;
306 /* Find all the nodes in GRAPH which are successors to NODE in the
307 edge list. VAR will hold the partition number found. CODE is the
308 code fragment executed for every node found. */
310 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, CODE) \
311 do { \
312 unsigned x_; \
313 int y_; \
314 for (x_ = 0; x_ < VARRAY_ACTIVE_SIZE ((GRAPH)->edge_list); x_ += 2) \
316 y_ = VARRAY_INT ((GRAPH)->edge_list, x_); \
317 if (y_ != (NODE)) \
318 continue; \
319 (VAR) = VARRAY_INT ((GRAPH)->edge_list, x_ + 1); \
320 CODE; \
322 } while (0)
325 /* Find all the nodes which are predecessors of NODE in the edge list for
326 GRAPH. VAR will hold the partition number found. CODE is the
327 code fragment executed for every node found. */
329 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, CODE) \
330 do { \
331 unsigned x_; \
332 int y_; \
333 for (x_ = 0; x_ < VARRAY_ACTIVE_SIZE ((GRAPH)->edge_list); x_ += 2) \
335 y_ = VARRAY_INT ((GRAPH)->edge_list, x_ + 1); \
336 if (y_ != (NODE)) \
337 continue; \
338 (VAR) = VARRAY_INT ((GRAPH)->edge_list, x_); \
339 CODE; \
341 } while (0)
344 /* Add T to elimination graph G. */
346 static inline void
347 eliminate_name (elim_graph g, tree T)
349 elim_graph_add_node (g, T);
353 /* Build elimination graph G for basic block BB on incoming PHI edge
354 G->e. */
356 static void
357 eliminate_build (elim_graph g, basic_block B)
359 tree phi;
360 tree T0, Ti;
361 int p0, pi;
363 clear_elim_graph (g);
365 for (phi = phi_nodes (B); phi; phi = PHI_CHAIN (phi))
367 T0 = var_to_partition_to_var (g->map, PHI_RESULT (phi));
369 /* Ignore results which are not in partitions. */
370 if (T0 == NULL_TREE)
371 continue;
373 Ti = PHI_ARG_DEF (phi, g->e->dest_idx);
375 /* If this argument is a constant, or a SSA_NAME which is being
376 left in SSA form, just queue a copy to be emitted on this
377 edge. */
378 if (!phi_ssa_name_p (Ti)
379 || (TREE_CODE (Ti) == SSA_NAME
380 && var_to_partition (g->map, Ti) == NO_PARTITION))
382 /* Save constant copies until all other copies have been emitted
383 on this edge. */
384 VARRAY_PUSH_TREE (g->const_copies, T0);
385 VARRAY_PUSH_TREE (g->const_copies, Ti);
387 else
389 Ti = var_to_partition_to_var (g->map, Ti);
390 if (T0 != Ti)
392 eliminate_name (g, T0);
393 eliminate_name (g, Ti);
394 p0 = var_to_partition (g->map, T0);
395 pi = var_to_partition (g->map, Ti);
396 elim_graph_add_edge (g, p0, pi);
403 /* Push successors of T onto the elimination stack for G. */
405 static void
406 elim_forward (elim_graph g, int T)
408 int S;
409 SET_BIT (g->visited, T);
410 FOR_EACH_ELIM_GRAPH_SUCC (g, T, S,
412 if (!TEST_BIT (g->visited, S))
413 elim_forward (g, S);
415 VARRAY_PUSH_INT (g->stack, T);
419 /* Return 1 if there unvisited predecessors of T in graph G. */
421 static int
422 elim_unvisited_predecessor (elim_graph g, int T)
424 int P;
425 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
427 if (!TEST_BIT (g->visited, P))
428 return 1;
430 return 0;
433 /* Process predecessors first, and insert a copy. */
435 static void
436 elim_backward (elim_graph g, int T)
438 int P;
439 SET_BIT (g->visited, T);
440 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
442 if (!TEST_BIT (g->visited, P))
444 elim_backward (g, P);
445 insert_copy_on_edge (g->e,
446 partition_to_var (g->map, P),
447 partition_to_var (g->map, T));
452 /* Insert required copies for T in graph G. Check for a strongly connected
453 region, and create a temporary to break the cycle if one is found. */
455 static void
456 elim_create (elim_graph g, int T)
458 tree U;
459 int P, S;
461 if (elim_unvisited_predecessor (g, T))
463 U = create_temp (partition_to_var (g->map, T));
464 insert_copy_on_edge (g->e, U, partition_to_var (g->map, T));
465 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
467 if (!TEST_BIT (g->visited, P))
469 elim_backward (g, P);
470 insert_copy_on_edge (g->e, partition_to_var (g->map, P), U);
474 else
476 S = elim_graph_remove_succ_edge (g, T);
477 if (S != -1)
479 SET_BIT (g->visited, T);
480 insert_copy_on_edge (g->e,
481 partition_to_var (g->map, T),
482 partition_to_var (g->map, S));
488 /* Eliminate all the phi nodes on edge E in graph G. */
490 static void
491 eliminate_phi (edge e, elim_graph g)
493 int num_nodes = 0;
494 int x;
495 basic_block B = e->dest;
497 gcc_assert (VARRAY_ACTIVE_SIZE (g->const_copies) == 0);
499 /* Abnormal edges already have everything coalesced, or the coalescer
500 would have aborted. */
501 if (e->flags & EDGE_ABNORMAL)
502 return;
504 num_nodes = num_var_partitions (g->map);
505 g->e = e;
507 eliminate_build (g, B);
509 if (elim_graph_size (g) != 0)
511 sbitmap_zero (g->visited);
512 VARRAY_POP_ALL (g->stack);
514 for (x = 0; x < elim_graph_size (g); x++)
516 tree var = VARRAY_TREE (g->nodes, x);
517 int p = var_to_partition (g->map, var);
518 if (!TEST_BIT (g->visited, p))
519 elim_forward (g, p);
522 sbitmap_zero (g->visited);
523 while (VARRAY_ACTIVE_SIZE (g->stack) > 0)
525 x = VARRAY_TOP_INT (g->stack);
526 VARRAY_POP (g->stack);
527 if (!TEST_BIT (g->visited, x))
528 elim_create (g, x);
532 /* If there are any pending constant copies, issue them now. */
533 while (VARRAY_ACTIVE_SIZE (g->const_copies) > 0)
535 tree src, dest;
536 src = VARRAY_TOP_TREE (g->const_copies);
537 VARRAY_POP (g->const_copies);
538 dest = VARRAY_TOP_TREE (g->const_copies);
539 VARRAY_POP (g->const_copies);
540 insert_copy_on_edge (e, dest, src);
545 /* Shortcut routine to print messages to file F of the form:
546 "STR1 EXPR1 STR2 EXPR2 STR3." */
548 static void
549 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
550 tree expr2, const char *str3)
552 fprintf (f, "%s", str1);
553 print_generic_expr (f, expr1, TDF_SLIM);
554 fprintf (f, "%s", str2);
555 print_generic_expr (f, expr2, TDF_SLIM);
556 fprintf (f, "%s", str3);
560 /* Shortcut routine to print abnormal edge messages to file F of the form:
561 "STR1 EXPR1 STR2 EXPR2 across edge E. */
563 static void
564 print_exprs_edge (FILE *f, edge e, const char *str1, tree expr1,
565 const char *str2, tree expr2)
567 print_exprs (f, str1, expr1, str2, expr2, " across an abnormal edge");
568 fprintf (f, " from BB%d->BB%d\n", e->src->index,
569 e->dest->index);
573 /* Coalesce partitions in MAP which are live across abnormal edges in GRAPH.
574 RV is the root variable groupings of the partitions in MAP. Since code
575 cannot be inserted on these edges, failure to coalesce something across
576 an abnormal edge is an error. */
578 static void
579 coalesce_abnormal_edges (var_map map, conflict_graph graph, root_var_p rv)
581 basic_block bb;
582 edge e;
583 tree phi, var, tmp;
584 int x, y, z;
585 edge_iterator ei;
587 /* Code cannot be inserted on abnormal edges. Look for all abnormal
588 edges, and coalesce any PHI results with their arguments across
589 that edge. */
591 FOR_EACH_BB (bb)
592 FOR_EACH_EDGE (e, ei, bb->succs)
593 if (e->dest != EXIT_BLOCK_PTR && e->flags & EDGE_ABNORMAL)
594 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
596 /* Visit each PHI on the destination side of this abnormal
597 edge, and attempt to coalesce the argument with the result. */
598 var = PHI_RESULT (phi);
599 x = var_to_partition (map, var);
601 /* Ignore results which are not relevant. */
602 if (x == NO_PARTITION)
603 continue;
605 tmp = PHI_ARG_DEF (phi, e->dest_idx);
606 #ifdef ENABLE_CHECKING
607 if (!phi_ssa_name_p (tmp))
609 print_exprs_edge (stderr, e,
610 "\nConstant argument in PHI. Can't insert :",
611 var, " = ", tmp);
612 internal_error ("SSA corruption");
614 #else
615 gcc_assert (phi_ssa_name_p (tmp));
616 #endif
617 y = var_to_partition (map, tmp);
618 gcc_assert (x != NO_PARTITION);
619 gcc_assert (y != NO_PARTITION);
620 #ifdef ENABLE_CHECKING
621 if (root_var_find (rv, x) != root_var_find (rv, y))
623 print_exprs_edge (stderr, e, "\nDifferent root vars: ",
624 root_var (rv, root_var_find (rv, x)),
625 " and ",
626 root_var (rv, root_var_find (rv, y)));
627 internal_error ("SSA corruption");
629 #else
630 gcc_assert (root_var_find (rv, x) == root_var_find (rv, y));
631 #endif
633 if (x != y)
635 #ifdef ENABLE_CHECKING
636 if (conflict_graph_conflict_p (graph, x, y))
638 print_exprs_edge (stderr, e, "\n Conflict ",
639 partition_to_var (map, x),
640 " and ", partition_to_var (map, y));
641 internal_error ("SSA corruption");
643 #else
644 gcc_assert (!conflict_graph_conflict_p (graph, x, y));
645 #endif
647 /* Now map the partitions back to their real variables. */
648 var = partition_to_var (map, x);
649 tmp = partition_to_var (map, y);
650 if (dump_file && (dump_flags & TDF_DETAILS))
652 print_exprs_edge (dump_file, e,
653 "ABNORMAL: Coalescing ",
654 var, " and ", tmp);
656 z = var_union (map, var, tmp);
657 #ifdef ENABLE_CHECKING
658 if (z == NO_PARTITION)
660 print_exprs_edge (stderr, e, "\nUnable to coalesce",
661 partition_to_var (map, x), " and ",
662 partition_to_var (map, y));
663 internal_error ("SSA corruption");
665 #else
666 gcc_assert (z != NO_PARTITION);
667 #endif
668 gcc_assert (z == x || z == y);
669 if (z == x)
670 conflict_graph_merge_regs (graph, x, y);
671 else
672 conflict_graph_merge_regs (graph, y, x);
678 /* Reduce the number of live ranges in MAP. Live range information is
679 returned if FLAGS indicates that we are combining temporaries, otherwise
680 NULL is returned. The only partitions which are associated with actual
681 variables at this point are those which are forced to be coalesced for
682 various reason. (live on entry, live across abnormal edges, etc.). */
684 static tree_live_info_p
685 coalesce_ssa_name (var_map map, int flags)
687 unsigned num, x, i;
688 sbitmap live;
689 tree var, phi;
690 root_var_p rv;
691 tree_live_info_p liveinfo;
692 var_ann_t ann;
693 conflict_graph graph;
694 basic_block bb;
695 coalesce_list_p cl = NULL;
697 if (num_var_partitions (map) <= 1)
698 return NULL;
700 /* If no preference given, use cheap coalescing of all partitions. */
701 if ((flags & (SSANORM_COALESCE_PARTITIONS | SSANORM_USE_COALESCE_LIST)) == 0)
702 flags |= SSANORM_COALESCE_PARTITIONS;
704 liveinfo = calculate_live_on_entry (map);
705 calculate_live_on_exit (liveinfo);
706 rv = root_var_init (map);
708 /* Remove single element variable from the list. */
709 root_var_compact (rv);
711 if (flags & SSANORM_USE_COALESCE_LIST)
713 cl = create_coalesce_list (map);
715 /* Add all potential copies via PHI arguments to the list. */
716 FOR_EACH_BB (bb)
718 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
720 tree res = PHI_RESULT (phi);
721 int p = var_to_partition (map, res);
722 if (p == NO_PARTITION)
723 continue;
724 for (x = 0; x < (unsigned)PHI_NUM_ARGS (phi); x++)
726 tree arg = PHI_ARG_DEF (phi, x);
727 int p2;
729 if (TREE_CODE (arg) != SSA_NAME)
730 continue;
731 if (SSA_NAME_VAR (res) != SSA_NAME_VAR (arg))
732 continue;
733 p2 = var_to_partition (map, PHI_ARG_DEF (phi, x));
734 if (p2 != NO_PARTITION)
735 add_coalesce (cl, p, p2, 1);
740 /* Coalesce all the result decls together. */
741 var = NULL_TREE;
742 i = 0;
743 for (x = 0; x < num_var_partitions (map); x++)
745 tree p = partition_to_var (map, x);
746 if (TREE_CODE (SSA_NAME_VAR(p)) == RESULT_DECL)
748 if (var == NULL_TREE)
750 var = p;
751 i = x;
753 else
754 add_coalesce (cl, i, x, 1);
759 /* Build a conflict graph. */
760 graph = build_tree_conflict_graph (liveinfo, rv, cl);
762 if (cl)
764 if (dump_file && (dump_flags & TDF_DETAILS))
766 fprintf (dump_file, "Before sorting:\n");
767 dump_coalesce_list (dump_file, cl);
770 sort_coalesce_list (cl);
772 if (dump_file && (dump_flags & TDF_DETAILS))
774 fprintf (dump_file, "\nAfter sorting:\n");
775 dump_coalesce_list (dump_file, cl);
779 /* Put the single element variables back in. */
780 root_var_decompact (rv);
782 /* First, coalesce all live on entry variables to their root variable.
783 This will ensure the first use is coming from the correct location. */
785 live = sbitmap_alloc (num_var_partitions (map));
786 sbitmap_zero (live);
788 /* Set 'live' vector to indicate live on entry partitions. */
789 num = num_var_partitions (map);
790 for (x = 0 ; x < num; x++)
792 var = partition_to_var (map, x);
793 if (default_def (SSA_NAME_VAR (var)) == var)
794 SET_BIT (live, x);
797 if ((flags & SSANORM_COMBINE_TEMPS) == 0)
799 delete_tree_live_info (liveinfo);
800 liveinfo = NULL;
803 /* Assign root variable as partition representative for each live on entry
804 partition. */
805 EXECUTE_IF_SET_IN_SBITMAP (live, 0, x,
807 var = root_var (rv, root_var_find (rv, x));
808 ann = var_ann (var);
809 /* If these aren't already coalesced... */
810 if (partition_to_var (map, x) != var)
812 /* This root variable should have not already been assigned
813 to another partition which is not coalesced with this one. */
814 gcc_assert (!ann->out_of_ssa_tag);
816 if (dump_file && (dump_flags & TDF_DETAILS))
818 print_exprs (dump_file, "Must coalesce ",
819 partition_to_var (map, x),
820 " with the root variable ", var, ".\n");
823 change_partition_var (map, var, x);
827 sbitmap_free (live);
829 /* Coalesce partitions live across abnormal edges. */
830 coalesce_abnormal_edges (map, graph, rv);
832 if (dump_file && (dump_flags & TDF_DETAILS))
833 dump_var_map (dump_file, map);
835 /* Coalesce partitions. */
836 if (flags & SSANORM_USE_COALESCE_LIST)
837 coalesce_tpa_members (rv, graph, map, cl,
838 ((dump_flags & TDF_DETAILS) ? dump_file
839 : NULL));
842 if (flags & SSANORM_COALESCE_PARTITIONS)
843 coalesce_tpa_members (rv, graph, map, NULL,
844 ((dump_flags & TDF_DETAILS) ? dump_file
845 : NULL));
846 if (cl)
847 delete_coalesce_list (cl);
848 root_var_delete (rv);
849 conflict_graph_delete (graph);
851 return liveinfo;
855 /* Take the ssa-name var_map MAP, and assign real variables to each
856 partition. */
858 static void
859 assign_vars (var_map map)
861 int x, i, num, rep;
862 tree t, var;
863 var_ann_t ann;
864 root_var_p rv;
866 rv = root_var_init (map);
867 if (!rv)
868 return;
870 /* Coalescing may already have forced some partitions to their root
871 variable. Find these and tag them. */
873 num = num_var_partitions (map);
874 for (x = 0; x < num; x++)
876 var = partition_to_var (map, x);
877 if (TREE_CODE (var) != SSA_NAME)
879 /* Coalescing will already have verified that more than one
880 partition doesn't have the same root variable. Simply marked
881 the variable as assigned. */
882 ann = var_ann (var);
883 ann->out_of_ssa_tag = 1;
884 if (dump_file && (dump_flags & TDF_DETAILS))
886 fprintf (dump_file, "partition %d has variable ", x);
887 print_generic_expr (dump_file, var, TDF_SLIM);
888 fprintf (dump_file, " assigned to it.\n");
894 num = root_var_num (rv);
895 for (x = 0; x < num; x++)
897 var = root_var (rv, x);
898 ann = var_ann (var);
899 for (i = root_var_first_partition (rv, x);
900 i != ROOT_VAR_NONE;
901 i = root_var_next_partition (rv, i))
903 t = partition_to_var (map, i);
905 if (t == var || TREE_CODE (t) != SSA_NAME)
906 continue;
908 rep = var_to_partition (map, t);
910 if (!ann->out_of_ssa_tag)
912 if (dump_file && (dump_flags & TDF_DETAILS))
913 print_exprs (dump_file, "", t, " --> ", var, "\n");
914 change_partition_var (map, var, rep);
915 continue;
918 if (dump_file && (dump_flags & TDF_DETAILS))
919 print_exprs (dump_file, "", t, " not coalesced with ", var,
920 "");
922 var = create_temp (t);
923 change_partition_var (map, var, rep);
924 ann = var_ann (var);
926 if (dump_file && (dump_flags & TDF_DETAILS))
928 fprintf (dump_file, " --> New temp: '");
929 print_generic_expr (dump_file, var, TDF_SLIM);
930 fprintf (dump_file, "'\n");
935 root_var_delete (rv);
939 /* Replace use operand P with whatever variable it has been rewritten to based
940 on the partitions in MAP. EXPR is an optional expression vector over SSA
941 versions which is used to replace P with an expression instead of a variable.
942 If the stmt is changed, return true. */
944 static inline bool
945 replace_use_variable (var_map map, use_operand_p p, tree *expr)
947 tree new_var;
948 tree var = USE_FROM_PTR (p);
950 /* Check if we are replacing this variable with an expression. */
951 if (expr)
953 int version = SSA_NAME_VERSION (var);
954 if (expr[version])
956 tree new_expr = TREE_OPERAND (expr[version], 1);
957 SET_USE (p, new_expr);
958 /* Clear the stmt's RHS, or GC might bite us. */
959 TREE_OPERAND (expr[version], 1) = NULL_TREE;
960 return true;
964 new_var = var_to_partition_to_var (map, var);
965 if (new_var)
967 SET_USE (p, new_var);
968 set_is_used (new_var);
969 return true;
971 return false;
975 /* Replace def operand DEF_P with whatever variable it has been rewritten to
976 based on the partitions in MAP. EXPR is an optional expression vector over
977 SSA versions which is used to replace DEF_P with an expression instead of a
978 variable. If the stmt is changed, return true. */
980 static inline bool
981 replace_def_variable (var_map map, def_operand_p def_p, tree *expr)
983 tree new_var;
984 tree var = DEF_FROM_PTR (def_p);
986 /* Check if we are replacing this variable with an expression. */
987 if (expr)
989 int version = SSA_NAME_VERSION (var);
990 if (expr[version])
992 tree new_expr = TREE_OPERAND (expr[version], 1);
993 SET_DEF (def_p, new_expr);
994 /* Clear the stmt's RHS, or GC might bite us. */
995 TREE_OPERAND (expr[version], 1) = NULL_TREE;
996 return true;
1000 new_var = var_to_partition_to_var (map, var);
1001 if (new_var)
1003 SET_DEF (def_p, new_var);
1004 set_is_used (new_var);
1005 return true;
1007 return false;
1011 /* Remove any PHI node which is a virtual PHI. */
1013 static void
1014 eliminate_virtual_phis (void)
1016 basic_block bb;
1017 tree phi, next;
1019 FOR_EACH_BB (bb)
1021 for (phi = phi_nodes (bb); phi; phi = next)
1023 next = PHI_CHAIN (phi);
1024 if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi))))
1026 #ifdef ENABLE_CHECKING
1027 int i;
1028 /* There should be no arguments of this PHI which are in
1029 the partition list, or we get incorrect results. */
1030 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1032 tree arg = PHI_ARG_DEF (phi, i);
1033 if (TREE_CODE (arg) == SSA_NAME
1034 && is_gimple_reg (SSA_NAME_VAR (arg)))
1036 fprintf (stderr, "Argument of PHI is not virtual (");
1037 print_generic_expr (stderr, arg, TDF_SLIM);
1038 fprintf (stderr, "), but the result is :");
1039 print_generic_stmt (stderr, phi, TDF_SLIM);
1040 internal_error ("SSA corruption");
1043 #endif
1044 remove_phi_node (phi, NULL_TREE, bb);
1051 /* This routine will coalesce variables in MAP of the same type which do not
1052 interfere with each other. LIVEINFO is the live range info for variables
1053 of interest. This will both reduce the memory footprint of the stack, and
1054 allow us to coalesce together local copies of globals and scalarized
1055 component refs. */
1057 static void
1058 coalesce_vars (var_map map, tree_live_info_p liveinfo)
1060 basic_block bb;
1061 type_var_p tv;
1062 tree var;
1063 unsigned x, p, p2;
1064 coalesce_list_p cl;
1065 conflict_graph graph;
1067 cl = create_coalesce_list (map);
1069 /* Merge all the live on entry vectors for coalesced partitions. */
1070 for (x = 0; x < num_var_partitions (map); x++)
1072 var = partition_to_var (map, x);
1073 p = var_to_partition (map, var);
1074 if (p != x)
1075 live_merge_and_clear (liveinfo, p, x);
1078 /* When PHI nodes are turned into copies, the result of each PHI node
1079 becomes live on entry to the block. Mark these now. */
1080 FOR_EACH_BB (bb)
1082 tree phi, arg;
1083 unsigned p;
1085 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1087 p = var_to_partition (map, PHI_RESULT (phi));
1089 /* Skip virtual PHI nodes. */
1090 if (p == (unsigned)NO_PARTITION)
1091 continue;
1093 make_live_on_entry (liveinfo, bb, p);
1095 /* Each argument is a potential copy operation. Add any arguments
1096 which are not coalesced to the result to the coalesce list. */
1097 for (x = 0; x < (unsigned)PHI_NUM_ARGS (phi); x++)
1099 arg = PHI_ARG_DEF (phi, x);
1100 if (!phi_ssa_name_p (arg))
1101 continue;
1102 p2 = var_to_partition (map, arg);
1103 if (p2 == (unsigned)NO_PARTITION)
1104 continue;
1105 if (p != p2)
1106 add_coalesce (cl, p, p2, 1);
1112 /* Re-calculate live on exit info. */
1113 calculate_live_on_exit (liveinfo);
1115 if (dump_file && (dump_flags & TDF_DETAILS))
1117 fprintf (dump_file, "Live range info for variable memory coalescing.\n");
1118 dump_live_info (dump_file, liveinfo, LIVEDUMP_ALL);
1120 fprintf (dump_file, "Coalesce list from phi nodes:\n");
1121 dump_coalesce_list (dump_file, cl);
1125 tv = type_var_init (map);
1126 if (dump_file)
1127 type_var_dump (dump_file, tv);
1128 type_var_compact (tv);
1129 if (dump_file)
1130 type_var_dump (dump_file, tv);
1132 graph = build_tree_conflict_graph (liveinfo, tv, cl);
1134 type_var_decompact (tv);
1135 if (dump_file && (dump_flags & TDF_DETAILS))
1137 fprintf (dump_file, "type var list now looks like:n");
1138 type_var_dump (dump_file, tv);
1140 fprintf (dump_file, "Coalesce list after conflict graph build:\n");
1141 dump_coalesce_list (dump_file, cl);
1144 sort_coalesce_list (cl);
1145 if (dump_file && (dump_flags & TDF_DETAILS))
1147 fprintf (dump_file, "Coalesce list after sorting:\n");
1148 dump_coalesce_list (dump_file, cl);
1151 coalesce_tpa_members (tv, graph, map, cl,
1152 ((dump_flags & TDF_DETAILS) ? dump_file : NULL));
1154 type_var_delete (tv);
1155 delete_coalesce_list (cl);
1159 /* Temporary Expression Replacement (TER)
1161 Replace SSA version variables during out-of-ssa with their defining
1162 expression if there is only one use of the variable.
1164 A pass is made through the function, one block at a time. No cross block
1165 information is tracked.
1167 Variables which only have one use, and whose defining stmt is considered
1168 a replaceable expression (see check_replaceable) are entered into
1169 consideration by adding a list of dependent partitions to the version_info
1170 vector for that ssa_name_version. This information comes from the partition
1171 mapping for each USE. At the same time, the partition_dep_list vector for
1172 these partitions have this version number entered into their lists.
1174 When the use of a replaceable ssa_variable is encountered, the dependence
1175 list in version_info[] is moved to the "pending_dependence" list in case
1176 the current expression is also replaceable. (To be determined later in
1177 processing this stmt.) version_info[] for the version is then updated to
1178 point to the defining stmt and the 'replaceable' bit is set.
1180 Any partition which is defined by a statement 'kills' any expression which
1181 is dependent on this partition. Every ssa version in the partitions'
1182 dependence list is removed from future consideration.
1184 All virtual references are lumped together. Any expression which is
1185 dependent on any virtual variable (via a VUSE) has a dependence added
1186 to the special partition defined by VIRTUAL_PARTITION.
1188 Whenever a V_MAY_DEF is seen, all expressions dependent this
1189 VIRTUAL_PARTITION are removed from consideration.
1191 At the end of a basic block, all expression are removed from consideration
1192 in preparation for the next block.
1194 The end result is a vector over SSA_NAME_VERSION which is passed back to
1195 rewrite_out_of_ssa. As the SSA variables are being rewritten, instead of
1196 replacing the SSA_NAME tree element with the partition it was assigned,
1197 it is replaced with the RHS of the defining expression. */
1200 /* Dependency list element. This can contain either a partition index or a
1201 version number, depending on which list it is in. */
1203 typedef struct value_expr_d
1205 int value;
1206 struct value_expr_d *next;
1207 } *value_expr_p;
1210 /* Temporary Expression Replacement (TER) table information. */
1212 typedef struct temp_expr_table_d
1214 var_map map;
1215 void **version_info;
1216 value_expr_p *partition_dep_list;
1217 bitmap replaceable;
1218 bool saw_replaceable;
1219 int virtual_partition;
1220 bitmap partition_in_use;
1221 value_expr_p free_list;
1222 value_expr_p pending_dependence;
1223 } *temp_expr_table_p;
1225 /* Used to indicate a dependency on V_MAY_DEFs. */
1226 #define VIRTUAL_PARTITION(table) (table->virtual_partition)
1228 static temp_expr_table_p new_temp_expr_table (var_map);
1229 static tree *free_temp_expr_table (temp_expr_table_p);
1230 static inline value_expr_p new_value_expr (temp_expr_table_p);
1231 static inline void free_value_expr (temp_expr_table_p, value_expr_p);
1232 static inline value_expr_p find_value_in_list (value_expr_p, int,
1233 value_expr_p *);
1234 static inline void add_value_to_list (temp_expr_table_p, value_expr_p *, int);
1235 static inline void add_info_to_list (temp_expr_table_p, value_expr_p *,
1236 value_expr_p);
1237 static value_expr_p remove_value_from_list (value_expr_p *, int);
1238 static void add_dependance (temp_expr_table_p, int, tree);
1239 static bool check_replaceable (temp_expr_table_p, tree);
1240 static void finish_expr (temp_expr_table_p, int, bool);
1241 static void mark_replaceable (temp_expr_table_p, tree);
1242 static inline void kill_expr (temp_expr_table_p, int, bool);
1243 static inline void kill_virtual_exprs (temp_expr_table_p, bool);
1244 static void find_replaceable_in_bb (temp_expr_table_p, basic_block);
1245 static tree *find_replaceable_exprs (var_map);
1246 static void dump_replaceable_exprs (FILE *, tree *);
1249 /* Create a new TER table for MAP. */
1251 static temp_expr_table_p
1252 new_temp_expr_table (var_map map)
1254 temp_expr_table_p t;
1256 t = (temp_expr_table_p) xmalloc (sizeof (struct temp_expr_table_d));
1257 t->map = map;
1259 t->version_info = xcalloc (num_ssa_names + 1, sizeof (void *));
1260 t->partition_dep_list = xcalloc (num_var_partitions (map) + 1,
1261 sizeof (value_expr_p));
1263 t->replaceable = BITMAP_XMALLOC ();
1264 t->partition_in_use = BITMAP_XMALLOC ();
1266 t->saw_replaceable = false;
1267 t->virtual_partition = num_var_partitions (map);
1268 t->free_list = NULL;
1269 t->pending_dependence = NULL;
1271 return t;
1275 /* Free TER table T. If there are valid replacements, return the expression
1276 vector. */
1278 static tree *
1279 free_temp_expr_table (temp_expr_table_p t)
1281 value_expr_p p;
1282 tree *ret = NULL;
1284 #ifdef ENABLE_CHECKING
1285 unsigned x;
1286 for (x = 0; x <= num_var_partitions (t->map); x++)
1287 gcc_assert (!t->partition_dep_list[x]);
1288 #endif
1290 while ((p = t->free_list))
1292 t->free_list = p->next;
1293 free (p);
1296 BITMAP_XFREE (t->partition_in_use);
1297 BITMAP_XFREE (t->replaceable);
1299 free (t->partition_dep_list);
1300 if (t->saw_replaceable)
1301 ret = (tree *)t->version_info;
1302 else
1303 free (t->version_info);
1305 free (t);
1306 return ret;
1310 /* Allocate a new value list node. Take it from the free list in TABLE if
1311 possible. */
1313 static inline value_expr_p
1314 new_value_expr (temp_expr_table_p table)
1316 value_expr_p p;
1317 if (table->free_list)
1319 p = table->free_list;
1320 table->free_list = p->next;
1322 else
1323 p = (value_expr_p) xmalloc (sizeof (struct value_expr_d));
1325 return p;
1329 /* Add value list node P to the free list in TABLE. */
1331 static inline void
1332 free_value_expr (temp_expr_table_p table, value_expr_p p)
1334 p->next = table->free_list;
1335 table->free_list = p;
1339 /* Find VALUE if it's in LIST. Return a pointer to the list object if found,
1340 else return NULL. If LAST_PTR is provided, it will point to the previous
1341 item upon return, or NULL if this is the first item in the list. */
1343 static inline value_expr_p
1344 find_value_in_list (value_expr_p list, int value, value_expr_p *last_ptr)
1346 value_expr_p curr;
1347 value_expr_p last = NULL;
1349 for (curr = list; curr; last = curr, curr = curr->next)
1351 if (curr->value == value)
1352 break;
1354 if (last_ptr)
1355 *last_ptr = last;
1356 return curr;
1360 /* Add VALUE to LIST, if it isn't already present. TAB is the expression
1361 table */
1363 static inline void
1364 add_value_to_list (temp_expr_table_p tab, value_expr_p *list, int value)
1366 value_expr_p info;
1368 if (!find_value_in_list (*list, value, NULL))
1370 info = new_value_expr (tab);
1371 info->value = value;
1372 info->next = *list;
1373 *list = info;
1378 /* Add value node INFO if it's value isn't already in LIST. Free INFO if
1379 it is already in the list. TAB is the expression table. */
1381 static inline void
1382 add_info_to_list (temp_expr_table_p tab, value_expr_p *list, value_expr_p info)
1384 if (find_value_in_list (*list, info->value, NULL))
1385 free_value_expr (tab, info);
1386 else
1388 info->next = *list;
1389 *list = info;
1394 /* Look for VALUE in LIST. If found, remove it from the list and return it's
1395 pointer. */
1397 static value_expr_p
1398 remove_value_from_list (value_expr_p *list, int value)
1400 value_expr_p info, last;
1402 info = find_value_in_list (*list, value, &last);
1403 if (!info)
1404 return NULL;
1405 if (!last)
1406 *list = info->next;
1407 else
1408 last->next = info->next;
1410 return info;
1414 /* Add a dependency between the def of ssa VERSION and VAR. If VAR is
1415 replaceable by an expression, add a dependence each of the elements of the
1416 expression. These are contained in the pending list. TAB is the
1417 expression table. */
1419 static void
1420 add_dependance (temp_expr_table_p tab, int version, tree var)
1422 int i, x;
1423 value_expr_p info;
1425 i = SSA_NAME_VERSION (var);
1426 if (bitmap_bit_p (tab->replaceable, i))
1428 /* This variable is being substituted, so use whatever dependences
1429 were queued up when we marked this as replaceable earlier. */
1430 while ((info = tab->pending_dependence))
1432 tab->pending_dependence = info->next;
1433 /* Get the partition this variable was dependent on. Reuse this
1434 object to represent the current expression instead. */
1435 x = info->value;
1436 info->value = version;
1437 add_info_to_list (tab, &(tab->partition_dep_list[x]), info);
1438 add_value_to_list (tab,
1439 (value_expr_p *)&(tab->version_info[version]), x);
1440 bitmap_set_bit (tab->partition_in_use, x);
1443 else
1445 i = var_to_partition (tab->map, var);
1446 gcc_assert (i != NO_PARTITION);
1447 add_value_to_list (tab, &(tab->partition_dep_list[i]), version);
1448 add_value_to_list (tab,
1449 (value_expr_p *)&(tab->version_info[version]), i);
1450 bitmap_set_bit (tab->partition_in_use, i);
1455 /* Check if expression STMT is suitable for replacement in table TAB. If so,
1456 create an expression entry. Return true if this stmt is replaceable. */
1458 static bool
1459 check_replaceable (temp_expr_table_p tab, tree stmt)
1461 stmt_ann_t ann;
1462 vuse_optype vuseops;
1463 def_optype defs;
1464 use_optype uses;
1465 tree var, def;
1466 int num_use_ops, version;
1467 var_map map = tab->map;
1468 ssa_op_iter iter;
1469 tree call_expr;
1471 if (TREE_CODE (stmt) != MODIFY_EXPR)
1472 return false;
1474 ann = stmt_ann (stmt);
1475 defs = DEF_OPS (ann);
1477 /* Punt if there is more than 1 def, or more than 1 use. */
1478 if (NUM_DEFS (defs) != 1)
1479 return false;
1480 def = DEF_OP (defs, 0);
1481 if (version_ref_count (map, def) != 1)
1482 return false;
1484 /* There must be no V_MAY_DEFS. */
1485 if (NUM_V_MAY_DEFS (V_MAY_DEF_OPS (ann)) != 0)
1486 return false;
1488 /* There must be no V_MUST_DEFS. */
1489 if (NUM_V_MUST_DEFS (V_MUST_DEF_OPS (ann)) != 0)
1490 return false;
1492 /* Float expressions must go through memory if float-store is on. */
1493 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 1))))
1494 return false;
1496 /* Calls to functions with side-effects cannot be replaced. */
1497 if ((call_expr = get_call_expr_in (stmt)) != NULL_TREE)
1499 int call_flags = call_expr_flags (call_expr);
1500 if (TREE_SIDE_EFFECTS (call_expr)
1501 && !(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
1502 return false;
1505 uses = USE_OPS (ann);
1506 num_use_ops = NUM_USES (uses);
1507 vuseops = VUSE_OPS (ann);
1509 /* Any expression which has no virtual operands and no real operands
1510 should have been propagated if it's possible to do anything with them.
1511 If this happens here, it probably exists that way for a reason, so we
1512 won't touch it. An example is:
1513 b_4 = &tab
1514 There are no virtual uses nor any real uses, so we just leave this
1515 alone to be safe. */
1517 if (num_use_ops == 0 && NUM_VUSES (vuseops) == 0)
1518 return false;
1520 version = SSA_NAME_VERSION (def);
1522 /* Add this expression to the dependency list for each use partition. */
1523 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
1525 add_dependance (tab, version, var);
1528 /* If there are VUSES, add a dependence on virtual defs. */
1529 if (NUM_VUSES (vuseops) != 0)
1531 add_value_to_list (tab, (value_expr_p *)&(tab->version_info[version]),
1532 VIRTUAL_PARTITION (tab));
1533 add_value_to_list (tab,
1534 &(tab->partition_dep_list[VIRTUAL_PARTITION (tab)]),
1535 version);
1536 bitmap_set_bit (tab->partition_in_use, VIRTUAL_PARTITION (tab));
1539 return true;
1543 /* This function will remove the expression for VERSION from replacement
1544 consideration.n table TAB If 'replace' is true, it is marked as
1545 replaceable, otherwise not. */
1547 static void
1548 finish_expr (temp_expr_table_p tab, int version, bool replace)
1550 value_expr_p info, tmp;
1551 int partition;
1553 /* Remove this expression from its dependent lists. The partition dependence
1554 list is retained and transfered later to whomever uses this version. */
1555 for (info = (value_expr_p) tab->version_info[version]; info; info = tmp)
1557 partition = info->value;
1558 gcc_assert (tab->partition_dep_list[partition]);
1559 tmp = remove_value_from_list (&(tab->partition_dep_list[partition]),
1560 version);
1561 gcc_assert (tmp);
1562 free_value_expr (tab, tmp);
1563 /* Only clear the bit when the dependency list is emptied via
1564 a replacement. Otherwise kill_expr will take care of it. */
1565 if (!(tab->partition_dep_list[partition]) && replace)
1566 bitmap_clear_bit (tab->partition_in_use, partition);
1567 tmp = info->next;
1568 if (!replace)
1569 free_value_expr (tab, info);
1572 if (replace)
1574 tab->saw_replaceable = true;
1575 bitmap_set_bit (tab->replaceable, version);
1577 else
1579 gcc_assert (!bitmap_bit_p (tab->replaceable, version));
1580 tab->version_info[version] = NULL;
1585 /* Mark the expression associated with VAR as replaceable, and enter
1586 the defining stmt into the version_info table TAB. */
1588 static void
1589 mark_replaceable (temp_expr_table_p tab, tree var)
1591 value_expr_p info;
1592 int version = SSA_NAME_VERSION (var);
1593 finish_expr (tab, version, true);
1595 /* Move the dependence list to the pending list. */
1596 if (tab->version_info[version])
1598 info = (value_expr_p) tab->version_info[version];
1599 for ( ; info->next; info = info->next)
1600 continue;
1601 info->next = tab->pending_dependence;
1602 tab->pending_dependence = (value_expr_p)tab->version_info[version];
1605 tab->version_info[version] = SSA_NAME_DEF_STMT (var);
1609 /* This function marks any expression in TAB which is dependent on PARTITION
1610 as NOT replaceable. CLEAR_BIT is used to determine whether partition_in_use
1611 should have its bit cleared. Since this routine can be called within an
1612 EXECUTE_IF_SET_IN_BITMAP, the bit can't always be cleared. */
1614 static inline void
1615 kill_expr (temp_expr_table_p tab, int partition, bool clear_bit)
1617 value_expr_p ptr;
1619 /* Mark every active expr dependent on this var as not replaceable. */
1620 while ((ptr = tab->partition_dep_list[partition]) != NULL)
1621 finish_expr (tab, ptr->value, false);
1623 if (clear_bit)
1624 bitmap_clear_bit (tab->partition_in_use, partition);
1628 /* This function kills all expressions in TAB which are dependent on virtual
1629 DEFs. CLEAR_BIT determines whether partition_in_use gets cleared. */
1631 static inline void
1632 kill_virtual_exprs (temp_expr_table_p tab, bool clear_bit)
1634 kill_expr (tab, VIRTUAL_PARTITION (tab), clear_bit);
1638 /* This function processes basic block BB, and looks for variables which can
1639 be replaced by their expressions. Results are stored in TAB. */
1641 static void
1642 find_replaceable_in_bb (temp_expr_table_p tab, basic_block bb)
1644 block_stmt_iterator bsi;
1645 tree stmt, def;
1646 stmt_ann_t ann;
1647 int partition;
1648 var_map map = tab->map;
1649 value_expr_p p;
1650 ssa_op_iter iter;
1652 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1654 stmt = bsi_stmt (bsi);
1655 ann = stmt_ann (stmt);
1657 /* Determine if this stmt finishes an existing expression. */
1658 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_USE)
1660 if (tab->version_info[SSA_NAME_VERSION (def)])
1662 bool same_root_var = false;
1663 tree def2;
1664 ssa_op_iter iter2;
1666 /* See if the root variables are the same. If they are, we
1667 do not want to do the replacement to avoid problems with
1668 code size, see PR tree-optimization/17549. */
1669 FOR_EACH_SSA_TREE_OPERAND (def2, stmt, iter2, SSA_OP_DEF)
1670 if (SSA_NAME_VAR (def) == SSA_NAME_VAR (def2))
1672 same_root_var = true;
1673 break;
1676 /* Mark expression as replaceable unless stmt is volatile
1677 or DEF sets the same root variable as STMT. */
1678 if (!ann->has_volatile_ops && !same_root_var)
1679 mark_replaceable (tab, def);
1680 else
1681 finish_expr (tab, SSA_NAME_VERSION (def), false);
1685 /* Next, see if this stmt kills off an active expression. */
1686 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
1688 partition = var_to_partition (map, def);
1689 if (partition != NO_PARTITION && tab->partition_dep_list[partition])
1690 kill_expr (tab, partition, true);
1693 /* Now see if we are creating a new expression or not. */
1694 if (!ann->has_volatile_ops)
1695 check_replaceable (tab, stmt);
1697 /* Free any unused dependency lists. */
1698 while ((p = tab->pending_dependence))
1700 tab->pending_dependence = p->next;
1701 free_value_expr (tab, p);
1704 /* A V_MAY_DEF kills any expression using a virtual operand. */
1705 if (NUM_V_MAY_DEFS (V_MAY_DEF_OPS (ann)) > 0)
1706 kill_virtual_exprs (tab, true);
1708 /* A V_MUST_DEF kills any expression using a virtual operand. */
1709 if (NUM_V_MUST_DEFS (V_MUST_DEF_OPS (ann)) > 0)
1710 kill_virtual_exprs (tab, true);
1715 /* This function is the driver routine for replacement of temporary expressions
1716 in the SSA->normal phase, operating on MAP. If there are replaceable
1717 expressions, a table is returned which maps SSA versions to the
1718 expressions they should be replaced with. A NULL_TREE indicates no
1719 replacement should take place. If there are no replacements at all,
1720 NULL is returned by the function, otherwise an expression vector indexed
1721 by SSA_NAME version numbers. */
1723 static tree *
1724 find_replaceable_exprs (var_map map)
1726 basic_block bb;
1727 unsigned i;
1728 temp_expr_table_p table;
1729 tree *ret;
1731 table = new_temp_expr_table (map);
1732 FOR_EACH_BB (bb)
1734 bitmap_iterator bi;
1736 find_replaceable_in_bb (table, bb);
1737 EXECUTE_IF_SET_IN_BITMAP ((table->partition_in_use), 0, i, bi)
1739 kill_expr (table, i, false);
1743 ret = free_temp_expr_table (table);
1744 return ret;
1748 /* Dump TER expression table EXPR to file F. */
1750 static void
1751 dump_replaceable_exprs (FILE *f, tree *expr)
1753 tree stmt, var;
1754 int x;
1755 fprintf (f, "\nReplacing Expressions\n");
1756 for (x = 0; x < (int)num_ssa_names + 1; x++)
1757 if (expr[x])
1759 stmt = expr[x];
1760 var = DEF_OP (STMT_DEF_OPS (stmt), 0);
1761 print_generic_expr (f, var, TDF_SLIM);
1762 fprintf (f, " replace with --> ");
1763 print_generic_expr (f, TREE_OPERAND (stmt, 1), TDF_SLIM);
1764 fprintf (f, "\n");
1766 fprintf (f, "\n");
1770 /* Helper function for discover_nonconstant_array_refs.
1771 Look for ARRAY_REF nodes with non-constant indexes and mark them
1772 addressable. */
1774 static tree
1775 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
1776 void *data ATTRIBUTE_UNUSED)
1778 tree t = *tp;
1780 if (IS_TYPE_OR_DECL_P (t))
1781 *walk_subtrees = 0;
1782 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1784 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1785 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
1786 && (!TREE_OPERAND (t, 2)
1787 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
1788 || (TREE_CODE (t) == COMPONENT_REF
1789 && (!TREE_OPERAND (t,2)
1790 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
1791 || TREE_CODE (t) == BIT_FIELD_REF
1792 || TREE_CODE (t) == REALPART_EXPR
1793 || TREE_CODE (t) == IMAGPART_EXPR
1794 || TREE_CODE (t) == VIEW_CONVERT_EXPR
1795 || TREE_CODE (t) == NOP_EXPR
1796 || TREE_CODE (t) == CONVERT_EXPR)
1797 t = TREE_OPERAND (t, 0);
1799 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1801 t = get_base_address (t);
1802 if (t && DECL_P (t))
1803 TREE_ADDRESSABLE (t) = 1;
1806 *walk_subtrees = 0;
1809 return NULL_TREE;
1813 /* RTL expansion is not able to compile array references with variable
1814 offsets for arrays stored in single register. Discover such
1815 expressions and mark variables as addressable to avoid this
1816 scenario. */
1818 static void
1819 discover_nonconstant_array_refs (void)
1821 basic_block bb;
1822 block_stmt_iterator bsi;
1824 FOR_EACH_BB (bb)
1826 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1827 walk_tree (bsi_stmt_ptr (bsi), discover_nonconstant_array_refs_r,
1828 NULL , NULL);
1833 /* This function will rewrite the current program using the variable mapping
1834 found in MAP. If the replacement vector VALUES is provided, any
1835 occurrences of partitions with non-null entries in the vector will be
1836 replaced with the expression in the vector instead of its mapped
1837 variable. */
1839 static void
1840 rewrite_trees (var_map map, tree *values)
1842 elim_graph g;
1843 basic_block bb;
1844 block_stmt_iterator si;
1845 edge e;
1846 tree phi;
1847 bool changed;
1849 #ifdef ENABLE_CHECKING
1850 /* Search for PHIs where the destination has no partition, but one
1851 or more arguments has a partition. This should not happen and can
1852 create incorrect code. */
1853 FOR_EACH_BB (bb)
1855 tree phi;
1857 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1859 tree T0 = var_to_partition_to_var (map, PHI_RESULT (phi));
1861 if (T0 == NULL_TREE)
1863 int i;
1865 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1867 tree arg = PHI_ARG_DEF (phi, i);
1869 if (TREE_CODE (arg) == SSA_NAME
1870 && var_to_partition (map, arg) != NO_PARTITION)
1872 fprintf (stderr, "Argument of PHI is in a partition :(");
1873 print_generic_expr (stderr, arg, TDF_SLIM);
1874 fprintf (stderr, "), but the result is not :");
1875 print_generic_stmt (stderr, phi, TDF_SLIM);
1876 internal_error ("SSA corruption");
1882 #endif
1884 /* Replace PHI nodes with any required copies. */
1885 g = new_elim_graph (map->num_partitions);
1886 g->map = map;
1887 FOR_EACH_BB (bb)
1889 for (si = bsi_start (bb); !bsi_end_p (si); )
1891 size_t num_uses, num_defs;
1892 use_optype uses;
1893 def_optype defs;
1894 tree stmt = bsi_stmt (si);
1895 use_operand_p use_p;
1896 def_operand_p def_p;
1897 int remove = 0, is_copy = 0;
1898 stmt_ann_t ann;
1899 ssa_op_iter iter;
1901 get_stmt_operands (stmt);
1902 ann = stmt_ann (stmt);
1903 changed = false;
1905 if (TREE_CODE (stmt) == MODIFY_EXPR
1906 && (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME))
1907 is_copy = 1;
1909 uses = USE_OPS (ann);
1910 num_uses = NUM_USES (uses);
1911 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1913 if (replace_use_variable (map, use_p, values))
1914 changed = true;
1917 defs = DEF_OPS (ann);
1918 num_defs = NUM_DEFS (defs);
1920 /* Mark this stmt for removal if it is the list of replaceable
1921 expressions. */
1922 if (values && num_defs == 1)
1924 tree def = DEF_OP (defs, 0);
1925 tree val;
1926 val = values[SSA_NAME_VERSION (def)];
1927 if (val)
1928 remove = 1;
1930 if (!remove)
1932 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_DEF)
1934 if (replace_def_variable (map, def_p, NULL))
1935 changed = true;
1937 /* If both SSA_NAMEs coalesce to the same variable,
1938 mark the now redundant copy for removal. */
1939 if (is_copy
1940 && num_uses == 1
1941 && (DEF_FROM_PTR (def_p) == USE_OP (uses, 0)))
1942 remove = 1;
1944 if (changed & !remove)
1945 modify_stmt (stmt);
1948 /* Remove any stmts marked for removal. */
1949 if (remove)
1950 bsi_remove (&si);
1951 else
1952 bsi_next (&si);
1955 phi = phi_nodes (bb);
1956 if (phi)
1958 edge_iterator ei;
1959 FOR_EACH_EDGE (e, ei, bb->preds)
1960 eliminate_phi (e, g);
1964 delete_elim_graph (g);
1968 /* These are the local work structures used to determine the best place to
1969 insert the copies that were placed on edges by the SSA->normal pass.. */
1970 static varray_type edge_leader = NULL;
1971 static varray_type GTY(()) stmt_list = NULL;
1972 static bitmap leader_has_match = NULL;
1973 static edge leader_match = NULL;
1976 /* Pass this function to make_forwarder_block so that all the edges with
1977 matching PENDING_STMT lists to 'curr_stmt_list' get redirected. */
1978 static bool
1979 same_stmt_list_p (edge e)
1981 return (e->aux == (PTR) leader_match) ? true : false;
1985 /* Return TRUE if S1 and S2 are equivalent copies. */
1986 static inline bool
1987 identical_copies_p (tree s1, tree s2)
1989 #ifdef ENABLE_CHECKING
1990 gcc_assert (TREE_CODE (s1) == MODIFY_EXPR);
1991 gcc_assert (TREE_CODE (s2) == MODIFY_EXPR);
1992 gcc_assert (DECL_P (TREE_OPERAND (s1, 0)));
1993 gcc_assert (DECL_P (TREE_OPERAND (s2, 0)));
1994 #endif
1996 if (TREE_OPERAND (s1, 0) != TREE_OPERAND (s2, 0))
1997 return false;
1999 s1 = TREE_OPERAND (s1, 1);
2000 s2 = TREE_OPERAND (s2, 1);
2002 if (s1 != s2)
2003 return false;
2005 return true;
2009 /* Compare the PENDING_STMT list for two edges, and return true if the lists
2010 contain the same sequence of copies. */
2012 static inline bool
2013 identical_stmt_lists_p (edge e1, edge e2)
2015 tree t1 = PENDING_STMT (e1);
2016 tree t2 = PENDING_STMT (e2);
2017 tree_stmt_iterator tsi1, tsi2;
2019 gcc_assert (TREE_CODE (t1) == STATEMENT_LIST);
2020 gcc_assert (TREE_CODE (t2) == STATEMENT_LIST);
2022 for (tsi1 = tsi_start (t1), tsi2 = tsi_start (t2);
2023 !tsi_end_p (tsi1) && !tsi_end_p (tsi2);
2024 tsi_next (&tsi1), tsi_next (&tsi2))
2026 if (!identical_copies_p (tsi_stmt (tsi1), tsi_stmt (tsi2)))
2027 break;
2030 if (!tsi_end_p (tsi1) || ! tsi_end_p (tsi2))
2031 return false;
2033 return true;
2037 /* Look at all the incoming edges to block BB, and decide where the best place
2038 to insert the stmts on each edge are, and perform those insertions. Output
2039 any debug information to DEBUG_FILE. Return true if anything other than a
2040 standard edge insertion is done. */
2042 static bool
2043 analyze_edges_for_bb (basic_block bb, FILE *debug_file)
2045 edge e;
2046 edge_iterator ei;
2047 int count;
2048 unsigned int x;
2049 bool have_opportunity;
2050 block_stmt_iterator bsi;
2051 tree stmt;
2052 edge single_edge = NULL;
2053 bool is_label;
2055 count = 0;
2057 /* Blocks which contain at least one abnormal edge cannot use
2058 make_forwarder_block. Look for these blocks, and commit any PENDING_STMTs
2059 found on edges in these block. */
2060 have_opportunity = true;
2061 FOR_EACH_EDGE (e, ei, bb->preds)
2062 if (e->flags & EDGE_ABNORMAL)
2064 have_opportunity = false;
2065 break;
2068 if (!have_opportunity)
2070 FOR_EACH_EDGE (e, ei, bb->preds)
2071 if (PENDING_STMT (e))
2072 bsi_commit_one_edge_insert (e, NULL);
2073 return false;
2075 /* Find out how many edges there are with interesting pending stmts on them.
2076 Commit the stmts on edges we are not interested in. */
2077 FOR_EACH_EDGE (e, ei, bb->preds)
2079 if (PENDING_STMT (e))
2081 gcc_assert (!(e->flags & EDGE_ABNORMAL));
2082 if (e->flags & EDGE_FALLTHRU)
2084 bsi = bsi_start (e->src);
2085 if (!bsi_end_p (bsi))
2087 stmt = bsi_stmt (bsi);
2088 bsi_next (&bsi);
2089 gcc_assert (stmt != NULL_TREE);
2090 is_label = (TREE_CODE (stmt) == LABEL_EXPR);
2091 /* Punt if it has non-label stmts, or isn't local. */
2092 if (!is_label || DECL_NONLOCAL (TREE_OPERAND (stmt, 0))
2093 || !bsi_end_p (bsi))
2095 bsi_commit_one_edge_insert (e, NULL);
2096 continue;
2100 single_edge = e;
2101 count++;
2105 /* If there aren't at least 2 edges, no sharing will happen. */
2106 if (count < 2)
2108 if (single_edge)
2109 bsi_commit_one_edge_insert (single_edge, NULL);
2110 return false;
2113 /* Ensure that we have empty worklists. */
2114 if (edge_leader == NULL)
2116 VARRAY_EDGE_INIT (edge_leader, 25, "edge_leader");
2117 VARRAY_TREE_INIT (stmt_list, 25, "stmt_list");
2118 leader_has_match = BITMAP_XMALLOC ();
2120 else
2122 #ifdef ENABLE_CHECKING
2123 gcc_assert (VARRAY_ACTIVE_SIZE (edge_leader) == 0);
2124 gcc_assert (VARRAY_ACTIVE_SIZE (stmt_list) == 0);
2125 gcc_assert (bitmap_empty_p (leader_has_match));
2126 #endif
2129 /* Find the "leader" block for each set of unique stmt lists. Preference is
2130 given to FALLTHRU blocks since they would need a GOTO to arrive at another
2131 block. The leader edge destination is the block which all the other edges
2132 with the same stmt list will be redirected to. */
2133 have_opportunity = false;
2134 FOR_EACH_EDGE (e, ei, bb->preds)
2136 if (PENDING_STMT (e))
2138 bool found = false;
2140 /* Look for the same stmt list in edge leaders list. */
2141 for (x = 0; x < VARRAY_ACTIVE_SIZE (edge_leader); x++)
2143 edge leader = VARRAY_EDGE (edge_leader, x);
2144 if (identical_stmt_lists_p (leader, e))
2146 /* Give this edge the same stmt list pointer. */
2147 PENDING_STMT (e) = NULL;
2148 e->aux = leader;
2149 bitmap_set_bit (leader_has_match, x);
2150 have_opportunity = found = true;
2151 break;
2155 /* If no similar stmt list, add this edge to the leader list. */
2156 if (!found)
2158 VARRAY_PUSH_EDGE (edge_leader, e);
2159 VARRAY_PUSH_TREE (stmt_list, PENDING_STMT (e));
2164 /* If there are no similar lists, just issue the stmts. */
2165 if (!have_opportunity)
2167 for (x = 0; x < VARRAY_ACTIVE_SIZE (edge_leader); x++)
2168 bsi_commit_one_edge_insert (VARRAY_EDGE (edge_leader, x), NULL);
2169 VARRAY_POP_ALL (edge_leader);
2170 VARRAY_POP_ALL (stmt_list);
2171 bitmap_clear (leader_has_match);
2172 return false;
2176 if (debug_file)
2177 fprintf (debug_file, "\nOpportunities in BB %d for stmt/block reduction:\n",
2178 bb->index);
2181 /* For each common list, create a forwarding block and issue the stmt's
2182 in that block. */
2183 for (x = 0 ; x < VARRAY_ACTIVE_SIZE (edge_leader); x++)
2184 if (bitmap_bit_p (leader_has_match, x))
2186 edge new_edge, leader_edge;
2187 block_stmt_iterator bsi;
2188 tree curr_stmt_list;
2190 leader_match = leader_edge = VARRAY_EDGE (edge_leader, x);
2192 /* The tree_* cfg manipulation routines use the PENDING_EDGE field
2193 for various PHI manipulations, so it gets cleared whhen calls are
2194 made to make_forwarder_block(). So make sure the edge is clear,
2195 and use the saved stmt list. */
2196 PENDING_STMT (leader_edge) = NULL;
2197 leader_edge->aux = leader_edge;
2198 curr_stmt_list = VARRAY_TREE (stmt_list, x);
2200 new_edge = make_forwarder_block (leader_edge->dest, same_stmt_list_p,
2201 NULL);
2202 bb = new_edge->dest;
2203 if (debug_file)
2205 fprintf (debug_file, "Splitting BB %d for Common stmt list. ",
2206 leader_edge->dest->index);
2207 fprintf (debug_file, "Original block is now BB%d.\n", bb->index);
2208 print_generic_stmt (debug_file, curr_stmt_list, TDF_VOPS);
2211 FOR_EACH_EDGE (e, ei, new_edge->src->preds)
2213 e->aux = NULL;
2214 if (debug_file)
2215 fprintf (debug_file, " Edge (%d->%d) lands here.\n",
2216 e->src->index, e->dest->index);
2219 bsi = bsi_last (leader_edge->dest);
2220 bsi_insert_after (&bsi, curr_stmt_list, BSI_NEW_STMT);
2222 leader_match = NULL;
2223 /* We should never get a new block now. */
2225 else
2227 e = VARRAY_EDGE (edge_leader, x);
2228 PENDING_STMT (e) = VARRAY_TREE (stmt_list, x);
2229 bsi_commit_one_edge_insert (e, NULL);
2233 /* Clear the working data structures. */
2234 VARRAY_POP_ALL (edge_leader);
2235 VARRAY_POP_ALL (stmt_list);
2236 bitmap_clear (leader_has_match);
2238 return true;
2242 /* This function will analyze the insertions which were performed on edges,
2243 and decide whether they should be left on that edge, or whether it is more
2244 efficient to emit some subset of them in a single block. All stmts are
2245 inserted somewhere, and if non-NULL, debug information is printed via
2246 DUMP_FILE. */
2248 static void
2249 perform_edge_inserts (FILE *dump_file)
2251 basic_block bb;
2252 bool changed = false;
2254 if (dump_file)
2255 fprintf(dump_file, "Analyzing Edge Insertions.\n");
2257 FOR_EACH_BB (bb)
2258 changed |= analyze_edges_for_bb (bb, dump_file);
2260 changed |= analyze_edges_for_bb (EXIT_BLOCK_PTR, dump_file);
2262 /* Clear out any tables which were created. */
2263 edge_leader = NULL;
2264 BITMAP_XFREE (leader_has_match);
2266 if (changed)
2268 free_dominance_info (CDI_DOMINATORS);
2269 free_dominance_info (CDI_POST_DOMINATORS);
2272 #ifdef ENABLE_CHECKING
2274 edge_iterator ei;
2275 edge e;
2276 FOR_EACH_BB (bb)
2278 FOR_EACH_EDGE (e, ei, bb->preds)
2280 if (PENDING_STMT (e))
2281 error (" Pending stmts not issued on PRED edge (%d, %d)\n",
2282 e->src->index, e->dest->index);
2284 FOR_EACH_EDGE (e, ei, bb->succs)
2286 if (PENDING_STMT (e))
2287 error (" Pending stmts not issued on SUCC edge (%d, %d)\n",
2288 e->src->index, e->dest->index);
2291 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2293 if (PENDING_STMT (e))
2294 error (" Pending stmts not issued on ENTRY edge (%d, %d)\n",
2295 e->src->index, e->dest->index);
2297 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
2299 if (PENDING_STMT (e))
2300 error (" Pending stmts not issued on EXIT edge (%d, %d)\n",
2301 e->src->index, e->dest->index);
2304 #endif
2308 /* Remove the variables specified in MAP from SSA form. Any debug information
2309 is sent to DUMP. FLAGS indicate what options should be used. */
2311 static void
2312 remove_ssa_form (FILE *dump, var_map map, int flags)
2314 tree_live_info_p liveinfo;
2315 basic_block bb;
2316 tree phi, next;
2317 FILE *save;
2318 tree *values = NULL;
2320 save = dump_file;
2321 dump_file = dump;
2323 /* If we are not combining temps, don't calculate live ranges for variables
2324 with only one SSA version. */
2325 if ((flags & SSANORM_COMBINE_TEMPS) == 0)
2326 compact_var_map (map, VARMAP_NO_SINGLE_DEFS);
2327 else
2328 compact_var_map (map, VARMAP_NORMAL);
2330 if (dump_file && (dump_flags & TDF_DETAILS))
2331 dump_var_map (dump_file, map);
2333 liveinfo = coalesce_ssa_name (map, flags);
2335 /* Make sure even single occurrence variables are in the list now. */
2336 if ((flags & SSANORM_COMBINE_TEMPS) == 0)
2337 compact_var_map (map, VARMAP_NORMAL);
2339 if (dump_file && (dump_flags & TDF_DETAILS))
2341 fprintf (dump_file, "After Coalescing:\n");
2342 dump_var_map (dump_file, map);
2345 if (flags & SSANORM_PERFORM_TER)
2347 values = find_replaceable_exprs (map);
2348 if (values && dump_file && (dump_flags & TDF_DETAILS))
2349 dump_replaceable_exprs (dump_file, values);
2352 /* Assign real variables to the partitions now. */
2353 assign_vars (map);
2355 if (dump_file && (dump_flags & TDF_DETAILS))
2357 fprintf (dump_file, "After Root variable replacement:\n");
2358 dump_var_map (dump_file, map);
2361 if ((flags & SSANORM_COMBINE_TEMPS) && liveinfo)
2363 coalesce_vars (map, liveinfo);
2364 if (dump_file && (dump_flags & TDF_DETAILS))
2366 fprintf (dump_file, "After variable memory coalescing:\n");
2367 dump_var_map (dump_file, map);
2371 if (liveinfo)
2372 delete_tree_live_info (liveinfo);
2374 rewrite_trees (map, values);
2376 if (values)
2377 free (values);
2379 /* Remove phi nodes which have been translated back to real variables. */
2380 FOR_EACH_BB (bb)
2382 for (phi = phi_nodes (bb); phi; phi = next)
2384 next = PHI_CHAIN (phi);
2385 if ((flags & SSANORM_REMOVE_ALL_PHIS)
2386 || var_to_partition (map, PHI_RESULT (phi)) != NO_PARTITION)
2387 remove_phi_node (phi, NULL_TREE, bb);
2391 /* If any copies were inserted on edges, analyze and insert them now. */
2392 perform_edge_inserts (dump_file);
2394 dump_file = save;
2397 /* Search every PHI node for arguments associated with backedges which
2398 we can trivially determine will need a copy (the argument is either
2399 not an SSA_NAME or the argument has a different underlying variable
2400 than the PHI result).
2402 Insert a copy from the PHI argument to a new destination at the
2403 end of the block with the backedge to the top of the loop. Update
2404 the PHI argument to reference this new destination. */
2406 static void
2407 insert_backedge_copies (void)
2409 basic_block bb;
2411 FOR_EACH_BB (bb)
2413 tree phi;
2415 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2417 tree result = PHI_RESULT (phi);
2418 tree result_var;
2419 int i;
2421 if (!is_gimple_reg (result))
2422 continue;
2424 result_var = SSA_NAME_VAR (result);
2425 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
2427 tree arg = PHI_ARG_DEF (phi, i);
2428 edge e = PHI_ARG_EDGE (phi, i);
2430 /* If the argument is not an SSA_NAME, then we will
2431 need a constant initialization. If the argument is
2432 an SSA_NAME with a different underlying variable and
2433 we are not combining temporaries, then we will
2434 need a copy statement. */
2435 if ((e->flags & EDGE_DFS_BACK)
2436 && (TREE_CODE (arg) != SSA_NAME
2437 || (!flag_tree_combine_temps
2438 && SSA_NAME_VAR (arg) != result_var)))
2440 tree stmt, name, last = NULL;
2441 block_stmt_iterator bsi;
2443 bsi = bsi_last (PHI_ARG_EDGE (phi, i)->src);
2444 if (!bsi_end_p (bsi))
2445 last = bsi_stmt (bsi);
2447 /* In theory the only way we ought to get back to the
2448 start of a loop should be with a COND_EXPR or GOTO_EXPR.
2449 However, better safe than sorry.
2451 If the block ends with a control statement or
2452 something that might throw, then we have to
2453 insert this assignment before the last
2454 statement. Else insert it after the last statement. */
2455 if (last && stmt_ends_bb_p (last))
2457 /* If the last statement in the block is the definition
2458 site of the PHI argument, then we can't insert
2459 anything after it. */
2460 if (TREE_CODE (arg) == SSA_NAME
2461 && SSA_NAME_DEF_STMT (arg) == last)
2462 continue;
2465 /* Create a new instance of the underlying
2466 variable of the PHI result. */
2467 stmt = build (MODIFY_EXPR, TREE_TYPE (result_var),
2468 NULL, PHI_ARG_DEF (phi, i));
2469 name = make_ssa_name (result_var, stmt);
2470 TREE_OPERAND (stmt, 0) = name;
2472 /* Insert the new statement into the block and update
2473 the PHI node. */
2474 if (last && stmt_ends_bb_p (last))
2475 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
2476 else
2477 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
2478 modify_stmt (stmt);
2479 SET_PHI_ARG_DEF (phi, i, name);
2486 /* Take the current function out of SSA form, as described in
2487 R. Morgan, ``Building an Optimizing Compiler'',
2488 Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
2490 static void
2491 rewrite_out_of_ssa (void)
2493 var_map map;
2494 int var_flags = 0;
2495 int ssa_flags = (SSANORM_REMOVE_ALL_PHIS | SSANORM_USE_COALESCE_LIST);
2497 /* If elimination of a PHI requires inserting a copy on a backedge,
2498 then we will have to split the backedge which has numerous
2499 undesirable performance effects.
2501 A significant number of such cases can be handled here by inserting
2502 copies into the loop itself. */
2503 insert_backedge_copies ();
2505 if (!flag_tree_live_range_split)
2506 ssa_flags |= SSANORM_COALESCE_PARTITIONS;
2508 eliminate_virtual_phis ();
2510 if (dump_file && (dump_flags & TDF_DETAILS))
2511 dump_tree_cfg (dump_file, dump_flags & ~TDF_DETAILS);
2513 /* We cannot allow unssa to un-gimplify trees before we instrument them. */
2514 if (flag_tree_ter && !flag_mudflap)
2515 var_flags = SSA_VAR_MAP_REF_COUNT;
2517 map = create_ssa_var_map (var_flags);
2519 if (flag_tree_combine_temps)
2520 ssa_flags |= SSANORM_COMBINE_TEMPS;
2521 if (flag_tree_ter && !flag_mudflap)
2522 ssa_flags |= SSANORM_PERFORM_TER;
2524 remove_ssa_form (dump_file, map, ssa_flags);
2526 if (dump_file && (dump_flags & TDF_DETAILS))
2527 dump_tree_cfg (dump_file, dump_flags & ~TDF_DETAILS);
2529 /* Do some cleanups which reduce the amount of data the
2530 tree->rtl expanders deal with. */
2531 cfg_remove_useless_stmts ();
2533 /* Flush out flow graph and SSA data. */
2534 delete_var_map (map);
2536 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
2537 discover_nonconstant_array_refs ();
2541 /* Define the parameters of the out of SSA pass. */
2543 struct tree_opt_pass pass_del_ssa =
2545 "optimized", /* name */
2546 NULL, /* gate */
2547 rewrite_out_of_ssa, /* execute */
2548 NULL, /* sub */
2549 NULL, /* next */
2550 0, /* static_pass_number */
2551 TV_TREE_SSA_TO_NORMAL, /* tv_id */
2552 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
2553 0, /* properties_provided */
2554 /* ??? If TER is enabled, we also kill gimple. */
2555 PROP_ssa, /* properties_destroyed */
2556 TODO_verify_ssa | TODO_verify_flow
2557 | TODO_verify_stmts, /* todo_flags_start */
2558 TODO_dump_func | TODO_ggc_collect, /* todo_flags_finish */
2559 0 /* letter */