Daily bump.
[official-gcc.git] / gcc / tree-ssa-copy.c
blobae4f50c2febe70372f5234b3e730a2d78e4ae0e5
1 /* Copy propagation and SSA_NAME replacement support routines.
2 Copyright (C) 2004, 2005, 2007 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"
41 /* This file implements the copy propagation pass and provides a
42 handful of interfaces for performing const/copy propagation and
43 simple expression replacement which keep variable annotations
44 up-to-date.
46 We require that for any copy operation where the RHS and LHS have
47 a non-null memory tag the memory tag be the same. It is OK
48 for one or both of the memory tags to be NULL.
50 We also require tracking if a variable is dereferenced in a load or
51 store operation.
53 We enforce these requirements by having all copy propagation and
54 replacements of one SSA_NAME with a different SSA_NAME to use the
55 APIs defined in this file. */
57 /* Return true if we may propagate ORIG into DEST, false otherwise. */
59 bool
60 may_propagate_copy (tree dest, tree orig)
62 tree type_d = TREE_TYPE (dest);
63 tree type_o = TREE_TYPE (orig);
65 /* For memory partitions, copies are OK as long as the memory symbol
66 belongs to the partition. */
67 if (TREE_CODE (dest) == SSA_NAME
68 && TREE_CODE (SSA_NAME_VAR (dest)) == MEMORY_PARTITION_TAG)
69 return (TREE_CODE (orig) == SSA_NAME
70 && !is_gimple_reg (orig)
71 && (SSA_NAME_VAR (dest) == SSA_NAME_VAR (orig)
72 || bitmap_bit_p (MPT_SYMBOLS (SSA_NAME_VAR (dest)),
73 DECL_UID (SSA_NAME_VAR (orig)))));
75 if (TREE_CODE (orig) == SSA_NAME
76 && TREE_CODE (SSA_NAME_VAR (orig)) == MEMORY_PARTITION_TAG)
77 return (TREE_CODE (dest) == SSA_NAME
78 && !is_gimple_reg (dest)
79 && (SSA_NAME_VAR (dest) == SSA_NAME_VAR (orig)
80 || bitmap_bit_p (MPT_SYMBOLS (SSA_NAME_VAR (orig)),
81 DECL_UID (SSA_NAME_VAR (dest)))));
83 /* Do not copy between types for which we *do* need a conversion. */
84 if (!useless_type_conversion_p (type_d, type_o))
85 return false;
87 /* FIXME. GIMPLE is allowing pointer assignments and comparisons of
88 pointers that have different alias sets. This means that these
89 pointers will have different memory tags associated to them.
91 If we allow copy propagation in these cases, statements de-referencing
92 the new pointer will now have a reference to a different memory tag
93 with potentially incorrect SSA information.
95 This was showing up in libjava/java/util/zip/ZipFile.java with code
96 like:
98 struct java.io.BufferedInputStream *T.660;
99 struct java.io.BufferedInputStream *T.647;
100 struct java.io.InputStream *is;
101 struct java.io.InputStream *is.662;
102 [ ... ]
103 T.660 = T.647;
104 is = T.660; <-- This ought to be type-casted
105 is.662 = is;
107 Also, f/name.c exposed a similar problem with a COND_EXPR predicate
108 that was causing DOM to generate and equivalence with two pointers of
109 alias-incompatible types:
111 struct _ffename_space *n;
112 struct _ffename *ns;
113 [ ... ]
114 if (n == ns)
115 goto lab;
117 lab:
118 return n;
120 I think that GIMPLE should emit the appropriate type-casts. For the
121 time being, blocking copy-propagation in these cases is the safe thing
122 to do. */
123 if (TREE_CODE (dest) == SSA_NAME
124 && TREE_CODE (orig) == SSA_NAME
125 && POINTER_TYPE_P (type_d)
126 && POINTER_TYPE_P (type_o))
128 tree mt_dest = symbol_mem_tag (SSA_NAME_VAR (dest));
129 tree mt_orig = symbol_mem_tag (SSA_NAME_VAR (orig));
130 if (mt_dest && mt_orig && mt_dest != mt_orig)
131 return false;
132 else if (get_alias_set (TREE_TYPE (type_d)) !=
133 get_alias_set (TREE_TYPE (type_o)))
134 return false;
136 /* Also verify flow-sensitive information is compatible. */
137 if (SSA_NAME_PTR_INFO (orig) && SSA_NAME_PTR_INFO (dest))
139 struct ptr_info_def *orig_ptr_info = SSA_NAME_PTR_INFO (orig);
140 struct ptr_info_def *dest_ptr_info = SSA_NAME_PTR_INFO (dest);
142 if (orig_ptr_info->name_mem_tag
143 && dest_ptr_info->name_mem_tag
144 && orig_ptr_info->pt_vars
145 && dest_ptr_info->pt_vars
146 && !bitmap_intersect_p (dest_ptr_info->pt_vars,
147 orig_ptr_info->pt_vars))
148 return false;
152 /* If the destination is a SSA_NAME for a virtual operand, then we have
153 some special cases to handle. */
154 if (TREE_CODE (dest) == SSA_NAME && !is_gimple_reg (dest))
156 /* If both operands are SSA_NAMEs referring to virtual operands, then
157 we can always propagate. */
158 if (TREE_CODE (orig) == SSA_NAME
159 && !is_gimple_reg (orig))
160 return true;
162 /* We have a "copy" from something like a constant into a virtual
163 operand. Reject these. */
164 return false;
167 /* If ORIG flows in from an abnormal edge, it cannot be propagated. */
168 if (TREE_CODE (orig) == SSA_NAME
169 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
170 return false;
172 /* If DEST is an SSA_NAME that flows from an abnormal edge, then it
173 cannot be replaced. */
174 if (TREE_CODE (dest) == SSA_NAME
175 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest))
176 return false;
178 /* Anything else is OK. */
179 return true;
182 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
184 bool
185 may_propagate_copy_into_asm (tree dest)
187 /* Hard register operands of asms are special. Do not bypass. */
188 return !(TREE_CODE (dest) == SSA_NAME
189 && TREE_CODE (SSA_NAME_VAR (dest)) == VAR_DECL
190 && DECL_HARD_REGISTER (SSA_NAME_VAR (dest)));
194 /* Given two SSA_NAMEs pointers ORIG and NEW such that we are copy
195 propagating NEW into ORIG, consolidate aliasing information so that
196 they both share the same memory tags. */
198 void
199 merge_alias_info (tree orig_name, tree new_name)
201 tree new_sym = SSA_NAME_VAR (new_name);
202 tree orig_sym = SSA_NAME_VAR (orig_name);
203 var_ann_t new_ann = var_ann (new_sym);
204 var_ann_t orig_ann = var_ann (orig_sym);
206 /* No merging necessary when memory partitions are involved. */
207 if (factoring_name_p (new_name))
209 gcc_assert (!is_gimple_reg (orig_sym));
210 return;
212 else if (factoring_name_p (orig_name))
214 gcc_assert (!is_gimple_reg (new_sym));
215 return;
218 gcc_assert (POINTER_TYPE_P (TREE_TYPE (orig_name))
219 && POINTER_TYPE_P (TREE_TYPE (new_name)));
221 #if defined ENABLE_CHECKING
222 gcc_assert (useless_type_conversion_p (TREE_TYPE (orig_name),
223 TREE_TYPE (new_name)));
225 /* Check that flow-sensitive information is compatible. Notice that
226 we may not merge flow-sensitive information here. This function
227 is called when propagating equivalences dictated by the IL, like
228 a copy operation P_i = Q_j, and from equivalences dictated by
229 control-flow, like if (P_i == Q_j).
231 In the former case, P_i and Q_j are equivalent in every block
232 dominated by the assignment, so their flow-sensitive information
233 is always the same. However, in the latter case, the pointers
234 P_i and Q_j are only equivalent in one of the sub-graphs out of
235 the predicate, so their flow-sensitive information is not the
236 same in every block dominated by the predicate.
238 Since we cannot distinguish one case from another in this
239 function, we can only make sure that if P_i and Q_j have
240 flow-sensitive information, they should be compatible.
242 As callers of merge_alias_info are supposed to call may_propagate_copy
243 first, the following check is redundant. Thus, only do it if checking
244 is enabled. */
245 if (SSA_NAME_PTR_INFO (orig_name) && SSA_NAME_PTR_INFO (new_name))
247 struct ptr_info_def *orig_ptr_info = SSA_NAME_PTR_INFO (orig_name);
248 struct ptr_info_def *new_ptr_info = SSA_NAME_PTR_INFO (new_name);
250 /* Note that pointer NEW and ORIG may actually have different
251 pointed-to variables (e.g., PR 18291 represented in
252 testsuite/gcc.c-torture/compile/pr18291.c). However, since
253 NEW is being copy-propagated into ORIG, it must always be
254 true that the pointed-to set for pointer NEW is the same, or
255 a subset, of the pointed-to set for pointer ORIG. If this
256 isn't the case, we shouldn't have been able to do the
257 propagation of NEW into ORIG. */
258 if (orig_ptr_info->name_mem_tag
259 && new_ptr_info->name_mem_tag
260 && orig_ptr_info->pt_vars
261 && new_ptr_info->pt_vars)
262 gcc_assert (bitmap_intersect_p (new_ptr_info->pt_vars,
263 orig_ptr_info->pt_vars));
265 #endif
267 /* Synchronize the symbol tags. If both pointers had a tag and they
268 are different, then something has gone wrong. Symbol tags can
269 always be merged because they are flow insensitive, all the SSA
270 names of the same base DECL share the same symbol tag. */
271 if (new_ann->symbol_mem_tag == NULL_TREE)
272 new_ann->symbol_mem_tag = orig_ann->symbol_mem_tag;
273 else if (orig_ann->symbol_mem_tag == NULL_TREE)
274 orig_ann->symbol_mem_tag = new_ann->symbol_mem_tag;
275 else
276 gcc_assert (new_ann->symbol_mem_tag == orig_ann->symbol_mem_tag);
278 /* Copy flow-sensitive alias information in case that NEW_NAME
279 didn't get a NMT but was set to pt_anything for optimization
280 purposes. In case ORIG_NAME has a NMT we can safely use its
281 flow-sensitive alias information as a conservative estimate. */
282 if (SSA_NAME_PTR_INFO (orig_name)
283 && SSA_NAME_PTR_INFO (orig_name)->name_mem_tag
284 && (!SSA_NAME_PTR_INFO (new_name)
285 || !SSA_NAME_PTR_INFO (new_name)->name_mem_tag))
287 struct ptr_info_def *orig_ptr_info = SSA_NAME_PTR_INFO (orig_name);
288 struct ptr_info_def *new_ptr_info = get_ptr_info (new_name);
289 memcpy (new_ptr_info, orig_ptr_info, sizeof (struct ptr_info_def));
294 /* Common code for propagate_value and replace_exp.
296 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
297 replacement is done to propagate a value or not. */
299 static void
300 replace_exp_1 (use_operand_p op_p, tree val,
301 bool for_propagation ATTRIBUTE_UNUSED)
303 tree op = USE_FROM_PTR (op_p);
305 #if defined ENABLE_CHECKING
306 gcc_assert (!(for_propagation
307 && TREE_CODE (op) == SSA_NAME
308 && TREE_CODE (val) == SSA_NAME
309 && !may_propagate_copy (op, val)));
310 #endif
312 if (TREE_CODE (val) == SSA_NAME)
314 if (TREE_CODE (op) == SSA_NAME && POINTER_TYPE_P (TREE_TYPE (op)))
315 merge_alias_info (op, val);
316 SET_USE (op_p, val);
318 else
319 SET_USE (op_p, unsave_expr_now (val));
323 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
324 into the operand pointed to by OP_P.
326 Use this version for const/copy propagation as it will perform additional
327 checks to ensure validity of the const/copy propagation. */
329 void
330 propagate_value (use_operand_p op_p, tree val)
332 replace_exp_1 (op_p, val, true);
336 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
337 into the tree pointed to by OP_P.
339 Use this version for const/copy propagation when SSA operands are not
340 available. It will perform the additional checks to ensure validity of
341 the const/copy propagation, but will not update any operand information.
342 Be sure to mark the stmt as modified. */
344 void
345 propagate_tree_value (tree *op_p, tree val)
347 #if defined ENABLE_CHECKING
348 gcc_assert (!(TREE_CODE (val) == SSA_NAME
349 && TREE_CODE (*op_p) == SSA_NAME
350 && !may_propagate_copy (*op_p, val)));
351 #endif
353 if (TREE_CODE (val) == SSA_NAME)
355 if (TREE_CODE (*op_p) == SSA_NAME && POINTER_TYPE_P (TREE_TYPE (*op_p)))
356 merge_alias_info (*op_p, val);
357 *op_p = val;
359 else
360 *op_p = unsave_expr_now (val);
364 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
366 Use this version when not const/copy propagating values. For example,
367 PRE uses this version when building expressions as they would appear
368 in specific blocks taking into account actions of PHI nodes. */
370 void
371 replace_exp (use_operand_p op_p, tree val)
373 replace_exp_1 (op_p, val, false);
377 /*---------------------------------------------------------------------------
378 Copy propagation
379 ---------------------------------------------------------------------------*/
380 /* During propagation, we keep chains of variables that are copies of
381 one another. If variable X_i is a copy of X_j and X_j is a copy of
382 X_k, COPY_OF will contain:
384 COPY_OF[i].VALUE = X_j
385 COPY_OF[j].VALUE = X_k
386 COPY_OF[k].VALUE = X_k
388 After propagation, the copy-of value for each variable X_i is
389 converted into the final value by walking the copy-of chains and
390 updating COPY_OF[i].VALUE to be the last element of the chain. */
391 static prop_value_t *copy_of;
393 /* Used in set_copy_of_val to determine if the last link of a copy-of
394 chain has changed. */
395 static tree *cached_last_copy_of;
398 /* Return true if this statement may generate a useful copy. */
400 static bool
401 stmt_may_generate_copy (tree stmt)
403 tree lhs, rhs;
404 stmt_ann_t ann;
406 if (TREE_CODE (stmt) == PHI_NODE)
407 return !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (stmt));
409 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
410 return false;
412 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
413 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
414 ann = stmt_ann (stmt);
416 /* If the statement has volatile operands, it won't generate a
417 useful copy. */
418 if (ann->has_volatile_ops)
419 return false;
421 /* Statements with loads and/or stores will never generate a useful copy. */
422 if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
423 return false;
425 /* Otherwise, the only statements that generate useful copies are
426 assignments whose RHS is just an SSA name that doesn't flow
427 through abnormal edges. */
428 return (TREE_CODE (rhs) == SSA_NAME
429 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs));
433 /* Return the copy-of value for VAR. */
435 static inline prop_value_t *
436 get_copy_of_val (tree var)
438 prop_value_t *val = &copy_of[SSA_NAME_VERSION (var)];
440 if (val->value == NULL_TREE
441 && !stmt_may_generate_copy (SSA_NAME_DEF_STMT (var)))
443 /* If the variable will never generate a useful copy relation,
444 make it its own copy. */
445 val->value = var;
448 return val;
452 /* Return last link in the copy-of chain for VAR. */
454 static tree
455 get_last_copy_of (tree var)
457 tree last;
458 int i;
460 /* Traverse COPY_OF starting at VAR until we get to the last
461 link in the chain. Since it is possible to have cycles in PHI
462 nodes, the copy-of chain may also contain cycles.
464 To avoid infinite loops and to avoid traversing lengthy copy-of
465 chains, we artificially limit the maximum number of chains we are
466 willing to traverse.
468 The value 5 was taken from a compiler and runtime library
469 bootstrap and a mixture of C and C++ code from various sources.
470 More than 82% of all copy-of chains were shorter than 5 links. */
471 #define LIMIT 5
473 last = var;
474 for (i = 0; i < LIMIT; i++)
476 tree copy = copy_of[SSA_NAME_VERSION (last)].value;
477 if (copy == NULL_TREE || copy == last)
478 break;
479 last = copy;
482 /* If we have reached the limit, then we are either in a copy-of
483 cycle or the copy-of chain is too long. In this case, just
484 return VAR so that it is not considered a copy of anything. */
485 return (i < LIMIT ? last : var);
489 /* Set FIRST to be the first variable in the copy-of chain for DEST.
490 If DEST's copy-of value or its copy-of chain has changed, return
491 true.
493 MEM_REF is the memory reference where FIRST is stored. This is
494 used when DEST is a non-register and we are copy propagating loads
495 and stores. */
497 static inline bool
498 set_copy_of_val (tree dest, tree first)
500 unsigned int dest_ver = SSA_NAME_VERSION (dest);
501 tree old_first, old_last, new_last;
503 /* Set FIRST to be the first link in COPY_OF[DEST]. If that
504 changed, return true. */
505 old_first = copy_of[dest_ver].value;
506 copy_of[dest_ver].value = first;
508 if (old_first != first)
509 return true;
511 /* If FIRST and OLD_FIRST are the same, we need to check whether the
512 copy-of chain starting at FIRST ends in a different variable. If
513 the copy-of chain starting at FIRST ends up in a different
514 variable than the last cached value we had for DEST, then return
515 true because DEST is now a copy of a different variable.
517 This test is necessary because even though the first link in the
518 copy-of chain may not have changed, if any of the variables in
519 the copy-of chain changed its final value, DEST will now be the
520 copy of a different variable, so we have to do another round of
521 propagation for everything that depends on DEST. */
522 old_last = cached_last_copy_of[dest_ver];
523 new_last = get_last_copy_of (dest);
524 cached_last_copy_of[dest_ver] = new_last;
526 return (old_last != new_last);
530 /* Dump the copy-of value for variable VAR to FILE. */
532 static void
533 dump_copy_of (FILE *file, tree var)
535 tree val;
536 sbitmap visited;
538 print_generic_expr (file, var, dump_flags);
540 if (TREE_CODE (var) != SSA_NAME)
541 return;
543 visited = sbitmap_alloc (num_ssa_names);
544 sbitmap_zero (visited);
545 SET_BIT (visited, SSA_NAME_VERSION (var));
547 fprintf (file, " copy-of chain: ");
549 val = var;
550 print_generic_expr (file, val, 0);
551 fprintf (file, " ");
552 while (copy_of[SSA_NAME_VERSION (val)].value)
554 fprintf (file, "-> ");
555 val = copy_of[SSA_NAME_VERSION (val)].value;
556 print_generic_expr (file, val, 0);
557 fprintf (file, " ");
558 if (TEST_BIT (visited, SSA_NAME_VERSION (val)))
559 break;
560 SET_BIT (visited, SSA_NAME_VERSION (val));
563 val = get_copy_of_val (var)->value;
564 if (val == NULL_TREE)
565 fprintf (file, "[UNDEFINED]");
566 else if (val != var)
567 fprintf (file, "[COPY]");
568 else
569 fprintf (file, "[NOT A COPY]");
571 sbitmap_free (visited);
575 /* Evaluate the RHS of STMT. If it produces a valid copy, set the LHS
576 value and store the LHS into *RESULT_P. If STMT generates more
577 than one name (i.e., STMT is an aliased store), it is enough to
578 store the first name in the VDEF list into *RESULT_P. After
579 all, the names generated will be VUSEd in the same statements. */
581 static enum ssa_prop_result
582 copy_prop_visit_assignment (tree stmt, tree *result_p)
584 tree lhs, rhs;
585 prop_value_t *rhs_val;
587 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
588 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
590 gcc_assert (TREE_CODE (rhs) == SSA_NAME);
592 rhs_val = get_copy_of_val (rhs);
594 if (TREE_CODE (lhs) == SSA_NAME)
596 /* Straight copy between two SSA names. First, make sure that
597 we can propagate the RHS into uses of LHS. */
598 if (!may_propagate_copy (lhs, rhs))
599 return SSA_PROP_VARYING;
601 /* Notice that in the case of assignments, we make the LHS be a
602 copy of RHS's value, not of RHS itself. This avoids keeping
603 unnecessary copy-of chains (assignments cannot be in a cycle
604 like PHI nodes), speeding up the propagation process.
605 This is different from what we do in copy_prop_visit_phi_node.
606 In those cases, we are interested in the copy-of chains. */
607 *result_p = lhs;
608 if (set_copy_of_val (*result_p, rhs_val->value))
609 return SSA_PROP_INTERESTING;
610 else
611 return SSA_PROP_NOT_INTERESTING;
614 return SSA_PROP_VARYING;
618 /* Visit the COND_EXPR STMT. Return SSA_PROP_INTERESTING
619 if it can determine which edge will be taken. Otherwise, return
620 SSA_PROP_VARYING. */
622 static enum ssa_prop_result
623 copy_prop_visit_cond_stmt (tree stmt, edge *taken_edge_p)
625 enum ssa_prop_result retval;
626 tree cond;
628 cond = COND_EXPR_COND (stmt);
629 retval = SSA_PROP_VARYING;
631 /* The only conditionals that we may be able to compute statically
632 are predicates involving two SSA_NAMEs. */
633 if (COMPARISON_CLASS_P (cond)
634 && TREE_CODE (TREE_OPERAND (cond, 0)) == SSA_NAME
635 && TREE_CODE (TREE_OPERAND (cond, 1)) == SSA_NAME)
637 tree op0 = get_last_copy_of (TREE_OPERAND (cond, 0));
638 tree op1 = get_last_copy_of (TREE_OPERAND (cond, 1));
640 /* See if we can determine the predicate's value. */
641 if (dump_file && (dump_flags & TDF_DETAILS))
643 fprintf (dump_file, "Trying to determine truth value of ");
644 fprintf (dump_file, "predicate ");
645 print_generic_stmt (dump_file, cond, 0);
648 /* We can fold COND and get a useful result only when we have
649 the same SSA_NAME on both sides of a comparison operator. */
650 if (op0 == op1)
652 tree folded_cond = fold_binary (TREE_CODE (cond), boolean_type_node,
653 op0, op1);
654 if (folded_cond)
656 basic_block bb = bb_for_stmt (stmt);
657 *taken_edge_p = find_taken_edge (bb, folded_cond);
658 if (*taken_edge_p)
659 retval = SSA_PROP_INTERESTING;
664 if (dump_file && (dump_flags & TDF_DETAILS) && *taken_edge_p)
665 fprintf (dump_file, "\nConditional will always take edge %d->%d\n",
666 (*taken_edge_p)->src->index, (*taken_edge_p)->dest->index);
668 return retval;
672 /* Evaluate statement STMT. If the statement produces a new output
673 value, return SSA_PROP_INTERESTING and store the SSA_NAME holding
674 the new value in *RESULT_P.
676 If STMT is a conditional branch and we can determine its truth
677 value, set *TAKEN_EDGE_P accordingly.
679 If the new value produced by STMT is varying, return
680 SSA_PROP_VARYING. */
682 static enum ssa_prop_result
683 copy_prop_visit_stmt (tree stmt, edge *taken_edge_p, tree *result_p)
685 enum ssa_prop_result retval;
687 if (dump_file && (dump_flags & TDF_DETAILS))
689 fprintf (dump_file, "\nVisiting statement:\n");
690 print_generic_stmt (dump_file, stmt, dump_flags);
691 fprintf (dump_file, "\n");
694 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
695 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == SSA_NAME
696 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME)
698 /* If the statement is a copy assignment, evaluate its RHS to
699 see if the lattice value of its output has changed. */
700 retval = copy_prop_visit_assignment (stmt, result_p);
702 else if (TREE_CODE (stmt) == COND_EXPR)
704 /* See if we can determine which edge goes out of a conditional
705 jump. */
706 retval = copy_prop_visit_cond_stmt (stmt, taken_edge_p);
708 else
709 retval = SSA_PROP_VARYING;
711 if (retval == SSA_PROP_VARYING)
713 tree def;
714 ssa_op_iter i;
716 /* Any other kind of statement is not interesting for constant
717 propagation and, therefore, not worth simulating. */
718 if (dump_file && (dump_flags & TDF_DETAILS))
719 fprintf (dump_file, "No interesting values produced.\n");
721 /* The assignment is not a copy operation. Don't visit this
722 statement again and mark all the definitions in the statement
723 to be copies of nothing. */
724 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_ALL_DEFS)
725 set_copy_of_val (def, def);
728 return retval;
732 /* Visit PHI node PHI. If all the arguments produce the same value,
733 set it to be the value of the LHS of PHI. */
735 static enum ssa_prop_result
736 copy_prop_visit_phi_node (tree phi)
738 enum ssa_prop_result retval;
739 int i;
740 tree lhs;
741 prop_value_t phi_val = { 0, NULL_TREE, NULL_TREE };
743 lhs = PHI_RESULT (phi);
745 if (dump_file && (dump_flags & TDF_DETAILS))
747 fprintf (dump_file, "\nVisiting PHI node: ");
748 print_generic_expr (dump_file, phi, dump_flags);
749 fprintf (dump_file, "\n\n");
752 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
754 prop_value_t *arg_val;
755 tree arg = PHI_ARG_DEF (phi, i);
756 edge e = PHI_ARG_EDGE (phi, i);
758 /* We don't care about values flowing through non-executable
759 edges. */
760 if (!(e->flags & EDGE_EXECUTABLE))
761 continue;
763 /* Constants in the argument list never generate a useful copy.
764 Similarly, names that flow through abnormal edges cannot be
765 used to derive copies. */
766 if (TREE_CODE (arg) != SSA_NAME || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg))
768 phi_val.value = lhs;
769 break;
772 /* Avoid copy propagation from an inner into an outer loop.
773 Otherwise, this may move loop variant variables outside of
774 their loops and prevent coalescing opportunities. If the
775 value was loop invariant, it will be hoisted by LICM and
776 exposed for copy propagation. */
777 if (loop_depth_of_name (arg) > loop_depth_of_name (lhs))
779 phi_val.value = lhs;
780 break;
783 /* If the LHS appears in the argument list, ignore it. It is
784 irrelevant as a copy. */
785 if (arg == lhs || get_last_copy_of (arg) == lhs)
786 continue;
788 if (dump_file && (dump_flags & TDF_DETAILS))
790 fprintf (dump_file, "\tArgument #%d: ", i);
791 dump_copy_of (dump_file, arg);
792 fprintf (dump_file, "\n");
795 arg_val = get_copy_of_val (arg);
797 /* If the LHS didn't have a value yet, make it a copy of the
798 first argument we find. Notice that while we make the LHS be
799 a copy of the argument itself, we take the memory reference
800 from the argument's value so that we can compare it to the
801 memory reference of all the other arguments. */
802 if (phi_val.value == NULL_TREE)
804 phi_val.value = arg;
805 continue;
808 /* If PHI_VAL and ARG don't have a common copy-of chain, then
809 this PHI node cannot be a copy operation. Also, if we are
810 copy propagating stores and these two arguments came from
811 different memory references, they cannot be considered
812 copies. */
813 if (get_last_copy_of (phi_val.value) != get_last_copy_of (arg))
815 phi_val.value = lhs;
816 break;
820 if (phi_val.value && set_copy_of_val (lhs, phi_val.value))
821 retval = (phi_val.value != lhs) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
822 else
823 retval = SSA_PROP_NOT_INTERESTING;
825 if (dump_file && (dump_flags & TDF_DETAILS))
827 fprintf (dump_file, "\nPHI node ");
828 dump_copy_of (dump_file, lhs);
829 fprintf (dump_file, "\nTelling the propagator to ");
830 if (retval == SSA_PROP_INTERESTING)
831 fprintf (dump_file, "add SSA edges out of this PHI and continue.");
832 else if (retval == SSA_PROP_VARYING)
833 fprintf (dump_file, "add SSA edges out of this PHI and never visit again.");
834 else
835 fprintf (dump_file, "do nothing with SSA edges and keep iterating.");
836 fprintf (dump_file, "\n\n");
839 return retval;
843 /* Initialize structures used for copy propagation. PHIS_ONLY is true
844 if we should only consider PHI nodes as generating copy propagation
845 opportunities. */
847 static void
848 init_copy_prop (void)
850 basic_block bb;
852 copy_of = XCNEWVEC (prop_value_t, num_ssa_names);
854 cached_last_copy_of = XCNEWVEC (tree, num_ssa_names);
856 FOR_EACH_BB (bb)
858 block_stmt_iterator si;
859 tree phi, def;
860 int depth = bb->loop_depth;
862 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
864 tree stmt = bsi_stmt (si);
865 ssa_op_iter iter;
867 /* The only statements that we care about are those that may
868 generate useful copies. We also need to mark conditional
869 jumps so that their outgoing edges are added to the work
870 lists of the propagator.
872 Avoid copy propagation from an inner into an outer loop.
873 Otherwise, this may move loop variant variables outside of
874 their loops and prevent coalescing opportunities. If the
875 value was loop invariant, it will be hoisted by LICM and
876 exposed for copy propagation. */
877 if (stmt_ends_bb_p (stmt))
878 DONT_SIMULATE_AGAIN (stmt) = false;
879 else if (stmt_may_generate_copy (stmt)
880 && loop_depth_of_name (GIMPLE_STMT_OPERAND (stmt, 1)) <= depth)
881 DONT_SIMULATE_AGAIN (stmt) = false;
882 else
883 DONT_SIMULATE_AGAIN (stmt) = true;
885 /* Mark all the outputs of this statement as not being
886 the copy of anything. */
887 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
888 if (DONT_SIMULATE_AGAIN (stmt))
889 set_copy_of_val (def, def);
890 else
891 cached_last_copy_of[SSA_NAME_VERSION (def)] = def;
894 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
896 def = PHI_RESULT (phi);
897 if (!is_gimple_reg (def))
898 DONT_SIMULATE_AGAIN (phi) = true;
899 else
900 DONT_SIMULATE_AGAIN (phi) = false;
902 if (DONT_SIMULATE_AGAIN (phi))
903 set_copy_of_val (def, def);
904 else
905 cached_last_copy_of[SSA_NAME_VERSION (def)] = def;
911 /* Deallocate memory used in copy propagation and do final
912 substitution. */
914 static void
915 fini_copy_prop (void)
917 size_t i;
918 prop_value_t *tmp;
920 /* Set the final copy-of value for each variable by traversing the
921 copy-of chains. */
922 tmp = XCNEWVEC (prop_value_t, num_ssa_names);
923 for (i = 1; i < num_ssa_names; i++)
925 tree var = ssa_name (i);
926 if (var && copy_of[i].value && copy_of[i].value != var)
927 tmp[i].value = get_last_copy_of (var);
930 substitute_and_fold (tmp, false);
932 free (cached_last_copy_of);
933 free (copy_of);
934 free (tmp);
938 /* Main entry point to the copy propagator.
940 PHIS_ONLY is true if we should only consider PHI nodes as generating
941 copy propagation opportunities.
943 The algorithm propagates the value COPY-OF using ssa_propagate. For
944 every variable X_i, COPY-OF(X_i) indicates which variable is X_i created
945 from. The following example shows how the algorithm proceeds at a
946 high level:
948 1 a_24 = x_1
949 2 a_2 = PHI <a_24, x_1>
950 3 a_5 = PHI <a_2>
951 4 x_1 = PHI <x_298, a_5, a_2>
953 The end result should be that a_2, a_5, a_24 and x_1 are a copy of
954 x_298. Propagation proceeds as follows.
956 Visit #1: a_24 is copy-of x_1. Value changed.
957 Visit #2: a_2 is copy-of x_1. Value changed.
958 Visit #3: a_5 is copy-of x_1. Value changed.
959 Visit #4: x_1 is copy-of x_298. Value changed.
960 Visit #1: a_24 is copy-of x_298. Value changed.
961 Visit #2: a_2 is copy-of x_298. Value changed.
962 Visit #3: a_5 is copy-of x_298. Value changed.
963 Visit #4: x_1 is copy-of x_298. Stable state reached.
965 When visiting PHI nodes, we only consider arguments that flow
966 through edges marked executable by the propagation engine. So,
967 when visiting statement #2 for the first time, we will only look at
968 the first argument (a_24) and optimistically assume that its value
969 is the copy of a_24 (x_1).
971 The problem with this approach is that it may fail to discover copy
972 relations in PHI cycles. Instead of propagating copy-of
973 values, we actually propagate copy-of chains. For instance:
975 A_3 = B_1;
976 C_9 = A_3;
977 D_4 = C_9;
978 X_i = D_4;
980 In this code fragment, COPY-OF (X_i) = { D_4, C_9, A_3, B_1 }.
981 Obviously, we are only really interested in the last value of the
982 chain, however the propagator needs to access the copy-of chain
983 when visiting PHI nodes.
985 To represent the copy-of chain, we use the array COPY_CHAINS, which
986 holds the first link in the copy-of chain for every variable.
987 If variable X_i is a copy of X_j, which in turn is a copy of X_k,
988 the array will contain:
990 COPY_CHAINS[i] = X_j
991 COPY_CHAINS[j] = X_k
992 COPY_CHAINS[k] = X_k
994 Keeping copy-of chains instead of copy-of values directly becomes
995 important when visiting PHI nodes. Suppose that we had the
996 following PHI cycle, such that x_52 is already considered a copy of
997 x_53:
999 1 x_54 = PHI <x_53, x_52>
1000 2 x_53 = PHI <x_898, x_54>
1002 Visit #1: x_54 is copy-of x_53 (because x_52 is copy-of x_53)
1003 Visit #2: x_53 is copy-of x_898 (because x_54 is a copy of x_53,
1004 so it is considered irrelevant
1005 as a copy).
1006 Visit #1: x_54 is copy-of nothing (x_53 is a copy-of x_898 and
1007 x_52 is a copy of x_53, so
1008 they don't match)
1009 Visit #2: x_53 is copy-of nothing
1011 This problem is avoided by keeping a chain of copies, instead of
1012 the final copy-of value. Propagation will now only keep the first
1013 element of a variable's copy-of chain. When visiting PHI nodes,
1014 arguments are considered equal if their copy-of chains end in the
1015 same variable. So, as long as their copy-of chains overlap, we
1016 know that they will be a copy of the same variable, regardless of
1017 which variable that may be).
1019 Propagation would then proceed as follows (the notation a -> b
1020 means that a is a copy-of b):
1022 Visit #1: x_54 = PHI <x_53, x_52>
1023 x_53 -> x_53
1024 x_52 -> x_53
1025 Result: x_54 -> x_53. Value changed. Add SSA edges.
1027 Visit #1: x_53 = PHI <x_898, x_54>
1028 x_898 -> x_898
1029 x_54 -> x_53
1030 Result: x_53 -> x_898. Value changed. Add SSA edges.
1032 Visit #2: x_54 = PHI <x_53, x_52>
1033 x_53 -> x_898
1034 x_52 -> x_53 -> x_898
1035 Result: x_54 -> x_898. Value changed. Add SSA edges.
1037 Visit #2: x_53 = PHI <x_898, x_54>
1038 x_898 -> x_898
1039 x_54 -> x_898
1040 Result: x_53 -> x_898. Value didn't change. Stable state
1042 Once the propagator stabilizes, we end up with the desired result
1043 x_53 and x_54 are both copies of x_898. */
1045 static unsigned int
1046 execute_copy_prop (void)
1048 init_copy_prop ();
1049 ssa_propagate (copy_prop_visit_stmt, copy_prop_visit_phi_node);
1050 fini_copy_prop ();
1051 return 0;
1054 static bool
1055 gate_copy_prop (void)
1057 return flag_tree_copy_prop != 0;
1060 struct tree_opt_pass pass_copy_prop =
1062 "copyprop", /* name */
1063 gate_copy_prop, /* gate */
1064 execute_copy_prop, /* execute */
1065 NULL, /* sub */
1066 NULL, /* next */
1067 0, /* static_pass_number */
1068 TV_TREE_COPY_PROP, /* tv_id */
1069 PROP_ssa | PROP_cfg, /* properties_required */
1070 0, /* properties_provided */
1071 0, /* properties_destroyed */
1072 0, /* todo_flags_start */
1073 TODO_cleanup_cfg
1074 | TODO_dump_func
1075 | TODO_ggc_collect
1076 | TODO_verify_ssa
1077 | TODO_update_ssa, /* todo_flags_finish */
1078 0 /* letter */