2015-08-04 Thomas Preud'homme <thomas.preudhomme@arm.com>
[official-gcc.git] / gcc / gimple-expr.c
blobb558d90dd2487f99b62ff7c5c4d62a6cd7e3ce3a
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 "hard-reg-set.h"
29 #include "alias.h"
30 #include "fold-const.h"
31 #include "internal-fn.h"
32 #include "tree-eh.h"
33 #include "stringpool.h"
34 #include "gimple-ssa.h"
35 #include "gimplify.h"
36 #include "stor-layout.h"
37 #include "demangle.h"
39 /* ----- Type related ----- */
41 /* Return true if the conversion from INNER_TYPE to OUTER_TYPE is a
42 useless type conversion, otherwise return false.
44 This function implicitly defines the middle-end type system. With
45 the notion of 'a < b' meaning that useless_type_conversion_p (a, b)
46 holds and 'a > b' meaning that useless_type_conversion_p (b, a) holds,
47 the following invariants shall be fulfilled:
49 1) useless_type_conversion_p is transitive.
50 If a < b and b < c then a < c.
52 2) useless_type_conversion_p is not symmetric.
53 From a < b does not follow a > b.
55 3) Types define the available set of operations applicable to values.
56 A type conversion is useless if the operations for the target type
57 is a subset of the operations for the source type. For example
58 casts to void* are useless, casts from void* are not (void* can't
59 be dereferenced or offsetted, but copied, hence its set of operations
60 is a strict subset of that of all other data pointer types). Casts
61 to const T* are useless (can't be written to), casts from const T*
62 to T* are not. */
64 bool
65 useless_type_conversion_p (tree outer_type, tree inner_type)
67 /* Do the following before stripping toplevel qualifiers. */
68 if (POINTER_TYPE_P (inner_type)
69 && POINTER_TYPE_P (outer_type))
71 /* Do not lose casts between pointers to different address spaces. */
72 if (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
73 != TYPE_ADDR_SPACE (TREE_TYPE (inner_type)))
74 return false;
75 /* Do not lose casts to function pointer types. */
76 if ((TREE_CODE (TREE_TYPE (outer_type)) == FUNCTION_TYPE
77 || TREE_CODE (TREE_TYPE (outer_type)) == METHOD_TYPE)
78 && !(TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE
79 || TREE_CODE (TREE_TYPE (inner_type)) == METHOD_TYPE))
80 return false;
83 /* From now on qualifiers on value types do not matter. */
84 inner_type = TYPE_MAIN_VARIANT (inner_type);
85 outer_type = TYPE_MAIN_VARIANT (outer_type);
87 if (inner_type == outer_type)
88 return true;
90 /* If we know the canonical types, compare them. */
91 if (TYPE_CANONICAL (inner_type)
92 && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type))
93 return true;
95 /* Changes in machine mode are never useless conversions unless we
96 deal with aggregate types in which case we defer to later checks. */
97 if (TYPE_MODE (inner_type) != TYPE_MODE (outer_type)
98 && !AGGREGATE_TYPE_P (inner_type))
99 return false;
101 /* If both the inner and outer types are integral types, then the
102 conversion is not necessary if they have the same mode and
103 signedness and precision, and both or neither are boolean. */
104 if (INTEGRAL_TYPE_P (inner_type)
105 && INTEGRAL_TYPE_P (outer_type))
107 /* Preserve changes in signedness or precision. */
108 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
109 || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
110 return false;
112 /* Preserve conversions to/from BOOLEAN_TYPE if types are not
113 of precision one. */
114 if (((TREE_CODE (inner_type) == BOOLEAN_TYPE)
115 != (TREE_CODE (outer_type) == BOOLEAN_TYPE))
116 && TYPE_PRECISION (outer_type) != 1)
117 return false;
119 /* We don't need to preserve changes in the types minimum or
120 maximum value in general as these do not generate code
121 unless the types precisions are different. */
122 return true;
125 /* Scalar floating point types with the same mode are compatible. */
126 else if (SCALAR_FLOAT_TYPE_P (inner_type)
127 && SCALAR_FLOAT_TYPE_P (outer_type))
128 return true;
130 /* Fixed point types with the same mode are compatible. */
131 else if (FIXED_POINT_TYPE_P (inner_type)
132 && FIXED_POINT_TYPE_P (outer_type))
133 return true;
135 /* We need to take special care recursing to pointed-to types. */
136 else if (POINTER_TYPE_P (inner_type)
137 && POINTER_TYPE_P (outer_type))
139 /* We do not care for const qualification of the pointed-to types
140 as const qualification has no semantic value to the middle-end. */
142 /* Otherwise pointers/references are equivalent. */
143 return true;
146 /* Recurse for complex types. */
147 else if (TREE_CODE (inner_type) == COMPLEX_TYPE
148 && TREE_CODE (outer_type) == COMPLEX_TYPE)
149 return useless_type_conversion_p (TREE_TYPE (outer_type),
150 TREE_TYPE (inner_type));
152 /* Recurse for vector types with the same number of subparts. */
153 else if (TREE_CODE (inner_type) == VECTOR_TYPE
154 && TREE_CODE (outer_type) == VECTOR_TYPE
155 && TYPE_PRECISION (inner_type) == TYPE_PRECISION (outer_type))
156 return useless_type_conversion_p (TREE_TYPE (outer_type),
157 TREE_TYPE (inner_type));
159 else if (TREE_CODE (inner_type) == ARRAY_TYPE
160 && TREE_CODE (outer_type) == ARRAY_TYPE)
162 /* Preserve string attributes. */
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)
169 && TYPE_DOMAIN (outer_type))
170 return false;
172 /* Nor are conversions from array types with non-constant size to
173 array types with constant size or to different size. */
174 if (TYPE_SIZE (outer_type)
175 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST
176 && (!TYPE_SIZE (inner_type)
177 || TREE_CODE (TYPE_SIZE (inner_type)) != INTEGER_CST
178 || !tree_int_cst_equal (TYPE_SIZE (outer_type),
179 TYPE_SIZE (inner_type))))
180 return false;
182 /* Check conversions between arrays with partially known extents.
183 If the array min/max values are constant they have to match.
184 Otherwise allow conversions to unknown and variable extents.
185 In particular this declares conversions that may change the
186 mode to BLKmode as useless. */
187 if (TYPE_DOMAIN (inner_type)
188 && TYPE_DOMAIN (outer_type)
189 && TYPE_DOMAIN (inner_type) != TYPE_DOMAIN (outer_type))
191 tree inner_min = TYPE_MIN_VALUE (TYPE_DOMAIN (inner_type));
192 tree outer_min = TYPE_MIN_VALUE (TYPE_DOMAIN (outer_type));
193 tree inner_max = TYPE_MAX_VALUE (TYPE_DOMAIN (inner_type));
194 tree outer_max = TYPE_MAX_VALUE (TYPE_DOMAIN (outer_type));
196 /* After gimplification a variable min/max value carries no
197 additional information compared to a NULL value. All that
198 matters has been lowered to be part of the IL. */
199 if (inner_min && TREE_CODE (inner_min) != INTEGER_CST)
200 inner_min = NULL_TREE;
201 if (outer_min && TREE_CODE (outer_min) != INTEGER_CST)
202 outer_min = NULL_TREE;
203 if (inner_max && TREE_CODE (inner_max) != INTEGER_CST)
204 inner_max = NULL_TREE;
205 if (outer_max && TREE_CODE (outer_max) != INTEGER_CST)
206 outer_max = NULL_TREE;
208 /* Conversions NULL / variable <- cst are useless, but not
209 the other way around. */
210 if (outer_min
211 && (!inner_min
212 || !tree_int_cst_equal (inner_min, outer_min)))
213 return false;
214 if (outer_max
215 && (!inner_max
216 || !tree_int_cst_equal (inner_max, outer_max)))
217 return false;
220 /* Recurse on the element check. */
221 return useless_type_conversion_p (TREE_TYPE (outer_type),
222 TREE_TYPE (inner_type));
225 else if ((TREE_CODE (inner_type) == FUNCTION_TYPE
226 || TREE_CODE (inner_type) == METHOD_TYPE)
227 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
229 tree outer_parm, inner_parm;
231 /* If the return types are not compatible bail out. */
232 if (!useless_type_conversion_p (TREE_TYPE (outer_type),
233 TREE_TYPE (inner_type)))
234 return false;
236 /* Method types should belong to a compatible base class. */
237 if (TREE_CODE (inner_type) == METHOD_TYPE
238 && !useless_type_conversion_p (TYPE_METHOD_BASETYPE (outer_type),
239 TYPE_METHOD_BASETYPE (inner_type)))
240 return false;
242 /* A conversion to an unprototyped argument list is ok. */
243 if (!prototype_p (outer_type))
244 return true;
246 /* If the unqualified argument types are compatible the conversion
247 is useless. */
248 if (TYPE_ARG_TYPES (outer_type) == TYPE_ARG_TYPES (inner_type))
249 return true;
251 for (outer_parm = TYPE_ARG_TYPES (outer_type),
252 inner_parm = TYPE_ARG_TYPES (inner_type);
253 outer_parm && inner_parm;
254 outer_parm = TREE_CHAIN (outer_parm),
255 inner_parm = TREE_CHAIN (inner_parm))
256 if (!useless_type_conversion_p
257 (TYPE_MAIN_VARIANT (TREE_VALUE (outer_parm)),
258 TYPE_MAIN_VARIANT (TREE_VALUE (inner_parm))))
259 return false;
261 /* If there is a mismatch in the number of arguments the functions
262 are not compatible. */
263 if (outer_parm || inner_parm)
264 return false;
266 /* Defer to the target if necessary. */
267 if (TYPE_ATTRIBUTES (inner_type) || TYPE_ATTRIBUTES (outer_type))
268 return comp_type_attributes (outer_type, inner_type) != 0;
270 return true;
273 /* For aggregates we rely on TYPE_CANONICAL exclusively and require
274 explicit conversions for types involving to be structurally
275 compared types. */
276 else if (AGGREGATE_TYPE_P (inner_type)
277 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
278 return false;
280 return false;
284 /* ----- Decl related ----- */
286 /* Set sequence SEQ to be the GIMPLE body for function FN. */
288 void
289 gimple_set_body (tree fndecl, gimple_seq seq)
291 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
292 if (fn == NULL)
294 /* If FNDECL still does not have a function structure associated
295 with it, then it does not make sense for it to receive a
296 GIMPLE body. */
297 gcc_assert (seq == NULL);
299 else
300 fn->gimple_body = seq;
304 /* Return the body of GIMPLE statements for function FN. After the
305 CFG pass, the function body doesn't exist anymore because it has
306 been split up into basic blocks. In this case, it returns
307 NULL. */
309 gimple_seq
310 gimple_body (tree fndecl)
312 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
313 return fn ? fn->gimple_body : NULL;
316 /* Return true when FNDECL has Gimple body either in unlowered
317 or CFG form. */
318 bool
319 gimple_has_body_p (tree fndecl)
321 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
322 return (gimple_body (fndecl) || (fn && fn->cfg));
325 /* Return a printable name for symbol DECL. */
327 const char *
328 gimple_decl_printable_name (tree decl, int verbosity)
330 if (!DECL_NAME (decl))
331 return NULL;
333 if (DECL_ASSEMBLER_NAME_SET_P (decl))
335 const char *str, *mangled_str;
336 int dmgl_opts = DMGL_NO_OPTS;
338 if (verbosity >= 2)
340 dmgl_opts = DMGL_VERBOSE
341 | DMGL_ANSI
342 | DMGL_GNU_V3
343 | DMGL_RET_POSTFIX;
344 if (TREE_CODE (decl) == FUNCTION_DECL)
345 dmgl_opts |= DMGL_PARAMS;
348 mangled_str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
349 str = cplus_demangle_v3 (mangled_str, dmgl_opts);
350 return (str) ? str : mangled_str;
353 return IDENTIFIER_POINTER (DECL_NAME (decl));
357 /* Create a new VAR_DECL and copy information from VAR to it. */
359 tree
360 copy_var_decl (tree var, tree name, tree type)
362 tree copy = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, name, type);
364 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (var);
365 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (var);
366 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (var);
367 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (var);
368 DECL_IGNORED_P (copy) = DECL_IGNORED_P (var);
369 DECL_CONTEXT (copy) = DECL_CONTEXT (var);
370 TREE_NO_WARNING (copy) = TREE_NO_WARNING (var);
371 TREE_USED (copy) = 1;
372 DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
373 DECL_ATTRIBUTES (copy) = DECL_ATTRIBUTES (var);
375 return copy;
378 /* Given SSA_NAMEs NAME1 and NAME2, return true if they are candidates for
379 coalescing together, false otherwise.
381 This must stay consistent with var_map_base_init in tree-ssa-live.c. */
383 bool
384 gimple_can_coalesce_p (tree name1, tree name2)
386 /* First check the SSA_NAME's associated DECL. We only want to
387 coalesce if they have the same DECL or both have no associated DECL. */
388 tree var1 = SSA_NAME_VAR (name1);
389 tree var2 = SSA_NAME_VAR (name2);
390 var1 = (var1 && (!VAR_P (var1) || !DECL_IGNORED_P (var1))) ? var1 : NULL_TREE;
391 var2 = (var2 && (!VAR_P (var2) || !DECL_IGNORED_P (var2))) ? var2 : NULL_TREE;
392 if (var1 != var2)
393 return false;
395 /* Now check the types. If the types are the same, then we should
396 try to coalesce V1 and V2. */
397 tree t1 = TREE_TYPE (name1);
398 tree t2 = TREE_TYPE (name2);
399 if (t1 == t2)
400 return true;
402 /* If the types are not the same, check for a canonical type match. This
403 (for example) allows coalescing when the types are fundamentally the
404 same, but just have different names.
406 Note pointer types with different address spaces may have the same
407 canonical type. Those are rejected for coalescing by the
408 types_compatible_p check. */
409 if (TYPE_CANONICAL (t1)
410 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)
411 && types_compatible_p (t1, t2))
412 return true;
414 return false;
417 /* Strip off a legitimate source ending from the input string NAME of
418 length LEN. Rather than having to know the names used by all of
419 our front ends, we strip off an ending of a period followed by
420 up to five characters. (Java uses ".class".) */
422 static inline void
423 remove_suffix (char *name, int len)
425 int i;
427 for (i = 2; i < 8 && len > i; i++)
429 if (name[len - i] == '.')
431 name[len - i] = '\0';
432 break;
437 /* Create a new temporary name with PREFIX. Return an identifier. */
439 static GTY(()) unsigned int tmp_var_id_num;
441 tree
442 create_tmp_var_name (const char *prefix)
444 char *tmp_name;
446 if (prefix)
448 char *preftmp = ASTRDUP (prefix);
450 remove_suffix (preftmp, strlen (preftmp));
451 clean_symbol_name (preftmp);
453 prefix = preftmp;
456 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
457 return get_identifier (tmp_name);
460 /* Create a new temporary variable declaration of type TYPE.
461 Do NOT push it into the current binding. */
463 tree
464 create_tmp_var_raw (tree type, const char *prefix)
466 tree tmp_var;
468 tmp_var = build_decl (input_location,
469 VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
470 type);
472 /* The variable was declared by the compiler. */
473 DECL_ARTIFICIAL (tmp_var) = 1;
474 /* And we don't want debug info for it. */
475 DECL_IGNORED_P (tmp_var) = 1;
477 /* Make the variable writable. */
478 TREE_READONLY (tmp_var) = 0;
480 DECL_EXTERNAL (tmp_var) = 0;
481 TREE_STATIC (tmp_var) = 0;
482 TREE_USED (tmp_var) = 1;
484 return tmp_var;
487 /* Create a new temporary variable declaration of type TYPE. DO push the
488 variable into the current binding. Further, assume that this is called
489 only from gimplification or optimization, at which point the creation of
490 certain types are bugs. */
492 tree
493 create_tmp_var (tree type, const char *prefix)
495 tree tmp_var;
497 /* We don't allow types that are addressable (meaning we can't make copies),
498 or incomplete. We also used to reject every variable size objects here,
499 but now support those for which a constant upper bound can be obtained.
500 The processing for variable sizes is performed in gimple_add_tmp_var,
501 point at which it really matters and possibly reached via paths not going
502 through this function, e.g. after direct calls to create_tmp_var_raw. */
503 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
505 tmp_var = create_tmp_var_raw (type, prefix);
506 gimple_add_tmp_var (tmp_var);
507 return tmp_var;
510 /* Create a new temporary variable declaration of type TYPE by calling
511 create_tmp_var and if TYPE is a vector or a complex number, mark the new
512 temporary as gimple register. */
514 tree
515 create_tmp_reg (tree type, const char *prefix)
517 tree tmp;
519 tmp = create_tmp_var (type, prefix);
520 if (TREE_CODE (type) == COMPLEX_TYPE
521 || TREE_CODE (type) == VECTOR_TYPE)
522 DECL_GIMPLE_REG_P (tmp) = 1;
524 return tmp;
527 /* Create a new temporary variable declaration of type TYPE by calling
528 create_tmp_var and if TYPE is a vector or a complex number, mark the new
529 temporary as gimple register. */
531 tree
532 create_tmp_reg_fn (struct function *fn, tree type, const char *prefix)
534 tree tmp;
536 tmp = create_tmp_var_raw (type, prefix);
537 gimple_add_tmp_var_fn (fn, tmp);
538 if (TREE_CODE (type) == COMPLEX_TYPE
539 || TREE_CODE (type) == VECTOR_TYPE)
540 DECL_GIMPLE_REG_P (tmp) = 1;
542 return tmp;
546 /* ----- Expression related ----- */
548 /* Extract the operands and code for expression EXPR into *SUBCODE_P,
549 *OP1_P, *OP2_P and *OP3_P respectively. */
551 void
552 extract_ops_from_tree_1 (tree expr, enum tree_code *subcode_p, tree *op1_p,
553 tree *op2_p, tree *op3_p)
555 enum gimple_rhs_class grhs_class;
557 *subcode_p = TREE_CODE (expr);
558 grhs_class = get_gimple_rhs_class (*subcode_p);
560 if (grhs_class == GIMPLE_TERNARY_RHS)
562 *op1_p = TREE_OPERAND (expr, 0);
563 *op2_p = TREE_OPERAND (expr, 1);
564 *op3_p = TREE_OPERAND (expr, 2);
566 else if (grhs_class == GIMPLE_BINARY_RHS)
568 *op1_p = TREE_OPERAND (expr, 0);
569 *op2_p = TREE_OPERAND (expr, 1);
570 *op3_p = NULL_TREE;
572 else if (grhs_class == GIMPLE_UNARY_RHS)
574 *op1_p = TREE_OPERAND (expr, 0);
575 *op2_p = NULL_TREE;
576 *op3_p = NULL_TREE;
578 else if (grhs_class == GIMPLE_SINGLE_RHS)
580 *op1_p = expr;
581 *op2_p = NULL_TREE;
582 *op3_p = NULL_TREE;
584 else
585 gcc_unreachable ();
588 /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
590 void
591 gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
592 tree *lhs_p, tree *rhs_p)
594 gcc_assert (COMPARISON_CLASS_P (cond)
595 || TREE_CODE (cond) == TRUTH_NOT_EXPR
596 || is_gimple_min_invariant (cond)
597 || SSA_VAR_P (cond));
599 extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
601 /* Canonicalize conditionals of the form 'if (!VAL)'. */
602 if (*code_p == TRUTH_NOT_EXPR)
604 *code_p = EQ_EXPR;
605 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
606 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
608 /* Canonicalize conditionals of the form 'if (VAL)' */
609 else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
611 *code_p = NE_EXPR;
612 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
613 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
617 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
619 bool
620 is_gimple_lvalue (tree t)
622 return (is_gimple_addressable (t)
623 || TREE_CODE (t) == WITH_SIZE_EXPR
624 /* These are complex lvalues, but don't have addresses, so they
625 go here. */
626 || TREE_CODE (t) == BIT_FIELD_REF);
629 /* Return true if T is a GIMPLE condition. */
631 bool
632 is_gimple_condexpr (tree t)
634 return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
635 && !tree_could_throw_p (t)
636 && is_gimple_val (TREE_OPERAND (t, 0))
637 && is_gimple_val (TREE_OPERAND (t, 1))));
640 /* Return true if T is a gimple address. */
642 bool
643 is_gimple_address (const_tree t)
645 tree op;
647 if (TREE_CODE (t) != ADDR_EXPR)
648 return false;
650 op = TREE_OPERAND (t, 0);
651 while (handled_component_p (op))
653 if ((TREE_CODE (op) == ARRAY_REF
654 || TREE_CODE (op) == ARRAY_RANGE_REF)
655 && !is_gimple_val (TREE_OPERAND (op, 1)))
656 return false;
658 op = TREE_OPERAND (op, 0);
661 if (CONSTANT_CLASS_P (op) || TREE_CODE (op) == MEM_REF)
662 return true;
664 switch (TREE_CODE (op))
666 case PARM_DECL:
667 case RESULT_DECL:
668 case LABEL_DECL:
669 case FUNCTION_DECL:
670 case VAR_DECL:
671 case CONST_DECL:
672 return true;
674 default:
675 return false;
679 /* Return true if T is a gimple invariant address. */
681 bool
682 is_gimple_invariant_address (const_tree t)
684 const_tree op;
686 if (TREE_CODE (t) != ADDR_EXPR)
687 return false;
689 op = strip_invariant_refs (TREE_OPERAND (t, 0));
690 if (!op)
691 return false;
693 if (TREE_CODE (op) == MEM_REF)
695 const_tree op0 = TREE_OPERAND (op, 0);
696 return (TREE_CODE (op0) == ADDR_EXPR
697 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
698 || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
701 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
704 /* Return true if T is a gimple invariant address at IPA level
705 (so addresses of variables on stack are not allowed). */
707 bool
708 is_gimple_ip_invariant_address (const_tree t)
710 const_tree op;
712 if (TREE_CODE (t) != ADDR_EXPR)
713 return false;
715 op = strip_invariant_refs (TREE_OPERAND (t, 0));
716 if (!op)
717 return false;
719 if (TREE_CODE (op) == MEM_REF)
721 const_tree op0 = TREE_OPERAND (op, 0);
722 return (TREE_CODE (op0) == ADDR_EXPR
723 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
724 || decl_address_ip_invariant_p (TREE_OPERAND (op0, 0))));
727 return CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op);
730 /* Return true if T is a GIMPLE minimal invariant. It's a restricted
731 form of function invariant. */
733 bool
734 is_gimple_min_invariant (const_tree t)
736 if (TREE_CODE (t) == ADDR_EXPR)
737 return is_gimple_invariant_address (t);
739 return is_gimple_constant (t);
742 /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
743 form of gimple minimal invariant. */
745 bool
746 is_gimple_ip_invariant (const_tree t)
748 if (TREE_CODE (t) == ADDR_EXPR)
749 return is_gimple_ip_invariant_address (t);
751 return is_gimple_constant (t);
754 /* Return true if T is a non-aggregate register variable. */
756 bool
757 is_gimple_reg (tree t)
759 if (virtual_operand_p (t))
760 return false;
762 if (TREE_CODE (t) == SSA_NAME)
763 return true;
765 if (!is_gimple_variable (t))
766 return false;
768 if (!is_gimple_reg_type (TREE_TYPE (t)))
769 return false;
771 /* A volatile decl is not acceptable because we can't reuse it as
772 needed. We need to copy it into a temp first. */
773 if (TREE_THIS_VOLATILE (t))
774 return false;
776 /* We define "registers" as things that can be renamed as needed,
777 which with our infrastructure does not apply to memory. */
778 if (needs_to_live_in_memory (t))
779 return false;
781 /* Hard register variables are an interesting case. For those that
782 are call-clobbered, we don't know where all the calls are, since
783 we don't (want to) take into account which operations will turn
784 into libcalls at the rtl level. For those that are call-saved,
785 we don't currently model the fact that calls may in fact change
786 global hard registers, nor do we examine ASM_CLOBBERS at the tree
787 level, and so miss variable changes that might imply. All around,
788 it seems safest to not do too much optimization with these at the
789 tree level at all. We'll have to rely on the rtl optimizers to
790 clean this up, as there we've got all the appropriate bits exposed. */
791 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
792 return false;
794 /* Complex and vector values must have been put into SSA-like form.
795 That is, no assignments to the individual components. */
796 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
797 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
798 return DECL_GIMPLE_REG_P (t);
800 return true;
804 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
806 bool
807 is_gimple_val (tree t)
809 /* Make loads from volatiles and memory vars explicit. */
810 if (is_gimple_variable (t)
811 && is_gimple_reg_type (TREE_TYPE (t))
812 && !is_gimple_reg (t))
813 return false;
815 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
818 /* Similarly, but accept hard registers as inputs to asm statements. */
820 bool
821 is_gimple_asm_val (tree t)
823 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
824 return true;
826 return is_gimple_val (t);
829 /* Return true if T is a GIMPLE minimal lvalue. */
831 bool
832 is_gimple_min_lval (tree t)
834 if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
835 return false;
836 return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF);
839 /* Return true if T is a valid function operand of a CALL_EXPR. */
841 bool
842 is_gimple_call_addr (tree t)
844 return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
847 /* Return true if T is a valid address operand of a MEM_REF. */
849 bool
850 is_gimple_mem_ref_addr (tree t)
852 return (is_gimple_reg (t)
853 || TREE_CODE (t) == INTEGER_CST
854 || (TREE_CODE (t) == ADDR_EXPR
855 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
856 || decl_address_invariant_p (TREE_OPERAND (t, 0)))));
859 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
860 form and we don't do any syntax checking. */
862 void
863 mark_addressable (tree x)
865 while (handled_component_p (x))
866 x = TREE_OPERAND (x, 0);
867 if (TREE_CODE (x) == MEM_REF
868 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
869 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
870 if (TREE_CODE (x) != VAR_DECL
871 && TREE_CODE (x) != PARM_DECL
872 && TREE_CODE (x) != RESULT_DECL)
873 return;
874 TREE_ADDRESSABLE (x) = 1;
876 /* Also mark the artificial SSA_NAME that points to the partition of X. */
877 if (TREE_CODE (x) == VAR_DECL
878 && !DECL_EXTERNAL (x)
879 && !TREE_STATIC (x)
880 && cfun->gimple_df != NULL
881 && cfun->gimple_df->decls_to_pointers != NULL)
883 tree *namep = cfun->gimple_df->decls_to_pointers->get (x);
884 if (namep)
885 TREE_ADDRESSABLE (*namep) = 1;
889 /* Returns true iff T is a valid RHS for an assignment to a renamed
890 user -- or front-end generated artificial -- variable. */
892 bool
893 is_gimple_reg_rhs (tree t)
895 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
898 #include "gt-gimple-expr.h"