2013-05-30 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / tree-ssa-copy.c
blob625f46e19429e32383c75dfebc8c0e6af0d49554
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 return false;
66 /* If DEST is an SSA_NAME that flows from an abnormal edge, then it
67 cannot be replaced. */
68 if (TREE_CODE (dest) == SSA_NAME
69 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest))
70 return false;
72 /* Do not copy between types for which we *do* need a conversion. */
73 if (!useless_type_conversion_p (type_d, type_o))
74 return false;
76 /* Generally propagating virtual operands is not ok as that may
77 create overlapping life-ranges. */
78 if (TREE_CODE (dest) == SSA_NAME && virtual_operand_p (dest))
79 return false;
81 /* Anything else is OK. */
82 return true;
85 /* Like may_propagate_copy, but use as the destination expression
86 the principal expression (typically, the RHS) contained in
87 statement DEST. This is more efficient when working with the
88 gimple tuples representation. */
90 bool
91 may_propagate_copy_into_stmt (gimple dest, tree orig)
93 tree type_d;
94 tree type_o;
96 /* If the statement is a switch or a single-rhs assignment,
97 then the expression to be replaced by the propagation may
98 be an SSA_NAME. Fortunately, there is an explicit tree
99 for the expression, so we delegate to may_propagate_copy. */
101 if (gimple_assign_single_p (dest))
102 return may_propagate_copy (gimple_assign_rhs1 (dest), orig);
103 else if (gimple_code (dest) == GIMPLE_SWITCH)
104 return may_propagate_copy (gimple_switch_index (dest), orig);
106 /* In other cases, the expression is not materialized, so there
107 is no destination to pass to may_propagate_copy. On the other
108 hand, the expression cannot be an SSA_NAME, so the analysis
109 is much simpler. */
111 if (TREE_CODE (orig) == SSA_NAME
112 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
113 return false;
115 if (is_gimple_assign (dest))
116 type_d = TREE_TYPE (gimple_assign_lhs (dest));
117 else if (gimple_code (dest) == GIMPLE_COND)
118 type_d = boolean_type_node;
119 else if (is_gimple_call (dest)
120 && gimple_call_lhs (dest) != NULL_TREE)
121 type_d = TREE_TYPE (gimple_call_lhs (dest));
122 else
123 gcc_unreachable ();
125 type_o = TREE_TYPE (orig);
127 if (!useless_type_conversion_p (type_d, type_o))
128 return false;
130 return true;
133 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
135 bool
136 may_propagate_copy_into_asm (tree dest ATTRIBUTE_UNUSED)
138 return true;
142 /* Common code for propagate_value and replace_exp.
144 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
145 replacement is done to propagate a value or not. */
147 static void
148 replace_exp_1 (use_operand_p op_p, tree val,
149 bool for_propagation ATTRIBUTE_UNUSED)
151 #if defined ENABLE_CHECKING
152 tree op = USE_FROM_PTR (op_p);
154 gcc_assert (!(for_propagation
155 && TREE_CODE (op) == SSA_NAME
156 && TREE_CODE (val) == SSA_NAME
157 && !may_propagate_copy (op, val)));
158 #endif
160 if (TREE_CODE (val) == SSA_NAME)
161 SET_USE (op_p, val);
162 else
163 SET_USE (op_p, unshare_expr (val));
167 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
168 into the operand pointed to by OP_P.
170 Use this version for const/copy propagation as it will perform additional
171 checks to ensure validity of the const/copy propagation. */
173 void
174 propagate_value (use_operand_p op_p, tree val)
176 replace_exp_1 (op_p, val, true);
179 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
181 Use this version when not const/copy propagating values. For example,
182 PRE uses this version when building expressions as they would appear
183 in specific blocks taking into account actions of PHI nodes.
185 The statement in which an expression has been replaced should be
186 folded using fold_stmt_inplace. */
188 void
189 replace_exp (use_operand_p op_p, tree val)
191 replace_exp_1 (op_p, val, false);
195 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
196 into the tree pointed to by OP_P.
198 Use this version for const/copy propagation when SSA operands are not
199 available. It will perform the additional checks to ensure validity of
200 the const/copy propagation, but will not update any operand information.
201 Be sure to mark the stmt as modified. */
203 void
204 propagate_tree_value (tree *op_p, tree val)
206 gcc_checking_assert (!(TREE_CODE (val) == SSA_NAME
207 && *op_p
208 && TREE_CODE (*op_p) == SSA_NAME
209 && !may_propagate_copy (*op_p, val)));
211 if (TREE_CODE (val) == SSA_NAME)
212 *op_p = val;
213 else
214 *op_p = unshare_expr (val);
218 /* Like propagate_tree_value, but use as the operand to replace
219 the principal expression (typically, the RHS) contained in the
220 statement referenced by iterator GSI. Note that it is not
221 always possible to update the statement in-place, so a new
222 statement may be created to replace the original. */
224 void
225 propagate_tree_value_into_stmt (gimple_stmt_iterator *gsi, tree val)
227 gimple stmt = gsi_stmt (*gsi);
229 if (is_gimple_assign (stmt))
231 tree expr = NULL_TREE;
232 if (gimple_assign_single_p (stmt))
233 expr = gimple_assign_rhs1 (stmt);
234 propagate_tree_value (&expr, val);
235 gimple_assign_set_rhs_from_tree (gsi, expr);
237 else if (gimple_code (stmt) == GIMPLE_COND)
239 tree lhs = NULL_TREE;
240 tree rhs = build_zero_cst (TREE_TYPE (val));
241 propagate_tree_value (&lhs, val);
242 gimple_cond_set_code (stmt, NE_EXPR);
243 gimple_cond_set_lhs (stmt, lhs);
244 gimple_cond_set_rhs (stmt, rhs);
246 else if (is_gimple_call (stmt)
247 && gimple_call_lhs (stmt) != NULL_TREE)
249 tree expr = NULL_TREE;
250 bool res;
251 propagate_tree_value (&expr, val);
252 res = update_call_from_tree (gsi, expr);
253 gcc_assert (res);
255 else if (gimple_code (stmt) == GIMPLE_SWITCH)
256 propagate_tree_value (gimple_switch_index_ptr (stmt), val);
257 else
258 gcc_unreachable ();
261 /*---------------------------------------------------------------------------
262 Copy propagation
263 ---------------------------------------------------------------------------*/
264 /* Lattice for copy-propagation. The lattice is initialized to
265 UNDEFINED (value == NULL) for SSA names that can become a copy
266 of something or VARYING (value == self) if not (see get_copy_of_val
267 and stmt_may_generate_copy). Other values make the name a COPY
268 of that value.
270 When visiting a statement or PHI node the lattice value for an
271 SSA name can transition from UNDEFINED to COPY to VARYING. */
273 struct prop_value_d {
274 /* Copy-of value. */
275 tree value;
277 typedef struct prop_value_d prop_value_t;
279 static prop_value_t *copy_of;
280 static unsigned n_copy_of;
283 /* Return true if this statement may generate a useful copy. */
285 static bool
286 stmt_may_generate_copy (gimple stmt)
288 if (gimple_code (stmt) == GIMPLE_PHI)
289 return !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (stmt));
291 if (gimple_code (stmt) != GIMPLE_ASSIGN)
292 return false;
294 /* If the statement has volatile operands, it won't generate a
295 useful copy. */
296 if (gimple_has_volatile_ops (stmt))
297 return false;
299 /* Statements with loads and/or stores will never generate a useful copy. */
300 if (gimple_vuse (stmt))
301 return false;
303 /* Otherwise, the only statements that generate useful copies are
304 assignments whose RHS is just an SSA name that doesn't flow
305 through abnormal edges. */
306 return ((gimple_assign_rhs_code (stmt) == SSA_NAME
307 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt)))
308 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)));
312 /* Return the copy-of value for VAR. */
314 static inline prop_value_t *
315 get_copy_of_val (tree var)
317 prop_value_t *val = &copy_of[SSA_NAME_VERSION (var)];
319 if (val->value == NULL_TREE
320 && !stmt_may_generate_copy (SSA_NAME_DEF_STMT (var)))
322 /* If the variable will never generate a useful copy relation,
323 make it its own copy. */
324 val->value = var;
327 return val;
330 /* Return the variable VAR is a copy of or VAR if VAR isn't the result
331 of a copy. */
333 static inline tree
334 valueize_val (tree var)
336 if (TREE_CODE (var) == SSA_NAME)
338 tree val = get_copy_of_val (var)->value;
339 if (val)
340 return val;
342 return var;
345 /* Set VAL to be the copy of VAR. If that changed return true. */
347 static inline bool
348 set_copy_of_val (tree var, tree val)
350 unsigned int ver = SSA_NAME_VERSION (var);
351 tree old;
353 /* Set FIRST to be the first link in COPY_OF[DEST]. If that
354 changed, return true. */
355 old = copy_of[ver].value;
356 copy_of[ver].value = val;
358 if (old != val
359 || (val && !operand_equal_p (old, val, 0)))
360 return true;
362 return false;
366 /* Dump the copy-of value for variable VAR to FILE. */
368 static void
369 dump_copy_of (FILE *file, tree var)
371 tree val;
373 print_generic_expr (file, var, dump_flags);
374 if (TREE_CODE (var) != SSA_NAME)
375 return;
377 val = copy_of[SSA_NAME_VERSION (var)].value;
378 fprintf (file, " copy-of chain: ");
379 print_generic_expr (file, var, 0);
380 fprintf (file, " ");
381 if (!val)
382 fprintf (file, "[UNDEFINED]");
383 else if (val == var)
384 fprintf (file, "[NOT A COPY]");
385 else
387 fprintf (file, "-> ");
388 print_generic_expr (file, val, 0);
389 fprintf (file, " ");
390 fprintf (file, "[COPY]");
395 /* Evaluate the RHS of STMT. If it produces a valid copy, set the LHS
396 value and store the LHS into *RESULT_P. */
398 static enum ssa_prop_result
399 copy_prop_visit_assignment (gimple stmt, tree *result_p)
401 tree lhs, rhs;
403 lhs = gimple_assign_lhs (stmt);
404 rhs = valueize_val (gimple_assign_rhs1 (stmt));
406 if (TREE_CODE (lhs) == SSA_NAME)
408 /* Straight copy between two SSA names. First, make sure that
409 we can propagate the RHS into uses of LHS. */
410 if (!may_propagate_copy (lhs, rhs))
411 return SSA_PROP_VARYING;
413 *result_p = lhs;
414 if (set_copy_of_val (*result_p, rhs))
415 return SSA_PROP_INTERESTING;
416 else
417 return SSA_PROP_NOT_INTERESTING;
420 return SSA_PROP_VARYING;
424 /* Visit the GIMPLE_COND STMT. Return SSA_PROP_INTERESTING
425 if it can determine which edge will be taken. Otherwise, return
426 SSA_PROP_VARYING. */
428 static enum ssa_prop_result
429 copy_prop_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
431 enum ssa_prop_result retval = SSA_PROP_VARYING;
432 location_t loc = gimple_location (stmt);
434 tree op0 = gimple_cond_lhs (stmt);
435 tree op1 = gimple_cond_rhs (stmt);
437 /* The only conditionals that we may be able to compute statically
438 are predicates involving two SSA_NAMEs. */
439 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
441 op0 = valueize_val (op0);
442 op1 = valueize_val (op1);
444 /* See if we can determine the predicate's value. */
445 if (dump_file && (dump_flags & TDF_DETAILS))
447 fprintf (dump_file, "Trying to determine truth value of ");
448 fprintf (dump_file, "predicate ");
449 print_gimple_stmt (dump_file, stmt, 0, 0);
452 /* We can fold COND and get a useful result only when we have
453 the same SSA_NAME on both sides of a comparison operator. */
454 if (op0 == op1)
456 tree folded_cond = fold_binary_loc (loc, gimple_cond_code (stmt),
457 boolean_type_node, op0, op1);
458 if (folded_cond)
460 basic_block bb = gimple_bb (stmt);
461 *taken_edge_p = find_taken_edge (bb, folded_cond);
462 if (*taken_edge_p)
463 retval = SSA_PROP_INTERESTING;
468 if (dump_file && (dump_flags & TDF_DETAILS) && *taken_edge_p)
469 fprintf (dump_file, "\nConditional will always take edge %d->%d\n",
470 (*taken_edge_p)->src->index, (*taken_edge_p)->dest->index);
472 return retval;
476 /* Evaluate statement STMT. If the statement produces a new output
477 value, return SSA_PROP_INTERESTING and store the SSA_NAME holding
478 the new value in *RESULT_P.
480 If STMT is a conditional branch and we can determine its truth
481 value, set *TAKEN_EDGE_P accordingly.
483 If the new value produced by STMT is varying, return
484 SSA_PROP_VARYING. */
486 static enum ssa_prop_result
487 copy_prop_visit_stmt (gimple stmt, edge *taken_edge_p, tree *result_p)
489 enum ssa_prop_result retval;
491 if (dump_file && (dump_flags & TDF_DETAILS))
493 fprintf (dump_file, "\nVisiting statement:\n");
494 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
495 fprintf (dump_file, "\n");
498 if (gimple_assign_single_p (stmt)
499 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
500 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
501 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
503 /* If the statement is a copy assignment, evaluate its RHS to
504 see if the lattice value of its output has changed. */
505 retval = copy_prop_visit_assignment (stmt, result_p);
507 else if (gimple_code (stmt) == GIMPLE_COND)
509 /* See if we can determine which edge goes out of a conditional
510 jump. */
511 retval = copy_prop_visit_cond_stmt (stmt, taken_edge_p);
513 else
514 retval = SSA_PROP_VARYING;
516 if (retval == SSA_PROP_VARYING)
518 tree def;
519 ssa_op_iter i;
521 /* Any other kind of statement is not interesting for constant
522 propagation and, therefore, not worth simulating. */
523 if (dump_file && (dump_flags & TDF_DETAILS))
524 fprintf (dump_file, "No interesting values produced.\n");
526 /* The assignment is not a copy operation. Don't visit this
527 statement again and mark all the definitions in the statement
528 to be copies of nothing. */
529 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_ALL_DEFS)
530 set_copy_of_val (def, def);
533 return retval;
537 /* Visit PHI node PHI. If all the arguments produce the same value,
538 set it to be the value of the LHS of PHI. */
540 static enum ssa_prop_result
541 copy_prop_visit_phi_node (gimple phi)
543 enum ssa_prop_result retval;
544 unsigned i;
545 prop_value_t phi_val = { NULL_TREE };
547 tree lhs = gimple_phi_result (phi);
549 if (dump_file && (dump_flags & TDF_DETAILS))
551 fprintf (dump_file, "\nVisiting PHI node: ");
552 print_gimple_stmt (dump_file, phi, 0, dump_flags);
555 for (i = 0; i < gimple_phi_num_args (phi); i++)
557 prop_value_t *arg_val;
558 tree arg_value;
559 tree arg = gimple_phi_arg_def (phi, i);
560 edge e = gimple_phi_arg_edge (phi, i);
562 /* We don't care about values flowing through non-executable
563 edges. */
564 if (!(e->flags & EDGE_EXECUTABLE))
565 continue;
567 /* Names that flow through abnormal edges cannot be used to
568 derive copies. */
569 if (TREE_CODE (arg) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg))
571 phi_val.value = lhs;
572 break;
575 if (dump_file && (dump_flags & TDF_DETAILS))
577 fprintf (dump_file, "\tArgument #%d: ", i);
578 dump_copy_of (dump_file, arg);
579 fprintf (dump_file, "\n");
582 if (TREE_CODE (arg) == SSA_NAME)
584 arg_val = get_copy_of_val (arg);
586 /* If we didn't visit the definition of arg yet treat it as
587 UNDEFINED. This also handles PHI arguments that are the
588 same as lhs. We'll come here again. */
589 if (!arg_val->value)
590 continue;
592 arg_value = arg_val->value;
594 else
595 arg_value = valueize_val (arg);
597 /* Avoid copy propagation from an inner into an outer loop.
598 Otherwise, this may move loop variant variables outside of
599 their loops and prevent coalescing opportunities. If the
600 value was loop invariant, it will be hoisted by LICM and
601 exposed for copy propagation.
602 ??? The value will be always loop invariant.
603 In loop-closed SSA form do not copy-propagate through
604 PHI nodes in blocks with a loop exit edge predecessor. */
605 if (current_loops
606 && TREE_CODE (arg_value) == SSA_NAME
607 && (loop_depth_of_name (arg_value) > loop_depth_of_name (lhs)
608 || (loops_state_satisfies_p (LOOP_CLOSED_SSA)
609 && loop_exit_edge_p (e->src->loop_father, e))))
611 phi_val.value = lhs;
612 break;
615 /* If the LHS didn't have a value yet, make it a copy of the
616 first argument we find. */
617 if (phi_val.value == NULL_TREE)
619 phi_val.value = arg_value;
620 continue;
623 /* If PHI_VAL and ARG don't have a common copy-of chain, then
624 this PHI node cannot be a copy operation. */
625 if (phi_val.value != arg_value
626 && !operand_equal_p (phi_val.value, arg_value, 0))
628 phi_val.value = lhs;
629 break;
633 if (phi_val.value
634 && may_propagate_copy (lhs, phi_val.value)
635 && set_copy_of_val (lhs, phi_val.value))
636 retval = (phi_val.value != lhs) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
637 else
638 retval = SSA_PROP_NOT_INTERESTING;
640 if (dump_file && (dump_flags & TDF_DETAILS))
642 fprintf (dump_file, "PHI node ");
643 dump_copy_of (dump_file, lhs);
644 fprintf (dump_file, "\nTelling the propagator to ");
645 if (retval == SSA_PROP_INTERESTING)
646 fprintf (dump_file, "add SSA edges out of this PHI and continue.");
647 else if (retval == SSA_PROP_VARYING)
648 fprintf (dump_file, "add SSA edges out of this PHI and never visit again.");
649 else
650 fprintf (dump_file, "do nothing with SSA edges and keep iterating.");
651 fprintf (dump_file, "\n\n");
654 return retval;
658 /* Initialize structures used for copy propagation. */
660 static void
661 init_copy_prop (void)
663 basic_block bb;
665 n_copy_of = num_ssa_names;
666 copy_of = XCNEWVEC (prop_value_t, n_copy_of);
668 FOR_EACH_BB (bb)
670 gimple_stmt_iterator si;
671 int depth = bb_loop_depth (bb);
673 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
675 gimple stmt = gsi_stmt (si);
676 ssa_op_iter iter;
677 tree def;
679 /* The only statements that we care about are those that may
680 generate useful copies. We also need to mark conditional
681 jumps so that their outgoing edges are added to the work
682 lists of the propagator.
684 Avoid copy propagation from an inner into an outer loop.
685 Otherwise, this may move loop variant variables outside of
686 their loops and prevent coalescing opportunities. If the
687 value was loop invariant, it will be hoisted by LICM and
688 exposed for copy propagation.
689 ??? This doesn't make sense. */
690 if (stmt_ends_bb_p (stmt))
691 prop_set_simulate_again (stmt, true);
692 else if (stmt_may_generate_copy (stmt)
693 /* Since we are iterating over the statements in
694 BB, not the phi nodes, STMT will always be an
695 assignment. */
696 && loop_depth_of_name (gimple_assign_rhs1 (stmt)) <= depth)
697 prop_set_simulate_again (stmt, true);
698 else
699 prop_set_simulate_again (stmt, false);
701 /* Mark all the outputs of this statement as not being
702 the copy of anything. */
703 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
704 if (!prop_simulate_again_p (stmt))
705 set_copy_of_val (def, def);
708 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
710 gimple phi = gsi_stmt (si);
711 tree def;
713 def = gimple_phi_result (phi);
714 if (virtual_operand_p (def))
715 prop_set_simulate_again (phi, false);
716 else
717 prop_set_simulate_again (phi, true);
719 if (!prop_simulate_again_p (phi))
720 set_copy_of_val (def, def);
725 /* Callback for substitute_and_fold to get at the final copy-of values. */
727 static tree
728 get_value (tree name)
730 tree val;
731 if (SSA_NAME_VERSION (name) >= n_copy_of)
732 return NULL_TREE;
733 val = copy_of[SSA_NAME_VERSION (name)].value;
734 if (val && val != name)
735 return val;
736 return NULL_TREE;
739 /* Deallocate memory used in copy propagation and do final
740 substitution. */
742 static void
743 fini_copy_prop (void)
745 unsigned i;
747 /* Set the final copy-of value for each variable by traversing the
748 copy-of chains. */
749 for (i = 1; i < num_ssa_names; i++)
751 tree var = ssa_name (i);
752 if (!var
753 || !copy_of[i].value
754 || copy_of[i].value == var)
755 continue;
757 /* In theory the points-to solution of all members of the
758 copy chain is their intersection. For now we do not bother
759 to compute this but only make sure we do not lose points-to
760 information completely by setting the points-to solution
761 of the representative to the first solution we find if
762 it doesn't have one already. */
763 if (copy_of[i].value != var
764 && TREE_CODE (copy_of[i].value) == SSA_NAME
765 && POINTER_TYPE_P (TREE_TYPE (var))
766 && SSA_NAME_PTR_INFO (var)
767 && !SSA_NAME_PTR_INFO (copy_of[i].value))
768 duplicate_ssa_name_ptr_info (copy_of[i].value, SSA_NAME_PTR_INFO (var));
771 /* Don't do DCE if SCEV is initialized. It would destroy the scev cache. */
772 substitute_and_fold (get_value, NULL, !scev_initialized_p ());
774 free (copy_of);
778 /* Main entry point to the copy propagator.
780 PHIS_ONLY is true if we should only consider PHI nodes as generating
781 copy propagation opportunities.
783 The algorithm propagates the value COPY-OF using ssa_propagate. For
784 every variable X_i, COPY-OF(X_i) indicates which variable is X_i created
785 from. The following example shows how the algorithm proceeds at a
786 high level:
788 1 a_24 = x_1
789 2 a_2 = PHI <a_24, x_1>
790 3 a_5 = PHI <a_2>
791 4 x_1 = PHI <x_298, a_5, a_2>
793 The end result should be that a_2, a_5, a_24 and x_1 are a copy of
794 x_298. Propagation proceeds as follows.
796 Visit #1: a_24 is copy-of x_1. Value changed.
797 Visit #2: a_2 is copy-of x_1. Value changed.
798 Visit #3: a_5 is copy-of x_1. Value changed.
799 Visit #4: x_1 is copy-of x_298. Value changed.
800 Visit #1: a_24 is copy-of x_298. Value changed.
801 Visit #2: a_2 is copy-of x_298. Value changed.
802 Visit #3: a_5 is copy-of x_298. Value changed.
803 Visit #4: x_1 is copy-of x_298. Stable state reached.
805 When visiting PHI nodes, we only consider arguments that flow
806 through edges marked executable by the propagation engine. So,
807 when visiting statement #2 for the first time, we will only look at
808 the first argument (a_24) and optimistically assume that its value
809 is the copy of a_24 (x_1). */
811 static unsigned int
812 execute_copy_prop (void)
814 init_copy_prop ();
815 ssa_propagate (copy_prop_visit_stmt, copy_prop_visit_phi_node);
816 fini_copy_prop ();
817 return 0;
820 static bool
821 gate_copy_prop (void)
823 return flag_tree_copy_prop != 0;
826 struct gimple_opt_pass pass_copy_prop =
829 GIMPLE_PASS,
830 "copyprop", /* name */
831 OPTGROUP_NONE, /* optinfo_flags */
832 gate_copy_prop, /* gate */
833 execute_copy_prop, /* execute */
834 NULL, /* sub */
835 NULL, /* next */
836 0, /* static_pass_number */
837 TV_TREE_COPY_PROP, /* tv_id */
838 PROP_ssa | PROP_cfg, /* properties_required */
839 0, /* properties_provided */
840 0, /* properties_destroyed */
841 0, /* todo_flags_start */
842 TODO_cleanup_cfg
843 | TODO_verify_ssa
844 | TODO_update_ssa /* todo_flags_finish */