]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tree-outof-ssa.c
basic-block.h, [...]: Don't include errors.h and include toplev.h if necessary.
[thirdparty/gcc.git] / gcc / tree-outof-ssa.c
1 /* Convert a program in SSA form into Normal form.
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Andrew Macleod <amacleod@redhat.com>
4
5 This file is part of GCC.
6
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.
11
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.
16
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. */
21
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 "expr.h"
36 #include "function.h"
37 #include "diagnostic.h"
38 #include "bitmap.h"
39 #include "tree-flow.h"
40 #include "tree-gimple.h"
41 #include "tree-inline.h"
42 #include "varray.h"
43 #include "timevar.h"
44 #include "hashtab.h"
45 #include "tree-dump.h"
46 #include "tree-ssa-live.h"
47 #include "tree-pass.h"
48 #include "toplev.h"
49
50 /* Flags to pass to remove_ssa_form. */
51
52 #define SSANORM_PERFORM_TER 0x1
53 #define SSANORM_COMBINE_TEMPS 0x2
54 #define SSANORM_COALESCE_PARTITIONS 0x4
55
56 DEF_VEC_I(int);
57 DEF_VEC_ALLOC_I(int,heap);
58
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.
62
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.
66
67 Rationale:
68 When implemented as bitmaps, very large programs SSA->Normal times were
69 being dominated by clearing the interference graph.
70
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. */
79
80 typedef struct _elim_graph {
81 /* Size of the elimination vectors. */
82 int size;
83
84 /* List of nodes in the elimination graph. */
85 VEC(tree,heap) *nodes;
86
87 /* The predecessor and successor edge list. */
88 VEC(int,heap) *edge_list;
89
90 /* Visited vector. */
91 sbitmap visited;
92
93 /* Stack for visited nodes. */
94 varray_type stack;
95
96 /* The variable partition map. */
97 var_map map;
98
99 /* Edge being eliminated by this graph. */
100 edge e;
101
102 /* List of constant copies to emit. These are pushed on in pairs. */
103 VEC(tree,heap) *const_copies;
104 } *elim_graph;
105
106
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);
117
118 static inline void eliminate_name (elim_graph, tree);
119 static void eliminate_build (elim_graph, basic_block);
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, 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);
135
136
137 /* Create a temporary variable based on the type of variable T. Use T's name
138 as the prefix. */
139
140 static tree
141 create_temp (tree t)
142 {
143 tree tmp;
144 const char *name = NULL;
145 tree type;
146
147 if (TREE_CODE (t) == SSA_NAME)
148 t = SSA_NAME_VAR (t);
149
150 gcc_assert (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL);
151
152 type = TREE_TYPE (t);
153 tmp = DECL_NAME (t);
154 if (tmp)
155 name = IDENTIFIER_POINTER (tmp);
156
157 if (name == NULL)
158 name = "temp";
159 tmp = create_tmp_var (type, name);
160
161 if (DECL_DEBUG_EXPR_IS_FROM (t) && DECL_DEBUG_EXPR (t))
162 {
163 SET_DECL_DEBUG_EXPR (tmp, DECL_DEBUG_EXPR (t));
164 DECL_DEBUG_EXPR_IS_FROM (tmp) = 1;
165 }
166 else if (!DECL_IGNORED_P (t))
167 {
168 SET_DECL_DEBUG_EXPR (tmp, t);
169 DECL_DEBUG_EXPR_IS_FROM (tmp) = 1;
170 }
171 DECL_ARTIFICIAL (tmp) = DECL_ARTIFICIAL (t);
172 DECL_IGNORED_P (tmp) = DECL_IGNORED_P (t);
173 add_referenced_tmp_var (tmp);
174
175 /* add_referenced_tmp_var will create the annotation and set up some
176 of the flags in the annotation. However, some flags we need to
177 inherit from our original variable. */
178 var_ann (tmp)->type_mem_tag = var_ann (t)->type_mem_tag;
179 if (is_call_clobbered (t))
180 mark_call_clobbered (tmp);
181
182 return tmp;
183 }
184
185
186 /* This helper function fill insert a copy from a constant or variable SRC to
187 variable DEST on edge E. */
188
189 static void
190 insert_copy_on_edge (edge e, tree dest, tree src)
191 {
192 tree copy;
193
194 copy = build (MODIFY_EXPR, TREE_TYPE (dest), dest, src);
195 set_is_used (dest);
196
197 if (TREE_CODE (src) == ADDR_EXPR)
198 src = TREE_OPERAND (src, 0);
199 if (TREE_CODE (src) == VAR_DECL || TREE_CODE (src) == PARM_DECL)
200 set_is_used (src);
201
202 if (dump_file && (dump_flags & TDF_DETAILS))
203 {
204 fprintf (dump_file,
205 "Inserting a copy on edge BB%d->BB%d :",
206 e->src->index,
207 e->dest->index);
208 print_generic_expr (dump_file, copy, dump_flags);
209 fprintf (dump_file, "\n");
210 }
211
212 bsi_insert_on_edge (e, copy);
213 }
214
215
216 /* Create an elimination graph with SIZE nodes and associated data
217 structures. */
218
219 static elim_graph
220 new_elim_graph (int size)
221 {
222 elim_graph g = (elim_graph) xmalloc (sizeof (struct _elim_graph));
223
224 g->nodes = VEC_alloc (tree, heap, 30);
225 g->const_copies = VEC_alloc (tree, heap, 20);
226 g->edge_list = VEC_alloc (int, heap, 20);
227 VARRAY_INT_INIT (g->stack, 30, " Elimination Stack");
228
229 g->visited = sbitmap_alloc (size);
230
231 return g;
232 }
233
234
235 /* Empty elimination graph G. */
236
237 static inline void
238 clear_elim_graph (elim_graph g)
239 {
240 VEC_truncate (tree, g->nodes, 0);
241 VEC_truncate (int, g->edge_list, 0);
242 }
243
244
245 /* Delete elimination graph G. */
246
247 static inline void
248 delete_elim_graph (elim_graph g)
249 {
250 sbitmap_free (g->visited);
251 VEC_free (int, heap, g->edge_list);
252 VEC_free (tree, heap, g->const_copies);
253 VEC_free (tree, heap, g->nodes);
254 free (g);
255 }
256
257
258 /* Return the number of nodes in graph G. */
259
260 static inline int
261 elim_graph_size (elim_graph g)
262 {
263 return VEC_length (tree, g->nodes);
264 }
265
266
267 /* Add NODE to graph G, if it doesn't exist already. */
268
269 static inline void
270 elim_graph_add_node (elim_graph g, tree node)
271 {
272 int x;
273 tree t;
274
275 for (x = 0; VEC_iterate (tree, g->nodes, x, t); x++)
276 if (t == node)
277 return;
278 VEC_safe_push (tree, heap, g->nodes, node);
279 }
280
281
282 /* Add the edge PRED->SUCC to graph G. */
283
284 static inline void
285 elim_graph_add_edge (elim_graph g, int pred, int succ)
286 {
287 VEC_safe_push (int, heap, g->edge_list, pred);
288 VEC_safe_push (int, heap, g->edge_list, succ);
289 }
290
291
292 /* Remove an edge from graph G for which NODE is the predecessor, and
293 return the successor node. -1 is returned if there is no such edge. */
294
295 static inline int
296 elim_graph_remove_succ_edge (elim_graph g, int node)
297 {
298 int y;
299 unsigned x;
300 for (x = 0; x < VEC_length (int, g->edge_list); x += 2)
301 if (VEC_index (int, g->edge_list, x) == node)
302 {
303 VEC_replace (int, g->edge_list, x, -1);
304 y = VEC_index (int, g->edge_list, x + 1);
305 VEC_replace (int, g->edge_list, x + 1, -1);
306 return y;
307 }
308 return -1;
309 }
310
311
312 /* Find all the nodes in GRAPH which are successors to NODE in the
313 edge list. VAR will hold the partition number found. CODE is the
314 code fragment executed for every node found. */
315
316 #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, CODE) \
317 do { \
318 unsigned x_; \
319 int y_; \
320 for (x_ = 0; x_ < VEC_length (int, (GRAPH)->edge_list); x_ += 2) \
321 { \
322 y_ = VEC_index (int, (GRAPH)->edge_list, x_); \
323 if (y_ != (NODE)) \
324 continue; \
325 (VAR) = VEC_index (int, (GRAPH)->edge_list, x_ + 1); \
326 CODE; \
327 } \
328 } while (0)
329
330
331 /* Find all the nodes which are predecessors of NODE in the edge list for
332 GRAPH. VAR will hold the partition number found. CODE is the
333 code fragment executed for every node found. */
334
335 #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, CODE) \
336 do { \
337 unsigned x_; \
338 int y_; \
339 for (x_ = 0; x_ < VEC_length (int, (GRAPH)->edge_list); x_ += 2) \
340 { \
341 y_ = VEC_index (int, (GRAPH)->edge_list, x_ + 1); \
342 if (y_ != (NODE)) \
343 continue; \
344 (VAR) = VEC_index (int, (GRAPH)->edge_list, x_); \
345 CODE; \
346 } \
347 } while (0)
348
349
350 /* Add T to elimination graph G. */
351
352 static inline void
353 eliminate_name (elim_graph g, tree T)
354 {
355 elim_graph_add_node (g, T);
356 }
357
358
359 /* Build elimination graph G for basic block BB on incoming PHI edge
360 G->e. */
361
362 static void
363 eliminate_build (elim_graph g, basic_block B)
364 {
365 tree phi;
366 tree T0, Ti;
367 int p0, pi;
368
369 clear_elim_graph (g);
370
371 for (phi = phi_nodes (B); phi; phi = PHI_CHAIN (phi))
372 {
373 T0 = var_to_partition_to_var (g->map, PHI_RESULT (phi));
374
375 /* Ignore results which are not in partitions. */
376 if (T0 == NULL_TREE)
377 continue;
378
379 Ti = PHI_ARG_DEF (phi, g->e->dest_idx);
380
381 /* If this argument is a constant, or a SSA_NAME which is being
382 left in SSA form, just queue a copy to be emitted on this
383 edge. */
384 if (!phi_ssa_name_p (Ti)
385 || (TREE_CODE (Ti) == SSA_NAME
386 && var_to_partition (g->map, Ti) == NO_PARTITION))
387 {
388 /* Save constant copies until all other copies have been emitted
389 on this edge. */
390 VEC_safe_push (tree, heap, g->const_copies, T0);
391 VEC_safe_push (tree, heap, g->const_copies, Ti);
392 }
393 else
394 {
395 Ti = var_to_partition_to_var (g->map, Ti);
396 if (T0 != Ti)
397 {
398 eliminate_name (g, T0);
399 eliminate_name (g, Ti);
400 p0 = var_to_partition (g->map, T0);
401 pi = var_to_partition (g->map, Ti);
402 elim_graph_add_edge (g, p0, pi);
403 }
404 }
405 }
406 }
407
408
409 /* Push successors of T onto the elimination stack for G. */
410
411 static void
412 elim_forward (elim_graph g, int T)
413 {
414 int S;
415 SET_BIT (g->visited, T);
416 FOR_EACH_ELIM_GRAPH_SUCC (g, T, S,
417 {
418 if (!TEST_BIT (g->visited, S))
419 elim_forward (g, S);
420 });
421 VARRAY_PUSH_INT (g->stack, T);
422 }
423
424
425 /* Return 1 if there unvisited predecessors of T in graph G. */
426
427 static int
428 elim_unvisited_predecessor (elim_graph g, int T)
429 {
430 int P;
431 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
432 {
433 if (!TEST_BIT (g->visited, P))
434 return 1;
435 });
436 return 0;
437 }
438
439 /* Process predecessors first, and insert a copy. */
440
441 static void
442 elim_backward (elim_graph g, int T)
443 {
444 int P;
445 SET_BIT (g->visited, T);
446 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
447 {
448 if (!TEST_BIT (g->visited, P))
449 {
450 elim_backward (g, P);
451 insert_copy_on_edge (g->e,
452 partition_to_var (g->map, P),
453 partition_to_var (g->map, T));
454 }
455 });
456 }
457
458 /* Insert required copies for T in graph G. Check for a strongly connected
459 region, and create a temporary to break the cycle if one is found. */
460
461 static void
462 elim_create (elim_graph g, int T)
463 {
464 tree U;
465 int P, S;
466
467 if (elim_unvisited_predecessor (g, T))
468 {
469 U = create_temp (partition_to_var (g->map, T));
470 insert_copy_on_edge (g->e, U, partition_to_var (g->map, T));
471 FOR_EACH_ELIM_GRAPH_PRED (g, T, P,
472 {
473 if (!TEST_BIT (g->visited, P))
474 {
475 elim_backward (g, P);
476 insert_copy_on_edge (g->e, partition_to_var (g->map, P), U);
477 }
478 });
479 }
480 else
481 {
482 S = elim_graph_remove_succ_edge (g, T);
483 if (S != -1)
484 {
485 SET_BIT (g->visited, T);
486 insert_copy_on_edge (g->e,
487 partition_to_var (g->map, T),
488 partition_to_var (g->map, S));
489 }
490 }
491
492 }
493
494 /* Eliminate all the phi nodes on edge E in graph G. */
495
496 static void
497 eliminate_phi (edge e, elim_graph g)
498 {
499 int x;
500 basic_block B = e->dest;
501
502 gcc_assert (VEC_length (tree, g->const_copies) == 0);
503
504 /* Abnormal edges already have everything coalesced. */
505 if (e->flags & EDGE_ABNORMAL)
506 return;
507
508 g->e = e;
509
510 eliminate_build (g, B);
511
512 if (elim_graph_size (g) != 0)
513 {
514 tree var;
515
516 sbitmap_zero (g->visited);
517 VARRAY_POP_ALL (g->stack);
518
519 for (x = 0; VEC_iterate (tree, g->nodes, x, var); x++)
520 {
521 int p = var_to_partition (g->map, var);
522 if (!TEST_BIT (g->visited, p))
523 elim_forward (g, p);
524 }
525
526 sbitmap_zero (g->visited);
527 while (VARRAY_ACTIVE_SIZE (g->stack) > 0)
528 {
529 x = VARRAY_TOP_INT (g->stack);
530 VARRAY_POP (g->stack);
531 if (!TEST_BIT (g->visited, x))
532 elim_create (g, x);
533 }
534 }
535
536 /* If there are any pending constant copies, issue them now. */
537 while (VEC_length (tree, g->const_copies) > 0)
538 {
539 tree src, dest;
540 src = VEC_pop (tree, g->const_copies);
541 dest = VEC_pop (tree, g->const_copies);
542 insert_copy_on_edge (e, dest, src);
543 }
544 }
545
546
547 /* Shortcut routine to print messages to file F of the form:
548 "STR1 EXPR1 STR2 EXPR2 STR3." */
549
550 static void
551 print_exprs (FILE *f, const char *str1, tree expr1, const char *str2,
552 tree expr2, const char *str3)
553 {
554 fprintf (f, "%s", str1);
555 print_generic_expr (f, expr1, TDF_SLIM);
556 fprintf (f, "%s", str2);
557 print_generic_expr (f, expr2, TDF_SLIM);
558 fprintf (f, "%s", str3);
559 }
560
561
562 /* Shortcut routine to print abnormal edge messages to file F of the form:
563 "STR1 EXPR1 STR2 EXPR2 across edge E. */
564
565 static void
566 print_exprs_edge (FILE *f, edge e, const char *str1, tree expr1,
567 const char *str2, tree expr2)
568 {
569 print_exprs (f, str1, expr1, str2, expr2, " across an abnormal edge");
570 fprintf (f, " from BB%d->BB%d\n", e->src->index,
571 e->dest->index);
572 }
573
574
575 /* Coalesce partitions in MAP which are live across abnormal edges in GRAPH.
576 RV is the root variable groupings of the partitions in MAP. Since code
577 cannot be inserted on these edges, failure to coalesce something across
578 an abnormal edge is an error. */
579
580 static void
581 coalesce_abnormal_edges (var_map map, conflict_graph graph, root_var_p rv)
582 {
583 basic_block bb;
584 edge e;
585 tree phi, var, tmp;
586 int x, y, z;
587 edge_iterator ei;
588
589 /* Code cannot be inserted on abnormal edges. Look for all abnormal
590 edges, and coalesce any PHI results with their arguments across
591 that edge. */
592
593 FOR_EACH_BB (bb)
594 FOR_EACH_EDGE (e, ei, bb->succs)
595 if (e->dest != EXIT_BLOCK_PTR && e->flags & EDGE_ABNORMAL)
596 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
597 {
598 /* Visit each PHI on the destination side of this abnormal
599 edge, and attempt to coalesce the argument with the result. */
600 var = PHI_RESULT (phi);
601 x = var_to_partition (map, var);
602
603 /* Ignore results which are not relevant. */
604 if (x == NO_PARTITION)
605 continue;
606
607 tmp = PHI_ARG_DEF (phi, e->dest_idx);
608 #ifdef ENABLE_CHECKING
609 if (!phi_ssa_name_p (tmp))
610 {
611 print_exprs_edge (stderr, e,
612 "\nConstant argument in PHI. Can't insert :",
613 var, " = ", tmp);
614 internal_error ("SSA corruption");
615 }
616 #else
617 gcc_assert (phi_ssa_name_p (tmp));
618 #endif
619 y = var_to_partition (map, tmp);
620 gcc_assert (x != NO_PARTITION);
621 gcc_assert (y != NO_PARTITION);
622 #ifdef ENABLE_CHECKING
623 if (root_var_find (rv, x) != root_var_find (rv, y))
624 {
625 print_exprs_edge (stderr, e, "\nDifferent root vars: ",
626 root_var (rv, root_var_find (rv, x)),
627 " and ",
628 root_var (rv, root_var_find (rv, y)));
629 internal_error ("SSA corruption");
630 }
631 #else
632 gcc_assert (root_var_find (rv, x) == root_var_find (rv, y));
633 #endif
634
635 if (x != y)
636 {
637 #ifdef ENABLE_CHECKING
638 if (conflict_graph_conflict_p (graph, x, y))
639 {
640 print_exprs_edge (stderr, e, "\n Conflict ",
641 partition_to_var (map, x),
642 " and ", partition_to_var (map, y));
643 internal_error ("SSA corruption");
644 }
645 #else
646 gcc_assert (!conflict_graph_conflict_p (graph, x, y));
647 #endif
648
649 /* Now map the partitions back to their real variables. */
650 var = partition_to_var (map, x);
651 tmp = partition_to_var (map, y);
652 if (dump_file && (dump_flags & TDF_DETAILS))
653 {
654 print_exprs_edge (dump_file, e,
655 "ABNORMAL: Coalescing ",
656 var, " and ", tmp);
657 }
658 z = var_union (map, var, tmp);
659 #ifdef ENABLE_CHECKING
660 if (z == NO_PARTITION)
661 {
662 print_exprs_edge (stderr, e, "\nUnable to coalesce",
663 partition_to_var (map, x), " and ",
664 partition_to_var (map, y));
665 internal_error ("SSA corruption");
666 }
667 #else
668 gcc_assert (z != NO_PARTITION);
669 #endif
670 gcc_assert (z == x || z == y);
671 if (z == x)
672 conflict_graph_merge_regs (graph, x, y);
673 else
674 conflict_graph_merge_regs (graph, y, x);
675 }
676 }
677 }
678
679
680 /* Reduce the number of live ranges in MAP. Live range information is
681 returned if FLAGS indicates that we are combining temporaries, otherwise
682 NULL is returned. The only partitions which are associated with actual
683 variables at this point are those which are forced to be coalesced for
684 various reason. (live on entry, live across abnormal edges, etc.). */
685
686 static tree_live_info_p
687 coalesce_ssa_name (var_map map, int flags)
688 {
689 unsigned num, x, i;
690 sbitmap live;
691 tree var, phi;
692 root_var_p rv;
693 tree_live_info_p liveinfo;
694 var_ann_t ann;
695 conflict_graph graph;
696 basic_block bb;
697 coalesce_list_p cl = NULL;
698
699 if (num_var_partitions (map) <= 1)
700 return NULL;
701
702 liveinfo = calculate_live_on_entry (map);
703 calculate_live_on_exit (liveinfo);
704 rv = root_var_init (map);
705
706 /* Remove single element variable from the list. */
707 root_var_compact (rv);
708
709 cl = create_coalesce_list (map);
710
711 /* Add all potential copies via PHI arguments to the list. */
712 FOR_EACH_BB (bb)
713 {
714 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
715 {
716 tree res = PHI_RESULT (phi);
717 int p = var_to_partition (map, res);
718 if (p == NO_PARTITION)
719 continue;
720 for (x = 0; x < (unsigned)PHI_NUM_ARGS (phi); x++)
721 {
722 tree arg = PHI_ARG_DEF (phi, x);
723 int p2;
724
725 if (TREE_CODE (arg) != SSA_NAME)
726 continue;
727 if (SSA_NAME_VAR (res) != SSA_NAME_VAR (arg))
728 continue;
729 p2 = var_to_partition (map, PHI_ARG_DEF (phi, x));
730 if (p2 != NO_PARTITION)
731 add_coalesce (cl, p, p2, 1);
732 }
733 }
734 }
735
736 /* Coalesce all the result decls together. */
737 var = NULL_TREE;
738 i = 0;
739 for (x = 0; x < num_var_partitions (map); x++)
740 {
741 tree p = partition_to_var (map, x);
742 if (TREE_CODE (SSA_NAME_VAR(p)) == RESULT_DECL)
743 {
744 if (var == NULL_TREE)
745 {
746 var = p;
747 i = x;
748 }
749 else
750 add_coalesce (cl, i, x, 1);
751 }
752 }
753
754 /* Build a conflict graph. */
755 graph = build_tree_conflict_graph (liveinfo, rv, cl);
756
757 if (cl)
758 {
759 if (dump_file && (dump_flags & TDF_DETAILS))
760 {
761 fprintf (dump_file, "Before sorting:\n");
762 dump_coalesce_list (dump_file, cl);
763 }
764
765 sort_coalesce_list (cl);
766
767 if (dump_file && (dump_flags & TDF_DETAILS))
768 {
769 fprintf (dump_file, "\nAfter sorting:\n");
770 dump_coalesce_list (dump_file, cl);
771 }
772 }
773
774 /* Put the single element variables back in. */
775 root_var_decompact (rv);
776
777 /* First, coalesce all live on entry variables to their root variable.
778 This will ensure the first use is coming from the correct location. */
779
780 live = sbitmap_alloc (num_var_partitions (map));
781 sbitmap_zero (live);
782
783 /* Set 'live' vector to indicate live on entry partitions. */
784 num = num_var_partitions (map);
785 for (x = 0 ; x < num; x++)
786 {
787 var = partition_to_var (map, x);
788 if (default_def (SSA_NAME_VAR (var)) == var)
789 SET_BIT (live, x);
790 }
791
792 if ((flags & SSANORM_COMBINE_TEMPS) == 0)
793 {
794 delete_tree_live_info (liveinfo);
795 liveinfo = NULL;
796 }
797
798 /* Assign root variable as partition representative for each live on entry
799 partition. */
800 EXECUTE_IF_SET_IN_SBITMAP (live, 0, x,
801 {
802 var = root_var (rv, root_var_find (rv, x));
803 ann = var_ann (var);
804 /* If these aren't already coalesced... */
805 if (partition_to_var (map, x) != var)
806 {
807 /* This root variable should have not already been assigned
808 to another partition which is not coalesced with this one. */
809 gcc_assert (!ann->out_of_ssa_tag);
810
811 if (dump_file && (dump_flags & TDF_DETAILS))
812 {
813 print_exprs (dump_file, "Must coalesce ",
814 partition_to_var (map, x),
815 " with the root variable ", var, ".\n");
816 }
817
818 change_partition_var (map, var, x);
819 }
820 });
821
822 sbitmap_free (live);
823
824 /* Coalesce partitions live across abnormal edges. */
825 coalesce_abnormal_edges (map, graph, rv);
826
827 if (dump_file && (dump_flags & TDF_DETAILS))
828 dump_var_map (dump_file, map);
829
830 /* Coalesce partitions. */
831 coalesce_tpa_members (rv, graph, map, cl,
832 ((dump_flags & TDF_DETAILS) ? dump_file
833 : NULL));
834
835 if (flags & SSANORM_COALESCE_PARTITIONS)
836 coalesce_tpa_members (rv, graph, map, NULL,
837 ((dump_flags & TDF_DETAILS) ? dump_file
838 : NULL));
839 if (cl)
840 delete_coalesce_list (cl);
841 root_var_delete (rv);
842 conflict_graph_delete (graph);
843
844 return liveinfo;
845 }
846
847
848 /* Take the ssa-name var_map MAP, and assign real variables to each
849 partition. */
850
851 static void
852 assign_vars (var_map map)
853 {
854 int x, i, num, rep;
855 tree t, var;
856 var_ann_t ann;
857 root_var_p rv;
858
859 rv = root_var_init (map);
860 if (!rv)
861 return;
862
863 /* Coalescing may already have forced some partitions to their root
864 variable. Find these and tag them. */
865
866 num = num_var_partitions (map);
867 for (x = 0; x < num; x++)
868 {
869 var = partition_to_var (map, x);
870 if (TREE_CODE (var) != SSA_NAME)
871 {
872 /* Coalescing will already have verified that more than one
873 partition doesn't have the same root variable. Simply marked
874 the variable as assigned. */
875 ann = var_ann (var);
876 ann->out_of_ssa_tag = 1;
877 if (dump_file && (dump_flags & TDF_DETAILS))
878 {
879 fprintf (dump_file, "partition %d has variable ", x);
880 print_generic_expr (dump_file, var, TDF_SLIM);
881 fprintf (dump_file, " assigned to it.\n");
882 }
883
884 }
885 }
886
887 num = root_var_num (rv);
888 for (x = 0; x < num; x++)
889 {
890 var = root_var (rv, x);
891 ann = var_ann (var);
892 for (i = root_var_first_partition (rv, x);
893 i != ROOT_VAR_NONE;
894 i = root_var_next_partition (rv, i))
895 {
896 t = partition_to_var (map, i);
897
898 if (t == var || TREE_CODE (t) != SSA_NAME)
899 continue;
900
901 rep = var_to_partition (map, t);
902
903 if (!ann->out_of_ssa_tag)
904 {
905 if (dump_file && (dump_flags & TDF_DETAILS))
906 print_exprs (dump_file, "", t, " --> ", var, "\n");
907 change_partition_var (map, var, rep);
908 continue;
909 }
910
911 if (dump_file && (dump_flags & TDF_DETAILS))
912 print_exprs (dump_file, "", t, " not coalesced with ", var,
913 "");
914
915 var = create_temp (t);
916 change_partition_var (map, var, rep);
917 ann = var_ann (var);
918
919 if (dump_file && (dump_flags & TDF_DETAILS))
920 {
921 fprintf (dump_file, " --> New temp: '");
922 print_generic_expr (dump_file, var, TDF_SLIM);
923 fprintf (dump_file, "'\n");
924 }
925 }
926 }
927
928 root_var_delete (rv);
929 }
930
931
932 /* Replace use operand P with whatever variable it has been rewritten to based
933 on the partitions in MAP. EXPR is an optional expression vector over SSA
934 versions which is used to replace P with an expression instead of a variable.
935 If the stmt is changed, return true. */
936
937 static inline bool
938 replace_use_variable (var_map map, use_operand_p p, tree *expr)
939 {
940 tree new_var;
941 tree var = USE_FROM_PTR (p);
942
943 /* Check if we are replacing this variable with an expression. */
944 if (expr)
945 {
946 int version = SSA_NAME_VERSION (var);
947 if (expr[version])
948 {
949 tree new_expr = TREE_OPERAND (expr[version], 1);
950 SET_USE (p, new_expr);
951 /* Clear the stmt's RHS, or GC might bite us. */
952 TREE_OPERAND (expr[version], 1) = NULL_TREE;
953 return true;
954 }
955 }
956
957 new_var = var_to_partition_to_var (map, var);
958 if (new_var)
959 {
960 SET_USE (p, new_var);
961 set_is_used (new_var);
962 return true;
963 }
964 return false;
965 }
966
967
968 /* Replace def operand DEF_P with whatever variable it has been rewritten to
969 based on the partitions in MAP. EXPR is an optional expression vector over
970 SSA versions which is used to replace DEF_P with an expression instead of a
971 variable. If the stmt is changed, return true. */
972
973 static inline bool
974 replace_def_variable (var_map map, def_operand_p def_p, tree *expr)
975 {
976 tree new_var;
977 tree var = DEF_FROM_PTR (def_p);
978
979 /* Check if we are replacing this variable with an expression. */
980 if (expr)
981 {
982 int version = SSA_NAME_VERSION (var);
983 if (expr[version])
984 {
985 tree new_expr = TREE_OPERAND (expr[version], 1);
986 SET_DEF (def_p, new_expr);
987 /* Clear the stmt's RHS, or GC might bite us. */
988 TREE_OPERAND (expr[version], 1) = NULL_TREE;
989 return true;
990 }
991 }
992
993 new_var = var_to_partition_to_var (map, var);
994 if (new_var)
995 {
996 SET_DEF (def_p, new_var);
997 set_is_used (new_var);
998 return true;
999 }
1000 return false;
1001 }
1002
1003
1004 /* Remove any PHI node which is a virtual PHI. */
1005
1006 static void
1007 eliminate_virtual_phis (void)
1008 {
1009 basic_block bb;
1010 tree phi, next;
1011
1012 FOR_EACH_BB (bb)
1013 {
1014 for (phi = phi_nodes (bb); phi; phi = next)
1015 {
1016 next = PHI_CHAIN (phi);
1017 if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi))))
1018 {
1019 #ifdef ENABLE_CHECKING
1020 int i;
1021 /* There should be no arguments of this PHI which are in
1022 the partition list, or we get incorrect results. */
1023 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1024 {
1025 tree arg = PHI_ARG_DEF (phi, i);
1026 if (TREE_CODE (arg) == SSA_NAME
1027 && is_gimple_reg (SSA_NAME_VAR (arg)))
1028 {
1029 fprintf (stderr, "Argument of PHI is not virtual (");
1030 print_generic_expr (stderr, arg, TDF_SLIM);
1031 fprintf (stderr, "), but the result is :");
1032 print_generic_stmt (stderr, phi, TDF_SLIM);
1033 internal_error ("SSA corruption");
1034 }
1035 }
1036 #endif
1037 remove_phi_node (phi, NULL_TREE);
1038 }
1039 }
1040 }
1041 }
1042
1043
1044 /* This routine will coalesce variables in MAP of the same type which do not
1045 interfere with each other. LIVEINFO is the live range info for variables
1046 of interest. This will both reduce the memory footprint of the stack, and
1047 allow us to coalesce together local copies of globals and scalarized
1048 component refs. */
1049
1050 static void
1051 coalesce_vars (var_map map, tree_live_info_p liveinfo)
1052 {
1053 basic_block bb;
1054 type_var_p tv;
1055 tree var;
1056 unsigned x, p, p2;
1057 coalesce_list_p cl;
1058 conflict_graph graph;
1059
1060 cl = create_coalesce_list (map);
1061
1062 /* Merge all the live on entry vectors for coalesced partitions. */
1063 for (x = 0; x < num_var_partitions (map); x++)
1064 {
1065 var = partition_to_var (map, x);
1066 p = var_to_partition (map, var);
1067 if (p != x)
1068 live_merge_and_clear (liveinfo, p, x);
1069 }
1070
1071 /* When PHI nodes are turned into copies, the result of each PHI node
1072 becomes live on entry to the block. Mark these now. */
1073 FOR_EACH_BB (bb)
1074 {
1075 tree phi, arg;
1076 unsigned p;
1077
1078 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1079 {
1080 p = var_to_partition (map, PHI_RESULT (phi));
1081
1082 /* Skip virtual PHI nodes. */
1083 if (p == (unsigned)NO_PARTITION)
1084 continue;
1085
1086 make_live_on_entry (liveinfo, bb, p);
1087
1088 /* Each argument is a potential copy operation. Add any arguments
1089 which are not coalesced to the result to the coalesce list. */
1090 for (x = 0; x < (unsigned)PHI_NUM_ARGS (phi); x++)
1091 {
1092 arg = PHI_ARG_DEF (phi, x);
1093 if (!phi_ssa_name_p (arg))
1094 continue;
1095 p2 = var_to_partition (map, arg);
1096 if (p2 == (unsigned)NO_PARTITION)
1097 continue;
1098 if (p != p2)
1099 add_coalesce (cl, p, p2, 1);
1100 }
1101 }
1102 }
1103
1104
1105 /* Re-calculate live on exit info. */
1106 calculate_live_on_exit (liveinfo);
1107
1108 if (dump_file && (dump_flags & TDF_DETAILS))
1109 {
1110 fprintf (dump_file, "Live range info for variable memory coalescing.\n");
1111 dump_live_info (dump_file, liveinfo, LIVEDUMP_ALL);
1112
1113 fprintf (dump_file, "Coalesce list from phi nodes:\n");
1114 dump_coalesce_list (dump_file, cl);
1115 }
1116
1117
1118 tv = type_var_init (map);
1119 if (dump_file)
1120 type_var_dump (dump_file, tv);
1121 type_var_compact (tv);
1122 if (dump_file)
1123 type_var_dump (dump_file, tv);
1124
1125 graph = build_tree_conflict_graph (liveinfo, tv, cl);
1126
1127 type_var_decompact (tv);
1128 if (dump_file && (dump_flags & TDF_DETAILS))
1129 {
1130 fprintf (dump_file, "type var list now looks like:n");
1131 type_var_dump (dump_file, tv);
1132
1133 fprintf (dump_file, "Coalesce list after conflict graph build:\n");
1134 dump_coalesce_list (dump_file, cl);
1135 }
1136
1137 sort_coalesce_list (cl);
1138 if (dump_file && (dump_flags & TDF_DETAILS))
1139 {
1140 fprintf (dump_file, "Coalesce list after sorting:\n");
1141 dump_coalesce_list (dump_file, cl);
1142 }
1143
1144 coalesce_tpa_members (tv, graph, map, cl,
1145 ((dump_flags & TDF_DETAILS) ? dump_file : NULL));
1146
1147 type_var_delete (tv);
1148 delete_coalesce_list (cl);
1149 }
1150
1151
1152 /* Temporary Expression Replacement (TER)
1153
1154 Replace SSA version variables during out-of-ssa with their defining
1155 expression if there is only one use of the variable.
1156
1157 A pass is made through the function, one block at a time. No cross block
1158 information is tracked.
1159
1160 Variables which only have one use, and whose defining stmt is considered
1161 a replaceable expression (see check_replaceable) are entered into
1162 consideration by adding a list of dependent partitions to the version_info
1163 vector for that ssa_name_version. This information comes from the partition
1164 mapping for each USE. At the same time, the partition_dep_list vector for
1165 these partitions have this version number entered into their lists.
1166
1167 When the use of a replaceable ssa_variable is encountered, the dependence
1168 list in version_info[] is moved to the "pending_dependence" list in case
1169 the current expression is also replaceable. (To be determined later in
1170 processing this stmt.) version_info[] for the version is then updated to
1171 point to the defining stmt and the 'replaceable' bit is set.
1172
1173 Any partition which is defined by a statement 'kills' any expression which
1174 is dependent on this partition. Every ssa version in the partitions'
1175 dependence list is removed from future consideration.
1176
1177 All virtual references are lumped together. Any expression which is
1178 dependent on any virtual variable (via a VUSE) has a dependence added
1179 to the special partition defined by VIRTUAL_PARTITION.
1180
1181 Whenever a V_MAY_DEF is seen, all expressions dependent this
1182 VIRTUAL_PARTITION are removed from consideration.
1183
1184 At the end of a basic block, all expression are removed from consideration
1185 in preparation for the next block.
1186
1187 The end result is a vector over SSA_NAME_VERSION which is passed back to
1188 rewrite_out_of_ssa. As the SSA variables are being rewritten, instead of
1189 replacing the SSA_NAME tree element with the partition it was assigned,
1190 it is replaced with the RHS of the defining expression. */
1191
1192
1193 /* Dependency list element. This can contain either a partition index or a
1194 version number, depending on which list it is in. */
1195
1196 typedef struct value_expr_d
1197 {
1198 int value;
1199 struct value_expr_d *next;
1200 } *value_expr_p;
1201
1202
1203 /* Temporary Expression Replacement (TER) table information. */
1204
1205 typedef struct temp_expr_table_d
1206 {
1207 var_map map;
1208 void **version_info;
1209 value_expr_p *partition_dep_list;
1210 bitmap replaceable;
1211 bool saw_replaceable;
1212 int virtual_partition;
1213 bitmap partition_in_use;
1214 value_expr_p free_list;
1215 value_expr_p pending_dependence;
1216 } *temp_expr_table_p;
1217
1218 /* Used to indicate a dependency on V_MAY_DEFs. */
1219 #define VIRTUAL_PARTITION(table) (table->virtual_partition)
1220
1221 static temp_expr_table_p new_temp_expr_table (var_map);
1222 static tree *free_temp_expr_table (temp_expr_table_p);
1223 static inline value_expr_p new_value_expr (temp_expr_table_p);
1224 static inline void free_value_expr (temp_expr_table_p, value_expr_p);
1225 static inline value_expr_p find_value_in_list (value_expr_p, int,
1226 value_expr_p *);
1227 static inline void add_value_to_list (temp_expr_table_p, value_expr_p *, int);
1228 static inline void add_info_to_list (temp_expr_table_p, value_expr_p *,
1229 value_expr_p);
1230 static value_expr_p remove_value_from_list (value_expr_p *, int);
1231 static void add_dependance (temp_expr_table_p, int, tree);
1232 static bool check_replaceable (temp_expr_table_p, tree);
1233 static void finish_expr (temp_expr_table_p, int, bool);
1234 static void mark_replaceable (temp_expr_table_p, tree);
1235 static inline void kill_expr (temp_expr_table_p, int, bool);
1236 static inline void kill_virtual_exprs (temp_expr_table_p, bool);
1237 static void find_replaceable_in_bb (temp_expr_table_p, basic_block);
1238 static tree *find_replaceable_exprs (var_map);
1239 static void dump_replaceable_exprs (FILE *, tree *);
1240
1241
1242 /* Create a new TER table for MAP. */
1243
1244 static temp_expr_table_p
1245 new_temp_expr_table (var_map map)
1246 {
1247 temp_expr_table_p t;
1248
1249 t = (temp_expr_table_p) xmalloc (sizeof (struct temp_expr_table_d));
1250 t->map = map;
1251
1252 t->version_info = xcalloc (num_ssa_names + 1, sizeof (void *));
1253 t->partition_dep_list = xcalloc (num_var_partitions (map) + 1,
1254 sizeof (value_expr_p));
1255
1256 t->replaceable = BITMAP_ALLOC (NULL);
1257 t->partition_in_use = BITMAP_ALLOC (NULL);
1258
1259 t->saw_replaceable = false;
1260 t->virtual_partition = num_var_partitions (map);
1261 t->free_list = NULL;
1262 t->pending_dependence = NULL;
1263
1264 return t;
1265 }
1266
1267
1268 /* Free TER table T. If there are valid replacements, return the expression
1269 vector. */
1270
1271 static tree *
1272 free_temp_expr_table (temp_expr_table_p t)
1273 {
1274 value_expr_p p;
1275 tree *ret = NULL;
1276
1277 #ifdef ENABLE_CHECKING
1278 unsigned x;
1279 for (x = 0; x <= num_var_partitions (t->map); x++)
1280 gcc_assert (!t->partition_dep_list[x]);
1281 #endif
1282
1283 while ((p = t->free_list))
1284 {
1285 t->free_list = p->next;
1286 free (p);
1287 }
1288
1289 BITMAP_FREE (t->partition_in_use);
1290 BITMAP_FREE (t->replaceable);
1291
1292 free (t->partition_dep_list);
1293 if (t->saw_replaceable)
1294 ret = (tree *)t->version_info;
1295 else
1296 free (t->version_info);
1297
1298 free (t);
1299 return ret;
1300 }
1301
1302
1303 /* Allocate a new value list node. Take it from the free list in TABLE if
1304 possible. */
1305
1306 static inline value_expr_p
1307 new_value_expr (temp_expr_table_p table)
1308 {
1309 value_expr_p p;
1310 if (table->free_list)
1311 {
1312 p = table->free_list;
1313 table->free_list = p->next;
1314 }
1315 else
1316 p = (value_expr_p) xmalloc (sizeof (struct value_expr_d));
1317
1318 return p;
1319 }
1320
1321
1322 /* Add value list node P to the free list in TABLE. */
1323
1324 static inline void
1325 free_value_expr (temp_expr_table_p table, value_expr_p p)
1326 {
1327 p->next = table->free_list;
1328 table->free_list = p;
1329 }
1330
1331
1332 /* Find VALUE if it's in LIST. Return a pointer to the list object if found,
1333 else return NULL. If LAST_PTR is provided, it will point to the previous
1334 item upon return, or NULL if this is the first item in the list. */
1335
1336 static inline value_expr_p
1337 find_value_in_list (value_expr_p list, int value, value_expr_p *last_ptr)
1338 {
1339 value_expr_p curr;
1340 value_expr_p last = NULL;
1341
1342 for (curr = list; curr; last = curr, curr = curr->next)
1343 {
1344 if (curr->value == value)
1345 break;
1346 }
1347 if (last_ptr)
1348 *last_ptr = last;
1349 return curr;
1350 }
1351
1352
1353 /* Add VALUE to LIST, if it isn't already present. TAB is the expression
1354 table */
1355
1356 static inline void
1357 add_value_to_list (temp_expr_table_p tab, value_expr_p *list, int value)
1358 {
1359 value_expr_p info;
1360
1361 if (!find_value_in_list (*list, value, NULL))
1362 {
1363 info = new_value_expr (tab);
1364 info->value = value;
1365 info->next = *list;
1366 *list = info;
1367 }
1368 }
1369
1370
1371 /* Add value node INFO if it's value isn't already in LIST. Free INFO if
1372 it is already in the list. TAB is the expression table. */
1373
1374 static inline void
1375 add_info_to_list (temp_expr_table_p tab, value_expr_p *list, value_expr_p info)
1376 {
1377 if (find_value_in_list (*list, info->value, NULL))
1378 free_value_expr (tab, info);
1379 else
1380 {
1381 info->next = *list;
1382 *list = info;
1383 }
1384 }
1385
1386
1387 /* Look for VALUE in LIST. If found, remove it from the list and return it's
1388 pointer. */
1389
1390 static value_expr_p
1391 remove_value_from_list (value_expr_p *list, int value)
1392 {
1393 value_expr_p info, last;
1394
1395 info = find_value_in_list (*list, value, &last);
1396 if (!info)
1397 return NULL;
1398 if (!last)
1399 *list = info->next;
1400 else
1401 last->next = info->next;
1402
1403 return info;
1404 }
1405
1406
1407 /* Add a dependency between the def of ssa VERSION and VAR. If VAR is
1408 replaceable by an expression, add a dependence each of the elements of the
1409 expression. These are contained in the pending list. TAB is the
1410 expression table. */
1411
1412 static void
1413 add_dependance (temp_expr_table_p tab, int version, tree var)
1414 {
1415 int i, x;
1416 value_expr_p info;
1417
1418 i = SSA_NAME_VERSION (var);
1419 if (bitmap_bit_p (tab->replaceable, i))
1420 {
1421 /* This variable is being substituted, so use whatever dependences
1422 were queued up when we marked this as replaceable earlier. */
1423 while ((info = tab->pending_dependence))
1424 {
1425 tab->pending_dependence = info->next;
1426 /* Get the partition this variable was dependent on. Reuse this
1427 object to represent the current expression instead. */
1428 x = info->value;
1429 info->value = version;
1430 add_info_to_list (tab, &(tab->partition_dep_list[x]), info);
1431 add_value_to_list (tab,
1432 (value_expr_p *)&(tab->version_info[version]), x);
1433 bitmap_set_bit (tab->partition_in_use, x);
1434 }
1435 }
1436 else
1437 {
1438 i = var_to_partition (tab->map, var);
1439 gcc_assert (i != NO_PARTITION);
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);
1444 }
1445 }
1446
1447
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. */
1450
1451 static bool
1452 check_replaceable (temp_expr_table_p tab, tree stmt)
1453 {
1454 tree var, def;
1455 int version;
1456 var_map map = tab->map;
1457 ssa_op_iter iter;
1458 tree call_expr;
1459
1460 if (TREE_CODE (stmt) != MODIFY_EXPR)
1461 return false;
1462
1463 /* Punt if there is more than 1 def, or more than 1 use. */
1464 def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
1465 if (!def)
1466 return false;
1467
1468 if (version_ref_count (map, def) != 1)
1469 return false;
1470
1471 /* There must be no V_MAY_DEFS or V_MUST_DEFS. */
1472 if (!(ZERO_SSA_OPERANDS (stmt, (SSA_OP_VMAYDEF | SSA_OP_VMUSTDEF))))
1473 return false;
1474
1475 /* Float expressions must go through memory if float-store is on. */
1476 if (flag_float_store && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (stmt, 1))))
1477 return false;
1478
1479 /* Calls to functions with side-effects cannot be replaced. */
1480 if ((call_expr = get_call_expr_in (stmt)) != NULL_TREE)
1481 {
1482 int call_flags = call_expr_flags (call_expr);
1483 if (TREE_SIDE_EFFECTS (call_expr)
1484 && !(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
1485 return false;
1486 }
1487
1488 version = SSA_NAME_VERSION (def);
1489
1490 /* Add this expression to the dependency list for each use partition. */
1491 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
1492 {
1493 add_dependance (tab, version, var);
1494 }
1495
1496 /* If there are VUSES, add a dependence on virtual defs. */
1497 if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VUSE))
1498 {
1499 add_value_to_list (tab, (value_expr_p *)&(tab->version_info[version]),
1500 VIRTUAL_PARTITION (tab));
1501 add_value_to_list (tab,
1502 &(tab->partition_dep_list[VIRTUAL_PARTITION (tab)]),
1503 version);
1504 bitmap_set_bit (tab->partition_in_use, VIRTUAL_PARTITION (tab));
1505 }
1506
1507 return true;
1508 }
1509
1510
1511 /* This function will remove the expression for VERSION from replacement
1512 consideration.n table TAB If 'replace' is true, it is marked as
1513 replaceable, otherwise not. */
1514
1515 static void
1516 finish_expr (temp_expr_table_p tab, int version, bool replace)
1517 {
1518 value_expr_p info, tmp;
1519 int partition;
1520
1521 /* Remove this expression from its dependent lists. The partition dependence
1522 list is retained and transfered later to whomever uses this version. */
1523 for (info = (value_expr_p) tab->version_info[version]; info; info = tmp)
1524 {
1525 partition = info->value;
1526 gcc_assert (tab->partition_dep_list[partition]);
1527 tmp = remove_value_from_list (&(tab->partition_dep_list[partition]),
1528 version);
1529 gcc_assert (tmp);
1530 free_value_expr (tab, tmp);
1531 /* Only clear the bit when the dependency list is emptied via
1532 a replacement. Otherwise kill_expr will take care of it. */
1533 if (!(tab->partition_dep_list[partition]) && replace)
1534 bitmap_clear_bit (tab->partition_in_use, partition);
1535 tmp = info->next;
1536 if (!replace)
1537 free_value_expr (tab, info);
1538 }
1539
1540 if (replace)
1541 {
1542 tab->saw_replaceable = true;
1543 bitmap_set_bit (tab->replaceable, version);
1544 }
1545 else
1546 {
1547 gcc_assert (!bitmap_bit_p (tab->replaceable, version));
1548 tab->version_info[version] = NULL;
1549 }
1550 }
1551
1552
1553 /* Mark the expression associated with VAR as replaceable, and enter
1554 the defining stmt into the version_info table TAB. */
1555
1556 static void
1557 mark_replaceable (temp_expr_table_p tab, tree var)
1558 {
1559 value_expr_p info;
1560 int version = SSA_NAME_VERSION (var);
1561 finish_expr (tab, version, true);
1562
1563 /* Move the dependence list to the pending list. */
1564 if (tab->version_info[version])
1565 {
1566 info = (value_expr_p) tab->version_info[version];
1567 for ( ; info->next; info = info->next)
1568 continue;
1569 info->next = tab->pending_dependence;
1570 tab->pending_dependence = (value_expr_p)tab->version_info[version];
1571 }
1572
1573 tab->version_info[version] = SSA_NAME_DEF_STMT (var);
1574 }
1575
1576
1577 /* This function marks any expression in TAB which is dependent on PARTITION
1578 as NOT replaceable. CLEAR_BIT is used to determine whether partition_in_use
1579 should have its bit cleared. Since this routine can be called within an
1580 EXECUTE_IF_SET_IN_BITMAP, the bit can't always be cleared. */
1581
1582 static inline void
1583 kill_expr (temp_expr_table_p tab, int partition, bool clear_bit)
1584 {
1585 value_expr_p ptr;
1586
1587 /* Mark every active expr dependent on this var as not replaceable. */
1588 while ((ptr = tab->partition_dep_list[partition]) != NULL)
1589 finish_expr (tab, ptr->value, false);
1590
1591 if (clear_bit)
1592 bitmap_clear_bit (tab->partition_in_use, partition);
1593 }
1594
1595
1596 /* This function kills all expressions in TAB which are dependent on virtual
1597 DEFs. CLEAR_BIT determines whether partition_in_use gets cleared. */
1598
1599 static inline void
1600 kill_virtual_exprs (temp_expr_table_p tab, bool clear_bit)
1601 {
1602 kill_expr (tab, VIRTUAL_PARTITION (tab), clear_bit);
1603 }
1604
1605
1606 /* This function processes basic block BB, and looks for variables which can
1607 be replaced by their expressions. Results are stored in TAB. */
1608
1609 static void
1610 find_replaceable_in_bb (temp_expr_table_p tab, basic_block bb)
1611 {
1612 block_stmt_iterator bsi;
1613 tree stmt, def;
1614 stmt_ann_t ann;
1615 int partition;
1616 var_map map = tab->map;
1617 value_expr_p p;
1618 ssa_op_iter iter;
1619
1620 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1621 {
1622 stmt = bsi_stmt (bsi);
1623 ann = stmt_ann (stmt);
1624
1625 /* Determine if this stmt finishes an existing expression. */
1626 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_USE)
1627 {
1628 if (tab->version_info[SSA_NAME_VERSION (def)])
1629 {
1630 bool same_root_var = false;
1631 tree def2;
1632 ssa_op_iter iter2;
1633
1634 /* See if the root variables are the same. If they are, we
1635 do not want to do the replacement to avoid problems with
1636 code size, see PR tree-optimization/17549. */
1637 FOR_EACH_SSA_TREE_OPERAND (def2, stmt, iter2, SSA_OP_DEF)
1638 if (SSA_NAME_VAR (def) == SSA_NAME_VAR (def2))
1639 {
1640 same_root_var = true;
1641 break;
1642 }
1643
1644 /* Mark expression as replaceable unless stmt is volatile
1645 or DEF sets the same root variable as STMT. */
1646 if (!ann->has_volatile_ops && !same_root_var)
1647 mark_replaceable (tab, def);
1648 else
1649 finish_expr (tab, SSA_NAME_VERSION (def), false);
1650 }
1651 }
1652
1653 /* Next, see if this stmt kills off an active expression. */
1654 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
1655 {
1656 partition = var_to_partition (map, def);
1657 if (partition != NO_PARTITION && tab->partition_dep_list[partition])
1658 kill_expr (tab, partition, true);
1659 }
1660
1661 /* Now see if we are creating a new expression or not. */
1662 if (!ann->has_volatile_ops)
1663 check_replaceable (tab, stmt);
1664
1665 /* Free any unused dependency lists. */
1666 while ((p = tab->pending_dependence))
1667 {
1668 tab->pending_dependence = p->next;
1669 free_value_expr (tab, p);
1670 }
1671
1672 /* A V_{MAY,MUST}_DEF kills any expression using a virtual operand. */
1673 if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_DEFS))
1674 kill_virtual_exprs (tab, true);
1675 }
1676 }
1677
1678
1679 /* This function is the driver routine for replacement of temporary expressions
1680 in the SSA->normal phase, operating on MAP. If there are replaceable
1681 expressions, a table is returned which maps SSA versions to the
1682 expressions they should be replaced with. A NULL_TREE indicates no
1683 replacement should take place. If there are no replacements at all,
1684 NULL is returned by the function, otherwise an expression vector indexed
1685 by SSA_NAME version numbers. */
1686
1687 static tree *
1688 find_replaceable_exprs (var_map map)
1689 {
1690 basic_block bb;
1691 unsigned i;
1692 temp_expr_table_p table;
1693 tree *ret;
1694
1695 table = new_temp_expr_table (map);
1696 FOR_EACH_BB (bb)
1697 {
1698 bitmap_iterator bi;
1699
1700 find_replaceable_in_bb (table, bb);
1701 EXECUTE_IF_SET_IN_BITMAP ((table->partition_in_use), 0, i, bi)
1702 {
1703 kill_expr (table, i, false);
1704 }
1705 }
1706
1707 ret = free_temp_expr_table (table);
1708 return ret;
1709 }
1710
1711
1712 /* Dump TER expression table EXPR to file F. */
1713
1714 static void
1715 dump_replaceable_exprs (FILE *f, tree *expr)
1716 {
1717 tree stmt, var;
1718 int x;
1719 fprintf (f, "\nReplacing Expressions\n");
1720 for (x = 0; x < (int)num_ssa_names + 1; x++)
1721 if (expr[x])
1722 {
1723 stmt = expr[x];
1724 var = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
1725 gcc_assert (var != NULL_TREE);
1726 print_generic_expr (f, var, TDF_SLIM);
1727 fprintf (f, " replace with --> ");
1728 print_generic_expr (f, TREE_OPERAND (stmt, 1), TDF_SLIM);
1729 fprintf (f, "\n");
1730 }
1731 fprintf (f, "\n");
1732 }
1733
1734
1735 /* Helper function for discover_nonconstant_array_refs.
1736 Look for ARRAY_REF nodes with non-constant indexes and mark them
1737 addressable. */
1738
1739 static tree
1740 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
1741 void *data ATTRIBUTE_UNUSED)
1742 {
1743 tree t = *tp;
1744
1745 if (IS_TYPE_OR_DECL_P (t))
1746 *walk_subtrees = 0;
1747 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1748 {
1749 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1750 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
1751 && (!TREE_OPERAND (t, 2)
1752 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
1753 || (TREE_CODE (t) == COMPONENT_REF
1754 && (!TREE_OPERAND (t,2)
1755 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
1756 || TREE_CODE (t) == BIT_FIELD_REF
1757 || TREE_CODE (t) == REALPART_EXPR
1758 || TREE_CODE (t) == IMAGPART_EXPR
1759 || TREE_CODE (t) == VIEW_CONVERT_EXPR
1760 || TREE_CODE (t) == NOP_EXPR
1761 || TREE_CODE (t) == CONVERT_EXPR)
1762 t = TREE_OPERAND (t, 0);
1763
1764 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1765 {
1766 t = get_base_address (t);
1767 if (t && DECL_P (t))
1768 TREE_ADDRESSABLE (t) = 1;
1769 }
1770
1771 *walk_subtrees = 0;
1772 }
1773
1774 return NULL_TREE;
1775 }
1776
1777
1778 /* RTL expansion is not able to compile array references with variable
1779 offsets for arrays stored in single register. Discover such
1780 expressions and mark variables as addressable to avoid this
1781 scenario. */
1782
1783 static void
1784 discover_nonconstant_array_refs (void)
1785 {
1786 basic_block bb;
1787 block_stmt_iterator bsi;
1788
1789 FOR_EACH_BB (bb)
1790 {
1791 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1792 walk_tree (bsi_stmt_ptr (bsi), discover_nonconstant_array_refs_r,
1793 NULL , NULL);
1794 }
1795 }
1796
1797
1798 /* This function will rewrite the current program using the variable mapping
1799 found in MAP. If the replacement vector VALUES is provided, any
1800 occurrences of partitions with non-null entries in the vector will be
1801 replaced with the expression in the vector instead of its mapped
1802 variable. */
1803
1804 static void
1805 rewrite_trees (var_map map, tree *values)
1806 {
1807 elim_graph g;
1808 basic_block bb;
1809 block_stmt_iterator si;
1810 edge e;
1811 tree phi;
1812 bool changed;
1813
1814 #ifdef ENABLE_CHECKING
1815 /* Search for PHIs where the destination has no partition, but one
1816 or more arguments has a partition. This should not happen and can
1817 create incorrect code. */
1818 FOR_EACH_BB (bb)
1819 {
1820 tree phi;
1821
1822 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1823 {
1824 tree T0 = var_to_partition_to_var (map, PHI_RESULT (phi));
1825
1826 if (T0 == NULL_TREE)
1827 {
1828 int i;
1829
1830 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1831 {
1832 tree arg = PHI_ARG_DEF (phi, i);
1833
1834 if (TREE_CODE (arg) == SSA_NAME
1835 && var_to_partition (map, arg) != NO_PARTITION)
1836 {
1837 fprintf (stderr, "Argument of PHI is in a partition :(");
1838 print_generic_expr (stderr, arg, TDF_SLIM);
1839 fprintf (stderr, "), but the result is not :");
1840 print_generic_stmt (stderr, phi, TDF_SLIM);
1841 internal_error ("SSA corruption");
1842 }
1843 }
1844 }
1845 }
1846 }
1847 #endif
1848
1849 /* Replace PHI nodes with any required copies. */
1850 g = new_elim_graph (map->num_partitions);
1851 g->map = map;
1852 FOR_EACH_BB (bb)
1853 {
1854 for (si = bsi_start (bb); !bsi_end_p (si); )
1855 {
1856 tree stmt = bsi_stmt (si);
1857 use_operand_p use_p, copy_use_p;
1858 def_operand_p def_p;
1859 bool remove = false, is_copy = false;
1860 int num_uses = 0;
1861 stmt_ann_t ann;
1862 ssa_op_iter iter;
1863
1864 ann = stmt_ann (stmt);
1865 changed = false;
1866
1867 if (TREE_CODE (stmt) == MODIFY_EXPR
1868 && (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME))
1869 is_copy = true;
1870
1871 copy_use_p = NULL_USE_OPERAND_P;
1872 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1873 {
1874 if (replace_use_variable (map, use_p, values))
1875 changed = true;
1876 copy_use_p = use_p;
1877 num_uses++;
1878 }
1879
1880 if (num_uses != 1)
1881 is_copy = false;
1882
1883 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
1884
1885 if (def_p != NULL)
1886 {
1887 /* Mark this stmt for removal if it is the list of replaceable
1888 expressions. */
1889 if (values && values[SSA_NAME_VERSION (DEF_FROM_PTR (def_p))])
1890 remove = true;
1891 else
1892 {
1893 if (replace_def_variable (map, def_p, NULL))
1894 changed = true;
1895 /* If both SSA_NAMEs coalesce to the same variable,
1896 mark the now redundant copy for removal. */
1897 if (is_copy)
1898 {
1899 gcc_assert (copy_use_p != NULL_USE_OPERAND_P);
1900 if (DEF_FROM_PTR (def_p) == USE_FROM_PTR (copy_use_p))
1901 remove = true;
1902 }
1903 }
1904 }
1905 else
1906 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_DEF)
1907 if (replace_def_variable (map, def_p, NULL))
1908 changed = true;
1909
1910 /* Remove any stmts marked for removal. */
1911 if (remove)
1912 bsi_remove (&si);
1913 else
1914 bsi_next (&si);
1915 }
1916
1917 phi = phi_nodes (bb);
1918 if (phi)
1919 {
1920 edge_iterator ei;
1921 FOR_EACH_EDGE (e, ei, bb->preds)
1922 eliminate_phi (e, g);
1923 }
1924 }
1925
1926 delete_elim_graph (g);
1927 }
1928
1929
1930 DEF_VEC_ALLOC_P(edge,heap);
1931
1932 /* These are the local work structures used to determine the best place to
1933 insert the copies that were placed on edges by the SSA->normal pass.. */
1934 static VEC(edge,heap) *edge_leader;
1935 static VEC(tree,heap) *stmt_list;
1936 static bitmap leader_has_match = NULL;
1937 static edge leader_match = NULL;
1938
1939
1940 /* Pass this function to make_forwarder_block so that all the edges with
1941 matching PENDING_STMT lists to 'curr_stmt_list' get redirected. */
1942 static bool
1943 same_stmt_list_p (edge e)
1944 {
1945 return (e->aux == (PTR) leader_match) ? true : false;
1946 }
1947
1948
1949 /* Return TRUE if S1 and S2 are equivalent copies. */
1950 static inline bool
1951 identical_copies_p (tree s1, tree s2)
1952 {
1953 #ifdef ENABLE_CHECKING
1954 gcc_assert (TREE_CODE (s1) == MODIFY_EXPR);
1955 gcc_assert (TREE_CODE (s2) == MODIFY_EXPR);
1956 gcc_assert (DECL_P (TREE_OPERAND (s1, 0)));
1957 gcc_assert (DECL_P (TREE_OPERAND (s2, 0)));
1958 #endif
1959
1960 if (TREE_OPERAND (s1, 0) != TREE_OPERAND (s2, 0))
1961 return false;
1962
1963 s1 = TREE_OPERAND (s1, 1);
1964 s2 = TREE_OPERAND (s2, 1);
1965
1966 if (s1 != s2)
1967 return false;
1968
1969 return true;
1970 }
1971
1972
1973 /* Compare the PENDING_STMT list for two edges, and return true if the lists
1974 contain the same sequence of copies. */
1975
1976 static inline bool
1977 identical_stmt_lists_p (edge e1, edge e2)
1978 {
1979 tree t1 = PENDING_STMT (e1);
1980 tree t2 = PENDING_STMT (e2);
1981 tree_stmt_iterator tsi1, tsi2;
1982
1983 gcc_assert (TREE_CODE (t1) == STATEMENT_LIST);
1984 gcc_assert (TREE_CODE (t2) == STATEMENT_LIST);
1985
1986 for (tsi1 = tsi_start (t1), tsi2 = tsi_start (t2);
1987 !tsi_end_p (tsi1) && !tsi_end_p (tsi2);
1988 tsi_next (&tsi1), tsi_next (&tsi2))
1989 {
1990 if (!identical_copies_p (tsi_stmt (tsi1), tsi_stmt (tsi2)))
1991 break;
1992 }
1993
1994 if (!tsi_end_p (tsi1) || ! tsi_end_p (tsi2))
1995 return false;
1996
1997 return true;
1998 }
1999
2000
2001 /* Allocate data structures used in analyze_edges_for_bb. */
2002
2003 static void
2004 init_analyze_edges_for_bb (void)
2005 {
2006 edge_leader = VEC_alloc (edge, heap, 25);
2007 stmt_list = VEC_alloc (tree, heap, 25);
2008 leader_has_match = BITMAP_ALLOC (NULL);
2009 }
2010
2011
2012 /* Free data structures used in analyze_edges_for_bb. */
2013
2014 static void
2015 fini_analyze_edges_for_bb (void)
2016 {
2017 VEC_free (edge, heap, edge_leader);
2018 VEC_free (tree, heap, stmt_list);
2019 BITMAP_FREE (leader_has_match);
2020 }
2021
2022
2023 /* Look at all the incoming edges to block BB, and decide where the best place
2024 to insert the stmts on each edge are, and perform those insertions. Output
2025 any debug information to DEBUG_FILE. */
2026
2027 static void
2028 analyze_edges_for_bb (basic_block bb, FILE *debug_file)
2029 {
2030 edge e;
2031 edge_iterator ei;
2032 int count;
2033 unsigned int x;
2034 bool have_opportunity;
2035 block_stmt_iterator bsi;
2036 tree stmt;
2037 edge single_edge = NULL;
2038 bool is_label;
2039 edge leader;
2040
2041 count = 0;
2042
2043 /* Blocks which contain at least one abnormal edge cannot use
2044 make_forwarder_block. Look for these blocks, and commit any PENDING_STMTs
2045 found on edges in these block. */
2046 have_opportunity = true;
2047 FOR_EACH_EDGE (e, ei, bb->preds)
2048 if (e->flags & EDGE_ABNORMAL)
2049 {
2050 have_opportunity = false;
2051 break;
2052 }
2053
2054 if (!have_opportunity)
2055 {
2056 FOR_EACH_EDGE (e, ei, bb->preds)
2057 if (PENDING_STMT (e))
2058 bsi_commit_one_edge_insert (e, NULL);
2059 return;
2060 }
2061 /* Find out how many edges there are with interesting pending stmts on them.
2062 Commit the stmts on edges we are not interested in. */
2063 FOR_EACH_EDGE (e, ei, bb->preds)
2064 {
2065 if (PENDING_STMT (e))
2066 {
2067 gcc_assert (!(e->flags & EDGE_ABNORMAL));
2068 if (e->flags & EDGE_FALLTHRU)
2069 {
2070 bsi = bsi_start (e->src);
2071 if (!bsi_end_p (bsi))
2072 {
2073 stmt = bsi_stmt (bsi);
2074 bsi_next (&bsi);
2075 gcc_assert (stmt != NULL_TREE);
2076 is_label = (TREE_CODE (stmt) == LABEL_EXPR);
2077 /* Punt if it has non-label stmts, or isn't local. */
2078 if (!is_label || DECL_NONLOCAL (TREE_OPERAND (stmt, 0))
2079 || !bsi_end_p (bsi))
2080 {
2081 bsi_commit_one_edge_insert (e, NULL);
2082 continue;
2083 }
2084 }
2085 }
2086 single_edge = e;
2087 count++;
2088 }
2089 }
2090
2091 /* If there aren't at least 2 edges, no sharing will happen. */
2092 if (count < 2)
2093 {
2094 if (single_edge)
2095 bsi_commit_one_edge_insert (single_edge, NULL);
2096 return;
2097 }
2098
2099 /* Ensure that we have empty worklists. */
2100 #ifdef ENABLE_CHECKING
2101 gcc_assert (VEC_length (edge, edge_leader) == 0);
2102 gcc_assert (VEC_length (tree, stmt_list) == 0);
2103 gcc_assert (bitmap_empty_p (leader_has_match));
2104 #endif
2105
2106 /* Find the "leader" block for each set of unique stmt lists. Preference is
2107 given to FALLTHRU blocks since they would need a GOTO to arrive at another
2108 block. The leader edge destination is the block which all the other edges
2109 with the same stmt list will be redirected to. */
2110 have_opportunity = false;
2111 FOR_EACH_EDGE (e, ei, bb->preds)
2112 {
2113 if (PENDING_STMT (e))
2114 {
2115 bool found = false;
2116
2117 /* Look for the same stmt list in edge leaders list. */
2118 for (x = 0; VEC_iterate (edge, edge_leader, x, leader); x++)
2119 {
2120 if (identical_stmt_lists_p (leader, e))
2121 {
2122 /* Give this edge the same stmt list pointer. */
2123 PENDING_STMT (e) = NULL;
2124 e->aux = leader;
2125 bitmap_set_bit (leader_has_match, x);
2126 have_opportunity = found = true;
2127 break;
2128 }
2129 }
2130
2131 /* If no similar stmt list, add this edge to the leader list. */
2132 if (!found)
2133 {
2134 VEC_safe_push (edge, heap, edge_leader, e);
2135 VEC_safe_push (tree, heap, stmt_list, PENDING_STMT (e));
2136 }
2137 }
2138 }
2139
2140 /* If there are no similar lists, just issue the stmts. */
2141 if (!have_opportunity)
2142 {
2143 for (x = 0; VEC_iterate (edge, edge_leader, x, leader); x++)
2144 bsi_commit_one_edge_insert (leader, NULL);
2145 VEC_truncate (edge, edge_leader, 0);
2146 VEC_truncate (tree, stmt_list, 0);
2147 bitmap_clear (leader_has_match);
2148 return;
2149 }
2150
2151
2152 if (debug_file)
2153 fprintf (debug_file, "\nOpportunities in BB %d for stmt/block reduction:\n",
2154 bb->index);
2155
2156
2157 /* For each common list, create a forwarding block and issue the stmt's
2158 in that block. */
2159 for (x = 0; VEC_iterate (edge, edge_leader, x, leader); x++)
2160 if (bitmap_bit_p (leader_has_match, x))
2161 {
2162 edge new_edge;
2163 block_stmt_iterator bsi;
2164 tree curr_stmt_list;
2165
2166 leader_match = leader;
2167
2168 /* The tree_* cfg manipulation routines use the PENDING_EDGE field
2169 for various PHI manipulations, so it gets cleared whhen calls are
2170 made to make_forwarder_block(). So make sure the edge is clear,
2171 and use the saved stmt list. */
2172 PENDING_STMT (leader) = NULL;
2173 leader->aux = leader;
2174 curr_stmt_list = VEC_index (tree, stmt_list, x);
2175
2176 new_edge = make_forwarder_block (leader->dest, same_stmt_list_p,
2177 NULL);
2178 bb = new_edge->dest;
2179 if (debug_file)
2180 {
2181 fprintf (debug_file, "Splitting BB %d for Common stmt list. ",
2182 leader->dest->index);
2183 fprintf (debug_file, "Original block is now BB%d.\n", bb->index);
2184 print_generic_stmt (debug_file, curr_stmt_list, TDF_VOPS);
2185 }
2186
2187 FOR_EACH_EDGE (e, ei, new_edge->src->preds)
2188 {
2189 e->aux = NULL;
2190 if (debug_file)
2191 fprintf (debug_file, " Edge (%d->%d) lands here.\n",
2192 e->src->index, e->dest->index);
2193 }
2194
2195 bsi = bsi_last (leader->dest);
2196 bsi_insert_after (&bsi, curr_stmt_list, BSI_NEW_STMT);
2197
2198 leader_match = NULL;
2199 /* We should never get a new block now. */
2200 }
2201 else
2202 {
2203 PENDING_STMT (leader) = VEC_index (tree, stmt_list, x);
2204 bsi_commit_one_edge_insert (leader, NULL);
2205 }
2206
2207
2208 /* Clear the working data structures. */
2209 VEC_truncate (edge, edge_leader, 0);
2210 VEC_truncate (tree, stmt_list, 0);
2211 bitmap_clear (leader_has_match);
2212 }
2213
2214
2215 /* This function will analyze the insertions which were performed on edges,
2216 and decide whether they should be left on that edge, or whether it is more
2217 efficient to emit some subset of them in a single block. All stmts are
2218 inserted somewhere, and if non-NULL, debug information is printed via
2219 DUMP_FILE. */
2220
2221 static void
2222 perform_edge_inserts (FILE *dump_file)
2223 {
2224 basic_block bb;
2225
2226 if (dump_file)
2227 fprintf(dump_file, "Analyzing Edge Insertions.\n");
2228
2229 /* analyze_edges_for_bb calls make_forwarder_block, which tries to
2230 incrementally update the dominator information. Since we don't
2231 need dominator information after this pass, go ahead and free the
2232 dominator information. */
2233 free_dominance_info (CDI_DOMINATORS);
2234 free_dominance_info (CDI_POST_DOMINATORS);
2235
2236 /* Allocate data structures used in analyze_edges_for_bb. */
2237 init_analyze_edges_for_bb ();
2238
2239 FOR_EACH_BB (bb)
2240 analyze_edges_for_bb (bb, dump_file);
2241
2242 analyze_edges_for_bb (EXIT_BLOCK_PTR, dump_file);
2243
2244 /* Free data structures used in analyze_edges_for_bb. */
2245 fini_analyze_edges_for_bb ();
2246
2247 #ifdef ENABLE_CHECKING
2248 {
2249 edge_iterator ei;
2250 edge e;
2251 FOR_EACH_BB (bb)
2252 {
2253 FOR_EACH_EDGE (e, ei, bb->preds)
2254 {
2255 if (PENDING_STMT (e))
2256 error (" Pending stmts not issued on PRED edge (%d, %d)\n",
2257 e->src->index, e->dest->index);
2258 }
2259 FOR_EACH_EDGE (e, ei, bb->succs)
2260 {
2261 if (PENDING_STMT (e))
2262 error (" Pending stmts not issued on SUCC edge (%d, %d)\n",
2263 e->src->index, e->dest->index);
2264 }
2265 }
2266 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2267 {
2268 if (PENDING_STMT (e))
2269 error (" Pending stmts not issued on ENTRY edge (%d, %d)\n",
2270 e->src->index, e->dest->index);
2271 }
2272 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
2273 {
2274 if (PENDING_STMT (e))
2275 error (" Pending stmts not issued on EXIT edge (%d, %d)\n",
2276 e->src->index, e->dest->index);
2277 }
2278 }
2279 #endif
2280 }
2281
2282
2283 /* Remove the variables specified in MAP from SSA form. Any debug information
2284 is sent to DUMP. FLAGS indicate what options should be used. */
2285
2286 static void
2287 remove_ssa_form (FILE *dump, var_map map, int flags)
2288 {
2289 tree_live_info_p liveinfo;
2290 basic_block bb;
2291 tree phi, next;
2292 FILE *save;
2293 tree *values = NULL;
2294
2295 save = dump_file;
2296 dump_file = dump;
2297
2298 /* If we are not combining temps, don't calculate live ranges for variables
2299 with only one SSA version. */
2300 if ((flags & SSANORM_COMBINE_TEMPS) == 0)
2301 compact_var_map (map, VARMAP_NO_SINGLE_DEFS);
2302 else
2303 compact_var_map (map, VARMAP_NORMAL);
2304
2305 if (dump_file && (dump_flags & TDF_DETAILS))
2306 dump_var_map (dump_file, map);
2307
2308 liveinfo = coalesce_ssa_name (map, flags);
2309
2310 /* Make sure even single occurrence variables are in the list now. */
2311 if ((flags & SSANORM_COMBINE_TEMPS) == 0)
2312 compact_var_map (map, VARMAP_NORMAL);
2313
2314 if (dump_file && (dump_flags & TDF_DETAILS))
2315 {
2316 fprintf (dump_file, "After Coalescing:\n");
2317 dump_var_map (dump_file, map);
2318 }
2319
2320 if (flags & SSANORM_PERFORM_TER)
2321 {
2322 values = find_replaceable_exprs (map);
2323 if (values && dump_file && (dump_flags & TDF_DETAILS))
2324 dump_replaceable_exprs (dump_file, values);
2325 }
2326
2327 /* Assign real variables to the partitions now. */
2328 assign_vars (map);
2329
2330 if (dump_file && (dump_flags & TDF_DETAILS))
2331 {
2332 fprintf (dump_file, "After Root variable replacement:\n");
2333 dump_var_map (dump_file, map);
2334 }
2335
2336 if ((flags & SSANORM_COMBINE_TEMPS) && liveinfo)
2337 {
2338 coalesce_vars (map, liveinfo);
2339 if (dump_file && (dump_flags & TDF_DETAILS))
2340 {
2341 fprintf (dump_file, "After variable memory coalescing:\n");
2342 dump_var_map (dump_file, map);
2343 }
2344 }
2345
2346 if (liveinfo)
2347 delete_tree_live_info (liveinfo);
2348
2349 rewrite_trees (map, values);
2350
2351 if (values)
2352 free (values);
2353
2354 /* Remove phi nodes which have been translated back to real variables. */
2355 FOR_EACH_BB (bb)
2356 {
2357 for (phi = phi_nodes (bb); phi; phi = next)
2358 {
2359 next = PHI_CHAIN (phi);
2360 remove_phi_node (phi, NULL_TREE);
2361 }
2362 }
2363
2364 /* we no longer maintain the SSA operand cache at this point. */
2365 fini_ssa_operands ();
2366
2367 /* If any copies were inserted on edges, analyze and insert them now. */
2368 perform_edge_inserts (dump_file);
2369
2370 dump_file = save;
2371 }
2372
2373 /* Search every PHI node for arguments associated with backedges which
2374 we can trivially determine will need a copy (the argument is either
2375 not an SSA_NAME or the argument has a different underlying variable
2376 than the PHI result).
2377
2378 Insert a copy from the PHI argument to a new destination at the
2379 end of the block with the backedge to the top of the loop. Update
2380 the PHI argument to reference this new destination. */
2381
2382 static void
2383 insert_backedge_copies (void)
2384 {
2385 basic_block bb;
2386
2387 FOR_EACH_BB (bb)
2388 {
2389 tree phi;
2390
2391 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2392 {
2393 tree result = PHI_RESULT (phi);
2394 tree result_var;
2395 int i;
2396
2397 if (!is_gimple_reg (result))
2398 continue;
2399
2400 result_var = SSA_NAME_VAR (result);
2401 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
2402 {
2403 tree arg = PHI_ARG_DEF (phi, i);
2404 edge e = PHI_ARG_EDGE (phi, i);
2405
2406 /* If the argument is not an SSA_NAME, then we will
2407 need a constant initialization. If the argument is
2408 an SSA_NAME with a different underlying variable and
2409 we are not combining temporaries, then we will
2410 need a copy statement. */
2411 if ((e->flags & EDGE_DFS_BACK)
2412 && (TREE_CODE (arg) != SSA_NAME
2413 || (!flag_tree_combine_temps
2414 && SSA_NAME_VAR (arg) != result_var)))
2415 {
2416 tree stmt, name, last = NULL;
2417 block_stmt_iterator bsi;
2418
2419 bsi = bsi_last (PHI_ARG_EDGE (phi, i)->src);
2420 if (!bsi_end_p (bsi))
2421 last = bsi_stmt (bsi);
2422
2423 /* In theory the only way we ought to get back to the
2424 start of a loop should be with a COND_EXPR or GOTO_EXPR.
2425 However, better safe than sorry.
2426
2427 If the block ends with a control statement or
2428 something that might throw, then we have to
2429 insert this assignment before the last
2430 statement. Else insert it after the last statement. */
2431 if (last && stmt_ends_bb_p (last))
2432 {
2433 /* If the last statement in the block is the definition
2434 site of the PHI argument, then we can't insert
2435 anything after it. */
2436 if (TREE_CODE (arg) == SSA_NAME
2437 && SSA_NAME_DEF_STMT (arg) == last)
2438 continue;
2439 }
2440
2441 /* Create a new instance of the underlying
2442 variable of the PHI result. */
2443 stmt = build (MODIFY_EXPR, TREE_TYPE (result_var),
2444 NULL, PHI_ARG_DEF (phi, i));
2445 name = make_ssa_name (result_var, stmt);
2446 TREE_OPERAND (stmt, 0) = name;
2447
2448 /* Insert the new statement into the block and update
2449 the PHI node. */
2450 if (last && stmt_ends_bb_p (last))
2451 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
2452 else
2453 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
2454 SET_PHI_ARG_DEF (phi, i, name);
2455 }
2456 }
2457 }
2458 }
2459 }
2460
2461 /* Take the current function out of SSA form, as described in
2462 R. Morgan, ``Building an Optimizing Compiler'',
2463 Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
2464
2465 static void
2466 rewrite_out_of_ssa (void)
2467 {
2468 var_map map;
2469 int var_flags = 0;
2470 int ssa_flags = 0;
2471
2472 /* If elimination of a PHI requires inserting a copy on a backedge,
2473 then we will have to split the backedge which has numerous
2474 undesirable performance effects.
2475
2476 A significant number of such cases can be handled here by inserting
2477 copies into the loop itself. */
2478 insert_backedge_copies ();
2479
2480 if (!flag_tree_live_range_split)
2481 ssa_flags |= SSANORM_COALESCE_PARTITIONS;
2482
2483 eliminate_virtual_phis ();
2484
2485 if (dump_file && (dump_flags & TDF_DETAILS))
2486 dump_tree_cfg (dump_file, dump_flags & ~TDF_DETAILS);
2487
2488 /* We cannot allow unssa to un-gimplify trees before we instrument them. */
2489 if (flag_tree_ter && !flag_mudflap)
2490 var_flags = SSA_VAR_MAP_REF_COUNT;
2491
2492 map = create_ssa_var_map (var_flags);
2493
2494 if (flag_tree_combine_temps)
2495 ssa_flags |= SSANORM_COMBINE_TEMPS;
2496 if (flag_tree_ter && !flag_mudflap)
2497 ssa_flags |= SSANORM_PERFORM_TER;
2498
2499 remove_ssa_form (dump_file, map, ssa_flags);
2500
2501 if (dump_file && (dump_flags & TDF_DETAILS))
2502 dump_tree_cfg (dump_file, dump_flags & ~TDF_DETAILS);
2503
2504 /* Flush out flow graph and SSA data. */
2505 delete_var_map (map);
2506
2507 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
2508 discover_nonconstant_array_refs ();
2509
2510 in_ssa_p = false;
2511 }
2512
2513
2514 /* Define the parameters of the out of SSA pass. */
2515
2516 struct tree_opt_pass pass_del_ssa =
2517 {
2518 "optimized", /* name */
2519 NULL, /* gate */
2520 rewrite_out_of_ssa, /* execute */
2521 NULL, /* sub */
2522 NULL, /* next */
2523 0, /* static_pass_number */
2524 TV_TREE_SSA_TO_NORMAL, /* tv_id */
2525 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
2526 0, /* properties_provided */
2527 /* ??? If TER is enabled, we also kill gimple. */
2528 PROP_ssa, /* properties_destroyed */
2529 TODO_verify_ssa | TODO_verify_flow
2530 | TODO_verify_stmts, /* todo_flags_start */
2531 TODO_dump_func | TODO_ggc_collect, /* todo_flags_finish */
2532 0 /* letter */
2533 };