PR rtl-optimization/57003
[official-gcc.git] / gcc / gimple-expr.c
blob0e8a01ac87722dcac34ad61504b0e8a93b9d0212
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 "basic-block.h"
28 #include "tree-ssa-alias.h"
29 #include "internal-fn.h"
30 #include "tree-eh.h"
31 #include "gimple-expr.h"
32 #include "is-a.h"
33 #include "gimple.h"
34 #include "stringpool.h"
35 #include "gimplify.h"
36 #include "stor-layout.h"
37 #include "demangle.h"
38 #include "gimple-ssa.h"
40 /* ----- Type related ----- */
42 /* Return true if the conversion from INNER_TYPE to OUTER_TYPE is a
43 useless type conversion, otherwise return false.
45 This function implicitly defines the middle-end type system. With
46 the notion of 'a < b' meaning that useless_type_conversion_p (a, b)
47 holds and 'a > b' meaning that useless_type_conversion_p (b, a) holds,
48 the following invariants shall be fulfilled:
50 1) useless_type_conversion_p is transitive.
51 If a < b and b < c then a < c.
53 2) useless_type_conversion_p is not symmetric.
54 From a < b does not follow a > b.
56 3) Types define the available set of operations applicable to values.
57 A type conversion is useless if the operations for the target type
58 is a subset of the operations for the source type. For example
59 casts to void* are useless, casts from void* are not (void* can't
60 be dereferenced or offsetted, but copied, hence its set of operations
61 is a strict subset of that of all other data pointer types). Casts
62 to const T* are useless (can't be written to), casts from const T*
63 to T* are not. */
65 bool
66 useless_type_conversion_p (tree outer_type, tree inner_type)
68 /* Do the following before stripping toplevel qualifiers. */
69 if (POINTER_TYPE_P (inner_type)
70 && POINTER_TYPE_P (outer_type))
72 /* Do not lose casts between pointers to different address spaces. */
73 if (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
74 != TYPE_ADDR_SPACE (TREE_TYPE (inner_type)))
75 return false;
78 /* From now on qualifiers on value types do not matter. */
79 inner_type = TYPE_MAIN_VARIANT (inner_type);
80 outer_type = TYPE_MAIN_VARIANT (outer_type);
82 if (inner_type == outer_type)
83 return true;
85 /* If we know the canonical types, compare them. */
86 if (TYPE_CANONICAL (inner_type)
87 && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type))
88 return true;
90 /* Changes in machine mode are never useless conversions unless we
91 deal with aggregate types in which case we defer to later checks. */
92 if (TYPE_MODE (inner_type) != TYPE_MODE (outer_type)
93 && !AGGREGATE_TYPE_P (inner_type))
94 return false;
96 /* If both the inner and outer types are integral types, then the
97 conversion is not necessary if they have the same mode and
98 signedness and precision, and both or neither are boolean. */
99 if (INTEGRAL_TYPE_P (inner_type)
100 && INTEGRAL_TYPE_P (outer_type))
102 /* Preserve changes in signedness or precision. */
103 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
104 || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
105 return false;
107 /* Preserve conversions to/from BOOLEAN_TYPE if types are not
108 of precision one. */
109 if (((TREE_CODE (inner_type) == BOOLEAN_TYPE)
110 != (TREE_CODE (outer_type) == BOOLEAN_TYPE))
111 && TYPE_PRECISION (outer_type) != 1)
112 return false;
114 /* We don't need to preserve changes in the types minimum or
115 maximum value in general as these do not generate code
116 unless the types precisions are different. */
117 return true;
120 /* Scalar floating point types with the same mode are compatible. */
121 else if (SCALAR_FLOAT_TYPE_P (inner_type)
122 && SCALAR_FLOAT_TYPE_P (outer_type))
123 return true;
125 /* Fixed point types with the same mode are compatible. */
126 else if (FIXED_POINT_TYPE_P (inner_type)
127 && FIXED_POINT_TYPE_P (outer_type))
128 return true;
130 /* We need to take special care recursing to pointed-to types. */
131 else if (POINTER_TYPE_P (inner_type)
132 && POINTER_TYPE_P (outer_type))
134 /* Do not lose casts to function pointer types. */
135 if ((TREE_CODE (TREE_TYPE (outer_type)) == FUNCTION_TYPE
136 || TREE_CODE (TREE_TYPE (outer_type)) == METHOD_TYPE)
137 && !(TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE
138 || TREE_CODE (TREE_TYPE (inner_type)) == METHOD_TYPE))
139 return false;
141 /* We do not care for const qualification of the pointed-to types
142 as const qualification has no semantic value to the middle-end. */
144 /* Otherwise pointers/references are equivalent. */
145 return true;
148 /* Recurse for complex types. */
149 else if (TREE_CODE (inner_type) == COMPLEX_TYPE
150 && TREE_CODE (outer_type) == COMPLEX_TYPE)
151 return useless_type_conversion_p (TREE_TYPE (outer_type),
152 TREE_TYPE (inner_type));
154 /* Recurse for vector types with the same number of subparts. */
155 else if (TREE_CODE (inner_type) == VECTOR_TYPE
156 && TREE_CODE (outer_type) == VECTOR_TYPE
157 && TYPE_PRECISION (inner_type) == TYPE_PRECISION (outer_type))
158 return useless_type_conversion_p (TREE_TYPE (outer_type),
159 TREE_TYPE (inner_type));
161 else if (TREE_CODE (inner_type) == ARRAY_TYPE
162 && TREE_CODE (outer_type) == ARRAY_TYPE)
164 /* Preserve string attributes. */
165 if (TYPE_STRING_FLAG (inner_type) != TYPE_STRING_FLAG (outer_type))
166 return false;
168 /* Conversions from array types with unknown extent to
169 array types with known extent are not useless. */
170 if (!TYPE_DOMAIN (inner_type)
171 && TYPE_DOMAIN (outer_type))
172 return false;
174 /* Nor are conversions from array types with non-constant size to
175 array types with constant size or to different size. */
176 if (TYPE_SIZE (outer_type)
177 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST
178 && (!TYPE_SIZE (inner_type)
179 || TREE_CODE (TYPE_SIZE (inner_type)) != INTEGER_CST
180 || !tree_int_cst_equal (TYPE_SIZE (outer_type),
181 TYPE_SIZE (inner_type))))
182 return false;
184 /* Check conversions between arrays with partially known extents.
185 If the array min/max values are constant they have to match.
186 Otherwise allow conversions to unknown and variable extents.
187 In particular this declares conversions that may change the
188 mode to BLKmode as useless. */
189 if (TYPE_DOMAIN (inner_type)
190 && TYPE_DOMAIN (outer_type)
191 && TYPE_DOMAIN (inner_type) != TYPE_DOMAIN (outer_type))
193 tree inner_min = TYPE_MIN_VALUE (TYPE_DOMAIN (inner_type));
194 tree outer_min = TYPE_MIN_VALUE (TYPE_DOMAIN (outer_type));
195 tree inner_max = TYPE_MAX_VALUE (TYPE_DOMAIN (inner_type));
196 tree outer_max = TYPE_MAX_VALUE (TYPE_DOMAIN (outer_type));
198 /* After gimplification a variable min/max value carries no
199 additional information compared to a NULL value. All that
200 matters has been lowered to be part of the IL. */
201 if (inner_min && TREE_CODE (inner_min) != INTEGER_CST)
202 inner_min = NULL_TREE;
203 if (outer_min && TREE_CODE (outer_min) != INTEGER_CST)
204 outer_min = NULL_TREE;
205 if (inner_max && TREE_CODE (inner_max) != INTEGER_CST)
206 inner_max = NULL_TREE;
207 if (outer_max && TREE_CODE (outer_max) != INTEGER_CST)
208 outer_max = NULL_TREE;
210 /* Conversions NULL / variable <- cst are useless, but not
211 the other way around. */
212 if (outer_min
213 && (!inner_min
214 || !tree_int_cst_equal (inner_min, outer_min)))
215 return false;
216 if (outer_max
217 && (!inner_max
218 || !tree_int_cst_equal (inner_max, outer_max)))
219 return false;
222 /* Recurse on the element check. */
223 return useless_type_conversion_p (TREE_TYPE (outer_type),
224 TREE_TYPE (inner_type));
227 else if ((TREE_CODE (inner_type) == FUNCTION_TYPE
228 || TREE_CODE (inner_type) == METHOD_TYPE)
229 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
231 tree outer_parm, inner_parm;
233 /* If the return types are not compatible bail out. */
234 if (!useless_type_conversion_p (TREE_TYPE (outer_type),
235 TREE_TYPE (inner_type)))
236 return false;
238 /* Method types should belong to a compatible base class. */
239 if (TREE_CODE (inner_type) == METHOD_TYPE
240 && !useless_type_conversion_p (TYPE_METHOD_BASETYPE (outer_type),
241 TYPE_METHOD_BASETYPE (inner_type)))
242 return false;
244 /* A conversion to an unprototyped argument list is ok. */
245 if (!prototype_p (outer_type))
246 return true;
248 /* If the unqualified argument types are compatible the conversion
249 is useless. */
250 if (TYPE_ARG_TYPES (outer_type) == TYPE_ARG_TYPES (inner_type))
251 return true;
253 for (outer_parm = TYPE_ARG_TYPES (outer_type),
254 inner_parm = TYPE_ARG_TYPES (inner_type);
255 outer_parm && inner_parm;
256 outer_parm = TREE_CHAIN (outer_parm),
257 inner_parm = TREE_CHAIN (inner_parm))
258 if (!useless_type_conversion_p
259 (TYPE_MAIN_VARIANT (TREE_VALUE (outer_parm)),
260 TYPE_MAIN_VARIANT (TREE_VALUE (inner_parm))))
261 return false;
263 /* If there is a mismatch in the number of arguments the functions
264 are not compatible. */
265 if (outer_parm || inner_parm)
266 return false;
268 /* Defer to the target if necessary. */
269 if (TYPE_ATTRIBUTES (inner_type) || TYPE_ATTRIBUTES (outer_type))
270 return comp_type_attributes (outer_type, inner_type) != 0;
272 return true;
275 /* For aggregates we rely on TYPE_CANONICAL exclusively and require
276 explicit conversions for types involving to be structurally
277 compared types. */
278 else if (AGGREGATE_TYPE_P (inner_type)
279 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
280 return false;
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 /* Given SSA_NAMEs NAME1 and NAME2, return true if they are candidates for
381 coalescing together, false otherwise.
383 This must stay consistent with var_map_base_init in tree-ssa-live.c. */
385 bool
386 gimple_can_coalesce_p (tree name1, tree name2)
388 /* First check the SSA_NAME's associated DECL. We only want to
389 coalesce if they have the same DECL or both have no associated DECL. */
390 tree var1 = SSA_NAME_VAR (name1);
391 tree var2 = SSA_NAME_VAR (name2);
392 var1 = (var1 && (!VAR_P (var1) || !DECL_IGNORED_P (var1))) ? var1 : NULL_TREE;
393 var2 = (var2 && (!VAR_P (var2) || !DECL_IGNORED_P (var2))) ? var2 : NULL_TREE;
394 if (var1 != var2)
395 return false;
397 /* Now check the types. If the types are the same, then we should
398 try to coalesce V1 and V2. */
399 tree t1 = TREE_TYPE (name1);
400 tree t2 = TREE_TYPE (name2);
401 if (t1 == t2)
402 return true;
404 /* If the types are not the same, check for a canonical type match. This
405 (for example) allows coalescing when the types are fundamentally the
406 same, but just have different names.
408 Note pointer types with different address spaces may have the same
409 canonical type. Those are rejected for coalescing by the
410 types_compatible_p check. */
411 if (TYPE_CANONICAL (t1)
412 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)
413 && types_compatible_p (t1, t2))
414 return true;
416 return false;
419 /* Strip off a legitimate source ending from the input string NAME of
420 length LEN. Rather than having to know the names used by all of
421 our front ends, we strip off an ending of a period followed by
422 up to five characters. (Java uses ".class".) */
424 static inline void
425 remove_suffix (char *name, int len)
427 int i;
429 for (i = 2; i < 8 && len > i; i++)
431 if (name[len - i] == '.')
433 name[len - i] = '\0';
434 break;
439 /* Create a new temporary name with PREFIX. Return an identifier. */
441 static GTY(()) unsigned int tmp_var_id_num;
443 tree
444 create_tmp_var_name (const char *prefix)
446 char *tmp_name;
448 if (prefix)
450 char *preftmp = ASTRDUP (prefix);
452 remove_suffix (preftmp, strlen (preftmp));
453 clean_symbol_name (preftmp);
455 prefix = preftmp;
458 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
459 return get_identifier (tmp_name);
462 /* Create a new temporary variable declaration of type TYPE.
463 Do NOT push it into the current binding. */
465 tree
466 create_tmp_var_raw (tree type, const char *prefix)
468 tree tmp_var;
470 tmp_var = build_decl (input_location,
471 VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
472 type);
474 /* The variable was declared by the compiler. */
475 DECL_ARTIFICIAL (tmp_var) = 1;
476 /* And we don't want debug info for it. */
477 DECL_IGNORED_P (tmp_var) = 1;
479 /* Make the variable writable. */
480 TREE_READONLY (tmp_var) = 0;
482 DECL_EXTERNAL (tmp_var) = 0;
483 TREE_STATIC (tmp_var) = 0;
484 TREE_USED (tmp_var) = 1;
486 return tmp_var;
489 /* Create a new temporary variable declaration of type TYPE. DO push the
490 variable into the current binding. Further, assume that this is called
491 only from gimplification or optimization, at which point the creation of
492 certain types are bugs. */
494 tree
495 create_tmp_var (tree type, const char *prefix)
497 tree tmp_var;
499 /* We don't allow types that are addressable (meaning we can't make copies),
500 or incomplete. We also used to reject every variable size objects here,
501 but now support those for which a constant upper bound can be obtained.
502 The processing for variable sizes is performed in gimple_add_tmp_var,
503 point at which it really matters and possibly reached via paths not going
504 through this function, e.g. after direct calls to create_tmp_var_raw. */
505 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
507 tmp_var = create_tmp_var_raw (type, prefix);
508 gimple_add_tmp_var (tmp_var);
509 return tmp_var;
512 /* Create a new temporary variable declaration of type TYPE by calling
513 create_tmp_var and if TYPE is a vector or a complex number, mark the new
514 temporary as gimple register. */
516 tree
517 create_tmp_reg (tree type, const char *prefix)
519 tree tmp;
521 tmp = create_tmp_var (type, prefix);
522 if (TREE_CODE (type) == COMPLEX_TYPE
523 || TREE_CODE (type) == VECTOR_TYPE)
524 DECL_GIMPLE_REG_P (tmp) = 1;
526 return tmp;
529 /* Create a new temporary variable declaration of type TYPE by calling
530 create_tmp_var and if TYPE is a vector or a complex number, mark the new
531 temporary as gimple register. */
533 tree
534 create_tmp_reg_fn (struct function *fn, tree type, const char *prefix)
536 tree tmp;
538 tmp = create_tmp_var_raw (type, prefix);
539 gimple_add_tmp_var_fn (fn, tmp);
540 if (TREE_CODE (type) == COMPLEX_TYPE
541 || TREE_CODE (type) == VECTOR_TYPE)
542 DECL_GIMPLE_REG_P (tmp) = 1;
544 return tmp;
548 /* ----- Expression related ----- */
550 /* Extract the operands and code for expression EXPR into *SUBCODE_P,
551 *OP1_P, *OP2_P and *OP3_P respectively. */
553 void
554 extract_ops_from_tree_1 (tree expr, enum tree_code *subcode_p, tree *op1_p,
555 tree *op2_p, tree *op3_p)
557 enum gimple_rhs_class grhs_class;
559 *subcode_p = TREE_CODE (expr);
560 grhs_class = get_gimple_rhs_class (*subcode_p);
562 if (grhs_class == GIMPLE_TERNARY_RHS)
564 *op1_p = TREE_OPERAND (expr, 0);
565 *op2_p = TREE_OPERAND (expr, 1);
566 *op3_p = TREE_OPERAND (expr, 2);
568 else if (grhs_class == GIMPLE_BINARY_RHS)
570 *op1_p = TREE_OPERAND (expr, 0);
571 *op2_p = TREE_OPERAND (expr, 1);
572 *op3_p = NULL_TREE;
574 else if (grhs_class == GIMPLE_UNARY_RHS)
576 *op1_p = TREE_OPERAND (expr, 0);
577 *op2_p = NULL_TREE;
578 *op3_p = NULL_TREE;
580 else if (grhs_class == GIMPLE_SINGLE_RHS)
582 *op1_p = expr;
583 *op2_p = NULL_TREE;
584 *op3_p = NULL_TREE;
586 else
587 gcc_unreachable ();
590 /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
592 void
593 gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
594 tree *lhs_p, tree *rhs_p)
596 gcc_assert (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison
597 || TREE_CODE (cond) == TRUTH_NOT_EXPR
598 || is_gimple_min_invariant (cond)
599 || SSA_VAR_P (cond));
601 extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
603 /* Canonicalize conditionals of the form 'if (!VAL)'. */
604 if (*code_p == TRUTH_NOT_EXPR)
606 *code_p = EQ_EXPR;
607 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
608 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
610 /* Canonicalize conditionals of the form 'if (VAL)' */
611 else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
613 *code_p = NE_EXPR;
614 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
615 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
619 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
621 bool
622 is_gimple_lvalue (tree t)
624 return (is_gimple_addressable (t)
625 || TREE_CODE (t) == WITH_SIZE_EXPR
626 /* These are complex lvalues, but don't have addresses, so they
627 go here. */
628 || TREE_CODE (t) == BIT_FIELD_REF);
631 /* Return true if T is a GIMPLE condition. */
633 bool
634 is_gimple_condexpr (tree t)
636 return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
637 && !tree_could_throw_p (t)
638 && is_gimple_val (TREE_OPERAND (t, 0))
639 && is_gimple_val (TREE_OPERAND (t, 1))));
642 /* Return true if T is a gimple address. */
644 bool
645 is_gimple_address (const_tree t)
647 tree op;
649 if (TREE_CODE (t) != ADDR_EXPR)
650 return false;
652 op = TREE_OPERAND (t, 0);
653 while (handled_component_p (op))
655 if ((TREE_CODE (op) == ARRAY_REF
656 || TREE_CODE (op) == ARRAY_RANGE_REF)
657 && !is_gimple_val (TREE_OPERAND (op, 1)))
658 return false;
660 op = TREE_OPERAND (op, 0);
663 if (CONSTANT_CLASS_P (op) || TREE_CODE (op) == MEM_REF)
664 return true;
666 switch (TREE_CODE (op))
668 case PARM_DECL:
669 case RESULT_DECL:
670 case LABEL_DECL:
671 case FUNCTION_DECL:
672 case VAR_DECL:
673 case CONST_DECL:
674 return true;
676 default:
677 return false;
681 /* Return true if T is a gimple invariant address. */
683 bool
684 is_gimple_invariant_address (const_tree t)
686 const_tree op;
688 if (TREE_CODE (t) != ADDR_EXPR)
689 return false;
691 op = strip_invariant_refs (TREE_OPERAND (t, 0));
692 if (!op)
693 return false;
695 if (TREE_CODE (op) == MEM_REF)
697 const_tree op0 = TREE_OPERAND (op, 0);
698 return (TREE_CODE (op0) == ADDR_EXPR
699 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
700 || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
703 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
706 /* Return true if T is a gimple invariant address at IPA level
707 (so addresses of variables on stack are not allowed). */
709 bool
710 is_gimple_ip_invariant_address (const_tree t)
712 const_tree op;
714 if (TREE_CODE (t) != ADDR_EXPR)
715 return false;
717 op = strip_invariant_refs (TREE_OPERAND (t, 0));
718 if (!op)
719 return false;
721 if (TREE_CODE (op) == MEM_REF)
723 const_tree op0 = TREE_OPERAND (op, 0);
724 return (TREE_CODE (op0) == ADDR_EXPR
725 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
726 || decl_address_ip_invariant_p (TREE_OPERAND (op0, 0))));
729 return CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op);
732 /* Return true if T is a GIMPLE minimal invariant. It's a restricted
733 form of function invariant. */
735 bool
736 is_gimple_min_invariant (const_tree t)
738 if (TREE_CODE (t) == ADDR_EXPR)
739 return is_gimple_invariant_address (t);
741 return is_gimple_constant (t);
744 /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
745 form of gimple minimal invariant. */
747 bool
748 is_gimple_ip_invariant (const_tree t)
750 if (TREE_CODE (t) == ADDR_EXPR)
751 return is_gimple_ip_invariant_address (t);
753 return is_gimple_constant (t);
756 /* Return true if T is a non-aggregate register variable. */
758 bool
759 is_gimple_reg (tree t)
761 if (virtual_operand_p (t))
762 return false;
764 if (TREE_CODE (t) == SSA_NAME)
765 return true;
767 if (!is_gimple_variable (t))
768 return false;
770 if (!is_gimple_reg_type (TREE_TYPE (t)))
771 return false;
773 /* A volatile decl is not acceptable because we can't reuse it as
774 needed. We need to copy it into a temp first. */
775 if (TREE_THIS_VOLATILE (t))
776 return false;
778 /* We define "registers" as things that can be renamed as needed,
779 which with our infrastructure does not apply to memory. */
780 if (needs_to_live_in_memory (t))
781 return false;
783 /* Hard register variables are an interesting case. For those that
784 are call-clobbered, we don't know where all the calls are, since
785 we don't (want to) take into account which operations will turn
786 into libcalls at the rtl level. For those that are call-saved,
787 we don't currently model the fact that calls may in fact change
788 global hard registers, nor do we examine ASM_CLOBBERS at the tree
789 level, and so miss variable changes that might imply. All around,
790 it seems safest to not do too much optimization with these at the
791 tree level at all. We'll have to rely on the rtl optimizers to
792 clean this up, as there we've got all the appropriate bits exposed. */
793 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
794 return false;
796 /* Complex and vector values must have been put into SSA-like form.
797 That is, no assignments to the individual components. */
798 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
799 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
800 return DECL_GIMPLE_REG_P (t);
802 return true;
806 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
808 bool
809 is_gimple_val (tree t)
811 /* Make loads from volatiles and memory vars explicit. */
812 if (is_gimple_variable (t)
813 && is_gimple_reg_type (TREE_TYPE (t))
814 && !is_gimple_reg (t))
815 return false;
817 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
820 /* Similarly, but accept hard registers as inputs to asm statements. */
822 bool
823 is_gimple_asm_val (tree t)
825 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
826 return true;
828 return is_gimple_val (t);
831 /* Return true if T is a GIMPLE minimal lvalue. */
833 bool
834 is_gimple_min_lval (tree t)
836 if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
837 return false;
838 return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF);
841 /* Return true if T is a valid function operand of a CALL_EXPR. */
843 bool
844 is_gimple_call_addr (tree t)
846 return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
849 /* Return true if T is a valid address operand of a MEM_REF. */
851 bool
852 is_gimple_mem_ref_addr (tree t)
854 return (is_gimple_reg (t)
855 || TREE_CODE (t) == INTEGER_CST
856 || (TREE_CODE (t) == ADDR_EXPR
857 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
858 || decl_address_invariant_p (TREE_OPERAND (t, 0)))));
861 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
862 form and we don't do any syntax checking. */
864 void
865 mark_addressable (tree x)
867 while (handled_component_p (x))
868 x = TREE_OPERAND (x, 0);
869 if (TREE_CODE (x) == MEM_REF
870 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
871 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
872 if (TREE_CODE (x) != VAR_DECL
873 && TREE_CODE (x) != PARM_DECL
874 && TREE_CODE (x) != RESULT_DECL)
875 return;
876 TREE_ADDRESSABLE (x) = 1;
878 /* Also mark the artificial SSA_NAME that points to the partition of X. */
879 if (TREE_CODE (x) == VAR_DECL
880 && !DECL_EXTERNAL (x)
881 && !TREE_STATIC (x)
882 && cfun->gimple_df != NULL
883 && cfun->gimple_df->decls_to_pointers != NULL)
885 tree *namep = cfun->gimple_df->decls_to_pointers->get (x);
886 if (namep)
887 TREE_ADDRESSABLE (*namep) = 1;
891 /* Returns true iff T is a valid RHS for an assignment to a renamed
892 user -- or front-end generated artificial -- variable. */
894 bool
895 is_gimple_reg_rhs (tree t)
897 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
900 #include "gt-gimple-expr.h"