bitmap.h (bitmap_ior_and_into): New.
[official-gcc.git] / gcc / tree-ssa-copy.c
blobd919681fb1abbbccee39acde8c928971c46059fd
1 /* Copy propagation and SSA_NAME replacement support routines.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "ggc.h"
29 #include "basic-block.h"
30 #include "output.h"
31 #include "expr.h"
32 #include "function.h"
33 #include "diagnostic.h"
34 #include "timevar.h"
35 #include "tree-dump.h"
36 #include "tree-flow.h"
37 #include "tree-pass.h"
38 #include "tree-ssa-propagate.h"
39 #include "langhooks.h"
40 #include "cfgloop.h"
42 /* This file implements the copy propagation pass and provides a
43 handful of interfaces for performing const/copy propagation and
44 simple expression replacement which keep variable annotations
45 up-to-date.
47 We require that for any copy operation where the RHS and LHS have
48 a non-null memory tag the memory tag be the same. It is OK
49 for one or both of the memory tags to be NULL.
51 We also require tracking if a variable is dereferenced in a load or
52 store operation.
54 We enforce these requirements by having all copy propagation and
55 replacements of one SSA_NAME with a different SSA_NAME to use the
56 APIs defined in this file. */
58 /* Return true if we may propagate ORIG into DEST, false otherwise. */
60 bool
61 may_propagate_copy (tree dest, tree orig)
63 tree type_d = TREE_TYPE (dest);
64 tree type_o = TREE_TYPE (orig);
66 /* If ORIG flows in from an abnormal edge, it cannot be propagated. */
67 if (TREE_CODE (orig) == SSA_NAME
68 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
69 return false;
71 /* If DEST is an SSA_NAME that flows from an abnormal edge, then it
72 cannot be replaced. */
73 if (TREE_CODE (dest) == SSA_NAME
74 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest))
75 return false;
77 /* Do not copy between types for which we *do* need a conversion. */
78 if (!useless_type_conversion_p (type_d, type_o))
79 return false;
81 /* Propagating virtual operands is always ok. */
82 if (TREE_CODE (dest) == SSA_NAME && !is_gimple_reg (dest))
84 /* But only between virtual operands. */
85 gcc_assert (TREE_CODE (orig) == SSA_NAME && !is_gimple_reg (orig));
87 return true;
90 /* Anything else is OK. */
91 return true;
94 /* Like may_propagate_copy, but use as the destination expression
95 the principal expression (typically, the RHS) contained in
96 statement DEST. This is more efficient when working with the
97 gimple tuples representation. */
99 bool
100 may_propagate_copy_into_stmt (gimple dest, tree orig)
102 tree type_d;
103 tree type_o;
105 /* If the statement is a switch or a single-rhs assignment,
106 then the expression to be replaced by the propagation may
107 be an SSA_NAME. Fortunately, there is an explicit tree
108 for the expression, so we delegate to may_propagate_copy. */
110 if (gimple_assign_single_p (dest))
111 return may_propagate_copy (gimple_assign_rhs1 (dest), orig);
112 else if (gimple_code (dest) == GIMPLE_SWITCH)
113 return may_propagate_copy (gimple_switch_index (dest), orig);
115 /* In other cases, the expression is not materialized, so there
116 is no destination to pass to may_propagate_copy. On the other
117 hand, the expression cannot be an SSA_NAME, so the analysis
118 is much simpler. */
120 if (TREE_CODE (orig) == SSA_NAME
121 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
122 return false;
124 if (is_gimple_assign (dest))
125 type_d = TREE_TYPE (gimple_assign_lhs (dest));
126 else if (gimple_code (dest) == GIMPLE_COND)
127 type_d = boolean_type_node;
128 else if (is_gimple_call (dest)
129 && gimple_call_lhs (dest) != NULL_TREE)
130 type_d = TREE_TYPE (gimple_call_lhs (dest));
131 else
132 gcc_unreachable ();
134 type_o = TREE_TYPE (orig);
136 if (!useless_type_conversion_p (type_d, type_o))
137 return false;
139 return true;
142 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
144 bool
145 may_propagate_copy_into_asm (tree dest)
147 /* Hard register operands of asms are special. Do not bypass. */
148 return !(TREE_CODE (dest) == SSA_NAME
149 && TREE_CODE (SSA_NAME_VAR (dest)) == VAR_DECL
150 && DECL_HARD_REGISTER (SSA_NAME_VAR (dest)));
154 /* Common code for propagate_value and replace_exp.
156 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
157 replacement is done to propagate a value or not. */
159 static void
160 replace_exp_1 (use_operand_p op_p, tree val,
161 bool for_propagation ATTRIBUTE_UNUSED)
163 #if defined ENABLE_CHECKING
164 tree op = USE_FROM_PTR (op_p);
165 gcc_assert (!(for_propagation
166 && TREE_CODE (op) == SSA_NAME
167 && TREE_CODE (val) == SSA_NAME
168 && !may_propagate_copy (op, val)));
169 #endif
171 if (TREE_CODE (val) == SSA_NAME)
172 SET_USE (op_p, val);
173 else
174 SET_USE (op_p, unsave_expr_now (val));
178 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
179 into the operand pointed to by OP_P.
181 Use this version for const/copy propagation as it will perform additional
182 checks to ensure validity of the const/copy propagation. */
184 void
185 propagate_value (use_operand_p op_p, tree val)
187 replace_exp_1 (op_p, val, true);
190 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
192 Use this version when not const/copy propagating values. For example,
193 PRE uses this version when building expressions as they would appear
194 in specific blocks taking into account actions of PHI nodes. */
196 void
197 replace_exp (use_operand_p op_p, tree val)
199 replace_exp_1 (op_p, val, false);
203 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
204 into the tree pointed to by OP_P.
206 Use this version for const/copy propagation when SSA operands are not
207 available. It will perform the additional checks to ensure validity of
208 the const/copy propagation, but will not update any operand information.
209 Be sure to mark the stmt as modified. */
211 void
212 propagate_tree_value (tree *op_p, tree val)
214 #if defined ENABLE_CHECKING
215 gcc_assert (!(TREE_CODE (val) == SSA_NAME
216 && *op_p
217 && TREE_CODE (*op_p) == SSA_NAME
218 && !may_propagate_copy (*op_p, val)));
219 #endif
221 if (TREE_CODE (val) == SSA_NAME)
222 *op_p = val;
223 else
224 *op_p = unsave_expr_now (val);
228 /* Like propagate_tree_value, but use as the operand to replace
229 the principal expression (typically, the RHS) contained in the
230 statement referenced by iterator GSI. Note that it is not
231 always possible to update the statement in-place, so a new
232 statement may be created to replace the original. */
234 void
235 propagate_tree_value_into_stmt (gimple_stmt_iterator *gsi, tree val)
237 gimple stmt = gsi_stmt (*gsi);
239 if (is_gimple_assign (stmt))
241 tree expr = NULL_TREE;
242 if (gimple_assign_single_p (stmt))
243 expr = gimple_assign_rhs1 (stmt);
244 propagate_tree_value (&expr, val);
245 gimple_assign_set_rhs_from_tree (gsi, expr);
246 stmt = gsi_stmt (*gsi);
248 else if (gimple_code (stmt) == GIMPLE_COND)
250 tree lhs = NULL_TREE;
251 tree rhs = fold_convert (TREE_TYPE (val), integer_zero_node);
252 propagate_tree_value (&lhs, val);
253 gimple_cond_set_code (stmt, NE_EXPR);
254 gimple_cond_set_lhs (stmt, lhs);
255 gimple_cond_set_rhs (stmt, rhs);
257 else if (is_gimple_call (stmt)
258 && gimple_call_lhs (stmt) != NULL_TREE)
260 gimple new_stmt;
262 tree expr = NULL_TREE;
263 propagate_tree_value (&expr, val);
264 new_stmt = gimple_build_assign (gimple_call_lhs (stmt), expr);
265 move_ssa_defining_stmt_for_defs (new_stmt, stmt);
266 gsi_replace (gsi, new_stmt, false);
268 else if (gimple_code (stmt) == GIMPLE_SWITCH)
269 propagate_tree_value (gimple_switch_index_ptr (stmt), val);
270 else
271 gcc_unreachable ();
274 /*---------------------------------------------------------------------------
275 Copy propagation
276 ---------------------------------------------------------------------------*/
277 /* During propagation, we keep chains of variables that are copies of
278 one another. If variable X_i is a copy of X_j and X_j is a copy of
279 X_k, COPY_OF will contain:
281 COPY_OF[i].VALUE = X_j
282 COPY_OF[j].VALUE = X_k
283 COPY_OF[k].VALUE = X_k
285 After propagation, the copy-of value for each variable X_i is
286 converted into the final value by walking the copy-of chains and
287 updating COPY_OF[i].VALUE to be the last element of the chain. */
288 static prop_value_t *copy_of;
290 /* Used in set_copy_of_val to determine if the last link of a copy-of
291 chain has changed. */
292 static tree *cached_last_copy_of;
295 /* Return true if this statement may generate a useful copy. */
297 static bool
298 stmt_may_generate_copy (gimple stmt)
300 if (gimple_code (stmt) == GIMPLE_PHI)
301 return !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (stmt));
303 if (gimple_code (stmt) != GIMPLE_ASSIGN)
304 return false;
306 /* If the statement has volatile operands, it won't generate a
307 useful copy. */
308 if (gimple_has_volatile_ops (stmt))
309 return false;
311 /* Statements with loads and/or stores will never generate a useful copy. */
312 if (gimple_vuse (stmt))
313 return false;
315 /* Otherwise, the only statements that generate useful copies are
316 assignments whose RHS is just an SSA name that doesn't flow
317 through abnormal edges. */
318 return (gimple_assign_rhs_code (stmt) == SSA_NAME
319 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt)));
323 /* Return the copy-of value for VAR. */
325 static inline prop_value_t *
326 get_copy_of_val (tree var)
328 prop_value_t *val = &copy_of[SSA_NAME_VERSION (var)];
330 if (val->value == NULL_TREE
331 && !stmt_may_generate_copy (SSA_NAME_DEF_STMT (var)))
333 /* If the variable will never generate a useful copy relation,
334 make it its own copy. */
335 val->value = var;
338 return val;
342 /* Return last link in the copy-of chain for VAR. */
344 static tree
345 get_last_copy_of (tree var)
347 tree last;
348 int i;
350 /* Traverse COPY_OF starting at VAR until we get to the last
351 link in the chain. Since it is possible to have cycles in PHI
352 nodes, the copy-of chain may also contain cycles.
354 To avoid infinite loops and to avoid traversing lengthy copy-of
355 chains, we artificially limit the maximum number of chains we are
356 willing to traverse.
358 The value 5 was taken from a compiler and runtime library
359 bootstrap and a mixture of C and C++ code from various sources.
360 More than 82% of all copy-of chains were shorter than 5 links. */
361 #define LIMIT 5
363 last = var;
364 for (i = 0; i < LIMIT; i++)
366 tree copy = copy_of[SSA_NAME_VERSION (last)].value;
367 if (copy == NULL_TREE || copy == last)
368 break;
369 last = copy;
372 /* If we have reached the limit, then we are either in a copy-of
373 cycle or the copy-of chain is too long. In this case, just
374 return VAR so that it is not considered a copy of anything. */
375 return (i < LIMIT ? last : var);
379 /* Set FIRST to be the first variable in the copy-of chain for DEST.
380 If DEST's copy-of value or its copy-of chain has changed, return
381 true.
383 MEM_REF is the memory reference where FIRST is stored. This is
384 used when DEST is a non-register and we are copy propagating loads
385 and stores. */
387 static inline bool
388 set_copy_of_val (tree dest, tree first)
390 unsigned int dest_ver = SSA_NAME_VERSION (dest);
391 tree old_first, old_last, new_last;
393 /* Set FIRST to be the first link in COPY_OF[DEST]. If that
394 changed, return true. */
395 old_first = copy_of[dest_ver].value;
396 copy_of[dest_ver].value = first;
398 if (old_first != first)
399 return true;
401 /* If FIRST and OLD_FIRST are the same, we need to check whether the
402 copy-of chain starting at FIRST ends in a different variable. If
403 the copy-of chain starting at FIRST ends up in a different
404 variable than the last cached value we had for DEST, then return
405 true because DEST is now a copy of a different variable.
407 This test is necessary because even though the first link in the
408 copy-of chain may not have changed, if any of the variables in
409 the copy-of chain changed its final value, DEST will now be the
410 copy of a different variable, so we have to do another round of
411 propagation for everything that depends on DEST. */
412 old_last = cached_last_copy_of[dest_ver];
413 new_last = get_last_copy_of (dest);
414 cached_last_copy_of[dest_ver] = new_last;
416 return (old_last != new_last);
420 /* Dump the copy-of value for variable VAR to FILE. */
422 static void
423 dump_copy_of (FILE *file, tree var)
425 tree val;
426 sbitmap visited;
428 print_generic_expr (file, var, dump_flags);
430 if (TREE_CODE (var) != SSA_NAME)
431 return;
433 visited = sbitmap_alloc (num_ssa_names);
434 sbitmap_zero (visited);
435 SET_BIT (visited, SSA_NAME_VERSION (var));
437 fprintf (file, " copy-of chain: ");
439 val = var;
440 print_generic_expr (file, val, 0);
441 fprintf (file, " ");
442 while (copy_of[SSA_NAME_VERSION (val)].value)
444 fprintf (file, "-> ");
445 val = copy_of[SSA_NAME_VERSION (val)].value;
446 print_generic_expr (file, val, 0);
447 fprintf (file, " ");
448 if (TEST_BIT (visited, SSA_NAME_VERSION (val)))
449 break;
450 SET_BIT (visited, SSA_NAME_VERSION (val));
453 val = get_copy_of_val (var)->value;
454 if (val == NULL_TREE)
455 fprintf (file, "[UNDEFINED]");
456 else if (val != var)
457 fprintf (file, "[COPY]");
458 else
459 fprintf (file, "[NOT A COPY]");
461 sbitmap_free (visited);
465 /* Evaluate the RHS of STMT. If it produces a valid copy, set the LHS
466 value and store the LHS into *RESULT_P. If STMT generates more
467 than one name (i.e., STMT is an aliased store), it is enough to
468 store the first name in the VDEF list into *RESULT_P. After
469 all, the names generated will be VUSEd in the same statements. */
471 static enum ssa_prop_result
472 copy_prop_visit_assignment (gimple stmt, tree *result_p)
474 tree lhs, rhs;
475 prop_value_t *rhs_val;
477 lhs = gimple_assign_lhs (stmt);
478 rhs = gimple_assign_rhs1 (stmt);
481 gcc_assert (gimple_assign_rhs_code (stmt) == SSA_NAME);
483 rhs_val = get_copy_of_val (rhs);
485 if (TREE_CODE (lhs) == SSA_NAME)
487 /* Straight copy between two SSA names. First, make sure that
488 we can propagate the RHS into uses of LHS. */
489 if (!may_propagate_copy (lhs, rhs))
490 return SSA_PROP_VARYING;
492 /* Notice that in the case of assignments, we make the LHS be a
493 copy of RHS's value, not of RHS itself. This avoids keeping
494 unnecessary copy-of chains (assignments cannot be in a cycle
495 like PHI nodes), speeding up the propagation process.
496 This is different from what we do in copy_prop_visit_phi_node.
497 In those cases, we are interested in the copy-of chains. */
498 *result_p = lhs;
499 if (set_copy_of_val (*result_p, rhs_val->value))
500 return SSA_PROP_INTERESTING;
501 else
502 return SSA_PROP_NOT_INTERESTING;
505 return SSA_PROP_VARYING;
509 /* Visit the GIMPLE_COND STMT. Return SSA_PROP_INTERESTING
510 if it can determine which edge will be taken. Otherwise, return
511 SSA_PROP_VARYING. */
513 static enum ssa_prop_result
514 copy_prop_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
516 enum ssa_prop_result retval = SSA_PROP_VARYING;
518 tree op0 = gimple_cond_lhs (stmt);
519 tree op1 = gimple_cond_rhs (stmt);
521 /* The only conditionals that we may be able to compute statically
522 are predicates involving two SSA_NAMEs. */
523 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
525 op0 = get_last_copy_of (op0);
526 op1 = get_last_copy_of (op1);
528 /* See if we can determine the predicate's value. */
529 if (dump_file && (dump_flags & TDF_DETAILS))
531 fprintf (dump_file, "Trying to determine truth value of ");
532 fprintf (dump_file, "predicate ");
533 print_gimple_stmt (dump_file, stmt, 0, 0);
536 /* We can fold COND and get a useful result only when we have
537 the same SSA_NAME on both sides of a comparison operator. */
538 if (op0 == op1)
540 tree folded_cond = fold_binary (gimple_cond_code (stmt),
541 boolean_type_node, op0, op1);
542 if (folded_cond)
544 basic_block bb = gimple_bb (stmt);
545 *taken_edge_p = find_taken_edge (bb, folded_cond);
546 if (*taken_edge_p)
547 retval = SSA_PROP_INTERESTING;
552 if (dump_file && (dump_flags & TDF_DETAILS) && *taken_edge_p)
553 fprintf (dump_file, "\nConditional will always take edge %d->%d\n",
554 (*taken_edge_p)->src->index, (*taken_edge_p)->dest->index);
556 return retval;
560 /* Evaluate statement STMT. If the statement produces a new output
561 value, return SSA_PROP_INTERESTING and store the SSA_NAME holding
562 the new value in *RESULT_P.
564 If STMT is a conditional branch and we can determine its truth
565 value, set *TAKEN_EDGE_P accordingly.
567 If the new value produced by STMT is varying, return
568 SSA_PROP_VARYING. */
570 static enum ssa_prop_result
571 copy_prop_visit_stmt (gimple stmt, edge *taken_edge_p, tree *result_p)
573 enum ssa_prop_result retval;
575 if (dump_file && (dump_flags & TDF_DETAILS))
577 fprintf (dump_file, "\nVisiting statement:\n");
578 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
579 fprintf (dump_file, "\n");
582 if (gimple_assign_single_p (stmt)
583 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
584 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
586 /* If the statement is a copy assignment, evaluate its RHS to
587 see if the lattice value of its output has changed. */
588 retval = copy_prop_visit_assignment (stmt, result_p);
590 else if (gimple_code (stmt) == GIMPLE_COND)
592 /* See if we can determine which edge goes out of a conditional
593 jump. */
594 retval = copy_prop_visit_cond_stmt (stmt, taken_edge_p);
596 else
597 retval = SSA_PROP_VARYING;
599 if (retval == SSA_PROP_VARYING)
601 tree def;
602 ssa_op_iter i;
604 /* Any other kind of statement is not interesting for constant
605 propagation and, therefore, not worth simulating. */
606 if (dump_file && (dump_flags & TDF_DETAILS))
607 fprintf (dump_file, "No interesting values produced.\n");
609 /* The assignment is not a copy operation. Don't visit this
610 statement again and mark all the definitions in the statement
611 to be copies of nothing. */
612 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_ALL_DEFS)
613 set_copy_of_val (def, def);
616 return retval;
620 /* Visit PHI node PHI. If all the arguments produce the same value,
621 set it to be the value of the LHS of PHI. */
623 static enum ssa_prop_result
624 copy_prop_visit_phi_node (gimple phi)
626 enum ssa_prop_result retval;
627 unsigned i;
628 prop_value_t phi_val = { 0, NULL_TREE };
630 tree lhs = gimple_phi_result (phi);
632 if (dump_file && (dump_flags & TDF_DETAILS))
634 fprintf (dump_file, "\nVisiting PHI node: ");
635 print_gimple_stmt (dump_file, phi, 0, dump_flags);
636 fprintf (dump_file, "\n\n");
639 for (i = 0; i < gimple_phi_num_args (phi); i++)
641 prop_value_t *arg_val;
642 tree arg = gimple_phi_arg_def (phi, i);
643 edge e = gimple_phi_arg_edge (phi, i);
645 /* We don't care about values flowing through non-executable
646 edges. */
647 if (!(e->flags & EDGE_EXECUTABLE))
648 continue;
650 /* Constants in the argument list never generate a useful copy.
651 Similarly, names that flow through abnormal edges cannot be
652 used to derive copies. */
653 if (TREE_CODE (arg) != SSA_NAME || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg))
655 phi_val.value = lhs;
656 break;
659 /* Avoid copy propagation from an inner into an outer loop.
660 Otherwise, this may move loop variant variables outside of
661 their loops and prevent coalescing opportunities. If the
662 value was loop invariant, it will be hoisted by LICM and
663 exposed for copy propagation. Not a problem for virtual
664 operands though. */
665 if (is_gimple_reg (lhs)
666 && loop_depth_of_name (arg) > loop_depth_of_name (lhs))
668 phi_val.value = lhs;
669 break;
672 /* If the LHS appears in the argument list, ignore it. It is
673 irrelevant as a copy. */
674 if (arg == lhs || get_last_copy_of (arg) == lhs)
675 continue;
677 if (dump_file && (dump_flags & TDF_DETAILS))
679 fprintf (dump_file, "\tArgument #%d: ", i);
680 dump_copy_of (dump_file, arg);
681 fprintf (dump_file, "\n");
684 arg_val = get_copy_of_val (arg);
686 /* If the LHS didn't have a value yet, make it a copy of the
687 first argument we find. Notice that while we make the LHS be
688 a copy of the argument itself, we take the memory reference
689 from the argument's value so that we can compare it to the
690 memory reference of all the other arguments. */
691 if (phi_val.value == NULL_TREE)
693 phi_val.value = arg_val->value ? arg_val->value : arg;
694 continue;
697 /* If PHI_VAL and ARG don't have a common copy-of chain, then
698 this PHI node cannot be a copy operation. Also, if we are
699 copy propagating stores and these two arguments came from
700 different memory references, they cannot be considered
701 copies. */
702 if (get_last_copy_of (phi_val.value) != get_last_copy_of (arg))
704 phi_val.value = lhs;
705 break;
709 if (phi_val.value && may_propagate_copy (lhs, phi_val.value)
710 && set_copy_of_val (lhs, phi_val.value))
711 retval = (phi_val.value != lhs) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
712 else
713 retval = SSA_PROP_NOT_INTERESTING;
715 if (dump_file && (dump_flags & TDF_DETAILS))
717 fprintf (dump_file, "\nPHI node ");
718 dump_copy_of (dump_file, lhs);
719 fprintf (dump_file, "\nTelling the propagator to ");
720 if (retval == SSA_PROP_INTERESTING)
721 fprintf (dump_file, "add SSA edges out of this PHI and continue.");
722 else if (retval == SSA_PROP_VARYING)
723 fprintf (dump_file, "add SSA edges out of this PHI and never visit again.");
724 else
725 fprintf (dump_file, "do nothing with SSA edges and keep iterating.");
726 fprintf (dump_file, "\n\n");
729 return retval;
733 /* Initialize structures used for copy propagation. PHIS_ONLY is true
734 if we should only consider PHI nodes as generating copy propagation
735 opportunities. */
737 static void
738 init_copy_prop (void)
740 basic_block bb;
742 copy_of = XCNEWVEC (prop_value_t, num_ssa_names);
744 cached_last_copy_of = XCNEWVEC (tree, num_ssa_names);
746 FOR_EACH_BB (bb)
748 gimple_stmt_iterator si;
749 int depth = bb->loop_depth;
751 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
753 gimple stmt = gsi_stmt (si);
754 ssa_op_iter iter;
755 tree def;
757 /* The only statements that we care about are those that may
758 generate useful copies. We also need to mark conditional
759 jumps so that their outgoing edges are added to the work
760 lists of the propagator.
762 Avoid copy propagation from an inner into an outer loop.
763 Otherwise, this may move loop variant variables outside of
764 their loops and prevent coalescing opportunities. If the
765 value was loop invariant, it will be hoisted by LICM and
766 exposed for copy propagation. */
767 if (stmt_ends_bb_p (stmt))
768 prop_set_simulate_again (stmt, true);
769 else if (stmt_may_generate_copy (stmt)
770 /* Since we are iterating over the statements in
771 BB, not the phi nodes, STMT will always be an
772 assignment. */
773 && loop_depth_of_name (gimple_assign_rhs1 (stmt)) <= depth)
774 prop_set_simulate_again (stmt, true);
775 else
776 prop_set_simulate_again (stmt, false);
778 /* Mark all the outputs of this statement as not being
779 the copy of anything. */
780 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
781 if (!prop_simulate_again_p (stmt))
782 set_copy_of_val (def, def);
783 else
784 cached_last_copy_of[SSA_NAME_VERSION (def)] = def;
787 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
789 gimple phi = gsi_stmt (si);
790 tree def;
792 def = gimple_phi_result (phi);
793 if (!is_gimple_reg (def)
794 /* In loop-closed SSA form do not copy-propagate through
795 PHI nodes. Technically this is only needed for loop
796 exit PHIs, but this is difficult to query. */
797 || (current_loops
798 && gimple_phi_num_args (phi) == 1
799 && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
800 prop_set_simulate_again (phi, false);
801 else
802 prop_set_simulate_again (phi, true);
804 if (!prop_simulate_again_p (phi))
805 set_copy_of_val (def, def);
806 else
807 cached_last_copy_of[SSA_NAME_VERSION (def)] = def;
813 /* Deallocate memory used in copy propagation and do final
814 substitution. */
816 static void
817 fini_copy_prop (void)
819 size_t i;
820 prop_value_t *tmp;
822 /* Set the final copy-of value for each variable by traversing the
823 copy-of chains. */
824 tmp = XCNEWVEC (prop_value_t, num_ssa_names);
825 for (i = 1; i < num_ssa_names; i++)
827 tree var = ssa_name (i);
828 if (!var
829 || !copy_of[i].value
830 || copy_of[i].value == var)
831 continue;
833 tmp[i].value = get_last_copy_of (var);
835 /* In theory the points-to solution of all members of the
836 copy chain is their intersection. For now we do not bother
837 to compute this but only make sure we do not lose points-to
838 information completely by setting the points-to solution
839 of the representative to the first solution we find if
840 it doesn't have one already. */
841 if (tmp[i].value != var
842 && POINTER_TYPE_P (TREE_TYPE (var))
843 && SSA_NAME_PTR_INFO (var)
844 && !SSA_NAME_PTR_INFO (tmp[i].value))
845 duplicate_ssa_name_ptr_info (tmp[i].value, SSA_NAME_PTR_INFO (var));
848 substitute_and_fold (tmp, false);
850 free (cached_last_copy_of);
851 free (copy_of);
852 free (tmp);
856 /* Main entry point to the copy propagator.
858 PHIS_ONLY is true if we should only consider PHI nodes as generating
859 copy propagation opportunities.
861 The algorithm propagates the value COPY-OF using ssa_propagate. For
862 every variable X_i, COPY-OF(X_i) indicates which variable is X_i created
863 from. The following example shows how the algorithm proceeds at a
864 high level:
866 1 a_24 = x_1
867 2 a_2 = PHI <a_24, x_1>
868 3 a_5 = PHI <a_2>
869 4 x_1 = PHI <x_298, a_5, a_2>
871 The end result should be that a_2, a_5, a_24 and x_1 are a copy of
872 x_298. Propagation proceeds as follows.
874 Visit #1: a_24 is copy-of x_1. Value changed.
875 Visit #2: a_2 is copy-of x_1. Value changed.
876 Visit #3: a_5 is copy-of x_1. Value changed.
877 Visit #4: x_1 is copy-of x_298. Value changed.
878 Visit #1: a_24 is copy-of x_298. Value changed.
879 Visit #2: a_2 is copy-of x_298. Value changed.
880 Visit #3: a_5 is copy-of x_298. Value changed.
881 Visit #4: x_1 is copy-of x_298. Stable state reached.
883 When visiting PHI nodes, we only consider arguments that flow
884 through edges marked executable by the propagation engine. So,
885 when visiting statement #2 for the first time, we will only look at
886 the first argument (a_24) and optimistically assume that its value
887 is the copy of a_24 (x_1).
889 The problem with this approach is that it may fail to discover copy
890 relations in PHI cycles. Instead of propagating copy-of
891 values, we actually propagate copy-of chains. For instance:
893 A_3 = B_1;
894 C_9 = A_3;
895 D_4 = C_9;
896 X_i = D_4;
898 In this code fragment, COPY-OF (X_i) = { D_4, C_9, A_3, B_1 }.
899 Obviously, we are only really interested in the last value of the
900 chain, however the propagator needs to access the copy-of chain
901 when visiting PHI nodes.
903 To represent the copy-of chain, we use the array COPY_CHAINS, which
904 holds the first link in the copy-of chain for every variable.
905 If variable X_i is a copy of X_j, which in turn is a copy of X_k,
906 the array will contain:
908 COPY_CHAINS[i] = X_j
909 COPY_CHAINS[j] = X_k
910 COPY_CHAINS[k] = X_k
912 Keeping copy-of chains instead of copy-of values directly becomes
913 important when visiting PHI nodes. Suppose that we had the
914 following PHI cycle, such that x_52 is already considered a copy of
915 x_53:
917 1 x_54 = PHI <x_53, x_52>
918 2 x_53 = PHI <x_898, x_54>
920 Visit #1: x_54 is copy-of x_53 (because x_52 is copy-of x_53)
921 Visit #2: x_53 is copy-of x_898 (because x_54 is a copy of x_53,
922 so it is considered irrelevant
923 as a copy).
924 Visit #1: x_54 is copy-of nothing (x_53 is a copy-of x_898 and
925 x_52 is a copy of x_53, so
926 they don't match)
927 Visit #2: x_53 is copy-of nothing
929 This problem is avoided by keeping a chain of copies, instead of
930 the final copy-of value. Propagation will now only keep the first
931 element of a variable's copy-of chain. When visiting PHI nodes,
932 arguments are considered equal if their copy-of chains end in the
933 same variable. So, as long as their copy-of chains overlap, we
934 know that they will be a copy of the same variable, regardless of
935 which variable that may be).
937 Propagation would then proceed as follows (the notation a -> b
938 means that a is a copy-of b):
940 Visit #1: x_54 = PHI <x_53, x_52>
941 x_53 -> x_53
942 x_52 -> x_53
943 Result: x_54 -> x_53. Value changed. Add SSA edges.
945 Visit #1: x_53 = PHI <x_898, x_54>
946 x_898 -> x_898
947 x_54 -> x_53
948 Result: x_53 -> x_898. Value changed. Add SSA edges.
950 Visit #2: x_54 = PHI <x_53, x_52>
951 x_53 -> x_898
952 x_52 -> x_53 -> x_898
953 Result: x_54 -> x_898. Value changed. Add SSA edges.
955 Visit #2: x_53 = PHI <x_898, x_54>
956 x_898 -> x_898
957 x_54 -> x_898
958 Result: x_53 -> x_898. Value didn't change. Stable state
960 Once the propagator stabilizes, we end up with the desired result
961 x_53 and x_54 are both copies of x_898. */
963 static unsigned int
964 execute_copy_prop (void)
966 init_copy_prop ();
967 ssa_propagate (copy_prop_visit_stmt, copy_prop_visit_phi_node);
968 fini_copy_prop ();
969 return 0;
972 static bool
973 gate_copy_prop (void)
975 return flag_tree_copy_prop != 0;
978 struct gimple_opt_pass pass_copy_prop =
981 GIMPLE_PASS,
982 "copyprop", /* name */
983 gate_copy_prop, /* gate */
984 execute_copy_prop, /* execute */
985 NULL, /* sub */
986 NULL, /* next */
987 0, /* static_pass_number */
988 TV_TREE_COPY_PROP, /* tv_id */
989 PROP_ssa | PROP_cfg, /* properties_required */
990 0, /* properties_provided */
991 0, /* properties_destroyed */
992 0, /* todo_flags_start */
993 TODO_cleanup_cfg
994 | TODO_dump_func
995 | TODO_ggc_collect
996 | TODO_verify_ssa
997 | TODO_update_ssa /* todo_flags_finish */