Handle constant fp classifications in fold-const-call.c
[official-gcc.git] / gcc / gimple-expr.c
blobdd2550a71c33eadb7b5604dbc05640cc9e029cc9
1 /* Gimple decl, type, and expression support functions.
3 Copyright (C) 2007-2015 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"
38 /* ----- Type related ----- */
40 /* Return true if the conversion from INNER_TYPE to OUTER_TYPE is a
41 useless type conversion, otherwise return false.
43 This function implicitly defines the middle-end type system. With
44 the notion of 'a < b' meaning that useless_type_conversion_p (a, b)
45 holds and 'a > b' meaning that useless_type_conversion_p (b, a) holds,
46 the following invariants shall be fulfilled:
48 1) useless_type_conversion_p is transitive.
49 If a < b and b < c then a < c.
51 2) useless_type_conversion_p is not symmetric.
52 From a < b does not follow a > b.
54 3) Types define the available set of operations applicable to values.
55 A type conversion is useless if the operations for the target type
56 is a subset of the operations for the source type. For example
57 casts to void* are useless, casts from void* are not (void* can't
58 be dereferenced or offsetted, but copied, hence its set of operations
59 is a strict subset of that of all other data pointer types). Casts
60 to const T* are useless (can't be written to), casts from const T*
61 to T* are not. */
63 bool
64 useless_type_conversion_p (tree outer_type, tree inner_type)
66 /* Do the following before stripping toplevel qualifiers. */
67 if (POINTER_TYPE_P (inner_type)
68 && POINTER_TYPE_P (outer_type))
70 /* Do not lose casts between pointers to different address spaces. */
71 if (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
72 != TYPE_ADDR_SPACE (TREE_TYPE (inner_type)))
73 return false;
74 /* Do not lose casts to function pointer types. */
75 if ((TREE_CODE (TREE_TYPE (outer_type)) == FUNCTION_TYPE
76 || TREE_CODE (TREE_TYPE (outer_type)) == METHOD_TYPE)
77 && !(TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE
78 || TREE_CODE (TREE_TYPE (inner_type)) == METHOD_TYPE))
79 return false;
82 /* From now on qualifiers on value types do not matter. */
83 inner_type = TYPE_MAIN_VARIANT (inner_type);
84 outer_type = TYPE_MAIN_VARIANT (outer_type);
86 if (inner_type == outer_type)
87 return true;
89 /* Changes in machine mode are never useless conversions because the RTL
90 middle-end expects explicit conversions between modes. */
91 if (TYPE_MODE (inner_type) != TYPE_MODE (outer_type))
92 return false;
94 /* If both the inner and outer types are integral types, then the
95 conversion is not necessary if they have the same mode and
96 signedness and precision, and both or neither are boolean. */
97 if (INTEGRAL_TYPE_P (inner_type)
98 && INTEGRAL_TYPE_P (outer_type))
100 /* Preserve changes in signedness or precision. */
101 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
102 || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
103 return false;
105 /* Preserve conversions to/from BOOLEAN_TYPE if types are not
106 of precision one. */
107 if (((TREE_CODE (inner_type) == BOOLEAN_TYPE)
108 != (TREE_CODE (outer_type) == BOOLEAN_TYPE))
109 && TYPE_PRECISION (outer_type) != 1)
110 return false;
112 /* We don't need to preserve changes in the types minimum or
113 maximum value in general as these do not generate code
114 unless the types precisions are different. */
115 return true;
118 /* Scalar floating point types with the same mode are compatible. */
119 else if (SCALAR_FLOAT_TYPE_P (inner_type)
120 && SCALAR_FLOAT_TYPE_P (outer_type))
121 return true;
123 /* Fixed point types with the same mode are compatible. */
124 else if (FIXED_POINT_TYPE_P (inner_type)
125 && FIXED_POINT_TYPE_P (outer_type))
126 return true;
128 /* We need to take special care recursing to pointed-to types. */
129 else if (POINTER_TYPE_P (inner_type)
130 && POINTER_TYPE_P (outer_type))
132 /* We do not care for const qualification of the pointed-to types
133 as const qualification has no semantic value to the middle-end. */
135 /* Otherwise pointers/references are equivalent. */
136 return true;
139 /* Recurse for complex types. */
140 else if (TREE_CODE (inner_type) == COMPLEX_TYPE
141 && TREE_CODE (outer_type) == COMPLEX_TYPE)
142 return useless_type_conversion_p (TREE_TYPE (outer_type),
143 TREE_TYPE (inner_type));
145 /* Recurse for vector types with the same number of subparts. */
146 else if (TREE_CODE (inner_type) == VECTOR_TYPE
147 && TREE_CODE (outer_type) == VECTOR_TYPE
148 && TYPE_PRECISION (inner_type) == TYPE_PRECISION (outer_type))
149 return useless_type_conversion_p (TREE_TYPE (outer_type),
150 TREE_TYPE (inner_type));
152 else if (TREE_CODE (inner_type) == ARRAY_TYPE
153 && TREE_CODE (outer_type) == ARRAY_TYPE)
155 /* Preserve string attributes. */
156 if (TYPE_STRING_FLAG (inner_type) != TYPE_STRING_FLAG (outer_type))
157 return false;
159 /* Conversions from array types with unknown extent to
160 array types with known extent are not useless. */
161 if (!TYPE_DOMAIN (inner_type)
162 && TYPE_DOMAIN (outer_type))
163 return false;
165 /* Nor are conversions from array types with non-constant size to
166 array types with constant size or to different size. */
167 if (TYPE_SIZE (outer_type)
168 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST
169 && (!TYPE_SIZE (inner_type)
170 || TREE_CODE (TYPE_SIZE (inner_type)) != INTEGER_CST
171 || !tree_int_cst_equal (TYPE_SIZE (outer_type),
172 TYPE_SIZE (inner_type))))
173 return false;
175 /* Check conversions between arrays with partially known extents.
176 If the array min/max values are constant they have to match.
177 Otherwise allow conversions to unknown and variable extents.
178 In particular this declares conversions that may change the
179 mode to BLKmode as useless. */
180 if (TYPE_DOMAIN (inner_type)
181 && TYPE_DOMAIN (outer_type)
182 && TYPE_DOMAIN (inner_type) != TYPE_DOMAIN (outer_type))
184 tree inner_min = TYPE_MIN_VALUE (TYPE_DOMAIN (inner_type));
185 tree outer_min = TYPE_MIN_VALUE (TYPE_DOMAIN (outer_type));
186 tree inner_max = TYPE_MAX_VALUE (TYPE_DOMAIN (inner_type));
187 tree outer_max = TYPE_MAX_VALUE (TYPE_DOMAIN (outer_type));
189 /* After gimplification a variable min/max value carries no
190 additional information compared to a NULL value. All that
191 matters has been lowered to be part of the IL. */
192 if (inner_min && TREE_CODE (inner_min) != INTEGER_CST)
193 inner_min = NULL_TREE;
194 if (outer_min && TREE_CODE (outer_min) != INTEGER_CST)
195 outer_min = NULL_TREE;
196 if (inner_max && TREE_CODE (inner_max) != INTEGER_CST)
197 inner_max = NULL_TREE;
198 if (outer_max && TREE_CODE (outer_max) != INTEGER_CST)
199 outer_max = NULL_TREE;
201 /* Conversions NULL / variable <- cst are useless, but not
202 the other way around. */
203 if (outer_min
204 && (!inner_min
205 || !tree_int_cst_equal (inner_min, outer_min)))
206 return false;
207 if (outer_max
208 && (!inner_max
209 || !tree_int_cst_equal (inner_max, outer_max)))
210 return false;
213 /* Recurse on the element check. */
214 return useless_type_conversion_p (TREE_TYPE (outer_type),
215 TREE_TYPE (inner_type));
218 else if ((TREE_CODE (inner_type) == FUNCTION_TYPE
219 || TREE_CODE (inner_type) == METHOD_TYPE)
220 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
222 tree outer_parm, inner_parm;
224 /* If the return types are not compatible bail out. */
225 if (!useless_type_conversion_p (TREE_TYPE (outer_type),
226 TREE_TYPE (inner_type)))
227 return false;
229 /* Method types should belong to a compatible base class. */
230 if (TREE_CODE (inner_type) == METHOD_TYPE
231 && !useless_type_conversion_p (TYPE_METHOD_BASETYPE (outer_type),
232 TYPE_METHOD_BASETYPE (inner_type)))
233 return false;
235 /* A conversion to an unprototyped argument list is ok. */
236 if (!prototype_p (outer_type))
237 return true;
239 /* If the unqualified argument types are compatible the conversion
240 is useless. */
241 if (TYPE_ARG_TYPES (outer_type) == TYPE_ARG_TYPES (inner_type))
242 return true;
244 for (outer_parm = TYPE_ARG_TYPES (outer_type),
245 inner_parm = TYPE_ARG_TYPES (inner_type);
246 outer_parm && inner_parm;
247 outer_parm = TREE_CHAIN (outer_parm),
248 inner_parm = TREE_CHAIN (inner_parm))
249 if (!useless_type_conversion_p
250 (TYPE_MAIN_VARIANT (TREE_VALUE (outer_parm)),
251 TYPE_MAIN_VARIANT (TREE_VALUE (inner_parm))))
252 return false;
254 /* If there is a mismatch in the number of arguments the functions
255 are not compatible. */
256 if (outer_parm || inner_parm)
257 return false;
259 /* Defer to the target if necessary. */
260 if (TYPE_ATTRIBUTES (inner_type) || TYPE_ATTRIBUTES (outer_type))
261 return comp_type_attributes (outer_type, inner_type) != 0;
263 return true;
266 /* For aggregates we rely on TYPE_CANONICAL exclusively and require
267 explicit conversions for types involving to be structurally
268 compared types. */
269 else if (AGGREGATE_TYPE_P (inner_type)
270 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
271 return TYPE_CANONICAL (inner_type)
272 && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type);
274 else if (TREE_CODE (inner_type) == OFFSET_TYPE
275 && TREE_CODE (outer_type) == OFFSET_TYPE)
276 return useless_type_conversion_p (TREE_TYPE (outer_type),
277 TREE_TYPE (inner_type))
278 && useless_type_conversion_p
279 (TYPE_OFFSET_BASETYPE (outer_type),
280 TYPE_OFFSET_BASETYPE (inner_type));
282 return false;
286 /* ----- Decl related ----- */
288 /* Set sequence SEQ to be the GIMPLE body for function FN. */
290 void
291 gimple_set_body (tree fndecl, gimple_seq seq)
293 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
294 if (fn == NULL)
296 /* If FNDECL still does not have a function structure associated
297 with it, then it does not make sense for it to receive a
298 GIMPLE body. */
299 gcc_assert (seq == NULL);
301 else
302 fn->gimple_body = seq;
306 /* Return the body of GIMPLE statements for function FN. After the
307 CFG pass, the function body doesn't exist anymore because it has
308 been split up into basic blocks. In this case, it returns
309 NULL. */
311 gimple_seq
312 gimple_body (tree fndecl)
314 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
315 return fn ? fn->gimple_body : NULL;
318 /* Return true when FNDECL has Gimple body either in unlowered
319 or CFG form. */
320 bool
321 gimple_has_body_p (tree fndecl)
323 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
324 return (gimple_body (fndecl) || (fn && fn->cfg));
327 /* Return a printable name for symbol DECL. */
329 const char *
330 gimple_decl_printable_name (tree decl, int verbosity)
332 if (!DECL_NAME (decl))
333 return NULL;
335 if (DECL_ASSEMBLER_NAME_SET_P (decl))
337 const char *str, *mangled_str;
338 int dmgl_opts = DMGL_NO_OPTS;
340 if (verbosity >= 2)
342 dmgl_opts = DMGL_VERBOSE
343 | DMGL_ANSI
344 | DMGL_GNU_V3
345 | DMGL_RET_POSTFIX;
346 if (TREE_CODE (decl) == FUNCTION_DECL)
347 dmgl_opts |= DMGL_PARAMS;
350 mangled_str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
351 str = cplus_demangle_v3 (mangled_str, dmgl_opts);
352 return (str) ? str : mangled_str;
355 return IDENTIFIER_POINTER (DECL_NAME (decl));
359 /* Create a new VAR_DECL and copy information from VAR to it. */
361 tree
362 copy_var_decl (tree var, tree name, tree type)
364 tree copy = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, name, type);
366 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (var);
367 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (var);
368 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (var);
369 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (var);
370 DECL_IGNORED_P (copy) = DECL_IGNORED_P (var);
371 DECL_CONTEXT (copy) = DECL_CONTEXT (var);
372 TREE_NO_WARNING (copy) = TREE_NO_WARNING (var);
373 TREE_USED (copy) = 1;
374 DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
375 DECL_ATTRIBUTES (copy) = DECL_ATTRIBUTES (var);
377 return copy;
380 /* Strip off a legitimate source ending from the input string NAME of
381 length LEN. Rather than having to know the names used by all of
382 our front ends, we strip off an ending of a period followed by
383 up to five characters. (Java uses ".class".) */
385 static inline void
386 remove_suffix (char *name, int len)
388 int i;
390 for (i = 2; i < 8 && len > i; i++)
392 if (name[len - i] == '.')
394 name[len - i] = '\0';
395 break;
400 /* Create a new temporary name with PREFIX. Return an identifier. */
402 static GTY(()) unsigned int tmp_var_id_num;
404 tree
405 create_tmp_var_name (const char *prefix)
407 char *tmp_name;
409 if (prefix)
411 char *preftmp = ASTRDUP (prefix);
413 remove_suffix (preftmp, strlen (preftmp));
414 clean_symbol_name (preftmp);
416 prefix = preftmp;
419 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
420 return get_identifier (tmp_name);
423 /* Create a new temporary variable declaration of type TYPE.
424 Do NOT push it into the current binding. */
426 tree
427 create_tmp_var_raw (tree type, const char *prefix)
429 tree tmp_var;
431 tmp_var = build_decl (input_location,
432 VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
433 type);
435 /* The variable was declared by the compiler. */
436 DECL_ARTIFICIAL (tmp_var) = 1;
437 /* And we don't want debug info for it. */
438 DECL_IGNORED_P (tmp_var) = 1;
440 /* Make the variable writable. */
441 TREE_READONLY (tmp_var) = 0;
443 DECL_EXTERNAL (tmp_var) = 0;
444 TREE_STATIC (tmp_var) = 0;
445 TREE_USED (tmp_var) = 1;
447 return tmp_var;
450 /* Create a new temporary variable declaration of type TYPE. DO push the
451 variable into the current binding. Further, assume that this is called
452 only from gimplification or optimization, at which point the creation of
453 certain types are bugs. */
455 tree
456 create_tmp_var (tree type, const char *prefix)
458 tree tmp_var;
460 /* We don't allow types that are addressable (meaning we can't make copies),
461 or incomplete. We also used to reject every variable size objects here,
462 but now support those for which a constant upper bound can be obtained.
463 The processing for variable sizes is performed in gimple_add_tmp_var,
464 point at which it really matters and possibly reached via paths not going
465 through this function, e.g. after direct calls to create_tmp_var_raw. */
466 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
468 tmp_var = create_tmp_var_raw (type, prefix);
469 gimple_add_tmp_var (tmp_var);
470 return tmp_var;
473 /* Create a new temporary variable declaration of type TYPE by calling
474 create_tmp_var and if TYPE is a vector or a complex number, mark the new
475 temporary as gimple register. */
477 tree
478 create_tmp_reg (tree type, const char *prefix)
480 tree tmp;
482 tmp = create_tmp_var (type, prefix);
483 if (TREE_CODE (type) == COMPLEX_TYPE
484 || TREE_CODE (type) == VECTOR_TYPE)
485 DECL_GIMPLE_REG_P (tmp) = 1;
487 return tmp;
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. */
494 tree
495 create_tmp_reg_fn (struct function *fn, tree type, const char *prefix)
497 tree tmp;
499 tmp = create_tmp_var_raw (type, prefix);
500 gimple_add_tmp_var_fn (fn, tmp);
501 if (TREE_CODE (type) == COMPLEX_TYPE
502 || TREE_CODE (type) == VECTOR_TYPE)
503 DECL_GIMPLE_REG_P (tmp) = 1;
505 return tmp;
509 /* ----- Expression related ----- */
511 /* Extract the operands and code for expression EXPR into *SUBCODE_P,
512 *OP1_P, *OP2_P and *OP3_P respectively. */
514 void
515 extract_ops_from_tree_1 (tree expr, enum tree_code *subcode_p, tree *op1_p,
516 tree *op2_p, tree *op3_p)
518 enum gimple_rhs_class grhs_class;
520 *subcode_p = TREE_CODE (expr);
521 grhs_class = get_gimple_rhs_class (*subcode_p);
523 if (grhs_class == GIMPLE_TERNARY_RHS)
525 *op1_p = TREE_OPERAND (expr, 0);
526 *op2_p = TREE_OPERAND (expr, 1);
527 *op3_p = TREE_OPERAND (expr, 2);
529 else if (grhs_class == GIMPLE_BINARY_RHS)
531 *op1_p = TREE_OPERAND (expr, 0);
532 *op2_p = TREE_OPERAND (expr, 1);
533 *op3_p = NULL_TREE;
535 else if (grhs_class == GIMPLE_UNARY_RHS)
537 *op1_p = TREE_OPERAND (expr, 0);
538 *op2_p = NULL_TREE;
539 *op3_p = NULL_TREE;
541 else if (grhs_class == GIMPLE_SINGLE_RHS)
543 *op1_p = expr;
544 *op2_p = NULL_TREE;
545 *op3_p = NULL_TREE;
547 else
548 gcc_unreachable ();
551 /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
553 void
554 gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
555 tree *lhs_p, tree *rhs_p)
557 gcc_assert (COMPARISON_CLASS_P (cond)
558 || TREE_CODE (cond) == TRUTH_NOT_EXPR
559 || is_gimple_min_invariant (cond)
560 || SSA_VAR_P (cond));
562 extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
564 /* Canonicalize conditionals of the form 'if (!VAL)'. */
565 if (*code_p == TRUTH_NOT_EXPR)
567 *code_p = EQ_EXPR;
568 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
569 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
571 /* Canonicalize conditionals of the form 'if (VAL)' */
572 else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
574 *code_p = NE_EXPR;
575 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
576 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
580 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
582 bool
583 is_gimple_lvalue (tree t)
585 return (is_gimple_addressable (t)
586 || TREE_CODE (t) == WITH_SIZE_EXPR
587 /* These are complex lvalues, but don't have addresses, so they
588 go here. */
589 || TREE_CODE (t) == BIT_FIELD_REF);
592 /* Return true if T is a GIMPLE condition. */
594 bool
595 is_gimple_condexpr (tree t)
597 return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
598 && !tree_could_throw_p (t)
599 && is_gimple_val (TREE_OPERAND (t, 0))
600 && is_gimple_val (TREE_OPERAND (t, 1))));
603 /* Return true if T is a gimple address. */
605 bool
606 is_gimple_address (const_tree t)
608 tree op;
610 if (TREE_CODE (t) != ADDR_EXPR)
611 return false;
613 op = TREE_OPERAND (t, 0);
614 while (handled_component_p (op))
616 if ((TREE_CODE (op) == ARRAY_REF
617 || TREE_CODE (op) == ARRAY_RANGE_REF)
618 && !is_gimple_val (TREE_OPERAND (op, 1)))
619 return false;
621 op = TREE_OPERAND (op, 0);
624 if (CONSTANT_CLASS_P (op) || TREE_CODE (op) == MEM_REF)
625 return true;
627 switch (TREE_CODE (op))
629 case PARM_DECL:
630 case RESULT_DECL:
631 case LABEL_DECL:
632 case FUNCTION_DECL:
633 case VAR_DECL:
634 case CONST_DECL:
635 return true;
637 default:
638 return false;
642 /* Return true if T is a gimple invariant address. */
644 bool
645 is_gimple_invariant_address (const_tree t)
647 const_tree op;
649 if (TREE_CODE (t) != ADDR_EXPR)
650 return false;
652 op = strip_invariant_refs (TREE_OPERAND (t, 0));
653 if (!op)
654 return false;
656 if (TREE_CODE (op) == MEM_REF)
658 const_tree op0 = TREE_OPERAND (op, 0);
659 return (TREE_CODE (op0) == ADDR_EXPR
660 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
661 || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
664 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
667 /* Return true if T is a gimple invariant address at IPA level
668 (so addresses of variables on stack are not allowed). */
670 bool
671 is_gimple_ip_invariant_address (const_tree t)
673 const_tree op;
675 if (TREE_CODE (t) != ADDR_EXPR)
676 return false;
678 op = strip_invariant_refs (TREE_OPERAND (t, 0));
679 if (!op)
680 return false;
682 if (TREE_CODE (op) == MEM_REF)
684 const_tree op0 = TREE_OPERAND (op, 0);
685 return (TREE_CODE (op0) == ADDR_EXPR
686 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
687 || decl_address_ip_invariant_p (TREE_OPERAND (op0, 0))));
690 return CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op);
693 /* Return true if T is a GIMPLE minimal invariant. It's a restricted
694 form of function invariant. */
696 bool
697 is_gimple_min_invariant (const_tree t)
699 if (TREE_CODE (t) == ADDR_EXPR)
700 return is_gimple_invariant_address (t);
702 return is_gimple_constant (t);
705 /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
706 form of gimple minimal invariant. */
708 bool
709 is_gimple_ip_invariant (const_tree t)
711 if (TREE_CODE (t) == ADDR_EXPR)
712 return is_gimple_ip_invariant_address (t);
714 return is_gimple_constant (t);
717 /* Return true if T is a non-aggregate register variable. */
719 bool
720 is_gimple_reg (tree t)
722 if (virtual_operand_p (t))
723 return false;
725 if (TREE_CODE (t) == SSA_NAME)
726 return true;
728 if (!is_gimple_variable (t))
729 return false;
731 if (!is_gimple_reg_type (TREE_TYPE (t)))
732 return false;
734 /* A volatile decl is not acceptable because we can't reuse it as
735 needed. We need to copy it into a temp first. */
736 if (TREE_THIS_VOLATILE (t))
737 return false;
739 /* We define "registers" as things that can be renamed as needed,
740 which with our infrastructure does not apply to memory. */
741 if (needs_to_live_in_memory (t))
742 return false;
744 /* Hard register variables are an interesting case. For those that
745 are call-clobbered, we don't know where all the calls are, since
746 we don't (want to) take into account which operations will turn
747 into libcalls at the rtl level. For those that are call-saved,
748 we don't currently model the fact that calls may in fact change
749 global hard registers, nor do we examine ASM_CLOBBERS at the tree
750 level, and so miss variable changes that might imply. All around,
751 it seems safest to not do too much optimization with these at the
752 tree level at all. We'll have to rely on the rtl optimizers to
753 clean this up, as there we've got all the appropriate bits exposed. */
754 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
755 return false;
757 /* Complex and vector values must have been put into SSA-like form.
758 That is, no assignments to the individual components. */
759 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
760 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
761 return DECL_GIMPLE_REG_P (t);
763 return true;
767 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
769 bool
770 is_gimple_val (tree t)
772 /* Make loads from volatiles and memory vars explicit. */
773 if (is_gimple_variable (t)
774 && is_gimple_reg_type (TREE_TYPE (t))
775 && !is_gimple_reg (t))
776 return false;
778 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
781 /* Similarly, but accept hard registers as inputs to asm statements. */
783 bool
784 is_gimple_asm_val (tree t)
786 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
787 return true;
789 return is_gimple_val (t);
792 /* Return true if T is a GIMPLE minimal lvalue. */
794 bool
795 is_gimple_min_lval (tree t)
797 if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
798 return false;
799 return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF);
802 /* Return true if T is a valid function operand of a CALL_EXPR. */
804 bool
805 is_gimple_call_addr (tree t)
807 return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
810 /* Return true if T is a valid address operand of a MEM_REF. */
812 bool
813 is_gimple_mem_ref_addr (tree t)
815 return (is_gimple_reg (t)
816 || TREE_CODE (t) == INTEGER_CST
817 || (TREE_CODE (t) == ADDR_EXPR
818 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
819 || decl_address_invariant_p (TREE_OPERAND (t, 0)))));
822 /* Hold trees marked addressable during expand. */
824 static hash_set<tree> *mark_addressable_queue;
826 /* Mark X as addressable or queue it up if called during expand. We
827 don't want to apply it immediately during expand because decls are
828 made addressable at that point due to RTL-only concerns, such as
829 uses of memcpy for block moves, and TREE_ADDRESSABLE changes
830 is_gimple_reg, which might make it seem like a variable that used
831 to be a gimple_reg shouldn't have been an SSA name. So we queue up
832 this flag setting and only apply it when we're done with GIMPLE and
833 only RTL issues matter. */
835 static void
836 mark_addressable_1 (tree x)
838 if (!currently_expanding_to_rtl)
840 TREE_ADDRESSABLE (x) = 1;
841 return;
844 if (!mark_addressable_queue)
845 mark_addressable_queue = new hash_set<tree>();
846 mark_addressable_queue->add (x);
849 /* Adaptor for mark_addressable_1 for use in hash_set traversal. */
851 bool
852 mark_addressable_2 (tree const &x, void * ATTRIBUTE_UNUSED = NULL)
854 mark_addressable_1 (x);
855 return false;
858 /* Mark all queued trees as addressable, and empty the queue. To be
859 called right after clearing CURRENTLY_EXPANDING_TO_RTL. */
861 void
862 flush_mark_addressable_queue ()
864 gcc_assert (!currently_expanding_to_rtl);
865 if (mark_addressable_queue)
867 mark_addressable_queue->traverse<void*, mark_addressable_2> (NULL);
868 delete mark_addressable_queue;
869 mark_addressable_queue = NULL;
873 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
874 form and we don't do any syntax checking. */
876 void
877 mark_addressable (tree x)
879 while (handled_component_p (x))
880 x = TREE_OPERAND (x, 0);
881 if (TREE_CODE (x) == MEM_REF
882 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
883 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
884 if (TREE_CODE (x) != VAR_DECL
885 && TREE_CODE (x) != PARM_DECL
886 && TREE_CODE (x) != RESULT_DECL)
887 return;
888 mark_addressable_1 (x);
890 /* Also mark the artificial SSA_NAME that points to the partition of X. */
891 if (TREE_CODE (x) == VAR_DECL
892 && !DECL_EXTERNAL (x)
893 && !TREE_STATIC (x)
894 && cfun->gimple_df != NULL
895 && cfun->gimple_df->decls_to_pointers != NULL)
897 tree *namep = cfun->gimple_df->decls_to_pointers->get (x);
898 if (namep)
899 mark_addressable_1 (*namep);
903 /* Returns true iff T is a valid RHS for an assignment to a renamed
904 user -- or front-end generated artificial -- variable. */
906 bool
907 is_gimple_reg_rhs (tree t)
909 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
912 #include "gt-gimple-expr.h"