New vectorizer messages; message format change.
[official-gcc.git] / gcc / tree-ssa-copy.c
blob9bc455c61a728760d9b17d4b90a41a3b81752393
1 /* Copy propagation and SSA_NAME replacement support routines.
2 Copyright (C) 2004-2013 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 "tm_p.h"
27 #include "basic-block.h"
28 #include "function.h"
29 #include "gimple-pretty-print.h"
30 #include "tree-flow.h"
31 #include "tree-pass.h"
32 #include "tree-ssa-propagate.h"
33 #include "langhooks.h"
34 #include "cfgloop.h"
35 #include "tree-scalar-evolution.h"
37 /* This file implements the copy propagation pass and provides a
38 handful of interfaces for performing const/copy propagation and
39 simple expression replacement which keep variable annotations
40 up-to-date.
42 We require that for any copy operation where the RHS and LHS have
43 a non-null memory tag the memory tag be the same. It is OK
44 for one or both of the memory tags to be NULL.
46 We also require tracking if a variable is dereferenced in a load or
47 store operation.
49 We enforce these requirements by having all copy propagation and
50 replacements of one SSA_NAME with a different SSA_NAME to use the
51 APIs defined in this file. */
53 /* Return true if we may propagate ORIG into DEST, false otherwise. */
55 bool
56 may_propagate_copy (tree dest, tree orig)
58 tree type_d = TREE_TYPE (dest);
59 tree type_o = TREE_TYPE (orig);
61 /* If ORIG flows in from an abnormal edge, it cannot be propagated. */
62 if (TREE_CODE (orig) == SSA_NAME
63 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig)
64 /* If it is the default definition and an automatic variable then
65 we can though and it is important that we do to avoid
66 uninitialized regular copies. */
67 && !(SSA_NAME_IS_DEFAULT_DEF (orig)
68 && (SSA_NAME_VAR (orig) == NULL_TREE
69 || TREE_CODE (SSA_NAME_VAR (orig)) == VAR_DECL)))
70 return false;
72 /* If DEST is an SSA_NAME that flows from an abnormal edge, then it
73 cannot be replaced. */
74 if (TREE_CODE (dest) == SSA_NAME
75 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest))
76 return false;
78 /* Do not copy between types for which we *do* need a conversion. */
79 if (!useless_type_conversion_p (type_d, type_o))
80 return false;
82 /* Generally propagating virtual operands is not ok as that may
83 create overlapping life-ranges. */
84 if (TREE_CODE (dest) == SSA_NAME && virtual_operand_p (dest))
85 return false;
87 /* Anything else is OK. */
88 return true;
91 /* Like may_propagate_copy, but use as the destination expression
92 the principal expression (typically, the RHS) contained in
93 statement DEST. This is more efficient when working with the
94 gimple tuples representation. */
96 bool
97 may_propagate_copy_into_stmt (gimple dest, tree orig)
99 tree type_d;
100 tree type_o;
102 /* If the statement is a switch or a single-rhs assignment,
103 then the expression to be replaced by the propagation may
104 be an SSA_NAME. Fortunately, there is an explicit tree
105 for the expression, so we delegate to may_propagate_copy. */
107 if (gimple_assign_single_p (dest))
108 return may_propagate_copy (gimple_assign_rhs1 (dest), orig);
109 else if (gimple_code (dest) == GIMPLE_SWITCH)
110 return may_propagate_copy (gimple_switch_index (dest), orig);
112 /* In other cases, the expression is not materialized, so there
113 is no destination to pass to may_propagate_copy. On the other
114 hand, the expression cannot be an SSA_NAME, so the analysis
115 is much simpler. */
117 if (TREE_CODE (orig) == SSA_NAME
118 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
119 return false;
121 if (is_gimple_assign (dest))
122 type_d = TREE_TYPE (gimple_assign_lhs (dest));
123 else if (gimple_code (dest) == GIMPLE_COND)
124 type_d = boolean_type_node;
125 else if (is_gimple_call (dest)
126 && gimple_call_lhs (dest) != NULL_TREE)
127 type_d = TREE_TYPE (gimple_call_lhs (dest));
128 else
129 gcc_unreachable ();
131 type_o = TREE_TYPE (orig);
133 if (!useless_type_conversion_p (type_d, type_o))
134 return false;
136 return true;
139 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
141 bool
142 may_propagate_copy_into_asm (tree dest ATTRIBUTE_UNUSED)
144 return true;
148 /* Common code for propagate_value and replace_exp.
150 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
151 replacement is done to propagate a value or not. */
153 static void
154 replace_exp_1 (use_operand_p op_p, tree val,
155 bool for_propagation ATTRIBUTE_UNUSED)
157 #if defined ENABLE_CHECKING
158 tree op = USE_FROM_PTR (op_p);
160 gcc_assert (!(for_propagation
161 && TREE_CODE (op) == SSA_NAME
162 && TREE_CODE (val) == SSA_NAME
163 && !may_propagate_copy (op, val)));
164 #endif
166 if (TREE_CODE (val) == SSA_NAME)
167 SET_USE (op_p, val);
168 else
169 SET_USE (op_p, unshare_expr (val));
173 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
174 into the operand pointed to by OP_P.
176 Use this version for const/copy propagation as it will perform additional
177 checks to ensure validity of the const/copy propagation. */
179 void
180 propagate_value (use_operand_p op_p, tree val)
182 replace_exp_1 (op_p, val, true);
185 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
187 Use this version when not const/copy propagating values. For example,
188 PRE uses this version when building expressions as they would appear
189 in specific blocks taking into account actions of PHI nodes.
191 The statement in which an expression has been replaced should be
192 folded using fold_stmt_inplace. */
194 void
195 replace_exp (use_operand_p op_p, tree val)
197 replace_exp_1 (op_p, val, false);
201 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
202 into the tree pointed to by OP_P.
204 Use this version for const/copy propagation when SSA operands are not
205 available. It will perform the additional checks to ensure validity of
206 the const/copy propagation, but will not update any operand information.
207 Be sure to mark the stmt as modified. */
209 void
210 propagate_tree_value (tree *op_p, tree val)
212 gcc_checking_assert (!(TREE_CODE (val) == SSA_NAME
213 && *op_p
214 && TREE_CODE (*op_p) == SSA_NAME
215 && !may_propagate_copy (*op_p, val)));
217 if (TREE_CODE (val) == SSA_NAME)
218 *op_p = val;
219 else
220 *op_p = unshare_expr (val);
224 /* Like propagate_tree_value, but use as the operand to replace
225 the principal expression (typically, the RHS) contained in the
226 statement referenced by iterator GSI. Note that it is not
227 always possible to update the statement in-place, so a new
228 statement may be created to replace the original. */
230 void
231 propagate_tree_value_into_stmt (gimple_stmt_iterator *gsi, tree val)
233 gimple stmt = gsi_stmt (*gsi);
235 if (is_gimple_assign (stmt))
237 tree expr = NULL_TREE;
238 if (gimple_assign_single_p (stmt))
239 expr = gimple_assign_rhs1 (stmt);
240 propagate_tree_value (&expr, val);
241 gimple_assign_set_rhs_from_tree (gsi, expr);
243 else if (gimple_code (stmt) == GIMPLE_COND)
245 tree lhs = NULL_TREE;
246 tree rhs = build_zero_cst (TREE_TYPE (val));
247 propagate_tree_value (&lhs, val);
248 gimple_cond_set_code (stmt, NE_EXPR);
249 gimple_cond_set_lhs (stmt, lhs);
250 gimple_cond_set_rhs (stmt, rhs);
252 else if (is_gimple_call (stmt)
253 && gimple_call_lhs (stmt) != NULL_TREE)
255 tree expr = NULL_TREE;
256 bool res;
257 propagate_tree_value (&expr, val);
258 res = update_call_from_tree (gsi, expr);
259 gcc_assert (res);
261 else if (gimple_code (stmt) == GIMPLE_SWITCH)
262 propagate_tree_value (gimple_switch_index_ptr (stmt), val);
263 else
264 gcc_unreachable ();
267 /*---------------------------------------------------------------------------
268 Copy propagation
269 ---------------------------------------------------------------------------*/
270 /* Lattice for copy-propagation. The lattice is initialized to
271 UNDEFINED (value == NULL) for SSA names that can become a copy
272 of something or VARYING (value == self) if not (see get_copy_of_val
273 and stmt_may_generate_copy). Other values make the name a COPY
274 of that value.
276 When visiting a statement or PHI node the lattice value for an
277 SSA name can transition from UNDEFINED to COPY to VARYING. */
279 struct prop_value_d {
280 /* Copy-of value. */
281 tree value;
283 typedef struct prop_value_d prop_value_t;
285 static prop_value_t *copy_of;
286 static unsigned n_copy_of;
289 /* Return true if this statement may generate a useful copy. */
291 static bool
292 stmt_may_generate_copy (gimple stmt)
294 if (gimple_code (stmt) == GIMPLE_PHI)
295 return !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (stmt));
297 if (gimple_code (stmt) != GIMPLE_ASSIGN)
298 return false;
300 /* If the statement has volatile operands, it won't generate a
301 useful copy. */
302 if (gimple_has_volatile_ops (stmt))
303 return false;
305 /* Statements with loads and/or stores will never generate a useful copy. */
306 if (gimple_vuse (stmt))
307 return false;
309 /* Otherwise, the only statements that generate useful copies are
310 assignments whose RHS is just an SSA name that doesn't flow
311 through abnormal edges. */
312 return ((gimple_assign_rhs_code (stmt) == SSA_NAME
313 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt)))
314 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)));
318 /* Return the copy-of value for VAR. */
320 static inline prop_value_t *
321 get_copy_of_val (tree var)
323 prop_value_t *val = &copy_of[SSA_NAME_VERSION (var)];
325 if (val->value == NULL_TREE
326 && !stmt_may_generate_copy (SSA_NAME_DEF_STMT (var)))
328 /* If the variable will never generate a useful copy relation,
329 make it its own copy. */
330 val->value = var;
333 return val;
336 /* Return the variable VAR is a copy of or VAR if VAR isn't the result
337 of a copy. */
339 static inline tree
340 valueize_val (tree var)
342 if (TREE_CODE (var) == SSA_NAME)
344 tree val = get_copy_of_val (var)->value;
345 if (val)
346 return val;
348 return var;
351 /* Set VAL to be the copy of VAR. If that changed return true. */
353 static inline bool
354 set_copy_of_val (tree var, tree val)
356 unsigned int ver = SSA_NAME_VERSION (var);
357 tree old;
359 /* Set FIRST to be the first link in COPY_OF[DEST]. If that
360 changed, return true. */
361 old = copy_of[ver].value;
362 copy_of[ver].value = val;
364 if (old != val
365 || (val && !operand_equal_p (old, val, 0)))
366 return true;
368 return false;
372 /* Dump the copy-of value for variable VAR to FILE. */
374 static void
375 dump_copy_of (FILE *file, tree var)
377 tree val;
379 print_generic_expr (file, var, dump_flags);
380 if (TREE_CODE (var) != SSA_NAME)
381 return;
383 val = copy_of[SSA_NAME_VERSION (var)].value;
384 fprintf (file, " copy-of chain: ");
385 print_generic_expr (file, var, 0);
386 fprintf (file, " ");
387 if (!val)
388 fprintf (file, "[UNDEFINED]");
389 else if (val == var)
390 fprintf (file, "[NOT A COPY]");
391 else
393 fprintf (file, "-> ");
394 print_generic_expr (file, val, 0);
395 fprintf (file, " ");
396 fprintf (file, "[COPY]");
401 /* Evaluate the RHS of STMT. If it produces a valid copy, set the LHS
402 value and store the LHS into *RESULT_P. */
404 static enum ssa_prop_result
405 copy_prop_visit_assignment (gimple stmt, tree *result_p)
407 tree lhs, rhs;
409 lhs = gimple_assign_lhs (stmt);
410 rhs = valueize_val (gimple_assign_rhs1 (stmt));
412 if (TREE_CODE (lhs) == SSA_NAME)
414 /* Straight copy between two SSA names. First, make sure that
415 we can propagate the RHS into uses of LHS. */
416 if (!may_propagate_copy (lhs, rhs))
417 return SSA_PROP_VARYING;
419 *result_p = lhs;
420 if (set_copy_of_val (*result_p, rhs))
421 return SSA_PROP_INTERESTING;
422 else
423 return SSA_PROP_NOT_INTERESTING;
426 return SSA_PROP_VARYING;
430 /* Visit the GIMPLE_COND STMT. Return SSA_PROP_INTERESTING
431 if it can determine which edge will be taken. Otherwise, return
432 SSA_PROP_VARYING. */
434 static enum ssa_prop_result
435 copy_prop_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
437 enum ssa_prop_result retval = SSA_PROP_VARYING;
438 location_t loc = gimple_location (stmt);
440 tree op0 = gimple_cond_lhs (stmt);
441 tree op1 = gimple_cond_rhs (stmt);
443 /* The only conditionals that we may be able to compute statically
444 are predicates involving two SSA_NAMEs. */
445 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
447 op0 = valueize_val (op0);
448 op1 = valueize_val (op1);
450 /* See if we can determine the predicate's value. */
451 if (dump_file && (dump_flags & TDF_DETAILS))
453 fprintf (dump_file, "Trying to determine truth value of ");
454 fprintf (dump_file, "predicate ");
455 print_gimple_stmt (dump_file, stmt, 0, 0);
458 /* We can fold COND and get a useful result only when we have
459 the same SSA_NAME on both sides of a comparison operator. */
460 if (op0 == op1)
462 tree folded_cond = fold_binary_loc (loc, gimple_cond_code (stmt),
463 boolean_type_node, op0, op1);
464 if (folded_cond)
466 basic_block bb = gimple_bb (stmt);
467 *taken_edge_p = find_taken_edge (bb, folded_cond);
468 if (*taken_edge_p)
469 retval = SSA_PROP_INTERESTING;
474 if (dump_file && (dump_flags & TDF_DETAILS) && *taken_edge_p)
475 fprintf (dump_file, "\nConditional will always take edge %d->%d\n",
476 (*taken_edge_p)->src->index, (*taken_edge_p)->dest->index);
478 return retval;
482 /* Evaluate statement STMT. If the statement produces a new output
483 value, return SSA_PROP_INTERESTING and store the SSA_NAME holding
484 the new value in *RESULT_P.
486 If STMT is a conditional branch and we can determine its truth
487 value, set *TAKEN_EDGE_P accordingly.
489 If the new value produced by STMT is varying, return
490 SSA_PROP_VARYING. */
492 static enum ssa_prop_result
493 copy_prop_visit_stmt (gimple stmt, edge *taken_edge_p, tree *result_p)
495 enum ssa_prop_result retval;
497 if (dump_file && (dump_flags & TDF_DETAILS))
499 fprintf (dump_file, "\nVisiting statement:\n");
500 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
501 fprintf (dump_file, "\n");
504 if (gimple_assign_single_p (stmt)
505 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
506 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
507 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
509 /* If the statement is a copy assignment, evaluate its RHS to
510 see if the lattice value of its output has changed. */
511 retval = copy_prop_visit_assignment (stmt, result_p);
513 else if (gimple_code (stmt) == GIMPLE_COND)
515 /* See if we can determine which edge goes out of a conditional
516 jump. */
517 retval = copy_prop_visit_cond_stmt (stmt, taken_edge_p);
519 else
520 retval = SSA_PROP_VARYING;
522 if (retval == SSA_PROP_VARYING)
524 tree def;
525 ssa_op_iter i;
527 /* Any other kind of statement is not interesting for constant
528 propagation and, therefore, not worth simulating. */
529 if (dump_file && (dump_flags & TDF_DETAILS))
530 fprintf (dump_file, "No interesting values produced.\n");
532 /* The assignment is not a copy operation. Don't visit this
533 statement again and mark all the definitions in the statement
534 to be copies of nothing. */
535 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_ALL_DEFS)
536 set_copy_of_val (def, def);
539 return retval;
543 /* Visit PHI node PHI. If all the arguments produce the same value,
544 set it to be the value of the LHS of PHI. */
546 static enum ssa_prop_result
547 copy_prop_visit_phi_node (gimple phi)
549 enum ssa_prop_result retval;
550 unsigned i;
551 prop_value_t phi_val = { NULL_TREE };
553 tree lhs = gimple_phi_result (phi);
555 if (dump_file && (dump_flags & TDF_DETAILS))
557 fprintf (dump_file, "\nVisiting PHI node: ");
558 print_gimple_stmt (dump_file, phi, 0, dump_flags);
561 for (i = 0; i < gimple_phi_num_args (phi); i++)
563 prop_value_t *arg_val;
564 tree arg_value;
565 tree arg = gimple_phi_arg_def (phi, i);
566 edge e = gimple_phi_arg_edge (phi, i);
568 /* We don't care about values flowing through non-executable
569 edges. */
570 if (!(e->flags & EDGE_EXECUTABLE))
571 continue;
573 /* Names that flow through abnormal edges cannot be used to
574 derive copies. */
575 if (TREE_CODE (arg) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg))
577 phi_val.value = lhs;
578 break;
581 if (dump_file && (dump_flags & TDF_DETAILS))
583 fprintf (dump_file, "\tArgument #%d: ", i);
584 dump_copy_of (dump_file, arg);
585 fprintf (dump_file, "\n");
588 if (TREE_CODE (arg) == SSA_NAME)
590 arg_val = get_copy_of_val (arg);
592 /* If we didn't visit the definition of arg yet treat it as
593 UNDEFINED. This also handles PHI arguments that are the
594 same as lhs. We'll come here again. */
595 if (!arg_val->value)
596 continue;
598 arg_value = arg_val->value;
600 else
601 arg_value = valueize_val (arg);
603 /* Avoid copy propagation from an inner into an outer loop.
604 Otherwise, this may move loop variant variables outside of
605 their loops and prevent coalescing opportunities. If the
606 value was loop invariant, it will be hoisted by LICM and
607 exposed for copy propagation.
608 ??? The value will be always loop invariant.
609 In loop-closed SSA form do not copy-propagate through
610 PHI nodes in blocks with a loop exit edge predecessor. */
611 if (current_loops
612 && TREE_CODE (arg_value) == SSA_NAME
613 && (loop_depth_of_name (arg_value) > loop_depth_of_name (lhs)
614 || (loops_state_satisfies_p (LOOP_CLOSED_SSA)
615 && loop_exit_edge_p (e->src->loop_father, e))))
617 phi_val.value = lhs;
618 break;
621 /* If the LHS didn't have a value yet, make it a copy of the
622 first argument we find. */
623 if (phi_val.value == NULL_TREE)
625 phi_val.value = arg_value;
626 continue;
629 /* If PHI_VAL and ARG don't have a common copy-of chain, then
630 this PHI node cannot be a copy operation. */
631 if (phi_val.value != arg_value
632 && !operand_equal_p (phi_val.value, arg_value, 0))
634 phi_val.value = lhs;
635 break;
639 if (phi_val.value
640 && may_propagate_copy (lhs, phi_val.value)
641 && set_copy_of_val (lhs, phi_val.value))
642 retval = (phi_val.value != lhs) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
643 else
644 retval = SSA_PROP_NOT_INTERESTING;
646 if (dump_file && (dump_flags & TDF_DETAILS))
648 fprintf (dump_file, "PHI node ");
649 dump_copy_of (dump_file, lhs);
650 fprintf (dump_file, "\nTelling the propagator to ");
651 if (retval == SSA_PROP_INTERESTING)
652 fprintf (dump_file, "add SSA edges out of this PHI and continue.");
653 else if (retval == SSA_PROP_VARYING)
654 fprintf (dump_file, "add SSA edges out of this PHI and never visit again.");
655 else
656 fprintf (dump_file, "do nothing with SSA edges and keep iterating.");
657 fprintf (dump_file, "\n\n");
660 return retval;
664 /* Initialize structures used for copy propagation. */
666 static void
667 init_copy_prop (void)
669 basic_block bb;
671 n_copy_of = num_ssa_names;
672 copy_of = XCNEWVEC (prop_value_t, n_copy_of);
674 FOR_EACH_BB (bb)
676 gimple_stmt_iterator si;
677 int depth = bb_loop_depth (bb);
679 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
681 gimple stmt = gsi_stmt (si);
682 ssa_op_iter iter;
683 tree def;
685 /* The only statements that we care about are those that may
686 generate useful copies. We also need to mark conditional
687 jumps so that their outgoing edges are added to the work
688 lists of the propagator.
690 Avoid copy propagation from an inner into an outer loop.
691 Otherwise, this may move loop variant variables outside of
692 their loops and prevent coalescing opportunities. If the
693 value was loop invariant, it will be hoisted by LICM and
694 exposed for copy propagation.
695 ??? This doesn't make sense. */
696 if (stmt_ends_bb_p (stmt))
697 prop_set_simulate_again (stmt, true);
698 else if (stmt_may_generate_copy (stmt)
699 /* Since we are iterating over the statements in
700 BB, not the phi nodes, STMT will always be an
701 assignment. */
702 && loop_depth_of_name (gimple_assign_rhs1 (stmt)) <= depth)
703 prop_set_simulate_again (stmt, true);
704 else
705 prop_set_simulate_again (stmt, false);
707 /* Mark all the outputs of this statement as not being
708 the copy of anything. */
709 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
710 if (!prop_simulate_again_p (stmt))
711 set_copy_of_val (def, def);
714 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
716 gimple phi = gsi_stmt (si);
717 tree def;
719 def = gimple_phi_result (phi);
720 if (virtual_operand_p (def))
721 prop_set_simulate_again (phi, false);
722 else
723 prop_set_simulate_again (phi, true);
725 if (!prop_simulate_again_p (phi))
726 set_copy_of_val (def, def);
731 /* Callback for substitute_and_fold to get at the final copy-of values. */
733 static tree
734 get_value (tree name)
736 tree val;
737 if (SSA_NAME_VERSION (name) >= n_copy_of)
738 return NULL_TREE;
739 val = copy_of[SSA_NAME_VERSION (name)].value;
740 if (val && val != name)
741 return val;
742 return NULL_TREE;
745 /* Deallocate memory used in copy propagation and do final
746 substitution. */
748 static void
749 fini_copy_prop (void)
751 unsigned i;
753 /* Set the final copy-of value for each variable by traversing the
754 copy-of chains. */
755 for (i = 1; i < num_ssa_names; i++)
757 tree var = ssa_name (i);
758 if (!var
759 || !copy_of[i].value
760 || copy_of[i].value == var)
761 continue;
763 /* In theory the points-to solution of all members of the
764 copy chain is their intersection. For now we do not bother
765 to compute this but only make sure we do not lose points-to
766 information completely by setting the points-to solution
767 of the representative to the first solution we find if
768 it doesn't have one already. */
769 if (copy_of[i].value != var
770 && TREE_CODE (copy_of[i].value) == SSA_NAME
771 && POINTER_TYPE_P (TREE_TYPE (var))
772 && SSA_NAME_PTR_INFO (var)
773 && !SSA_NAME_PTR_INFO (copy_of[i].value))
774 duplicate_ssa_name_ptr_info (copy_of[i].value, SSA_NAME_PTR_INFO (var));
777 /* Don't do DCE if SCEV is initialized. It would destroy the scev cache. */
778 substitute_and_fold (get_value, NULL, !scev_initialized_p ());
780 free (copy_of);
784 /* Main entry point to the copy propagator.
786 PHIS_ONLY is true if we should only consider PHI nodes as generating
787 copy propagation opportunities.
789 The algorithm propagates the value COPY-OF using ssa_propagate. For
790 every variable X_i, COPY-OF(X_i) indicates which variable is X_i created
791 from. The following example shows how the algorithm proceeds at a
792 high level:
794 1 a_24 = x_1
795 2 a_2 = PHI <a_24, x_1>
796 3 a_5 = PHI <a_2>
797 4 x_1 = PHI <x_298, a_5, a_2>
799 The end result should be that a_2, a_5, a_24 and x_1 are a copy of
800 x_298. Propagation proceeds as follows.
802 Visit #1: a_24 is copy-of x_1. Value changed.
803 Visit #2: a_2 is copy-of x_1. Value changed.
804 Visit #3: a_5 is copy-of x_1. Value changed.
805 Visit #4: x_1 is copy-of x_298. Value changed.
806 Visit #1: a_24 is copy-of x_298. Value changed.
807 Visit #2: a_2 is copy-of x_298. Value changed.
808 Visit #3: a_5 is copy-of x_298. Value changed.
809 Visit #4: x_1 is copy-of x_298. Stable state reached.
811 When visiting PHI nodes, we only consider arguments that flow
812 through edges marked executable by the propagation engine. So,
813 when visiting statement #2 for the first time, we will only look at
814 the first argument (a_24) and optimistically assume that its value
815 is the copy of a_24 (x_1). */
817 static unsigned int
818 execute_copy_prop (void)
820 init_copy_prop ();
821 ssa_propagate (copy_prop_visit_stmt, copy_prop_visit_phi_node);
822 fini_copy_prop ();
823 return 0;
826 static bool
827 gate_copy_prop (void)
829 return flag_tree_copy_prop != 0;
832 namespace {
834 const pass_data pass_data_copy_prop =
836 GIMPLE_PASS, /* type */
837 "copyprop", /* name */
838 OPTGROUP_NONE, /* optinfo_flags */
839 true, /* has_gate */
840 true, /* has_execute */
841 TV_TREE_COPY_PROP, /* tv_id */
842 ( PROP_ssa | PROP_cfg ), /* properties_required */
843 0, /* properties_provided */
844 0, /* properties_destroyed */
845 0, /* todo_flags_start */
846 ( TODO_cleanup_cfg | TODO_verify_ssa
847 | TODO_update_ssa ), /* todo_flags_finish */
850 class pass_copy_prop : public gimple_opt_pass
852 public:
853 pass_copy_prop(gcc::context *ctxt)
854 : gimple_opt_pass(pass_data_copy_prop, ctxt)
857 /* opt_pass methods: */
858 opt_pass * clone () { return new pass_copy_prop (ctxt_); }
859 bool gate () { return gate_copy_prop (); }
860 unsigned int execute () { return execute_copy_prop (); }
862 }; // class pass_copy_prop
864 } // anon namespace
866 gimple_opt_pass *
867 make_pass_copy_prop (gcc::context *ctxt)
869 return new pass_copy_prop (ctxt);