Patch to fix -mcpu=G5 interface to EH runtime library.
[official-gcc.git] / gcc / tree-outof-ssa.c
blob5b1e2a8fc1e4225012a4a9ef25c4fcbe6da5b376
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 /* Flags to pass to remove_ssa_form. */
53 #define SSANORM_PERFORM_TER 0x1
54 #define SSANORM_COMBINE_TEMPS 0x2
55 #define SSANORM_REMOVE_ALL_PHIS 0x4
56 #define SSANORM_COALESCE_PARTITIONS 0x8
57 #define SSANORM_USE_COALESCE_LIST 0x10
59 /* Used to hold all the components required to do SSA PHI elimination.
60 The node and pred/succ list is a simple linear list of nodes and
61 edges represented as pairs of nodes.
63 The predecessor and successor list: Nodes are entered in pairs, where
64 [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
65 predecessors, all the odd elements are successors.
67 Rationale:
68 When implemented as bitmaps, very large programs SSA->Normal times were
69 being dominated by clearing the interference graph.
71 Typically this list of edges is extremely small since it only includes
72 PHI results and uses from a single edge which have not coalesced with
73 each other. This means that no virtual PHI nodes are included, and
74 empirical evidence suggests that the number of edges rarely exceed
75 3, and in a bootstrap of GCC, the maximum size encountered was 7.
76 This also limits the number of possible nodes that are involved to
77 rarely more than 6, and in the bootstrap of gcc, the maximum number
78 of nodes encountered was 12. */
80 typedef struct _elim_graph {
81 /* Size of the elimination vectors. */
82 int size;
84 /* List of nodes in the elimination graph. */
85 varray_type nodes;
87 /* The predecessor and successor edge list. */
88 varray_type edge_list;
90 /* Visited vector. */
91 sbitmap visited;
93 /* Stack for visited nodes. */
94 varray_type stack;
96 /* The variable partition map. */
97 var_map map;
99 /* Edge being eliminated by this graph. */
100 edge e;
102 /* List of constant copies to emit. These are pushed on in pairs. */
103 varray_type const_copies;
104 } *elim_graph;
107 /* Local functions. */
108 static tree create_temp (tree);
109 static void insert_copy_on_edge (edge, tree, tree);
110 static elim_graph new_elim_graph (int);
111 static inline void delete_elim_graph (elim_graph);
112 static inline void clear_elim_graph (elim_graph);
113 static inline int elim_graph_size (elim_graph);
114 static inline void elim_graph_add_node (elim_graph, tree);
115 static inline void elim_graph_add_edge (elim_graph, int, int);
116 static inline int elim_graph_remove_succ_edge (elim_graph, int);
118 static inline void eliminate_name (elim_graph, tree);
119 static void eliminate_build (elim_graph, basic_block, int);
120 static void elim_forward (elim_graph, int);
121 static int elim_unvisited_predecessor (elim_graph, int);
122 static void elim_backward (elim_graph, int);
123 static void elim_create (elim_graph, int);
124 static void eliminate_phi (edge, int, elim_graph);
125 static tree_live_info_p coalesce_ssa_name (var_map, int);
126 static void assign_vars (var_map);
127 static bool replace_use_variable (var_map, use_operand_p, tree *);
128 static bool replace_def_variable (var_map, def_operand_p, tree *);
129 static void eliminate_virtual_phis (void);
130 static void coalesce_abnormal_edges (var_map, conflict_graph, root_var_p);
131 static void print_exprs (FILE *, const char *, tree, const char *, tree,
132 const char *);
133 static void print_exprs_edge (FILE *, edge, const char *, tree, const char *,
134 tree);
137 /* Create a temporary variable based on the type of variable T. Use T's name
138 as the prefix. */
140 static tree
141 create_temp (tree t)
143 tree tmp;
144 const char *name = NULL;
145 tree type;
147 if (TREE_CODE (t) == SSA_NAME)
148 t = SSA_NAME_VAR (t);
150 if (TREE_CODE (t) != VAR_DECL
151 && TREE_CODE (t) != PARM_DECL)
152 abort ();
154 type = TREE_TYPE (t);
155 tmp = DECL_NAME (t);
156 if (tmp)
157 name = IDENTIFIER_POINTER (tmp);
159 if (name == NULL)
160 name = "temp";
161 tmp = create_tmp_var (type, name);
162 DECL_ARTIFICIAL (tmp) = DECL_ARTIFICIAL (t);
163 add_referenced_tmp_var (tmp);
165 /* add_referenced_tmp_var will create the annotation and set up some
166 of the flags in the annotation. However, some flags we need to
167 inherit from our original variable. */
168 var_ann (tmp)->type_mem_tag = var_ann (t)->type_mem_tag;
169 if (is_call_clobbered (t))
170 mark_call_clobbered (tmp);
172 return tmp;
176 /* This helper function fill insert a copy from a constant or variable SRC to
177 variable DEST on edge E. */
179 static void
180 insert_copy_on_edge (edge e, tree dest, tree src)
182 tree copy;
184 copy = build (MODIFY_EXPR, TREE_TYPE (dest), dest, src);
185 set_is_used (dest);
187 if (TREE_CODE (src) == ADDR_EXPR)
188 src = TREE_OPERAND (src, 0);
189 if (TREE_CODE (src) == VAR_DECL || TREE_CODE (src) == PARM_DECL)
190 set_is_used (src);
192 if (dump_file && (dump_flags & TDF_DETAILS))
194 fprintf (dump_file,
195 "Inserting a copy on edge BB%d->BB%d :",
196 e->src->index,
197 e->dest->index);
198 print_generic_expr (dump_file, copy, dump_flags);
199 fprintf (dump_file, "\n");
202 bsi_insert_on_edge (e, copy);
206 /* Create an elimination graph with SIZE nodes and associated data
207 structures. */
209 static elim_graph
210 new_elim_graph (int size)
212 elim_graph g = (elim_graph) xmalloc (sizeof (struct _elim_graph));
214 VARRAY_TREE_INIT (g->nodes, 30, "Elimination Node List");
215 VARRAY_TREE_INIT (g->const_copies, 20, "Elimination Constant Copies");
216 VARRAY_INT_INIT (g->edge_list, 20, "Elimination Edge List");
217 VARRAY_INT_INIT (g->stack, 30, " Elimination Stack");
219 g->visited = sbitmap_alloc (size);
221 return g;
225 /* Empty elimination graph G. */
227 static inline void
228 clear_elim_graph (elim_graph g)
230 VARRAY_POP_ALL (g->nodes);
231 VARRAY_POP_ALL (g->edge_list);
235 /* Delete elimination graph G. */
237 static inline void
238 delete_elim_graph (elim_graph g)
240 sbitmap_free (g->visited);
241 free (g);
245 /* Return the number of nodes in graph G. */
247 static inline int
248 elim_graph_size (elim_graph g)
250 return VARRAY_ACTIVE_SIZE (g->nodes);
254 /* Add NODE to graph G, if it doesn't exist already. */
256 static inline void
257 elim_graph_add_node (elim_graph g, tree node)
259 int x;
260 for (x = 0; x < elim_graph_size (g); x++)
261 if (VARRAY_TREE (g->nodes, x) == node)
262 return;
263 VARRAY_PUSH_TREE (g->nodes, node);
267 /* Add the edge PRED->SUCC to graph G. */
269 static inline void
270 elim_graph_add_edge (elim_graph g, int pred, int succ)
272 VARRAY_PUSH_INT (g->edge_list, pred);
273 VARRAY_PUSH_INT (g->edge_list, succ);
277 /* Remove an edge from graph G for which NODE is the predecessor, and
278 return the successor node. -1 is returned if there is no such edge. */
280 static inline int
281 elim_graph_remove_succ_edge (elim_graph g, int node)
283 int y;
284 unsigned x;
285 for (x = 0; x < VARRAY_ACTIVE_SIZE (g->edge_list); x += 2)
286 if (VARRAY_INT (g->edge_list, x) == node)
288 VARRAY_INT (g->edge_list, x) = -1;
289 y = VARRAY_INT (g->edge_list, x + 1);
290 VARRAY_INT (g->edge_list, x + 1) = -1;
291 return y;
293 return -1;
297 /* Find all the nodes in GRAPH which are successors to NODE in the
298 edge list. VAR will hold the partition number found. CODE is the
299 code fragment executed for every node found. */
301 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, CODE) \
302 do { \
303 unsigned x_; \
304 int y_; \
305 for (x_ = 0; x_ < VARRAY_ACTIVE_SIZE ((GRAPH)->edge_list); x_ += 2) \
307 y_ = VARRAY_INT ((GRAPH)->edge_list, x_); \
308 if (y_ != (NODE)) \
309 continue; \
310 (VAR) = VARRAY_INT ((GRAPH)->edge_list, x_ + 1); \
311 CODE; \
313 } while (0)
316 /* Find all the nodes which are predecessors of NODE in the edge list for
317 GRAPH. VAR will hold the partition number found. CODE is the
318 code fragment executed for every node found. */
320 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, CODE) \
321 do { \
322 unsigned x_; \
323 int y_; \
324 for (x_ = 0; x_ < VARRAY_ACTIVE_SIZE ((GRAPH)->edge_list); x_ += 2) \
326 y_ = VARRAY_INT ((GRAPH)->edge_list, x_ + 1); \
327 if (y_ != (NODE)) \
328 continue; \
329 (VAR) = VARRAY_INT ((GRAPH)->edge_list, x_); \
330 CODE; \
332 } while (0)
335 /* Add T to elimination graph G. */
337 static inline void
338 eliminate_name (elim_graph g, tree T)
340 elim_graph_add_node (g, T);
344 /* Build elimination graph G for basic block BB on incoming PHI edge I. */
346 static void
347 eliminate_build (elim_graph g, basic_block B, int i)
349 tree phi;
350 tree T0, Ti;
351 int p0, pi;
353 clear_elim_graph (g);
355 for (phi = phi_nodes (B); phi; phi = PHI_CHAIN (phi))
357 T0 = var_to_partition_to_var (g->map, PHI_RESULT (phi));
359 /* Ignore results which are not in partitions. */
360 if (T0 == NULL_TREE)
361 continue;
363 if (PHI_ARG_EDGE (phi, i) == g->e)
364 Ti = PHI_ARG_DEF (phi, i);
365 else
367 /* On rare occasions, a PHI node may not have the arguments
368 in the same order as all of the other PHI nodes. If they don't
369 match, find the appropriate index here. */
370 pi = phi_arg_from_edge (phi, g->e);
371 if (pi == -1)
372 abort();
373 Ti = PHI_ARG_DEF (phi, pi);
376 /* If this argument is a constant, or a SSA_NAME which is being
377 left in SSA form, just queue a copy to be emitted on this
378 edge. */
379 if (!phi_ssa_name_p (Ti)
380 || (TREE_CODE (Ti) == SSA_NAME
381 && var_to_partition (g->map, Ti) == NO_PARTITION))
383 /* Save constant copies until all other copies have been emitted
384 on this edge. */
385 VARRAY_PUSH_TREE (g->const_copies, T0);
386 VARRAY_PUSH_TREE (g->const_copies, Ti);
388 else
390 Ti = var_to_partition_to_var (g->map, Ti);
391 if (T0 != Ti)
393 eliminate_name (g, T0);
394 eliminate_name (g, Ti);
395 p0 = var_to_partition (g->map, T0);
396 pi = var_to_partition (g->map, Ti);
397 elim_graph_add_edge (g, p0, pi);
404 /* Push successors of T onto the elimination stack for G. */
406 static void
407 elim_forward (elim_graph g, int T)
409 int S;
410 SET_BIT (g->visited, T);
411 FOR_EACH_ELIM_GRAPH_SUCC (g, T, S,
413 if (!TEST_BIT (g->visited, S))
414 elim_forward (g, S);
416 VARRAY_PUSH_INT (g->stack, T);
420 /* Return 1 if there unvisited predecessors of T in graph G. */
422 static int
423 elim_unvisited_predecessor (elim_graph g, int T)
425 int P;
426 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
428 if (!TEST_BIT (g->visited, P))
429 return 1;
431 return 0;
434 /* Process predecessors first, and insert a copy. */
436 static void
437 elim_backward (elim_graph g, int T)
439 int P;
440 SET_BIT (g->visited, T);
441 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
443 if (!TEST_BIT (g->visited, P))
445 elim_backward (g, P);
446 insert_copy_on_edge (g->e,
447 partition_to_var (g->map, P),
448 partition_to_var (g->map, T));
453 /* Insert required copies for T in graph G. Check for a strongly connected
454 region, and create a temporary to break the cycle if one is found. */
456 static void
457 elim_create (elim_graph g, int T)
459 tree U;
460 int P, S;
462 if (elim_unvisited_predecessor (g, T))
464 U = create_temp (partition_to_var (g->map, T));
465 insert_copy_on_edge (g->e, U, partition_to_var (g->map, T));
466 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
468 if (!TEST_BIT (g->visited, P))
470 elim_backward (g, P);
471 insert_copy_on_edge (g->e, partition_to_var (g->map, P), U);
475 else
477 S = elim_graph_remove_succ_edge (g, T);
478 if (S != -1)
480 SET_BIT (g->visited, T);
481 insert_copy_on_edge (g->e,
482 partition_to_var (g->map, T),
483 partition_to_var (g->map, S));
489 /* Eliminate all the phi nodes on edge E in graph G. I is the usual PHI
490 index that edge E's values are found on. */
492 static void
493 eliminate_phi (edge e, int i, elim_graph g)
495 int num_nodes = 0;
496 int x;
497 basic_block B = e->dest;
499 #if defined ENABLE_CHECKING
500 if (i == -1)
501 abort ();
502 if (VARRAY_ACTIVE_SIZE (g->const_copies) != 0)
503 abort ();
504 #endif
506 /* Abnormal edges already have everything coalesced, or the coalescer
507 would have aborted. */
508 if (e->flags & EDGE_ABNORMAL)
509 return;
511 num_nodes = num_var_partitions (g->map);
512 g->e = e;
514 eliminate_build (g, B, i);
516 if (elim_graph_size (g) != 0)
518 sbitmap_zero (g->visited);
519 VARRAY_POP_ALL (g->stack);
521 for (x = 0; x < elim_graph_size (g); x++)
523 tree var = VARRAY_TREE (g->nodes, x);
524 int p = var_to_partition (g->map, var);
525 if (!TEST_BIT (g->visited, p))
526 elim_forward (g, p);
529 sbitmap_zero (g->visited);
530 while (VARRAY_ACTIVE_SIZE (g->stack) > 0)
532 x = VARRAY_TOP_INT (g->stack);
533 VARRAY_POP (g->stack);
534 if (!TEST_BIT (g->visited, x))
535 elim_create (g, x);
539 /* If there are any pending constant copies, issue them now. */
540 while (VARRAY_ACTIVE_SIZE (g->const_copies) > 0)
542 tree src, dest;
543 src = VARRAY_TOP_TREE (g->const_copies);
544 VARRAY_POP (g->const_copies);
545 dest = VARRAY_TOP_TREE (g->const_copies);
546 VARRAY_POP (g->const_copies);
547 insert_copy_on_edge (e, dest, src);
552 /* Shortcut routine to print messages to file F of the form:
553 "STR1 EXPR1 STR2 EXPR2 STR3." */
555 static void
556 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
557 tree expr2, const char *str3)
559 fprintf (f, "%s", str1);
560 print_generic_expr (f, expr1, TDF_SLIM);
561 fprintf (f, "%s", str2);
562 print_generic_expr (f, expr2, TDF_SLIM);
563 fprintf (f, "%s", str3);
567 /* Shortcut routine to print abnormal edge messages to file F of the form:
568 "STR1 EXPR1 STR2 EXPR2 across edge E. */
570 static void
571 print_exprs_edge (FILE *f, edge e, const char *str1, tree expr1,
572 const char *str2, tree expr2)
574 print_exprs (f, str1, expr1, str2, expr2, " across an abnormal edge");
575 fprintf (f, " from BB%d->BB%d\n", e->src->index,
576 e->dest->index);
580 /* Coalesce partitions in MAP which are live across abnormal edges in GRAPH.
581 RV is the root variable groupings of the partitions in MAP. Since code
582 cannot be inserted on these edges, failure to coalesce something across
583 an abnormal edge is an error. */
585 static void
586 coalesce_abnormal_edges (var_map map, conflict_graph graph, root_var_p rv)
588 basic_block bb;
589 edge e;
590 tree phi, var, tmp;
591 int x, y;
593 /* Code cannot be inserted on abnormal edges. Look for all abnormal
594 edges, and coalesce any PHI results with their arguments across
595 that edge. */
597 FOR_EACH_BB (bb)
598 for (e = bb->succ; e; e = e->succ_next)
599 if (e->dest != EXIT_BLOCK_PTR && e->flags & EDGE_ABNORMAL)
600 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
602 /* Visit each PHI on the destination side of this abnormal
603 edge, and attempt to coalesce the argument with the result. */
604 var = PHI_RESULT (phi);
605 x = var_to_partition (map, var);
607 /* Ignore results which are not relevant. */
608 if (x == NO_PARTITION)
609 continue;
611 y = phi_arg_from_edge (phi, e);
612 if (y == -1)
613 abort ();
615 tmp = PHI_ARG_DEF (phi, y);
616 if (!phi_ssa_name_p (tmp))
618 print_exprs_edge (stderr, e,
619 "\nConstant argument in PHI. Can't insert :",
620 var, " = ", tmp);
621 abort ();
623 y = var_to_partition (map, tmp);
624 if (x == NO_PARTITION || y == NO_PARTITION)
625 abort ();
626 if (root_var_find (rv, x) != root_var_find (rv, y))
628 print_exprs_edge (stderr, e, "\nDifferent root vars: ",
629 root_var (rv, root_var_find (rv, x)),
630 " and ",
631 root_var (rv, root_var_find (rv, y)));
632 abort ();
635 if (x != y)
637 if (!conflict_graph_conflict_p (graph, x, y))
639 /* Now map the partitions back to their real variables. */
640 var = partition_to_var (map, x);
641 tmp = partition_to_var (map, y);
642 if (dump_file
643 && (dump_flags & TDF_DETAILS))
645 print_exprs_edge (dump_file, e,
646 "ABNORMAL: Coalescing ",
647 var, " and ", tmp);
649 if (var_union (map, var, tmp) == NO_PARTITION)
651 print_exprs_edge (stderr, e, "\nUnable to coalesce",
652 partition_to_var (map, x), " and ",
653 partition_to_var (map, y));
654 abort ();
656 conflict_graph_merge_regs (graph, x, y);
658 else
660 print_exprs_edge (stderr, e, "\n Conflict ",
661 partition_to_var (map, x),
662 " and ", partition_to_var (map, y));
663 abort ();
670 /* Reduce the number of live ranges in MAP. Live range information is
671 returned if FLAGS indicates that we are combining temporaries, otherwise
672 NULL is returned. The only partitions which are associated with actual
673 variables at this point are those which are forced to be coalesced for
674 various reason. (live on entry, live across abnormal edges, etc.). */
676 static tree_live_info_p
677 coalesce_ssa_name (var_map map, int flags)
679 int num, x, i;
680 sbitmap live;
681 tree var, phi;
682 root_var_p rv;
683 tree_live_info_p liveinfo;
684 var_ann_t ann;
685 conflict_graph graph;
686 basic_block bb;
687 coalesce_list_p cl = NULL;
689 if (num_var_partitions (map) <= 1)
690 return NULL;
692 /* If no preference given, use cheap coalescing of all partitions. */
693 if ((flags & (SSANORM_COALESCE_PARTITIONS | SSANORM_USE_COALESCE_LIST)) == 0)
694 flags |= SSANORM_COALESCE_PARTITIONS;
696 liveinfo = calculate_live_on_entry (map);
697 calculate_live_on_exit (liveinfo);
698 rv = root_var_init (map);
700 /* Remove single element variable from the list. */
701 root_var_compact (rv);
703 if (flags & SSANORM_USE_COALESCE_LIST)
705 cl = create_coalesce_list (map);
707 /* Add all potential copies via PHI arguments to the list. */
708 FOR_EACH_BB (bb)
710 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
712 tree res = PHI_RESULT (phi);
713 int p = var_to_partition (map, res);
714 if (p == NO_PARTITION)
715 continue;
716 for (x = 0; x < PHI_NUM_ARGS (phi); x++)
718 tree arg = PHI_ARG_DEF (phi, x);
719 int p2;
721 if (TREE_CODE (arg) != SSA_NAME)
722 continue;
723 if (SSA_NAME_VAR (res) != SSA_NAME_VAR (arg))
724 continue;
725 p2 = var_to_partition (map, PHI_ARG_DEF (phi, x));
726 if (p2 != NO_PARTITION)
727 add_coalesce (cl, p, p2, 1);
732 /* Coalesce all the result decls together. */
733 var = NULL_TREE;
734 i = 0;
735 for (x = 0; x < num_var_partitions (map); x++)
737 tree p = partition_to_var (map, x);
738 if (TREE_CODE (SSA_NAME_VAR(p)) == RESULT_DECL)
740 if (var == NULL_TREE)
742 var = p;
743 i = x;
745 else
746 add_coalesce (cl, i, x, 1);
751 /* Build a conflict graph. */
752 graph = build_tree_conflict_graph (liveinfo, rv, cl);
754 if (cl)
756 if (dump_file && (dump_flags & TDF_DETAILS))
758 fprintf (dump_file, "Before sorting:\n");
759 dump_coalesce_list (dump_file, cl);
762 sort_coalesce_list (cl);
764 if (dump_file && (dump_flags & TDF_DETAILS))
766 fprintf (dump_file, "\nAfter sorting:\n");
767 dump_coalesce_list (dump_file, cl);
771 /* Put the single element variables back in. */
772 root_var_decompact (rv);
774 /* First, coalesce all live on entry variables to their root variable.
775 This will ensure the first use is coming from the correct location. */
777 live = sbitmap_alloc (num_var_partitions (map));
778 sbitmap_zero (live);
780 /* Set 'live' vector to indicate live on entry partitions. */
781 num = num_var_partitions (map);
782 for (x = 0 ; x < num; x++)
784 var = partition_to_var (map, x);
785 if (default_def (SSA_NAME_VAR (var)) == var)
786 SET_BIT (live, x);
789 if ((flags & SSANORM_COMBINE_TEMPS) == 0)
791 delete_tree_live_info (liveinfo);
792 liveinfo = NULL;
795 /* Assign root variable as partition representative for each live on entry
796 partition. */
797 EXECUTE_IF_SET_IN_SBITMAP (live, 0, x,
799 var = root_var (rv, root_var_find (rv, x));
800 ann = var_ann (var);
801 /* If these aren't already coalesced... */
802 if (partition_to_var (map, x) != var)
804 if (ann->out_of_ssa_tag)
806 /* This root variable has already been assigned to another
807 partition which is not coalesced with this one. */
808 abort ();
811 if (dump_file && (dump_flags & TDF_DETAILS))
813 print_exprs (dump_file, "Must coalesce ",
814 partition_to_var (map, x),
815 " with the root variable ", var, ".\n");
818 change_partition_var (map, var, x);
822 sbitmap_free (live);
824 /* Coalesce partitions live across abnormal edges. */
825 coalesce_abnormal_edges (map, graph, rv);
827 if (dump_file && (dump_flags & TDF_DETAILS))
828 dump_var_map (dump_file, map);
830 /* Coalesce partitions. */
831 if (flags & SSANORM_USE_COALESCE_LIST)
832 coalesce_tpa_members (rv, graph, map, cl,
833 ((dump_flags & TDF_DETAILS) ? dump_file
834 : NULL));
837 if (flags & SSANORM_COALESCE_PARTITIONS)
838 coalesce_tpa_members (rv, graph, map, NULL,
839 ((dump_flags & TDF_DETAILS) ? dump_file
840 : NULL));
841 if (cl)
842 delete_coalesce_list (cl);
843 root_var_delete (rv);
844 conflict_graph_delete (graph);
846 return liveinfo;
850 /* Take the ssa-name var_map MAP, and assign real variables to each
851 partition. */
853 static void
854 assign_vars (var_map map)
856 int x, i, num, rep;
857 tree t, var;
858 var_ann_t ann;
859 root_var_p rv;
861 rv = root_var_init (map);
862 if (!rv)
863 return;
865 /* Coalescing may already have forced some partitions to their root
866 variable. Find these and tag them. */
868 num = num_var_partitions (map);
869 for (x = 0; x < num; x++)
871 var = partition_to_var (map, x);
872 if (TREE_CODE (var) != SSA_NAME)
874 /* Coalescing will already have verified that more than one
875 partition doesn't have the same root variable. Simply marked
876 the variable as assigned. */
877 ann = var_ann (var);
878 ann->out_of_ssa_tag = 1;
879 if (dump_file && (dump_flags & TDF_DETAILS))
881 fprintf (dump_file, "partition %d has variable ", x);
882 print_generic_expr (dump_file, var, TDF_SLIM);
883 fprintf (dump_file, " assigned to it.\n");
889 num = root_var_num (rv);
890 for (x = 0; x < num; x++)
892 var = root_var (rv, x);
893 ann = var_ann (var);
894 for (i = root_var_first_partition (rv, x);
895 i != ROOT_VAR_NONE;
896 i = root_var_next_partition (rv, i))
898 t = partition_to_var (map, i);
900 if (t == var || TREE_CODE (t) != SSA_NAME)
901 continue;
903 rep = var_to_partition (map, t);
905 if (!ann->out_of_ssa_tag)
907 if (dump_file && (dump_flags & TDF_DETAILS))
908 print_exprs (dump_file, "", t, " --> ", var, "\n");
909 change_partition_var (map, var, rep);
910 continue;
913 if (dump_file && (dump_flags & TDF_DETAILS))
914 print_exprs (dump_file, "", t, " not coalesced with ", var,
915 "");
917 var = create_temp (t);
918 change_partition_var (map, var, rep);
919 ann = var_ann (var);
921 if (dump_file && (dump_flags & TDF_DETAILS))
923 fprintf (dump_file, " --> New temp: '");
924 print_generic_expr (dump_file, var, TDF_SLIM);
925 fprintf (dump_file, "'\n");
930 root_var_delete (rv);
934 /* Replace use operand P with whatever variable it has been rewritten to based
935 on the partitions in MAP. EXPR is an optional expression vector over SSA
936 versions which is used to replace P with an expression instead of a variable.
937 If the stmt is changed, return true. */
939 static inline bool
940 replace_use_variable (var_map map, use_operand_p p, tree *expr)
942 tree new_var;
943 tree var = USE_FROM_PTR (p);
945 /* Check if we are replacing this variable with an expression. */
946 if (expr)
948 int version = SSA_NAME_VERSION (var);
949 if (expr[version])
951 tree new_expr = TREE_OPERAND (expr[version], 1);
952 SET_USE (p, new_expr);
953 /* Clear the stmt's RHS, or GC might bite us. */
954 TREE_OPERAND (expr[version], 1) = NULL_TREE;
955 return true;
959 new_var = var_to_partition_to_var (map, var);
960 if (new_var)
962 SET_USE (p, new_var);
963 set_is_used (new_var);
964 return true;
966 return false;
970 /* Replace def operand DEF_P with whatever variable it has been rewritten to
971 based on the partitions in MAP. EXPR is an optional expression vector over
972 SSA versions which is used to replace DEF_P with an expression instead of a
973 variable. If the stmt is changed, return true. */
975 static inline bool
976 replace_def_variable (var_map map, def_operand_p def_p, tree *expr)
978 tree new_var;
979 tree var = DEF_FROM_PTR (def_p);
981 /* Check if we are replacing this variable with an expression. */
982 if (expr)
984 int version = SSA_NAME_VERSION (var);
985 if (expr[version])
987 tree new_expr = TREE_OPERAND (expr[version], 1);
988 SET_DEF (def_p, new_expr);
989 /* Clear the stmt's RHS, or GC might bite us. */
990 TREE_OPERAND (expr[version], 1) = NULL_TREE;
991 return true;
995 new_var = var_to_partition_to_var (map, var);
996 if (new_var)
998 SET_DEF (def_p, new_var);
999 set_is_used (new_var);
1000 return true;
1002 return false;
1006 /* Remove any PHI node which is a virtual PHI. */
1008 static void
1009 eliminate_virtual_phis (void)
1011 basic_block bb;
1012 tree phi, next;
1014 FOR_EACH_BB (bb)
1016 for (phi = phi_nodes (bb); phi; phi = next)
1018 next = PHI_CHAIN (phi);
1019 if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi))))
1021 #ifdef ENABLE_CHECKING
1022 int i;
1023 /* There should be no arguments of this PHI which are in
1024 the partition list, or we get incorrect results. */
1025 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1027 tree arg = PHI_ARG_DEF (phi, i);
1028 if (TREE_CODE (arg) == SSA_NAME
1029 && is_gimple_reg (SSA_NAME_VAR (arg)))
1031 fprintf (stderr, "Argument of PHI is not virtual (");
1032 print_generic_expr (stderr, arg, TDF_SLIM);
1033 fprintf (stderr, "), but the result is :");
1034 print_generic_stmt (stderr, phi, TDF_SLIM);
1035 abort();
1038 #endif
1039 remove_phi_node (phi, NULL_TREE, bb);
1046 /* This routine will coalesce variables in MAP of the same type which do not
1047 interfere with each other. LIVEINFO is the live range info for variables
1048 of interest. This will both reduce the memory footprint of the stack, and
1049 allow us to coalesce together local copies of globals and scalarized
1050 component refs. */
1052 static void
1053 coalesce_vars (var_map map, tree_live_info_p liveinfo)
1055 basic_block bb;
1056 type_var_p tv;
1057 tree var;
1058 int x, p, p2;
1059 coalesce_list_p cl;
1060 conflict_graph graph;
1062 cl = create_coalesce_list (map);
1064 /* Merge all the live on entry vectors for coalesced partitions. */
1065 for (x = 0; x < num_var_partitions (map); x++)
1067 var = partition_to_var (map, x);
1068 p = var_to_partition (map, var);
1069 if (p != x)
1070 live_merge_and_clear (liveinfo, p, x);
1073 /* When PHI nodes are turned into copies, the result of each PHI node
1074 becomes live on entry to the block. Mark these now. */
1075 FOR_EACH_BB (bb)
1077 tree phi, arg;
1078 int p;
1079 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1081 p = var_to_partition (map, PHI_RESULT (phi));
1083 /* Skip virtual PHI nodes. */
1084 if (p == NO_PARTITION)
1085 continue;
1087 make_live_on_entry (liveinfo, bb, p);
1089 /* Each argument is a potential copy operation. Add any arguments
1090 which are not coalesced to the result to the coalesce list. */
1091 for (x = 0; x < PHI_NUM_ARGS (phi); x++)
1093 arg = PHI_ARG_DEF (phi, x);
1094 if (!phi_ssa_name_p (arg))
1095 continue;
1096 p2 = var_to_partition (map, arg);
1097 if (p2 == NO_PARTITION)
1098 continue;
1099 if (p != p2)
1100 add_coalesce (cl, p, p2, 1);
1106 /* Re-calculate live on exit info. */
1107 calculate_live_on_exit (liveinfo);
1109 if (dump_file && (dump_flags & TDF_DETAILS))
1111 fprintf (dump_file, "Live range info for variable memory coalescing.\n");
1112 dump_live_info (dump_file, liveinfo, LIVEDUMP_ALL);
1114 fprintf (dump_file, "Coalesce list from phi nodes:\n");
1115 dump_coalesce_list (dump_file, cl);
1119 tv = type_var_init (map);
1120 if (dump_file)
1121 type_var_dump (dump_file, tv);
1122 type_var_compact (tv);
1123 if (dump_file)
1124 type_var_dump (dump_file, tv);
1126 graph = build_tree_conflict_graph (liveinfo, tv, cl);
1128 type_var_decompact (tv);
1129 if (dump_file && (dump_flags & TDF_DETAILS))
1131 fprintf (dump_file, "type var list now looks like:n");
1132 type_var_dump (dump_file, tv);
1134 fprintf (dump_file, "Coalesce list after conflict graph build:\n");
1135 dump_coalesce_list (dump_file, cl);
1138 sort_coalesce_list (cl);
1139 if (dump_file && (dump_flags & TDF_DETAILS))
1141 fprintf (dump_file, "Coalesce list after sorting:\n");
1142 dump_coalesce_list (dump_file, cl);
1145 coalesce_tpa_members (tv, graph, map, cl,
1146 ((dump_flags & TDF_DETAILS) ? dump_file : NULL));
1148 type_var_delete (tv);
1149 delete_coalesce_list (cl);
1153 /* Temporary Expression Replacement (TER)
1155 Replace SSA version variables during out-of-ssa with their defining
1156 expression if there is only one use of the variable.
1158 A pass is made through the function, one block at a time. No cross block
1159 information is tracked.
1161 Variables which only have one use, and whose defining stmt is considered
1162 a replaceable expression (see check_replaceable) are entered into
1163 consideration by adding a list of dependent partitions to the version_info
1164 vector for that ssa_name_version. This information comes from the partition
1165 mapping for each USE. At the same time, the partition_dep_list vector for
1166 these partitions have this version number entered into their lists.
1168 When the use of a replaceable ssa_variable is encountered, the dependence
1169 list in version_info[] is moved to the "pending_dependence" list in case
1170 the current expression is also replaceable. (To be determined later in
1171 processing this stmt.) version_info[] for the version is then updated to
1172 point to the defining stmt and the 'replaceable' bit is set.
1174 Any partition which is defined by a statement 'kills' any expression which
1175 is dependent on this partition. Every ssa version in the partitions'
1176 dependence list is removed from future consideration.
1178 All virtual references are lumped together. Any expression which is
1179 dependent on any virtual variable (via a VUSE) has a dependence added
1180 to the special partition defined by VIRTUAL_PARTITION.
1182 Whenever a V_MAY_DEF is seen, all expressions dependent this
1183 VIRTUAL_PARTITION are removed from consideration.
1185 At the end of a basic block, all expression are removed from consideration
1186 in preparation for the next block.
1188 The end result is a vector over SSA_NAME_VERSION which is passed back to
1189 rewrite_out_of_ssa. As the SSA variables are being rewritten, instead of
1190 replacing the SSA_NAME tree element with the partition it was assigned,
1191 it is replaced with the RHS of the defining expression. */
1194 /* Dependency list element. This can contain either a partition index or a
1195 version number, depending on which list it is in. */
1197 typedef struct value_expr_d
1199 int value;
1200 struct value_expr_d *next;
1201 } *value_expr_p;
1204 /* Temporary Expression Replacement (TER) table information. */
1206 typedef struct temp_expr_table_d
1208 var_map map;
1209 void **version_info;
1210 value_expr_p *partition_dep_list;
1211 bitmap replaceable;
1212 bool saw_replaceable;
1213 int virtual_partition;
1214 bitmap partition_in_use;
1215 value_expr_p free_list;
1216 value_expr_p pending_dependence;
1217 } *temp_expr_table_p;
1219 /* Used to indicate a dependency on V_MAY_DEFs. */
1220 #define VIRTUAL_PARTITION(table) (table->virtual_partition)
1222 static temp_expr_table_p new_temp_expr_table (var_map);
1223 static tree *free_temp_expr_table (temp_expr_table_p);
1224 static inline value_expr_p new_value_expr (temp_expr_table_p);
1225 static inline void free_value_expr (temp_expr_table_p, value_expr_p);
1226 static inline value_expr_p find_value_in_list (value_expr_p, int,
1227 value_expr_p *);
1228 static inline void add_value_to_list (temp_expr_table_p, value_expr_p *, int);
1229 static inline void add_info_to_list (temp_expr_table_p, value_expr_p *,
1230 value_expr_p);
1231 static value_expr_p remove_value_from_list (value_expr_p *, int);
1232 static void add_dependance (temp_expr_table_p, int, tree);
1233 static bool check_replaceable (temp_expr_table_p, tree);
1234 static void finish_expr (temp_expr_table_p, int, bool);
1235 static void mark_replaceable (temp_expr_table_p, tree);
1236 static inline void kill_expr (temp_expr_table_p, int, bool);
1237 static inline void kill_virtual_exprs (temp_expr_table_p, bool);
1238 static void find_replaceable_in_bb (temp_expr_table_p, basic_block);
1239 static tree *find_replaceable_exprs (var_map);
1240 static void dump_replaceable_exprs (FILE *, tree *);
1243 /* Create a new TER table for MAP. */
1245 static temp_expr_table_p
1246 new_temp_expr_table (var_map map)
1248 temp_expr_table_p t;
1250 t = (temp_expr_table_p) xmalloc (sizeof (struct temp_expr_table_d));
1251 t->map = map;
1253 t->version_info = xcalloc (num_ssa_names + 1, sizeof (void *));
1254 t->partition_dep_list = xcalloc (num_var_partitions (map) + 1,
1255 sizeof (value_expr_p));
1257 t->replaceable = BITMAP_XMALLOC ();
1258 t->partition_in_use = BITMAP_XMALLOC ();
1260 t->saw_replaceable = false;
1261 t->virtual_partition = num_var_partitions (map);
1262 t->free_list = NULL;
1263 t->pending_dependence = NULL;
1265 return t;
1269 /* Free TER table T. If there are valid replacements, return the expression
1270 vector. */
1272 static tree *
1273 free_temp_expr_table (temp_expr_table_p t)
1275 value_expr_p p;
1276 tree *ret = NULL;
1278 #ifdef ENABLE_CHECKING
1279 int x;
1280 for (x = 0; x <= num_var_partitions (t->map); x++)
1281 if (t->partition_dep_list[x] != NULL)
1282 abort();
1283 #endif
1285 while ((p = t->free_list))
1287 t->free_list = p->next;
1288 free (p);
1291 BITMAP_XFREE (t->partition_in_use);
1292 BITMAP_XFREE (t->replaceable);
1294 free (t->partition_dep_list);
1295 if (t->saw_replaceable)
1296 ret = (tree *)t->version_info;
1297 else
1298 free (t->version_info);
1300 free (t);
1301 return ret;
1305 /* Allocate a new value list node. Take it from the free list in TABLE if
1306 possible. */
1308 static inline value_expr_p
1309 new_value_expr (temp_expr_table_p table)
1311 value_expr_p p;
1312 if (table->free_list)
1314 p = table->free_list;
1315 table->free_list = p->next;
1317 else
1318 p = (value_expr_p) xmalloc (sizeof (struct value_expr_d));
1320 return p;
1324 /* Add value list node P to the free list in TABLE. */
1326 static inline void
1327 free_value_expr (temp_expr_table_p table, value_expr_p p)
1329 p->next = table->free_list;
1330 table->free_list = p;
1334 /* Find VALUE if its in LIST. Return a pointer to the list object if found,
1335 else return NULL. If LAST_PTR is provided, it will point to the previous
1336 item upon return, or NULL if this is the first item in the list. */
1338 static inline value_expr_p
1339 find_value_in_list (value_expr_p list, int value, value_expr_p *last_ptr)
1341 value_expr_p curr;
1342 value_expr_p last = NULL;
1344 for (curr = list; curr; last = curr, curr = curr->next)
1346 if (curr->value == value)
1347 break;
1349 if (last_ptr)
1350 *last_ptr = last;
1351 return curr;
1355 /* Add VALUE to LIST, if it isn't already present. TAB is the expression
1356 table */
1358 static inline void
1359 add_value_to_list (temp_expr_table_p tab, value_expr_p *list, int value)
1361 value_expr_p info;
1363 if (!find_value_in_list (*list, value, NULL))
1365 info = new_value_expr (tab);
1366 info->value = value;
1367 info->next = *list;
1368 *list = info;
1373 /* Add value node INFO if it's value isn't already in LIST. Free INFO if
1374 it is already in the list. TAB is the expression table. */
1376 static inline void
1377 add_info_to_list (temp_expr_table_p tab, value_expr_p *list, value_expr_p info)
1379 if (find_value_in_list (*list, info->value, NULL))
1380 free_value_expr (tab, info);
1381 else
1383 info->next = *list;
1384 *list = info;
1389 /* Look for VALUE in LIST. If found, remove it from the list and return it's
1390 pointer. */
1392 static value_expr_p
1393 remove_value_from_list (value_expr_p *list, int value)
1395 value_expr_p info, last;
1397 info = find_value_in_list (*list, value, &last);
1398 if (!info)
1399 return NULL;
1400 if (!last)
1401 *list = info->next;
1402 else
1403 last->next = info->next;
1405 return info;
1409 /* Add a dependency between the def of ssa VERSION and VAR. If VAR is
1410 replaceable by an expression, add a dependence each of the elements of the
1411 expression. These are contained in the pending list. TAB is the
1412 expression table. */
1414 static void
1415 add_dependance (temp_expr_table_p tab, int version, tree var)
1417 int i, x;
1418 value_expr_p info;
1420 i = SSA_NAME_VERSION (var);
1421 if (bitmap_bit_p (tab->replaceable, i))
1423 /* This variable is being substituted, so use whatever dependences
1424 were queued up when we marked this as replaceable earlier. */
1425 while ((info = tab->pending_dependence))
1427 tab->pending_dependence = info->next;
1428 /* Get the partition this variable was dependent on. Reuse this
1429 object to represent the current expression instead. */
1430 x = info->value;
1431 info->value = version;
1432 add_info_to_list (tab, &(tab->partition_dep_list[x]), info);
1433 add_value_to_list (tab,
1434 (value_expr_p *)&(tab->version_info[version]), x);
1435 bitmap_set_bit (tab->partition_in_use, x);
1438 else
1440 i = var_to_partition (tab->map, var);
1441 #ifdef ENABLE_CHECKING
1442 if (i== NO_PARTITION)
1443 abort ();
1444 #endif
1445 add_value_to_list (tab, &(tab->partition_dep_list[i]), version);
1446 add_value_to_list (tab,
1447 (value_expr_p *)&(tab->version_info[version]), i);
1448 bitmap_set_bit (tab->partition_in_use, i);
1453 /* Check if expression STMT is suitable for replacement in table TAB. If so,
1454 create an expression entry. Return true if this stmt is replaceable. */
1456 static bool
1457 check_replaceable (temp_expr_table_p tab, tree stmt)
1459 stmt_ann_t ann;
1460 vuse_optype vuseops;
1461 def_optype defs;
1462 use_optype uses;
1463 tree var, def;
1464 int num_use_ops, version, i;
1465 var_map map = tab->map;
1467 if (TREE_CODE (stmt) != MODIFY_EXPR)
1468 return false;
1470 ann = stmt_ann (stmt);
1471 defs = DEF_OPS (ann);
1473 /* Punt if there is more than 1 def, or more than 1 use. */
1474 if (NUM_DEFS (defs) != 1)
1475 return false;
1476 def = DEF_OP (defs, 0);
1477 if (version_ref_count (map, def) != 1)
1478 return false;
1480 /* Assignments to variables assigned to hard registers are not
1481 replaceable. */
1482 if (DECL_HARD_REGISTER (SSA_NAME_VAR (def)))
1483 return false;
1485 /* There must be no V_MAY_DEFS. */
1486 if (NUM_V_MAY_DEFS (V_MAY_DEF_OPS (ann)) != 0)
1487 return false;
1489 /* There must be no V_MUST_DEFS. */
1490 if (NUM_V_MUST_DEFS (V_MUST_DEF_OPS (ann)) != 0)
1491 return false;
1493 /* Float expressions must go through memory if float-store is on. */
1494 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 1))))
1495 return false;
1497 uses = USE_OPS (ann);
1498 num_use_ops = NUM_USES (uses);
1499 vuseops = VUSE_OPS (ann);
1501 /* Any expression which has no virtual operands and no real operands
1502 should have been propagated if it's possible to do anything with them.
1503 If this happens here, it probably exists that way for a reason, so we
1504 won't touch it. An example is:
1505 b_4 = &tab
1506 There are no virtual uses nor any real uses, so we just leave this
1507 alone to be safe. */
1509 if (num_use_ops == 0 && NUM_VUSES (vuseops) == 0)
1510 return false;
1512 version = SSA_NAME_VERSION (def);
1514 /* Add this expression to the dependency list for each use partition. */
1515 for (i = 0; i < num_use_ops; i++)
1517 var = USE_OP (uses, i);
1518 add_dependance (tab, version, var);
1521 /* If there are VUSES, add a dependence on virtual defs. */
1522 if (NUM_VUSES (vuseops) != 0)
1524 add_value_to_list (tab, (value_expr_p *)&(tab->version_info[version]),
1525 VIRTUAL_PARTITION (tab));
1526 add_value_to_list (tab,
1527 &(tab->partition_dep_list[VIRTUAL_PARTITION (tab)]),
1528 version);
1529 bitmap_set_bit (tab->partition_in_use, VIRTUAL_PARTITION (tab));
1532 return true;
1536 /* This function will remove the expression for VERSION from replacement
1537 consideration.n table TAB If 'replace' is true, it is marked as
1538 replaceable, otherwise not. */
1540 static void
1541 finish_expr (temp_expr_table_p tab, int version, bool replace)
1543 value_expr_p info, tmp;
1544 int partition;
1546 /* Remove this expression from its dependent lists. The partition dependence
1547 list is retained and transfered later to whomever uses this version. */
1548 for (info = (value_expr_p) tab->version_info[version]; info; info = tmp)
1550 partition = info->value;
1551 #ifdef ENABLE_CHECKING
1552 if (tab->partition_dep_list[partition] == NULL)
1553 abort ();
1554 #endif
1555 tmp = remove_value_from_list (&(tab->partition_dep_list[partition]),
1556 version);
1557 #ifdef ENABLE_CHECKING
1558 if (!tmp)
1559 abort ();
1560 #endif
1561 free_value_expr (tab, tmp);
1562 /* Only clear the bit when the dependency list is emptied via
1563 a replacement. Otherwise kill_expr will take care of it. */
1564 if (!(tab->partition_dep_list[partition]) && replace)
1565 bitmap_clear_bit (tab->partition_in_use, partition);
1566 tmp = info->next;
1567 if (!replace)
1568 free_value_expr (tab, info);
1571 if (replace)
1573 tab->saw_replaceable = true;
1574 bitmap_set_bit (tab->replaceable, version);
1576 else
1578 #ifdef ENABLE_CHECKING
1579 if (bitmap_bit_p (tab->replaceable, version))
1580 abort ();
1581 #endif
1582 tab->version_info[version] = NULL;
1587 /* Mark the expression associated with VAR as replaceable, and enter
1588 the defining stmt into the version_info table TAB. */
1590 static void
1591 mark_replaceable (temp_expr_table_p tab, tree var)
1593 value_expr_p info;
1594 int version = SSA_NAME_VERSION (var);
1595 finish_expr (tab, version, true);
1597 /* Move the dependence list to the pending list. */
1598 if (tab->version_info[version])
1600 info = (value_expr_p) tab->version_info[version];
1601 for ( ; info->next; info = info->next)
1602 continue;
1603 info->next = tab->pending_dependence;
1604 tab->pending_dependence = (value_expr_p)tab->version_info[version];
1607 tab->version_info[version] = SSA_NAME_DEF_STMT (var);
1611 /* This function marks any expression in TAB which is dependent on PARTITION
1612 as NOT replaceable. CLEAR_BIT is used to determine whether partition_in_use
1613 should have its bit cleared. Since this routine can be called within an
1614 EXECUTE_IF_SET_IN_BITMAP, the bit can't always be cleared. */
1616 static inline void
1617 kill_expr (temp_expr_table_p tab, int partition, bool clear_bit)
1619 value_expr_p ptr;
1621 /* Mark every active expr dependent on this var as not replaceable. */
1622 while ((ptr = tab->partition_dep_list[partition]) != NULL)
1623 finish_expr (tab, ptr->value, false);
1625 if (clear_bit)
1626 bitmap_clear_bit (tab->partition_in_use, partition);
1630 /* This function kills all expressions in TAB which are dependent on virtual
1631 DEFs. CLEAR_BIT determines whether partition_in_use gets cleared. */
1633 static inline void
1634 kill_virtual_exprs (temp_expr_table_p tab, bool clear_bit)
1636 kill_expr (tab, VIRTUAL_PARTITION (tab), clear_bit);
1640 /* This function processes basic block BB, and looks for variables which can
1641 be replaced by their expressions. Results are stored in TAB. */
1643 static void
1644 find_replaceable_in_bb (temp_expr_table_p tab, basic_block bb)
1646 block_stmt_iterator bsi;
1647 tree stmt, def;
1648 stmt_ann_t ann;
1649 int partition, num, i;
1650 use_optype uses;
1651 def_optype defs;
1652 var_map map = tab->map;
1653 value_expr_p p;
1655 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1657 stmt = bsi_stmt (bsi);
1658 ann = stmt_ann (stmt);
1660 /* Determine if this stmt finishes an existing expression. */
1661 uses = USE_OPS (ann);
1662 num = NUM_USES (uses);
1663 for (i = 0; i < num; i++)
1665 def = USE_OP (uses, i);
1666 if (tab->version_info[SSA_NAME_VERSION (def)])
1668 /* Mark expression as replaceable unless stmt is volatile. */
1669 if (!ann->has_volatile_ops)
1670 mark_replaceable (tab, def);
1671 else
1672 finish_expr (tab, SSA_NAME_VERSION (def), false);
1676 /* Next, see if this stmt kills off an active expression. */
1677 defs = DEF_OPS (ann);
1678 num = NUM_DEFS (defs);
1679 for (i = 0; i < num; i++)
1681 def = DEF_OP (defs, i);
1682 partition = var_to_partition (map, def);
1683 if (partition != NO_PARTITION && tab->partition_dep_list[partition])
1684 kill_expr (tab, partition, true);
1687 /* Now see if we are creating a new expression or not. */
1688 if (!ann->has_volatile_ops)
1689 check_replaceable (tab, stmt);
1691 /* Free any unused dependency lists. */
1692 while ((p = tab->pending_dependence))
1694 tab->pending_dependence = p->next;
1695 free_value_expr (tab, p);
1698 /* A V_MAY_DEF kills any expression using a virtual operand. */
1699 if (NUM_V_MAY_DEFS (V_MAY_DEF_OPS (ann)) > 0)
1700 kill_virtual_exprs (tab, true);
1702 /* A V_MUST_DEF kills any expression using a virtual operand. */
1703 if (NUM_V_MUST_DEFS (V_MUST_DEF_OPS (ann)) > 0)
1704 kill_virtual_exprs (tab, true);
1709 /* This function is the driver routine for replacement of temporary expressions
1710 in the SSA->normal phase, operating on MAP. If there are replaceable
1711 expressions, a table is returned which maps SSA versions to the
1712 expressions they should be replaced with. A NULL_TREE indicates no
1713 replacement should take place. If there are no replacements at all,
1714 NULL is returned by the function, otherwise an expression vector indexed
1715 by SSA_NAME version numbers. */
1717 static tree *
1718 find_replaceable_exprs (var_map map)
1720 basic_block bb;
1721 int i;
1722 temp_expr_table_p table;
1723 tree *ret;
1725 table = new_temp_expr_table (map);
1726 FOR_EACH_BB (bb)
1728 find_replaceable_in_bb (table, bb);
1729 EXECUTE_IF_SET_IN_BITMAP ((table->partition_in_use), 0, i,
1731 kill_expr (table, i, false);
1735 ret = free_temp_expr_table (table);
1736 return ret;
1740 /* Dump TER expression table EXPR to file F. */
1742 static void
1743 dump_replaceable_exprs (FILE *f, tree *expr)
1745 tree stmt, var;
1746 int x;
1747 fprintf (f, "\nReplacing Expressions\n");
1748 for (x = 0; x < (int)num_ssa_names + 1; x++)
1749 if (expr[x])
1751 stmt = expr[x];
1752 var = DEF_OP (STMT_DEF_OPS (stmt), 0);
1753 print_generic_expr (f, var, TDF_SLIM);
1754 fprintf (f, " replace with --> ");
1755 print_generic_expr (f, TREE_OPERAND (stmt, 1), TDF_SLIM);
1756 fprintf (f, "\n");
1758 fprintf (f, "\n");
1762 /* Helper function for discover_nonconstant_array_refs.
1763 Look for ARRAY_REF nodes with non-constant indexes and mark them
1764 addressable. */
1766 static tree
1767 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
1768 void *data ATTRIBUTE_UNUSED)
1770 tree t = *tp;
1772 if (TYPE_P (t) || DECL_P (t))
1773 *walk_subtrees = 0;
1774 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1776 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1777 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
1778 && (!TREE_OPERAND (t, 2)
1779 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
1780 || (TREE_CODE (t) == COMPONENT_REF
1781 && (!TREE_OPERAND (t,2)
1782 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
1783 || TREE_CODE (t) == BIT_FIELD_REF
1784 || TREE_CODE (t) == REALPART_EXPR
1785 || TREE_CODE (t) == IMAGPART_EXPR
1786 || TREE_CODE (t) == VIEW_CONVERT_EXPR
1787 || TREE_CODE (t) == NOP_EXPR
1788 || TREE_CODE (t) == CONVERT_EXPR)
1789 t = TREE_OPERAND (t, 0);
1791 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1793 t = get_base_address (t);
1794 if (t && DECL_P (t))
1795 TREE_ADDRESSABLE (t) = 1;
1798 *walk_subtrees = 0;
1801 return NULL_TREE;
1805 /* RTL expansion is not able to compile array references with variable
1806 offsets for arrays stored in single register. Discover such
1807 expressions and mark variables as addressable to avoid this
1808 scenario. */
1810 static void
1811 discover_nonconstant_array_refs (void)
1813 basic_block bb;
1814 block_stmt_iterator bsi;
1816 FOR_EACH_BB (bb)
1818 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1819 walk_tree (bsi_stmt_ptr (bsi), discover_nonconstant_array_refs_r,
1820 NULL , NULL);
1825 /* This function will rewrite the current program using the variable mapping
1826 found in MAP. If the replacement vector VALUES is provided, any
1827 occurrences of partitions with non-null entries in the vector will be
1828 replaced with the expression in the vector instead of its mapped
1829 variable. */
1831 static void
1832 rewrite_trees (var_map map, tree *values)
1834 elim_graph g;
1835 basic_block bb;
1836 block_stmt_iterator si;
1837 edge e;
1838 tree phi;
1839 bool changed;
1841 #ifdef ENABLE_CHECKING
1842 /* Search for PHIs where the destination has no partition, but one
1843 or more arguments has a partition. This should not happen and can
1844 create incorrect code. */
1845 FOR_EACH_BB (bb)
1847 tree phi;
1849 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1851 tree T0 = var_to_partition_to_var (map, PHI_RESULT (phi));
1853 if (T0 == NULL_TREE)
1855 int i;
1857 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1859 tree arg = PHI_ARG_DEF (phi, i);
1861 if (TREE_CODE (arg) == SSA_NAME
1862 && var_to_partition (map, arg) != NO_PARTITION)
1864 fprintf (stderr, "Argument of PHI is in a partition :(");
1865 print_generic_expr (stderr, arg, TDF_SLIM);
1866 fprintf (stderr, "), but the result is not :");
1867 print_generic_stmt (stderr, phi, TDF_SLIM);
1868 abort();
1874 #endif
1876 /* Replace PHI nodes with any required copies. */
1877 g = new_elim_graph (map->num_partitions);
1878 g->map = map;
1879 FOR_EACH_BB (bb)
1881 for (si = bsi_start (bb); !bsi_end_p (si); )
1883 size_t i, num_uses, num_defs;
1884 use_optype uses;
1885 def_optype defs;
1886 tree stmt = bsi_stmt (si);
1887 use_operand_p use_p;
1888 int remove = 0, is_copy = 0;
1889 stmt_ann_t ann;
1891 get_stmt_operands (stmt);
1892 ann = stmt_ann (stmt);
1893 changed = false;
1895 if (TREE_CODE (stmt) == MODIFY_EXPR
1896 && (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME))
1897 is_copy = 1;
1899 uses = USE_OPS (ann);
1900 num_uses = NUM_USES (uses);
1902 for (i = 0; i < num_uses; i++)
1904 use_p = USE_OP_PTR (uses, i);
1905 if (replace_use_variable (map, use_p, values))
1906 changed = true;
1909 defs = DEF_OPS (ann);
1910 num_defs = NUM_DEFS (defs);
1912 /* Mark this stmt for removal if it is the list of replaceable
1913 expressions. */
1914 if (values && num_defs == 1)
1916 tree def = DEF_OP (defs, 0);
1917 tree val;
1918 val = values[SSA_NAME_VERSION (def)];
1919 if (val)
1920 remove = 1;
1922 if (!remove)
1924 for (i = 0; i < num_defs; i++)
1926 def_operand_p def_p = DEF_OP_PTR (defs, i);
1928 if (replace_def_variable (map, def_p, NULL))
1929 changed = true;
1931 /* If both SSA_NAMEs coalesce to the same variable,
1932 mark the now redundant copy for removal. */
1933 if (is_copy
1934 && num_uses == 1
1935 && (DEF_FROM_PTR (def_p) == USE_OP (uses, 0)))
1936 remove = 1;
1938 if (changed & !remove)
1939 modify_stmt (stmt);
1942 /* Remove any stmts marked for removal. */
1943 if (remove)
1944 bsi_remove (&si);
1945 else
1946 bsi_next (&si);
1949 phi = phi_nodes (bb);
1950 if (phi)
1952 for (e = bb->pred; e; e = e->pred_next)
1953 eliminate_phi (e, phi_arg_from_edge (phi, e), g);
1957 delete_elim_graph (g);
1959 /* If any copies were inserted on edges, actually insert them now. */
1960 bsi_commit_edge_inserts (NULL);
1964 /* Remove the variables specified in MAP from SSA form. Any debug information
1965 is sent to DUMP. FLAGS indicate what options should be used. */
1967 static void
1968 remove_ssa_form (FILE *dump, var_map map, int flags)
1970 tree_live_info_p liveinfo;
1971 basic_block bb;
1972 tree phi, next;
1973 FILE *save;
1974 tree *values = NULL;
1976 save = dump_file;
1977 dump_file = dump;
1979 /* If we are not combining temps, don't calculate live ranges for variables
1980 with only one SSA version. */
1981 if ((flags & SSANORM_COMBINE_TEMPS) == 0)
1982 compact_var_map (map, VARMAP_NO_SINGLE_DEFS);
1983 else
1984 compact_var_map (map, VARMAP_NORMAL);
1986 if (dump_file && (dump_flags & TDF_DETAILS))
1987 dump_var_map (dump_file, map);
1989 liveinfo = coalesce_ssa_name (map, flags);
1991 /* Make sure even single occurrence variables are in the list now. */
1992 if ((flags & SSANORM_COMBINE_TEMPS) == 0)
1993 compact_var_map (map, VARMAP_NORMAL);
1995 if (dump_file && (dump_flags & TDF_DETAILS))
1997 fprintf (dump_file, "After Coalescing:\n");
1998 dump_var_map (dump_file, map);
2001 if (flags & SSANORM_PERFORM_TER)
2003 values = find_replaceable_exprs (map);
2004 if (values && dump_file && (dump_flags & TDF_DETAILS))
2005 dump_replaceable_exprs (dump_file, values);
2008 /* Assign real variables to the partitions now. */
2009 assign_vars (map);
2011 if (dump_file && (dump_flags & TDF_DETAILS))
2013 fprintf (dump_file, "After Root variable replacement:\n");
2014 dump_var_map (dump_file, map);
2017 if ((flags & SSANORM_COMBINE_TEMPS) && liveinfo)
2019 coalesce_vars (map, liveinfo);
2020 if (dump_file && (dump_flags & TDF_DETAILS))
2022 fprintf (dump_file, "After variable memory coalescing:\n");
2023 dump_var_map (dump_file, map);
2027 if (liveinfo)
2028 delete_tree_live_info (liveinfo);
2030 rewrite_trees (map, values);
2032 if (values)
2033 free (values);
2035 /* Remove phi nodes which have been translated back to real variables. */
2036 FOR_EACH_BB (bb)
2038 for (phi = phi_nodes (bb); phi; phi = next)
2040 next = PHI_CHAIN (phi);
2041 if ((flags & SSANORM_REMOVE_ALL_PHIS)
2042 || var_to_partition (map, PHI_RESULT (phi)) != NO_PARTITION)
2043 remove_phi_node (phi, NULL_TREE, bb);
2047 dump_file = save;
2050 /* Take the current function out of SSA form, as described in
2051 R. Morgan, ``Building an Optimizing Compiler'',
2052 Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
2054 static void
2055 rewrite_out_of_ssa (void)
2057 var_map map;
2058 int var_flags = 0;
2059 int ssa_flags = (SSANORM_REMOVE_ALL_PHIS | SSANORM_USE_COALESCE_LIST);
2061 if (!flag_tree_live_range_split)
2062 ssa_flags |= SSANORM_COALESCE_PARTITIONS;
2064 eliminate_virtual_phis ();
2066 if (dump_file && (dump_flags & TDF_DETAILS))
2067 dump_tree_cfg (dump_file, dump_flags & ~TDF_DETAILS);
2069 /* We cannot allow unssa to un-gimplify trees before we instrument them. */
2070 if (flag_tree_ter && !flag_mudflap)
2071 var_flags = SSA_VAR_MAP_REF_COUNT;
2073 map = create_ssa_var_map (var_flags);
2075 if (flag_tree_combine_temps)
2076 ssa_flags |= SSANORM_COMBINE_TEMPS;
2077 if (flag_tree_ter && !flag_mudflap)
2078 ssa_flags |= SSANORM_PERFORM_TER;
2080 remove_ssa_form (dump_file, map, ssa_flags);
2082 if (dump_file && (dump_flags & TDF_DETAILS))
2083 dump_tree_cfg (dump_file, dump_flags & ~TDF_DETAILS);
2085 /* Do some cleanups which reduce the amount of data the
2086 tree->rtl expanders deal with. */
2087 cfg_remove_useless_stmts ();
2089 /* Flush out flow graph and SSA data. */
2090 delete_var_map (map);
2092 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
2093 discover_nonconstant_array_refs ();
2097 /* Define the parameters of the out of SSA pass. */
2099 struct tree_opt_pass pass_del_ssa =
2101 "optimized", /* name */
2102 NULL, /* gate */
2103 rewrite_out_of_ssa, /* execute */
2104 NULL, /* sub */
2105 NULL, /* next */
2106 0, /* static_pass_number */
2107 TV_TREE_SSA_TO_NORMAL, /* tv_id */
2108 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
2109 0, /* properties_provided */
2110 /* ??? If TER is enabled, we also kill gimple. */
2111 PROP_ssa, /* properties_destroyed */
2112 TODO_verify_ssa | TODO_verify_flow
2113 | TODO_verify_stmts, /* todo_flags_start */
2114 TODO_dump_func | TODO_ggc_collect /* todo_flags_finish */