2013-11-21 Edward Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / gimple-expr.c
blob77660a34c6de374eac68281193531261418c6991
1 /* Gimple decl, type, and expression support functions.
3 Copyright (C) 2007-2013 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 "tm.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "stringpool.h"
29 #include "gimplify.h"
30 #include "stor-layout.h"
31 #include "demangle.h"
32 #include "gimple-ssa.h"
34 /* ----- Type related ----- */
36 /* Return true if the conversion from INNER_TYPE to OUTER_TYPE is a
37 useless type conversion, otherwise return false.
39 This function implicitly defines the middle-end type system. With
40 the notion of 'a < b' meaning that useless_type_conversion_p (a, b)
41 holds and 'a > b' meaning that useless_type_conversion_p (b, a) holds,
42 the following invariants shall be fulfilled:
44 1) useless_type_conversion_p is transitive.
45 If a < b and b < c then a < c.
47 2) useless_type_conversion_p is not symmetric.
48 From a < b does not follow a > b.
50 3) Types define the available set of operations applicable to values.
51 A type conversion is useless if the operations for the target type
52 is a subset of the operations for the source type. For example
53 casts to void* are useless, casts from void* are not (void* can't
54 be dereferenced or offsetted, but copied, hence its set of operations
55 is a strict subset of that of all other data pointer types). Casts
56 to const T* are useless (can't be written to), casts from const T*
57 to T* are not. */
59 bool
60 useless_type_conversion_p (tree outer_type, tree inner_type)
62 /* Do the following before stripping toplevel qualifiers. */
63 if (POINTER_TYPE_P (inner_type)
64 && POINTER_TYPE_P (outer_type))
66 /* Do not lose casts between pointers to different address spaces. */
67 if (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
68 != TYPE_ADDR_SPACE (TREE_TYPE (inner_type)))
69 return false;
72 /* From now on qualifiers on value types do not matter. */
73 inner_type = TYPE_MAIN_VARIANT (inner_type);
74 outer_type = TYPE_MAIN_VARIANT (outer_type);
76 if (inner_type == outer_type)
77 return true;
79 /* If we know the canonical types, compare them. */
80 if (TYPE_CANONICAL (inner_type)
81 && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type))
82 return true;
84 /* Changes in machine mode are never useless conversions unless we
85 deal with aggregate types in which case we defer to later checks. */
86 if (TYPE_MODE (inner_type) != TYPE_MODE (outer_type)
87 && !AGGREGATE_TYPE_P (inner_type))
88 return false;
90 /* If both the inner and outer types are integral types, then the
91 conversion is not necessary if they have the same mode and
92 signedness and precision, and both or neither are boolean. */
93 if (INTEGRAL_TYPE_P (inner_type)
94 && INTEGRAL_TYPE_P (outer_type))
96 /* Preserve changes in signedness or precision. */
97 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
98 || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
99 return false;
101 /* Preserve conversions to/from BOOLEAN_TYPE if types are not
102 of precision one. */
103 if (((TREE_CODE (inner_type) == BOOLEAN_TYPE)
104 != (TREE_CODE (outer_type) == BOOLEAN_TYPE))
105 && TYPE_PRECISION (outer_type) != 1)
106 return false;
108 /* We don't need to preserve changes in the types minimum or
109 maximum value in general as these do not generate code
110 unless the types precisions are different. */
111 return true;
114 /* Scalar floating point types with the same mode are compatible. */
115 else if (SCALAR_FLOAT_TYPE_P (inner_type)
116 && SCALAR_FLOAT_TYPE_P (outer_type))
117 return true;
119 /* Fixed point types with the same mode are compatible. */
120 else if (FIXED_POINT_TYPE_P (inner_type)
121 && FIXED_POINT_TYPE_P (outer_type))
122 return true;
124 /* We need to take special care recursing to pointed-to types. */
125 else if (POINTER_TYPE_P (inner_type)
126 && POINTER_TYPE_P (outer_type))
128 /* Do not lose casts to function pointer types. */
129 if ((TREE_CODE (TREE_TYPE (outer_type)) == FUNCTION_TYPE
130 || TREE_CODE (TREE_TYPE (outer_type)) == METHOD_TYPE)
131 && !(TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE
132 || TREE_CODE (TREE_TYPE (inner_type)) == METHOD_TYPE))
133 return false;
135 /* We do not care for const qualification of the pointed-to types
136 as const qualification has no semantic value to the middle-end. */
138 /* Otherwise pointers/references are equivalent. */
139 return true;
142 /* Recurse for complex types. */
143 else if (TREE_CODE (inner_type) == COMPLEX_TYPE
144 && TREE_CODE (outer_type) == COMPLEX_TYPE)
145 return useless_type_conversion_p (TREE_TYPE (outer_type),
146 TREE_TYPE (inner_type));
148 /* Recurse for vector types with the same number of subparts. */
149 else if (TREE_CODE (inner_type) == VECTOR_TYPE
150 && TREE_CODE (outer_type) == VECTOR_TYPE
151 && TYPE_PRECISION (inner_type) == TYPE_PRECISION (outer_type))
152 return useless_type_conversion_p (TREE_TYPE (outer_type),
153 TREE_TYPE (inner_type));
155 else if (TREE_CODE (inner_type) == ARRAY_TYPE
156 && TREE_CODE (outer_type) == ARRAY_TYPE)
158 /* Preserve string attributes. */
159 if (TYPE_STRING_FLAG (inner_type) != TYPE_STRING_FLAG (outer_type))
160 return false;
162 /* Conversions from array types with unknown extent to
163 array types with known extent are not useless. */
164 if (!TYPE_DOMAIN (inner_type)
165 && TYPE_DOMAIN (outer_type))
166 return false;
168 /* Nor are conversions from array types with non-constant size to
169 array types with constant size or to different size. */
170 if (TYPE_SIZE (outer_type)
171 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST
172 && (!TYPE_SIZE (inner_type)
173 || TREE_CODE (TYPE_SIZE (inner_type)) != INTEGER_CST
174 || !tree_int_cst_equal (TYPE_SIZE (outer_type),
175 TYPE_SIZE (inner_type))))
176 return false;
178 /* Check conversions between arrays with partially known extents.
179 If the array min/max values are constant they have to match.
180 Otherwise allow conversions to unknown and variable extents.
181 In particular this declares conversions that may change the
182 mode to BLKmode as useless. */
183 if (TYPE_DOMAIN (inner_type)
184 && TYPE_DOMAIN (outer_type)
185 && TYPE_DOMAIN (inner_type) != TYPE_DOMAIN (outer_type))
187 tree inner_min = TYPE_MIN_VALUE (TYPE_DOMAIN (inner_type));
188 tree outer_min = TYPE_MIN_VALUE (TYPE_DOMAIN (outer_type));
189 tree inner_max = TYPE_MAX_VALUE (TYPE_DOMAIN (inner_type));
190 tree outer_max = TYPE_MAX_VALUE (TYPE_DOMAIN (outer_type));
192 /* After gimplification a variable min/max value carries no
193 additional information compared to a NULL value. All that
194 matters has been lowered to be part of the IL. */
195 if (inner_min && TREE_CODE (inner_min) != INTEGER_CST)
196 inner_min = NULL_TREE;
197 if (outer_min && TREE_CODE (outer_min) != INTEGER_CST)
198 outer_min = NULL_TREE;
199 if (inner_max && TREE_CODE (inner_max) != INTEGER_CST)
200 inner_max = NULL_TREE;
201 if (outer_max && TREE_CODE (outer_max) != INTEGER_CST)
202 outer_max = NULL_TREE;
204 /* Conversions NULL / variable <- cst are useless, but not
205 the other way around. */
206 if (outer_min
207 && (!inner_min
208 || !tree_int_cst_equal (inner_min, outer_min)))
209 return false;
210 if (outer_max
211 && (!inner_max
212 || !tree_int_cst_equal (inner_max, outer_max)))
213 return false;
216 /* Recurse on the element check. */
217 return useless_type_conversion_p (TREE_TYPE (outer_type),
218 TREE_TYPE (inner_type));
221 else if ((TREE_CODE (inner_type) == FUNCTION_TYPE
222 || TREE_CODE (inner_type) == METHOD_TYPE)
223 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
225 tree outer_parm, inner_parm;
227 /* If the return types are not compatible bail out. */
228 if (!useless_type_conversion_p (TREE_TYPE (outer_type),
229 TREE_TYPE (inner_type)))
230 return false;
232 /* Method types should belong to a compatible base class. */
233 if (TREE_CODE (inner_type) == METHOD_TYPE
234 && !useless_type_conversion_p (TYPE_METHOD_BASETYPE (outer_type),
235 TYPE_METHOD_BASETYPE (inner_type)))
236 return false;
238 /* A conversion to an unprototyped argument list is ok. */
239 if (!prototype_p (outer_type))
240 return true;
242 /* If the unqualified argument types are compatible the conversion
243 is useless. */
244 if (TYPE_ARG_TYPES (outer_type) == TYPE_ARG_TYPES (inner_type))
245 return true;
247 for (outer_parm = TYPE_ARG_TYPES (outer_type),
248 inner_parm = TYPE_ARG_TYPES (inner_type);
249 outer_parm && inner_parm;
250 outer_parm = TREE_CHAIN (outer_parm),
251 inner_parm = TREE_CHAIN (inner_parm))
252 if (!useless_type_conversion_p
253 (TYPE_MAIN_VARIANT (TREE_VALUE (outer_parm)),
254 TYPE_MAIN_VARIANT (TREE_VALUE (inner_parm))))
255 return false;
257 /* If there is a mismatch in the number of arguments the functions
258 are not compatible. */
259 if (outer_parm || inner_parm)
260 return false;
262 /* Defer to the target if necessary. */
263 if (TYPE_ATTRIBUTES (inner_type) || TYPE_ATTRIBUTES (outer_type))
264 return comp_type_attributes (outer_type, inner_type) != 0;
266 return true;
269 /* For aggregates we rely on TYPE_CANONICAL exclusively and require
270 explicit conversions for types involving to be structurally
271 compared types. */
272 else if (AGGREGATE_TYPE_P (inner_type)
273 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
274 return false;
276 return false;
280 /* ----- Decl related ----- */
282 /* Set sequence SEQ to be the GIMPLE body for function FN. */
284 void
285 gimple_set_body (tree fndecl, gimple_seq seq)
287 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
288 if (fn == NULL)
290 /* If FNDECL still does not have a function structure associated
291 with it, then it does not make sense for it to receive a
292 GIMPLE body. */
293 gcc_assert (seq == NULL);
295 else
296 fn->gimple_body = seq;
300 /* Return the body of GIMPLE statements for function FN. After the
301 CFG pass, the function body doesn't exist anymore because it has
302 been split up into basic blocks. In this case, it returns
303 NULL. */
305 gimple_seq
306 gimple_body (tree fndecl)
308 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
309 return fn ? fn->gimple_body : NULL;
312 /* Return true when FNDECL has Gimple body either in unlowered
313 or CFG form. */
314 bool
315 gimple_has_body_p (tree fndecl)
317 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
318 return (gimple_body (fndecl) || (fn && fn->cfg));
321 /* Return a printable name for symbol DECL. */
323 const char *
324 gimple_decl_printable_name (tree decl, int verbosity)
326 if (!DECL_NAME (decl))
327 return NULL;
329 if (DECL_ASSEMBLER_NAME_SET_P (decl))
331 const char *str, *mangled_str;
332 int dmgl_opts = DMGL_NO_OPTS;
334 if (verbosity >= 2)
336 dmgl_opts = DMGL_VERBOSE
337 | DMGL_ANSI
338 | DMGL_GNU_V3
339 | DMGL_RET_POSTFIX;
340 if (TREE_CODE (decl) == FUNCTION_DECL)
341 dmgl_opts |= DMGL_PARAMS;
344 mangled_str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
345 str = cplus_demangle_v3 (mangled_str, dmgl_opts);
346 return (str) ? str : mangled_str;
349 return IDENTIFIER_POINTER (DECL_NAME (decl));
353 /* Create a new VAR_DECL and copy information from VAR to it. */
355 tree
356 copy_var_decl (tree var, tree name, tree type)
358 tree copy = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, name, type);
360 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (var);
361 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (var);
362 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (var);
363 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (var);
364 DECL_IGNORED_P (copy) = DECL_IGNORED_P (var);
365 DECL_CONTEXT (copy) = DECL_CONTEXT (var);
366 TREE_NO_WARNING (copy) = TREE_NO_WARNING (var);
367 TREE_USED (copy) = 1;
368 DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
369 DECL_ATTRIBUTES (copy) = DECL_ATTRIBUTES (var);
371 return copy;
374 /* Given SSA_NAMEs NAME1 and NAME2, return true if they are candidates for
375 coalescing together, false otherwise.
377 This must stay consistent with var_map_base_init in tree-ssa-live.c. */
379 bool
380 gimple_can_coalesce_p (tree name1, tree name2)
382 /* First check the SSA_NAME's associated DECL. We only want to
383 coalesce if they have the same DECL or both have no associated DECL. */
384 tree var1 = SSA_NAME_VAR (name1);
385 tree var2 = SSA_NAME_VAR (name2);
386 var1 = (var1 && (!VAR_P (var1) || !DECL_IGNORED_P (var1))) ? var1 : NULL_TREE;
387 var2 = (var2 && (!VAR_P (var2) || !DECL_IGNORED_P (var2))) ? var2 : NULL_TREE;
388 if (var1 != var2)
389 return false;
391 /* Now check the types. If the types are the same, then we should
392 try to coalesce V1 and V2. */
393 tree t1 = TREE_TYPE (name1);
394 tree t2 = TREE_TYPE (name2);
395 if (t1 == t2)
396 return true;
398 /* If the types are not the same, check for a canonical type match. This
399 (for example) allows coalescing when the types are fundamentally the
400 same, but just have different names.
402 Note pointer types with different address spaces may have the same
403 canonical type. Those are rejected for coalescing by the
404 types_compatible_p check. */
405 if (TYPE_CANONICAL (t1)
406 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)
407 && types_compatible_p (t1, t2))
408 return true;
410 return false;
413 /* Strip off a legitimate source ending from the input string NAME of
414 length LEN. Rather than having to know the names used by all of
415 our front ends, we strip off an ending of a period followed by
416 up to five characters. (Java uses ".class".) */
418 static inline void
419 remove_suffix (char *name, int len)
421 int i;
423 for (i = 2; i < 8 && len > i; i++)
425 if (name[len - i] == '.')
427 name[len - i] = '\0';
428 break;
433 /* Create a new temporary name with PREFIX. Return an identifier. */
435 static GTY(()) unsigned int tmp_var_id_num;
437 tree
438 create_tmp_var_name (const char *prefix)
440 char *tmp_name;
442 if (prefix)
444 char *preftmp = ASTRDUP (prefix);
446 remove_suffix (preftmp, strlen (preftmp));
447 clean_symbol_name (preftmp);
449 prefix = preftmp;
452 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
453 return get_identifier (tmp_name);
456 /* Create a new temporary variable declaration of type TYPE.
457 Do NOT push it into the current binding. */
459 tree
460 create_tmp_var_raw (tree type, const char *prefix)
462 tree tmp_var;
464 tmp_var = build_decl (input_location,
465 VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
466 type);
468 /* The variable was declared by the compiler. */
469 DECL_ARTIFICIAL (tmp_var) = 1;
470 /* And we don't want debug info for it. */
471 DECL_IGNORED_P (tmp_var) = 1;
473 /* Make the variable writable. */
474 TREE_READONLY (tmp_var) = 0;
476 DECL_EXTERNAL (tmp_var) = 0;
477 TREE_STATIC (tmp_var) = 0;
478 TREE_USED (tmp_var) = 1;
480 return tmp_var;
483 /* Create a new temporary variable declaration of type TYPE. DO push the
484 variable into the current binding. Further, assume that this is called
485 only from gimplification or optimization, at which point the creation of
486 certain types are bugs. */
488 tree
489 create_tmp_var (tree type, const char *prefix)
491 tree tmp_var;
493 /* We don't allow types that are addressable (meaning we can't make copies),
494 or incomplete. We also used to reject every variable size objects here,
495 but now support those for which a constant upper bound can be obtained.
496 The processing for variable sizes is performed in gimple_add_tmp_var,
497 point at which it really matters and possibly reached via paths not going
498 through this function, e.g. after direct calls to create_tmp_var_raw. */
499 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
501 tmp_var = create_tmp_var_raw (type, prefix);
502 gimple_add_tmp_var (tmp_var);
503 return tmp_var;
506 /* Create a new temporary variable declaration of type TYPE by calling
507 create_tmp_var and if TYPE is a vector or a complex number, mark the new
508 temporary as gimple register. */
510 tree
511 create_tmp_reg (tree type, const char *prefix)
513 tree tmp;
515 tmp = create_tmp_var (type, prefix);
516 if (TREE_CODE (type) == COMPLEX_TYPE
517 || TREE_CODE (type) == VECTOR_TYPE)
518 DECL_GIMPLE_REG_P (tmp) = 1;
520 return tmp;
524 /* ----- Expression related ----- */
526 /* Extract the operands and code for expression EXPR into *SUBCODE_P,
527 *OP1_P, *OP2_P and *OP3_P respectively. */
529 void
530 extract_ops_from_tree_1 (tree expr, enum tree_code *subcode_p, tree *op1_p,
531 tree *op2_p, tree *op3_p)
533 enum gimple_rhs_class grhs_class;
535 *subcode_p = TREE_CODE (expr);
536 grhs_class = get_gimple_rhs_class (*subcode_p);
538 if (grhs_class == GIMPLE_TERNARY_RHS)
540 *op1_p = TREE_OPERAND (expr, 0);
541 *op2_p = TREE_OPERAND (expr, 1);
542 *op3_p = TREE_OPERAND (expr, 2);
544 else if (grhs_class == GIMPLE_BINARY_RHS)
546 *op1_p = TREE_OPERAND (expr, 0);
547 *op2_p = TREE_OPERAND (expr, 1);
548 *op3_p = NULL_TREE;
550 else if (grhs_class == GIMPLE_UNARY_RHS)
552 *op1_p = TREE_OPERAND (expr, 0);
553 *op2_p = NULL_TREE;
554 *op3_p = NULL_TREE;
556 else if (grhs_class == GIMPLE_SINGLE_RHS)
558 *op1_p = expr;
559 *op2_p = NULL_TREE;
560 *op3_p = NULL_TREE;
562 else
563 gcc_unreachable ();
566 /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
568 void
569 gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
570 tree *lhs_p, tree *rhs_p)
572 gcc_assert (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison
573 || TREE_CODE (cond) == TRUTH_NOT_EXPR
574 || is_gimple_min_invariant (cond)
575 || SSA_VAR_P (cond));
577 extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
579 /* Canonicalize conditionals of the form 'if (!VAL)'. */
580 if (*code_p == TRUTH_NOT_EXPR)
582 *code_p = EQ_EXPR;
583 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
584 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
586 /* Canonicalize conditionals of the form 'if (VAL)' */
587 else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
589 *code_p = NE_EXPR;
590 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
591 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
595 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
597 bool
598 is_gimple_lvalue (tree t)
600 return (is_gimple_addressable (t)
601 || TREE_CODE (t) == WITH_SIZE_EXPR
602 /* These are complex lvalues, but don't have addresses, so they
603 go here. */
604 || TREE_CODE (t) == BIT_FIELD_REF);
607 /* Return true if T is a GIMPLE condition. */
609 bool
610 is_gimple_condexpr (tree t)
612 return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
613 && !tree_could_throw_p (t)
614 && is_gimple_val (TREE_OPERAND (t, 0))
615 && is_gimple_val (TREE_OPERAND (t, 1))));
618 /* Return true if T is a gimple address. */
620 bool
621 is_gimple_address (const_tree t)
623 tree op;
625 if (TREE_CODE (t) != ADDR_EXPR)
626 return false;
628 op = TREE_OPERAND (t, 0);
629 while (handled_component_p (op))
631 if ((TREE_CODE (op) == ARRAY_REF
632 || TREE_CODE (op) == ARRAY_RANGE_REF)
633 && !is_gimple_val (TREE_OPERAND (op, 1)))
634 return false;
636 op = TREE_OPERAND (op, 0);
639 if (CONSTANT_CLASS_P (op) || TREE_CODE (op) == MEM_REF)
640 return true;
642 switch (TREE_CODE (op))
644 case PARM_DECL:
645 case RESULT_DECL:
646 case LABEL_DECL:
647 case FUNCTION_DECL:
648 case VAR_DECL:
649 case CONST_DECL:
650 return true;
652 default:
653 return false;
657 /* Return true if T is a gimple invariant address. */
659 bool
660 is_gimple_invariant_address (const_tree t)
662 const_tree op;
664 if (TREE_CODE (t) != ADDR_EXPR)
665 return false;
667 op = strip_invariant_refs (TREE_OPERAND (t, 0));
668 if (!op)
669 return false;
671 if (TREE_CODE (op) == MEM_REF)
673 const_tree op0 = TREE_OPERAND (op, 0);
674 return (TREE_CODE (op0) == ADDR_EXPR
675 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
676 || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
679 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
682 /* Return true if T is a gimple invariant address at IPA level
683 (so addresses of variables on stack are not allowed). */
685 bool
686 is_gimple_ip_invariant_address (const_tree t)
688 const_tree op;
690 if (TREE_CODE (t) != ADDR_EXPR)
691 return false;
693 op = strip_invariant_refs (TREE_OPERAND (t, 0));
694 if (!op)
695 return false;
697 if (TREE_CODE (op) == MEM_REF)
699 const_tree op0 = TREE_OPERAND (op, 0);
700 return (TREE_CODE (op0) == ADDR_EXPR
701 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
702 || decl_address_ip_invariant_p (TREE_OPERAND (op0, 0))));
705 return CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op);
708 /* Return true if T is a GIMPLE minimal invariant. It's a restricted
709 form of function invariant. */
711 bool
712 is_gimple_min_invariant (const_tree t)
714 if (TREE_CODE (t) == ADDR_EXPR)
715 return is_gimple_invariant_address (t);
717 return is_gimple_constant (t);
720 /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
721 form of gimple minimal invariant. */
723 bool
724 is_gimple_ip_invariant (const_tree t)
726 if (TREE_CODE (t) == ADDR_EXPR)
727 return is_gimple_ip_invariant_address (t);
729 return is_gimple_constant (t);
732 /* Return true if T is a non-aggregate register variable. */
734 bool
735 is_gimple_reg (tree t)
737 if (virtual_operand_p (t))
738 return false;
740 if (TREE_CODE (t) == SSA_NAME)
741 return true;
743 if (!is_gimple_variable (t))
744 return false;
746 if (!is_gimple_reg_type (TREE_TYPE (t)))
747 return false;
749 /* A volatile decl is not acceptable because we can't reuse it as
750 needed. We need to copy it into a temp first. */
751 if (TREE_THIS_VOLATILE (t))
752 return false;
754 /* We define "registers" as things that can be renamed as needed,
755 which with our infrastructure does not apply to memory. */
756 if (needs_to_live_in_memory (t))
757 return false;
759 /* Hard register variables are an interesting case. For those that
760 are call-clobbered, we don't know where all the calls are, since
761 we don't (want to) take into account which operations will turn
762 into libcalls at the rtl level. For those that are call-saved,
763 we don't currently model the fact that calls may in fact change
764 global hard registers, nor do we examine ASM_CLOBBERS at the tree
765 level, and so miss variable changes that might imply. All around,
766 it seems safest to not do too much optimization with these at the
767 tree level at all. We'll have to rely on the rtl optimizers to
768 clean this up, as there we've got all the appropriate bits exposed. */
769 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
770 return false;
772 /* Complex and vector values must have been put into SSA-like form.
773 That is, no assignments to the individual components. */
774 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
775 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
776 return DECL_GIMPLE_REG_P (t);
778 return true;
782 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
784 bool
785 is_gimple_val (tree t)
787 /* Make loads from volatiles and memory vars explicit. */
788 if (is_gimple_variable (t)
789 && is_gimple_reg_type (TREE_TYPE (t))
790 && !is_gimple_reg (t))
791 return false;
793 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
796 /* Similarly, but accept hard registers as inputs to asm statements. */
798 bool
799 is_gimple_asm_val (tree t)
801 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
802 return true;
804 return is_gimple_val (t);
807 /* Return true if T is a GIMPLE minimal lvalue. */
809 bool
810 is_gimple_min_lval (tree t)
812 if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
813 return false;
814 return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF);
817 /* Return true if T is a valid function operand of a CALL_EXPR. */
819 bool
820 is_gimple_call_addr (tree t)
822 return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
825 /* Return true if T is a valid address operand of a MEM_REF. */
827 bool
828 is_gimple_mem_ref_addr (tree t)
830 return (is_gimple_reg (t)
831 || TREE_CODE (t) == INTEGER_CST
832 || (TREE_CODE (t) == ADDR_EXPR
833 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
834 || decl_address_invariant_p (TREE_OPERAND (t, 0)))));
837 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
838 form and we don't do any syntax checking. */
840 void
841 mark_addressable (tree x)
843 while (handled_component_p (x))
844 x = TREE_OPERAND (x, 0);
845 if (TREE_CODE (x) == MEM_REF
846 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
847 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
848 if (TREE_CODE (x) != VAR_DECL
849 && TREE_CODE (x) != PARM_DECL
850 && TREE_CODE (x) != RESULT_DECL)
851 return;
852 TREE_ADDRESSABLE (x) = 1;
854 /* Also mark the artificial SSA_NAME that points to the partition of X. */
855 if (TREE_CODE (x) == VAR_DECL
856 && !DECL_EXTERNAL (x)
857 && !TREE_STATIC (x)
858 && cfun->gimple_df != NULL
859 && cfun->gimple_df->decls_to_pointers != NULL)
861 void *namep
862 = pointer_map_contains (cfun->gimple_df->decls_to_pointers, x);
863 if (namep)
864 TREE_ADDRESSABLE (*(tree *)namep) = 1;
868 /* Returns true iff T is a valid RHS for an assignment to a renamed
869 user -- or front-end generated artificial -- variable. */
871 bool
872 is_gimple_reg_rhs (tree t)
874 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
877 #include "gt-gimple-expr.h"