]>
Commit | Line | Data |
---|---|---|
2a0603f1 AM |
1 | /* Gimple decl, type, and expression support functions. |
2 | ||
aeee4812 | 3 | Copyright (C) 2007-2023 Free Software Foundation, Inc. |
2a0603f1 AM |
4 | Contributed by Aldy Hernandez <aldyh@redhat.com> |
5 | ||
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify it under | |
9 | the terms of the GNU General Public License as published by the Free | |
10 | Software Foundation; either version 3, or (at your option) any later | |
11 | version. | |
12 | ||
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GCC; see the file COPYING3. If not see | |
20 | <http://www.gnu.org/licenses/>. */ | |
21 | ||
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
c7131fb2 | 25 | #include "backend.h" |
40e23961 | 26 | #include "tree.h" |
c7131fb2 | 27 | #include "gimple.h" |
957060b5 AM |
28 | #include "stringpool.h" |
29 | #include "gimple-ssa.h" | |
c7131fb2 | 30 | #include "fold-const.h" |
2fb9a547 | 31 | #include "tree-eh.h" |
45b0be94 | 32 | #include "gimplify.h" |
d8a2d370 | 33 | #include "stor-layout.h" |
2a0603f1 | 34 | #include "demangle.h" |
1b223a9f AO |
35 | #include "hash-set.h" |
36 | #include "rtl.h" | |
c2e84327 | 37 | #include "tree-pass.h" |
314e6352 ML |
38 | #include "stringpool.h" |
39 | #include "attribs.h" | |
482b2b43 | 40 | #include "target.h" |
2a0603f1 AM |
41 | |
42 | /* ----- Type related ----- */ | |
43 | ||
44 | /* Return true if the conversion from INNER_TYPE to OUTER_TYPE is a | |
45 | useless type conversion, otherwise return false. | |
46 | ||
47 | This function implicitly defines the middle-end type system. With | |
48 | the notion of 'a < b' meaning that useless_type_conversion_p (a, b) | |
49 | holds and 'a > b' meaning that useless_type_conversion_p (b, a) holds, | |
50 | the following invariants shall be fulfilled: | |
51 | ||
52 | 1) useless_type_conversion_p is transitive. | |
53 | If a < b and b < c then a < c. | |
54 | ||
55 | 2) useless_type_conversion_p is not symmetric. | |
56 | From a < b does not follow a > b. | |
57 | ||
58 | 3) Types define the available set of operations applicable to values. | |
59 | A type conversion is useless if the operations for the target type | |
60 | is a subset of the operations for the source type. For example | |
61 | casts to void* are useless, casts from void* are not (void* can't | |
62 | be dereferenced or offsetted, but copied, hence its set of operations | |
63 | is a strict subset of that of all other data pointer types). Casts | |
64 | to const T* are useless (can't be written to), casts from const T* | |
65 | to T* are not. */ | |
66 | ||
67 | bool | |
68 | useless_type_conversion_p (tree outer_type, tree inner_type) | |
69 | { | |
70 | /* Do the following before stripping toplevel qualifiers. */ | |
71 | if (POINTER_TYPE_P (inner_type) | |
72 | && POINTER_TYPE_P (outer_type)) | |
73 | { | |
74 | /* Do not lose casts between pointers to different address spaces. */ | |
75 | if (TYPE_ADDR_SPACE (TREE_TYPE (outer_type)) | |
76 | != TYPE_ADDR_SPACE (TREE_TYPE (inner_type))) | |
77 | return false; | |
ccb5ad37 JH |
78 | /* Do not lose casts to function pointer types. */ |
79 | if ((TREE_CODE (TREE_TYPE (outer_type)) == FUNCTION_TYPE | |
80 | || TREE_CODE (TREE_TYPE (outer_type)) == METHOD_TYPE) | |
81 | && !(TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE | |
82 | || TREE_CODE (TREE_TYPE (inner_type)) == METHOD_TYPE)) | |
83 | return false; | |
2a0603f1 AM |
84 | } |
85 | ||
86 | /* From now on qualifiers on value types do not matter. */ | |
87 | inner_type = TYPE_MAIN_VARIANT (inner_type); | |
88 | outer_type = TYPE_MAIN_VARIANT (outer_type); | |
89 | ||
90 | if (inner_type == outer_type) | |
91 | return true; | |
92 | ||
5993d1c9 EB |
93 | /* Changes in machine mode are never useless conversions because the RTL |
94 | middle-end expects explicit conversions between modes. */ | |
b6d3c031 | 95 | if (TYPE_MODE (inner_type) != TYPE_MODE (outer_type)) |
2a0603f1 AM |
96 | return false; |
97 | ||
98 | /* If both the inner and outer types are integral types, then the | |
99 | conversion is not necessary if they have the same mode and | |
100 | signedness and precision, and both or neither are boolean. */ | |
101 | if (INTEGRAL_TYPE_P (inner_type) | |
102 | && INTEGRAL_TYPE_P (outer_type)) | |
103 | { | |
104 | /* Preserve changes in signedness or precision. */ | |
105 | if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type) | |
106 | || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type)) | |
107 | return false; | |
108 | ||
109 | /* Preserve conversions to/from BOOLEAN_TYPE if types are not | |
110 | of precision one. */ | |
111 | if (((TREE_CODE (inner_type) == BOOLEAN_TYPE) | |
112 | != (TREE_CODE (outer_type) == BOOLEAN_TYPE)) | |
113 | && TYPE_PRECISION (outer_type) != 1) | |
114 | return false; | |
115 | ||
116 | /* We don't need to preserve changes in the types minimum or | |
117 | maximum value in general as these do not generate code | |
118 | unless the types precisions are different. */ | |
119 | return true; | |
120 | } | |
121 | ||
122 | /* Scalar floating point types with the same mode are compatible. */ | |
123 | else if (SCALAR_FLOAT_TYPE_P (inner_type) | |
124 | && SCALAR_FLOAT_TYPE_P (outer_type)) | |
125 | return true; | |
126 | ||
127 | /* Fixed point types with the same mode are compatible. */ | |
128 | else if (FIXED_POINT_TYPE_P (inner_type) | |
129 | && FIXED_POINT_TYPE_P (outer_type)) | |
16967aff | 130 | return TYPE_SATURATING (inner_type) == TYPE_SATURATING (outer_type); |
2a0603f1 AM |
131 | |
132 | /* We need to take special care recursing to pointed-to types. */ | |
133 | else if (POINTER_TYPE_P (inner_type) | |
134 | && POINTER_TYPE_P (outer_type)) | |
135 | { | |
2a0603f1 AM |
136 | /* We do not care for const qualification of the pointed-to types |
137 | as const qualification has no semantic value to the middle-end. */ | |
138 | ||
139 | /* Otherwise pointers/references are equivalent. */ | |
140 | return true; | |
141 | } | |
142 | ||
143 | /* Recurse for complex types. */ | |
144 | else if (TREE_CODE (inner_type) == COMPLEX_TYPE | |
145 | && TREE_CODE (outer_type) == COMPLEX_TYPE) | |
146 | return useless_type_conversion_p (TREE_TYPE (outer_type), | |
147 | TREE_TYPE (inner_type)); | |
148 | ||
149 | /* Recurse for vector types with the same number of subparts. */ | |
150 | else if (TREE_CODE (inner_type) == VECTOR_TYPE | |
482b2b43 RS |
151 | && TREE_CODE (outer_type) == VECTOR_TYPE) |
152 | return (known_eq (TYPE_VECTOR_SUBPARTS (inner_type), | |
153 | TYPE_VECTOR_SUBPARTS (outer_type)) | |
154 | && useless_type_conversion_p (TREE_TYPE (outer_type), | |
155 | TREE_TYPE (inner_type)) | |
156 | && targetm.compatible_vector_types_p (inner_type, outer_type)); | |
2a0603f1 AM |
157 | |
158 | else if (TREE_CODE (inner_type) == ARRAY_TYPE | |
159 | && TREE_CODE (outer_type) == ARRAY_TYPE) | |
160 | { | |
ee45a32d EB |
161 | /* Preserve various attributes. */ |
162 | if (TYPE_REVERSE_STORAGE_ORDER (inner_type) | |
163 | != TYPE_REVERSE_STORAGE_ORDER (outer_type)) | |
164 | return false; | |
2a0603f1 AM |
165 | if (TYPE_STRING_FLAG (inner_type) != TYPE_STRING_FLAG (outer_type)) |
166 | return false; | |
167 | ||
168 | /* Conversions from array types with unknown extent to | |
169 | array types with known extent are not useless. */ | |
ee45a32d | 170 | if (!TYPE_DOMAIN (inner_type) && TYPE_DOMAIN (outer_type)) |
2a0603f1 AM |
171 | return false; |
172 | ||
173 | /* Nor are conversions from array types with non-constant size to | |
174 | array types with constant size or to different size. */ | |
175 | if (TYPE_SIZE (outer_type) | |
176 | && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST | |
177 | && (!TYPE_SIZE (inner_type) | |
178 | || TREE_CODE (TYPE_SIZE (inner_type)) != INTEGER_CST | |
179 | || !tree_int_cst_equal (TYPE_SIZE (outer_type), | |
180 | TYPE_SIZE (inner_type)))) | |
181 | return false; | |
182 | ||
183 | /* Check conversions between arrays with partially known extents. | |
184 | If the array min/max values are constant they have to match. | |
185 | Otherwise allow conversions to unknown and variable extents. | |
186 | In particular this declares conversions that may change the | |
187 | mode to BLKmode as useless. */ | |
188 | if (TYPE_DOMAIN (inner_type) | |
189 | && TYPE_DOMAIN (outer_type) | |
190 | && TYPE_DOMAIN (inner_type) != TYPE_DOMAIN (outer_type)) | |
191 | { | |
192 | tree inner_min = TYPE_MIN_VALUE (TYPE_DOMAIN (inner_type)); | |
193 | tree outer_min = TYPE_MIN_VALUE (TYPE_DOMAIN (outer_type)); | |
194 | tree inner_max = TYPE_MAX_VALUE (TYPE_DOMAIN (inner_type)); | |
195 | tree outer_max = TYPE_MAX_VALUE (TYPE_DOMAIN (outer_type)); | |
196 | ||
197 | /* After gimplification a variable min/max value carries no | |
198 | additional information compared to a NULL value. All that | |
199 | matters has been lowered to be part of the IL. */ | |
200 | if (inner_min && TREE_CODE (inner_min) != INTEGER_CST) | |
201 | inner_min = NULL_TREE; | |
202 | if (outer_min && TREE_CODE (outer_min) != INTEGER_CST) | |
203 | outer_min = NULL_TREE; | |
204 | if (inner_max && TREE_CODE (inner_max) != INTEGER_CST) | |
205 | inner_max = NULL_TREE; | |
206 | if (outer_max && TREE_CODE (outer_max) != INTEGER_CST) | |
207 | outer_max = NULL_TREE; | |
208 | ||
209 | /* Conversions NULL / variable <- cst are useless, but not | |
210 | the other way around. */ | |
211 | if (outer_min | |
212 | && (!inner_min | |
213 | || !tree_int_cst_equal (inner_min, outer_min))) | |
214 | return false; | |
215 | if (outer_max | |
216 | && (!inner_max | |
217 | || !tree_int_cst_equal (inner_max, outer_max))) | |
218 | return false; | |
219 | } | |
220 | ||
221 | /* Recurse on the element check. */ | |
222 | return useless_type_conversion_p (TREE_TYPE (outer_type), | |
223 | TREE_TYPE (inner_type)); | |
224 | } | |
225 | ||
226 | else if ((TREE_CODE (inner_type) == FUNCTION_TYPE | |
227 | || TREE_CODE (inner_type) == METHOD_TYPE) | |
228 | && TREE_CODE (inner_type) == TREE_CODE (outer_type)) | |
229 | { | |
230 | tree outer_parm, inner_parm; | |
231 | ||
232 | /* If the return types are not compatible bail out. */ | |
233 | if (!useless_type_conversion_p (TREE_TYPE (outer_type), | |
234 | TREE_TYPE (inner_type))) | |
235 | return false; | |
236 | ||
237 | /* Method types should belong to a compatible base class. */ | |
238 | if (TREE_CODE (inner_type) == METHOD_TYPE | |
239 | && !useless_type_conversion_p (TYPE_METHOD_BASETYPE (outer_type), | |
240 | TYPE_METHOD_BASETYPE (inner_type))) | |
241 | return false; | |
242 | ||
243 | /* A conversion to an unprototyped argument list is ok. */ | |
244 | if (!prototype_p (outer_type)) | |
245 | return true; | |
246 | ||
247 | /* If the unqualified argument types are compatible the conversion | |
248 | is useless. */ | |
249 | if (TYPE_ARG_TYPES (outer_type) == TYPE_ARG_TYPES (inner_type)) | |
250 | return true; | |
251 | ||
252 | for (outer_parm = TYPE_ARG_TYPES (outer_type), | |
253 | inner_parm = TYPE_ARG_TYPES (inner_type); | |
254 | outer_parm && inner_parm; | |
255 | outer_parm = TREE_CHAIN (outer_parm), | |
256 | inner_parm = TREE_CHAIN (inner_parm)) | |
257 | if (!useless_type_conversion_p | |
258 | (TYPE_MAIN_VARIANT (TREE_VALUE (outer_parm)), | |
259 | TYPE_MAIN_VARIANT (TREE_VALUE (inner_parm)))) | |
260 | return false; | |
261 | ||
262 | /* If there is a mismatch in the number of arguments the functions | |
263 | are not compatible. */ | |
264 | if (outer_parm || inner_parm) | |
265 | return false; | |
266 | ||
267 | /* Defer to the target if necessary. */ | |
268 | if (TYPE_ATTRIBUTES (inner_type) || TYPE_ATTRIBUTES (outer_type)) | |
269 | return comp_type_attributes (outer_type, inner_type) != 0; | |
270 | ||
271 | return true; | |
272 | } | |
273 | ||
5993d1c9 EB |
274 | /* For aggregates we rely on TYPE_CANONICAL exclusively and require |
275 | explicit conversions for types involving to be structurally | |
276 | compared types. */ | |
2a0603f1 AM |
277 | else if (AGGREGATE_TYPE_P (inner_type) |
278 | && TREE_CODE (inner_type) == TREE_CODE (outer_type)) | |
5993d1c9 EB |
279 | return TYPE_CANONICAL (inner_type) |
280 | && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type); | |
ee3db47d JH |
281 | |
282 | else if (TREE_CODE (inner_type) == OFFSET_TYPE | |
283 | && TREE_CODE (outer_type) == OFFSET_TYPE) | |
284 | return useless_type_conversion_p (TREE_TYPE (outer_type), | |
285 | TREE_TYPE (inner_type)) | |
286 | && useless_type_conversion_p | |
287 | (TYPE_OFFSET_BASETYPE (outer_type), | |
288 | TYPE_OFFSET_BASETYPE (inner_type)); | |
2a0603f1 AM |
289 | |
290 | return false; | |
291 | } | |
292 | ||
293 | ||
294 | /* ----- Decl related ----- */ | |
295 | ||
296 | /* Set sequence SEQ to be the GIMPLE body for function FN. */ | |
297 | ||
298 | void | |
299 | gimple_set_body (tree fndecl, gimple_seq seq) | |
300 | { | |
301 | struct function *fn = DECL_STRUCT_FUNCTION (fndecl); | |
302 | if (fn == NULL) | |
303 | { | |
304 | /* If FNDECL still does not have a function structure associated | |
305 | with it, then it does not make sense for it to receive a | |
306 | GIMPLE body. */ | |
307 | gcc_assert (seq == NULL); | |
308 | } | |
309 | else | |
310 | fn->gimple_body = seq; | |
311 | } | |
312 | ||
313 | ||
314 | /* Return the body of GIMPLE statements for function FN. After the | |
315 | CFG pass, the function body doesn't exist anymore because it has | |
316 | been split up into basic blocks. In this case, it returns | |
317 | NULL. */ | |
318 | ||
319 | gimple_seq | |
320 | gimple_body (tree fndecl) | |
321 | { | |
322 | struct function *fn = DECL_STRUCT_FUNCTION (fndecl); | |
323 | return fn ? fn->gimple_body : NULL; | |
324 | } | |
325 | ||
326 | /* Return true when FNDECL has Gimple body either in unlowered | |
327 | or CFG form. */ | |
328 | bool | |
329 | gimple_has_body_p (tree fndecl) | |
330 | { | |
331 | struct function *fn = DECL_STRUCT_FUNCTION (fndecl); | |
c2e84327 | 332 | return (gimple_body (fndecl) || (fn && fn->cfg && !(fn->curr_properties & PROP_rtl))); |
2a0603f1 AM |
333 | } |
334 | ||
335 | /* Return a printable name for symbol DECL. */ | |
336 | ||
337 | const char * | |
338 | gimple_decl_printable_name (tree decl, int verbosity) | |
339 | { | |
340 | if (!DECL_NAME (decl)) | |
341 | return NULL; | |
342 | ||
a92f6726 | 343 | if (HAS_DECL_ASSEMBLER_NAME_P (decl) && DECL_ASSEMBLER_NAME_SET_P (decl)) |
2a0603f1 | 344 | { |
2a0603f1 AM |
345 | int dmgl_opts = DMGL_NO_OPTS; |
346 | ||
347 | if (verbosity >= 2) | |
348 | { | |
349 | dmgl_opts = DMGL_VERBOSE | |
350 | | DMGL_ANSI | |
351 | | DMGL_GNU_V3 | |
352 | | DMGL_RET_POSTFIX; | |
353 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
354 | dmgl_opts |= DMGL_PARAMS; | |
355 | } | |
356 | ||
a92f6726 NS |
357 | const char *mangled_str |
358 | = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME_RAW (decl)); | |
359 | const char *str = cplus_demangle_v3 (mangled_str, dmgl_opts); | |
360 | return str ? str : mangled_str; | |
2a0603f1 AM |
361 | } |
362 | ||
363 | return IDENTIFIER_POINTER (DECL_NAME (decl)); | |
364 | } | |
365 | ||
366 | ||
367 | /* Create a new VAR_DECL and copy information from VAR to it. */ | |
368 | ||
369 | tree | |
370 | copy_var_decl (tree var, tree name, tree type) | |
371 | { | |
372 | tree copy = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, name, type); | |
373 | ||
374 | TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (var); | |
375 | TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (var); | |
eb72dc66 | 376 | DECL_NOT_GIMPLE_REG_P (copy) = DECL_NOT_GIMPLE_REG_P (var); |
2a0603f1 AM |
377 | DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (var); |
378 | DECL_IGNORED_P (copy) = DECL_IGNORED_P (var); | |
379 | DECL_CONTEXT (copy) = DECL_CONTEXT (var); | |
2a0603f1 AM |
380 | TREE_USED (copy) = 1; |
381 | DECL_SEEN_IN_BIND_EXPR_P (copy) = 1; | |
382 | DECL_ATTRIBUTES (copy) = DECL_ATTRIBUTES (var); | |
8fc708b7 JJ |
383 | if (DECL_USER_ALIGN (var)) |
384 | { | |
fe37c7af | 385 | SET_DECL_ALIGN (copy, DECL_ALIGN (var)); |
8fc708b7 JJ |
386 | DECL_USER_ALIGN (copy) = 1; |
387 | } | |
2a0603f1 | 388 | |
e9e2bad7 | 389 | copy_warning (copy, var); |
2a0603f1 AM |
390 | return copy; |
391 | } | |
392 | ||
45b0be94 AM |
393 | /* Strip off a legitimate source ending from the input string NAME of |
394 | length LEN. Rather than having to know the names used by all of | |
395 | our front ends, we strip off an ending of a period followed by | |
ed5cd5bc | 396 | up to four characters. (like ".cpp".) */ |
45b0be94 AM |
397 | |
398 | static inline void | |
399 | remove_suffix (char *name, int len) | |
400 | { | |
401 | int i; | |
402 | ||
ed5cd5bc | 403 | for (i = 2; i < 7 && len > i; i++) |
45b0be94 AM |
404 | { |
405 | if (name[len - i] == '.') | |
406 | { | |
407 | name[len - i] = '\0'; | |
408 | break; | |
409 | } | |
410 | } | |
411 | } | |
412 | ||
413 | /* Create a new temporary name with PREFIX. Return an identifier. */ | |
414 | ||
415 | static GTY(()) unsigned int tmp_var_id_num; | |
416 | ||
417 | tree | |
418 | create_tmp_var_name (const char *prefix) | |
419 | { | |
420 | char *tmp_name; | |
421 | ||
422 | if (prefix) | |
423 | { | |
424 | char *preftmp = ASTRDUP (prefix); | |
425 | ||
426 | remove_suffix (preftmp, strlen (preftmp)); | |
427 | clean_symbol_name (preftmp); | |
428 | ||
429 | prefix = preftmp; | |
430 | } | |
431 | ||
432 | ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++); | |
433 | return get_identifier (tmp_name); | |
434 | } | |
435 | ||
436 | /* Create a new temporary variable declaration of type TYPE. | |
437 | Do NOT push it into the current binding. */ | |
438 | ||
439 | tree | |
440 | create_tmp_var_raw (tree type, const char *prefix) | |
441 | { | |
442 | tree tmp_var; | |
443 | ||
444 | tmp_var = build_decl (input_location, | |
445 | VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL, | |
446 | type); | |
447 | ||
448 | /* The variable was declared by the compiler. */ | |
449 | DECL_ARTIFICIAL (tmp_var) = 1; | |
450 | /* And we don't want debug info for it. */ | |
451 | DECL_IGNORED_P (tmp_var) = 1; | |
b6f03d13 JJ |
452 | /* And we don't want even the fancy names of those printed in |
453 | -fdump-final-insns= dumps. */ | |
454 | DECL_NAMELESS (tmp_var) = 1; | |
45b0be94 AM |
455 | |
456 | /* Make the variable writable. */ | |
457 | TREE_READONLY (tmp_var) = 0; | |
458 | ||
459 | DECL_EXTERNAL (tmp_var) = 0; | |
460 | TREE_STATIC (tmp_var) = 0; | |
461 | TREE_USED (tmp_var) = 1; | |
462 | ||
463 | return tmp_var; | |
464 | } | |
465 | ||
466 | /* Create a new temporary variable declaration of type TYPE. DO push the | |
467 | variable into the current binding. Further, assume that this is called | |
468 | only from gimplification or optimization, at which point the creation of | |
469 | certain types are bugs. */ | |
470 | ||
471 | tree | |
472 | create_tmp_var (tree type, const char *prefix) | |
473 | { | |
474 | tree tmp_var; | |
475 | ||
476 | /* We don't allow types that are addressable (meaning we can't make copies), | |
477 | or incomplete. We also used to reject every variable size objects here, | |
478 | but now support those for which a constant upper bound can be obtained. | |
479 | The processing for variable sizes is performed in gimple_add_tmp_var, | |
480 | point at which it really matters and possibly reached via paths not going | |
481 | through this function, e.g. after direct calls to create_tmp_var_raw. */ | |
482 | gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type)); | |
483 | ||
484 | tmp_var = create_tmp_var_raw (type, prefix); | |
485 | gimple_add_tmp_var (tmp_var); | |
486 | return tmp_var; | |
487 | } | |
488 | ||
489 | /* Create a new temporary variable declaration of type TYPE by calling | |
490 | create_tmp_var and if TYPE is a vector or a complex number, mark the new | |
491 | temporary as gimple register. */ | |
492 | ||
493 | tree | |
494 | create_tmp_reg (tree type, const char *prefix) | |
495 | { | |
eb72dc66 | 496 | return create_tmp_var (type, prefix); |
45b62594 RB |
497 | } |
498 | ||
499 | /* Create a new temporary variable declaration of type TYPE by calling | |
500 | create_tmp_var and if TYPE is a vector or a complex number, mark the new | |
501 | temporary as gimple register. */ | |
502 | ||
503 | tree | |
504 | create_tmp_reg_fn (struct function *fn, tree type, const char *prefix) | |
505 | { | |
506 | tree tmp; | |
507 | ||
508 | tmp = create_tmp_var_raw (type, prefix); | |
509 | gimple_add_tmp_var_fn (fn, tmp); | |
45b62594 RB |
510 | |
511 | return tmp; | |
45b0be94 AM |
512 | } |
513 | ||
2a0603f1 AM |
514 | |
515 | /* ----- Expression related ----- */ | |
516 | ||
517 | /* Extract the operands and code for expression EXPR into *SUBCODE_P, | |
518 | *OP1_P, *OP2_P and *OP3_P respectively. */ | |
519 | ||
520 | void | |
d1e2bb2d JJ |
521 | extract_ops_from_tree (tree expr, enum tree_code *subcode_p, tree *op1_p, |
522 | tree *op2_p, tree *op3_p) | |
2a0603f1 | 523 | { |
2a0603f1 | 524 | *subcode_p = TREE_CODE (expr); |
90acd49f | 525 | switch (get_gimple_rhs_class (*subcode_p)) |
2a0603f1 | 526 | { |
90acd49f ML |
527 | case GIMPLE_TERNARY_RHS: |
528 | { | |
529 | *op1_p = TREE_OPERAND (expr, 0); | |
530 | *op2_p = TREE_OPERAND (expr, 1); | |
531 | *op3_p = TREE_OPERAND (expr, 2); | |
532 | break; | |
533 | } | |
534 | case GIMPLE_BINARY_RHS: | |
535 | { | |
536 | *op1_p = TREE_OPERAND (expr, 0); | |
537 | *op2_p = TREE_OPERAND (expr, 1); | |
538 | *op3_p = NULL_TREE; | |
539 | break; | |
540 | } | |
541 | case GIMPLE_UNARY_RHS: | |
542 | { | |
543 | *op1_p = TREE_OPERAND (expr, 0); | |
544 | *op2_p = NULL_TREE; | |
545 | *op3_p = NULL_TREE; | |
546 | break; | |
547 | } | |
548 | case GIMPLE_SINGLE_RHS: | |
549 | { | |
550 | *op1_p = expr; | |
551 | *op2_p = NULL_TREE; | |
552 | *op3_p = NULL_TREE; | |
553 | break; | |
554 | } | |
555 | default: | |
556 | gcc_unreachable (); | |
2a0603f1 | 557 | } |
2a0603f1 AM |
558 | } |
559 | ||
560 | /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */ | |
561 | ||
562 | void | |
563 | gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p, | |
564 | tree *lhs_p, tree *rhs_p) | |
565 | { | |
98209db3 | 566 | gcc_assert (COMPARISON_CLASS_P (cond) |
2a0603f1 AM |
567 | || TREE_CODE (cond) == TRUTH_NOT_EXPR |
568 | || is_gimple_min_invariant (cond) | |
569 | || SSA_VAR_P (cond)); | |
70e2a30a | 570 | gcc_checking_assert (!tree_could_throw_p (cond)); |
2a0603f1 AM |
571 | |
572 | extract_ops_from_tree (cond, code_p, lhs_p, rhs_p); | |
573 | ||
574 | /* Canonicalize conditionals of the form 'if (!VAL)'. */ | |
575 | if (*code_p == TRUTH_NOT_EXPR) | |
576 | { | |
577 | *code_p = EQ_EXPR; | |
578 | gcc_assert (*lhs_p && *rhs_p == NULL_TREE); | |
579 | *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p)); | |
580 | } | |
581 | /* Canonicalize conditionals of the form 'if (VAL)' */ | |
582 | else if (TREE_CODE_CLASS (*code_p) != tcc_comparison) | |
583 | { | |
584 | *code_p = NE_EXPR; | |
585 | gcc_assert (*lhs_p && *rhs_p == NULL_TREE); | |
586 | *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p)); | |
587 | } | |
588 | } | |
589 | ||
590 | /* Return true if T is a valid LHS for a GIMPLE assignment expression. */ | |
591 | ||
592 | bool | |
593 | is_gimple_lvalue (tree t) | |
594 | { | |
595 | return (is_gimple_addressable (t) | |
596 | || TREE_CODE (t) == WITH_SIZE_EXPR | |
597 | /* These are complex lvalues, but don't have addresses, so they | |
598 | go here. */ | |
599 | || TREE_CODE (t) == BIT_FIELD_REF); | |
600 | } | |
601 | ||
70e2a30a | 602 | /* Helper for is_gimple_condexpr and is_gimple_condexpr_for_cond. */ |
2a0603f1 | 603 | |
70e2a30a | 604 | static bool |
70430001 | 605 | is_gimple_condexpr_1 (tree t, bool allow_traps, bool allow_cplx) |
2a0603f1 | 606 | { |
70430001 RB |
607 | tree op0; |
608 | return (is_gimple_val (t) | |
609 | || (COMPARISON_CLASS_P (t) | |
610 | && (allow_traps || !tree_could_throw_p (t)) | |
611 | && ((op0 = TREE_OPERAND (t, 0)), true) | |
612 | && (allow_cplx || TREE_CODE (TREE_TYPE (op0)) != COMPLEX_TYPE) | |
613 | && is_gimple_val (op0) | |
614 | && is_gimple_val (TREE_OPERAND (t, 1)))); | |
2a0603f1 AM |
615 | } |
616 | ||
70e2a30a IL |
617 | /* Like is_gimple_condexpr, but does not allow T to trap. */ |
618 | ||
619 | bool | |
620 | is_gimple_condexpr_for_cond (tree t) | |
621 | { | |
70430001 | 622 | return is_gimple_condexpr_1 (t, false, true); |
70e2a30a IL |
623 | } |
624 | ||
c090743b RB |
625 | /* Canonicalize a tree T for use in a COND_EXPR as conditional. Returns |
626 | a canonicalized tree that is valid for a COND_EXPR or NULL_TREE, if | |
627 | we failed to create one. */ | |
628 | ||
629 | tree | |
630 | canonicalize_cond_expr_cond (tree t) | |
631 | { | |
632 | /* Strip conversions around boolean operations. */ | |
633 | if (CONVERT_EXPR_P (t) | |
634 | && (truth_value_p (TREE_CODE (TREE_OPERAND (t, 0))) | |
635 | || TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) | |
636 | == BOOLEAN_TYPE)) | |
637 | t = TREE_OPERAND (t, 0); | |
638 | ||
639 | /* For !x use x == 0. */ | |
640 | if (TREE_CODE (t) == TRUTH_NOT_EXPR) | |
641 | { | |
642 | tree top0 = TREE_OPERAND (t, 0); | |
643 | t = build2 (EQ_EXPR, TREE_TYPE (t), | |
644 | top0, build_int_cst (TREE_TYPE (top0), 0)); | |
645 | } | |
646 | /* For cmp ? 1 : 0 use cmp. */ | |
647 | else if (TREE_CODE (t) == COND_EXPR | |
648 | && COMPARISON_CLASS_P (TREE_OPERAND (t, 0)) | |
649 | && integer_onep (TREE_OPERAND (t, 1)) | |
650 | && integer_zerop (TREE_OPERAND (t, 2))) | |
651 | { | |
652 | tree top0 = TREE_OPERAND (t, 0); | |
653 | t = build2 (TREE_CODE (top0), TREE_TYPE (t), | |
654 | TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1)); | |
655 | } | |
656 | /* For x ^ y use x != y. */ | |
657 | else if (TREE_CODE (t) == BIT_XOR_EXPR) | |
658 | t = build2 (NE_EXPR, TREE_TYPE (t), | |
659 | TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)); | |
660 | ||
661 | /* We don't know where this will be used so allow both traps and | |
662 | _Complex. The caller is responsible for more precise checking. */ | |
663 | if (is_gimple_condexpr_1 (t, true, true)) | |
664 | return t; | |
665 | ||
666 | return NULL_TREE; | |
667 | } | |
668 | ||
2a0603f1 AM |
669 | /* Return true if T is a gimple address. */ |
670 | ||
671 | bool | |
672 | is_gimple_address (const_tree t) | |
673 | { | |
674 | tree op; | |
675 | ||
676 | if (TREE_CODE (t) != ADDR_EXPR) | |
677 | return false; | |
678 | ||
679 | op = TREE_OPERAND (t, 0); | |
680 | while (handled_component_p (op)) | |
681 | { | |
682 | if ((TREE_CODE (op) == ARRAY_REF | |
683 | || TREE_CODE (op) == ARRAY_RANGE_REF) | |
684 | && !is_gimple_val (TREE_OPERAND (op, 1))) | |
685 | return false; | |
686 | ||
687 | op = TREE_OPERAND (op, 0); | |
688 | } | |
689 | ||
65dd1346 RS |
690 | if (CONSTANT_CLASS_P (op) |
691 | || TREE_CODE (op) == TARGET_MEM_REF | |
692 | || TREE_CODE (op) == MEM_REF) | |
2a0603f1 AM |
693 | return true; |
694 | ||
695 | switch (TREE_CODE (op)) | |
696 | { | |
697 | case PARM_DECL: | |
698 | case RESULT_DECL: | |
699 | case LABEL_DECL: | |
700 | case FUNCTION_DECL: | |
701 | case VAR_DECL: | |
702 | case CONST_DECL: | |
703 | return true; | |
704 | ||
705 | default: | |
706 | return false; | |
707 | } | |
708 | } | |
709 | ||
710 | /* Return true if T is a gimple invariant address. */ | |
711 | ||
712 | bool | |
713 | is_gimple_invariant_address (const_tree t) | |
714 | { | |
715 | const_tree op; | |
716 | ||
717 | if (TREE_CODE (t) != ADDR_EXPR) | |
718 | return false; | |
719 | ||
720 | op = strip_invariant_refs (TREE_OPERAND (t, 0)); | |
721 | if (!op) | |
722 | return false; | |
723 | ||
724 | if (TREE_CODE (op) == MEM_REF) | |
725 | { | |
726 | const_tree op0 = TREE_OPERAND (op, 0); | |
727 | return (TREE_CODE (op0) == ADDR_EXPR | |
728 | && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0)) | |
729 | || decl_address_invariant_p (TREE_OPERAND (op0, 0)))); | |
730 | } | |
731 | ||
732 | return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op); | |
733 | } | |
734 | ||
735 | /* Return true if T is a gimple invariant address at IPA level | |
736 | (so addresses of variables on stack are not allowed). */ | |
737 | ||
738 | bool | |
739 | is_gimple_ip_invariant_address (const_tree t) | |
740 | { | |
741 | const_tree op; | |
742 | ||
743 | if (TREE_CODE (t) != ADDR_EXPR) | |
744 | return false; | |
745 | ||
746 | op = strip_invariant_refs (TREE_OPERAND (t, 0)); | |
747 | if (!op) | |
748 | return false; | |
749 | ||
750 | if (TREE_CODE (op) == MEM_REF) | |
751 | { | |
752 | const_tree op0 = TREE_OPERAND (op, 0); | |
753 | return (TREE_CODE (op0) == ADDR_EXPR | |
754 | && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0)) | |
755 | || decl_address_ip_invariant_p (TREE_OPERAND (op0, 0)))); | |
756 | } | |
757 | ||
758 | return CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op); | |
759 | } | |
760 | ||
761 | /* Return true if T is a GIMPLE minimal invariant. It's a restricted | |
762 | form of function invariant. */ | |
763 | ||
764 | bool | |
765 | is_gimple_min_invariant (const_tree t) | |
766 | { | |
767 | if (TREE_CODE (t) == ADDR_EXPR) | |
768 | return is_gimple_invariant_address (t); | |
769 | ||
770 | return is_gimple_constant (t); | |
771 | } | |
772 | ||
773 | /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted | |
774 | form of gimple minimal invariant. */ | |
775 | ||
776 | bool | |
777 | is_gimple_ip_invariant (const_tree t) | |
778 | { | |
779 | if (TREE_CODE (t) == ADDR_EXPR) | |
780 | return is_gimple_ip_invariant_address (t); | |
781 | ||
782 | return is_gimple_constant (t); | |
783 | } | |
784 | ||
785 | /* Return true if T is a non-aggregate register variable. */ | |
786 | ||
787 | bool | |
788 | is_gimple_reg (tree t) | |
789 | { | |
790 | if (virtual_operand_p (t)) | |
791 | return false; | |
792 | ||
793 | if (TREE_CODE (t) == SSA_NAME) | |
794 | return true; | |
795 | ||
796 | if (!is_gimple_variable (t)) | |
797 | return false; | |
798 | ||
799 | if (!is_gimple_reg_type (TREE_TYPE (t))) | |
800 | return false; | |
801 | ||
802 | /* A volatile decl is not acceptable because we can't reuse it as | |
803 | needed. We need to copy it into a temp first. */ | |
804 | if (TREE_THIS_VOLATILE (t)) | |
805 | return false; | |
806 | ||
807 | /* We define "registers" as things that can be renamed as needed, | |
808 | which with our infrastructure does not apply to memory. */ | |
809 | if (needs_to_live_in_memory (t)) | |
810 | return false; | |
811 | ||
812 | /* Hard register variables are an interesting case. For those that | |
813 | are call-clobbered, we don't know where all the calls are, since | |
814 | we don't (want to) take into account which operations will turn | |
815 | into libcalls at the rtl level. For those that are call-saved, | |
816 | we don't currently model the fact that calls may in fact change | |
817 | global hard registers, nor do we examine ASM_CLOBBERS at the tree | |
818 | level, and so miss variable changes that might imply. All around, | |
819 | it seems safest to not do too much optimization with these at the | |
820 | tree level at all. We'll have to rely on the rtl optimizers to | |
821 | clean this up, as there we've got all the appropriate bits exposed. */ | |
822 | if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t)) | |
823 | return false; | |
824 | ||
eb72dc66 RB |
825 | /* Variables can be marked as having partial definitions, avoid |
826 | putting them into SSA form. */ | |
827 | return !DECL_NOT_GIMPLE_REG_P (t); | |
2a0603f1 AM |
828 | } |
829 | ||
830 | ||
831 | /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */ | |
832 | ||
833 | bool | |
834 | is_gimple_val (tree t) | |
835 | { | |
836 | /* Make loads from volatiles and memory vars explicit. */ | |
837 | if (is_gimple_variable (t) | |
838 | && is_gimple_reg_type (TREE_TYPE (t)) | |
839 | && !is_gimple_reg (t)) | |
840 | return false; | |
841 | ||
842 | return (is_gimple_variable (t) || is_gimple_min_invariant (t)); | |
843 | } | |
844 | ||
845 | /* Similarly, but accept hard registers as inputs to asm statements. */ | |
846 | ||
847 | bool | |
848 | is_gimple_asm_val (tree t) | |
849 | { | |
850 | if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t)) | |
851 | return true; | |
852 | ||
853 | return is_gimple_val (t); | |
854 | } | |
855 | ||
856 | /* Return true if T is a GIMPLE minimal lvalue. */ | |
857 | ||
858 | bool | |
859 | is_gimple_min_lval (tree t) | |
860 | { | |
861 | if (!(t = CONST_CAST_TREE (strip_invariant_refs (t)))) | |
862 | return false; | |
863 | return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF); | |
864 | } | |
865 | ||
866 | /* Return true if T is a valid function operand of a CALL_EXPR. */ | |
867 | ||
868 | bool | |
869 | is_gimple_call_addr (tree t) | |
870 | { | |
871 | return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t)); | |
872 | } | |
873 | ||
874 | /* Return true if T is a valid address operand of a MEM_REF. */ | |
875 | ||
876 | bool | |
877 | is_gimple_mem_ref_addr (tree t) | |
878 | { | |
879 | return (is_gimple_reg (t) | |
880 | || TREE_CODE (t) == INTEGER_CST | |
881 | || (TREE_CODE (t) == ADDR_EXPR | |
882 | && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0)) | |
883 | || decl_address_invariant_p (TREE_OPERAND (t, 0))))); | |
884 | } | |
45b0be94 | 885 | |
1b223a9f AO |
886 | /* Hold trees marked addressable during expand. */ |
887 | ||
888 | static hash_set<tree> *mark_addressable_queue; | |
889 | ||
890 | /* Mark X as addressable or queue it up if called during expand. We | |
891 | don't want to apply it immediately during expand because decls are | |
892 | made addressable at that point due to RTL-only concerns, such as | |
893 | uses of memcpy for block moves, and TREE_ADDRESSABLE changes | |
894 | is_gimple_reg, which might make it seem like a variable that used | |
895 | to be a gimple_reg shouldn't have been an SSA name. So we queue up | |
896 | this flag setting and only apply it when we're done with GIMPLE and | |
897 | only RTL issues matter. */ | |
898 | ||
899 | static void | |
900 | mark_addressable_1 (tree x) | |
901 | { | |
902 | if (!currently_expanding_to_rtl) | |
903 | { | |
904 | TREE_ADDRESSABLE (x) = 1; | |
905 | return; | |
906 | } | |
907 | ||
908 | if (!mark_addressable_queue) | |
909 | mark_addressable_queue = new hash_set<tree>(); | |
910 | mark_addressable_queue->add (x); | |
911 | } | |
912 | ||
913 | /* Adaptor for mark_addressable_1 for use in hash_set traversal. */ | |
914 | ||
aeb1e2bf | 915 | static bool |
1b223a9f AO |
916 | mark_addressable_2 (tree const &x, void * ATTRIBUTE_UNUSED = NULL) |
917 | { | |
918 | mark_addressable_1 (x); | |
919 | return false; | |
920 | } | |
921 | ||
922 | /* Mark all queued trees as addressable, and empty the queue. To be | |
923 | called right after clearing CURRENTLY_EXPANDING_TO_RTL. */ | |
924 | ||
925 | void | |
926 | flush_mark_addressable_queue () | |
927 | { | |
928 | gcc_assert (!currently_expanding_to_rtl); | |
929 | if (mark_addressable_queue) | |
930 | { | |
931 | mark_addressable_queue->traverse<void*, mark_addressable_2> (NULL); | |
932 | delete mark_addressable_queue; | |
933 | mark_addressable_queue = NULL; | |
934 | } | |
935 | } | |
936 | ||
45b0be94 AM |
937 | /* Mark X addressable. Unlike the langhook we expect X to be in gimple |
938 | form and we don't do any syntax checking. */ | |
939 | ||
940 | void | |
941 | mark_addressable (tree x) | |
942 | { | |
316bdb2e RB |
943 | if (TREE_CODE (x) == WITH_SIZE_EXPR) |
944 | x = TREE_OPERAND (x, 0); | |
45b0be94 AM |
945 | while (handled_component_p (x)) |
946 | x = TREE_OPERAND (x, 0); | |
b75f996e RB |
947 | if ((TREE_CODE (x) == MEM_REF |
948 | || TREE_CODE (x) == TARGET_MEM_REF) | |
45b0be94 AM |
949 | && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR) |
950 | x = TREE_OPERAND (TREE_OPERAND (x, 0), 0); | |
8813a647 | 951 | if (!VAR_P (x) |
45b0be94 AM |
952 | && TREE_CODE (x) != PARM_DECL |
953 | && TREE_CODE (x) != RESULT_DECL) | |
954 | return; | |
1b223a9f | 955 | mark_addressable_1 (x); |
45b0be94 AM |
956 | |
957 | /* Also mark the artificial SSA_NAME that points to the partition of X. */ | |
958 | if (TREE_CODE (x) == VAR_DECL | |
959 | && !DECL_EXTERNAL (x) | |
960 | && !TREE_STATIC (x) | |
961 | && cfun->gimple_df != NULL | |
962 | && cfun->gimple_df->decls_to_pointers != NULL) | |
963 | { | |
39c8aaa4 | 964 | tree *namep = cfun->gimple_df->decls_to_pointers->get (x); |
45b0be94 | 965 | if (namep) |
1b223a9f | 966 | mark_addressable_1 (*namep); |
45b0be94 AM |
967 | } |
968 | } | |
969 | ||
970 | /* Returns true iff T is a valid RHS for an assignment to a renamed | |
971 | user -- or front-end generated artificial -- variable. */ | |
972 | ||
973 | bool | |
974 | is_gimple_reg_rhs (tree t) | |
975 | { | |
976 | return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS; | |
977 | } | |
978 | ||
979 | #include "gt-gimple-expr.h" |