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