ada: Fix wrong resolution for hidden discriminant in predicate
[official-gcc.git] / gcc / gimple-expr.cc
blobf15cc0ba71511b0d82764b8a9331e63e0a50d295
1 /* Gimple decl, type, and expression support functions.
3 Copyright (C) 2007-2023 Free Software Foundation, Inc.
4 Contributed by Aldy Hernandez <aldyh@redhat.com>
6 This file is part of GCC.
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.
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.
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/>. */
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"
42 /* ----- Type related ----- */
44 /* Return true if the conversion from INNER_TYPE to OUTER_TYPE is a
45 useless type conversion, otherwise return false.
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:
52 1) useless_type_conversion_p is transitive.
53 If a < b and b < c then a < c.
55 2) useless_type_conversion_p is not symmetric.
56 From a < b does not follow a > b.
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. */
67 bool
68 useless_type_conversion_p (tree outer_type, tree inner_type)
70 /* Do the following before stripping toplevel qualifiers. */
71 if (POINTER_TYPE_P (inner_type)
72 && POINTER_TYPE_P (outer_type))
74 /* Do not lose casts between pointers to different address spaces. */
75 if (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
76 != TYPE_ADDR_SPACE (TREE_TYPE (inner_type)))
77 return false;
78 /* Do not lose casts to function pointer types. */
79 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (outer_type))
80 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (inner_type)))
81 return false;
84 /* From now on qualifiers on value types do not matter. */
85 inner_type = TYPE_MAIN_VARIANT (inner_type);
86 outer_type = TYPE_MAIN_VARIANT (outer_type);
88 if (inner_type == outer_type)
89 return true;
91 /* Changes in machine mode are never useless conversions because the RTL
92 middle-end expects explicit conversions between modes. */
93 if (TYPE_MODE (inner_type) != TYPE_MODE (outer_type))
94 return false;
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 if (INTEGRAL_TYPE_P (inner_type)
100 && INTEGRAL_TYPE_P (outer_type))
102 /* Preserve changes in signedness or precision. */
103 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
104 || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
105 return false;
107 /* Preserve conversions to/from BOOLEAN_TYPE if types are not
108 of precision one. */
109 if (((TREE_CODE (inner_type) == BOOLEAN_TYPE)
110 != (TREE_CODE (outer_type) == BOOLEAN_TYPE))
111 && TYPE_PRECISION (outer_type) != 1)
112 return false;
114 /* We don't need to preserve changes in the types minimum or
115 maximum value in general as these do not generate code
116 unless the types precisions are different. */
117 return true;
120 /* Scalar floating point types with the same mode are compatible. */
121 else if (SCALAR_FLOAT_TYPE_P (inner_type)
122 && SCALAR_FLOAT_TYPE_P (outer_type))
123 return true;
125 /* Fixed point types with the same mode are compatible. */
126 else if (FIXED_POINT_TYPE_P (inner_type)
127 && FIXED_POINT_TYPE_P (outer_type))
128 return TYPE_SATURATING (inner_type) == TYPE_SATURATING (outer_type);
130 /* We need to take special care recursing to pointed-to types. */
131 else if (POINTER_TYPE_P (inner_type)
132 && POINTER_TYPE_P (outer_type))
134 /* We do not care for const qualification of the pointed-to types
135 as const qualification has no semantic value to the middle-end. */
137 /* Otherwise pointers/references are equivalent. */
138 return true;
141 /* Recurse for complex types. */
142 else if (TREE_CODE (inner_type) == COMPLEX_TYPE
143 && TREE_CODE (outer_type) == COMPLEX_TYPE)
144 return useless_type_conversion_p (TREE_TYPE (outer_type),
145 TREE_TYPE (inner_type));
147 /* Recurse for vector types with the same number of subparts. */
148 else if (VECTOR_TYPE_P (inner_type)
149 && VECTOR_TYPE_P (outer_type))
150 return (known_eq (TYPE_VECTOR_SUBPARTS (inner_type),
151 TYPE_VECTOR_SUBPARTS (outer_type))
152 && useless_type_conversion_p (TREE_TYPE (outer_type),
153 TREE_TYPE (inner_type))
154 && targetm.compatible_vector_types_p (inner_type, outer_type));
156 else if (TREE_CODE (inner_type) == ARRAY_TYPE
157 && TREE_CODE (outer_type) == ARRAY_TYPE)
159 /* Preserve various attributes. */
160 if (TYPE_REVERSE_STORAGE_ORDER (inner_type)
161 != TYPE_REVERSE_STORAGE_ORDER (outer_type))
162 return false;
163 if (TYPE_STRING_FLAG (inner_type) != TYPE_STRING_FLAG (outer_type))
164 return false;
166 /* Conversions from array types with unknown extent to
167 array types with known extent are not useless. */
168 if (!TYPE_DOMAIN (inner_type) && TYPE_DOMAIN (outer_type))
169 return false;
171 /* Nor are conversions from array types with non-constant size to
172 array types with constant size or to different size. */
173 if (TYPE_SIZE (outer_type)
174 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST
175 && (!TYPE_SIZE (inner_type)
176 || TREE_CODE (TYPE_SIZE (inner_type)) != INTEGER_CST
177 || !tree_int_cst_equal (TYPE_SIZE (outer_type),
178 TYPE_SIZE (inner_type))))
179 return false;
181 /* Check conversions between arrays with partially known extents.
182 If the array min/max values are constant they have to match.
183 Otherwise allow conversions to unknown and variable extents.
184 In particular this declares conversions that may change the
185 mode to BLKmode as useless. */
186 if (TYPE_DOMAIN (inner_type)
187 && TYPE_DOMAIN (outer_type)
188 && TYPE_DOMAIN (inner_type) != TYPE_DOMAIN (outer_type))
190 tree inner_min = TYPE_MIN_VALUE (TYPE_DOMAIN (inner_type));
191 tree outer_min = TYPE_MIN_VALUE (TYPE_DOMAIN (outer_type));
192 tree inner_max = TYPE_MAX_VALUE (TYPE_DOMAIN (inner_type));
193 tree outer_max = TYPE_MAX_VALUE (TYPE_DOMAIN (outer_type));
195 /* After gimplification a variable min/max value carries no
196 additional information compared to a NULL value. All that
197 matters has been lowered to be part of the IL. */
198 if (inner_min && TREE_CODE (inner_min) != INTEGER_CST)
199 inner_min = NULL_TREE;
200 if (outer_min && TREE_CODE (outer_min) != INTEGER_CST)
201 outer_min = NULL_TREE;
202 if (inner_max && TREE_CODE (inner_max) != INTEGER_CST)
203 inner_max = NULL_TREE;
204 if (outer_max && TREE_CODE (outer_max) != INTEGER_CST)
205 outer_max = NULL_TREE;
207 /* Conversions NULL / variable <- cst are useless, but not
208 the other way around. */
209 if (outer_min
210 && (!inner_min
211 || !tree_int_cst_equal (inner_min, outer_min)))
212 return false;
213 if (outer_max
214 && (!inner_max
215 || !tree_int_cst_equal (inner_max, outer_max)))
216 return false;
219 /* Recurse on the element check. */
220 return useless_type_conversion_p (TREE_TYPE (outer_type),
221 TREE_TYPE (inner_type));
224 else if (FUNC_OR_METHOD_TYPE_P (inner_type)
225 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
227 tree outer_parm, inner_parm;
229 /* If the return types are not compatible bail out. */
230 if (!useless_type_conversion_p (TREE_TYPE (outer_type),
231 TREE_TYPE (inner_type)))
232 return false;
234 /* Method types should belong to a compatible base class. */
235 if (TREE_CODE (inner_type) == METHOD_TYPE
236 && !useless_type_conversion_p (TYPE_METHOD_BASETYPE (outer_type),
237 TYPE_METHOD_BASETYPE (inner_type)))
238 return false;
240 /* A conversion to an unprototyped argument list is ok. */
241 if (!prototype_p (outer_type))
242 return true;
244 /* If the unqualified argument types are compatible the conversion
245 is useless. */
246 if (TYPE_ARG_TYPES (outer_type) == TYPE_ARG_TYPES (inner_type))
247 return true;
249 for (outer_parm = TYPE_ARG_TYPES (outer_type),
250 inner_parm = TYPE_ARG_TYPES (inner_type);
251 outer_parm && inner_parm;
252 outer_parm = TREE_CHAIN (outer_parm),
253 inner_parm = TREE_CHAIN (inner_parm))
254 if (!useless_type_conversion_p
255 (TYPE_MAIN_VARIANT (TREE_VALUE (outer_parm)),
256 TYPE_MAIN_VARIANT (TREE_VALUE (inner_parm))))
257 return false;
259 /* If there is a mismatch in the number of arguments the functions
260 are not compatible. */
261 if (outer_parm || inner_parm)
262 return false;
264 /* Defer to the target if necessary. */
265 if (TYPE_ATTRIBUTES (inner_type) || TYPE_ATTRIBUTES (outer_type))
266 return comp_type_attributes (outer_type, inner_type) != 0;
268 return true;
271 /* For aggregates we rely on TYPE_CANONICAL exclusively and require
272 explicit conversions for types involving to be structurally
273 compared types. */
274 else if (AGGREGATE_TYPE_P (inner_type)
275 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
276 return TYPE_CANONICAL (inner_type)
277 && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type);
279 else if (TREE_CODE (inner_type) == OFFSET_TYPE
280 && TREE_CODE (outer_type) == OFFSET_TYPE)
281 return useless_type_conversion_p (TREE_TYPE (outer_type),
282 TREE_TYPE (inner_type))
283 && useless_type_conversion_p
284 (TYPE_OFFSET_BASETYPE (outer_type),
285 TYPE_OFFSET_BASETYPE (inner_type));
287 return false;
291 /* ----- Decl related ----- */
293 /* Set sequence SEQ to be the GIMPLE body for function FN. */
295 void
296 gimple_set_body (tree fndecl, gimple_seq seq)
298 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
299 if (fn == NULL)
301 /* If FNDECL still does not have a function structure associated
302 with it, then it does not make sense for it to receive a
303 GIMPLE body. */
304 gcc_assert (seq == NULL);
306 else
307 fn->gimple_body = seq;
311 /* Return the body of GIMPLE statements for function FN. After the
312 CFG pass, the function body doesn't exist anymore because it has
313 been split up into basic blocks. In this case, it returns
314 NULL. */
316 gimple_seq
317 gimple_body (tree fndecl)
319 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
320 return fn ? fn->gimple_body : NULL;
323 /* Return true when FNDECL has Gimple body either in unlowered
324 or CFG form. */
325 bool
326 gimple_has_body_p (tree fndecl)
328 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
329 return (gimple_body (fndecl) || (fn && fn->cfg && !(fn->curr_properties & PROP_rtl)));
332 /* Return a printable name for symbol DECL. */
334 const char *
335 gimple_decl_printable_name (tree decl, int verbosity)
337 if (!DECL_NAME (decl))
338 return NULL;
340 if (HAS_DECL_ASSEMBLER_NAME_P (decl) && DECL_ASSEMBLER_NAME_SET_P (decl))
342 int dmgl_opts = DMGL_NO_OPTS;
344 if (verbosity >= 2)
346 dmgl_opts = DMGL_VERBOSE
347 | DMGL_ANSI
348 | DMGL_GNU_V3
349 | DMGL_RET_POSTFIX;
350 if (TREE_CODE (decl) == FUNCTION_DECL)
351 dmgl_opts |= DMGL_PARAMS;
354 const char *mangled_str
355 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME_RAW (decl));
356 const char *str = cplus_demangle_v3 (mangled_str, dmgl_opts);
357 return str ? str : mangled_str;
360 return IDENTIFIER_POINTER (DECL_NAME (decl));
364 /* Create a new VAR_DECL and copy information from VAR to it. */
366 tree
367 copy_var_decl (tree var, tree name, tree type)
369 tree copy = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, name, type);
371 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (var);
372 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (var);
373 DECL_NOT_GIMPLE_REG_P (copy) = DECL_NOT_GIMPLE_REG_P (var);
374 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (var);
375 DECL_IGNORED_P (copy) = DECL_IGNORED_P (var);
376 DECL_CONTEXT (copy) = DECL_CONTEXT (var);
377 TREE_USED (copy) = 1;
378 DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
379 DECL_ATTRIBUTES (copy) = DECL_ATTRIBUTES (var);
380 if (DECL_USER_ALIGN (var))
382 SET_DECL_ALIGN (copy, DECL_ALIGN (var));
383 DECL_USER_ALIGN (copy) = 1;
386 copy_warning (copy, var);
387 return copy;
390 /* Strip off a legitimate source ending from the input string NAME of
391 length LEN. Rather than having to know the names used by all of
392 our front ends, we strip off an ending of a period followed by
393 up to four characters. (like ".cpp".) */
395 static inline void
396 remove_suffix (char *name, int len)
398 int i;
400 for (i = 2; i < 7 && len > i; i++)
402 if (name[len - i] == '.')
404 name[len - i] = '\0';
405 break;
410 /* Create a new temporary name with PREFIX. Return an identifier. */
412 static GTY(()) unsigned int tmp_var_id_num;
414 tree
415 create_tmp_var_name (const char *prefix)
417 char *tmp_name;
419 if (prefix)
421 char *preftmp = ASTRDUP (prefix);
423 remove_suffix (preftmp, strlen (preftmp));
424 clean_symbol_name (preftmp);
426 prefix = preftmp;
429 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
430 return get_identifier (tmp_name);
433 /* Create a new temporary variable declaration of type TYPE.
434 Do NOT push it into the current binding. */
436 tree
437 create_tmp_var_raw (tree type, const char *prefix)
439 tree tmp_var;
441 tmp_var = build_decl (input_location,
442 VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
443 type);
445 /* The variable was declared by the compiler. */
446 DECL_ARTIFICIAL (tmp_var) = 1;
447 /* And we don't want debug info for it. */
448 DECL_IGNORED_P (tmp_var) = 1;
449 /* And we don't want even the fancy names of those printed in
450 -fdump-final-insns= dumps. */
451 DECL_NAMELESS (tmp_var) = 1;
453 /* Make the variable writable. */
454 TREE_READONLY (tmp_var) = 0;
456 DECL_EXTERNAL (tmp_var) = 0;
457 TREE_STATIC (tmp_var) = 0;
458 TREE_USED (tmp_var) = 1;
460 return tmp_var;
463 /* Create a new temporary variable declaration of type TYPE. DO push the
464 variable into the current binding. Further, assume that this is called
465 only from gimplification or optimization, at which point the creation of
466 certain types are bugs. */
468 tree
469 create_tmp_var (tree type, const char *prefix)
471 tree tmp_var;
473 /* We don't allow types that are addressable (meaning we can't make copies),
474 or incomplete. We also used to reject every variable size objects here,
475 but now support those for which a constant upper bound can be obtained.
476 The processing for variable sizes is performed in gimple_add_tmp_var,
477 point at which it really matters and possibly reached via paths not going
478 through this function, e.g. after direct calls to create_tmp_var_raw. */
479 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
481 tmp_var = create_tmp_var_raw (type, prefix);
482 gimple_add_tmp_var (tmp_var);
483 return tmp_var;
486 /* Create a new temporary variable declaration of type TYPE by calling
487 create_tmp_var and if TYPE is a vector or a complex number, mark the new
488 temporary as gimple register. */
490 tree
491 create_tmp_reg (tree type, const char *prefix)
493 return create_tmp_var (type, prefix);
496 /* Create a new temporary variable declaration of type TYPE by calling
497 create_tmp_var and if TYPE is a vector or a complex number, mark the new
498 temporary as gimple register. */
500 tree
501 create_tmp_reg_fn (struct function *fn, tree type, const char *prefix)
503 tree tmp;
505 tmp = create_tmp_var_raw (type, prefix);
506 gimple_add_tmp_var_fn (fn, tmp);
508 return tmp;
512 /* ----- Expression related ----- */
514 /* Extract the operands and code for expression EXPR into *SUBCODE_P,
515 *OP1_P, *OP2_P and *OP3_P respectively. */
517 void
518 extract_ops_from_tree (tree expr, enum tree_code *subcode_p, tree *op1_p,
519 tree *op2_p, tree *op3_p)
521 *subcode_p = TREE_CODE (expr);
522 switch (get_gimple_rhs_class (*subcode_p))
524 case GIMPLE_TERNARY_RHS:
526 *op1_p = TREE_OPERAND (expr, 0);
527 *op2_p = TREE_OPERAND (expr, 1);
528 *op3_p = TREE_OPERAND (expr, 2);
529 break;
531 case GIMPLE_BINARY_RHS:
533 *op1_p = TREE_OPERAND (expr, 0);
534 *op2_p = TREE_OPERAND (expr, 1);
535 *op3_p = NULL_TREE;
536 break;
538 case GIMPLE_UNARY_RHS:
540 *op1_p = TREE_OPERAND (expr, 0);
541 *op2_p = NULL_TREE;
542 *op3_p = NULL_TREE;
543 break;
545 case GIMPLE_SINGLE_RHS:
547 *op1_p = expr;
548 *op2_p = NULL_TREE;
549 *op3_p = NULL_TREE;
550 break;
552 default:
553 gcc_unreachable ();
557 /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
559 void
560 gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
561 tree *lhs_p, tree *rhs_p)
563 gcc_assert (COMPARISON_CLASS_P (cond)
564 || TREE_CODE (cond) == TRUTH_NOT_EXPR
565 || is_gimple_min_invariant (cond)
566 || SSA_VAR_P (cond));
567 gcc_checking_assert (!tree_could_throw_p (cond));
569 extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
571 /* Canonicalize conditionals of the form 'if (!VAL)'. */
572 if (*code_p == TRUTH_NOT_EXPR)
574 *code_p = EQ_EXPR;
575 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
576 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
578 /* Canonicalize conditionals of the form 'if (VAL)' */
579 else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
581 *code_p = NE_EXPR;
582 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
583 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
587 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
589 bool
590 is_gimple_lvalue (tree t)
592 return (is_gimple_addressable (t)
593 || TREE_CODE (t) == WITH_SIZE_EXPR
594 /* These are complex lvalues, but don't have addresses, so they
595 go here. */
596 || TREE_CODE (t) == BIT_FIELD_REF);
599 /* Helper for is_gimple_condexpr and is_gimple_condexpr_for_cond. */
601 static bool
602 is_gimple_condexpr_1 (tree t, bool allow_traps, bool allow_cplx)
604 tree op0;
605 return (is_gimple_val (t)
606 || (COMPARISON_CLASS_P (t)
607 && (allow_traps || !tree_could_throw_p (t))
608 && ((op0 = TREE_OPERAND (t, 0)), true)
609 && (allow_cplx || TREE_CODE (TREE_TYPE (op0)) != COMPLEX_TYPE)
610 && is_gimple_val (op0)
611 && is_gimple_val (TREE_OPERAND (t, 1))));
614 /* Like is_gimple_condexpr, but does not allow T to trap. */
616 bool
617 is_gimple_condexpr_for_cond (tree t)
619 return is_gimple_condexpr_1 (t, false, true);
622 /* Canonicalize a tree T for use in a COND_EXPR as conditional. Returns
623 a canonicalized tree that is valid for a COND_EXPR or NULL_TREE, if
624 we failed to create one. */
626 tree
627 canonicalize_cond_expr_cond (tree t)
629 /* Strip conversions around boolean operations. */
630 if (CONVERT_EXPR_P (t)
631 && (truth_value_p (TREE_CODE (TREE_OPERAND (t, 0)))
632 || TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
633 == BOOLEAN_TYPE))
634 t = TREE_OPERAND (t, 0);
636 /* For !x use x == 0. */
637 if (TREE_CODE (t) == TRUTH_NOT_EXPR)
639 tree top0 = TREE_OPERAND (t, 0);
640 t = build2 (EQ_EXPR, TREE_TYPE (t),
641 top0, build_int_cst (TREE_TYPE (top0), 0));
643 /* For cmp ? 1 : 0 use cmp. */
644 else if (TREE_CODE (t) == COND_EXPR
645 && COMPARISON_CLASS_P (TREE_OPERAND (t, 0))
646 && integer_onep (TREE_OPERAND (t, 1))
647 && integer_zerop (TREE_OPERAND (t, 2)))
649 tree top0 = TREE_OPERAND (t, 0);
650 t = build2 (TREE_CODE (top0), TREE_TYPE (t),
651 TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1));
653 /* For x ^ y use x != y. */
654 else if (TREE_CODE (t) == BIT_XOR_EXPR)
655 t = build2 (NE_EXPR, TREE_TYPE (t),
656 TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
658 /* We don't know where this will be used so allow both traps and
659 _Complex. The caller is responsible for more precise checking. */
660 if (is_gimple_condexpr_1 (t, true, true))
661 return t;
663 return NULL_TREE;
666 /* Return true if T is a gimple address. */
668 bool
669 is_gimple_address (const_tree t)
671 tree op;
673 if (TREE_CODE (t) != ADDR_EXPR)
674 return false;
676 op = TREE_OPERAND (t, 0);
677 while (handled_component_p (op))
679 if ((TREE_CODE (op) == ARRAY_REF
680 || TREE_CODE (op) == ARRAY_RANGE_REF)
681 && !is_gimple_val (TREE_OPERAND (op, 1)))
682 return false;
684 op = TREE_OPERAND (op, 0);
687 if (CONSTANT_CLASS_P (op)
688 || TREE_CODE (op) == TARGET_MEM_REF
689 || TREE_CODE (op) == MEM_REF)
690 return true;
692 switch (TREE_CODE (op))
694 case PARM_DECL:
695 case RESULT_DECL:
696 case LABEL_DECL:
697 case FUNCTION_DECL:
698 case VAR_DECL:
699 case CONST_DECL:
700 return true;
702 default:
703 return false;
707 /* Return true if T is a gimple invariant address. */
709 bool
710 is_gimple_invariant_address (const_tree t)
712 const_tree op;
714 if (TREE_CODE (t) != ADDR_EXPR)
715 return false;
717 op = strip_invariant_refs (TREE_OPERAND (t, 0));
718 if (!op)
719 return false;
721 if (TREE_CODE (op) == MEM_REF)
723 const_tree op0 = TREE_OPERAND (op, 0);
724 return (TREE_CODE (op0) == ADDR_EXPR
725 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
726 || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
729 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
732 /* Return true if T is a gimple invariant address at IPA level
733 (so addresses of variables on stack are not allowed). */
735 bool
736 is_gimple_ip_invariant_address (const_tree t)
738 const_tree op;
740 if (TREE_CODE (t) != ADDR_EXPR)
741 return false;
743 op = strip_invariant_refs (TREE_OPERAND (t, 0));
744 if (!op)
745 return false;
747 if (TREE_CODE (op) == MEM_REF)
749 const_tree op0 = TREE_OPERAND (op, 0);
750 return (TREE_CODE (op0) == ADDR_EXPR
751 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
752 || decl_address_ip_invariant_p (TREE_OPERAND (op0, 0))));
755 return CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op);
758 /* Return true if T is a GIMPLE minimal invariant. It's a restricted
759 form of function invariant. */
761 bool
762 is_gimple_min_invariant (const_tree t)
764 if (TREE_CODE (t) == ADDR_EXPR)
765 return is_gimple_invariant_address (t);
767 return is_gimple_constant (t);
770 /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
771 form of gimple minimal invariant. */
773 bool
774 is_gimple_ip_invariant (const_tree t)
776 if (TREE_CODE (t) == ADDR_EXPR)
777 return is_gimple_ip_invariant_address (t);
779 return is_gimple_constant (t);
782 /* Return true if T is a non-aggregate register variable. */
784 bool
785 is_gimple_reg (tree t)
787 if (virtual_operand_p (t))
788 return false;
790 if (TREE_CODE (t) == SSA_NAME)
791 return true;
793 if (!is_gimple_variable (t))
794 return false;
796 if (!is_gimple_reg_type (TREE_TYPE (t)))
797 return false;
799 /* A volatile decl is not acceptable because we can't reuse it as
800 needed. We need to copy it into a temp first. */
801 if (TREE_THIS_VOLATILE (t))
802 return false;
804 /* We define "registers" as things that can be renamed as needed,
805 which with our infrastructure does not apply to memory. */
806 if (needs_to_live_in_memory (t))
807 return false;
809 /* Hard register variables are an interesting case. For those that
810 are call-clobbered, we don't know where all the calls are, since
811 we don't (want to) take into account which operations will turn
812 into libcalls at the rtl level. For those that are call-saved,
813 we don't currently model the fact that calls may in fact change
814 global hard registers, nor do we examine ASM_CLOBBERS at the tree
815 level, and so miss variable changes that might imply. All around,
816 it seems safest to not do too much optimization with these at the
817 tree level at all. We'll have to rely on the rtl optimizers to
818 clean this up, as there we've got all the appropriate bits exposed. */
819 if (VAR_P (t) && DECL_HARD_REGISTER (t))
820 return false;
822 /* Variables can be marked as having partial definitions, avoid
823 putting them into SSA form. */
824 return !DECL_NOT_GIMPLE_REG_P (t);
828 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
830 bool
831 is_gimple_val (tree t)
833 /* Make loads from volatiles and memory vars explicit. */
834 if (is_gimple_variable (t)
835 && is_gimple_reg_type (TREE_TYPE (t))
836 && !is_gimple_reg (t))
837 return false;
839 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
842 /* Similarly, but accept hard registers as inputs to asm statements. */
844 bool
845 is_gimple_asm_val (tree t)
847 if (VAR_P (t) && DECL_HARD_REGISTER (t))
848 return true;
850 return is_gimple_val (t);
853 /* Return true if T is a GIMPLE minimal lvalue. */
855 bool
856 is_gimple_min_lval (tree t)
858 if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
859 return false;
860 return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF);
863 /* Return true if T is a valid function operand of a CALL_EXPR. */
865 bool
866 is_gimple_call_addr (tree t)
868 return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
871 /* Return true if T is a valid address operand of a MEM_REF. */
873 bool
874 is_gimple_mem_ref_addr (tree t)
876 return (is_gimple_reg (t)
877 || TREE_CODE (t) == INTEGER_CST
878 || (TREE_CODE (t) == ADDR_EXPR
879 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
880 || decl_address_invariant_p (TREE_OPERAND (t, 0)))));
883 /* Hold trees marked addressable during expand. */
885 static hash_set<tree> *mark_addressable_queue;
887 /* Mark X as addressable or queue it up if called during expand. We
888 don't want to apply it immediately during expand because decls are
889 made addressable at that point due to RTL-only concerns, such as
890 uses of memcpy for block moves, and TREE_ADDRESSABLE changes
891 is_gimple_reg, which might make it seem like a variable that used
892 to be a gimple_reg shouldn't have been an SSA name. So we queue up
893 this flag setting and only apply it when we're done with GIMPLE and
894 only RTL issues matter. */
896 static void
897 mark_addressable_1 (tree x)
899 if (!currently_expanding_to_rtl)
901 TREE_ADDRESSABLE (x) = 1;
902 return;
905 if (!mark_addressable_queue)
906 mark_addressable_queue = new hash_set<tree>();
907 mark_addressable_queue->add (x);
910 /* Adaptor for mark_addressable_1 for use in hash_set traversal. */
912 static bool
913 mark_addressable_2 (tree const &x, void * ATTRIBUTE_UNUSED = NULL)
915 mark_addressable_1 (x);
916 return false;
919 /* Mark all queued trees as addressable, and empty the queue. To be
920 called right after clearing CURRENTLY_EXPANDING_TO_RTL. */
922 void
923 flush_mark_addressable_queue ()
925 gcc_assert (!currently_expanding_to_rtl);
926 if (mark_addressable_queue)
928 mark_addressable_queue->traverse<void*, mark_addressable_2> (NULL);
929 delete mark_addressable_queue;
930 mark_addressable_queue = NULL;
934 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
935 form and we don't do any syntax checking. */
937 void
938 mark_addressable (tree x)
940 if (TREE_CODE (x) == WITH_SIZE_EXPR)
941 x = TREE_OPERAND (x, 0);
942 while (handled_component_p (x))
943 x = TREE_OPERAND (x, 0);
944 if ((TREE_CODE (x) == MEM_REF
945 || TREE_CODE (x) == TARGET_MEM_REF)
946 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
947 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
948 if (!VAR_P (x)
949 && TREE_CODE (x) != PARM_DECL
950 && TREE_CODE (x) != RESULT_DECL)
951 return;
952 mark_addressable_1 (x);
954 /* Also mark the artificial SSA_NAME that points to the partition of X. */
955 if (VAR_P (x)
956 && !DECL_EXTERNAL (x)
957 && !TREE_STATIC (x)
958 && cfun->gimple_df != NULL
959 && cfun->gimple_df->decls_to_pointers != NULL)
961 tree *namep = cfun->gimple_df->decls_to_pointers->get (x);
962 if (namep)
963 mark_addressable_1 (*namep);
967 /* Returns true iff T is a valid RHS for an assignment to a renamed
968 user -- or front-end generated artificial -- variable. */
970 bool
971 is_gimple_reg_rhs (tree t)
973 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
976 #include "gt-gimple-expr.h"