1 /* Const/copy propagation and SSA_NAME replacement support routines.
2 Copyright (C) 2004 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include "coretypes.h"
30 #include "basic-block.h"
35 #include "diagnostic.h"
37 #include "tree-dump.h"
38 #include "tree-flow.h"
39 #include "tree-pass.h"
40 #include "langhooks.h"
42 /* This file provides a handful of interfaces for performing const/copy
43 propagation and simple expression replacement which keep variable
44 annotations up-to-date.
46 We require that for any copy operation where the RHS and LHS have
47 a non-null memory tag that 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
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. */
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
&& TREE_CODE (orig
) == SSA_NAME
107 && POINTER_TYPE_P (type_d
) && POINTER_TYPE_P (type_o
))
109 tree mt_dest
= var_ann (SSA_NAME_VAR (dest
))->type_mem_tag
;
110 tree mt_orig
= var_ann (SSA_NAME_VAR (orig
))->type_mem_tag
;
111 if (mt_dest
&& mt_orig
&& mt_dest
!= mt_orig
)
115 /* If the destination is a SSA_NAME for a virtual operand, then we have
116 some special cases to handle. */
117 if (TREE_CODE (dest
) == SSA_NAME
&& !is_gimple_reg (dest
))
119 /* If both operands are SSA_NAMEs referring to virtual operands, then
120 we can always propagate. */
121 if (TREE_CODE (orig
) == SSA_NAME
)
123 if (!is_gimple_reg (orig
))
126 #ifdef ENABLE_CHECKING
127 /* If we have one real and one virtual operand, then something has
128 gone terribly wrong. */
129 if (is_gimple_reg (orig
))
134 /* We have a "copy" from something like a constant into a virtual
135 operand. Reject these. */
139 /* If ORIG flows in from an abnormal edge, it cannot be propagated. */
140 if (TREE_CODE (orig
) == SSA_NAME
141 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig
))
144 /* If DEST is an SSA_NAME that flows from an abnormal edge or if it
145 represents a hard register, then it cannot be replaced. */
146 if (TREE_CODE (dest
) == SSA_NAME
147 && (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest
)
148 || DECL_HARD_REGISTER (SSA_NAME_VAR (dest
))))
151 /* Anything else is OK. */
155 /* Given two SSA_NAMEs, replace the annotations for the one referred to by OP
156 with VAR's annotations.
158 If OP is a pointer, copy the memory tag used originally by OP into
159 VAR. This is needed in cases where VAR had never been dereferenced in the
162 If FOR_PROPAGATION is true, then perform additional checks to ensure
163 that const/copy propagation of var for OP is valid. */
166 replace_ssa_names_ann (tree op
,
168 bool for_propagation ATTRIBUTE_UNUSED
)
170 #if defined ENABLE_CHECKING
171 if (for_propagation
&& !may_propagate_copy (op
, var
))
175 /* If VAR doesn't have a memory tag, copy the one from the original
176 operand. Also copy the dereferenced flags. */
177 if (POINTER_TYPE_P (TREE_TYPE (op
)))
179 var_ann_t new_ann
= var_ann (SSA_NAME_VAR (var
));
180 var_ann_t orig_ann
= var_ann (SSA_NAME_VAR (op
));
182 if (new_ann
->type_mem_tag
== NULL_TREE
)
183 new_ann
->type_mem_tag
= orig_ann
->type_mem_tag
;
184 else if (orig_ann
->type_mem_tag
== NULL_TREE
)
185 orig_ann
->type_mem_tag
= new_ann
->type_mem_tag
;
186 else if (new_ann
->type_mem_tag
!= orig_ann
->type_mem_tag
)
193 /* Common code for propagate_value and replace_exp.
195 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
196 replacement is done to propagate a value or not. */
199 replace_exp_1 (use_operand_p op_p
, tree val
, bool for_propagation
)
201 if (TREE_CODE (val
) == SSA_NAME
)
203 if (TREE_CODE (USE_FROM_PTR (op_p
)) == SSA_NAME
)
204 replace_ssa_names_ann (USE_FROM_PTR (op_p
), val
, for_propagation
);
208 SET_USE (op_p
, lhd_unsave_expr_now (val
));
212 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
213 into the operand pointed by OP_P.
215 Use this version for const/copy propagation as it will perform additional
216 checks to ensure validity of the const/copy propagation. */
219 propagate_value (use_operand_p op_p
, tree val
)
221 replace_exp_1 (op_p
, val
, true);
225 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
226 into the tree pointed by OP_P.
228 Use this version for const/copy propagation when SSA operands are not
229 available. It will perform the additional checks to ensure validity of
230 the const/copy propagation, but will not update any operand information.
231 Be sure to mark the stmt as modified. */
234 propagate_tree_value (tree
*op_p
, tree val
)
236 if (TREE_CODE (val
) == SSA_NAME
)
238 if (TREE_CODE (*op_p
) == SSA_NAME
)
239 replace_ssa_names_ann (*op_p
, val
, true);
243 *op_p
= lhd_unsave_expr_now (val
);
247 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
249 Use this version when not const/copy propagating values. For example,
250 PRE uses this version when building expressions as they would appear
251 in specific blocks taking into account actions of PHI nodes. */
254 replace_exp (use_operand_p op_p
, tree val
)
256 replace_exp_1 (op_p
, val
, false);