PR libgcc/63832
[official-gcc.git] / gcc / gimple-expr.c
blob856ffd900e4da2b059c0a2e86391fbb5930df9bf
1 /* Gimple decl, type, and expression support functions.
3 Copyright (C) 2007-2014 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 "predict.h"
28 #include "vec.h"
29 #include "hashtab.h"
30 #include "hash-set.h"
31 #include "machmode.h"
32 #include "hard-reg-set.h"
33 #include "input.h"
34 #include "function.h"
35 #include "basic-block.h"
36 #include "tree-ssa-alias.h"
37 #include "internal-fn.h"
38 #include "tree-eh.h"
39 #include "gimple-expr.h"
40 #include "is-a.h"
41 #include "gimple.h"
42 #include "stringpool.h"
43 #include "gimplify.h"
44 #include "stor-layout.h"
45 #include "demangle.h"
46 #include "gimple-ssa.h"
48 /* ----- Type related ----- */
50 /* Return true if the conversion from INNER_TYPE to OUTER_TYPE is a
51 useless type conversion, otherwise return false.
53 This function implicitly defines the middle-end type system. With
54 the notion of 'a < b' meaning that useless_type_conversion_p (a, b)
55 holds and 'a > b' meaning that useless_type_conversion_p (b, a) holds,
56 the following invariants shall be fulfilled:
58 1) useless_type_conversion_p is transitive.
59 If a < b and b < c then a < c.
61 2) useless_type_conversion_p is not symmetric.
62 From a < b does not follow a > b.
64 3) Types define the available set of operations applicable to values.
65 A type conversion is useless if the operations for the target type
66 is a subset of the operations for the source type. For example
67 casts to void* are useless, casts from void* are not (void* can't
68 be dereferenced or offsetted, but copied, hence its set of operations
69 is a strict subset of that of all other data pointer types). Casts
70 to const T* are useless (can't be written to), casts from const T*
71 to T* are not. */
73 bool
74 useless_type_conversion_p (tree outer_type, tree inner_type)
76 /* Do the following before stripping toplevel qualifiers. */
77 if (POINTER_TYPE_P (inner_type)
78 && POINTER_TYPE_P (outer_type))
80 /* Do not lose casts between pointers to different address spaces. */
81 if (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
82 != TYPE_ADDR_SPACE (TREE_TYPE (inner_type)))
83 return false;
86 /* From now on qualifiers on value types do not matter. */
87 inner_type = TYPE_MAIN_VARIANT (inner_type);
88 outer_type = TYPE_MAIN_VARIANT (outer_type);
90 if (inner_type == outer_type)
91 return true;
93 /* If we know the canonical types, compare them. */
94 if (TYPE_CANONICAL (inner_type)
95 && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type))
96 return true;
98 /* Changes in machine mode are never useless conversions unless we
99 deal with aggregate types in which case we defer to later checks. */
100 if (TYPE_MODE (inner_type) != TYPE_MODE (outer_type)
101 && !AGGREGATE_TYPE_P (inner_type))
102 return false;
104 /* If both the inner and outer types are integral types, then the
105 conversion is not necessary if they have the same mode and
106 signedness and precision, and both or neither are boolean. */
107 if (INTEGRAL_TYPE_P (inner_type)
108 && INTEGRAL_TYPE_P (outer_type))
110 /* Preserve changes in signedness or precision. */
111 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
112 || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
113 return false;
115 /* Preserve conversions to/from BOOLEAN_TYPE if types are not
116 of precision one. */
117 if (((TREE_CODE (inner_type) == BOOLEAN_TYPE)
118 != (TREE_CODE (outer_type) == BOOLEAN_TYPE))
119 && TYPE_PRECISION (outer_type) != 1)
120 return false;
122 /* We don't need to preserve changes in the types minimum or
123 maximum value in general as these do not generate code
124 unless the types precisions are different. */
125 return true;
128 /* Scalar floating point types with the same mode are compatible. */
129 else if (SCALAR_FLOAT_TYPE_P (inner_type)
130 && SCALAR_FLOAT_TYPE_P (outer_type))
131 return true;
133 /* Fixed point types with the same mode are compatible. */
134 else if (FIXED_POINT_TYPE_P (inner_type)
135 && FIXED_POINT_TYPE_P (outer_type))
136 return true;
138 /* We need to take special care recursing to pointed-to types. */
139 else if (POINTER_TYPE_P (inner_type)
140 && POINTER_TYPE_P (outer_type))
142 /* Do not lose casts to function pointer types. */
143 if ((TREE_CODE (TREE_TYPE (outer_type)) == FUNCTION_TYPE
144 || TREE_CODE (TREE_TYPE (outer_type)) == METHOD_TYPE)
145 && !(TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE
146 || TREE_CODE (TREE_TYPE (inner_type)) == METHOD_TYPE))
147 return false;
149 /* We do not care for const qualification of the pointed-to types
150 as const qualification has no semantic value to the middle-end. */
152 /* Otherwise pointers/references are equivalent. */
153 return true;
156 /* Recurse for complex types. */
157 else if (TREE_CODE (inner_type) == COMPLEX_TYPE
158 && TREE_CODE (outer_type) == COMPLEX_TYPE)
159 return useless_type_conversion_p (TREE_TYPE (outer_type),
160 TREE_TYPE (inner_type));
162 /* Recurse for vector types with the same number of subparts. */
163 else if (TREE_CODE (inner_type) == VECTOR_TYPE
164 && TREE_CODE (outer_type) == VECTOR_TYPE
165 && TYPE_PRECISION (inner_type) == TYPE_PRECISION (outer_type))
166 return useless_type_conversion_p (TREE_TYPE (outer_type),
167 TREE_TYPE (inner_type));
169 else if (TREE_CODE (inner_type) == ARRAY_TYPE
170 && TREE_CODE (outer_type) == ARRAY_TYPE)
172 /* Preserve string attributes. */
173 if (TYPE_STRING_FLAG (inner_type) != TYPE_STRING_FLAG (outer_type))
174 return false;
176 /* Conversions from array types with unknown extent to
177 array types with known extent are not useless. */
178 if (!TYPE_DOMAIN (inner_type)
179 && TYPE_DOMAIN (outer_type))
180 return false;
182 /* Nor are conversions from array types with non-constant size to
183 array types with constant size or to different size. */
184 if (TYPE_SIZE (outer_type)
185 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST
186 && (!TYPE_SIZE (inner_type)
187 || TREE_CODE (TYPE_SIZE (inner_type)) != INTEGER_CST
188 || !tree_int_cst_equal (TYPE_SIZE (outer_type),
189 TYPE_SIZE (inner_type))))
190 return false;
192 /* Check conversions between arrays with partially known extents.
193 If the array min/max values are constant they have to match.
194 Otherwise allow conversions to unknown and variable extents.
195 In particular this declares conversions that may change the
196 mode to BLKmode as useless. */
197 if (TYPE_DOMAIN (inner_type)
198 && TYPE_DOMAIN (outer_type)
199 && TYPE_DOMAIN (inner_type) != TYPE_DOMAIN (outer_type))
201 tree inner_min = TYPE_MIN_VALUE (TYPE_DOMAIN (inner_type));
202 tree outer_min = TYPE_MIN_VALUE (TYPE_DOMAIN (outer_type));
203 tree inner_max = TYPE_MAX_VALUE (TYPE_DOMAIN (inner_type));
204 tree outer_max = TYPE_MAX_VALUE (TYPE_DOMAIN (outer_type));
206 /* After gimplification a variable min/max value carries no
207 additional information compared to a NULL value. All that
208 matters has been lowered to be part of the IL. */
209 if (inner_min && TREE_CODE (inner_min) != INTEGER_CST)
210 inner_min = NULL_TREE;
211 if (outer_min && TREE_CODE (outer_min) != INTEGER_CST)
212 outer_min = NULL_TREE;
213 if (inner_max && TREE_CODE (inner_max) != INTEGER_CST)
214 inner_max = NULL_TREE;
215 if (outer_max && TREE_CODE (outer_max) != INTEGER_CST)
216 outer_max = NULL_TREE;
218 /* Conversions NULL / variable <- cst are useless, but not
219 the other way around. */
220 if (outer_min
221 && (!inner_min
222 || !tree_int_cst_equal (inner_min, outer_min)))
223 return false;
224 if (outer_max
225 && (!inner_max
226 || !tree_int_cst_equal (inner_max, outer_max)))
227 return false;
230 /* Recurse on the element check. */
231 return useless_type_conversion_p (TREE_TYPE (outer_type),
232 TREE_TYPE (inner_type));
235 else if ((TREE_CODE (inner_type) == FUNCTION_TYPE
236 || TREE_CODE (inner_type) == METHOD_TYPE)
237 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
239 tree outer_parm, inner_parm;
241 /* If the return types are not compatible bail out. */
242 if (!useless_type_conversion_p (TREE_TYPE (outer_type),
243 TREE_TYPE (inner_type)))
244 return false;
246 /* Method types should belong to a compatible base class. */
247 if (TREE_CODE (inner_type) == METHOD_TYPE
248 && !useless_type_conversion_p (TYPE_METHOD_BASETYPE (outer_type),
249 TYPE_METHOD_BASETYPE (inner_type)))
250 return false;
252 /* A conversion to an unprototyped argument list is ok. */
253 if (!prototype_p (outer_type))
254 return true;
256 /* If the unqualified argument types are compatible the conversion
257 is useless. */
258 if (TYPE_ARG_TYPES (outer_type) == TYPE_ARG_TYPES (inner_type))
259 return true;
261 for (outer_parm = TYPE_ARG_TYPES (outer_type),
262 inner_parm = TYPE_ARG_TYPES (inner_type);
263 outer_parm && inner_parm;
264 outer_parm = TREE_CHAIN (outer_parm),
265 inner_parm = TREE_CHAIN (inner_parm))
266 if (!useless_type_conversion_p
267 (TYPE_MAIN_VARIANT (TREE_VALUE (outer_parm)),
268 TYPE_MAIN_VARIANT (TREE_VALUE (inner_parm))))
269 return false;
271 /* If there is a mismatch in the number of arguments the functions
272 are not compatible. */
273 if (outer_parm || inner_parm)
274 return false;
276 /* Defer to the target if necessary. */
277 if (TYPE_ATTRIBUTES (inner_type) || TYPE_ATTRIBUTES (outer_type))
278 return comp_type_attributes (outer_type, inner_type) != 0;
280 return true;
283 /* For aggregates we rely on TYPE_CANONICAL exclusively and require
284 explicit conversions for types involving to be structurally
285 compared types. */
286 else if (AGGREGATE_TYPE_P (inner_type)
287 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
288 return false;
290 return false;
294 /* ----- Decl related ----- */
296 /* Set sequence SEQ to be the GIMPLE body for function FN. */
298 void
299 gimple_set_body (tree fndecl, gimple_seq seq)
301 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
302 if (fn == NULL)
304 /* If FNDECL still does not have a function structure associated
305 with it, then it does not make sense for it to receive a
306 GIMPLE body. */
307 gcc_assert (seq == NULL);
309 else
310 fn->gimple_body = seq;
314 /* Return the body of GIMPLE statements for function FN. After the
315 CFG pass, the function body doesn't exist anymore because it has
316 been split up into basic blocks. In this case, it returns
317 NULL. */
319 gimple_seq
320 gimple_body (tree fndecl)
322 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
323 return fn ? fn->gimple_body : NULL;
326 /* Return true when FNDECL has Gimple body either in unlowered
327 or CFG form. */
328 bool
329 gimple_has_body_p (tree fndecl)
331 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
332 return (gimple_body (fndecl) || (fn && fn->cfg));
335 /* Return a printable name for symbol DECL. */
337 const char *
338 gimple_decl_printable_name (tree decl, int verbosity)
340 if (!DECL_NAME (decl))
341 return NULL;
343 if (DECL_ASSEMBLER_NAME_SET_P (decl))
345 const char *str, *mangled_str;
346 int dmgl_opts = DMGL_NO_OPTS;
348 if (verbosity >= 2)
350 dmgl_opts = DMGL_VERBOSE
351 | DMGL_ANSI
352 | DMGL_GNU_V3
353 | DMGL_RET_POSTFIX;
354 if (TREE_CODE (decl) == FUNCTION_DECL)
355 dmgl_opts |= DMGL_PARAMS;
358 mangled_str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
359 str = cplus_demangle_v3 (mangled_str, dmgl_opts);
360 return (str) ? str : mangled_str;
363 return IDENTIFIER_POINTER (DECL_NAME (decl));
367 /* Create a new VAR_DECL and copy information from VAR to it. */
369 tree
370 copy_var_decl (tree var, tree name, tree type)
372 tree copy = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, name, type);
374 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (var);
375 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (var);
376 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (var);
377 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (var);
378 DECL_IGNORED_P (copy) = DECL_IGNORED_P (var);
379 DECL_CONTEXT (copy) = DECL_CONTEXT (var);
380 TREE_NO_WARNING (copy) = TREE_NO_WARNING (var);
381 TREE_USED (copy) = 1;
382 DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
383 DECL_ATTRIBUTES (copy) = DECL_ATTRIBUTES (var);
385 return copy;
388 /* Given SSA_NAMEs NAME1 and NAME2, return true if they are candidates for
389 coalescing together, false otherwise.
391 This must stay consistent with var_map_base_init in tree-ssa-live.c. */
393 bool
394 gimple_can_coalesce_p (tree name1, tree name2)
396 /* First check the SSA_NAME's associated DECL. We only want to
397 coalesce if they have the same DECL or both have no associated DECL. */
398 tree var1 = SSA_NAME_VAR (name1);
399 tree var2 = SSA_NAME_VAR (name2);
400 var1 = (var1 && (!VAR_P (var1) || !DECL_IGNORED_P (var1))) ? var1 : NULL_TREE;
401 var2 = (var2 && (!VAR_P (var2) || !DECL_IGNORED_P (var2))) ? var2 : NULL_TREE;
402 if (var1 != var2)
403 return false;
405 /* Now check the types. If the types are the same, then we should
406 try to coalesce V1 and V2. */
407 tree t1 = TREE_TYPE (name1);
408 tree t2 = TREE_TYPE (name2);
409 if (t1 == t2)
410 return true;
412 /* If the types are not the same, check for a canonical type match. This
413 (for example) allows coalescing when the types are fundamentally the
414 same, but just have different names.
416 Note pointer types with different address spaces may have the same
417 canonical type. Those are rejected for coalescing by the
418 types_compatible_p check. */
419 if (TYPE_CANONICAL (t1)
420 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)
421 && types_compatible_p (t1, t2))
422 return true;
424 return false;
427 /* Strip off a legitimate source ending from the input string NAME of
428 length LEN. Rather than having to know the names used by all of
429 our front ends, we strip off an ending of a period followed by
430 up to five characters. (Java uses ".class".) */
432 static inline void
433 remove_suffix (char *name, int len)
435 int i;
437 for (i = 2; i < 8 && len > i; i++)
439 if (name[len - i] == '.')
441 name[len - i] = '\0';
442 break;
447 /* Create a new temporary name with PREFIX. Return an identifier. */
449 static GTY(()) unsigned int tmp_var_id_num;
451 tree
452 create_tmp_var_name (const char *prefix)
454 char *tmp_name;
456 if (prefix)
458 char *preftmp = ASTRDUP (prefix);
460 remove_suffix (preftmp, strlen (preftmp));
461 clean_symbol_name (preftmp);
463 prefix = preftmp;
466 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
467 return get_identifier (tmp_name);
470 /* Create a new temporary variable declaration of type TYPE.
471 Do NOT push it into the current binding. */
473 tree
474 create_tmp_var_raw (tree type, const char *prefix)
476 tree tmp_var;
478 tmp_var = build_decl (input_location,
479 VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
480 type);
482 /* The variable was declared by the compiler. */
483 DECL_ARTIFICIAL (tmp_var) = 1;
484 /* And we don't want debug info for it. */
485 DECL_IGNORED_P (tmp_var) = 1;
487 /* Make the variable writable. */
488 TREE_READONLY (tmp_var) = 0;
490 DECL_EXTERNAL (tmp_var) = 0;
491 TREE_STATIC (tmp_var) = 0;
492 TREE_USED (tmp_var) = 1;
494 return tmp_var;
497 /* Create a new temporary variable declaration of type TYPE. DO push the
498 variable into the current binding. Further, assume that this is called
499 only from gimplification or optimization, at which point the creation of
500 certain types are bugs. */
502 tree
503 create_tmp_var (tree type, const char *prefix)
505 tree tmp_var;
507 /* We don't allow types that are addressable (meaning we can't make copies),
508 or incomplete. We also used to reject every variable size objects here,
509 but now support those for which a constant upper bound can be obtained.
510 The processing for variable sizes is performed in gimple_add_tmp_var,
511 point at which it really matters and possibly reached via paths not going
512 through this function, e.g. after direct calls to create_tmp_var_raw. */
513 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
515 tmp_var = create_tmp_var_raw (type, prefix);
516 gimple_add_tmp_var (tmp_var);
517 return tmp_var;
520 /* Create a new temporary variable declaration of type TYPE by calling
521 create_tmp_var and if TYPE is a vector or a complex number, mark the new
522 temporary as gimple register. */
524 tree
525 create_tmp_reg (tree type, const char *prefix)
527 tree tmp;
529 tmp = create_tmp_var (type, prefix);
530 if (TREE_CODE (type) == COMPLEX_TYPE
531 || TREE_CODE (type) == VECTOR_TYPE)
532 DECL_GIMPLE_REG_P (tmp) = 1;
534 return tmp;
537 /* Create a new temporary variable declaration of type TYPE by calling
538 create_tmp_var and if TYPE is a vector or a complex number, mark the new
539 temporary as gimple register. */
541 tree
542 create_tmp_reg_fn (struct function *fn, tree type, const char *prefix)
544 tree tmp;
546 tmp = create_tmp_var_raw (type, prefix);
547 gimple_add_tmp_var_fn (fn, tmp);
548 if (TREE_CODE (type) == COMPLEX_TYPE
549 || TREE_CODE (type) == VECTOR_TYPE)
550 DECL_GIMPLE_REG_P (tmp) = 1;
552 return tmp;
556 /* ----- Expression related ----- */
558 /* Extract the operands and code for expression EXPR into *SUBCODE_P,
559 *OP1_P, *OP2_P and *OP3_P respectively. */
561 void
562 extract_ops_from_tree_1 (tree expr, enum tree_code *subcode_p, tree *op1_p,
563 tree *op2_p, tree *op3_p)
565 enum gimple_rhs_class grhs_class;
567 *subcode_p = TREE_CODE (expr);
568 grhs_class = get_gimple_rhs_class (*subcode_p);
570 if (grhs_class == GIMPLE_TERNARY_RHS)
572 *op1_p = TREE_OPERAND (expr, 0);
573 *op2_p = TREE_OPERAND (expr, 1);
574 *op3_p = TREE_OPERAND (expr, 2);
576 else if (grhs_class == GIMPLE_BINARY_RHS)
578 *op1_p = TREE_OPERAND (expr, 0);
579 *op2_p = TREE_OPERAND (expr, 1);
580 *op3_p = NULL_TREE;
582 else if (grhs_class == GIMPLE_UNARY_RHS)
584 *op1_p = TREE_OPERAND (expr, 0);
585 *op2_p = NULL_TREE;
586 *op3_p = NULL_TREE;
588 else if (grhs_class == GIMPLE_SINGLE_RHS)
590 *op1_p = expr;
591 *op2_p = NULL_TREE;
592 *op3_p = NULL_TREE;
594 else
595 gcc_unreachable ();
598 /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
600 void
601 gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
602 tree *lhs_p, tree *rhs_p)
604 gcc_assert (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison
605 || TREE_CODE (cond) == TRUTH_NOT_EXPR
606 || is_gimple_min_invariant (cond)
607 || SSA_VAR_P (cond));
609 extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
611 /* Canonicalize conditionals of the form 'if (!VAL)'. */
612 if (*code_p == TRUTH_NOT_EXPR)
614 *code_p = EQ_EXPR;
615 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
616 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
618 /* Canonicalize conditionals of the form 'if (VAL)' */
619 else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
621 *code_p = NE_EXPR;
622 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
623 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
627 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
629 bool
630 is_gimple_lvalue (tree t)
632 return (is_gimple_addressable (t)
633 || TREE_CODE (t) == WITH_SIZE_EXPR
634 /* These are complex lvalues, but don't have addresses, so they
635 go here. */
636 || TREE_CODE (t) == BIT_FIELD_REF);
639 /* Return true if T is a GIMPLE condition. */
641 bool
642 is_gimple_condexpr (tree t)
644 return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
645 && !tree_could_throw_p (t)
646 && is_gimple_val (TREE_OPERAND (t, 0))
647 && is_gimple_val (TREE_OPERAND (t, 1))));
650 /* Return true if T is a gimple address. */
652 bool
653 is_gimple_address (const_tree t)
655 tree op;
657 if (TREE_CODE (t) != ADDR_EXPR)
658 return false;
660 op = TREE_OPERAND (t, 0);
661 while (handled_component_p (op))
663 if ((TREE_CODE (op) == ARRAY_REF
664 || TREE_CODE (op) == ARRAY_RANGE_REF)
665 && !is_gimple_val (TREE_OPERAND (op, 1)))
666 return false;
668 op = TREE_OPERAND (op, 0);
671 if (CONSTANT_CLASS_P (op) || TREE_CODE (op) == MEM_REF)
672 return true;
674 switch (TREE_CODE (op))
676 case PARM_DECL:
677 case RESULT_DECL:
678 case LABEL_DECL:
679 case FUNCTION_DECL:
680 case VAR_DECL:
681 case CONST_DECL:
682 return true;
684 default:
685 return false;
689 /* Return true if T is a gimple invariant address. */
691 bool
692 is_gimple_invariant_address (const_tree t)
694 const_tree op;
696 if (TREE_CODE (t) != ADDR_EXPR)
697 return false;
699 op = strip_invariant_refs (TREE_OPERAND (t, 0));
700 if (!op)
701 return false;
703 if (TREE_CODE (op) == MEM_REF)
705 const_tree op0 = TREE_OPERAND (op, 0);
706 return (TREE_CODE (op0) == ADDR_EXPR
707 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
708 || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
711 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
714 /* Return true if T is a gimple invariant address at IPA level
715 (so addresses of variables on stack are not allowed). */
717 bool
718 is_gimple_ip_invariant_address (const_tree t)
720 const_tree op;
722 if (TREE_CODE (t) != ADDR_EXPR)
723 return false;
725 op = strip_invariant_refs (TREE_OPERAND (t, 0));
726 if (!op)
727 return false;
729 if (TREE_CODE (op) == MEM_REF)
731 const_tree op0 = TREE_OPERAND (op, 0);
732 return (TREE_CODE (op0) == ADDR_EXPR
733 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
734 || decl_address_ip_invariant_p (TREE_OPERAND (op0, 0))));
737 return CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op);
740 /* Return true if T is a GIMPLE minimal invariant. It's a restricted
741 form of function invariant. */
743 bool
744 is_gimple_min_invariant (const_tree t)
746 if (TREE_CODE (t) == ADDR_EXPR)
747 return is_gimple_invariant_address (t);
749 return is_gimple_constant (t);
752 /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
753 form of gimple minimal invariant. */
755 bool
756 is_gimple_ip_invariant (const_tree t)
758 if (TREE_CODE (t) == ADDR_EXPR)
759 return is_gimple_ip_invariant_address (t);
761 return is_gimple_constant (t);
764 /* Return true if T is a non-aggregate register variable. */
766 bool
767 is_gimple_reg (tree t)
769 if (virtual_operand_p (t))
770 return false;
772 if (TREE_CODE (t) == SSA_NAME)
773 return true;
775 if (!is_gimple_variable (t))
776 return false;
778 if (!is_gimple_reg_type (TREE_TYPE (t)))
779 return false;
781 /* A volatile decl is not acceptable because we can't reuse it as
782 needed. We need to copy it into a temp first. */
783 if (TREE_THIS_VOLATILE (t))
784 return false;
786 /* We define "registers" as things that can be renamed as needed,
787 which with our infrastructure does not apply to memory. */
788 if (needs_to_live_in_memory (t))
789 return false;
791 /* Hard register variables are an interesting case. For those that
792 are call-clobbered, we don't know where all the calls are, since
793 we don't (want to) take into account which operations will turn
794 into libcalls at the rtl level. For those that are call-saved,
795 we don't currently model the fact that calls may in fact change
796 global hard registers, nor do we examine ASM_CLOBBERS at the tree
797 level, and so miss variable changes that might imply. All around,
798 it seems safest to not do too much optimization with these at the
799 tree level at all. We'll have to rely on the rtl optimizers to
800 clean this up, as there we've got all the appropriate bits exposed. */
801 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
802 return false;
804 /* Complex and vector values must have been put into SSA-like form.
805 That is, no assignments to the individual components. */
806 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
807 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
808 return DECL_GIMPLE_REG_P (t);
810 return true;
814 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
816 bool
817 is_gimple_val (tree t)
819 /* Make loads from volatiles and memory vars explicit. */
820 if (is_gimple_variable (t)
821 && is_gimple_reg_type (TREE_TYPE (t))
822 && !is_gimple_reg (t))
823 return false;
825 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
828 /* Similarly, but accept hard registers as inputs to asm statements. */
830 bool
831 is_gimple_asm_val (tree t)
833 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
834 return true;
836 return is_gimple_val (t);
839 /* Return true if T is a GIMPLE minimal lvalue. */
841 bool
842 is_gimple_min_lval (tree t)
844 if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
845 return false;
846 return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF);
849 /* Return true if T is a valid function operand of a CALL_EXPR. */
851 bool
852 is_gimple_call_addr (tree t)
854 return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
857 /* Return true if T is a valid address operand of a MEM_REF. */
859 bool
860 is_gimple_mem_ref_addr (tree t)
862 return (is_gimple_reg (t)
863 || TREE_CODE (t) == INTEGER_CST
864 || (TREE_CODE (t) == ADDR_EXPR
865 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
866 || decl_address_invariant_p (TREE_OPERAND (t, 0)))));
869 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
870 form and we don't do any syntax checking. */
872 void
873 mark_addressable (tree x)
875 while (handled_component_p (x))
876 x = TREE_OPERAND (x, 0);
877 if (TREE_CODE (x) == MEM_REF
878 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
879 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
880 if (TREE_CODE (x) != VAR_DECL
881 && TREE_CODE (x) != PARM_DECL
882 && TREE_CODE (x) != RESULT_DECL)
883 return;
884 TREE_ADDRESSABLE (x) = 1;
886 /* Also mark the artificial SSA_NAME that points to the partition of X. */
887 if (TREE_CODE (x) == VAR_DECL
888 && !DECL_EXTERNAL (x)
889 && !TREE_STATIC (x)
890 && cfun->gimple_df != NULL
891 && cfun->gimple_df->decls_to_pointers != NULL)
893 tree *namep = cfun->gimple_df->decls_to_pointers->get (x);
894 if (namep)
895 TREE_ADDRESSABLE (*namep) = 1;
899 /* Returns true iff T is a valid RHS for an assignment to a renamed
900 user -- or front-end generated artificial -- variable. */
902 bool
903 is_gimple_reg_rhs (tree t)
905 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
908 #include "gt-gimple-expr.h"