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 11799193984 : useless_type_conversion_p (tree outer_type, tree inner_type)
69 : {
70 : /* Do the following before stripping toplevel qualifiers. */
71 11799729088 : if (POINTER_TYPE_P (inner_type)
72 2959234882 : && POINTER_TYPE_P (outer_type))
73 : {
74 : /* Do not lose casts between pointers to different address spaces. */
75 2929776890 : if (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
76 2929776890 : != TYPE_ADDR_SPACE (TREE_TYPE (inner_type)))
77 : return false;
78 : /* Do not lose casts to function pointer types. */
79 5783787795 : if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (outer_type))
80 2937051372 : && !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 11798253826 : inner_type = TYPE_MAIN_VARIANT (inner_type);
86 11798253826 : outer_type = TYPE_MAIN_VARIANT (outer_type);
87 :
88 11798253826 : 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 1046567384 : 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 940242154 : if (INTEGRAL_TYPE_P (inner_type)
100 173494318 : && INTEGRAL_TYPE_P (outer_type))
101 : {
102 : /* Preserve changes in signedness or precision. */
103 161242659 : if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
104 161242659 : || 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 66355614 : if (((TREE_CODE (inner_type) == BOOLEAN_TYPE)
110 66355614 : != (TREE_CODE (outer_type) == BOOLEAN_TYPE))
111 66355614 : && 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 66297054 : if ((TREE_CODE (inner_type) == BITINT_TYPE)
120 66297054 : != (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 778999495 : else if (SCALAR_FLOAT_TYPE_P (inner_type)
131 583841 : && SCALAR_FLOAT_TYPE_P (outer_type))
132 : return true;
133 :
134 : /* Fixed point types with the same mode are compatible. */
135 778418836 : 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 778418836 : else if (POINTER_TYPE_P (inner_type)
141 751936683 : && 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 47601928 : else if (TREE_CODE (inner_type) == COMPLEX_TYPE
152 33467 : && TREE_CODE (outer_type) == COMPLEX_TYPE)
153 32609 : return useless_type_conversion_p (TREE_TYPE (outer_type),
154 65218 : TREE_TYPE (inner_type));
155 :
156 : /* Recurse for vector types with the same number of subparts. */
157 47569319 : else if (VECTOR_TYPE_P (inner_type)
158 9683033 : && VECTOR_TYPE_P (outer_type))
159 19293912 : return (known_eq (TYPE_VECTOR_SUBPARTS (inner_type),
160 : TYPE_VECTOR_SUBPARTS (outer_type))
161 9640055 : && useless_type_conversion_p (TREE_TYPE (outer_type),
162 9640055 : TREE_TYPE (inner_type))
163 18978697 : && targetm.compatible_vector_types_p (inner_type, outer_type));
164 :
165 37922363 : else if (TREE_CODE (inner_type) == ARRAY_TYPE
166 836397 : && TREE_CODE (outer_type) == ARRAY_TYPE)
167 : {
168 : /* Preserve various attributes. */
169 678416 : if (TYPE_REVERSE_STORAGE_ORDER (inner_type)
170 678416 : != TYPE_REVERSE_STORAGE_ORDER (outer_type))
171 : return false;
172 678416 : 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 643827 : 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 641391 : if (TYPE_SIZE (outer_type)
183 624980 : && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST
184 1241430 : && (!TYPE_SIZE (inner_type)
185 598343 : || TREE_CODE (TYPE_SIZE (inner_type)) != INTEGER_CST
186 596489 : || !tree_int_cst_equal (TYPE_SIZE (outer_type),
187 596489 : TYPE_SIZE (inner_type))))
188 40546 : 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 600845 : if (TYPE_DOMAIN (inner_type)
196 600843 : && TYPE_DOMAIN (outer_type)
197 1201666 : && TYPE_DOMAIN (inner_type) != TYPE_DOMAIN (outer_type))
198 : {
199 136400 : tree inner_min = TYPE_MIN_VALUE (TYPE_DOMAIN (inner_type));
200 136400 : tree outer_min = TYPE_MIN_VALUE (TYPE_DOMAIN (outer_type));
201 136400 : tree inner_max = TYPE_MAX_VALUE (TYPE_DOMAIN (inner_type));
202 136400 : 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 136400 : if (inner_min && TREE_CODE (inner_min) != INTEGER_CST)
208 0 : inner_min = NULL_TREE;
209 136400 : if (outer_min && TREE_CODE (outer_min) != INTEGER_CST)
210 0 : outer_min = NULL_TREE;
211 136400 : if (inner_max && TREE_CODE (inner_max) != INTEGER_CST)
212 23107 : inner_max = NULL_TREE;
213 136400 : if (outer_max && TREE_CODE (outer_max) != INTEGER_CST)
214 27406 : outer_max = NULL_TREE;
215 :
216 : /* Conversions NULL / variable <- cst are useless, but not
217 : the other way around. */
218 136400 : if (outer_min
219 136400 : && (!inner_min
220 136400 : || !tree_int_cst_equal (inner_min, outer_min)))
221 8551 : return false;
222 127849 : if (outer_max
223 127849 : && (!inner_max
224 100431 : || !tree_int_cst_equal (inner_max, outer_max)))
225 89799 : return false;
226 : }
227 :
228 : /* Recurse on the element check. */
229 502495 : return useless_type_conversion_p (TREE_TYPE (outer_type),
230 1004990 : TREE_TYPE (inner_type));
231 : }
232 :
233 37243947 : else if (FUNC_OR_METHOD_TYPE_P (inner_type)
234 322540 : && TREE_CODE (inner_type) == TREE_CODE (outer_type))
235 : {
236 316994 : tree outer_parm, inner_parm;
237 :
238 : /* If the return types are not compatible bail out. */
239 316994 : if (!useless_type_conversion_p (TREE_TYPE (outer_type),
240 316994 : TREE_TYPE (inner_type)))
241 : return false;
242 :
243 : /* Method types should belong to a compatible base class. */
244 308887 : if (TREE_CODE (inner_type) == METHOD_TYPE
245 323621 : && !useless_type_conversion_p (TYPE_METHOD_BASETYPE (outer_type),
246 14734 : TYPE_METHOD_BASETYPE (inner_type)))
247 : return false;
248 :
249 : /* A conversion to an unprototyped argument list is ok. */
250 300530 : if (!prototype_p (outer_type))
251 : return true;
252 :
253 : /* If the unqualified argument types are compatible the conversion
254 : is useless. */
255 300423 : if (TYPE_ARG_TYPES (outer_type) == TYPE_ARG_TYPES (inner_type))
256 : return true;
257 :
258 296019 : for (outer_parm = TYPE_ARG_TYPES (outer_type),
259 296019 : inner_parm = TYPE_ARG_TYPES (inner_type);
260 1245888 : outer_parm && inner_parm;
261 949869 : outer_parm = TREE_CHAIN (outer_parm),
262 949869 : inner_parm = TREE_CHAIN (inner_parm))
263 1905520 : if (!useless_type_conversion_p
264 952760 : (TYPE_MAIN_VARIANT (TREE_VALUE (outer_parm)),
265 952760 : 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 293128 : if (outer_parm || inner_parm)
271 : return false;
272 :
273 : /* Defer to the target if necessary. */
274 291507 : if (TYPE_ATTRIBUTES (inner_type) || TYPE_ATTRIBUTES (outer_type))
275 280892 : 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 36926953 : else if (AGGREGATE_TYPE_P (inner_type)
284 3499931 : && TREE_CODE (inner_type) == TREE_CODE (outer_type))
285 2336836 : return TYPE_CANONICAL (inner_type)
286 2336836 : && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type);
287 :
288 34590117 : else if (TREE_CODE (inner_type) == OFFSET_TYPE
289 9904 : && TREE_CODE (outer_type) == OFFSET_TYPE)
290 2550 : return useless_type_conversion_p (TREE_TYPE (outer_type),
291 2550 : TREE_TYPE (inner_type))
292 4920 : && useless_type_conversion_p
293 2370 : (TYPE_OFFSET_BASETYPE (outer_type),
294 2370 : 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 117816881 : gimple_set_body (tree fndecl, gimple_seq seq)
306 : {
307 117816881 : struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
308 117816881 : 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 62 : gcc_assert (seq == NULL);
314 : }
315 : else
316 117816819 : fn->gimple_body = seq;
317 117816881 : }
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 433606750 : gimple_body (tree fndecl)
327 : {
328 433606750 : struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
329 433606750 : 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 397965297 : gimple_has_body_p (tree fndecl)
336 : {
337 397965297 : struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
338 397965297 : 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 33778 : gimple_decl_printable_name (tree decl, int verbosity)
345 : {
346 33778 : if (!DECL_NAME (decl))
347 : return NULL;
348 :
349 33778 : if (HAS_DECL_ASSEMBLER_NAME_P (decl) && DECL_ASSEMBLER_NAME_SET_P (decl))
350 : {
351 33417 : int dmgl_opts = DMGL_NO_OPTS;
352 :
353 33417 : if (verbosity >= 2)
354 : {
355 4454 : dmgl_opts = DMGL_VERBOSE
356 : | DMGL_ANSI
357 : | DMGL_GNU_V3
358 : | DMGL_RET_POSTFIX;
359 4454 : if (TREE_CODE (decl) == FUNCTION_DECL)
360 4381 : dmgl_opts |= DMGL_PARAMS;
361 : }
362 :
363 33417 : const char *mangled_str
364 33417 : = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME_RAW (decl));
365 33417 : const char *str = cplus_demangle_v3 (mangled_str, dmgl_opts);
366 65488 : return str ? str : mangled_str;
367 : }
368 :
369 361 : 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 1508133 : copy_var_decl (tree var, tree name, tree type)
377 : {
378 1508133 : tree copy = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, name, type);
379 :
380 1508133 : TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (var);
381 1508133 : TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (var);
382 1508133 : DECL_NOT_GIMPLE_REG_P (copy) = DECL_NOT_GIMPLE_REG_P (var);
383 1508133 : DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (var);
384 1508133 : DECL_IGNORED_P (copy) = DECL_IGNORED_P (var);
385 1508133 : DECL_CONTEXT (copy) = DECL_CONTEXT (var);
386 1508133 : TREE_USED (copy) = 1;
387 1508133 : DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
388 1508133 : DECL_ATTRIBUTES (copy) = DECL_ATTRIBUTES (var);
389 1508133 : if (DECL_USER_ALIGN (var))
390 : {
391 145 : SET_DECL_ALIGN (copy, DECL_ALIGN (var));
392 145 : DECL_USER_ALIGN (copy) = 1;
393 : }
394 :
395 1508133 : copy_warning (copy, var);
396 1508133 : 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 23725343 : remove_suffix (char *name, int len)
406 : {
407 23725343 : int i;
408 :
409 89772009 : for (i = 2; i < 7 && len > i; i++)
410 67191209 : if (name[len - i] == '.')
411 : {
412 1144543 : name[len - i] = '\0';
413 1144543 : break;
414 : }
415 23725343 : }
416 :
417 : /* Create a new temporary name with PREFIX. Return an identifier. */
418 :
419 : static GTY(()) unsigned int tmp_var_id_num;
420 :
421 : tree
422 23725343 : create_tmp_var_name (const char *prefix)
423 : {
424 23725343 : char *tmp_name;
425 :
426 23725343 : if (prefix)
427 : {
428 23725343 : char *preftmp = ASTRDUP (prefix);
429 :
430 23725343 : remove_suffix (preftmp, strlen (preftmp));
431 23725343 : prefix = preftmp;
432 : }
433 :
434 23725343 : ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
435 23725343 : return get_identifier (tmp_name);
436 : }
437 :
438 : /* Create a new temporary variable declaration of type TYPE.
439 : Do NOT push it into the current binding. */
440 :
441 : tree
442 27040101 : create_tmp_var_raw (tree type, const char *prefix)
443 : {
444 27040101 : tree tmp_var;
445 :
446 47103241 : tmp_var = build_decl (input_location,
447 20063140 : VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
448 : type);
449 :
450 : /* The variable was declared by the compiler. */
451 27040101 : DECL_ARTIFICIAL (tmp_var) = 1;
452 : /* And we don't want debug info for it. */
453 27040101 : DECL_IGNORED_P (tmp_var) = 1;
454 : /* And we don't want even the fancy names of those printed in
455 : -fdump-final-insns= dumps. */
456 27040101 : DECL_NAMELESS (tmp_var) = 1;
457 :
458 : /* Make the variable writable. */
459 27040101 : TREE_READONLY (tmp_var) = 0;
460 :
461 27040101 : DECL_EXTERNAL (tmp_var) = 0;
462 27040101 : TREE_STATIC (tmp_var) = 0;
463 27040101 : TREE_USED (tmp_var) = 1;
464 :
465 27040101 : return tmp_var;
466 : }
467 :
468 : /* Create a new temporary variable declaration of type TYPE. DO push the
469 : variable into the current binding. Further, assume that this is called
470 : only from gimplification or optimization, at which point the creation of
471 : certain types are bugs. */
472 :
473 : tree
474 15005292 : create_tmp_var (tree type, const char *prefix)
475 : {
476 15005292 : tree tmp_var;
477 :
478 : /* We don't allow types that are addressable (meaning we can't make copies),
479 : or incomplete. We also used to reject every variable size objects here,
480 : but now support those for which a constant upper bound can be obtained.
481 : The processing for variable sizes is performed in gimple_add_tmp_var,
482 : point at which it really matters and possibly reached via paths not going
483 : through this function, e.g. after direct calls to create_tmp_var_raw. */
484 15005292 : gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
485 :
486 15005292 : tmp_var = create_tmp_var_raw (type, prefix);
487 15005292 : gimple_add_tmp_var (tmp_var);
488 15005292 : return tmp_var;
489 : }
490 :
491 : /* Create a new temporary variable declaration of type TYPE by calling
492 : create_tmp_var and if TYPE is a vector or a complex number, mark the new
493 : temporary as gimple register. */
494 :
495 : tree
496 3558810 : create_tmp_reg (tree type, const char *prefix)
497 : {
498 3558810 : return create_tmp_var (type, prefix);
499 : }
500 :
501 : /* Create a new temporary variable declaration of type TYPE by calling
502 : create_tmp_var and if TYPE is a vector or a complex number, mark the new
503 : temporary as gimple register. */
504 :
505 : tree
506 27345 : create_tmp_reg_fn (struct function *fn, tree type, const char *prefix)
507 : {
508 27345 : tree tmp;
509 :
510 27345 : tmp = create_tmp_var_raw (type, prefix);
511 27345 : gimple_add_tmp_var_fn (fn, tmp);
512 :
513 27345 : return tmp;
514 : }
515 :
516 :
517 : /* ----- Expression related ----- */
518 :
519 : /* Extract the operands and code for expression EXPR into *SUBCODE_P,
520 : *OP1_P, *OP2_P and *OP3_P respectively. */
521 :
522 : void
523 174790976 : extract_ops_from_tree (tree expr, enum tree_code *subcode_p, tree *op1_p,
524 : tree *op2_p, tree *op3_p)
525 : {
526 174790976 : *subcode_p = TREE_CODE (expr);
527 174790976 : switch (get_gimple_rhs_class (*subcode_p))
528 : {
529 104936 : case GIMPLE_TERNARY_RHS:
530 104936 : {
531 104936 : *op1_p = TREE_OPERAND (expr, 0);
532 104936 : *op2_p = TREE_OPERAND (expr, 1);
533 104936 : *op3_p = TREE_OPERAND (expr, 2);
534 104936 : break;
535 : }
536 21133449 : case GIMPLE_BINARY_RHS:
537 21133449 : {
538 21133449 : *op1_p = TREE_OPERAND (expr, 0);
539 21133449 : *op2_p = TREE_OPERAND (expr, 1);
540 21133449 : *op3_p = NULL_TREE;
541 21133449 : break;
542 : }
543 11605816 : case GIMPLE_UNARY_RHS:
544 11605816 : {
545 11605816 : *op1_p = TREE_OPERAND (expr, 0);
546 11605816 : *op2_p = NULL_TREE;
547 11605816 : *op3_p = NULL_TREE;
548 11605816 : break;
549 : }
550 141946775 : case GIMPLE_SINGLE_RHS:
551 141946775 : {
552 141946775 : *op1_p = expr;
553 141946775 : *op2_p = NULL_TREE;
554 141946775 : *op3_p = NULL_TREE;
555 141946775 : break;
556 : }
557 0 : default:
558 0 : gcc_unreachable ();
559 : }
560 174790976 : }
561 :
562 : /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
563 :
564 : void
565 6043677 : gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
566 : tree *lhs_p, tree *rhs_p)
567 : {
568 6043677 : gcc_assert (COMPARISON_CLASS_P (cond)
569 : || TREE_CODE (cond) == TRUTH_NOT_EXPR
570 : || is_gimple_min_invariant (cond)
571 : || SSA_VAR_P (cond));
572 6043677 : gcc_checking_assert (!tree_could_throw_p (cond));
573 :
574 6043677 : extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
575 :
576 : /* Canonicalize conditionals of the form 'if (!VAL)'. */
577 6043677 : if (*code_p == TRUTH_NOT_EXPR)
578 : {
579 0 : *code_p = EQ_EXPR;
580 0 : gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
581 0 : *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
582 : }
583 : /* Canonicalize conditionals of the form 'if (VAL)' */
584 6043677 : else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
585 : {
586 1814487 : *code_p = NE_EXPR;
587 1814487 : gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
588 1814487 : *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
589 : }
590 6043677 : }
591 :
592 : /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
593 :
594 : bool
595 254561707 : is_gimple_lvalue (tree t)
596 : {
597 254561707 : return (is_gimple_addressable (t)
598 14499655 : || TREE_CODE (t) == WITH_SIZE_EXPR
599 : /* These are complex lvalues, but don't have addresses, so they
600 : go here. */
601 269037269 : || TREE_CODE (t) == BIT_FIELD_REF);
602 : }
603 :
604 : /* Helper for is_gimple_condexpr and is_gimple_condexpr_for_cond. */
605 :
606 : static bool
607 10185784 : is_gimple_condexpr_1 (tree t, bool allow_traps, bool allow_cplx)
608 : {
609 10185784 : tree op0;
610 10185784 : return (is_gimple_val (t)
611 10185784 : || (COMPARISON_CLASS_P (t)
612 7022578 : && (allow_traps || !tree_could_throw_p (t))
613 7015702 : && ((op0 = TREE_OPERAND (t, 0)), true)
614 7015702 : && (allow_cplx || TREE_CODE (TREE_TYPE (op0)) != COMPLEX_TYPE)
615 7015702 : && is_gimple_val (op0)
616 5439453 : && is_gimple_val (TREE_OPERAND (t, 1))));
617 : }
618 :
619 : /* Like is_gimple_condexpr, but does not allow T to trap. */
620 :
621 : bool
622 6541361 : is_gimple_condexpr_for_cond (tree t)
623 : {
624 6541361 : return is_gimple_condexpr_1 (t, false, true);
625 : }
626 :
627 : /* Canonicalize a tree T for use in a COND_EXPR as conditional. Returns
628 : a canonicalized tree that is valid for a COND_EXPR or NULL_TREE, if
629 : we failed to create one. */
630 :
631 : tree
632 3644423 : canonicalize_cond_expr_cond (tree t)
633 : {
634 : /* Strip conversions around boolean operations. */
635 3493994 : if (CONVERT_EXPR_P (t)
636 3644423 : && (truth_value_p (TREE_CODE (TREE_OPERAND (t, 0)))
637 149307 : || TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
638 : == BOOLEAN_TYPE))
639 1126 : t = TREE_OPERAND (t, 0);
640 :
641 : /* For !x use x == 0. */
642 3644423 : if (TREE_CODE (t) == TRUTH_NOT_EXPR)
643 : {
644 22520 : tree top0 = TREE_OPERAND (t, 0);
645 22520 : t = build2 (EQ_EXPR, TREE_TYPE (t),
646 22520 : top0, build_int_cst (TREE_TYPE (top0), 0));
647 : }
648 : /* For cmp ? 1 : 0 use cmp. */
649 3621903 : else if (TREE_CODE (t) == COND_EXPR
650 100441 : && COMPARISON_CLASS_P (TREE_OPERAND (t, 0))
651 98757 : && integer_onep (TREE_OPERAND (t, 1))
652 3720660 : && integer_zerop (TREE_OPERAND (t, 2)))
653 : {
654 98757 : tree top0 = TREE_OPERAND (t, 0);
655 98757 : t = build2 (TREE_CODE (top0), TREE_TYPE (t),
656 98757 : TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1));
657 : }
658 : /* For x ^ y use x != y. */
659 3523146 : else if (TREE_CODE (t) == BIT_XOR_EXPR)
660 0 : t = build2 (NE_EXPR, TREE_TYPE (t),
661 0 : TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
662 :
663 : /* We don't know where this will be used so allow both traps and
664 : _Complex. The caller is responsible for more precise checking. */
665 3644423 : if (is_gimple_condexpr_1 (t, true, true))
666 1288857 : return t;
667 :
668 : return NULL_TREE;
669 : }
670 :
671 : /* Return true if T is a gimple address. */
672 :
673 : bool
674 0 : is_gimple_address (const_tree t)
675 : {
676 0 : tree op;
677 :
678 0 : if (TREE_CODE (t) != ADDR_EXPR)
679 : return false;
680 :
681 0 : op = TREE_OPERAND (t, 0);
682 0 : while (handled_component_p (op))
683 : {
684 0 : if ((TREE_CODE (op) == ARRAY_REF
685 0 : || TREE_CODE (op) == ARRAY_RANGE_REF)
686 0 : && !is_gimple_val (TREE_OPERAND (op, 1)))
687 : return false;
688 :
689 0 : op = TREE_OPERAND (op, 0);
690 : }
691 :
692 0 : if (CONSTANT_CLASS_P (op)
693 0 : || TREE_CODE (op) == TARGET_MEM_REF
694 0 : || TREE_CODE (op) == MEM_REF)
695 : return true;
696 :
697 0 : switch (TREE_CODE (op))
698 : {
699 : case PARM_DECL:
700 : case RESULT_DECL:
701 : case LABEL_DECL:
702 : case FUNCTION_DECL:
703 : case VAR_DECL:
704 : case CONST_DECL:
705 : return true;
706 :
707 : default:
708 : return false;
709 : }
710 : }
711 :
712 : /* Return true if T is a gimple invariant address. */
713 :
714 : bool
715 5731394587 : is_gimple_invariant_address (const_tree t)
716 : {
717 5731394587 : const_tree op;
718 :
719 5731394587 : if (TREE_CODE (t) != ADDR_EXPR)
720 : return false;
721 :
722 5731394587 : op = strip_invariant_refs (TREE_OPERAND (t, 0));
723 5731394587 : if (!op)
724 : return false;
725 :
726 5719266057 : if (TREE_CODE (op) == MEM_REF)
727 : {
728 454996249 : const_tree op0 = TREE_OPERAND (op, 0);
729 454996249 : return (TREE_CODE (op0) == ADDR_EXPR
730 454996249 : && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
731 156360967 : || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
732 : }
733 :
734 5264269808 : return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
735 : }
736 :
737 : /* Return true if T is a gimple invariant address at IPA level
738 : (so addresses of variables on stack are not allowed). */
739 :
740 : bool
741 2381703 : is_gimple_ip_invariant_address (const_tree t)
742 : {
743 2381703 : const_tree op;
744 :
745 2381703 : if (TREE_CODE (t) != ADDR_EXPR)
746 : return false;
747 :
748 2381703 : op = strip_invariant_refs (TREE_OPERAND (t, 0));
749 2381703 : if (!op)
750 : return false;
751 :
752 2380293 : if (TREE_CODE (op) == MEM_REF)
753 : {
754 116717 : const_tree op0 = TREE_OPERAND (op, 0);
755 116717 : return (TREE_CODE (op0) == ADDR_EXPR
756 116717 : && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
757 88345 : || decl_address_ip_invariant_p (TREE_OPERAND (op0, 0))));
758 : }
759 :
760 2263576 : return CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op);
761 : }
762 :
763 : /* Return true if T is a GIMPLE minimal invariant. It's a restricted
764 : form of function invariant. */
765 :
766 : bool
767 22159000555 : is_gimple_min_invariant (const_tree t)
768 : {
769 22159000555 : if (TREE_CODE (t) == ADDR_EXPR)
770 5730168757 : return is_gimple_invariant_address (t);
771 :
772 16428831798 : return is_gimple_constant (t);
773 : }
774 :
775 : /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
776 : form of gimple minimal invariant. */
777 :
778 : bool
779 25714778 : is_gimple_ip_invariant (const_tree t)
780 : {
781 25714778 : if (TREE_CODE (t) == ADDR_EXPR)
782 2380511 : return is_gimple_ip_invariant_address (t);
783 :
784 23334267 : return is_gimple_constant (t);
785 : }
786 :
787 : /* Return true if T is a non-aggregate register variable. */
788 :
789 : bool
790 44147117031 : is_gimple_reg (tree t)
791 : {
792 77032259678 : if (virtual_operand_p (t))
793 : return false;
794 :
795 44000242369 : if (TREE_CODE (t) == SSA_NAME)
796 : return true;
797 :
798 16278592720 : if (!is_gimple_variable (t))
799 : return false;
800 :
801 5534585933 : if (!is_gimple_reg_type (TREE_TYPE (t)))
802 : return false;
803 :
804 : /* A volatile decl is not acceptable because we can't reuse it as
805 : needed. We need to copy it into a temp first. */
806 2213170292 : if (TREE_THIS_VOLATILE (t))
807 : return false;
808 :
809 : /* We define "registers" as things that can be renamed as needed,
810 : which with our infrastructure does not apply to memory. */
811 1971159230 : if (needs_to_live_in_memory (t))
812 : return false;
813 :
814 : /* Hard register variables are an interesting case. For those that
815 : are call-clobbered, we don't know where all the calls are, since
816 : we don't (want to) take into account which operations will turn
817 : into libcalls at the rtl level. For those that are call-saved,
818 : we don't currently model the fact that calls may in fact change
819 : global hard registers, nor do we examine ASM_CLOBBERS at the tree
820 : level, and so miss variable changes that might imply. All around,
821 : it seems safest to not do too much optimization with these at the
822 : tree level at all. We'll have to rely on the rtl optimizers to
823 : clean this up, as there we've got all the appropriate bits exposed. */
824 1152222321 : if (VAR_P (t) && DECL_HARD_REGISTER (t))
825 : return false;
826 :
827 : /* Variables can be marked as having partial definitions, avoid
828 : putting them into SSA form. */
829 1150773606 : return !DECL_NOT_GIMPLE_REG_P (t);
830 : }
831 :
832 :
833 : /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
834 :
835 : bool
836 8986006736 : is_gimple_val (tree t)
837 : {
838 : /* Make loads from volatiles and memory vars explicit. */
839 8986006736 : if (is_gimple_variable (t)
840 4786135714 : && is_gimple_reg_type (TREE_TYPE (t))
841 13748834868 : && !is_gimple_reg (t))
842 : return false;
843 :
844 : /* These eventually expand into constants, so treat them like that. */
845 8975570333 : if (TREE_CODE (t) == OMP_NEXT_VARIANT
846 8975570333 : || TREE_CODE (t) == OMP_TARGET_DEVICE_MATCHES)
847 : return true;
848 :
849 8975570281 : return (is_gimple_variable (t) || is_gimple_min_invariant (t));
850 : }
851 :
852 : /* Similarly, but accept hard registers as inputs to asm statements. */
853 :
854 : bool
855 43441 : is_gimple_asm_val (tree t)
856 : {
857 43441 : if (VAR_P (t) && DECL_HARD_REGISTER (t))
858 : return true;
859 :
860 42564 : return is_gimple_val (t);
861 : }
862 :
863 : /* Return true if T is a GIMPLE minimal lvalue. */
864 :
865 : bool
866 29972583 : is_gimple_min_lval (tree t)
867 : {
868 29972583 : if (!(t = const_cast<tree> (strip_invariant_refs (t))))
869 : return false;
870 29972568 : return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF);
871 : }
872 :
873 : /* Return true if T is a valid function operand of a CALL_EXPR. */
874 :
875 : bool
876 1042336777 : is_gimple_call_addr (tree t)
877 : {
878 1042336777 : return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
879 : }
880 :
881 : /* Return true if T is a valid address operand of a MEM_REF. */
882 :
883 : bool
884 1390846680 : is_gimple_mem_ref_addr (tree t)
885 : {
886 1390846680 : return (is_gimple_reg (t)
887 380876481 : || poly_int_tree_p (t)
888 1771333063 : || (TREE_CODE (t) == ADDR_EXPR
889 378299893 : && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
890 376454391 : || decl_address_invariant_p (TREE_OPERAND (t, 0)))));
891 : }
892 :
893 : /* Hold trees marked addressable during expand. */
894 :
895 : static hash_set<tree> *mark_addressable_queue;
896 :
897 : /* Mark X as addressable or queue it up if called during expand. We
898 : don't want to apply it immediately during expand because decls are
899 : made addressable at that point due to RTL-only concerns, such as
900 : uses of memcpy for block moves, and TREE_ADDRESSABLE changes
901 : is_gimple_reg, which might make it seem like a variable that used
902 : to be a gimple_reg shouldn't have been an SSA name. So we queue up
903 : this flag setting and only apply it when we're done with GIMPLE and
904 : only RTL issues matter. */
905 :
906 : static void
907 10885493 : mark_addressable_1 (tree x)
908 : {
909 10885493 : if (!currently_expanding_to_rtl)
910 : {
911 10825732 : TREE_ADDRESSABLE (x) = 1;
912 10825732 : return;
913 : }
914 :
915 59761 : if (!mark_addressable_queue)
916 14619 : mark_addressable_queue = new hash_set<tree>();
917 59761 : mark_addressable_queue->add (x);
918 : }
919 :
920 : /* Adaptor for mark_addressable_1 for use in hash_set traversal. */
921 :
922 : static bool
923 36584 : mark_addressable_2 (tree const &x, void * ATTRIBUTE_UNUSED = NULL)
924 : {
925 36584 : mark_addressable_1 (x);
926 36584 : return false;
927 : }
928 :
929 : /* Mark all queued trees as addressable, and empty the queue. To be
930 : called right after clearing CURRENTLY_EXPANDING_TO_RTL. */
931 :
932 : void
933 1480895 : flush_mark_addressable_queue ()
934 : {
935 1480895 : gcc_assert (!currently_expanding_to_rtl);
936 1480895 : if (mark_addressable_queue)
937 : {
938 51203 : mark_addressable_queue->traverse<void*, mark_addressable_2> (NULL);
939 29238 : delete mark_addressable_queue;
940 14619 : mark_addressable_queue = NULL;
941 : }
942 1480895 : }
943 :
944 : /* Mark X addressable. Unlike the langhook we expect X to be in gimple
945 : form and we don't do any syntax checking. */
946 :
947 : void
948 33320062 : mark_addressable (tree x)
949 : {
950 33320062 : if (TREE_CODE (x) == WITH_SIZE_EXPR)
951 0 : x = TREE_OPERAND (x, 0);
952 38637935 : while (handled_component_p (x))
953 5317873 : x = TREE_OPERAND (x, 0);
954 33320062 : if ((TREE_CODE (x) == MEM_REF
955 33320062 : || TREE_CODE (x) == TARGET_MEM_REF)
956 33320062 : && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
957 10789 : x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
958 33320062 : if (!VAR_P (x)
959 : && TREE_CODE (x) != PARM_DECL
960 : && TREE_CODE (x) != RESULT_DECL)
961 : return;
962 10840621 : mark_addressable_1 (x);
963 :
964 : /* Also mark the artificial SSA_NAME that points to the partition of X. */
965 10840621 : if (VAR_P (x)
966 10236675 : && !DECL_EXTERNAL (x)
967 9091250 : && !TREE_STATIC (x)
968 5762915 : && cfun->gimple_df != NULL
969 16603536 : && cfun->gimple_df->decls_to_pointers != NULL)
970 : {
971 14353 : tree *namep = cfun->gimple_df->decls_to_pointers->get (x);
972 14353 : if (namep)
973 8288 : mark_addressable_1 (*namep);
974 : }
975 : }
976 :
977 : /* Returns true iff T is a valid RHS for an assignment to a renamed
978 : user -- or front-end generated artificial -- variable. */
979 :
980 : bool
981 816078 : is_gimple_reg_rhs (tree t)
982 : {
983 816078 : return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
984 : }
985 :
986 : #include "gt-gimple-expr.h"
|