* basic-block.h: Include "errors.h".
[official-gcc.git] / gcc / tree-outof-ssa.c
blob5dddc6de19acc176615b6a760ec1b8e4c9f412d8
1 /* Convert a program in SSA form into Normal form.
2 Copyright (C) 2004 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 "tree-alias-common.h"
46 #include "hashtab.h"
47 #include "tree-dump.h"
48 #include "tree-ssa-live.h"
49 #include "tree-pass.h"
51 /* Used to hold all the components required to do SSA PHI elimination.
52 The node and pred/succ list is a simple linear list of nodes and
53 edges represented as pairs of nodes.
55 The predecessor and successor list: Nodes are entered in pairs, where
56 [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
57 predecessors, all the odd elements are successors.
59 Rationale:
60 When implemented as bitmaps, very large programs SSA->Normal times were
61 being dominated by clearing the interference graph.
63 Typically this list of edges is extremely small since it only includes
64 PHI results and uses from a single edge which have not coalesced with
65 each other. This means that no virtual PHI nodes are included, and
66 empirical evidence suggests that the number of edges rarely exceed
67 3, and in a bootstrap of GCC, the maximum size encountered was 7.
68 This also limits the number of possible nodes that are involved to
69 rarely more than 6, and in the bootstrap of gcc, the maximum number
70 of nodes encountered was 12. */
72 typedef struct _elim_graph {
73 /* Size of the elimination vectors. */
74 int size;
76 /* List of nodes in the elimination graph. */
77 varray_type nodes;
79 /* The predecessor and successor edge list. */
80 varray_type edge_list;
82 /* Visited vector. */
83 sbitmap visited;
85 /* Stack for visited nodes. */
86 varray_type stack;
88 /* The variable partition map. */
89 var_map map;
91 /* Edge being eliminated by this graph. */
92 edge e;
94 /* List of constant copies to emit. These are pushed on in pairs. */
95 varray_type const_copies;
96 } *elim_graph;
99 /* Local functions. */
100 static tree create_temp (tree);
101 static void insert_copy_on_edge (edge, tree, tree);
102 static elim_graph new_elim_graph (int);
103 static inline void delete_elim_graph (elim_graph);
104 static inline void clear_elim_graph (elim_graph);
105 static inline int elim_graph_size (elim_graph);
106 static inline void elim_graph_add_node (elim_graph, tree);
107 static inline void elim_graph_add_edge (elim_graph, int, int);
108 static inline int elim_graph_remove_succ_edge (elim_graph, int);
110 static inline void eliminate_name (elim_graph, tree);
111 static void eliminate_build (elim_graph, basic_block, int);
112 static void elim_forward (elim_graph, int);
113 static int elim_unvisited_predecessor (elim_graph, int);
114 static void elim_backward (elim_graph, int);
115 static void elim_create (elim_graph, int);
116 static void eliminate_phi (edge, int, elim_graph);
117 static tree_live_info_p coalesce_ssa_name (var_map, int);
118 static void assign_vars (var_map);
119 static bool replace_use_variable (var_map, use_operand_p, tree *);
120 static bool replace_def_variable (var_map, def_operand_p, tree *);
121 static void eliminate_virtual_phis (void);
122 static void coalesce_abnormal_edges (var_map, conflict_graph, root_var_p);
123 static void print_exprs (FILE *, const char *, tree, const char *, tree,
124 const char *);
125 static void print_exprs_edge (FILE *, edge, const char *, tree, const char *,
126 tree);
129 /* Create a temporary variable based on the type of variable T. Use T's name
130 as the prefix. */
132 static tree
133 create_temp (tree t)
135 tree tmp;
136 const char *name = NULL;
137 tree type;
139 if (TREE_CODE (t) == SSA_NAME)
140 t = SSA_NAME_VAR (t);
142 if (TREE_CODE (t) != VAR_DECL
143 && TREE_CODE (t) != PARM_DECL)
144 abort ();
146 type = TREE_TYPE (t);
147 tmp = DECL_NAME (t);
148 if (tmp)
149 name = IDENTIFIER_POINTER (tmp);
151 if (name == NULL)
152 name = "temp";
153 tmp = create_tmp_var (type, name);
154 DECL_ARTIFICIAL (tmp) = DECL_ARTIFICIAL (t);
155 add_referenced_tmp_var (tmp);
157 /* add_referenced_tmp_var will create the annotation and set up some
158 of the flags in the annotation. However, some flags we need to
159 inherit from our original variable. */
160 var_ann (tmp)->type_mem_tag = var_ann (t)->type_mem_tag;
161 if (is_call_clobbered (t))
162 mark_call_clobbered (tmp);
164 return tmp;
168 /* This helper function fill insert a copy from a constant or variable SRC to
169 variable DEST on edge E. */
171 static void
172 insert_copy_on_edge (edge e, tree dest, tree src)
174 tree copy;
176 copy = build (MODIFY_EXPR, TREE_TYPE (dest), dest, src);
177 set_is_used (dest);
179 if (TREE_CODE (src) == ADDR_EXPR)
180 src = TREE_OPERAND (src, 0);
181 if (TREE_CODE (src) == VAR_DECL || TREE_CODE (src) == PARM_DECL)
182 set_is_used (src);
184 if (dump_file && (dump_flags & TDF_DETAILS))
186 fprintf (dump_file,
187 "Inserting a copy on edge BB%d->BB%d :",
188 e->src->index,
189 e->dest->index);
190 print_generic_expr (dump_file, copy, dump_flags);
191 fprintf (dump_file, "\n");
194 bsi_insert_on_edge (e, copy);
198 /* Create an elimination graph with SIZE nodes and associated data
199 structures. */
201 static elim_graph
202 new_elim_graph (int size)
204 elim_graph g = (elim_graph) xmalloc (sizeof (struct _elim_graph));
206 VARRAY_TREE_INIT (g->nodes, 30, "Elimination Node List");
207 VARRAY_TREE_INIT (g->const_copies, 20, "Elimination Constant Copies");
208 VARRAY_INT_INIT (g->edge_list, 20, "Elimination Edge List");
209 VARRAY_INT_INIT (g->stack, 30, " Elimination Stack");
211 g->visited = sbitmap_alloc (size);
213 return g;
217 /* Empty elimination graph G. */
219 static inline void
220 clear_elim_graph (elim_graph g)
222 VARRAY_POP_ALL (g->nodes);
223 VARRAY_POP_ALL (g->edge_list);
227 /* Delete elimination graph G. */
229 static inline void
230 delete_elim_graph (elim_graph g)
232 sbitmap_free (g->visited);
233 free (g);
237 /* Return the number of nodes in graph G. */
239 static inline int
240 elim_graph_size (elim_graph g)
242 return VARRAY_ACTIVE_SIZE (g->nodes);
246 /* Add NODE to graph G, if it doesn't exist already. */
248 static inline void
249 elim_graph_add_node (elim_graph g, tree node)
251 int x;
252 for (x = 0; x < elim_graph_size (g); x++)
253 if (VARRAY_TREE (g->nodes, x) == node)
254 return;
255 VARRAY_PUSH_TREE (g->nodes, node);
259 /* Add the edge PRED->SUCC to graph G. */
261 static inline void
262 elim_graph_add_edge (elim_graph g, int pred, int succ)
264 VARRAY_PUSH_INT (g->edge_list, pred);
265 VARRAY_PUSH_INT (g->edge_list, succ);
269 /* Remove an edge from graph G for which NODE is the predecessor, and
270 return the successor node. -1 is returned if there is no such edge. */
272 static inline int
273 elim_graph_remove_succ_edge (elim_graph g, int node)
275 int y;
276 unsigned x;
277 for (x = 0; x < VARRAY_ACTIVE_SIZE (g->edge_list); x += 2)
278 if (VARRAY_INT (g->edge_list, x) == node)
280 VARRAY_INT (g->edge_list, x) = -1;
281 y = VARRAY_INT (g->edge_list, x + 1);
282 VARRAY_INT (g->edge_list, x + 1) = -1;
283 return y;
285 return -1;
289 /* Find all the nodes in GRAPH which are successors to NODE in the
290 edge list. VAR will hold the partition number found. CODE is the
291 code fragment executed for every node found. */
293 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, CODE) \
294 do { \
295 unsigned x_; \
296 int y_; \
297 for (x_ = 0; x_ < VARRAY_ACTIVE_SIZE ((GRAPH)->edge_list); x_ += 2) \
299 y_ = VARRAY_INT ((GRAPH)->edge_list, x_); \
300 if (y_ != (NODE)) \
301 continue; \
302 (VAR) = VARRAY_INT ((GRAPH)->edge_list, x_ + 1); \
303 CODE; \
305 } while (0)
308 /* Find all the nodes which are predecessors of NODE in the edge list for
309 GRAPH. VAR will hold the partition number found. CODE is the
310 code fragment executed for every node found. */
312 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, CODE) \
313 do { \
314 unsigned x_; \
315 int y_; \
316 for (x_ = 0; x_ < VARRAY_ACTIVE_SIZE ((GRAPH)->edge_list); x_ += 2) \
318 y_ = VARRAY_INT ((GRAPH)->edge_list, x_ + 1); \
319 if (y_ != (NODE)) \
320 continue; \
321 (VAR) = VARRAY_INT ((GRAPH)->edge_list, x_); \
322 CODE; \
324 } while (0)
327 /* Add T to elimination graph G. */
329 static inline void
330 eliminate_name (elim_graph g, tree T)
332 elim_graph_add_node (g, T);
336 /* Build elimination graph G for basic block BB on incoming PHI edge I. */
338 static void
339 eliminate_build (elim_graph g, basic_block B, int i)
341 tree phi;
342 tree T0, Ti;
343 int p0, pi;
345 clear_elim_graph (g);
347 for (phi = phi_nodes (B); phi; phi = PHI_CHAIN (phi))
349 T0 = var_to_partition_to_var (g->map, PHI_RESULT (phi));
351 /* Ignore results which are not in partitions. */
352 if (T0 == NULL_TREE)
353 continue;
355 if (PHI_ARG_EDGE (phi, i) == g->e)
356 Ti = PHI_ARG_DEF (phi, i);
357 else
359 /* On rare occasions, a PHI node may not have the arguments
360 in the same order as all of the other PHI nodes. If they don't
361 match, find the appropriate index here. */
362 pi = phi_arg_from_edge (phi, g->e);
363 if (pi == -1)
364 abort();
365 Ti = PHI_ARG_DEF (phi, pi);
368 /* If this argument is a constant, or a SSA_NAME which is being
369 left in SSA form, just queue a copy to be emitted on this
370 edge. */
371 if (!phi_ssa_name_p (Ti)
372 || (TREE_CODE (Ti) == SSA_NAME
373 && var_to_partition (g->map, Ti) == NO_PARTITION))
375 /* Save constant copies until all other copies have been emitted
376 on this edge. */
377 VARRAY_PUSH_TREE (g->const_copies, T0);
378 VARRAY_PUSH_TREE (g->const_copies, Ti);
380 else
382 Ti = var_to_partition_to_var (g->map, Ti);
383 if (T0 != Ti)
385 eliminate_name (g, T0);
386 eliminate_name (g, Ti);
387 p0 = var_to_partition (g->map, T0);
388 pi = var_to_partition (g->map, Ti);
389 elim_graph_add_edge (g, p0, pi);
396 /* Push successors of T onto the elimination stack for G. */
398 static void
399 elim_forward (elim_graph g, int T)
401 int S;
402 SET_BIT (g->visited, T);
403 FOR_EACH_ELIM_GRAPH_SUCC (g, T, S,
405 if (!TEST_BIT (g->visited, S))
406 elim_forward (g, S);
408 VARRAY_PUSH_INT (g->stack, T);
412 /* Return 1 if there unvisited predecessors of T in graph G. */
414 static int
415 elim_unvisited_predecessor (elim_graph g, int T)
417 int P;
418 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
420 if (!TEST_BIT (g->visited, P))
421 return 1;
423 return 0;
426 /* Process predecessors first, and insert a copy. */
428 static void
429 elim_backward (elim_graph g, int T)
431 int P;
432 SET_BIT (g->visited, T);
433 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
435 if (!TEST_BIT (g->visited, P))
437 elim_backward (g, P);
438 insert_copy_on_edge (g->e,
439 partition_to_var (g->map, P),
440 partition_to_var (g->map, T));
445 /* Insert required copies for T in graph G. Check for a strongly connected
446 region, and create a temporary to break the cycle if one is found. */
448 static void
449 elim_create (elim_graph g, int T)
451 tree U;
452 int P, S;
454 if (elim_unvisited_predecessor (g, T))
456 U = create_temp (partition_to_var (g->map, T));
457 insert_copy_on_edge (g->e, U, partition_to_var (g->map, T));
458 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
460 if (!TEST_BIT (g->visited, P))
462 elim_backward (g, P);
463 insert_copy_on_edge (g->e, partition_to_var (g->map, P), U);
467 else
469 S = elim_graph_remove_succ_edge (g, T);
470 if (S != -1)
472 SET_BIT (g->visited, T);
473 insert_copy_on_edge (g->e,
474 partition_to_var (g->map, T),
475 partition_to_var (g->map, S));
481 /* Eliminate all the phi nodes on edge E in graph G. I is the usual PHI
482 index that edge E's values are found on. */
484 static void
485 eliminate_phi (edge e, int i, elim_graph g)
487 int num_nodes = 0;
488 int x;
489 basic_block B = e->dest;
491 #if defined ENABLE_CHECKING
492 if (i == -1)
493 abort ();
494 if (VARRAY_ACTIVE_SIZE (g->const_copies) != 0)
495 abort ();
496 #endif
498 /* Abnormal edges already have everything coalesced, or the coalescer
499 would have aborted. */
500 if (e->flags & EDGE_ABNORMAL)
501 return;
503 num_nodes = num_var_partitions (g->map);
504 g->e = e;
506 eliminate_build (g, B, i);
508 if (elim_graph_size (g) != 0)
510 sbitmap_zero (g->visited);
511 VARRAY_POP_ALL (g->stack);
513 for (x = 0; x < elim_graph_size (g); x++)
515 tree var = VARRAY_TREE (g->nodes, x);
516 int p = var_to_partition (g->map, var);
517 if (!TEST_BIT (g->visited, p))
518 elim_forward (g, p);
521 sbitmap_zero (g->visited);
522 while (VARRAY_ACTIVE_SIZE (g->stack) > 0)
524 x = VARRAY_TOP_INT (g->stack);
525 VARRAY_POP (g->stack);
526 if (!TEST_BIT (g->visited, x))
527 elim_create (g, x);
531 /* If there are any pending constant copies, issue them now. */
532 while (VARRAY_ACTIVE_SIZE (g->const_copies) > 0)
534 tree src, dest;
535 src = VARRAY_TOP_TREE (g->const_copies);
536 VARRAY_POP (g->const_copies);
537 dest = VARRAY_TOP_TREE (g->const_copies);
538 VARRAY_POP (g->const_copies);
539 insert_copy_on_edge (e, dest, src);
544 /* Shortcut routine to print messages to file F of the form:
545 "STR1 EXPR1 STR2 EXPR2 STR3." */
547 static void
548 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
549 tree expr2, const char *str3)
551 fprintf (f, "%s", str1);
552 print_generic_expr (f, expr1, TDF_SLIM);
553 fprintf (f, "%s", str2);
554 print_generic_expr (f, expr2, TDF_SLIM);
555 fprintf (f, "%s", str3);
559 /* Shortcut routine to print abnormal edge messages to file F of the form:
560 "STR1 EXPR1 STR2 EXPR2 across edge E. */
562 static void
563 print_exprs_edge (FILE *f, edge e, const char *str1, tree expr1,
564 const char *str2, tree expr2)
566 print_exprs (f, str1, expr1, str2, expr2, " across an abnormal edge");
567 fprintf (f, " from BB%d->BB%d\n", e->src->index,
568 e->dest->index);
572 /* Coalesce partitions in MAP which are live across abnormal edges in GRAPH.
573 RV is the root variable groupings of the partitions in MAP. Since code
574 cannot be inserted on these edges, failure to coalesce something across
575 an abnormal edge is an error. */
577 static void
578 coalesce_abnormal_edges (var_map map, conflict_graph graph, root_var_p rv)
580 basic_block bb;
581 edge e;
582 tree phi, var, tmp;
583 int x, y;
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, bb->succs)
592 if (e->dest != EXIT_BLOCK_PTR && e->flags & EDGE_ABNORMAL)
593 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
595 /* Visit each PHI on the destination side of this abnormal
596 edge, and attempt to coalesce the argument with the result. */
597 var = PHI_RESULT (phi);
598 x = var_to_partition (map, var);
600 /* Ignore results which are not relevant. */
601 if (x == NO_PARTITION)
602 continue;
604 y = phi_arg_from_edge (phi, e);
605 if (y == -1)
606 abort ();
608 tmp = PHI_ARG_DEF (phi, y);
609 if (!phi_ssa_name_p (tmp))
611 print_exprs_edge (stderr, e,
612 "\nConstant argument in PHI. Can't insert :",
613 var, " = ", tmp);
614 abort ();
616 y = var_to_partition (map, tmp);
617 if (x == NO_PARTITION || y == NO_PARTITION)
618 abort ();
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 abort ();
628 if (x != y)
630 if (!conflict_graph_conflict_p (graph, x, y))
632 /* Now map the partitions back to their real variables. */
633 var = partition_to_var (map, x);
634 tmp = partition_to_var (map, y);
635 if (dump_file
636 && (dump_flags & TDF_DETAILS))
638 print_exprs_edge (dump_file, e,
639 "ABNORMAL: Coalescing ",
640 var, " and ", tmp);
642 if (var_union (map, var, tmp) == NO_PARTITION)
644 print_exprs_edge (stderr, e, "\nUnable to coalesce",
645 partition_to_var (map, x), " and ",
646 partition_to_var (map, y));
647 abort ();
649 conflict_graph_merge_regs (graph, x, y);
651 else
653 print_exprs_edge (stderr, e, "\n Conflict ",
654 partition_to_var (map, x),
655 " and ", partition_to_var (map, y));
656 abort ();
661 END_FOR_EACH_EDGE;
665 /* Reduce the number of live ranges in MAP. Live range information is
666 returned if FLAGS indicates that we are combining temporaries, otherwise
667 NULL is returned. The only partitions which are associated with actual
668 variables at this point are those which are forced to be coalesced for
669 various reason. (live on entry, live across abnormal edges, etc.). */
671 static tree_live_info_p
672 coalesce_ssa_name (var_map map, int flags)
674 int num, x, i;
675 sbitmap live;
676 tree var, phi;
677 root_var_p rv;
678 tree_live_info_p liveinfo;
679 var_ann_t ann;
680 conflict_graph graph;
681 basic_block bb;
682 coalesce_list_p cl = NULL;
684 if (num_var_partitions (map) <= 1)
685 return NULL;
687 /* If no preference given, use cheap coalescing of all partitions. */
688 if ((flags & (SSANORM_COALESCE_PARTITIONS | SSANORM_USE_COALESCE_LIST)) == 0)
689 flags |= SSANORM_COALESCE_PARTITIONS;
691 liveinfo = calculate_live_on_entry (map);
692 calculate_live_on_exit (liveinfo);
693 rv = root_var_init (map);
695 /* Remove single element variable from the list. */
696 root_var_compact (rv);
698 if (flags & SSANORM_USE_COALESCE_LIST)
700 cl = create_coalesce_list (map);
702 /* Add all potential copies via PHI arguments to the list. */
703 FOR_EACH_BB (bb)
705 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
707 tree res = PHI_RESULT (phi);
708 int p = var_to_partition (map, res);
709 if (p == NO_PARTITION)
710 continue;
711 for (x = 0; x < PHI_NUM_ARGS (phi); x++)
713 tree arg = PHI_ARG_DEF (phi, x);
714 int p2;
716 if (TREE_CODE (arg) != SSA_NAME)
717 continue;
718 if (SSA_NAME_VAR (res) != SSA_NAME_VAR (arg))
719 continue;
720 p2 = var_to_partition (map, PHI_ARG_DEF (phi, x));
721 if (p2 != NO_PARTITION)
722 add_coalesce (cl, p, p2, 1);
727 /* Coalesce all the result decls together. */
728 var = NULL_TREE;
729 i = 0;
730 for (x = 0; x < num_var_partitions (map); x++)
732 tree p = partition_to_var (map, x);
733 if (TREE_CODE (SSA_NAME_VAR(p)) == RESULT_DECL)
735 if (var == NULL_TREE)
737 var = p;
738 i = x;
740 else
741 add_coalesce (cl, i, x, 1);
746 /* Build a conflict graph. */
747 graph = build_tree_conflict_graph (liveinfo, rv, cl);
749 if (cl)
751 if (dump_file && (dump_flags & TDF_DETAILS))
753 fprintf (dump_file, "Before sorting:\n");
754 dump_coalesce_list (dump_file, cl);
757 sort_coalesce_list (cl);
759 if (dump_file && (dump_flags & TDF_DETAILS))
761 fprintf (dump_file, "\nAfter sorting:\n");
762 dump_coalesce_list (dump_file, cl);
766 /* Put the single element variables back in. */
767 root_var_decompact (rv);
769 /* First, coalesce all live on entry variables to their root variable.
770 This will ensure the first use is coming from the correct location. */
772 live = sbitmap_alloc (num_var_partitions (map));
773 sbitmap_zero (live);
775 /* Set 'live' vector to indicate live on entry partitions. */
776 num = num_var_partitions (map);
777 for (x = 0 ; x < num; x++)
779 var = partition_to_var (map, x);
780 if (default_def (SSA_NAME_VAR (var)) == var)
781 SET_BIT (live, x);
784 if ((flags & SSANORM_COMBINE_TEMPS) == 0)
786 delete_tree_live_info (liveinfo);
787 liveinfo = NULL;
790 /* Assign root variable as partition representative for each live on entry
791 partition. */
792 EXECUTE_IF_SET_IN_SBITMAP (live, 0, x,
794 var = root_var (rv, root_var_find (rv, x));
795 ann = var_ann (var);
796 /* If these aren't already coalesced... */
797 if (partition_to_var (map, x) != var)
799 if (ann->out_of_ssa_tag)
801 /* This root variable has already been assigned to another
802 partition which is not coalesced with this one. */
803 abort ();
806 if (dump_file && (dump_flags & TDF_DETAILS))
808 print_exprs (dump_file, "Must coalesce ",
809 partition_to_var (map, x),
810 " with the root variable ", var, ".\n");
813 change_partition_var (map, var, x);
817 sbitmap_free (live);
819 /* Coalesce partitions live across abnormal edges. */
820 coalesce_abnormal_edges (map, graph, rv);
822 if (dump_file && (dump_flags & TDF_DETAILS))
823 dump_var_map (dump_file, map);
825 /* Coalesce partitions. */
826 if (flags & SSANORM_USE_COALESCE_LIST)
827 coalesce_tpa_members (rv, graph, map, cl,
828 ((dump_flags & TDF_DETAILS) ? dump_file
829 : NULL));
832 if (flags & SSANORM_COALESCE_PARTITIONS)
833 coalesce_tpa_members (rv, graph, map, NULL,
834 ((dump_flags & TDF_DETAILS) ? dump_file
835 : NULL));
836 if (cl)
837 delete_coalesce_list (cl);
838 root_var_delete (rv);
839 conflict_graph_delete (graph);
841 return liveinfo;
845 /* Take the ssa-name var_map MAP, and assign real variables to each
846 partition. */
848 static void
849 assign_vars (var_map map)
851 int x, i, num, rep;
852 tree t, var;
853 var_ann_t ann;
854 root_var_p rv;
856 rv = root_var_init (map);
857 if (!rv)
858 return;
860 /* Coalescing may already have forced some partitions to their root
861 variable. Find these and tag them. */
863 num = num_var_partitions (map);
864 for (x = 0; x < num; x++)
866 var = partition_to_var (map, x);
867 if (TREE_CODE (var) != SSA_NAME)
869 /* Coalescing will already have verified that more than one
870 partition doesn't have the same root variable. Simply marked
871 the variable as assigned. */
872 ann = var_ann (var);
873 ann->out_of_ssa_tag = 1;
874 if (dump_file && (dump_flags & TDF_DETAILS))
876 fprintf (dump_file, "partition %d has variable ", x);
877 print_generic_expr (dump_file, var, TDF_SLIM);
878 fprintf (dump_file, " assigned to it.\n");
884 num = root_var_num (rv);
885 for (x = 0; x < num; x++)
887 var = root_var (rv, x);
888 ann = var_ann (var);
889 for (i = root_var_first_partition (rv, x);
890 i != ROOT_VAR_NONE;
891 i = root_var_next_partition (rv, i))
893 t = partition_to_var (map, i);
895 if (t == var || TREE_CODE (t) != SSA_NAME)
896 continue;
898 rep = var_to_partition (map, t);
900 if (!ann->out_of_ssa_tag)
902 if (dump_file && (dump_flags & TDF_DETAILS))
903 print_exprs (dump_file, "", t, " --> ", var, "\n");
904 change_partition_var (map, var, rep);
905 continue;
908 if (dump_file && (dump_flags & TDF_DETAILS))
909 print_exprs (dump_file, "", t, " not coalesced with ", var,
910 "");
912 var = create_temp (t);
913 change_partition_var (map, var, rep);
914 ann = var_ann (var);
916 if (dump_file && (dump_flags & TDF_DETAILS))
918 fprintf (dump_file, " --> New temp: '");
919 print_generic_expr (dump_file, var, TDF_SLIM);
920 fprintf (dump_file, "'\n");
925 root_var_delete (rv);
929 /* Replace use operand P with whatever variable it has been rewritten to based
930 on the partitions in MAP. EXPR is an optional expression vector over SSA
931 versions which is used to replace P with an expression instead of a variable.
932 If the stmt is changed, return true. */
934 static inline bool
935 replace_use_variable (var_map map, use_operand_p p, tree *expr)
937 tree new_var;
938 tree var = USE_FROM_PTR (p);
940 /* Check if we are replacing this variable with an expression. */
941 if (expr)
943 int version = SSA_NAME_VERSION (var);
944 if (expr[version])
946 tree new_expr = TREE_OPERAND (expr[version], 1);
947 SET_USE (p, new_expr);
948 /* Clear the stmt's RHS, or GC might bite us. */
949 TREE_OPERAND (expr[version], 1) = NULL_TREE;
950 return true;
954 new_var = var_to_partition_to_var (map, var);
955 if (new_var)
957 SET_USE (p, new_var);
958 set_is_used (new_var);
959 return true;
961 return false;
965 /* Replace def operand DEF_P with whatever variable it has been rewritten to
966 based on the partitions in MAP. EXPR is an optional expression vector over
967 SSA versions which is used to replace DEF_P with an expression instead of a
968 variable. If the stmt is changed, return true. */
970 static inline bool
971 replace_def_variable (var_map map, def_operand_p def_p, tree *expr)
973 tree new_var;
974 tree var = DEF_FROM_PTR (def_p);
976 /* Check if we are replacing this variable with an expression. */
977 if (expr)
979 int version = SSA_NAME_VERSION (var);
980 if (expr[version])
982 tree new_expr = TREE_OPERAND (expr[version], 1);
983 SET_DEF (def_p, new_expr);
984 /* Clear the stmt's RHS, or GC might bite us. */
985 TREE_OPERAND (expr[version], 1) = NULL_TREE;
986 return true;
990 new_var = var_to_partition_to_var (map, var);
991 if (new_var)
993 SET_DEF (def_p, new_var);
994 set_is_used (new_var);
995 return true;
997 return false;
1001 /* Remove any PHI node which is a virtual PHI. */
1003 static void
1004 eliminate_virtual_phis (void)
1006 basic_block bb;
1007 tree phi, next;
1009 FOR_EACH_BB (bb)
1011 for (phi = phi_nodes (bb); phi; phi = next)
1013 next = PHI_CHAIN (phi);
1014 if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi))))
1016 #ifdef ENABLE_CHECKING
1017 int i;
1018 /* There should be no arguments of this PHI which are in
1019 the partition list, or we get incorrect results. */
1020 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1022 tree arg = PHI_ARG_DEF (phi, i);
1023 if (TREE_CODE (arg) == SSA_NAME
1024 && is_gimple_reg (SSA_NAME_VAR (arg)))
1026 fprintf (stderr, "Argument of PHI is not virtual (");
1027 print_generic_expr (stderr, arg, TDF_SLIM);
1028 fprintf (stderr, "), but the result is :");
1029 print_generic_stmt (stderr, phi, TDF_SLIM);
1030 abort();
1033 #endif
1034 remove_phi_node (phi, NULL_TREE, bb);
1041 /* This routine will coalesce variables in MAP of the same type which do not
1042 interfere with each other. LIVEINFO is the live range info for variables
1043 of interest. This will both reduce the memory footprint of the stack, and
1044 allow us to coalesce together local copies of globals and scalarized
1045 component refs. */
1047 static void
1048 coalesce_vars (var_map map, tree_live_info_p liveinfo)
1050 basic_block bb;
1051 type_var_p tv;
1052 tree var;
1053 int x, p, p2;
1054 coalesce_list_p cl;
1055 conflict_graph graph;
1057 cl = create_coalesce_list (map);
1059 /* Merge all the live on entry vectors for coalesced partitions. */
1060 for (x = 0; x < num_var_partitions (map); x++)
1062 var = partition_to_var (map, x);
1063 p = var_to_partition (map, var);
1064 if (p != x)
1065 live_merge_and_clear (liveinfo, p, x);
1068 /* When PHI nodes are turned into copies, the result of each PHI node
1069 becomes live on entry to the block. Mark these now. */
1070 FOR_EACH_BB (bb)
1072 tree phi, arg;
1073 int p;
1074 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1076 p = var_to_partition (map, PHI_RESULT (phi));
1078 /* Skip virtual PHI nodes. */
1079 if (p == NO_PARTITION)
1080 continue;
1082 make_live_on_entry (liveinfo, bb, p);
1084 /* Each argument is a potential copy operation. Add any arguments
1085 which are not coalesced to the result to the coalesce list. */
1086 for (x = 0; x < PHI_NUM_ARGS (phi); x++)
1088 arg = PHI_ARG_DEF (phi, x);
1089 if (!phi_ssa_name_p (arg))
1090 continue;
1091 p2 = var_to_partition (map, arg);
1092 if (p2 == NO_PARTITION)
1093 continue;
1094 if (p != p2)
1095 add_coalesce (cl, p, p2, 1);
1101 /* Re-calculate live on exit info. */
1102 calculate_live_on_exit (liveinfo);
1104 if (dump_file && (dump_flags & TDF_DETAILS))
1106 fprintf (dump_file, "Live range info for variable memory coalescing.\n");
1107 dump_live_info (dump_file, liveinfo, LIVEDUMP_ALL);
1109 fprintf (dump_file, "Coalesce list from phi nodes:\n");
1110 dump_coalesce_list (dump_file, cl);
1114 tv = type_var_init (map);
1115 if (dump_file)
1116 type_var_dump (dump_file, tv);
1117 type_var_compact (tv);
1118 if (dump_file)
1119 type_var_dump (dump_file, tv);
1121 graph = build_tree_conflict_graph (liveinfo, tv, cl);
1123 type_var_decompact (tv);
1124 if (dump_file && (dump_flags & TDF_DETAILS))
1126 fprintf (dump_file, "type var list now looks like:n");
1127 type_var_dump (dump_file, tv);
1129 fprintf (dump_file, "Coalesce list after conflict graph build:\n");
1130 dump_coalesce_list (dump_file, cl);
1133 sort_coalesce_list (cl);
1134 if (dump_file && (dump_flags & TDF_DETAILS))
1136 fprintf (dump_file, "Coalesce list after sorting:\n");
1137 dump_coalesce_list (dump_file, cl);
1140 coalesce_tpa_members (tv, graph, map, cl,
1141 ((dump_flags & TDF_DETAILS) ? dump_file : NULL));
1143 type_var_delete (tv);
1144 delete_coalesce_list (cl);
1148 /* Temporary Expression Replacement (TER)
1150 Replace SSA version variables during out-of-ssa with their defining
1151 expression if there is only one use of the variable.
1153 A pass is made through the function, one block at a time. No cross block
1154 information is tracked.
1156 Variables which only have one use, and whose defining stmt is considered
1157 a replaceable expression (see check_replaceable) are entered into
1158 consideration by adding a list of dependent partitions to the version_info
1159 vector for that ssa_name_version. This information comes from the partition
1160 mapping for each USE. At the same time, the partition_dep_list vector for
1161 these partitions have this version number entered into their lists.
1163 When the use of a replaceable ssa_variable is encountered, the dependence
1164 list in version_info[] is moved to the "pending_dependence" list in case
1165 the current expression is also replaceable. (To be determined later in
1166 processing this stmt.) version_info[] for the version is then updated to
1167 point to the defining stmt and the 'replaceable' bit is set.
1169 Any partition which is defined by a statement 'kills' any expression which
1170 is dependent on this partition. Every ssa version in the partitions'
1171 dependence list is removed from future consideration.
1173 All virtual references are lumped together. Any expression which is
1174 dependent on any virtual variable (via a VUSE) has a dependence added
1175 to the special partition defined by VIRTUAL_PARTITION.
1177 Whenever a V_MAY_DEF is seen, all expressions dependent this
1178 VIRTUAL_PARTITION are removed from consideration.
1180 At the end of a basic block, all expression are removed from consideration
1181 in preparation for the next block.
1183 The end result is a vector over SSA_NAME_VERSION which is passed back to
1184 rewrite_out_of_ssa. As the SSA variables are being rewritten, instead of
1185 replacing the SSA_NAME tree element with the partition it was assigned,
1186 it is replaced with the RHS of the defining expression. */
1189 /* Dependency list element. This can contain either a partition index or a
1190 version number, depending on which list it is in. */
1192 typedef struct value_expr_d
1194 int value;
1195 struct value_expr_d *next;
1196 } *value_expr_p;
1199 /* Temporary Expression Replacement (TER) table information. */
1201 typedef struct temp_expr_table_d
1203 var_map map;
1204 void **version_info;
1205 value_expr_p *partition_dep_list;
1206 bitmap replaceable;
1207 bool saw_replaceable;
1208 int virtual_partition;
1209 bitmap partition_in_use;
1210 value_expr_p free_list;
1211 value_expr_p pending_dependence;
1212 } *temp_expr_table_p;
1214 /* Used to indicate a dependency on V_MAY_DEFs. */
1215 #define VIRTUAL_PARTITION(table) (table->virtual_partition)
1217 static temp_expr_table_p new_temp_expr_table (var_map);
1218 static tree *free_temp_expr_table (temp_expr_table_p);
1219 static inline value_expr_p new_value_expr (temp_expr_table_p);
1220 static inline void free_value_expr (temp_expr_table_p, value_expr_p);
1221 static inline value_expr_p find_value_in_list (value_expr_p, int,
1222 value_expr_p *);
1223 static inline void add_value_to_list (temp_expr_table_p, value_expr_p *, int);
1224 static inline void add_info_to_list (temp_expr_table_p, value_expr_p *,
1225 value_expr_p);
1226 static value_expr_p remove_value_from_list (value_expr_p *, int);
1227 static void add_dependance (temp_expr_table_p, int, tree);
1228 static bool check_replaceable (temp_expr_table_p, tree);
1229 static void finish_expr (temp_expr_table_p, int, bool);
1230 static void mark_replaceable (temp_expr_table_p, tree);
1231 static inline void kill_expr (temp_expr_table_p, int, bool);
1232 static inline void kill_virtual_exprs (temp_expr_table_p, bool);
1233 static void find_replaceable_in_bb (temp_expr_table_p, basic_block);
1234 static tree *find_replaceable_exprs (var_map);
1235 static void dump_replaceable_exprs (FILE *, tree *);
1238 /* Create a new TER table for MAP. */
1240 static temp_expr_table_p
1241 new_temp_expr_table (var_map map)
1243 temp_expr_table_p t;
1245 t = (temp_expr_table_p) xmalloc (sizeof (struct temp_expr_table_d));
1246 t->map = map;
1248 t->version_info = xcalloc (num_ssa_names + 1, sizeof (void *));
1249 t->partition_dep_list = xcalloc (num_var_partitions (map) + 1,
1250 sizeof (value_expr_p));
1252 t->replaceable = BITMAP_XMALLOC ();
1253 t->partition_in_use = BITMAP_XMALLOC ();
1255 t->saw_replaceable = false;
1256 t->virtual_partition = num_var_partitions (map);
1257 t->free_list = NULL;
1258 t->pending_dependence = NULL;
1260 return t;
1264 /* Free TER table T. If there are valid replacements, return the expression
1265 vector. */
1267 static tree *
1268 free_temp_expr_table (temp_expr_table_p t)
1270 value_expr_p p;
1271 tree *ret = NULL;
1273 #ifdef ENABLE_CHECKING
1274 int x;
1275 for (x = 0; x <= num_var_partitions (t->map); x++)
1276 if (t->partition_dep_list[x] != NULL)
1277 abort();
1278 #endif
1280 while ((p = t->free_list))
1282 t->free_list = p->next;
1283 free (p);
1286 BITMAP_XFREE (t->partition_in_use);
1287 BITMAP_XFREE (t->replaceable);
1289 free (t->partition_dep_list);
1290 if (t->saw_replaceable)
1291 ret = (tree *)t->version_info;
1292 else
1293 free (t->version_info);
1295 free (t);
1296 return ret;
1300 /* Allocate a new value list node. Take it from the free list in TABLE if
1301 possible. */
1303 static inline value_expr_p
1304 new_value_expr (temp_expr_table_p table)
1306 value_expr_p p;
1307 if (table->free_list)
1309 p = table->free_list;
1310 table->free_list = p->next;
1312 else
1313 p = (value_expr_p) xmalloc (sizeof (struct value_expr_d));
1315 return p;
1319 /* Add value list node P to the free list in TABLE. */
1321 static inline void
1322 free_value_expr (temp_expr_table_p table, value_expr_p p)
1324 p->next = table->free_list;
1325 table->free_list = p;
1329 /* Find VALUE if its in LIST. Return a pointer to the list object if found,
1330 else return NULL. If LAST_PTR is provided, it will point to the previous
1331 item upon return, or NULL if this is the first item in the list. */
1333 static inline value_expr_p
1334 find_value_in_list (value_expr_p list, int value, value_expr_p *last_ptr)
1336 value_expr_p curr;
1337 value_expr_p last = NULL;
1339 for (curr = list; curr; last = curr, curr = curr->next)
1341 if (curr->value == value)
1342 break;
1344 if (last_ptr)
1345 *last_ptr = last;
1346 return curr;
1350 /* Add VALUE to LIST, if it isn't already present. TAB is the expression
1351 table */
1353 static inline void
1354 add_value_to_list (temp_expr_table_p tab, value_expr_p *list, int value)
1356 value_expr_p info;
1358 if (!find_value_in_list (*list, value, NULL))
1360 info = new_value_expr (tab);
1361 info->value = value;
1362 info->next = *list;
1363 *list = info;
1368 /* Add value node INFO if it's value isn't already in LIST. Free INFO if
1369 it is already in the list. TAB is the expression table. */
1371 static inline void
1372 add_info_to_list (temp_expr_table_p tab, value_expr_p *list, value_expr_p info)
1374 if (find_value_in_list (*list, info->value, NULL))
1375 free_value_expr (tab, info);
1376 else
1378 info->next = *list;
1379 *list = info;
1384 /* Look for VALUE in LIST. If found, remove it from the list and return it's
1385 pointer. */
1387 static value_expr_p
1388 remove_value_from_list (value_expr_p *list, int value)
1390 value_expr_p info, last;
1392 info = find_value_in_list (*list, value, &last);
1393 if (!info)
1394 return NULL;
1395 if (!last)
1396 *list = info->next;
1397 else
1398 last->next = info->next;
1400 return info;
1404 /* Add a dependency between the def of ssa VERSION and VAR. If VAR is
1405 replaceable by an expression, add a dependence each of the elements of the
1406 expression. These are contained in the pending list. TAB is the
1407 expression table. */
1409 static void
1410 add_dependance (temp_expr_table_p tab, int version, tree var)
1412 int i, x;
1413 value_expr_p info;
1415 i = SSA_NAME_VERSION (var);
1416 if (bitmap_bit_p (tab->replaceable, i))
1418 /* This variable is being substituted, so use whatever dependences
1419 were queued up when we marked this as replaceable earlier. */
1420 while ((info = tab->pending_dependence))
1422 tab->pending_dependence = info->next;
1423 /* Get the partition this variable was dependent on. Reuse this
1424 object to represent the current expression instead. */
1425 x = info->value;
1426 info->value = version;
1427 add_info_to_list (tab, &(tab->partition_dep_list[x]), info);
1428 add_value_to_list (tab,
1429 (value_expr_p *)&(tab->version_info[version]), x);
1430 bitmap_set_bit (tab->partition_in_use, x);
1433 else
1435 i = var_to_partition (tab->map, var);
1436 #ifdef ENABLE_CHECKING
1437 if (i== NO_PARTITION)
1438 abort ();
1439 #endif
1440 add_value_to_list (tab, &(tab->partition_dep_list[i]), version);
1441 add_value_to_list (tab,
1442 (value_expr_p *)&(tab->version_info[version]), i);
1443 bitmap_set_bit (tab->partition_in_use, i);
1448 /* Check if expression STMT is suitable for replacement in table TAB. If so,
1449 create an expression entry. Return true if this stmt is replaceable. */
1451 static bool
1452 check_replaceable (temp_expr_table_p tab, tree stmt)
1454 stmt_ann_t ann;
1455 vuse_optype vuseops;
1456 def_optype defs;
1457 use_optype uses;
1458 tree var, def;
1459 int num_use_ops, version, i;
1460 var_map map = tab->map;
1462 if (TREE_CODE (stmt) != MODIFY_EXPR)
1463 return false;
1465 ann = stmt_ann (stmt);
1466 defs = DEF_OPS (ann);
1468 /* Punt if there is more than 1 def, or more than 1 use. */
1469 if (NUM_DEFS (defs) != 1)
1470 return false;
1471 def = DEF_OP (defs, 0);
1472 if (version_ref_count (map, def) != 1)
1473 return false;
1475 /* Assignments to variables assigned to hard registers are not
1476 replaceable. */
1477 if (DECL_HARD_REGISTER (SSA_NAME_VAR (def)))
1478 return false;
1480 /* There must be no V_MAY_DEFS. */
1481 if (NUM_V_MAY_DEFS (V_MAY_DEF_OPS (ann)) != 0)
1482 return false;
1484 /* There must be no V_MUST_DEFS. */
1485 if (NUM_V_MUST_DEFS (V_MUST_DEF_OPS (ann)) != 0)
1486 return false;
1488 /* Float expressions must go through memory if float-store is on. */
1489 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 1))))
1490 return false;
1492 uses = USE_OPS (ann);
1493 num_use_ops = NUM_USES (uses);
1494 vuseops = VUSE_OPS (ann);
1496 /* Any expression which has no virtual operands and no real operands
1497 should have been propagated if it's possible to do anything with them.
1498 If this happens here, it probably exists that way for a reason, so we
1499 won't touch it. An example is:
1500 b_4 = &tab
1501 There are no virtual uses nor any real uses, so we just leave this
1502 alone to be safe. */
1504 if (num_use_ops == 0 && NUM_VUSES (vuseops) == 0)
1505 return false;
1507 version = SSA_NAME_VERSION (def);
1509 /* Add this expression to the dependency list for each use partition. */
1510 for (i = 0; i < num_use_ops; i++)
1512 var = USE_OP (uses, i);
1513 add_dependance (tab, version, var);
1516 /* If there are VUSES, add a dependence on virtual defs. */
1517 if (NUM_VUSES (vuseops) != 0)
1519 add_value_to_list (tab, (value_expr_p *)&(tab->version_info[version]),
1520 VIRTUAL_PARTITION (tab));
1521 add_value_to_list (tab,
1522 &(tab->partition_dep_list[VIRTUAL_PARTITION (tab)]),
1523 version);
1524 bitmap_set_bit (tab->partition_in_use, VIRTUAL_PARTITION (tab));
1527 return true;
1531 /* This function will remove the expression for VERSION from replacement
1532 consideration.n table TAB If 'replace' is true, it is marked as
1533 replaceable, otherwise not. */
1535 static void
1536 finish_expr (temp_expr_table_p tab, int version, bool replace)
1538 value_expr_p info, tmp;
1539 int partition;
1541 /* Remove this expression from its dependent lists. The partition dependence
1542 list is retained and transfered later to whomever uses this version. */
1543 for (info = (value_expr_p) tab->version_info[version]; info; info = tmp)
1545 partition = info->value;
1546 #ifdef ENABLE_CHECKING
1547 if (tab->partition_dep_list[partition] == NULL)
1548 abort ();
1549 #endif
1550 tmp = remove_value_from_list (&(tab->partition_dep_list[partition]),
1551 version);
1552 #ifdef ENABLE_CHECKING
1553 if (!tmp)
1554 abort ();
1555 #endif
1556 free_value_expr (tab, tmp);
1557 /* Only clear the bit when the dependency list is emptied via
1558 a replacement. Otherwise kill_expr will take care of it. */
1559 if (!(tab->partition_dep_list[partition]) && replace)
1560 bitmap_clear_bit (tab->partition_in_use, partition);
1561 tmp = info->next;
1562 if (!replace)
1563 free_value_expr (tab, info);
1566 if (replace)
1568 tab->saw_replaceable = true;
1569 bitmap_set_bit (tab->replaceable, version);
1571 else
1573 #ifdef ENABLE_CHECKING
1574 if (bitmap_bit_p (tab->replaceable, version))
1575 abort ();
1576 #endif
1577 tab->version_info[version] = NULL;
1582 /* Mark the expression associated with VAR as replaceable, and enter
1583 the defining stmt into the version_info table TAB. */
1585 static void
1586 mark_replaceable (temp_expr_table_p tab, tree var)
1588 value_expr_p info;
1589 int version = SSA_NAME_VERSION (var);
1590 finish_expr (tab, version, true);
1592 /* Move the dependence list to the pending list. */
1593 if (tab->version_info[version])
1595 info = (value_expr_p) tab->version_info[version];
1596 for ( ; info->next; info = info->next)
1597 continue;
1598 info->next = tab->pending_dependence;
1599 tab->pending_dependence = (value_expr_p)tab->version_info[version];
1602 tab->version_info[version] = SSA_NAME_DEF_STMT (var);
1606 /* This function marks any expression in TAB which is dependent on PARTITION
1607 as NOT replaceable. CLEAR_BIT is used to determine whether partition_in_use
1608 should have its bit cleared. Since this routine can be called within an
1609 EXECUTE_IF_SET_IN_BITMAP, the bit can't always be cleared. */
1611 static inline void
1612 kill_expr (temp_expr_table_p tab, int partition, bool clear_bit)
1614 value_expr_p ptr;
1616 /* Mark every active expr dependent on this var as not replaceable. */
1617 while ((ptr = tab->partition_dep_list[partition]) != NULL)
1618 finish_expr (tab, ptr->value, false);
1620 if (clear_bit)
1621 bitmap_clear_bit (tab->partition_in_use, partition);
1625 /* This function kills all expressions in TAB which are dependent on virtual
1626 DEFs. CLEAR_BIT determines whether partition_in_use gets cleared. */
1628 static inline void
1629 kill_virtual_exprs (temp_expr_table_p tab, bool clear_bit)
1631 kill_expr (tab, VIRTUAL_PARTITION (tab), clear_bit);
1635 /* This function processes basic block BB, and looks for variables which can
1636 be replaced by their expressions. Results are stored in TAB. */
1638 static void
1639 find_replaceable_in_bb (temp_expr_table_p tab, basic_block bb)
1641 block_stmt_iterator bsi;
1642 tree stmt, def;
1643 stmt_ann_t ann;
1644 int partition, num, i;
1645 use_optype uses;
1646 def_optype defs;
1647 var_map map = tab->map;
1648 value_expr_p p;
1650 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1652 stmt = bsi_stmt (bsi);
1653 ann = stmt_ann (stmt);
1655 /* Determine if this stmt finishes an existing expression. */
1656 uses = USE_OPS (ann);
1657 num = NUM_USES (uses);
1658 for (i = 0; i < num; i++)
1660 def = USE_OP (uses, i);
1661 if (tab->version_info[SSA_NAME_VERSION (def)])
1663 /* Mark expression as replaceable unless stmt is volatile. */
1664 if (!ann->has_volatile_ops)
1665 mark_replaceable (tab, def);
1666 else
1667 finish_expr (tab, SSA_NAME_VERSION (def), false);
1671 /* Next, see if this stmt kills off an active expression. */
1672 defs = DEF_OPS (ann);
1673 num = NUM_DEFS (defs);
1674 for (i = 0; i < num; i++)
1676 def = DEF_OP (defs, i);
1677 partition = var_to_partition (map, def);
1678 if (partition != NO_PARTITION && tab->partition_dep_list[partition])
1679 kill_expr (tab, partition, true);
1682 /* Now see if we are creating a new expression or not. */
1683 if (!ann->has_volatile_ops)
1684 check_replaceable (tab, stmt);
1686 /* Free any unused dependency lists. */
1687 while ((p = tab->pending_dependence))
1689 tab->pending_dependence = p->next;
1690 free_value_expr (tab, p);
1693 /* A V_MAY_DEF kills any expression using a virtual operand. */
1694 if (NUM_V_MAY_DEFS (V_MAY_DEF_OPS (ann)) > 0)
1695 kill_virtual_exprs (tab, true);
1697 /* A V_MUST_DEF kills any expression using a virtual operand. */
1698 if (NUM_V_MUST_DEFS (V_MUST_DEF_OPS (ann)) > 0)
1699 kill_virtual_exprs (tab, true);
1704 /* This function is the driver routine for replacement of temporary expressions
1705 in the SSA->normal phase, operating on MAP. If there are replaceable
1706 expressions, a table is returned which maps SSA versions to the
1707 expressions they should be replaced with. A NULL_TREE indicates no
1708 replacement should take place. If there are no replacements at all,
1709 NULL is returned by the function, otherwise an expression vector indexed
1710 by SSA_NAME version numbers. */
1712 static tree *
1713 find_replaceable_exprs (var_map map)
1715 basic_block bb;
1716 int i;
1717 temp_expr_table_p table;
1718 tree *ret;
1720 table = new_temp_expr_table (map);
1721 FOR_EACH_BB (bb)
1723 find_replaceable_in_bb (table, bb);
1724 EXECUTE_IF_SET_IN_BITMAP ((table->partition_in_use), 0, i,
1726 kill_expr (table, i, false);
1730 ret = free_temp_expr_table (table);
1731 return ret;
1735 /* Dump TER expression table EXPR to file F. */
1737 static void
1738 dump_replaceable_exprs (FILE *f, tree *expr)
1740 tree stmt, var;
1741 int x;
1742 fprintf (f, "\nReplacing Expressions\n");
1743 for (x = 0; x < (int)num_ssa_names + 1; x++)
1744 if (expr[x])
1746 stmt = expr[x];
1747 var = DEF_OP (STMT_DEF_OPS (stmt), 0);
1748 print_generic_expr (f, var, TDF_SLIM);
1749 fprintf (f, " replace with --> ");
1750 print_generic_expr (f, TREE_OPERAND (stmt, 1), TDF_SLIM);
1751 fprintf (f, "\n");
1753 fprintf (f, "\n");
1757 /* Helper function for discover_nonconstant_array_refs.
1758 Look for ARRAY_REF nodes with non-constant indexes and mark them
1759 addressable. */
1761 static tree
1762 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
1763 void *data ATTRIBUTE_UNUSED)
1765 tree t = *tp;
1767 if (TYPE_P (t) || DECL_P (t))
1768 *walk_subtrees = 0;
1769 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1771 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1772 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
1773 && (!TREE_OPERAND (t, 2)
1774 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
1775 || (TREE_CODE (t) == COMPONENT_REF
1776 && (!TREE_OPERAND (t,2)
1777 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
1778 || TREE_CODE (t) == BIT_FIELD_REF
1779 || TREE_CODE (t) == REALPART_EXPR
1780 || TREE_CODE (t) == IMAGPART_EXPR
1781 || TREE_CODE (t) == VIEW_CONVERT_EXPR
1782 || TREE_CODE (t) == NOP_EXPR
1783 || TREE_CODE (t) == CONVERT_EXPR)
1784 t = TREE_OPERAND (t, 0);
1786 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1788 t = get_base_address (t);
1789 if (t && DECL_P (t))
1790 TREE_ADDRESSABLE (t) = 1;
1793 *walk_subtrees = 0;
1796 return NULL_TREE;
1800 /* RTL expansion is not able to compile array references with variable
1801 offsets for arrays stored in single register. Discover such
1802 expressions and mark variables as addressable to avoid this
1803 scenario. */
1805 static void
1806 discover_nonconstant_array_refs (void)
1808 basic_block bb;
1809 block_stmt_iterator bsi;
1811 FOR_EACH_BB (bb)
1813 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1814 walk_tree (bsi_stmt_ptr (bsi), discover_nonconstant_array_refs_r,
1815 NULL , NULL);
1820 /* This function will rewrite the current program using the variable mapping
1821 found in MAP. If the replacement vector VALUES is provided, any
1822 occurrences of partitions with non-null entries in the vector will be
1823 replaced with the expression in the vector instead of its mapped
1824 variable. */
1826 static void
1827 rewrite_trees (var_map map, tree *values)
1829 elim_graph g;
1830 basic_block bb;
1831 block_stmt_iterator si;
1832 edge e;
1833 tree phi;
1834 bool changed;
1836 #ifdef ENABLE_CHECKING
1837 /* Search for PHIs where the destination has no partition, but one
1838 or more arguments has a partition. This should not happen and can
1839 create incorrect code. */
1840 FOR_EACH_BB (bb)
1842 tree phi;
1844 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1846 tree T0 = var_to_partition_to_var (map, PHI_RESULT (phi));
1848 if (T0 == NULL_TREE)
1850 int i;
1852 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1854 tree arg = PHI_ARG_DEF (phi, i);
1856 if (TREE_CODE (arg) == SSA_NAME
1857 && var_to_partition (map, arg) != NO_PARTITION)
1859 fprintf (stderr, "Argument of PHI is in a partition :(");
1860 print_generic_expr (stderr, arg, TDF_SLIM);
1861 fprintf (stderr, "), but the result is not :");
1862 print_generic_stmt (stderr, phi, TDF_SLIM);
1863 abort();
1869 #endif
1871 /* Replace PHI nodes with any required copies. */
1872 g = new_elim_graph (map->num_partitions);
1873 g->map = map;
1874 FOR_EACH_BB (bb)
1876 for (si = bsi_start (bb); !bsi_end_p (si); )
1878 size_t i, num_uses, num_defs;
1879 use_optype uses;
1880 def_optype defs;
1881 tree stmt = bsi_stmt (si);
1882 use_operand_p use_p;
1883 int remove = 0, is_copy = 0;
1884 stmt_ann_t ann;
1886 get_stmt_operands (stmt);
1887 ann = stmt_ann (stmt);
1888 changed = false;
1890 if (TREE_CODE (stmt) == MODIFY_EXPR
1891 && (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME))
1892 is_copy = 1;
1894 uses = USE_OPS (ann);
1895 num_uses = NUM_USES (uses);
1897 for (i = 0; i < num_uses; i++)
1899 use_p = USE_OP_PTR (uses, i);
1900 if (replace_use_variable (map, use_p, values))
1901 changed = true;
1904 defs = DEF_OPS (ann);
1905 num_defs = NUM_DEFS (defs);
1907 /* Mark this stmt for removal if it is the list of replaceable
1908 expressions. */
1909 if (values && num_defs == 1)
1911 tree def = DEF_OP (defs, 0);
1912 tree val;
1913 val = values[SSA_NAME_VERSION (def)];
1914 if (val)
1915 remove = 1;
1917 if (!remove)
1919 for (i = 0; i < num_defs; i++)
1921 def_operand_p def_p = DEF_OP_PTR (defs, i);
1923 if (replace_def_variable (map, def_p, NULL))
1924 changed = true;
1926 /* If both SSA_NAMEs coalesce to the same variable,
1927 mark the now redundant copy for removal. */
1928 if (is_copy
1929 && num_uses == 1
1930 && (DEF_FROM_PTR (def_p) == USE_OP (uses, 0)))
1931 remove = 1;
1933 if (changed)
1934 modify_stmt (stmt);
1937 /* Remove any stmts marked for removal. */
1938 if (remove)
1939 bsi_remove (&si);
1940 else
1941 bsi_next (&si);
1944 phi = phi_nodes (bb);
1945 if (phi)
1947 FOR_EACH_EDGE (e, bb->preds)
1949 eliminate_phi (e, phi_arg_from_edge (phi, e), g);
1951 END_FOR_EACH_EDGE;
1955 delete_elim_graph (g);
1957 /* If any copies were inserted on edges, actually insert them now. */
1958 bsi_commit_edge_inserts (NULL);
1962 /* Remove the variables specified in MAP from SSA form. Any debug information
1963 is sent to DUMP. FLAGS indicate what options should be used. */
1965 void
1966 remove_ssa_form (FILE *dump, var_map map, int flags)
1968 tree_live_info_p liveinfo;
1969 basic_block bb;
1970 tree phi, next;
1971 FILE *save;
1972 tree *values = NULL;
1974 save = dump_file;
1975 dump_file = dump;
1977 /* If we are not combining temps, don't calculate live ranges for variables
1978 with only one SSA version. */
1979 if ((flags & SSANORM_COMBINE_TEMPS) == 0)
1980 compact_var_map (map, VARMAP_NO_SINGLE_DEFS);
1981 else
1982 compact_var_map (map, VARMAP_NORMAL);
1984 if (dump_file && (dump_flags & TDF_DETAILS))
1985 dump_var_map (dump_file, map);
1987 liveinfo = coalesce_ssa_name (map, flags);
1989 /* Make sure even single occurrence variables are in the list now. */
1990 if ((flags & SSANORM_COMBINE_TEMPS) == 0)
1991 compact_var_map (map, VARMAP_NORMAL);
1993 if (dump_file && (dump_flags & TDF_DETAILS))
1995 fprintf (dump_file, "After Coalescing:\n");
1996 dump_var_map (dump_file, map);
1999 if (flags & SSANORM_PERFORM_TER)
2001 values = find_replaceable_exprs (map);
2002 if (values && dump_file && (dump_flags & TDF_DETAILS))
2003 dump_replaceable_exprs (dump_file, values);
2006 /* Assign real variables to the partitions now. */
2007 assign_vars (map);
2009 if (dump_file && (dump_flags & TDF_DETAILS))
2011 fprintf (dump_file, "After Root variable replacement:\n");
2012 dump_var_map (dump_file, map);
2015 if ((flags & SSANORM_COMBINE_TEMPS) && liveinfo)
2017 coalesce_vars (map, liveinfo);
2018 if (dump_file && (dump_flags & TDF_DETAILS))
2020 fprintf (dump_file, "After variable memory coalescing:\n");
2021 dump_var_map (dump_file, map);
2025 if (liveinfo)
2026 delete_tree_live_info (liveinfo);
2028 rewrite_trees (map, values);
2030 if (values)
2031 free (values);
2033 /* Remove phi nodes which have been translated back to real variables. */
2034 FOR_EACH_BB (bb)
2036 for (phi = phi_nodes (bb); phi; phi = next)
2038 next = PHI_CHAIN (phi);
2039 if ((flags & SSANORM_REMOVE_ALL_PHIS)
2040 || var_to_partition (map, PHI_RESULT (phi)) != NO_PARTITION)
2041 remove_phi_node (phi, NULL_TREE, bb);
2045 dump_file = save;
2049 /* Take a subset of the variables VARS in the current function out of SSA
2050 form. */
2052 void
2053 rewrite_vars_out_of_ssa (bitmap vars)
2055 if (bitmap_first_set_bit (vars) >= 0)
2057 var_map map;
2058 basic_block bb;
2059 tree phi;
2060 int i;
2061 int ssa_flags;
2063 /* Search for PHIs in which one of the PHI arguments is marked for
2064 translation out of SSA form, but for which the PHI result is not
2065 marked for translation out of SSA form.
2067 Our per-variable out of SSA translation can not handle that case;
2068 however we can easily handle it here by creating a new instance
2069 of the PHI result's underlying variable and initializing it to
2070 the offending PHI argument on the edge associated with the
2071 PHI argument. We then change the PHI argument to use our new
2072 instead of the PHI's underlying variable.
2074 You might think we could register partitions for the out-of-ssa
2075 translation here and avoid a second walk of the PHI nodes. No
2076 such luck since the size of the var map will change if we have
2077 to manually take variables out of SSA form here. */
2078 FOR_EACH_BB (bb)
2080 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2082 tree result = SSA_NAME_VAR (PHI_RESULT (phi));
2084 /* If the definition is marked for renaming, then we need
2085 to do nothing more for this PHI node. */
2086 if (bitmap_bit_p (vars, var_ann (result)->uid))
2087 continue;
2089 /* Look at all the arguments and see if any of them are
2090 marked for renaming. If so, we need to handle them
2091 specially. */
2092 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
2094 tree arg = PHI_ARG_DEF (phi, i);
2096 /* If the argument is not an SSA_NAME, then we can ignore
2097 this argument. */
2098 if (TREE_CODE (arg) != SSA_NAME)
2099 continue;
2101 /* If this argument is marked for renaming, then we need
2102 to undo the copy propagation so that we can take
2103 the argument out of SSA form without taking the
2104 result out of SSA form. */
2105 arg = SSA_NAME_VAR (arg);
2106 if (bitmap_bit_p (vars, var_ann (arg)->uid))
2108 tree new_name, copy;
2110 /* Get a new SSA_NAME for the copy, it is based on
2111 the result, not the argument! We use the PHI
2112 as the definition since we haven't created the
2113 definition statement yet. */
2114 new_name = make_ssa_name (result, phi);
2116 /* Now create the copy statement. */
2117 copy = build (MODIFY_EXPR, TREE_TYPE (arg),
2118 new_name, PHI_ARG_DEF (phi, i));
2120 /* Now update SSA_NAME_DEF_STMT to point to the
2121 newly created statement. */
2122 SSA_NAME_DEF_STMT (new_name) = copy;
2124 /* Now make the argument reference our new SSA_NAME. */
2125 SET_PHI_ARG_DEF (phi, i, new_name);
2127 /* Queue the statement for insertion. */
2128 bsi_insert_on_edge (PHI_ARG_EDGE (phi, i), copy);
2129 modify_stmt (copy);
2135 /* If any copies were inserted on edges, actually insert them now. */
2136 bsi_commit_edge_inserts (NULL);
2138 /* Now register partitions for all instances of the variables we
2139 are taking out of SSA form. */
2140 map = init_var_map (num_ssa_names + 1);
2141 register_ssa_partitions_for_vars (vars, map);
2143 /* Now that we have all the partitions registered, translate the
2144 appropriate variables out of SSA form. */
2145 ssa_flags = SSANORM_COALESCE_PARTITIONS;
2146 if (flag_tree_combine_temps)
2147 ssa_flags |= SSANORM_COMBINE_TEMPS;
2148 remove_ssa_form (dump_file, map, ssa_flags);
2150 /* And finally, reset the out_of_ssa flag for each of the vars
2151 we just took out of SSA form. */
2152 EXECUTE_IF_SET_IN_BITMAP (vars, 0, i,
2154 var_ann (referenced_var (i))->out_of_ssa_tag = 0;
2157 /* Free the map as we are done with it. */
2158 delete_var_map (map);
2164 /* Take the current function out of SSA form, as described in
2165 R. Morgan, ``Building an Optimizing Compiler'',
2166 Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
2168 static void
2169 rewrite_out_of_ssa (void)
2171 var_map map;
2172 int var_flags = 0;
2173 int ssa_flags = (SSANORM_REMOVE_ALL_PHIS | SSANORM_USE_COALESCE_LIST);
2175 if (!flag_tree_live_range_split)
2176 ssa_flags |= SSANORM_COALESCE_PARTITIONS;
2178 eliminate_virtual_phis ();
2180 if (dump_file && (dump_flags & TDF_DETAILS))
2181 dump_tree_cfg (dump_file, dump_flags & ~TDF_DETAILS);
2183 /* We cannot allow unssa to un-gimplify trees before we instrument them. */
2184 if (flag_tree_ter && !flag_mudflap)
2185 var_flags = SSA_VAR_MAP_REF_COUNT;
2187 map = create_ssa_var_map (var_flags);
2189 if (flag_tree_combine_temps)
2190 ssa_flags |= SSANORM_COMBINE_TEMPS;
2191 if (flag_tree_ter && !flag_mudflap)
2192 ssa_flags |= SSANORM_PERFORM_TER;
2194 remove_ssa_form (dump_file, map, ssa_flags);
2196 if (dump_file && (dump_flags & TDF_DETAILS))
2197 dump_tree_cfg (dump_file, dump_flags & ~TDF_DETAILS);
2199 /* Do some cleanups which reduce the amount of data the
2200 tree->rtl expanders deal with. */
2201 cfg_remove_useless_stmts ();
2203 /* Flush out flow graph and SSA data. */
2204 delete_var_map (map);
2206 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
2207 discover_nonconstant_array_refs ();
2211 /* Define the parameters of the out of SSA pass. */
2213 struct tree_opt_pass pass_del_ssa =
2215 "optimized", /* name */
2216 NULL, /* gate */
2217 rewrite_out_of_ssa, /* execute */
2218 NULL, /* sub */
2219 NULL, /* next */
2220 0, /* static_pass_number */
2221 TV_TREE_SSA_TO_NORMAL, /* tv_id */
2222 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
2223 0, /* properties_provided */
2224 /* ??? If TER is enabled, we also kill gimple. */
2225 PROP_ssa, /* properties_destroyed */
2226 TODO_verify_ssa | TODO_verify_flow
2227 | TODO_verify_stmts, /* todo_flags_start */
2228 TODO_dump_func | TODO_ggc_collect /* todo_flags_finish */