1 /* Copy propagation and SSA_NAME replacement support routines.
2 Copyright (C) 2004, 2005 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 2, or (at your option)
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 COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
23 #include "coretypes.h"
30 #include "basic-block.h"
34 #include "diagnostic.h"
36 #include "tree-dump.h"
37 #include "tree-flow.h"
38 #include "tree-pass.h"
39 #include "tree-ssa-propagate.h"
40 #include "langhooks.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
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
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. */
61 may_propagate_copy (tree dest
, tree orig
)
63 tree type_d
= TREE_TYPE (dest
);
64 tree type_o
= TREE_TYPE (orig
);
66 /* Do not copy between types for which we *do* need a conversion. */
67 if (!tree_ssa_useless_type_conversion_1 (type_d
, type_o
))
70 /* FIXME. GIMPLE is allowing pointer assignments and comparisons of
71 pointers that have different alias sets. This means that these
72 pointers will have different memory tags associated to them.
74 If we allow copy propagation in these cases, statements de-referencing
75 the new pointer will now have a reference to a different memory tag
76 with potentially incorrect SSA information.
78 This was showing up in libjava/java/util/zip/ZipFile.java with code
81 struct java.io.BufferedInputStream *T.660;
82 struct java.io.BufferedInputStream *T.647;
83 struct java.io.InputStream *is;
84 struct java.io.InputStream *is.662;
87 is = T.660; <-- This ought to be type-casted
90 Also, f/name.c exposed a similar problem with a COND_EXPR predicate
91 that was causing DOM to generate and equivalence with two pointers of
92 alias-incompatible types:
94 struct _ffename_space *n;
103 I think that GIMPLE should emit the appropriate type-casts. For the
104 time being, blocking copy-propagation in these cases is the safe thing
106 if (TREE_CODE (dest
) == SSA_NAME
107 && TREE_CODE (orig
) == SSA_NAME
108 && POINTER_TYPE_P (type_d
)
109 && POINTER_TYPE_P (type_o
))
111 tree mt_dest
= var_ann (SSA_NAME_VAR (dest
))->symbol_mem_tag
;
112 tree mt_orig
= var_ann (SSA_NAME_VAR (orig
))->symbol_mem_tag
;
113 if (mt_dest
&& mt_orig
&& mt_dest
!= mt_orig
)
115 else if (!lang_hooks
.types_compatible_p (type_d
, type_o
))
117 else if (get_alias_set (TREE_TYPE (type_d
)) !=
118 get_alias_set (TREE_TYPE (type_o
)))
121 /* Also verify flow-sensitive information is compatible. */
122 if (SSA_NAME_PTR_INFO (orig
) && SSA_NAME_PTR_INFO (dest
))
124 struct ptr_info_def
*orig_ptr_info
= SSA_NAME_PTR_INFO (orig
);
125 struct ptr_info_def
*dest_ptr_info
= SSA_NAME_PTR_INFO (dest
);
127 if (orig_ptr_info
->name_mem_tag
128 && dest_ptr_info
->name_mem_tag
129 && orig_ptr_info
->pt_vars
130 && dest_ptr_info
->pt_vars
131 && !bitmap_intersect_p (dest_ptr_info
->pt_vars
,
132 orig_ptr_info
->pt_vars
))
137 /* If the destination is a SSA_NAME for a virtual operand, then we have
138 some special cases to handle. */
139 if (TREE_CODE (dest
) == SSA_NAME
&& !is_gimple_reg (dest
))
141 /* If both operands are SSA_NAMEs referring to virtual operands, then
142 we can always propagate. */
143 if (TREE_CODE (orig
) == SSA_NAME
144 && !is_gimple_reg (orig
))
147 /* We have a "copy" from something like a constant into a virtual
148 operand. Reject these. */
152 /* If ORIG flows in from an abnormal edge, it cannot be propagated. */
153 if (TREE_CODE (orig
) == SSA_NAME
154 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig
))
157 /* If DEST is an SSA_NAME that flows from an abnormal edge, then it
158 cannot be replaced. */
159 if (TREE_CODE (dest
) == SSA_NAME
160 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest
))
163 /* Anything else is OK. */
167 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
170 may_propagate_copy_into_asm (tree dest
)
172 /* Hard register operands of asms are special. Do not bypass. */
173 return !(TREE_CODE (dest
) == SSA_NAME
174 && TREE_CODE (SSA_NAME_VAR (dest
)) == VAR_DECL
175 && DECL_HARD_REGISTER (SSA_NAME_VAR (dest
)));
179 /* Given two SSA_NAMEs pointers ORIG and NEW such that we are copy
180 propagating NEW into ORIG, consolidate aliasing information so that
181 they both share the same memory tags. */
184 merge_alias_info (tree orig
, tree
new)
186 tree new_sym
= SSA_NAME_VAR (new);
187 tree orig_sym
= SSA_NAME_VAR (orig
);
188 var_ann_t new_ann
= var_ann (new_sym
);
189 var_ann_t orig_ann
= var_ann (orig_sym
);
191 gcc_assert (POINTER_TYPE_P (TREE_TYPE (orig
)));
192 gcc_assert (POINTER_TYPE_P (TREE_TYPE (new)));
194 #if defined ENABLE_CHECKING
195 gcc_assert (lang_hooks
.types_compatible_p (TREE_TYPE (orig
),
198 /* If the pointed-to alias sets are different, these two pointers
199 would never have the same memory tag. In this case, NEW should
200 not have been propagated into ORIG. */
201 gcc_assert (get_alias_set (TREE_TYPE (TREE_TYPE (new_sym
)))
202 == get_alias_set (TREE_TYPE (TREE_TYPE (orig_sym
))));
205 /* Synchronize the symbol tags. If both pointers had a tag and they
206 are different, then something has gone wrong. Symbol tags can
207 always be merged because they are flow insensitive, all the SSA
208 names of the same base DECL share the same symbol tag. */
209 if (new_ann
->symbol_mem_tag
== NULL_TREE
)
210 new_ann
->symbol_mem_tag
= orig_ann
->symbol_mem_tag
;
211 else if (orig_ann
->symbol_mem_tag
== NULL_TREE
)
212 orig_ann
->symbol_mem_tag
= new_ann
->symbol_mem_tag
;
214 gcc_assert (new_ann
->symbol_mem_tag
== orig_ann
->symbol_mem_tag
);
216 /* Check that flow-sensitive information is compatible. Notice that
217 we may not merge flow-sensitive information here. This function
218 is called when propagating equivalences dictated by the IL, like
219 a copy operation P_i = Q_j, and from equivalences dictated by
220 control-flow, like if (P_i == Q_j).
222 In the former case, P_i and Q_j are equivalent in every block
223 dominated by the assignment, so their flow-sensitive information
224 is always the same. However, in the latter case, the pointers
225 P_i and Q_j are only equivalent in one of the sub-graphs out of
226 the predicate, so their flow-sensitive information is not the
227 same in every block dominated by the predicate.
229 Since we cannot distinguish one case from another in this
230 function, we can only make sure that if P_i and Q_j have
231 flow-sensitive information, they should be compatible. */
232 if (SSA_NAME_PTR_INFO (orig
) && SSA_NAME_PTR_INFO (new))
234 struct ptr_info_def
*orig_ptr_info
= SSA_NAME_PTR_INFO (orig
);
235 struct ptr_info_def
*new_ptr_info
= SSA_NAME_PTR_INFO (new);
237 /* Note that pointer NEW and ORIG may actually have different
238 pointed-to variables (e.g., PR 18291 represented in
239 testsuite/gcc.c-torture/compile/pr18291.c). However, since
240 NEW is being copy-propagated into ORIG, it must always be
241 true that the pointed-to set for pointer NEW is the same, or
242 a subset, of the pointed-to set for pointer ORIG. If this
243 isn't the case, we shouldn't have been able to do the
244 propagation of NEW into ORIG. */
245 if (orig_ptr_info
->name_mem_tag
246 && new_ptr_info
->name_mem_tag
247 && orig_ptr_info
->pt_vars
248 && new_ptr_info
->pt_vars
)
249 gcc_assert (bitmap_intersect_p (new_ptr_info
->pt_vars
,
250 orig_ptr_info
->pt_vars
));
255 /* Common code for propagate_value and replace_exp.
257 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
258 replacement is done to propagate a value or not. */
261 replace_exp_1 (use_operand_p op_p
, tree val
,
262 bool for_propagation ATTRIBUTE_UNUSED
)
264 tree op
= USE_FROM_PTR (op_p
);
266 #if defined ENABLE_CHECKING
267 gcc_assert (!(for_propagation
268 && TREE_CODE (op
) == SSA_NAME
269 && TREE_CODE (val
) == SSA_NAME
270 && !may_propagate_copy (op
, val
)));
273 if (TREE_CODE (val
) == SSA_NAME
)
275 if (TREE_CODE (op
) == SSA_NAME
&& POINTER_TYPE_P (TREE_TYPE (op
)))
276 merge_alias_info (op
, val
);
280 SET_USE (op_p
, unsave_expr_now (val
));
284 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
285 into the operand pointed to by OP_P.
287 Use this version for const/copy propagation as it will perform additional
288 checks to ensure validity of the const/copy propagation. */
291 propagate_value (use_operand_p op_p
, tree val
)
293 replace_exp_1 (op_p
, val
, true);
297 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
298 into the tree pointed to by OP_P.
300 Use this version for const/copy propagation when SSA operands are not
301 available. It will perform the additional checks to ensure validity of
302 the const/copy propagation, but will not update any operand information.
303 Be sure to mark the stmt as modified. */
306 propagate_tree_value (tree
*op_p
, tree val
)
308 #if defined ENABLE_CHECKING
309 gcc_assert (!(TREE_CODE (val
) == SSA_NAME
310 && TREE_CODE (*op_p
) == SSA_NAME
311 && !may_propagate_copy (*op_p
, val
)));
314 if (TREE_CODE (val
) == SSA_NAME
)
316 if (TREE_CODE (*op_p
) == SSA_NAME
&& POINTER_TYPE_P (TREE_TYPE (*op_p
)))
317 merge_alias_info (*op_p
, val
);
321 *op_p
= unsave_expr_now (val
);
325 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
327 Use this version when not const/copy propagating values. For example,
328 PRE uses this version when building expressions as they would appear
329 in specific blocks taking into account actions of PHI nodes. */
332 replace_exp (use_operand_p op_p
, tree val
)
334 replace_exp_1 (op_p
, val
, false);
338 /*---------------------------------------------------------------------------
340 ---------------------------------------------------------------------------*/
341 /* During propagation, we keep chains of variables that are copies of
342 one another. If variable X_i is a copy of X_j and X_j is a copy of
343 X_k, COPY_OF will contain:
345 COPY_OF[i].VALUE = X_j
346 COPY_OF[j].VALUE = X_k
347 COPY_OF[k].VALUE = X_k
349 After propagation, the copy-of value for each variable X_i is
350 converted into the final value by walking the copy-of chains and
351 updating COPY_OF[i].VALUE to be the last element of the chain. */
352 static prop_value_t
*copy_of
;
354 /* Used in set_copy_of_val to determine if the last link of a copy-of
355 chain has changed. */
356 static tree
*cached_last_copy_of
;
358 /* True if we are doing copy propagation on loads and stores. */
359 static bool do_store_copy_prop
;
362 /* Return true if this statement may generate a useful copy. */
365 stmt_may_generate_copy (tree stmt
)
370 if (TREE_CODE (stmt
) == PHI_NODE
)
371 return !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (stmt
));
373 if (TREE_CODE (stmt
) != MODIFY_EXPR
)
376 lhs
= TREE_OPERAND (stmt
, 0);
377 rhs
= TREE_OPERAND (stmt
, 1);
378 ann
= stmt_ann (stmt
);
380 /* If the statement has volatile operands, it won't generate a
382 if (ann
->has_volatile_ops
)
385 /* If we are not doing store copy-prop, statements with loads and/or
386 stores will never generate a useful copy. */
387 if (!do_store_copy_prop
388 && !ZERO_SSA_OPERANDS (stmt
, SSA_OP_ALL_VIRTUALS
))
391 /* Otherwise, the only statements that generate useful copies are
392 assignments whose RHS is just an SSA name that doesn't flow
393 through abnormal edges. */
394 return (do_store_copy_prop
395 && TREE_CODE (lhs
) == SSA_NAME
)
396 || (TREE_CODE (rhs
) == SSA_NAME
397 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs
));
401 /* Return the copy-of value for VAR. */
403 static inline prop_value_t
*
404 get_copy_of_val (tree var
)
406 prop_value_t
*val
= ©_of
[SSA_NAME_VERSION (var
)];
408 if (val
->value
== NULL_TREE
409 && !stmt_may_generate_copy (SSA_NAME_DEF_STMT (var
)))
411 /* If the variable will never generate a useful copy relation,
412 make it its own copy. */
414 val
->mem_ref
= NULL_TREE
;
421 /* Return last link in the copy-of chain for VAR. */
424 get_last_copy_of (tree var
)
429 /* Traverse COPY_OF starting at VAR until we get to the last
430 link in the chain. Since it is possible to have cycles in PHI
431 nodes, the copy-of chain may also contain cycles.
433 To avoid infinite loops and to avoid traversing lengthy copy-of
434 chains, we artificially limit the maximum number of chains we are
437 The value 5 was taken from a compiler and runtime library
438 bootstrap and a mixture of C and C++ code from various sources.
439 More than 82% of all copy-of chains were shorter than 5 links. */
443 for (i
= 0; i
< LIMIT
; i
++)
445 tree copy
= copy_of
[SSA_NAME_VERSION (last
)].value
;
446 if (copy
== NULL_TREE
|| copy
== last
)
451 /* If we have reached the limit, then we are either in a copy-of
452 cycle or the copy-of chain is too long. In this case, just
453 return VAR so that it is not considered a copy of anything. */
454 return (i
< LIMIT
? last
: var
);
458 /* Set FIRST to be the first variable in the copy-of chain for DEST.
459 If DEST's copy-of value or its copy-of chain has changed, return
462 MEM_REF is the memory reference where FIRST is stored. This is
463 used when DEST is a non-register and we are copy propagating loads
467 set_copy_of_val (tree dest
, tree first
, tree mem_ref
)
469 unsigned int dest_ver
= SSA_NAME_VERSION (dest
);
470 tree old_first
, old_last
, new_last
;
472 /* Set FIRST to be the first link in COPY_OF[DEST]. If that
473 changed, return true. */
474 old_first
= copy_of
[dest_ver
].value
;
475 copy_of
[dest_ver
].value
= first
;
476 copy_of
[dest_ver
].mem_ref
= mem_ref
;
478 if (old_first
!= first
)
481 /* If FIRST and OLD_FIRST are the same, we need to check whether the
482 copy-of chain starting at FIRST ends in a different variable. If
483 the copy-of chain starting at FIRST ends up in a different
484 variable than the last cached value we had for DEST, then return
485 true because DEST is now a copy of a different variable.
487 This test is necessary because even though the first link in the
488 copy-of chain may not have changed, if any of the variables in
489 the copy-of chain changed its final value, DEST will now be the
490 copy of a different variable, so we have to do another round of
491 propagation for everything that depends on DEST. */
492 old_last
= cached_last_copy_of
[dest_ver
];
493 new_last
= get_last_copy_of (dest
);
494 cached_last_copy_of
[dest_ver
] = new_last
;
496 return (old_last
!= new_last
);
500 /* Dump the copy-of value for variable VAR to FILE. */
503 dump_copy_of (FILE *file
, tree var
)
508 print_generic_expr (file
, var
, dump_flags
);
510 if (TREE_CODE (var
) != SSA_NAME
)
513 visited
= sbitmap_alloc (num_ssa_names
);
514 sbitmap_zero (visited
);
515 SET_BIT (visited
, SSA_NAME_VERSION (var
));
517 fprintf (file
, " copy-of chain: ");
520 print_generic_expr (file
, val
, 0);
522 while (copy_of
[SSA_NAME_VERSION (val
)].value
)
524 fprintf (file
, "-> ");
525 val
= copy_of
[SSA_NAME_VERSION (val
)].value
;
526 print_generic_expr (file
, val
, 0);
528 if (TEST_BIT (visited
, SSA_NAME_VERSION (val
)))
530 SET_BIT (visited
, SSA_NAME_VERSION (val
));
533 val
= get_copy_of_val (var
)->value
;
534 if (val
== NULL_TREE
)
535 fprintf (file
, "[UNDEFINED]");
537 fprintf (file
, "[COPY]");
539 fprintf (file
, "[NOT A COPY]");
541 sbitmap_free (visited
);
545 /* Evaluate the RHS of STMT. If it produces a valid copy, set the LHS
546 value and store the LHS into *RESULT_P. If STMT generates more
547 than one name (i.e., STMT is an aliased store), it is enough to
548 store the first name in the V_MAY_DEF list into *RESULT_P. After
549 all, the names generated will be VUSEd in the same statements. */
551 static enum ssa_prop_result
552 copy_prop_visit_assignment (tree stmt
, tree
*result_p
)
555 prop_value_t
*rhs_val
;
557 lhs
= TREE_OPERAND (stmt
, 0);
558 rhs
= TREE_OPERAND (stmt
, 1);
560 gcc_assert (TREE_CODE (rhs
) == SSA_NAME
);
562 rhs_val
= get_copy_of_val (rhs
);
564 if (TREE_CODE (lhs
) == SSA_NAME
)
566 /* Straight copy between two SSA names. First, make sure that
567 we can propagate the RHS into uses of LHS. */
568 if (!may_propagate_copy (lhs
, rhs
))
569 return SSA_PROP_VARYING
;
571 /* Notice that in the case of assignments, we make the LHS be a
572 copy of RHS's value, not of RHS itself. This avoids keeping
573 unnecessary copy-of chains (assignments cannot be in a cycle
574 like PHI nodes), speeding up the propagation process.
575 This is different from what we do in copy_prop_visit_phi_node.
576 In those cases, we are interested in the copy-of chains. */
578 if (set_copy_of_val (*result_p
, rhs_val
->value
, rhs_val
->mem_ref
))
579 return SSA_PROP_INTERESTING
;
581 return SSA_PROP_NOT_INTERESTING
;
583 else if (stmt_makes_single_store (stmt
))
585 /* Otherwise, set the names in V_MAY_DEF/V_MUST_DEF operands
586 to be a copy of RHS. */
591 /* This should only be executed when doing store copy-prop. */
592 gcc_assert (do_store_copy_prop
);
594 /* Set the value of every VDEF to RHS_VAL. */
596 FOR_EACH_SSA_TREE_OPERAND (vdef
, stmt
, i
, SSA_OP_VIRTUAL_DEFS
)
597 changed
|= set_copy_of_val (vdef
, rhs_val
->value
, lhs
);
599 /* Note that for propagation purposes, we are only interested in
600 visiting statements that load the exact same memory reference
601 stored here. Those statements will have the exact same list
602 of virtual uses, so it is enough to set the output of this
603 statement to be its first virtual definition. */
604 *result_p
= first_vdef (stmt
);
607 return SSA_PROP_INTERESTING
;
609 return SSA_PROP_NOT_INTERESTING
;
613 return SSA_PROP_VARYING
;
617 /* Visit the COND_EXPR STMT. Return SSA_PROP_INTERESTING
618 if it can determine which edge will be taken. Otherwise, return
621 static enum ssa_prop_result
622 copy_prop_visit_cond_stmt (tree stmt
, edge
*taken_edge_p
)
624 enum ssa_prop_result retval
;
627 cond
= COND_EXPR_COND (stmt
);
628 retval
= SSA_PROP_VARYING
;
630 /* The only conditionals that we may be able to compute statically
631 are predicates involving two SSA_NAMEs. */
632 if (COMPARISON_CLASS_P (cond
)
633 && TREE_CODE (TREE_OPERAND (cond
, 0)) == SSA_NAME
634 && TREE_CODE (TREE_OPERAND (cond
, 1)) == SSA_NAME
)
636 tree op0
= get_last_copy_of (TREE_OPERAND (cond
, 0));
637 tree op1
= get_last_copy_of (TREE_OPERAND (cond
, 1));
639 /* See if we can determine the predicate's value. */
640 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
642 fprintf (dump_file
, "Trying to determine truth value of ");
643 fprintf (dump_file
, "predicate ");
644 print_generic_stmt (dump_file
, cond
, 0);
647 /* We can fold COND and get a useful result only when we have
648 the same SSA_NAME on both sides of a comparison operator. */
651 tree folded_cond
= fold_binary (TREE_CODE (cond
), boolean_type_node
,
655 basic_block bb
= bb_for_stmt (stmt
);
656 *taken_edge_p
= find_taken_edge (bb
, folded_cond
);
658 retval
= SSA_PROP_INTERESTING
;
663 if (dump_file
&& (dump_flags
& TDF_DETAILS
) && *taken_edge_p
)
664 fprintf (dump_file
, "\nConditional will always take edge %d->%d\n",
665 (*taken_edge_p
)->src
->index
, (*taken_edge_p
)->dest
->index
);
671 /* Evaluate statement STMT. If the statement produces a new output
672 value, return SSA_PROP_INTERESTING and store the SSA_NAME holding
673 the new value in *RESULT_P.
675 If STMT is a conditional branch and we can determine its truth
676 value, set *TAKEN_EDGE_P accordingly.
678 If the new value produced by STMT is varying, return
681 static enum ssa_prop_result
682 copy_prop_visit_stmt (tree stmt
, edge
*taken_edge_p
, tree
*result_p
)
684 enum ssa_prop_result retval
;
686 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
688 fprintf (dump_file
, "\nVisiting statement:\n");
689 print_generic_stmt (dump_file
, stmt
, dump_flags
);
690 fprintf (dump_file
, "\n");
693 if (TREE_CODE (stmt
) == MODIFY_EXPR
694 && TREE_CODE (TREE_OPERAND (stmt
, 1)) == SSA_NAME
695 && (do_store_copy_prop
696 || TREE_CODE (TREE_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
) == MODIFY_EXPR
703 && TREE_CODE (TREE_OPERAND (stmt
, 0)) == SSA_NAME
704 && do_store_copy_prop
705 && stmt_makes_single_load (stmt
))
707 /* If the statement is a copy assignment with a memory load
708 on the RHS, see if we know the value of this load and
709 update the lattice accordingly. */
710 prop_value_t
*val
= get_value_loaded_by (stmt
, copy_of
);
713 && is_gimple_reg (val
->value
)
714 && operand_equal_p (val
->mem_ref
, TREE_OPERAND (stmt
, 1), 0))
717 changed
= set_copy_of_val (TREE_OPERAND (stmt
, 0),
718 val
->value
, val
->mem_ref
);
721 *result_p
= TREE_OPERAND (stmt
, 0);
722 retval
= SSA_PROP_INTERESTING
;
725 retval
= SSA_PROP_NOT_INTERESTING
;
728 retval
= SSA_PROP_VARYING
;
730 else if (TREE_CODE (stmt
) == COND_EXPR
)
732 /* See if we can determine which edge goes out of a conditional
734 retval
= copy_prop_visit_cond_stmt (stmt
, taken_edge_p
);
737 retval
= SSA_PROP_VARYING
;
739 if (retval
== SSA_PROP_VARYING
)
744 /* Any other kind of statement is not interesting for constant
745 propagation and, therefore, not worth simulating. */
746 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
747 fprintf (dump_file
, "No interesting values produced.\n");
749 /* The assignment is not a copy operation. Don't visit this
750 statement again and mark all the definitions in the statement
751 to be copies of nothing. */
752 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_ALL_DEFS
)
753 set_copy_of_val (def
, def
, NULL_TREE
);
760 /* Visit PHI node PHI. If all the arguments produce the same value,
761 set it to be the value of the LHS of PHI. */
763 static enum ssa_prop_result
764 copy_prop_visit_phi_node (tree phi
)
766 enum ssa_prop_result retval
;
769 prop_value_t phi_val
= { 0, NULL_TREE
, NULL_TREE
};
771 lhs
= PHI_RESULT (phi
);
773 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
775 fprintf (dump_file
, "\nVisiting PHI node: ");
776 print_generic_expr (dump_file
, phi
, dump_flags
);
777 fprintf (dump_file
, "\n\n");
780 for (i
= 0; i
< PHI_NUM_ARGS (phi
); i
++)
782 prop_value_t
*arg_val
;
783 tree arg
= PHI_ARG_DEF (phi
, i
);
784 edge e
= PHI_ARG_EDGE (phi
, i
);
786 /* We don't care about values flowing through non-executable
788 if (!(e
->flags
& EDGE_EXECUTABLE
))
791 /* Constants in the argument list never generate a useful copy.
792 Similarly, names that flow through abnormal edges cannot be
793 used to derive copies. */
794 if (TREE_CODE (arg
) != SSA_NAME
|| SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg
))
800 /* Avoid copy propagation from an inner into an outer loop.
801 Otherwise, this may move loop variant variables outside of
802 their loops and prevent coalescing opportunities. If the
803 value was loop invariant, it will be hoisted by LICM and
804 exposed for copy propagation. */
805 if (loop_depth_of_name (arg
) > loop_depth_of_name (lhs
))
811 /* If the LHS appears in the argument list, ignore it. It is
812 irrelevant as a copy. */
813 if (arg
== lhs
|| get_last_copy_of (arg
) == lhs
)
816 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
818 fprintf (dump_file
, "\tArgument #%d: ", i
);
819 dump_copy_of (dump_file
, arg
);
820 fprintf (dump_file
, "\n");
823 arg_val
= get_copy_of_val (arg
);
825 /* If the LHS didn't have a value yet, make it a copy of the
826 first argument we find. Notice that while we make the LHS be
827 a copy of the argument itself, we take the memory reference
828 from the argument's value so that we can compare it to the
829 memory reference of all the other arguments. */
830 if (phi_val
.value
== NULL_TREE
)
833 phi_val
.mem_ref
= arg_val
->mem_ref
;
837 /* If PHI_VAL and ARG don't have a common copy-of chain, then
838 this PHI node cannot be a copy operation. Also, if we are
839 copy propagating stores and these two arguments came from
840 different memory references, they cannot be considered
842 if (get_last_copy_of (phi_val
.value
) != get_last_copy_of (arg
)
843 || (do_store_copy_prop
846 && simple_cst_equal (phi_val
.mem_ref
, arg_val
->mem_ref
) != 1))
853 if (phi_val
.value
&& set_copy_of_val (lhs
, phi_val
.value
, phi_val
.mem_ref
))
854 retval
= (phi_val
.value
!= lhs
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
856 retval
= SSA_PROP_NOT_INTERESTING
;
858 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
860 fprintf (dump_file
, "\nPHI node ");
861 dump_copy_of (dump_file
, lhs
);
862 fprintf (dump_file
, "\nTelling the propagator to ");
863 if (retval
== SSA_PROP_INTERESTING
)
864 fprintf (dump_file
, "add SSA edges out of this PHI and continue.");
865 else if (retval
== SSA_PROP_VARYING
)
866 fprintf (dump_file
, "add SSA edges out of this PHI and never visit again.");
868 fprintf (dump_file
, "do nothing with SSA edges and keep iterating.");
869 fprintf (dump_file
, "\n\n");
876 /* Initialize structures used for copy propagation. PHIS_ONLY is true
877 if we should only consider PHI nodes as generating copy propagation
881 init_copy_prop (void)
885 copy_of
= XNEWVEC (prop_value_t
, num_ssa_names
);
886 memset (copy_of
, 0, num_ssa_names
* sizeof (*copy_of
));
888 cached_last_copy_of
= XNEWVEC (tree
, num_ssa_names
);
889 memset (cached_last_copy_of
, 0, num_ssa_names
* sizeof (*cached_last_copy_of
));
893 block_stmt_iterator si
;
895 int depth
= bb
->loop_depth
;
897 for (si
= bsi_start (bb
); !bsi_end_p (si
); bsi_next (&si
))
899 tree stmt
= bsi_stmt (si
);
902 /* The only statements that we care about are those that may
903 generate useful copies. We also need to mark conditional
904 jumps so that their outgoing edges are added to the work
905 lists of the propagator.
907 Avoid copy propagation from an inner into an outer loop.
908 Otherwise, this may move loop variant variables outside of
909 their loops and prevent coalescing opportunities. If the
910 value was loop invariant, it will be hoisted by LICM and
911 exposed for copy propagation. */
912 if (stmt_ends_bb_p (stmt
))
913 DONT_SIMULATE_AGAIN (stmt
) = false;
914 else if (stmt_may_generate_copy (stmt
)
915 && loop_depth_of_name (TREE_OPERAND (stmt
, 1)) <= depth
)
916 DONT_SIMULATE_AGAIN (stmt
) = false;
918 DONT_SIMULATE_AGAIN (stmt
) = true;
920 /* Mark all the outputs of this statement as not being
921 the copy of anything. */
922 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_ALL_DEFS
)
923 if (DONT_SIMULATE_AGAIN (stmt
))
924 set_copy_of_val (def
, def
, NULL_TREE
);
926 cached_last_copy_of
[SSA_NAME_VERSION (def
)] = def
;
929 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
931 def
= PHI_RESULT (phi
);
932 if (!do_store_copy_prop
&& !is_gimple_reg (def
))
933 DONT_SIMULATE_AGAIN (phi
) = true;
935 DONT_SIMULATE_AGAIN (phi
) = false;
937 if (DONT_SIMULATE_AGAIN (phi
))
938 set_copy_of_val (def
, def
, NULL_TREE
);
940 cached_last_copy_of
[SSA_NAME_VERSION (def
)] = def
;
946 /* Deallocate memory used in copy propagation and do final
950 fini_copy_prop (void)
955 /* Set the final copy-of value for each variable by traversing the
957 tmp
= XNEWVEC (prop_value_t
, num_ssa_names
);
958 memset (tmp
, 0, num_ssa_names
* sizeof (*tmp
));
959 for (i
= 1; i
< num_ssa_names
; i
++)
961 tree var
= ssa_name (i
);
962 if (var
&& copy_of
[i
].value
&& copy_of
[i
].value
!= var
)
963 tmp
[i
].value
= get_last_copy_of (var
);
966 substitute_and_fold (tmp
, false);
968 free (cached_last_copy_of
);
974 /* Main entry point to the copy propagator.
976 PHIS_ONLY is true if we should only consider PHI nodes as generating
977 copy propagation opportunities.
979 The algorithm propagates the value COPY-OF using ssa_propagate. For
980 every variable X_i, COPY-OF(X_i) indicates which variable is X_i created
981 from. The following example shows how the algorithm proceeds at a
985 2 a_2 = PHI <a_24, x_1>
987 4 x_1 = PHI <x_298, a_5, a_2>
989 The end result should be that a_2, a_5, a_24 and x_1 are a copy of
990 x_298. Propagation proceeds as follows.
992 Visit #1: a_24 is copy-of x_1. Value changed.
993 Visit #2: a_2 is copy-of x_1. Value changed.
994 Visit #3: a_5 is copy-of x_1. Value changed.
995 Visit #4: x_1 is copy-of x_298. Value changed.
996 Visit #1: a_24 is copy-of x_298. Value changed.
997 Visit #2: a_2 is copy-of x_298. Value changed.
998 Visit #3: a_5 is copy-of x_298. Value changed.
999 Visit #4: x_1 is copy-of x_298. Stable state reached.
1001 When visiting PHI nodes, we only consider arguments that flow
1002 through edges marked executable by the propagation engine. So,
1003 when visiting statement #2 for the first time, we will only look at
1004 the first argument (a_24) and optimistically assume that its value
1005 is the copy of a_24 (x_1).
1007 The problem with this approach is that it may fail to discover copy
1008 relations in PHI cycles. Instead of propagating copy-of
1009 values, we actually propagate copy-of chains. For instance:
1016 In this code fragment, COPY-OF (X_i) = { D_4, C_9, A_3, B_1 }.
1017 Obviously, we are only really interested in the last value of the
1018 chain, however the propagator needs to access the copy-of chain
1019 when visiting PHI nodes.
1021 To represent the copy-of chain, we use the array COPY_CHAINS, which
1022 holds the first link in the copy-of chain for every variable.
1023 If variable X_i is a copy of X_j, which in turn is a copy of X_k,
1024 the array will contain:
1026 COPY_CHAINS[i] = X_j
1027 COPY_CHAINS[j] = X_k
1028 COPY_CHAINS[k] = X_k
1030 Keeping copy-of chains instead of copy-of values directly becomes
1031 important when visiting PHI nodes. Suppose that we had the
1032 following PHI cycle, such that x_52 is already considered a copy of
1035 1 x_54 = PHI <x_53, x_52>
1036 2 x_53 = PHI <x_898, x_54>
1038 Visit #1: x_54 is copy-of x_53 (because x_52 is copy-of x_53)
1039 Visit #2: x_53 is copy-of x_898 (because x_54 is a copy of x_53,
1040 so it is considered irrelevant
1042 Visit #1: x_54 is copy-of nothing (x_53 is a copy-of x_898 and
1043 x_52 is a copy of x_53, so
1045 Visit #2: x_53 is copy-of nothing
1047 This problem is avoided by keeping a chain of copies, instead of
1048 the final copy-of value. Propagation will now only keep the first
1049 element of a variable's copy-of chain. When visiting PHI nodes,
1050 arguments are considered equal if their copy-of chains end in the
1051 same variable. So, as long as their copy-of chains overlap, we
1052 know that they will be a copy of the same variable, regardless of
1053 which variable that may be).
1055 Propagation would then proceed as follows (the notation a -> b
1056 means that a is a copy-of b):
1058 Visit #1: x_54 = PHI <x_53, x_52>
1061 Result: x_54 -> x_53. Value changed. Add SSA edges.
1063 Visit #1: x_53 = PHI <x_898, x_54>
1066 Result: x_53 -> x_898. Value changed. Add SSA edges.
1068 Visit #2: x_54 = PHI <x_53, x_52>
1070 x_52 -> x_53 -> x_898
1071 Result: x_54 -> x_898. Value changed. Add SSA edges.
1073 Visit #2: x_53 = PHI <x_898, x_54>
1076 Result: x_53 -> x_898. Value didn't change. Stable state
1078 Once the propagator stabilizes, we end up with the desired result
1079 x_53 and x_54 are both copies of x_898. */
1082 execute_copy_prop (bool store_copy_prop
)
1084 do_store_copy_prop
= store_copy_prop
;
1086 ssa_propagate (copy_prop_visit_stmt
, copy_prop_visit_phi_node
);
1092 gate_copy_prop (void)
1094 return flag_tree_copy_prop
!= 0;
1100 execute_copy_prop (false);
1104 struct tree_opt_pass pass_copy_prop
=
1106 "copyprop", /* name */
1107 gate_copy_prop
, /* gate */
1108 do_copy_prop
, /* execute */
1111 0, /* static_pass_number */
1112 TV_TREE_COPY_PROP
, /* tv_id */
1113 PROP_ssa
| PROP_alias
| PROP_cfg
, /* properties_required */
1114 0, /* properties_provided */
1115 0, /* properties_destroyed */
1116 0, /* todo_flags_start */
1121 | TODO_update_ssa
, /* todo_flags_finish */
1126 gate_store_copy_prop (void)
1128 /* STORE-COPY-PROP is enabled only with -ftree-store-copy-prop, but
1129 when -fno-tree-store-copy-prop is specified, we should run
1130 regular COPY-PROP. That's why the pass is enabled with either
1132 return flag_tree_store_copy_prop
!= 0 || flag_tree_copy_prop
!= 0;
1136 store_copy_prop (void)
1138 /* If STORE-COPY-PROP is not enabled, we just run regular COPY-PROP. */
1139 execute_copy_prop (flag_tree_store_copy_prop
!= 0);
1143 struct tree_opt_pass pass_store_copy_prop
=
1145 "store_copyprop", /* name */
1146 gate_store_copy_prop
, /* gate */
1147 store_copy_prop
, /* execute */
1150 0, /* static_pass_number */
1151 TV_TREE_STORE_COPY_PROP
, /* tv_id */
1152 PROP_ssa
| PROP_alias
| PROP_cfg
, /* properties_required */
1153 0, /* properties_provided */
1154 0, /* properties_destroyed */
1155 0, /* todo_flags_start */
1160 | TODO_update_ssa
, /* todo_flags_finish */