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 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
)
113 else if (!lang_hooks
.types_compatible_p (type_d
, type_o
))
115 else if (get_alias_set (TREE_TYPE (type_d
)) !=
116 get_alias_set (TREE_TYPE (type_o
)))
120 /* If the destination is a SSA_NAME for a virtual operand, then we have
121 some special cases to handle. */
122 if (TREE_CODE (dest
) == SSA_NAME
&& !is_gimple_reg (dest
))
124 /* If both operands are SSA_NAMEs referring to virtual operands, then
125 we can always propagate. */
126 if (TREE_CODE (orig
) == SSA_NAME
)
128 if (!is_gimple_reg (orig
))
131 #ifdef ENABLE_CHECKING
132 /* If we have one real and one virtual operand, then something has
133 gone terribly wrong. */
134 gcc_assert (!is_gimple_reg (orig
));
138 /* We have a "copy" from something like a constant into a virtual
139 operand. Reject these. */
143 /* If ORIG flows in from an abnormal edge, it cannot be propagated. */
144 if (TREE_CODE (orig
) == SSA_NAME
145 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig
))
148 /* If DEST is an SSA_NAME that flows from an abnormal edge, then it
149 cannot be replaced. */
150 if (TREE_CODE (dest
) == SSA_NAME
151 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest
))
154 /* Anything else is OK. */
158 /* Similarly, but we know that we're propagating into an ASM_EXPR. */
161 may_propagate_copy_into_asm (tree dest
)
163 /* Hard register operands of asms are special. Do not bypass. */
164 return !(TREE_CODE (dest
) == SSA_NAME
165 && TREE_CODE (SSA_NAME_VAR (dest
)) == VAR_DECL
166 && DECL_HARD_REGISTER (SSA_NAME_VAR (dest
)));
170 /* Given two SSA_NAMEs pointers ORIG and NEW such that we are copy
171 propagating NEW into ORIG, consolidate aliasing information so that
172 they both share the same memory tags. */
175 merge_alias_info (tree orig
, tree
new)
177 tree new_sym
= SSA_NAME_VAR (new);
178 tree orig_sym
= SSA_NAME_VAR (orig
);
179 var_ann_t new_ann
= var_ann (new_sym
);
180 var_ann_t orig_ann
= var_ann (orig_sym
);
181 struct ptr_info_def
*orig_ptr_info
;
183 gcc_assert (POINTER_TYPE_P (TREE_TYPE (orig
)));
184 gcc_assert (POINTER_TYPE_P (TREE_TYPE (new)));
185 #if defined ENABLE_CHECKING
186 gcc_assert (lang_hooks
.types_compatible_p (TREE_TYPE (orig
),
189 /* If the pointed-to alias sets are different, these two pointers
190 would never have the same memory tag. In this case, NEW should
191 not have been propagated into ORIG. */
192 gcc_assert (get_alias_set (TREE_TYPE (TREE_TYPE (new_sym
)))
193 == get_alias_set (TREE_TYPE (TREE_TYPE (orig_sym
))));
196 /* Synchronize the type tags. If both pointers had a tag and they
197 are different, then something has gone wrong. */
198 if (new_ann
->type_mem_tag
== NULL_TREE
)
199 new_ann
->type_mem_tag
= orig_ann
->type_mem_tag
;
200 else if (orig_ann
->type_mem_tag
== NULL_TREE
)
201 orig_ann
->type_mem_tag
= new_ann
->type_mem_tag
;
203 gcc_assert (new_ann
->type_mem_tag
== orig_ann
->type_mem_tag
);
205 orig_ptr_info
= SSA_NAME_PTR_INFO (orig
);
206 if (orig_ptr_info
&& orig_ptr_info
->name_mem_tag
)
208 struct ptr_info_def
*new_ptr_info
= get_ptr_info (new);
210 if (new_ptr_info
->name_mem_tag
== NULL_TREE
)
212 /* If ORIG had a name tag, it means that was dereferenced in
213 the code, and since pointer NEW will now replace every
214 occurrence of ORIG, we have to make sure that NEW has an
215 appropriate tag. If, NEW did not have a name tag, get it
217 memcpy (new_ptr_info
, orig_ptr_info
, sizeof (*new_ptr_info
));
218 new_ptr_info
->pt_vars
= BITMAP_GGC_ALLOC ();
219 bitmap_copy (new_ptr_info
->pt_vars
, orig_ptr_info
->pt_vars
);
220 new_ptr_info
->name_mem_tag
= orig_ptr_info
->name_mem_tag
;
224 /* If NEW already had a name tag, nothing needs to be done.
225 Note that pointer NEW may actually have a different set of
226 pointed-to variables.
228 However, since NEW is being copy-propagated into ORIG, it must
229 always be true that the pointed-to set for pointer NEW is the
230 same, or a subset, of the pointed-to set for pointer ORIG. If
231 this isn't the case, we shouldn't have been able to do the
232 propagation of NEW into ORIG. */
233 #if defined ENABLE_CHECKING
234 if (orig_ptr_info
->pt_vars
&& new_ptr_info
->pt_vars
)
235 gcc_assert (bitmap_intersect_p (new_ptr_info
->pt_vars
,
236 orig_ptr_info
->pt_vars
));
243 /* Common code for propagate_value and replace_exp.
245 Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
246 replacement is done to propagate a value or not. */
249 replace_exp_1 (use_operand_p op_p
, tree val
,
250 bool for_propagation ATTRIBUTE_UNUSED
)
252 tree op
= USE_FROM_PTR (op_p
);
254 #if defined ENABLE_CHECKING
255 gcc_assert (!(for_propagation
256 && TREE_CODE (op
) == SSA_NAME
257 && TREE_CODE (val
) == SSA_NAME
258 && !may_propagate_copy (op
, val
)));
261 if (TREE_CODE (val
) == SSA_NAME
)
263 if (TREE_CODE (op
) == SSA_NAME
&& POINTER_TYPE_P (TREE_TYPE (op
)))
264 merge_alias_info (op
, val
);
268 SET_USE (op_p
, unsave_expr_now (val
));
272 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
273 into the operand pointed by OP_P.
275 Use this version for const/copy propagation as it will perform additional
276 checks to ensure validity of the const/copy propagation. */
279 propagate_value (use_operand_p op_p
, tree val
)
281 replace_exp_1 (op_p
, val
, true);
285 /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
286 into the tree pointed by OP_P.
288 Use this version for const/copy propagation when SSA operands are not
289 available. It will perform the additional checks to ensure validity of
290 the const/copy propagation, but will not update any operand information.
291 Be sure to mark the stmt as modified. */
294 propagate_tree_value (tree
*op_p
, tree val
)
296 #if defined ENABLE_CHECKING
297 gcc_assert (!(TREE_CODE (val
) == SSA_NAME
298 && TREE_CODE (*op_p
) == SSA_NAME
299 && !may_propagate_copy (*op_p
, val
)));
302 if (TREE_CODE (val
) == SSA_NAME
)
304 if (TREE_CODE (*op_p
) == SSA_NAME
&& POINTER_TYPE_P (TREE_TYPE (*op_p
)))
305 merge_alias_info (*op_p
, val
);
309 *op_p
= unsave_expr_now (val
);
313 /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
315 Use this version when not const/copy propagating values. For example,
316 PRE uses this version when building expressions as they would appear
317 in specific blocks taking into account actions of PHI nodes. */
320 replace_exp (use_operand_p op_p
, tree val
)
322 replace_exp_1 (op_p
, val
, false);