Line data Source code
1 : /* Gimple decl, type, and expression support functions.
2 :
3 : Copyright (C) 2007-2026 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 11788585101 : useless_type_conversion_p (tree outer_type, tree inner_type)
69 : {
70 : /* Do the following before stripping toplevel qualifiers. */
71 11788960978 : if (POINTER_TYPE_P (inner_type)
72 2947239878 : && POINTER_TYPE_P (outer_type))
73 : {
74 : /* Do not lose casts between pointers to different address spaces. */
75 2924039097 : if (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
76 2924039097 : != TYPE_ADDR_SPACE (TREE_TYPE (inner_type)))
77 : return false;
78 : /* Do not lose casts to function pointer types. */
79 5771991708 : if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (outer_type))
80 2931060657 : && !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 11787176457 : inner_type = TYPE_MAIN_VARIANT (inner_type);
86 11787176457 : outer_type = TYPE_MAIN_VARIANT (outer_type);
87 :
88 11787176457 : 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 1053782284 : 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 947738814 : if (INTEGRAL_TYPE_P (inner_type)
100 177052095 : && INTEGRAL_TYPE_P (outer_type))
101 : {
102 : /* Preserve changes in signedness or precision. */
103 162458429 : if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
104 162458429 : || 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 65122486 : if (((TREE_CODE (inner_type) == BOOLEAN_TYPE)
110 65122486 : != (TREE_CODE (outer_type) == BOOLEAN_TYPE))
111 65122486 : && 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 65075951 : if (BITINT_TYPE_P (inner_type) != BITINT_TYPE_P (outer_type))
120 : return false;
121 :
122 : /* We don't need to preserve changes in the types minimum or
123 : maximum value in general as these do not generate code
124 : unless the types precisions are different. */
125 : return true;
126 : }
127 :
128 : /* Scalar floating point types with the same mode are compatible. */
129 785280385 : else if (SCALAR_FLOAT_TYPE_P (inner_type)
130 596933 : && SCALAR_FLOAT_TYPE_P (outer_type))
131 : return true;
132 :
133 : /* Fixed point types with the same mode are compatible. */
134 784686660 : else if (FIXED_POINT_TYPE_P (inner_type)
135 0 : && FIXED_POINT_TYPE_P (outer_type))
136 0 : return TYPE_SATURATING (inner_type) == TYPE_SATURATING (outer_type);
137 :
138 : /* We need to take special care recursing to pointed-to types. */
139 784686660 : else if (POINTER_TYPE_P (inner_type)
140 756178322 : && POINTER_TYPE_P (outer_type))
141 : {
142 : /* We do not care for const qualification of the pointed-to types
143 : as const qualification has no semantic value to the middle-end. */
144 :
145 : /* Otherwise pointers/references are equivalent. */
146 : return true;
147 : }
148 :
149 : /* Recurse for complex types. */
150 45933924 : else if (TREE_CODE (inner_type) == COMPLEX_TYPE
151 34626 : && TREE_CODE (outer_type) == COMPLEX_TYPE)
152 33447 : return useless_type_conversion_p (TREE_TYPE (outer_type),
153 66894 : TREE_TYPE (inner_type));
154 :
155 : /* Recurse for vector types with the same number of subparts. */
156 45900477 : else if (VECTOR_TYPE_P (inner_type)
157 9690511 : && VECTOR_TYPE_P (outer_type))
158 19308738 : return (known_eq (TYPE_VECTOR_SUBPARTS (inner_type),
159 : TYPE_VECTOR_SUBPARTS (outer_type))
160 9647468 : && useless_type_conversion_p (TREE_TYPE (outer_type),
161 9647468 : TREE_TYPE (inner_type))
162 18983898 : && targetm.compatible_vector_types_p (inner_type, outer_type));
163 :
164 36246108 : else if (TREE_CODE (inner_type) == ARRAY_TYPE
165 622918 : && TREE_CODE (outer_type) == ARRAY_TYPE)
166 : {
167 : /* Preserve various attributes. */
168 466027 : if (TYPE_REVERSE_STORAGE_ORDER (inner_type)
169 466027 : != TYPE_REVERSE_STORAGE_ORDER (outer_type))
170 : return false;
171 466027 : if (TYPE_STRING_FLAG (inner_type) != TYPE_STRING_FLAG (outer_type))
172 : return false;
173 :
174 : /* Conversions from array types with unknown extent to
175 : array types with known extent are not useless. */
176 452436 : if (!TYPE_DOMAIN (inner_type) && TYPE_DOMAIN (outer_type))
177 : return false;
178 :
179 : /* Nor are conversions from array types with non-constant size to
180 : array types with constant size or to different size. */
181 450010 : if (TYPE_SIZE (outer_type)
182 434398 : && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST
183 860437 : && (!TYPE_SIZE (inner_type)
184 408733 : || TREE_CODE (TYPE_SIZE (inner_type)) != INTEGER_CST
185 406879 : || !tree_int_cst_equal (TYPE_SIZE (outer_type),
186 406879 : TYPE_SIZE (inner_type))))
187 40520 : return false;
188 :
189 : /* Check conversions between arrays with partially known extents.
190 : If the array min/max values are constant they have to match.
191 : Otherwise allow conversions to unknown and variable extents.
192 : In particular this declares conversions that may change the
193 : mode to BLKmode as useless. */
194 409490 : if (TYPE_DOMAIN (inner_type)
195 409488 : && TYPE_DOMAIN (outer_type)
196 818956 : && TYPE_DOMAIN (inner_type) != TYPE_DOMAIN (outer_type))
197 : {
198 94819 : tree inner_min = TYPE_MIN_VALUE (TYPE_DOMAIN (inner_type));
199 94819 : tree outer_min = TYPE_MIN_VALUE (TYPE_DOMAIN (outer_type));
200 94819 : tree inner_max = TYPE_MAX_VALUE (TYPE_DOMAIN (inner_type));
201 94819 : tree outer_max = TYPE_MAX_VALUE (TYPE_DOMAIN (outer_type));
202 :
203 : /* After gimplification a variable min/max value carries no
204 : additional information compared to a NULL value. All that
205 : matters has been lowered to be part of the IL. */
206 94819 : if (inner_min && TREE_CODE (inner_min) != INTEGER_CST)
207 0 : inner_min = NULL_TREE;
208 94819 : if (outer_min && TREE_CODE (outer_min) != INTEGER_CST)
209 0 : outer_min = NULL_TREE;
210 94819 : if (inner_max && TREE_CODE (inner_max) != INTEGER_CST)
211 22137 : inner_max = NULL_TREE;
212 94819 : if (outer_max && TREE_CODE (outer_max) != INTEGER_CST)
213 26440 : outer_max = NULL_TREE;
214 :
215 : /* Conversions NULL / variable <- cst are useless, but not
216 : the other way around. */
217 94819 : if (outer_min
218 94819 : && (!inner_min
219 94819 : || !tree_int_cst_equal (inner_min, outer_min)))
220 8740 : return false;
221 86079 : if (outer_max
222 86079 : && (!inner_max
223 59627 : || !tree_int_cst_equal (inner_max, outer_max)))
224 58320 : return false;
225 : }
226 :
227 : /* Recurse on the element check. */
228 342430 : return useless_type_conversion_p (TREE_TYPE (outer_type),
229 684860 : TREE_TYPE (inner_type));
230 : }
231 :
232 35780081 : else if (FUNC_OR_METHOD_TYPE_P (inner_type)
233 327231 : && TREE_CODE (inner_type) == TREE_CODE (outer_type))
234 : {
235 321553 : tree outer_parm, inner_parm;
236 :
237 : /* If the return types are not compatible bail out. */
238 321553 : if (!useless_type_conversion_p (TREE_TYPE (outer_type),
239 321553 : TREE_TYPE (inner_type)))
240 : return false;
241 :
242 : /* Method types should belong to a compatible base class. */
243 313312 : if (TREE_CODE (inner_type) == METHOD_TYPE
244 326939 : && !useless_type_conversion_p (TYPE_METHOD_BASETYPE (outer_type),
245 13627 : TYPE_METHOD_BASETYPE (inner_type)))
246 : return false;
247 :
248 : /* A conversion to an unprototyped argument list is ok. */
249 305381 : if (!prototype_p (outer_type))
250 : return true;
251 :
252 : /* If the unqualified argument types are compatible the conversion
253 : is useless. */
254 305274 : if (TYPE_ARG_TYPES (outer_type) == TYPE_ARG_TYPES (inner_type))
255 : return true;
256 :
257 300864 : for (outer_parm = TYPE_ARG_TYPES (outer_type),
258 300864 : inner_parm = TYPE_ARG_TYPES (inner_type);
259 1249606 : outer_parm && inner_parm;
260 948742 : outer_parm = TREE_CHAIN (outer_parm),
261 948742 : inner_parm = TREE_CHAIN (inner_parm))
262 1903440 : if (!useless_type_conversion_p
263 951720 : (TYPE_MAIN_VARIANT (TREE_VALUE (outer_parm)),
264 951720 : TYPE_MAIN_VARIANT (TREE_VALUE (inner_parm))))
265 : return false;
266 :
267 : /* If there is a mismatch in the number of arguments the functions
268 : are not compatible. */
269 297886 : if (outer_parm || inner_parm)
270 : return false;
271 :
272 : /* Defer to the target if necessary. */
273 296261 : if (TYPE_ATTRIBUTES (inner_type) || TYPE_ATTRIBUTES (outer_type))
274 287930 : return comp_type_attributes (outer_type, inner_type) != 0;
275 :
276 : return true;
277 : }
278 :
279 : /* For aggregates we rely on TYPE_CANONICAL exclusively and require
280 : explicit conversions for types involving to be structurally
281 : compared types. */
282 35458528 : else if (AGGREGATE_TYPE_P (inner_type)
283 3383140 : && TREE_CODE (inner_type) == TREE_CODE (outer_type))
284 2359290 : return TYPE_CANONICAL (inner_type)
285 2359290 : && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type);
286 :
287 33099238 : else if (TREE_CODE (inner_type) == OFFSET_TYPE
288 9908 : && TREE_CODE (outer_type) == OFFSET_TYPE)
289 2554 : return useless_type_conversion_p (TREE_TYPE (outer_type),
290 2554 : TREE_TYPE (inner_type))
291 4928 : && useless_type_conversion_p
292 2374 : (TYPE_OFFSET_BASETYPE (outer_type),
293 2374 : TYPE_OFFSET_BASETYPE (inner_type));
294 :
295 : return false;
296 : }
297 :
298 :
299 : /* ----- Decl related ----- */
300 :
301 : /* Set sequence SEQ to be the GIMPLE body for function FN. */
302 :
303 : void
304 117746479 : gimple_set_body (tree fndecl, gimple_seq seq)
305 : {
306 117746479 : struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
307 117746479 : if (fn == NULL)
308 : {
309 : /* If FNDECL still does not have a function structure associated
310 : with it, then it does not make sense for it to receive a
311 : GIMPLE body. */
312 62 : gcc_assert (seq == NULL);
313 : }
314 : else
315 117746417 : fn->gimple_body = seq;
316 117746479 : }
317 :
318 :
319 : /* Return the body of GIMPLE statements for function FN. After the
320 : CFG pass, the function body doesn't exist anymore because it has
321 : been split up into basic blocks. In this case, it returns
322 : NULL. */
323 :
324 : gimple_seq
325 430184324 : gimple_body (tree fndecl)
326 : {
327 430184324 : struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
328 430184324 : return fn ? fn->gimple_body : NULL;
329 : }
330 :
331 : /* Return true when FNDECL has Gimple body either in unlowered
332 : or CFG form. */
333 : bool
334 394838150 : gimple_has_body_p (tree fndecl)
335 : {
336 394838150 : struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
337 394838150 : return (gimple_body (fndecl) || (fn && fn->cfg && !(fn->curr_properties & PROP_rtl)));
338 : }
339 :
340 : /* Return a printable name for symbol DECL. */
341 :
342 : const char *
343 33936 : gimple_decl_printable_name (tree decl, int verbosity)
344 : {
345 33936 : if (!DECL_NAME (decl))
346 : return NULL;
347 :
348 33936 : if (HAS_DECL_ASSEMBLER_NAME_P (decl) && DECL_ASSEMBLER_NAME_SET_P (decl))
349 : {
350 33575 : int dmgl_opts = DMGL_NO_OPTS;
351 :
352 33575 : if (verbosity >= 2)
353 : {
354 4509 : dmgl_opts = DMGL_VERBOSE
355 : | DMGL_ANSI
356 : | DMGL_GNU_V3
357 : | DMGL_RET_POSTFIX;
358 4509 : if (TREE_CODE (decl) == FUNCTION_DECL)
359 4436 : dmgl_opts |= DMGL_PARAMS;
360 : }
361 :
362 33575 : const char *mangled_str
363 33575 : = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME_RAW (decl));
364 33575 : const char *str = cplus_demangle_v3 (mangled_str, dmgl_opts);
365 65742 : return str ? str : mangled_str;
366 : }
367 :
368 361 : return IDENTIFIER_POINTER (DECL_NAME (decl));
369 : }
370 :
371 :
372 : /* Create a new VAR_DECL and copy information from VAR to it. */
373 :
374 : tree
375 1518838 : copy_var_decl (tree var, tree name, tree type)
376 : {
377 1518838 : tree copy = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, name, type);
378 :
379 1518838 : TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (var);
380 1518838 : TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (var);
381 1518838 : DECL_NOT_GIMPLE_REG_P (copy) = DECL_NOT_GIMPLE_REG_P (var);
382 1518838 : DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (var);
383 1518838 : DECL_IGNORED_P (copy) = DECL_IGNORED_P (var);
384 1518838 : DECL_CONTEXT (copy) = DECL_CONTEXT (var);
385 1518838 : TREE_USED (copy) = 1;
386 1518838 : DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
387 1518838 : DECL_ATTRIBUTES (copy) = DECL_ATTRIBUTES (var);
388 1518838 : if (DECL_USER_ALIGN (var))
389 : {
390 145 : SET_DECL_ALIGN (copy, DECL_ALIGN (var));
391 145 : DECL_USER_ALIGN (copy) = 1;
392 : }
393 :
394 1518838 : copy_warning (copy, var);
395 1518838 : return copy;
396 : }
397 :
398 : /* Strip off a legitimate source ending from the input string NAME of
399 : length LEN. Rather than having to know the names used by all of
400 : our front ends, we strip off an ending of a period followed by
401 : up to four characters. (like ".cpp".) */
402 :
403 : static inline void
404 24001925 : remove_suffix (char *name, int len)
405 : {
406 24001925 : int i;
407 :
408 90744530 : for (i = 2; i < 7 && len > i; i++)
409 67889735 : if (name[len - i] == '.')
410 : {
411 1147130 : name[len - i] = '\0';
412 1147130 : break;
413 : }
414 24001925 : }
415 :
416 : /* Create a new temporary name with PREFIX. Return an identifier. */
417 :
418 : static GTY(()) unsigned int tmp_var_id_num;
419 :
420 : tree
421 24001925 : create_tmp_var_name (const char *prefix)
422 : {
423 24001925 : char *tmp_name;
424 :
425 24001925 : if (prefix)
426 : {
427 24001925 : char *preftmp = ASTRDUP (prefix);
428 :
429 24001925 : remove_suffix (preftmp, strlen (preftmp));
430 24001925 : prefix = preftmp;
431 : }
432 :
433 24001925 : ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
434 24001925 : return get_identifier (tmp_name);
435 : }
436 :
437 : /* Create a new temporary variable declaration of type TYPE.
438 : Do NOT push it into the current binding. */
439 :
440 : tree
441 27301795 : create_tmp_var_raw (tree type, const char *prefix)
442 : {
443 27301795 : tree tmp_var;
444 :
445 47651170 : tmp_var = build_decl (input_location,
446 20349375 : VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
447 : type);
448 :
449 : /* The variable was declared by the compiler. */
450 27301795 : DECL_ARTIFICIAL (tmp_var) = 1;
451 : /* And we don't want debug info for it. */
452 27301795 : DECL_IGNORED_P (tmp_var) = 1;
453 : /* And we don't want even the fancy names of those printed in
454 : -fdump-final-insns= dumps. */
455 27301795 : DECL_NAMELESS (tmp_var) = 1;
456 :
457 : /* Make the variable writable. */
458 27301795 : TREE_READONLY (tmp_var) = 0;
459 :
460 27301795 : DECL_EXTERNAL (tmp_var) = 0;
461 27301795 : TREE_STATIC (tmp_var) = 0;
462 27301795 : TREE_USED (tmp_var) = 1;
463 :
464 27301795 : return tmp_var;
465 : }
466 :
467 : /* Create a new temporary variable declaration of type TYPE. DO push the
468 : variable into the current binding. Further, assume that this is called
469 : only from gimplification or optimization, at which point the creation of
470 : certain types are bugs. */
471 :
472 : tree
473 15005108 : create_tmp_var (tree type, const char *prefix)
474 : {
475 15005108 : tree tmp_var;
476 :
477 : /* We don't allow types that are addressable (meaning we can't make copies),
478 : or incomplete. We also used to reject every variable size objects here,
479 : but now support those for which a constant upper bound can be obtained.
480 : The processing for variable sizes is performed in gimple_add_tmp_var,
481 : point at which it really matters and possibly reached via paths not going
482 : through this function, e.g. after direct calls to create_tmp_var_raw. */
483 15005108 : gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
484 :
485 15005108 : tmp_var = create_tmp_var_raw (type, prefix);
486 15005108 : gimple_add_tmp_var (tmp_var);
487 15005108 : return tmp_var;
488 : }
489 :
490 : /* Create a new temporary variable declaration of type TYPE by calling
491 : create_tmp_var and if TYPE is a vector or a complex number, mark the new
492 : temporary as gimple register. */
493 :
494 : tree
495 3547865 : create_tmp_reg (tree type, const char *prefix)
496 : {
497 3547865 : return create_tmp_var (type, prefix);
498 : }
499 :
500 : /* Create a new temporary variable declaration of type TYPE by calling
501 : create_tmp_var and if TYPE is a vector or a complex number, mark the new
502 : temporary as gimple register. */
503 :
504 : tree
505 24028 : create_tmp_reg_fn (struct function *fn, tree type, const char *prefix)
506 : {
507 24028 : tree tmp;
508 :
509 24028 : tmp = create_tmp_var_raw (type, prefix);
510 24028 : gimple_add_tmp_var_fn (fn, tmp);
511 :
512 24028 : return tmp;
513 : }
514 :
515 :
516 : /* ----- Expression related ----- */
517 :
518 : /* Extract the operands and code for expression EXPR into *SUBCODE_P,
519 : *OP1_P, *OP2_P and *OP3_P respectively. */
520 :
521 : void
522 174664966 : extract_ops_from_tree (tree expr, enum tree_code *subcode_p, tree *op1_p,
523 : tree *op2_p, tree *op3_p)
524 : {
525 174664966 : *subcode_p = TREE_CODE (expr);
526 174664966 : switch (get_gimple_rhs_class (*subcode_p))
527 : {
528 105736 : case GIMPLE_TERNARY_RHS:
529 105736 : {
530 105736 : *op1_p = TREE_OPERAND (expr, 0);
531 105736 : *op2_p = TREE_OPERAND (expr, 1);
532 105736 : *op3_p = TREE_OPERAND (expr, 2);
533 105736 : break;
534 : }
535 21555824 : case GIMPLE_BINARY_RHS:
536 21555824 : {
537 21555824 : *op1_p = TREE_OPERAND (expr, 0);
538 21555824 : *op2_p = TREE_OPERAND (expr, 1);
539 21555824 : *op3_p = NULL_TREE;
540 21555824 : break;
541 : }
542 10742306 : case GIMPLE_UNARY_RHS:
543 10742306 : {
544 10742306 : *op1_p = TREE_OPERAND (expr, 0);
545 10742306 : *op2_p = NULL_TREE;
546 10742306 : *op3_p = NULL_TREE;
547 10742306 : break;
548 : }
549 142261100 : case GIMPLE_SINGLE_RHS:
550 142261100 : {
551 142261100 : *op1_p = expr;
552 142261100 : *op2_p = NULL_TREE;
553 142261100 : *op3_p = NULL_TREE;
554 142261100 : break;
555 : }
556 0 : default:
557 0 : gcc_unreachable ();
558 : }
559 174664966 : }
560 :
561 : /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
562 :
563 : void
564 6042173 : gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
565 : tree *lhs_p, tree *rhs_p)
566 : {
567 6042173 : gcc_assert (COMPARISON_CLASS_P (cond)
568 : || TREE_CODE (cond) == TRUTH_NOT_EXPR
569 : || is_gimple_min_invariant (cond)
570 : || SSA_VAR_P (cond));
571 6042173 : gcc_checking_assert (!tree_could_throw_p (cond));
572 :
573 6042173 : extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
574 :
575 : /* Canonicalize conditionals of the form 'if (!VAL)'. */
576 6042173 : if (*code_p == TRUTH_NOT_EXPR)
577 : {
578 0 : *code_p = EQ_EXPR;
579 0 : gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
580 0 : *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
581 : }
582 : /* Canonicalize conditionals of the form 'if (VAL)' */
583 6042173 : else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
584 : {
585 1805819 : *code_p = NE_EXPR;
586 1805819 : gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
587 1805819 : *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
588 : }
589 6042173 : }
590 :
591 : /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
592 :
593 : bool
594 255543310 : is_gimple_lvalue (tree t)
595 : {
596 255543310 : return (is_gimple_addressable (t)
597 14132060 : || TREE_CODE (t) == WITH_SIZE_EXPR
598 : /* These are complex lvalues, but don't have addresses, so they
599 : go here. */
600 269651256 : || TREE_CODE (t) == BIT_FIELD_REF);
601 : }
602 :
603 : /* Helper for is_gimple_condexpr and is_gimple_condexpr_for_cond. */
604 :
605 : static bool
606 10154343 : is_gimple_condexpr_1 (tree t, bool allow_traps, bool allow_cplx)
607 : {
608 10154343 : tree op0;
609 10154343 : return (is_gimple_val (t)
610 10154343 : || (COMPARISON_CLASS_P (t)
611 6999856 : && (allow_traps || !tree_could_throw_p (t))
612 6992987 : && ((op0 = TREE_OPERAND (t, 0)), true)
613 6992987 : && (allow_cplx || TREE_CODE (TREE_TYPE (op0)) != COMPLEX_TYPE)
614 6992987 : && is_gimple_val (op0)
615 5430632 : && is_gimple_val (TREE_OPERAND (t, 1))));
616 : }
617 :
618 : /* Like is_gimple_condexpr, but does not allow T to trap. */
619 :
620 : bool
621 6541041 : is_gimple_condexpr_for_cond (tree t)
622 : {
623 6541041 : return is_gimple_condexpr_1 (t, false, true);
624 : }
625 :
626 : /* Canonicalize a tree T for use in a COND_EXPR as conditional. Returns
627 : a canonicalized tree that is valid for a COND_EXPR or NULL_TREE, if
628 : we failed to create one. */
629 :
630 : tree
631 3613302 : canonicalize_cond_expr_cond (tree t)
632 : {
633 : /* Strip conversions around boolean operations. */
634 3462429 : if (CONVERT_EXPR_P (t)
635 3613302 : && (truth_value_p (TREE_CODE (TREE_OPERAND (t, 0)))
636 149753 : || TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
637 : == BOOLEAN_TYPE))
638 1134 : t = TREE_OPERAND (t, 0);
639 :
640 : /* For !x use x == 0. */
641 3613302 : if (TREE_CODE (t) == TRUTH_NOT_EXPR)
642 : {
643 22422 : tree top0 = TREE_OPERAND (t, 0);
644 22422 : t = build2 (EQ_EXPR, TREE_TYPE (t),
645 22422 : top0, build_int_cst (TREE_TYPE (top0), 0));
646 : }
647 : /* For cmp ? 1 : 0 use cmp. */
648 3590880 : else if (TREE_CODE (t) == COND_EXPR
649 101380 : && COMPARISON_CLASS_P (TREE_OPERAND (t, 0))
650 99665 : && integer_onep (TREE_OPERAND (t, 1))
651 3690545 : && integer_zerop (TREE_OPERAND (t, 2)))
652 : {
653 99665 : tree top0 = TREE_OPERAND (t, 0);
654 99665 : t = build2 (TREE_CODE (top0), TREE_TYPE (t),
655 99665 : TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1));
656 : }
657 : /* For x ^ y use x != y. */
658 3491215 : else if (TREE_CODE (t) == BIT_XOR_EXPR)
659 0 : t = build2 (NE_EXPR, TREE_TYPE (t),
660 0 : TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
661 :
662 : /* We don't know where this will be used so allow both traps and
663 : _Complex. The caller is responsible for more precise checking. */
664 3613302 : if (is_gimple_condexpr_1 (t, true, true))
665 1269625 : return t;
666 :
667 : return NULL_TREE;
668 : }
669 :
670 : /* Return true if T is a gimple address. */
671 :
672 : bool
673 0 : is_gimple_address (const_tree t)
674 : {
675 0 : tree op;
676 :
677 0 : if (TREE_CODE (t) != ADDR_EXPR)
678 : return false;
679 :
680 0 : op = TREE_OPERAND (t, 0);
681 0 : while (handled_component_p (op))
682 : {
683 0 : if ((TREE_CODE (op) == ARRAY_REF
684 0 : || TREE_CODE (op) == ARRAY_RANGE_REF)
685 0 : && !is_gimple_val (TREE_OPERAND (op, 1)))
686 : return false;
687 :
688 0 : op = TREE_OPERAND (op, 0);
689 : }
690 :
691 0 : if (CONSTANT_CLASS_P (op)
692 0 : || TREE_CODE (op) == TARGET_MEM_REF
693 0 : || TREE_CODE (op) == MEM_REF)
694 : return true;
695 :
696 0 : switch (TREE_CODE (op))
697 : {
698 : case PARM_DECL:
699 : case RESULT_DECL:
700 : case LABEL_DECL:
701 : case FUNCTION_DECL:
702 : case VAR_DECL:
703 : case CONST_DECL:
704 : return true;
705 :
706 : default:
707 : return false;
708 : }
709 : }
710 :
711 : /* Return true if T is a gimple invariant address. */
712 :
713 : bool
714 5702905751 : is_gimple_invariant_address (const_tree t)
715 : {
716 5702905751 : const_tree op;
717 :
718 5702905751 : if (TREE_CODE (t) != ADDR_EXPR)
719 : return false;
720 :
721 5702905751 : op = strip_invariant_refs (TREE_OPERAND (t, 0));
722 5702905751 : if (!op)
723 : return false;
724 :
725 5690699488 : if (TREE_CODE (op) == MEM_REF)
726 : {
727 447514126 : const_tree op0 = TREE_OPERAND (op, 0);
728 447514126 : return (TREE_CODE (op0) == ADDR_EXPR
729 447514126 : && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
730 148096604 : || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
731 : }
732 :
733 5243185362 : return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
734 : }
735 :
736 : /* Return true if T is a gimple invariant address at IPA level
737 : (so addresses of variables on stack are not allowed). */
738 :
739 : bool
740 2351511 : is_gimple_ip_invariant_address (const_tree t)
741 : {
742 2351511 : const_tree op;
743 :
744 2351511 : if (TREE_CODE (t) != ADDR_EXPR)
745 : return false;
746 :
747 2351511 : op = strip_invariant_refs (TREE_OPERAND (t, 0));
748 2351511 : if (!op)
749 : return false;
750 :
751 2350075 : if (TREE_CODE (op) == MEM_REF)
752 : {
753 112647 : const_tree op0 = TREE_OPERAND (op, 0);
754 112647 : return (TREE_CODE (op0) == ADDR_EXPR
755 112647 : && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
756 84639 : || decl_address_ip_invariant_p (TREE_OPERAND (op0, 0))));
757 : }
758 :
759 2237428 : return CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op);
760 : }
761 :
762 : /* Return true if T is a GIMPLE minimal invariant. It's a restricted
763 : form of function invariant. */
764 :
765 : bool
766 22154747046 : is_gimple_min_invariant (const_tree t)
767 : {
768 22154747046 : if (TREE_CODE (t) == ADDR_EXPR)
769 5701692171 : return is_gimple_invariant_address (t);
770 :
771 16453054875 : return is_gimple_constant (t);
772 : }
773 :
774 : /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
775 : form of gimple minimal invariant. */
776 :
777 : bool
778 25655092 : is_gimple_ip_invariant (const_tree t)
779 : {
780 25655092 : if (TREE_CODE (t) == ADDR_EXPR)
781 2350297 : return is_gimple_ip_invariant_address (t);
782 :
783 23304795 : return is_gimple_constant (t);
784 : }
785 :
786 : /* Return true if T is a non-aggregate register variable. */
787 :
788 : bool
789 44095773585 : is_gimple_reg (tree t)
790 : {
791 76934022192 : if (virtual_operand_p (t))
792 : return false;
793 :
794 43948234274 : if (TREE_CODE (t) == SSA_NAME)
795 : return true;
796 :
797 16303422832 : if (!is_gimple_variable (t))
798 : return false;
799 :
800 5560777322 : if (!is_gimple_reg_type (TREE_TYPE (t)))
801 : return false;
802 :
803 : /* A volatile decl is not acceptable because we can't reuse it as
804 : needed. We need to copy it into a temp first. */
805 2204009345 : if (TREE_THIS_VOLATILE (t))
806 : return false;
807 :
808 : /* We define "registers" as things that can be renamed as needed,
809 : which with our infrastructure does not apply to memory. */
810 1961856714 : if (needs_to_live_in_memory (t))
811 : return false;
812 :
813 : /* Hard register variables are an interesting case. For those that
814 : are call-clobbered, we don't know where all the calls are, since
815 : we don't (want to) take into account which operations will turn
816 : into libcalls at the rtl level. For those that are call-saved,
817 : we don't currently model the fact that calls may in fact change
818 : global hard registers, nor do we examine ASM_CLOBBERS at the tree
819 : level, and so miss variable changes that might imply. All around,
820 : it seems safest to not do too much optimization with these at the
821 : tree level at all. We'll have to rely on the rtl optimizers to
822 : clean this up, as there we've got all the appropriate bits exposed. */
823 1149263792 : if (VAR_P (t) && DECL_HARD_REGISTER (t))
824 : return false;
825 :
826 : /* Variables can be marked as having partial definitions, avoid
827 : putting them into SSA form. */
828 1147813767 : return !DECL_NOT_GIMPLE_REG_P (t);
829 : }
830 :
831 :
832 : /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
833 :
834 : bool
835 8955072872 : is_gimple_val (tree t)
836 : {
837 : /* Make loads from volatiles and memory vars explicit. */
838 8955072872 : if (is_gimple_variable (t)
839 4765401566 : && is_gimple_reg_type (TREE_TYPE (t))
840 13696591183 : && !is_gimple_reg (t))
841 : return false;
842 :
843 : /* These eventually expand into constants, so treat them like that. */
844 8944668468 : if (TREE_CODE (t) == OMP_NEXT_VARIANT
845 8944668468 : || TREE_CODE (t) == OMP_TARGET_DEVICE_MATCHES)
846 : return true;
847 :
848 8944668416 : 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 43788 : is_gimple_asm_val (tree t)
855 : {
856 43788 : if (VAR_P (t) && DECL_HARD_REGISTER (t))
857 : return true;
858 :
859 42911 : return is_gimple_val (t);
860 : }
861 :
862 : /* Return true if T is a GIMPLE minimal lvalue. */
863 :
864 : bool
865 30108325 : is_gimple_min_lval (tree t)
866 : {
867 30108325 : if (!(t = const_cast<tree> (strip_invariant_refs (t))))
868 : return false;
869 30108310 : 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 1033631507 : is_gimple_call_addr (tree t)
876 : {
877 1033631507 : 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 1382414891 : is_gimple_mem_ref_addr (tree t)
884 : {
885 1382414891 : return (is_gimple_reg (t)
886 379710232 : || poly_int_tree_p (t)
887 1761733328 : || (TREE_CODE (t) == ADDR_EXPR
888 377056870 : && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
889 375168448 : || 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 10816365 : mark_addressable_1 (tree x)
907 : {
908 10816365 : if (!currently_expanding_to_rtl)
909 : {
910 10755418 : TREE_ADDRESSABLE (x) = 1;
911 10755418 : return;
912 : }
913 :
914 60947 : if (!mark_addressable_queue)
915 15183 : mark_addressable_queue = new hash_set<tree>();
916 60947 : mark_addressable_queue->add (x);
917 : }
918 :
919 : /* Adaptor for mark_addressable_1 for use in hash_set traversal. */
920 :
921 : static bool
922 37689 : mark_addressable_2 (tree const &x, void * ATTRIBUTE_UNUSED = NULL)
923 : {
924 37689 : mark_addressable_1 (x);
925 37689 : 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 1489148 : flush_mark_addressable_queue ()
933 : {
934 1489148 : gcc_assert (!currently_expanding_to_rtl);
935 1489148 : if (mark_addressable_queue)
936 : {
937 52872 : mark_addressable_queue->traverse<void*, mark_addressable_2> (NULL);
938 30366 : delete mark_addressable_queue;
939 15183 : mark_addressable_queue = NULL;
940 : }
941 1489148 : }
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 33057647 : mark_addressable (tree x)
948 : {
949 33057647 : if (TREE_CODE (x) == WITH_SIZE_EXPR)
950 0 : x = TREE_OPERAND (x, 0);
951 38353182 : while (handled_component_p (x))
952 5295535 : x = TREE_OPERAND (x, 0);
953 33057647 : if ((TREE_CODE (x) == MEM_REF
954 33057647 : || TREE_CODE (x) == TARGET_MEM_REF)
955 33057647 : && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
956 11007 : x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
957 33057647 : if (!VAR_P (x)
958 : && TREE_CODE (x) != PARM_DECL
959 : && TREE_CODE (x) != RESULT_DECL)
960 : return;
961 10770309 : mark_addressable_1 (x);
962 :
963 : /* Also mark the artificial SSA_NAME that points to the partition of X. */
964 10770309 : if (VAR_P (x)
965 10174290 : && !DECL_EXTERNAL (x)
966 9043327 : && !TREE_STATIC (x)
967 5706662 : && cfun->gimple_df != NULL
968 16476971 : && cfun->gimple_df->decls_to_pointers != NULL)
969 : {
970 14436 : tree *namep = cfun->gimple_df->decls_to_pointers->get (x);
971 14436 : if (namep)
972 8367 : 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 813074 : is_gimple_reg_rhs (tree t)
981 : {
982 813074 : return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
983 : }
984 :
985 : #include "gt-gimple-expr.h"
|