Mark ChangeLog
[official-gcc.git] / gcc / tree-outof-ssa.c
blob9805c4029b66e9fff71ba814c86403b9061b7644
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_COALESCE_PARTITIONS 0x4
56 /* Used to hold all the components required to do SSA PHI elimination.
57 The node and pred/succ list is a simple linear list of nodes and
58 edges represented as pairs of nodes.
60 The predecessor and successor list: Nodes are entered in pairs, where
61 [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
62 predecessors, all the odd elements are successors.
64 Rationale:
65 When implemented as bitmaps, very large programs SSA->Normal times were
66 being dominated by clearing the interference graph.
68 Typically this list of edges is extremely small since it only includes
69 PHI results and uses from a single edge which have not coalesced with
70 each other. This means that no virtual PHI nodes are included, and
71 empirical evidence suggests that the number of edges rarely exceed
72 3, and in a bootstrap of GCC, the maximum size encountered was 7.
73 This also limits the number of possible nodes that are involved to
74 rarely more than 6, and in the bootstrap of gcc, the maximum number
75 of nodes encountered was 12. */
77 typedef struct _elim_graph {
78 /* Size of the elimination vectors. */
79 int size;
81 /* List of nodes in the elimination graph. */
82 varray_type nodes;
84 /* The predecessor and successor edge list. */
85 varray_type edge_list;
87 /* Visited vector. */
88 sbitmap visited;
90 /* Stack for visited nodes. */
91 varray_type stack;
93 /* The variable partition map. */
94 var_map map;
96 /* Edge being eliminated by this graph. */
97 edge e;
99 /* List of constant copies to emit. These are pushed on in pairs. */
100 varray_type const_copies;
101 } *elim_graph;
104 /* Local functions. */
105 static tree create_temp (tree);
106 static void insert_copy_on_edge (edge, tree, tree);
107 static elim_graph new_elim_graph (int);
108 static inline void delete_elim_graph (elim_graph);
109 static inline void clear_elim_graph (elim_graph);
110 static inline int elim_graph_size (elim_graph);
111 static inline void elim_graph_add_node (elim_graph, tree);
112 static inline void elim_graph_add_edge (elim_graph, int, int);
113 static inline int elim_graph_remove_succ_edge (elim_graph, int);
115 static inline void eliminate_name (elim_graph, tree);
116 static void eliminate_build (elim_graph, basic_block);
117 static void elim_forward (elim_graph, int);
118 static int elim_unvisited_predecessor (elim_graph, int);
119 static void elim_backward (elim_graph, int);
120 static void elim_create (elim_graph, int);
121 static void eliminate_phi (edge, elim_graph);
122 static tree_live_info_p coalesce_ssa_name (var_map, int);
123 static void assign_vars (var_map);
124 static bool replace_use_variable (var_map, use_operand_p, tree *);
125 static bool replace_def_variable (var_map, def_operand_p, tree *);
126 static void eliminate_virtual_phis (void);
127 static void coalesce_abnormal_edges (var_map, conflict_graph, root_var_p);
128 static void print_exprs (FILE *, const char *, tree, const char *, tree,
129 const char *);
130 static void print_exprs_edge (FILE *, edge, const char *, tree, const char *,
131 tree);
134 /* Create a temporary variable based on the type of variable T. Use T's name
135 as the prefix. */
137 static tree
138 create_temp (tree t)
140 tree tmp;
141 const char *name = NULL;
142 tree type;
144 if (TREE_CODE (t) == SSA_NAME)
145 t = SSA_NAME_VAR (t);
147 gcc_assert (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL);
149 type = TREE_TYPE (t);
150 tmp = DECL_NAME (t);
151 if (tmp)
152 name = IDENTIFIER_POINTER (tmp);
154 if (name == NULL)
155 name = "temp";
156 tmp = create_tmp_var (type, name);
158 if (DECL_DEBUG_EXPR (t) && DECL_DEBUG_EXPR_IS_FROM (t))
160 DECL_DEBUG_EXPR (tmp) = DECL_DEBUG_EXPR (t);
161 DECL_DEBUG_EXPR_IS_FROM (tmp) = 1;
163 else if (!DECL_IGNORED_P (t))
165 DECL_DEBUG_EXPR (tmp) = t;
166 DECL_DEBUG_EXPR_IS_FROM (tmp) = 1;
168 DECL_ARTIFICIAL (tmp) = DECL_ARTIFICIAL (t);
169 DECL_IGNORED_P (tmp) = DECL_IGNORED_P (t);
170 add_referenced_tmp_var (tmp);
172 /* add_referenced_tmp_var will create the annotation and set up some
173 of the flags in the annotation. However, some flags we need to
174 inherit from our original variable. */
175 var_ann (tmp)->type_mem_tag = var_ann (t)->type_mem_tag;
176 if (is_call_clobbered (t))
177 mark_call_clobbered (tmp);
179 return tmp;
183 /* This helper function fill insert a copy from a constant or variable SRC to
184 variable DEST on edge E. */
186 static void
187 insert_copy_on_edge (edge e, tree dest, tree src)
189 tree copy;
191 copy = build (MODIFY_EXPR, TREE_TYPE (dest), dest, src);
192 set_is_used (dest);
194 if (TREE_CODE (src) == ADDR_EXPR)
195 src = TREE_OPERAND (src, 0);
196 if (TREE_CODE (src) == VAR_DECL || TREE_CODE (src) == PARM_DECL)
197 set_is_used (src);
199 if (dump_file && (dump_flags & TDF_DETAILS))
201 fprintf (dump_file,
202 "Inserting a copy on edge BB%d->BB%d :",
203 e->src->index,
204 e->dest->index);
205 print_generic_expr (dump_file, copy, dump_flags);
206 fprintf (dump_file, "\n");
209 bsi_insert_on_edge (e, copy);
213 /* Create an elimination graph with SIZE nodes and associated data
214 structures. */
216 static elim_graph
217 new_elim_graph (int size)
219 elim_graph g = (elim_graph) xmalloc (sizeof (struct _elim_graph));
221 VARRAY_TREE_INIT (g->nodes, 30, "Elimination Node List");
222 VARRAY_TREE_INIT (g->const_copies, 20, "Elimination Constant Copies");
223 VARRAY_INT_INIT (g->edge_list, 20, "Elimination Edge List");
224 VARRAY_INT_INIT (g->stack, 30, " Elimination Stack");
226 g->visited = sbitmap_alloc (size);
228 return g;
232 /* Empty elimination graph G. */
234 static inline void
235 clear_elim_graph (elim_graph g)
237 VARRAY_POP_ALL (g->nodes);
238 VARRAY_POP_ALL (g->edge_list);
242 /* Delete elimination graph G. */
244 static inline void
245 delete_elim_graph (elim_graph g)
247 sbitmap_free (g->visited);
248 free (g);
252 /* Return the number of nodes in graph G. */
254 static inline int
255 elim_graph_size (elim_graph g)
257 return VARRAY_ACTIVE_SIZE (g->nodes);
261 /* Add NODE to graph G, if it doesn't exist already. */
263 static inline void
264 elim_graph_add_node (elim_graph g, tree node)
266 int x;
267 for (x = 0; x < elim_graph_size (g); x++)
268 if (VARRAY_TREE (g->nodes, x) == node)
269 return;
270 VARRAY_PUSH_TREE (g->nodes, node);
274 /* Add the edge PRED->SUCC to graph G. */
276 static inline void
277 elim_graph_add_edge (elim_graph g, int pred, int succ)
279 VARRAY_PUSH_INT (g->edge_list, pred);
280 VARRAY_PUSH_INT (g->edge_list, succ);
284 /* Remove an edge from graph G for which NODE is the predecessor, and
285 return the successor node. -1 is returned if there is no such edge. */
287 static inline int
288 elim_graph_remove_succ_edge (elim_graph g, int node)
290 int y;
291 unsigned x;
292 for (x = 0; x < VARRAY_ACTIVE_SIZE (g->edge_list); x += 2)
293 if (VARRAY_INT (g->edge_list, x) == node)
295 VARRAY_INT (g->edge_list, x) = -1;
296 y = VARRAY_INT (g->edge_list, x + 1);
297 VARRAY_INT (g->edge_list, x + 1) = -1;
298 return y;
300 return -1;
304 /* Find all the nodes in GRAPH which are successors to NODE in the
305 edge list. VAR will hold the partition number found. CODE is the
306 code fragment executed for every node found. */
308 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, CODE) \
309 do { \
310 unsigned x_; \
311 int y_; \
312 for (x_ = 0; x_ < VARRAY_ACTIVE_SIZE ((GRAPH)->edge_list); x_ += 2) \
314 y_ = VARRAY_INT ((GRAPH)->edge_list, x_); \
315 if (y_ != (NODE)) \
316 continue; \
317 (VAR) = VARRAY_INT ((GRAPH)->edge_list, x_ + 1); \
318 CODE; \
320 } while (0)
323 /* Find all the nodes which are predecessors of NODE in the edge list for
324 GRAPH. VAR will hold the partition number found. CODE is the
325 code fragment executed for every node found. */
327 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, CODE) \
328 do { \
329 unsigned x_; \
330 int y_; \
331 for (x_ = 0; x_ < VARRAY_ACTIVE_SIZE ((GRAPH)->edge_list); x_ += 2) \
333 y_ = VARRAY_INT ((GRAPH)->edge_list, x_ + 1); \
334 if (y_ != (NODE)) \
335 continue; \
336 (VAR) = VARRAY_INT ((GRAPH)->edge_list, x_); \
337 CODE; \
339 } while (0)
342 /* Add T to elimination graph G. */
344 static inline void
345 eliminate_name (elim_graph g, tree T)
347 elim_graph_add_node (g, T);
351 /* Build elimination graph G for basic block BB on incoming PHI edge
352 G->e. */
354 static void
355 eliminate_build (elim_graph g, basic_block B)
357 tree phi;
358 tree T0, Ti;
359 int p0, pi;
361 clear_elim_graph (g);
363 for (phi = phi_nodes (B); phi; phi = PHI_CHAIN (phi))
365 T0 = var_to_partition_to_var (g->map, PHI_RESULT (phi));
367 /* Ignore results which are not in partitions. */
368 if (T0 == NULL_TREE)
369 continue;
371 Ti = PHI_ARG_DEF (phi, g->e->dest_idx);
373 /* If this argument is a constant, or a SSA_NAME which is being
374 left in SSA form, just queue a copy to be emitted on this
375 edge. */
376 if (!phi_ssa_name_p (Ti)
377 || (TREE_CODE (Ti) == SSA_NAME
378 && var_to_partition (g->map, Ti) == NO_PARTITION))
380 /* Save constant copies until all other copies have been emitted
381 on this edge. */
382 VARRAY_PUSH_TREE (g->const_copies, T0);
383 VARRAY_PUSH_TREE (g->const_copies, Ti);
385 else
387 Ti = var_to_partition_to_var (g->map, Ti);
388 if (T0 != Ti)
390 eliminate_name (g, T0);
391 eliminate_name (g, Ti);
392 p0 = var_to_partition (g->map, T0);
393 pi = var_to_partition (g->map, Ti);
394 elim_graph_add_edge (g, p0, pi);
401 /* Push successors of T onto the elimination stack for G. */
403 static void
404 elim_forward (elim_graph g, int T)
406 int S;
407 SET_BIT (g->visited, T);
408 FOR_EACH_ELIM_GRAPH_SUCC (g, T, S,
410 if (!TEST_BIT (g->visited, S))
411 elim_forward (g, S);
413 VARRAY_PUSH_INT (g->stack, T);
417 /* Return 1 if there unvisited predecessors of T in graph G. */
419 static int
420 elim_unvisited_predecessor (elim_graph g, int T)
422 int P;
423 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
425 if (!TEST_BIT (g->visited, P))
426 return 1;
428 return 0;
431 /* Process predecessors first, and insert a copy. */
433 static void
434 elim_backward (elim_graph g, int T)
436 int P;
437 SET_BIT (g->visited, T);
438 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
440 if (!TEST_BIT (g->visited, P))
442 elim_backward (g, P);
443 insert_copy_on_edge (g->e,
444 partition_to_var (g->map, P),
445 partition_to_var (g->map, T));
450 /* Insert required copies for T in graph G. Check for a strongly connected
451 region, and create a temporary to break the cycle if one is found. */
453 static void
454 elim_create (elim_graph g, int T)
456 tree U;
457 int P, S;
459 if (elim_unvisited_predecessor (g, T))
461 U = create_temp (partition_to_var (g->map, T));
462 insert_copy_on_edge (g->e, U, partition_to_var (g->map, T));
463 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
465 if (!TEST_BIT (g->visited, P))
467 elim_backward (g, P);
468 insert_copy_on_edge (g->e, partition_to_var (g->map, P), U);
472 else
474 S = elim_graph_remove_succ_edge (g, T);
475 if (S != -1)
477 SET_BIT (g->visited, T);
478 insert_copy_on_edge (g->e,
479 partition_to_var (g->map, T),
480 partition_to_var (g->map, S));
486 /* Eliminate all the phi nodes on edge E in graph G. */
488 static void
489 eliminate_phi (edge e, elim_graph g)
491 int num_nodes = 0;
492 int x;
493 basic_block B = e->dest;
495 gcc_assert (VARRAY_ACTIVE_SIZE (g->const_copies) == 0);
497 /* Abnormal edges already have everything coalesced, or the coalescer
498 would have aborted. */
499 if (e->flags & EDGE_ABNORMAL)
500 return;
502 num_nodes = num_var_partitions (g->map);
503 g->e = e;
505 eliminate_build (g, B);
507 if (elim_graph_size (g) != 0)
509 sbitmap_zero (g->visited);
510 VARRAY_POP_ALL (g->stack);
512 for (x = 0; x < elim_graph_size (g); x++)
514 tree var = VARRAY_TREE (g->nodes, x);
515 int p = var_to_partition (g->map, var);
516 if (!TEST_BIT (g->visited, p))
517 elim_forward (g, p);
520 sbitmap_zero (g->visited);
521 while (VARRAY_ACTIVE_SIZE (g->stack) > 0)
523 x = VARRAY_TOP_INT (g->stack);
524 VARRAY_POP (g->stack);
525 if (!TEST_BIT (g->visited, x))
526 elim_create (g, x);
530 /* If there are any pending constant copies, issue them now. */
531 while (VARRAY_ACTIVE_SIZE (g->const_copies) > 0)
533 tree src, dest;
534 src = VARRAY_TOP_TREE (g->const_copies);
535 VARRAY_POP (g->const_copies);
536 dest = VARRAY_TOP_TREE (g->const_copies);
537 VARRAY_POP (g->const_copies);
538 insert_copy_on_edge (e, dest, src);
543 /* Shortcut routine to print messages to file F of the form:
544 "STR1 EXPR1 STR2 EXPR2 STR3." */
546 static void
547 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
548 tree expr2, const char *str3)
550 fprintf (f, "%s", str1);
551 print_generic_expr (f, expr1, TDF_SLIM);
552 fprintf (f, "%s", str2);
553 print_generic_expr (f, expr2, TDF_SLIM);
554 fprintf (f, "%s", str3);
558 /* Shortcut routine to print abnormal edge messages to file F of the form:
559 "STR1 EXPR1 STR2 EXPR2 across edge E. */
561 static void
562 print_exprs_edge (FILE *f, edge e, const char *str1, tree expr1,
563 const char *str2, tree expr2)
565 print_exprs (f, str1, expr1, str2, expr2, " across an abnormal edge");
566 fprintf (f, " from BB%d->BB%d\n", e->src->index,
567 e->dest->index);
571 /* Coalesce partitions in MAP which are live across abnormal edges in GRAPH.
572 RV is the root variable groupings of the partitions in MAP. Since code
573 cannot be inserted on these edges, failure to coalesce something across
574 an abnormal edge is an error. */
576 static void
577 coalesce_abnormal_edges (var_map map, conflict_graph graph, root_var_p rv)
579 basic_block bb;
580 edge e;
581 tree phi, var, tmp;
582 int x, y, z;
583 edge_iterator ei;
585 /* Code cannot be inserted on abnormal edges. Look for all abnormal
586 edges, and coalesce any PHI results with their arguments across
587 that edge. */
589 FOR_EACH_BB (bb)
590 FOR_EACH_EDGE (e, ei, bb->succs)
591 if (e->dest != EXIT_BLOCK_PTR && e->flags & EDGE_ABNORMAL)
592 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
594 /* Visit each PHI on the destination side of this abnormal
595 edge, and attempt to coalesce the argument with the result. */
596 var = PHI_RESULT (phi);
597 x = var_to_partition (map, var);
599 /* Ignore results which are not relevant. */
600 if (x == NO_PARTITION)
601 continue;
603 tmp = PHI_ARG_DEF (phi, e->dest_idx);
604 #ifdef ENABLE_CHECKING
605 if (!phi_ssa_name_p (tmp))
607 print_exprs_edge (stderr, e,
608 "\nConstant argument in PHI. Can't insert :",
609 var, " = ", tmp);
610 internal_error ("SSA corruption");
612 #else
613 gcc_assert (phi_ssa_name_p (tmp));
614 #endif
615 y = var_to_partition (map, tmp);
616 gcc_assert (x != NO_PARTITION);
617 gcc_assert (y != NO_PARTITION);
618 #ifdef ENABLE_CHECKING
619 if (root_var_find (rv, x) != root_var_find (rv, y))
621 print_exprs_edge (stderr, e, "\nDifferent root vars: ",
622 root_var (rv, root_var_find (rv, x)),
623 " and ",
624 root_var (rv, root_var_find (rv, y)));
625 internal_error ("SSA corruption");
627 #else
628 gcc_assert (root_var_find (rv, x) == root_var_find (rv, y));
629 #endif
631 if (x != y)
633 #ifdef ENABLE_CHECKING
634 if (conflict_graph_conflict_p (graph, x, y))
636 print_exprs_edge (stderr, e, "\n Conflict ",
637 partition_to_var (map, x),
638 " and ", partition_to_var (map, y));
639 internal_error ("SSA corruption");
641 #else
642 gcc_assert (!conflict_graph_conflict_p (graph, x, y));
643 #endif
645 /* Now map the partitions back to their real variables. */
646 var = partition_to_var (map, x);
647 tmp = partition_to_var (map, y);
648 if (dump_file && (dump_flags & TDF_DETAILS))
650 print_exprs_edge (dump_file, e,
651 "ABNORMAL: Coalescing ",
652 var, " and ", tmp);
654 z = var_union (map, var, tmp);
655 #ifdef ENABLE_CHECKING
656 if (z == NO_PARTITION)
658 print_exprs_edge (stderr, e, "\nUnable to coalesce",
659 partition_to_var (map, x), " and ",
660 partition_to_var (map, y));
661 internal_error ("SSA corruption");
663 #else
664 gcc_assert (z != NO_PARTITION);
665 #endif
666 gcc_assert (z == x || z == y);
667 if (z == x)
668 conflict_graph_merge_regs (graph, x, y);
669 else
670 conflict_graph_merge_regs (graph, y, x);
676 /* Coalesce potential copies via PHI arguments. */
678 static void
679 coalesce_phi_operands (var_map map, coalesce_list_p cl)
681 basic_block bb;
682 tree phi;
684 FOR_EACH_BB (bb)
686 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
688 tree res = PHI_RESULT (phi);
689 int p = var_to_partition (map, res);
690 int x;
692 if (p == NO_PARTITION)
693 continue;
694 for (x = 0; x < PHI_NUM_ARGS (phi); x++)
696 tree arg = PHI_ARG_DEF (phi, x);
697 int p2;
699 if (TREE_CODE (arg) != SSA_NAME)
700 continue;
701 if (SSA_NAME_VAR (res) != SSA_NAME_VAR (arg))
702 continue;
703 p2 = var_to_partition (map, PHI_ARG_DEF (phi, x));
704 if (p2 != NO_PARTITION)
705 add_coalesce (cl, p, p2, 1);
711 /* Coalesce all the result decls together. */
713 static void
714 coalesce_result_decls (var_map map, coalesce_list_p cl)
716 unsigned int i, x;
717 tree var = NULL;
719 for (i = x = 0; x < num_var_partitions (map); x++)
721 tree p = partition_to_var (map, x);
722 if (TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL)
724 if (var == NULL_TREE)
726 var = p;
727 i = x;
729 else
730 add_coalesce (cl, i, x, 1);
735 /* Coalesce matching constraints in asms. */
737 static void
738 coalesce_asm_operands (var_map map, coalesce_list_p cl)
740 basic_block bb;
742 FOR_EACH_BB (bb)
744 block_stmt_iterator bsi;
745 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
747 tree stmt = bsi_stmt (bsi);
748 unsigned long noutputs, i;
749 tree *outputs, link;
751 if (TREE_CODE (stmt) != ASM_EXPR)
752 continue;
754 noutputs = list_length (ASM_OUTPUTS (stmt));
755 outputs = (tree *) alloca (noutputs * sizeof (tree));
756 for (i = 0, link = ASM_OUTPUTS (stmt); link;
757 ++i, link = TREE_CHAIN (link))
758 outputs[i] = TREE_VALUE (link);
760 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
762 const char *constraint
763 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
764 tree input = TREE_VALUE (link);
765 char *end;
766 unsigned long match;
767 int p1, p2;
769 if (TREE_CODE (input) != SSA_NAME && !DECL_P (input))
770 continue;
772 match = strtoul (constraint, &end, 10);
773 if (match >= noutputs || end == constraint)
774 continue;
776 if (TREE_CODE (outputs[match]) != SSA_NAME
777 && !DECL_P (outputs[match]))
778 continue;
780 p1 = var_to_partition (map, outputs[match]);
781 if (p1 == NO_PARTITION)
782 continue;
783 p2 = var_to_partition (map, input);
784 if (p2 == NO_PARTITION)
785 continue;
787 add_coalesce (cl, p1, p2, 1);
793 /* Reduce the number of live ranges in MAP. Live range information is
794 returned if FLAGS indicates that we are combining temporaries, otherwise
795 NULL is returned. The only partitions which are associated with actual
796 variables at this point are those which are forced to be coalesced for
797 various reason. (live on entry, live across abnormal edges, etc.). */
799 static tree_live_info_p
800 coalesce_ssa_name (var_map map, int flags)
802 unsigned num, x;
803 sbitmap live;
804 root_var_p rv;
805 tree_live_info_p liveinfo;
806 conflict_graph graph;
807 coalesce_list_p cl = NULL;
809 if (num_var_partitions (map) <= 1)
810 return NULL;
812 liveinfo = calculate_live_on_entry (map);
813 calculate_live_on_exit (liveinfo);
814 rv = root_var_init (map);
816 /* Remove single element variable from the list. */
817 root_var_compact (rv);
819 cl = create_coalesce_list (map);
821 coalesce_phi_operands (map, cl);
822 coalesce_result_decls (map, cl);
823 coalesce_asm_operands (map, cl);
825 /* Build a conflict graph. */
826 graph = build_tree_conflict_graph (liveinfo, rv, cl);
828 if (cl)
830 if (dump_file && (dump_flags & TDF_DETAILS))
832 fprintf (dump_file, "Before sorting:\n");
833 dump_coalesce_list (dump_file, cl);
836 sort_coalesce_list (cl);
838 if (dump_file && (dump_flags & TDF_DETAILS))
840 fprintf (dump_file, "\nAfter sorting:\n");
841 dump_coalesce_list (dump_file, cl);
845 /* Put the single element variables back in. */
846 root_var_decompact (rv);
848 /* First, coalesce all live on entry variables to their root variable.
849 This will ensure the first use is coming from the correct location. */
851 num = num_var_partitions (map);
852 live = sbitmap_alloc (num);
853 sbitmap_zero (live);
855 /* Set 'live' vector to indicate live on entry partitions. */
856 for (x = 0 ; x < num; x++)
858 tree var = partition_to_var (map, x);
859 if (default_def (SSA_NAME_VAR (var)) == var)
860 SET_BIT (live, x);
863 if ((flags & SSANORM_COMBINE_TEMPS) == 0)
865 delete_tree_live_info (liveinfo);
866 liveinfo = NULL;
869 /* Assign root variable as partition representative for each live on entry
870 partition. */
871 EXECUTE_IF_SET_IN_SBITMAP (live, 0, x,
873 tree var = root_var (rv, root_var_find (rv, x));
874 var_ann_t ann = var_ann (var);
875 /* If these aren't already coalesced... */
876 if (partition_to_var (map, x) != var)
878 /* This root variable should have not already been assigned
879 to another partition which is not coalesced with this one. */
880 gcc_assert (!ann->out_of_ssa_tag);
882 if (dump_file && (dump_flags & TDF_DETAILS))
884 print_exprs (dump_file, "Must coalesce ",
885 partition_to_var (map, x),
886 " with the root variable ", var, ".\n");
889 change_partition_var (map, var, x);
893 sbitmap_free (live);
895 /* Coalesce partitions live across abnormal edges. */
896 coalesce_abnormal_edges (map, graph, rv);
898 if (dump_file && (dump_flags & TDF_DETAILS))
899 dump_var_map (dump_file, map);
901 /* Coalesce partitions. */
902 coalesce_tpa_members (rv, graph, map, cl,
903 ((dump_flags & TDF_DETAILS) ? dump_file
904 : NULL));
906 if (flags & SSANORM_COALESCE_PARTITIONS)
907 coalesce_tpa_members (rv, graph, map, NULL,
908 ((dump_flags & TDF_DETAILS) ? dump_file
909 : NULL));
910 if (cl)
911 delete_coalesce_list (cl);
912 root_var_delete (rv);
913 conflict_graph_delete (graph);
915 return liveinfo;
919 /* Take the ssa-name var_map MAP, and assign real variables to each
920 partition. */
922 static void
923 assign_vars (var_map map)
925 int x, i, num, rep;
926 tree t, var;
927 var_ann_t ann;
928 root_var_p rv;
930 rv = root_var_init (map);
931 if (!rv)
932 return;
934 /* Coalescing may already have forced some partitions to their root
935 variable. Find these and tag them. */
937 num = num_var_partitions (map);
938 for (x = 0; x < num; x++)
940 var = partition_to_var (map, x);
941 if (TREE_CODE (var) != SSA_NAME)
943 /* Coalescing will already have verified that more than one
944 partition doesn't have the same root variable. Simply marked
945 the variable as assigned. */
946 ann = var_ann (var);
947 ann->out_of_ssa_tag = 1;
948 if (dump_file && (dump_flags & TDF_DETAILS))
950 fprintf (dump_file, "partition %d has variable ", x);
951 print_generic_expr (dump_file, var, TDF_SLIM);
952 fprintf (dump_file, " assigned to it.\n");
958 num = root_var_num (rv);
959 for (x = 0; x < num; x++)
961 var = root_var (rv, x);
962 ann = var_ann (var);
963 for (i = root_var_first_partition (rv, x);
964 i != ROOT_VAR_NONE;
965 i = root_var_next_partition (rv, i))
967 t = partition_to_var (map, i);
969 if (t == var || TREE_CODE (t) != SSA_NAME)
970 continue;
972 rep = var_to_partition (map, t);
974 if (!ann->out_of_ssa_tag)
976 if (dump_file && (dump_flags & TDF_DETAILS))
977 print_exprs (dump_file, "", t, " --> ", var, "\n");
978 change_partition_var (map, var, rep);
979 continue;
982 if (dump_file && (dump_flags & TDF_DETAILS))
983 print_exprs (dump_file, "", t, " not coalesced with ", var,
984 "");
986 var = create_temp (t);
987 change_partition_var (map, var, rep);
988 ann = var_ann (var);
990 if (dump_file && (dump_flags & TDF_DETAILS))
992 fprintf (dump_file, " --> New temp: '");
993 print_generic_expr (dump_file, var, TDF_SLIM);
994 fprintf (dump_file, "'\n");
999 root_var_delete (rv);
1003 /* Replace use operand P with whatever variable it has been rewritten to based
1004 on the partitions in MAP. EXPR is an optional expression vector over SSA
1005 versions which is used to replace P with an expression instead of a variable.
1006 If the stmt is changed, return true. */
1008 static inline bool
1009 replace_use_variable (var_map map, use_operand_p p, tree *expr)
1011 tree new_var;
1012 tree var = USE_FROM_PTR (p);
1014 /* Check if we are replacing this variable with an expression. */
1015 if (expr)
1017 int version = SSA_NAME_VERSION (var);
1018 if (expr[version])
1020 tree new_expr = TREE_OPERAND (expr[version], 1);
1021 SET_USE (p, new_expr);
1022 /* Clear the stmt's RHS, or GC might bite us. */
1023 TREE_OPERAND (expr[version], 1) = NULL_TREE;
1024 return true;
1028 new_var = var_to_partition_to_var (map, var);
1029 if (new_var)
1031 SET_USE (p, new_var);
1032 set_is_used (new_var);
1033 return true;
1035 return false;
1039 /* Replace def operand DEF_P with whatever variable it has been rewritten to
1040 based on the partitions in MAP. EXPR is an optional expression vector over
1041 SSA versions which is used to replace DEF_P with an expression instead of a
1042 variable. If the stmt is changed, return true. */
1044 static inline bool
1045 replace_def_variable (var_map map, def_operand_p def_p, tree *expr)
1047 tree new_var;
1048 tree var = DEF_FROM_PTR (def_p);
1050 /* Check if we are replacing this variable with an expression. */
1051 if (expr)
1053 int version = SSA_NAME_VERSION (var);
1054 if (expr[version])
1056 tree new_expr = TREE_OPERAND (expr[version], 1);
1057 SET_DEF (def_p, new_expr);
1058 /* Clear the stmt's RHS, or GC might bite us. */
1059 TREE_OPERAND (expr[version], 1) = NULL_TREE;
1060 return true;
1064 new_var = var_to_partition_to_var (map, var);
1065 if (new_var)
1067 SET_DEF (def_p, new_var);
1068 set_is_used (new_var);
1069 return true;
1071 return false;
1075 /* Remove any PHI node which is a virtual PHI. */
1077 static void
1078 eliminate_virtual_phis (void)
1080 basic_block bb;
1081 tree phi, next;
1083 FOR_EACH_BB (bb)
1085 for (phi = phi_nodes (bb); phi; phi = next)
1087 next = PHI_CHAIN (phi);
1088 if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi))))
1090 #ifdef ENABLE_CHECKING
1091 int i;
1092 /* There should be no arguments of this PHI which are in
1093 the partition list, or we get incorrect results. */
1094 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1096 tree arg = PHI_ARG_DEF (phi, i);
1097 if (TREE_CODE (arg) == SSA_NAME
1098 && is_gimple_reg (SSA_NAME_VAR (arg)))
1100 fprintf (stderr, "Argument of PHI is not virtual (");
1101 print_generic_expr (stderr, arg, TDF_SLIM);
1102 fprintf (stderr, "), but the result is :");
1103 print_generic_stmt (stderr, phi, TDF_SLIM);
1104 internal_error ("SSA corruption");
1107 #endif
1108 remove_phi_node (phi, NULL_TREE, bb);
1115 /* This routine will coalesce variables in MAP of the same type which do not
1116 interfere with each other. LIVEINFO is the live range info for variables
1117 of interest. This will both reduce the memory footprint of the stack, and
1118 allow us to coalesce together local copies of globals and scalarized
1119 component refs. */
1121 static void
1122 coalesce_vars (var_map map, tree_live_info_p liveinfo)
1124 basic_block bb;
1125 type_var_p tv;
1126 tree var;
1127 unsigned x, p, p2;
1128 coalesce_list_p cl;
1129 conflict_graph graph;
1131 cl = create_coalesce_list (map);
1133 /* Merge all the live on entry vectors for coalesced partitions. */
1134 for (x = 0; x < num_var_partitions (map); x++)
1136 var = partition_to_var (map, x);
1137 p = var_to_partition (map, var);
1138 if (p != x)
1139 live_merge_and_clear (liveinfo, p, x);
1142 /* When PHI nodes are turned into copies, the result of each PHI node
1143 becomes live on entry to the block. Mark these now. */
1144 FOR_EACH_BB (bb)
1146 tree phi, arg;
1147 unsigned p;
1149 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1151 p = var_to_partition (map, PHI_RESULT (phi));
1153 /* Skip virtual PHI nodes. */
1154 if (p == (unsigned)NO_PARTITION)
1155 continue;
1157 make_live_on_entry (liveinfo, bb, p);
1159 /* Each argument is a potential copy operation. Add any arguments
1160 which are not coalesced to the result to the coalesce list. */
1161 for (x = 0; x < (unsigned)PHI_NUM_ARGS (phi); x++)
1163 arg = PHI_ARG_DEF (phi, x);
1164 if (!phi_ssa_name_p (arg))
1165 continue;
1166 p2 = var_to_partition (map, arg);
1167 if (p2 == (unsigned)NO_PARTITION)
1168 continue;
1169 if (p != p2)
1170 add_coalesce (cl, p, p2, 1);
1176 /* Re-calculate live on exit info. */
1177 calculate_live_on_exit (liveinfo);
1179 if (dump_file && (dump_flags & TDF_DETAILS))
1181 fprintf (dump_file, "Live range info for variable memory coalescing.\n");
1182 dump_live_info (dump_file, liveinfo, LIVEDUMP_ALL);
1184 fprintf (dump_file, "Coalesce list from phi nodes:\n");
1185 dump_coalesce_list (dump_file, cl);
1189 tv = type_var_init (map);
1190 if (dump_file)
1191 type_var_dump (dump_file, tv);
1192 type_var_compact (tv);
1193 if (dump_file)
1194 type_var_dump (dump_file, tv);
1196 graph = build_tree_conflict_graph (liveinfo, tv, cl);
1198 type_var_decompact (tv);
1199 if (dump_file && (dump_flags & TDF_DETAILS))
1201 fprintf (dump_file, "type var list now looks like:n");
1202 type_var_dump (dump_file, tv);
1204 fprintf (dump_file, "Coalesce list after conflict graph build:\n");
1205 dump_coalesce_list (dump_file, cl);
1208 sort_coalesce_list (cl);
1209 if (dump_file && (dump_flags & TDF_DETAILS))
1211 fprintf (dump_file, "Coalesce list after sorting:\n");
1212 dump_coalesce_list (dump_file, cl);
1215 coalesce_tpa_members (tv, graph, map, cl,
1216 ((dump_flags & TDF_DETAILS) ? dump_file : NULL));
1218 type_var_delete (tv);
1219 delete_coalesce_list (cl);
1223 /* Temporary Expression Replacement (TER)
1225 Replace SSA version variables during out-of-ssa with their defining
1226 expression if there is only one use of the variable.
1228 A pass is made through the function, one block at a time. No cross block
1229 information is tracked.
1231 Variables which only have one use, and whose defining stmt is considered
1232 a replaceable expression (see check_replaceable) are entered into
1233 consideration by adding a list of dependent partitions to the version_info
1234 vector for that ssa_name_version. This information comes from the partition
1235 mapping for each USE. At the same time, the partition_dep_list vector for
1236 these partitions have this version number entered into their lists.
1238 When the use of a replaceable ssa_variable is encountered, the dependence
1239 list in version_info[] is moved to the "pending_dependence" list in case
1240 the current expression is also replaceable. (To be determined later in
1241 processing this stmt.) version_info[] for the version is then updated to
1242 point to the defining stmt and the 'replaceable' bit is set.
1244 Any partition which is defined by a statement 'kills' any expression which
1245 is dependent on this partition. Every ssa version in the partitions'
1246 dependence list is removed from future consideration.
1248 All virtual references are lumped together. Any expression which is
1249 dependent on any virtual variable (via a VUSE) has a dependence added
1250 to the special partition defined by VIRTUAL_PARTITION.
1252 Whenever a V_MAY_DEF is seen, all expressions dependent this
1253 VIRTUAL_PARTITION are removed from consideration.
1255 At the end of a basic block, all expression are removed from consideration
1256 in preparation for the next block.
1258 The end result is a vector over SSA_NAME_VERSION which is passed back to
1259 rewrite_out_of_ssa. As the SSA variables are being rewritten, instead of
1260 replacing the SSA_NAME tree element with the partition it was assigned,
1261 it is replaced with the RHS of the defining expression. */
1264 /* Dependency list element. This can contain either a partition index or a
1265 version number, depending on which list it is in. */
1267 typedef struct value_expr_d
1269 int value;
1270 struct value_expr_d *next;
1271 } *value_expr_p;
1274 /* Temporary Expression Replacement (TER) table information. */
1276 typedef struct temp_expr_table_d
1278 var_map map;
1279 void **version_info;
1280 value_expr_p *partition_dep_list;
1281 bitmap replaceable;
1282 bool saw_replaceable;
1283 int virtual_partition;
1284 bitmap partition_in_use;
1285 value_expr_p free_list;
1286 value_expr_p pending_dependence;
1287 } *temp_expr_table_p;
1289 /* Used to indicate a dependency on V_MAY_DEFs. */
1290 #define VIRTUAL_PARTITION(table) (table->virtual_partition)
1292 static temp_expr_table_p new_temp_expr_table (var_map);
1293 static tree *free_temp_expr_table (temp_expr_table_p);
1294 static inline value_expr_p new_value_expr (temp_expr_table_p);
1295 static inline void free_value_expr (temp_expr_table_p, value_expr_p);
1296 static inline value_expr_p find_value_in_list (value_expr_p, int,
1297 value_expr_p *);
1298 static inline void add_value_to_list (temp_expr_table_p, value_expr_p *, int);
1299 static inline void add_info_to_list (temp_expr_table_p, value_expr_p *,
1300 value_expr_p);
1301 static value_expr_p remove_value_from_list (value_expr_p *, int);
1302 static void add_dependance (temp_expr_table_p, int, tree);
1303 static bool check_replaceable (temp_expr_table_p, tree);
1304 static void finish_expr (temp_expr_table_p, int, bool);
1305 static void mark_replaceable (temp_expr_table_p, tree);
1306 static inline void kill_expr (temp_expr_table_p, int, bool);
1307 static inline void kill_virtual_exprs (temp_expr_table_p, bool);
1308 static void find_replaceable_in_bb (temp_expr_table_p, basic_block);
1309 static tree *find_replaceable_exprs (var_map);
1310 static void dump_replaceable_exprs (FILE *, tree *);
1313 /* Create a new TER table for MAP. */
1315 static temp_expr_table_p
1316 new_temp_expr_table (var_map map)
1318 temp_expr_table_p t;
1320 t = (temp_expr_table_p) xmalloc (sizeof (struct temp_expr_table_d));
1321 t->map = map;
1323 t->version_info = xcalloc (num_ssa_names + 1, sizeof (void *));
1324 t->partition_dep_list = xcalloc (num_var_partitions (map) + 1,
1325 sizeof (value_expr_p));
1327 t->replaceable = BITMAP_ALLOC (NULL);
1328 t->partition_in_use = BITMAP_ALLOC (NULL);
1330 t->saw_replaceable = false;
1331 t->virtual_partition = num_var_partitions (map);
1332 t->free_list = NULL;
1333 t->pending_dependence = NULL;
1335 return t;
1339 /* Free TER table T. If there are valid replacements, return the expression
1340 vector. */
1342 static tree *
1343 free_temp_expr_table (temp_expr_table_p t)
1345 value_expr_p p;
1346 tree *ret = NULL;
1348 #ifdef ENABLE_CHECKING
1349 unsigned x;
1350 for (x = 0; x <= num_var_partitions (t->map); x++)
1351 gcc_assert (!t->partition_dep_list[x]);
1352 #endif
1354 while ((p = t->free_list))
1356 t->free_list = p->next;
1357 free (p);
1360 BITMAP_FREE (t->partition_in_use);
1361 BITMAP_FREE (t->replaceable);
1363 free (t->partition_dep_list);
1364 if (t->saw_replaceable)
1365 ret = (tree *)t->version_info;
1366 else
1367 free (t->version_info);
1369 free (t);
1370 return ret;
1374 /* Allocate a new value list node. Take it from the free list in TABLE if
1375 possible. */
1377 static inline value_expr_p
1378 new_value_expr (temp_expr_table_p table)
1380 value_expr_p p;
1381 if (table->free_list)
1383 p = table->free_list;
1384 table->free_list = p->next;
1386 else
1387 p = (value_expr_p) xmalloc (sizeof (struct value_expr_d));
1389 return p;
1393 /* Add value list node P to the free list in TABLE. */
1395 static inline void
1396 free_value_expr (temp_expr_table_p table, value_expr_p p)
1398 p->next = table->free_list;
1399 table->free_list = p;
1403 /* Find VALUE if it's in LIST. Return a pointer to the list object if found,
1404 else return NULL. If LAST_PTR is provided, it will point to the previous
1405 item upon return, or NULL if this is the first item in the list. */
1407 static inline value_expr_p
1408 find_value_in_list (value_expr_p list, int value, value_expr_p *last_ptr)
1410 value_expr_p curr;
1411 value_expr_p last = NULL;
1413 for (curr = list; curr; last = curr, curr = curr->next)
1415 if (curr->value == value)
1416 break;
1418 if (last_ptr)
1419 *last_ptr = last;
1420 return curr;
1424 /* Add VALUE to LIST, if it isn't already present. TAB is the expression
1425 table */
1427 static inline void
1428 add_value_to_list (temp_expr_table_p tab, value_expr_p *list, int value)
1430 value_expr_p info;
1432 if (!find_value_in_list (*list, value, NULL))
1434 info = new_value_expr (tab);
1435 info->value = value;
1436 info->next = *list;
1437 *list = info;
1442 /* Add value node INFO if it's value isn't already in LIST. Free INFO if
1443 it is already in the list. TAB is the expression table. */
1445 static inline void
1446 add_info_to_list (temp_expr_table_p tab, value_expr_p *list, value_expr_p info)
1448 if (find_value_in_list (*list, info->value, NULL))
1449 free_value_expr (tab, info);
1450 else
1452 info->next = *list;
1453 *list = info;
1458 /* Look for VALUE in LIST. If found, remove it from the list and return it's
1459 pointer. */
1461 static value_expr_p
1462 remove_value_from_list (value_expr_p *list, int value)
1464 value_expr_p info, last;
1466 info = find_value_in_list (*list, value, &last);
1467 if (!info)
1468 return NULL;
1469 if (!last)
1470 *list = info->next;
1471 else
1472 last->next = info->next;
1474 return info;
1478 /* Add a dependency between the def of ssa VERSION and VAR. If VAR is
1479 replaceable by an expression, add a dependence each of the elements of the
1480 expression. These are contained in the pending list. TAB is the
1481 expression table. */
1483 static void
1484 add_dependance (temp_expr_table_p tab, int version, tree var)
1486 int i, x;
1487 value_expr_p info;
1489 i = SSA_NAME_VERSION (var);
1490 if (bitmap_bit_p (tab->replaceable, i))
1492 /* This variable is being substituted, so use whatever dependences
1493 were queued up when we marked this as replaceable earlier. */
1494 while ((info = tab->pending_dependence))
1496 tab->pending_dependence = info->next;
1497 /* Get the partition this variable was dependent on. Reuse this
1498 object to represent the current expression instead. */
1499 x = info->value;
1500 info->value = version;
1501 add_info_to_list (tab, &(tab->partition_dep_list[x]), info);
1502 add_value_to_list (tab,
1503 (value_expr_p *)&(tab->version_info[version]), x);
1504 bitmap_set_bit (tab->partition_in_use, x);
1507 else
1509 i = var_to_partition (tab->map, var);
1510 gcc_assert (i != NO_PARTITION);
1511 add_value_to_list (tab, &(tab->partition_dep_list[i]), version);
1512 add_value_to_list (tab,
1513 (value_expr_p *)&(tab->version_info[version]), i);
1514 bitmap_set_bit (tab->partition_in_use, i);
1519 /* Check if expression STMT is suitable for replacement in table TAB. If so,
1520 create an expression entry. Return true if this stmt is replaceable. */
1522 static bool
1523 check_replaceable (temp_expr_table_p tab, tree stmt)
1525 stmt_ann_t ann;
1526 vuse_optype vuseops;
1527 def_optype defs;
1528 use_optype uses;
1529 tree var, def;
1530 int num_use_ops, version;
1531 var_map map = tab->map;
1532 ssa_op_iter iter;
1533 tree call_expr;
1535 if (TREE_CODE (stmt) != MODIFY_EXPR)
1536 return false;
1538 ann = stmt_ann (stmt);
1539 defs = DEF_OPS (ann);
1541 /* Punt if there is more than 1 def, or more than 1 use. */
1542 if (NUM_DEFS (defs) != 1)
1543 return false;
1544 def = DEF_OP (defs, 0);
1545 if (version_ref_count (map, def) != 1)
1546 return false;
1548 /* There must be no V_MAY_DEFS. */
1549 if (NUM_V_MAY_DEFS (V_MAY_DEF_OPS (ann)) != 0)
1550 return false;
1552 /* There must be no V_MUST_DEFS. */
1553 if (NUM_V_MUST_DEFS (V_MUST_DEF_OPS (ann)) != 0)
1554 return false;
1556 /* Float expressions must go through memory if float-store is on. */
1557 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 1))))
1558 return false;
1560 /* Calls to functions with side-effects cannot be replaced. */
1561 if ((call_expr = get_call_expr_in (stmt)) != NULL_TREE)
1563 int call_flags = call_expr_flags (call_expr);
1564 if (TREE_SIDE_EFFECTS (call_expr)
1565 && !(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
1566 return false;
1569 uses = USE_OPS (ann);
1570 num_use_ops = NUM_USES (uses);
1571 vuseops = VUSE_OPS (ann);
1573 /* Any expression which has no virtual operands and no real operands
1574 should have been propagated if it's possible to do anything with them.
1575 If this happens here, it probably exists that way for a reason, so we
1576 won't touch it. An example is:
1577 b_4 = &tab
1578 There are no virtual uses nor any real uses, so we just leave this
1579 alone to be safe. */
1581 if (num_use_ops == 0 && NUM_VUSES (vuseops) == 0)
1582 return false;
1584 version = SSA_NAME_VERSION (def);
1586 /* Add this expression to the dependency list for each use partition. */
1587 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
1589 add_dependance (tab, version, var);
1592 /* If there are VUSES, add a dependence on virtual defs. */
1593 if (NUM_VUSES (vuseops) != 0)
1595 add_value_to_list (tab, (value_expr_p *)&(tab->version_info[version]),
1596 VIRTUAL_PARTITION (tab));
1597 add_value_to_list (tab,
1598 &(tab->partition_dep_list[VIRTUAL_PARTITION (tab)]),
1599 version);
1600 bitmap_set_bit (tab->partition_in_use, VIRTUAL_PARTITION (tab));
1603 return true;
1607 /* This function will remove the expression for VERSION from replacement
1608 consideration.n table TAB If 'replace' is true, it is marked as
1609 replaceable, otherwise not. */
1611 static void
1612 finish_expr (temp_expr_table_p tab, int version, bool replace)
1614 value_expr_p info, tmp;
1615 int partition;
1617 /* Remove this expression from its dependent lists. The partition dependence
1618 list is retained and transfered later to whomever uses this version. */
1619 for (info = (value_expr_p) tab->version_info[version]; info; info = tmp)
1621 partition = info->value;
1622 gcc_assert (tab->partition_dep_list[partition]);
1623 tmp = remove_value_from_list (&(tab->partition_dep_list[partition]),
1624 version);
1625 gcc_assert (tmp);
1626 free_value_expr (tab, tmp);
1627 /* Only clear the bit when the dependency list is emptied via
1628 a replacement. Otherwise kill_expr will take care of it. */
1629 if (!(tab->partition_dep_list[partition]) && replace)
1630 bitmap_clear_bit (tab->partition_in_use, partition);
1631 tmp = info->next;
1632 if (!replace)
1633 free_value_expr (tab, info);
1636 if (replace)
1638 tab->saw_replaceable = true;
1639 bitmap_set_bit (tab->replaceable, version);
1641 else
1643 gcc_assert (!bitmap_bit_p (tab->replaceable, version));
1644 tab->version_info[version] = NULL;
1649 /* Mark the expression associated with VAR as replaceable, and enter
1650 the defining stmt into the version_info table TAB. */
1652 static void
1653 mark_replaceable (temp_expr_table_p tab, tree var)
1655 value_expr_p info;
1656 int version = SSA_NAME_VERSION (var);
1657 finish_expr (tab, version, true);
1659 /* Move the dependence list to the pending list. */
1660 if (tab->version_info[version])
1662 info = (value_expr_p) tab->version_info[version];
1663 for ( ; info->next; info = info->next)
1664 continue;
1665 info->next = tab->pending_dependence;
1666 tab->pending_dependence = (value_expr_p)tab->version_info[version];
1669 tab->version_info[version] = SSA_NAME_DEF_STMT (var);
1673 /* This function marks any expression in TAB which is dependent on PARTITION
1674 as NOT replaceable. CLEAR_BIT is used to determine whether partition_in_use
1675 should have its bit cleared. Since this routine can be called within an
1676 EXECUTE_IF_SET_IN_BITMAP, the bit can't always be cleared. */
1678 static inline void
1679 kill_expr (temp_expr_table_p tab, int partition, bool clear_bit)
1681 value_expr_p ptr;
1683 /* Mark every active expr dependent on this var as not replaceable. */
1684 while ((ptr = tab->partition_dep_list[partition]) != NULL)
1685 finish_expr (tab, ptr->value, false);
1687 if (clear_bit)
1688 bitmap_clear_bit (tab->partition_in_use, partition);
1692 /* This function kills all expressions in TAB which are dependent on virtual
1693 DEFs. CLEAR_BIT determines whether partition_in_use gets cleared. */
1695 static inline void
1696 kill_virtual_exprs (temp_expr_table_p tab, bool clear_bit)
1698 kill_expr (tab, VIRTUAL_PARTITION (tab), clear_bit);
1702 /* This function processes basic block BB, and looks for variables which can
1703 be replaced by their expressions. Results are stored in TAB. */
1705 static void
1706 find_replaceable_in_bb (temp_expr_table_p tab, basic_block bb)
1708 block_stmt_iterator bsi;
1709 tree stmt, def;
1710 stmt_ann_t ann;
1711 int partition;
1712 var_map map = tab->map;
1713 value_expr_p p;
1714 ssa_op_iter iter;
1716 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1718 stmt = bsi_stmt (bsi);
1719 ann = stmt_ann (stmt);
1721 /* Determine if this stmt finishes an existing expression. */
1722 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_USE)
1724 if (tab->version_info[SSA_NAME_VERSION (def)])
1726 bool same_root_var = false;
1727 tree def2;
1728 ssa_op_iter iter2;
1730 /* See if the root variables are the same. If they are, we
1731 do not want to do the replacement to avoid problems with
1732 code size, see PR tree-optimization/17549. */
1733 FOR_EACH_SSA_TREE_OPERAND (def2, stmt, iter2, SSA_OP_DEF)
1734 if (SSA_NAME_VAR (def) == SSA_NAME_VAR (def2))
1736 same_root_var = true;
1737 break;
1740 /* Mark expression as replaceable unless stmt is volatile
1741 or DEF sets the same root variable as STMT. */
1742 if (!ann->has_volatile_ops && !same_root_var)
1743 mark_replaceable (tab, def);
1744 else
1745 finish_expr (tab, SSA_NAME_VERSION (def), false);
1749 /* Next, see if this stmt kills off an active expression. */
1750 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
1752 partition = var_to_partition (map, def);
1753 if (partition != NO_PARTITION && tab->partition_dep_list[partition])
1754 kill_expr (tab, partition, true);
1757 /* Now see if we are creating a new expression or not. */
1758 if (!ann->has_volatile_ops)
1759 check_replaceable (tab, stmt);
1761 /* Free any unused dependency lists. */
1762 while ((p = tab->pending_dependence))
1764 tab->pending_dependence = p->next;
1765 free_value_expr (tab, p);
1768 /* A V_MAY_DEF kills any expression using a virtual operand. */
1769 if (NUM_V_MAY_DEFS (V_MAY_DEF_OPS (ann)) > 0)
1770 kill_virtual_exprs (tab, true);
1772 /* A V_MUST_DEF kills any expression using a virtual operand. */
1773 if (NUM_V_MUST_DEFS (V_MUST_DEF_OPS (ann)) > 0)
1774 kill_virtual_exprs (tab, true);
1779 /* This function is the driver routine for replacement of temporary expressions
1780 in the SSA->normal phase, operating on MAP. If there are replaceable
1781 expressions, a table is returned which maps SSA versions to the
1782 expressions they should be replaced with. A NULL_TREE indicates no
1783 replacement should take place. If there are no replacements at all,
1784 NULL is returned by the function, otherwise an expression vector indexed
1785 by SSA_NAME version numbers. */
1787 static tree *
1788 find_replaceable_exprs (var_map map)
1790 basic_block bb;
1791 unsigned i;
1792 temp_expr_table_p table;
1793 tree *ret;
1795 table = new_temp_expr_table (map);
1796 FOR_EACH_BB (bb)
1798 bitmap_iterator bi;
1800 find_replaceable_in_bb (table, bb);
1801 EXECUTE_IF_SET_IN_BITMAP ((table->partition_in_use), 0, i, bi)
1803 kill_expr (table, i, false);
1807 ret = free_temp_expr_table (table);
1808 return ret;
1812 /* Dump TER expression table EXPR to file F. */
1814 static void
1815 dump_replaceable_exprs (FILE *f, tree *expr)
1817 tree stmt, var;
1818 int x;
1819 fprintf (f, "\nReplacing Expressions\n");
1820 for (x = 0; x < (int)num_ssa_names + 1; x++)
1821 if (expr[x])
1823 stmt = expr[x];
1824 var = DEF_OP (STMT_DEF_OPS (stmt), 0);
1825 print_generic_expr (f, var, TDF_SLIM);
1826 fprintf (f, " replace with --> ");
1827 print_generic_expr (f, TREE_OPERAND (stmt, 1), TDF_SLIM);
1828 fprintf (f, "\n");
1830 fprintf (f, "\n");
1834 /* This function will rewrite the current program using the variable mapping
1835 found in MAP. If the replacement vector VALUES is provided, any
1836 occurrences of partitions with non-null entries in the vector will be
1837 replaced with the expression in the vector instead of its mapped
1838 variable. */
1840 static void
1841 rewrite_trees (var_map map, tree *values)
1843 elim_graph g;
1844 basic_block bb;
1845 block_stmt_iterator si;
1846 edge e;
1847 tree phi;
1848 bool changed;
1850 #ifdef ENABLE_CHECKING
1851 /* Search for PHIs where the destination has no partition, but one
1852 or more arguments has a partition. This should not happen and can
1853 create incorrect code. */
1854 FOR_EACH_BB (bb)
1856 tree phi;
1858 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1860 tree T0 = var_to_partition_to_var (map, PHI_RESULT (phi));
1862 if (T0 == NULL_TREE)
1864 int i;
1866 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1868 tree arg = PHI_ARG_DEF (phi, i);
1870 if (TREE_CODE (arg) == SSA_NAME
1871 && var_to_partition (map, arg) != NO_PARTITION)
1873 fprintf (stderr, "Argument of PHI is in a partition :(");
1874 print_generic_expr (stderr, arg, TDF_SLIM);
1875 fprintf (stderr, "), but the result is not :");
1876 print_generic_stmt (stderr, phi, TDF_SLIM);
1877 internal_error ("SSA corruption");
1883 #endif
1885 /* Replace PHI nodes with any required copies. */
1886 g = new_elim_graph (map->num_partitions);
1887 g->map = map;
1888 FOR_EACH_BB (bb)
1890 for (si = bsi_start (bb); !bsi_end_p (si); )
1892 size_t num_uses, num_defs;
1893 use_optype uses;
1894 def_optype defs;
1895 tree stmt = bsi_stmt (si);
1896 use_operand_p use_p;
1897 def_operand_p def_p;
1898 int remove = 0, is_copy = 0;
1899 stmt_ann_t ann;
1900 ssa_op_iter iter;
1902 get_stmt_operands (stmt);
1903 ann = stmt_ann (stmt);
1904 changed = false;
1906 if (TREE_CODE (stmt) == MODIFY_EXPR
1907 && (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME))
1908 is_copy = 1;
1910 uses = USE_OPS (ann);
1911 num_uses = NUM_USES (uses);
1912 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1914 if (replace_use_variable (map, use_p, values))
1915 changed = true;
1918 defs = DEF_OPS (ann);
1919 num_defs = NUM_DEFS (defs);
1921 /* Mark this stmt for removal if it is the list of replaceable
1922 expressions. */
1923 if (values && num_defs == 1)
1925 tree def = DEF_OP (defs, 0);
1926 tree val;
1927 val = values[SSA_NAME_VERSION (def)];
1928 if (val)
1929 remove = 1;
1931 if (!remove)
1933 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_DEF)
1935 if (replace_def_variable (map, def_p, NULL))
1936 changed = true;
1938 /* If both SSA_NAMEs coalesce to the same variable,
1939 mark the now redundant copy for removal. */
1940 if (is_copy
1941 && num_uses == 1
1942 && (DEF_FROM_PTR (def_p) == USE_OP (uses, 0)))
1943 remove = 1;
1945 if (changed & !remove)
1946 modify_stmt (stmt);
1949 /* Remove any stmts marked for removal. */
1950 if (remove)
1951 bsi_remove (&si);
1952 else
1953 bsi_next (&si);
1956 phi = phi_nodes (bb);
1957 if (phi)
1959 edge_iterator ei;
1960 FOR_EACH_EDGE (e, ei, bb->preds)
1961 eliminate_phi (e, g);
1965 delete_elim_graph (g);
1969 /* These are the local work structures used to determine the best place to
1970 insert the copies that were placed on edges by the SSA->normal pass.. */
1971 static varray_type edge_leader = NULL;
1972 static varray_type GTY(()) stmt_list = NULL;
1973 static bitmap leader_has_match = NULL;
1974 static edge leader_match = NULL;
1977 /* Pass this function to make_forwarder_block so that all the edges with
1978 matching PENDING_STMT lists to 'curr_stmt_list' get redirected. */
1979 static bool
1980 same_stmt_list_p (edge e)
1982 return (e->aux == (PTR) leader_match) ? true : false;
1986 /* Return TRUE if S1 and S2 are equivalent copies. */
1987 static inline bool
1988 identical_copies_p (tree s1, tree s2)
1990 #ifdef ENABLE_CHECKING
1991 gcc_assert (TREE_CODE (s1) == MODIFY_EXPR);
1992 gcc_assert (TREE_CODE (s2) == MODIFY_EXPR);
1993 gcc_assert (DECL_P (TREE_OPERAND (s1, 0)));
1994 gcc_assert (DECL_P (TREE_OPERAND (s2, 0)));
1995 #endif
1997 if (TREE_OPERAND (s1, 0) != TREE_OPERAND (s2, 0))
1998 return false;
2000 s1 = TREE_OPERAND (s1, 1);
2001 s2 = TREE_OPERAND (s2, 1);
2003 if (s1 != s2)
2004 return false;
2006 return true;
2010 /* Compare the PENDING_STMT list for two edges, and return true if the lists
2011 contain the same sequence of copies. */
2013 static inline bool
2014 identical_stmt_lists_p (edge e1, edge e2)
2016 tree t1 = PENDING_STMT (e1);
2017 tree t2 = PENDING_STMT (e2);
2018 tree_stmt_iterator tsi1, tsi2;
2020 gcc_assert (TREE_CODE (t1) == STATEMENT_LIST);
2021 gcc_assert (TREE_CODE (t2) == STATEMENT_LIST);
2023 for (tsi1 = tsi_start (t1), tsi2 = tsi_start (t2);
2024 !tsi_end_p (tsi1) && !tsi_end_p (tsi2);
2025 tsi_next (&tsi1), tsi_next (&tsi2))
2027 if (!identical_copies_p (tsi_stmt (tsi1), tsi_stmt (tsi2)))
2028 break;
2031 if (!tsi_end_p (tsi1) || ! tsi_end_p (tsi2))
2032 return false;
2034 return true;
2038 /* Look at all the incoming edges to block BB, and decide where the best place
2039 to insert the stmts on each edge are, and perform those insertions. Output
2040 any debug information to DEBUG_FILE. Return true if anything other than a
2041 standard edge insertion is done. */
2043 static bool
2044 analyze_edges_for_bb (basic_block bb, FILE *debug_file)
2046 edge e;
2047 edge_iterator ei;
2048 int count;
2049 unsigned int x;
2050 bool have_opportunity;
2051 block_stmt_iterator bsi;
2052 tree stmt;
2053 edge single_edge = NULL;
2054 bool is_label;
2056 count = 0;
2058 /* Blocks which contain at least one abnormal edge cannot use
2059 make_forwarder_block. Look for these blocks, and commit any PENDING_STMTs
2060 found on edges in these block. */
2061 have_opportunity = true;
2062 FOR_EACH_EDGE (e, ei, bb->preds)
2063 if (e->flags & EDGE_ABNORMAL)
2065 have_opportunity = false;
2066 break;
2069 if (!have_opportunity)
2071 FOR_EACH_EDGE (e, ei, bb->preds)
2072 if (PENDING_STMT (e))
2073 bsi_commit_one_edge_insert (e, NULL);
2074 return false;
2076 /* Find out how many edges there are with interesting pending stmts on them.
2077 Commit the stmts on edges we are not interested in. */
2078 FOR_EACH_EDGE (e, ei, bb->preds)
2080 if (PENDING_STMT (e))
2082 gcc_assert (!(e->flags & EDGE_ABNORMAL));
2083 if (e->flags & EDGE_FALLTHRU)
2085 bsi = bsi_start (e->src);
2086 if (!bsi_end_p (bsi))
2088 stmt = bsi_stmt (bsi);
2089 bsi_next (&bsi);
2090 gcc_assert (stmt != NULL_TREE);
2091 is_label = (TREE_CODE (stmt) == LABEL_EXPR);
2092 /* Punt if it has non-label stmts, or isn't local. */
2093 if (!is_label || DECL_NONLOCAL (TREE_OPERAND (stmt, 0))
2094 || !bsi_end_p (bsi))
2096 bsi_commit_one_edge_insert (e, NULL);
2097 continue;
2101 single_edge = e;
2102 count++;
2106 /* If there aren't at least 2 edges, no sharing will happen. */
2107 if (count < 2)
2109 if (single_edge)
2110 bsi_commit_one_edge_insert (single_edge, NULL);
2111 return false;
2114 /* Ensure that we have empty worklists. */
2115 if (edge_leader == NULL)
2117 VARRAY_EDGE_INIT (edge_leader, 25, "edge_leader");
2118 VARRAY_TREE_INIT (stmt_list, 25, "stmt_list");
2119 leader_has_match = BITMAP_ALLOC (NULL);
2121 else
2123 #ifdef ENABLE_CHECKING
2124 gcc_assert (VARRAY_ACTIVE_SIZE (edge_leader) == 0);
2125 gcc_assert (VARRAY_ACTIVE_SIZE (stmt_list) == 0);
2126 gcc_assert (bitmap_empty_p (leader_has_match));
2127 #endif
2130 /* Find the "leader" block for each set of unique stmt lists. Preference is
2131 given to FALLTHRU blocks since they would need a GOTO to arrive at another
2132 block. The leader edge destination is the block which all the other edges
2133 with the same stmt list will be redirected to. */
2134 have_opportunity = false;
2135 FOR_EACH_EDGE (e, ei, bb->preds)
2137 if (PENDING_STMT (e))
2139 bool found = false;
2141 /* Look for the same stmt list in edge leaders list. */
2142 for (x = 0; x < VARRAY_ACTIVE_SIZE (edge_leader); x++)
2144 edge leader = VARRAY_EDGE (edge_leader, x);
2145 if (identical_stmt_lists_p (leader, e))
2147 /* Give this edge the same stmt list pointer. */
2148 PENDING_STMT (e) = NULL;
2149 e->aux = leader;
2150 bitmap_set_bit (leader_has_match, x);
2151 have_opportunity = found = true;
2152 break;
2156 /* If no similar stmt list, add this edge to the leader list. */
2157 if (!found)
2159 VARRAY_PUSH_EDGE (edge_leader, e);
2160 VARRAY_PUSH_TREE (stmt_list, PENDING_STMT (e));
2165 /* If there are no similar lists, just issue the stmts. */
2166 if (!have_opportunity)
2168 for (x = 0; x < VARRAY_ACTIVE_SIZE (edge_leader); x++)
2169 bsi_commit_one_edge_insert (VARRAY_EDGE (edge_leader, x), NULL);
2170 VARRAY_POP_ALL (edge_leader);
2171 VARRAY_POP_ALL (stmt_list);
2172 bitmap_clear (leader_has_match);
2173 return false;
2177 if (debug_file)
2178 fprintf (debug_file, "\nOpportunities in BB %d for stmt/block reduction:\n",
2179 bb->index);
2182 /* For each common list, create a forwarding block and issue the stmt's
2183 in that block. */
2184 for (x = 0 ; x < VARRAY_ACTIVE_SIZE (edge_leader); x++)
2185 if (bitmap_bit_p (leader_has_match, x))
2187 edge new_edge, leader_edge;
2188 block_stmt_iterator bsi;
2189 tree curr_stmt_list;
2191 leader_match = leader_edge = VARRAY_EDGE (edge_leader, x);
2193 /* The tree_* cfg manipulation routines use the PENDING_EDGE field
2194 for various PHI manipulations, so it gets cleared whhen calls are
2195 made to make_forwarder_block(). So make sure the edge is clear,
2196 and use the saved stmt list. */
2197 PENDING_STMT (leader_edge) = NULL;
2198 leader_edge->aux = leader_edge;
2199 curr_stmt_list = VARRAY_TREE (stmt_list, x);
2201 new_edge = make_forwarder_block (leader_edge->dest, same_stmt_list_p,
2202 NULL);
2203 bb = new_edge->dest;
2204 if (debug_file)
2206 fprintf (debug_file, "Splitting BB %d for Common stmt list. ",
2207 leader_edge->dest->index);
2208 fprintf (debug_file, "Original block is now BB%d.\n", bb->index);
2209 print_generic_stmt (debug_file, curr_stmt_list, TDF_VOPS);
2212 FOR_EACH_EDGE (e, ei, new_edge->src->preds)
2214 e->aux = NULL;
2215 if (debug_file)
2216 fprintf (debug_file, " Edge (%d->%d) lands here.\n",
2217 e->src->index, e->dest->index);
2220 bsi = bsi_last (leader_edge->dest);
2221 bsi_insert_after (&bsi, curr_stmt_list, BSI_NEW_STMT);
2223 leader_match = NULL;
2224 /* We should never get a new block now. */
2226 else
2228 e = VARRAY_EDGE (edge_leader, x);
2229 PENDING_STMT (e) = VARRAY_TREE (stmt_list, x);
2230 bsi_commit_one_edge_insert (e, NULL);
2234 /* Clear the working data structures. */
2235 VARRAY_POP_ALL (edge_leader);
2236 VARRAY_POP_ALL (stmt_list);
2237 bitmap_clear (leader_has_match);
2239 return true;
2243 /* This function will analyze the insertions which were performed on edges,
2244 and decide whether they should be left on that edge, or whether it is more
2245 efficient to emit some subset of them in a single block. All stmts are
2246 inserted somewhere, and if non-NULL, debug information is printed via
2247 DUMP_FILE. */
2249 static void
2250 perform_edge_inserts (FILE *dump_file)
2252 basic_block bb;
2253 bool changed = false;
2255 if (dump_file)
2256 fprintf(dump_file, "Analyzing Edge Insertions.\n");
2258 FOR_EACH_BB (bb)
2259 changed |= analyze_edges_for_bb (bb, dump_file);
2261 changed |= analyze_edges_for_bb (EXIT_BLOCK_PTR, dump_file);
2263 /* Clear out any tables which were created. */
2264 edge_leader = NULL;
2265 BITMAP_FREE (leader_has_match);
2267 if (changed)
2269 free_dominance_info (CDI_DOMINATORS);
2270 free_dominance_info (CDI_POST_DOMINATORS);
2273 #ifdef ENABLE_CHECKING
2275 edge_iterator ei;
2276 edge e;
2277 FOR_EACH_BB (bb)
2279 FOR_EACH_EDGE (e, ei, bb->preds)
2281 if (PENDING_STMT (e))
2282 error (" Pending stmts not issued on PRED edge (%d, %d)\n",
2283 e->src->index, e->dest->index);
2285 FOR_EACH_EDGE (e, ei, bb->succs)
2287 if (PENDING_STMT (e))
2288 error (" Pending stmts not issued on SUCC edge (%d, %d)\n",
2289 e->src->index, e->dest->index);
2292 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2294 if (PENDING_STMT (e))
2295 error (" Pending stmts not issued on ENTRY edge (%d, %d)\n",
2296 e->src->index, e->dest->index);
2298 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
2300 if (PENDING_STMT (e))
2301 error (" Pending stmts not issued on EXIT edge (%d, %d)\n",
2302 e->src->index, e->dest->index);
2305 #endif
2309 /* Remove the variables specified in MAP from SSA form. Any debug information
2310 is sent to DUMP. FLAGS indicate what options should be used. */
2312 static void
2313 remove_ssa_form (FILE *dump, var_map map, int flags)
2315 tree_live_info_p liveinfo;
2316 basic_block bb;
2317 tree phi, next;
2318 FILE *save;
2319 tree *values = NULL;
2321 save = dump_file;
2322 dump_file = dump;
2324 /* If we are not combining temps, don't calculate live ranges for variables
2325 with only one SSA version. */
2326 if ((flags & SSANORM_COMBINE_TEMPS) == 0)
2327 compact_var_map (map, VARMAP_NO_SINGLE_DEFS);
2328 else
2329 compact_var_map (map, VARMAP_NORMAL);
2331 if (dump_file && (dump_flags & TDF_DETAILS))
2332 dump_var_map (dump_file, map);
2334 liveinfo = coalesce_ssa_name (map, flags);
2336 /* Make sure even single occurrence variables are in the list now. */
2337 if ((flags & SSANORM_COMBINE_TEMPS) == 0)
2338 compact_var_map (map, VARMAP_NORMAL);
2340 if (dump_file && (dump_flags & TDF_DETAILS))
2342 fprintf (dump_file, "After Coalescing:\n");
2343 dump_var_map (dump_file, map);
2346 if (flags & SSANORM_PERFORM_TER)
2348 values = find_replaceable_exprs (map);
2349 if (values && dump_file && (dump_flags & TDF_DETAILS))
2350 dump_replaceable_exprs (dump_file, values);
2353 /* Assign real variables to the partitions now. */
2354 assign_vars (map);
2356 if (dump_file && (dump_flags & TDF_DETAILS))
2358 fprintf (dump_file, "After Root variable replacement:\n");
2359 dump_var_map (dump_file, map);
2362 if ((flags & SSANORM_COMBINE_TEMPS) && liveinfo)
2364 coalesce_vars (map, liveinfo);
2365 if (dump_file && (dump_flags & TDF_DETAILS))
2367 fprintf (dump_file, "After variable memory coalescing:\n");
2368 dump_var_map (dump_file, map);
2372 if (liveinfo)
2373 delete_tree_live_info (liveinfo);
2375 rewrite_trees (map, values);
2377 if (values)
2378 free (values);
2380 /* Remove phi nodes which have been translated back to real variables. */
2381 FOR_EACH_BB (bb)
2383 for (phi = phi_nodes (bb); phi; phi = next)
2385 next = PHI_CHAIN (phi);
2386 remove_phi_node (phi, NULL_TREE, bb);
2390 /* If any copies were inserted on edges, analyze and insert them now. */
2391 perform_edge_inserts (dump_file);
2393 dump_file = save;
2396 /* Search every PHI node for arguments associated with backedges which
2397 we can trivially determine will need a copy (the argument is either
2398 not an SSA_NAME or the argument has a different underlying variable
2399 than the PHI result).
2401 Insert a copy from the PHI argument to a new destination at the
2402 end of the block with the backedge to the top of the loop. Update
2403 the PHI argument to reference this new destination. */
2405 static void
2406 insert_backedge_copies (void)
2408 basic_block bb;
2410 FOR_EACH_BB (bb)
2412 tree phi;
2414 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2416 tree result = PHI_RESULT (phi);
2417 tree result_var;
2418 int i;
2420 if (!is_gimple_reg (result))
2421 continue;
2423 result_var = SSA_NAME_VAR (result);
2424 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
2426 tree arg = PHI_ARG_DEF (phi, i);
2427 edge e = PHI_ARG_EDGE (phi, i);
2429 /* If the argument is not an SSA_NAME, then we will
2430 need a constant initialization. If the argument is
2431 an SSA_NAME with a different underlying variable and
2432 we are not combining temporaries, then we will
2433 need a copy statement. */
2434 if ((e->flags & EDGE_DFS_BACK)
2435 && (TREE_CODE (arg) != SSA_NAME
2436 || (!flag_tree_combine_temps
2437 && SSA_NAME_VAR (arg) != result_var)))
2439 tree stmt, name, last = NULL;
2440 block_stmt_iterator bsi;
2442 bsi = bsi_last (PHI_ARG_EDGE (phi, i)->src);
2443 if (!bsi_end_p (bsi))
2444 last = bsi_stmt (bsi);
2446 /* In theory the only way we ought to get back to the
2447 start of a loop should be with a COND_EXPR or GOTO_EXPR.
2448 However, better safe than sorry.
2450 If the block ends with a control statement or
2451 something that might throw, then we have to
2452 insert this assignment before the last
2453 statement. Else insert it after the last statement. */
2454 if (last && stmt_ends_bb_p (last))
2456 /* If the last statement in the block is the definition
2457 site of the PHI argument, then we can't insert
2458 anything after it. */
2459 if (TREE_CODE (arg) == SSA_NAME
2460 && SSA_NAME_DEF_STMT (arg) == last)
2461 continue;
2464 /* Create a new instance of the underlying
2465 variable of the PHI result. */
2466 stmt = build (MODIFY_EXPR, TREE_TYPE (result_var),
2467 NULL, PHI_ARG_DEF (phi, i));
2468 name = make_ssa_name (result_var, stmt);
2469 TREE_OPERAND (stmt, 0) = name;
2471 /* Insert the new statement into the block and update
2472 the PHI node. */
2473 if (last && stmt_ends_bb_p (last))
2474 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
2475 else
2476 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
2477 modify_stmt (stmt);
2478 SET_PHI_ARG_DEF (phi, i, name);
2485 /* Take the current function out of SSA form, as described in
2486 R. Morgan, ``Building an Optimizing Compiler'',
2487 Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
2489 static void
2490 rewrite_out_of_ssa (void)
2492 var_map map;
2493 int var_flags = 0;
2494 int ssa_flags = 0;
2496 /* If elimination of a PHI requires inserting a copy on a backedge,
2497 then we will have to split the backedge which has numerous
2498 undesirable performance effects.
2500 A significant number of such cases can be handled here by inserting
2501 copies into the loop itself. */
2502 insert_backedge_copies ();
2504 if (!flag_tree_live_range_split)
2505 ssa_flags |= SSANORM_COALESCE_PARTITIONS;
2507 eliminate_virtual_phis ();
2509 if (dump_file && (dump_flags & TDF_DETAILS))
2510 dump_tree_cfg (dump_file, dump_flags & ~TDF_DETAILS);
2512 /* We cannot allow unssa to un-gimplify trees before we instrument them. */
2513 if (flag_tree_ter && !flag_mudflap)
2514 var_flags = SSA_VAR_MAP_REF_COUNT;
2516 map = create_ssa_var_map (var_flags);
2518 if (flag_tree_combine_temps)
2519 ssa_flags |= SSANORM_COMBINE_TEMPS;
2520 if (flag_tree_ter && !flag_mudflap)
2521 ssa_flags |= SSANORM_PERFORM_TER;
2523 remove_ssa_form (dump_file, map, ssa_flags);
2525 if (dump_file && (dump_flags & TDF_DETAILS))
2526 dump_tree_cfg (dump_file, dump_flags & ~TDF_DETAILS);
2528 /* Do some cleanups which reduce the amount of data the
2529 tree->rtl expanders deal with. */
2530 cfg_remove_useless_stmts ();
2532 /* Flush out flow graph and SSA data. */
2533 delete_var_map (map);
2537 /* Define the parameters of the out of SSA pass. */
2539 struct tree_opt_pass pass_del_ssa =
2541 "optimized", /* name */
2542 NULL, /* gate */
2543 rewrite_out_of_ssa, /* execute */
2544 NULL, /* sub */
2545 NULL, /* next */
2546 0, /* static_pass_number */
2547 TV_TREE_SSA_TO_NORMAL, /* tv_id */
2548 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
2549 0, /* properties_provided */
2550 /* ??? If TER is enabled, we also kill gimple. */
2551 PROP_ssa, /* properties_destroyed */
2552 TODO_verify_ssa | TODO_verify_flow
2553 | TODO_verify_stmts, /* todo_flags_start */
2554 TODO_dump_func | TODO_ggc_collect, /* todo_flags_finish */
2555 0 /* letter */