* Makefile.in (C_COMMON_OBJS): Depend on c-cilkplus.o.
[official-gcc.git] / gcc / gimple-expr.c
blob9156f952784e9750aee22027744652696cca48f3
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 "gimplify.h"
29 #include "demangle.h"
30 #include "gimple-ssa.h"
32 /* ----- Type related ----- */
34 /* Return true if the conversion from INNER_TYPE to OUTER_TYPE is a
35 useless type conversion, otherwise return false.
37 This function implicitly defines the middle-end type system. With
38 the notion of 'a < b' meaning that useless_type_conversion_p (a, b)
39 holds and 'a > b' meaning that useless_type_conversion_p (b, a) holds,
40 the following invariants shall be fulfilled:
42 1) useless_type_conversion_p is transitive.
43 If a < b and b < c then a < c.
45 2) useless_type_conversion_p is not symmetric.
46 From a < b does not follow a > b.
48 3) Types define the available set of operations applicable to values.
49 A type conversion is useless if the operations for the target type
50 is a subset of the operations for the source type. For example
51 casts to void* are useless, casts from void* are not (void* can't
52 be dereferenced or offsetted, but copied, hence its set of operations
53 is a strict subset of that of all other data pointer types). Casts
54 to const T* are useless (can't be written to), casts from const T*
55 to T* are not. */
57 bool
58 useless_type_conversion_p (tree outer_type, tree inner_type)
60 /* Do the following before stripping toplevel qualifiers. */
61 if (POINTER_TYPE_P (inner_type)
62 && POINTER_TYPE_P (outer_type))
64 /* Do not lose casts between pointers to different address spaces. */
65 if (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
66 != TYPE_ADDR_SPACE (TREE_TYPE (inner_type)))
67 return false;
70 /* From now on qualifiers on value types do not matter. */
71 inner_type = TYPE_MAIN_VARIANT (inner_type);
72 outer_type = TYPE_MAIN_VARIANT (outer_type);
74 if (inner_type == outer_type)
75 return true;
77 /* If we know the canonical types, compare them. */
78 if (TYPE_CANONICAL (inner_type)
79 && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type))
80 return true;
82 /* Changes in machine mode are never useless conversions unless we
83 deal with aggregate types in which case we defer to later checks. */
84 if (TYPE_MODE (inner_type) != TYPE_MODE (outer_type)
85 && !AGGREGATE_TYPE_P (inner_type))
86 return false;
88 /* If both the inner and outer types are integral types, then the
89 conversion is not necessary if they have the same mode and
90 signedness and precision, and both or neither are boolean. */
91 if (INTEGRAL_TYPE_P (inner_type)
92 && INTEGRAL_TYPE_P (outer_type))
94 /* Preserve changes in signedness or precision. */
95 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
96 || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
97 return false;
99 /* Preserve conversions to/from BOOLEAN_TYPE if types are not
100 of precision one. */
101 if (((TREE_CODE (inner_type) == BOOLEAN_TYPE)
102 != (TREE_CODE (outer_type) == BOOLEAN_TYPE))
103 && TYPE_PRECISION (outer_type) != 1)
104 return false;
106 /* We don't need to preserve changes in the types minimum or
107 maximum value in general as these do not generate code
108 unless the types precisions are different. */
109 return true;
112 /* Scalar floating point types with the same mode are compatible. */
113 else if (SCALAR_FLOAT_TYPE_P (inner_type)
114 && SCALAR_FLOAT_TYPE_P (outer_type))
115 return true;
117 /* Fixed point types with the same mode are compatible. */
118 else if (FIXED_POINT_TYPE_P (inner_type)
119 && FIXED_POINT_TYPE_P (outer_type))
120 return true;
122 /* We need to take special care recursing to pointed-to types. */
123 else if (POINTER_TYPE_P (inner_type)
124 && POINTER_TYPE_P (outer_type))
126 /* Do not lose casts to function pointer types. */
127 if ((TREE_CODE (TREE_TYPE (outer_type)) == FUNCTION_TYPE
128 || TREE_CODE (TREE_TYPE (outer_type)) == METHOD_TYPE)
129 && !(TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE
130 || TREE_CODE (TREE_TYPE (inner_type)) == METHOD_TYPE))
131 return false;
133 /* We do not care for const qualification of the pointed-to types
134 as const qualification has no semantic value to the middle-end. */
136 /* Otherwise pointers/references are equivalent. */
137 return true;
140 /* Recurse for complex types. */
141 else if (TREE_CODE (inner_type) == COMPLEX_TYPE
142 && TREE_CODE (outer_type) == COMPLEX_TYPE)
143 return useless_type_conversion_p (TREE_TYPE (outer_type),
144 TREE_TYPE (inner_type));
146 /* Recurse for vector types with the same number of subparts. */
147 else if (TREE_CODE (inner_type) == VECTOR_TYPE
148 && TREE_CODE (outer_type) == VECTOR_TYPE
149 && TYPE_PRECISION (inner_type) == TYPE_PRECISION (outer_type))
150 return useless_type_conversion_p (TREE_TYPE (outer_type),
151 TREE_TYPE (inner_type));
153 else if (TREE_CODE (inner_type) == ARRAY_TYPE
154 && TREE_CODE (outer_type) == ARRAY_TYPE)
156 /* Preserve string attributes. */
157 if (TYPE_STRING_FLAG (inner_type) != TYPE_STRING_FLAG (outer_type))
158 return false;
160 /* Conversions from array types with unknown extent to
161 array types with known extent are not useless. */
162 if (!TYPE_DOMAIN (inner_type)
163 && TYPE_DOMAIN (outer_type))
164 return false;
166 /* Nor are conversions from array types with non-constant size to
167 array types with constant size or to different size. */
168 if (TYPE_SIZE (outer_type)
169 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST
170 && (!TYPE_SIZE (inner_type)
171 || TREE_CODE (TYPE_SIZE (inner_type)) != INTEGER_CST
172 || !tree_int_cst_equal (TYPE_SIZE (outer_type),
173 TYPE_SIZE (inner_type))))
174 return false;
176 /* Check conversions between arrays with partially known extents.
177 If the array min/max values are constant they have to match.
178 Otherwise allow conversions to unknown and variable extents.
179 In particular this declares conversions that may change the
180 mode to BLKmode as useless. */
181 if (TYPE_DOMAIN (inner_type)
182 && TYPE_DOMAIN (outer_type)
183 && TYPE_DOMAIN (inner_type) != TYPE_DOMAIN (outer_type))
185 tree inner_min = TYPE_MIN_VALUE (TYPE_DOMAIN (inner_type));
186 tree outer_min = TYPE_MIN_VALUE (TYPE_DOMAIN (outer_type));
187 tree inner_max = TYPE_MAX_VALUE (TYPE_DOMAIN (inner_type));
188 tree outer_max = TYPE_MAX_VALUE (TYPE_DOMAIN (outer_type));
190 /* After gimplification a variable min/max value carries no
191 additional information compared to a NULL value. All that
192 matters has been lowered to be part of the IL. */
193 if (inner_min && TREE_CODE (inner_min) != INTEGER_CST)
194 inner_min = NULL_TREE;
195 if (outer_min && TREE_CODE (outer_min) != INTEGER_CST)
196 outer_min = NULL_TREE;
197 if (inner_max && TREE_CODE (inner_max) != INTEGER_CST)
198 inner_max = NULL_TREE;
199 if (outer_max && TREE_CODE (outer_max) != INTEGER_CST)
200 outer_max = NULL_TREE;
202 /* Conversions NULL / variable <- cst are useless, but not
203 the other way around. */
204 if (outer_min
205 && (!inner_min
206 || !tree_int_cst_equal (inner_min, outer_min)))
207 return false;
208 if (outer_max
209 && (!inner_max
210 || !tree_int_cst_equal (inner_max, outer_max)))
211 return false;
214 /* Recurse on the element check. */
215 return useless_type_conversion_p (TREE_TYPE (outer_type),
216 TREE_TYPE (inner_type));
219 else if ((TREE_CODE (inner_type) == FUNCTION_TYPE
220 || TREE_CODE (inner_type) == METHOD_TYPE)
221 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
223 tree outer_parm, inner_parm;
225 /* If the return types are not compatible bail out. */
226 if (!useless_type_conversion_p (TREE_TYPE (outer_type),
227 TREE_TYPE (inner_type)))
228 return false;
230 /* Method types should belong to a compatible base class. */
231 if (TREE_CODE (inner_type) == METHOD_TYPE
232 && !useless_type_conversion_p (TYPE_METHOD_BASETYPE (outer_type),
233 TYPE_METHOD_BASETYPE (inner_type)))
234 return false;
236 /* A conversion to an unprototyped argument list is ok. */
237 if (!prototype_p (outer_type))
238 return true;
240 /* If the unqualified argument types are compatible the conversion
241 is useless. */
242 if (TYPE_ARG_TYPES (outer_type) == TYPE_ARG_TYPES (inner_type))
243 return true;
245 for (outer_parm = TYPE_ARG_TYPES (outer_type),
246 inner_parm = TYPE_ARG_TYPES (inner_type);
247 outer_parm && inner_parm;
248 outer_parm = TREE_CHAIN (outer_parm),
249 inner_parm = TREE_CHAIN (inner_parm))
250 if (!useless_type_conversion_p
251 (TYPE_MAIN_VARIANT (TREE_VALUE (outer_parm)),
252 TYPE_MAIN_VARIANT (TREE_VALUE (inner_parm))))
253 return false;
255 /* If there is a mismatch in the number of arguments the functions
256 are not compatible. */
257 if (outer_parm || inner_parm)
258 return false;
260 /* Defer to the target if necessary. */
261 if (TYPE_ATTRIBUTES (inner_type) || TYPE_ATTRIBUTES (outer_type))
262 return comp_type_attributes (outer_type, inner_type) != 0;
264 return true;
267 /* For aggregates we rely on TYPE_CANONICAL exclusively and require
268 explicit conversions for types involving to be structurally
269 compared types. */
270 else if (AGGREGATE_TYPE_P (inner_type)
271 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
272 return false;
274 return false;
278 /* ----- Decl related ----- */
280 /* Set sequence SEQ to be the GIMPLE body for function FN. */
282 void
283 gimple_set_body (tree fndecl, gimple_seq seq)
285 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
286 if (fn == NULL)
288 /* If FNDECL still does not have a function structure associated
289 with it, then it does not make sense for it to receive a
290 GIMPLE body. */
291 gcc_assert (seq == NULL);
293 else
294 fn->gimple_body = seq;
298 /* Return the body of GIMPLE statements for function FN. After the
299 CFG pass, the function body doesn't exist anymore because it has
300 been split up into basic blocks. In this case, it returns
301 NULL. */
303 gimple_seq
304 gimple_body (tree fndecl)
306 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
307 return fn ? fn->gimple_body : NULL;
310 /* Return true when FNDECL has Gimple body either in unlowered
311 or CFG form. */
312 bool
313 gimple_has_body_p (tree fndecl)
315 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
316 return (gimple_body (fndecl) || (fn && fn->cfg));
319 /* Return a printable name for symbol DECL. */
321 const char *
322 gimple_decl_printable_name (tree decl, int verbosity)
324 if (!DECL_NAME (decl))
325 return NULL;
327 if (DECL_ASSEMBLER_NAME_SET_P (decl))
329 const char *str, *mangled_str;
330 int dmgl_opts = DMGL_NO_OPTS;
332 if (verbosity >= 2)
334 dmgl_opts = DMGL_VERBOSE
335 | DMGL_ANSI
336 | DMGL_GNU_V3
337 | DMGL_RET_POSTFIX;
338 if (TREE_CODE (decl) == FUNCTION_DECL)
339 dmgl_opts |= DMGL_PARAMS;
342 mangled_str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
343 str = cplus_demangle_v3 (mangled_str, dmgl_opts);
344 return (str) ? str : mangled_str;
347 return IDENTIFIER_POINTER (DECL_NAME (decl));
351 /* Create a new VAR_DECL and copy information from VAR to it. */
353 tree
354 copy_var_decl (tree var, tree name, tree type)
356 tree copy = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, name, type);
358 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (var);
359 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (var);
360 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (var);
361 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (var);
362 DECL_IGNORED_P (copy) = DECL_IGNORED_P (var);
363 DECL_CONTEXT (copy) = DECL_CONTEXT (var);
364 TREE_NO_WARNING (copy) = TREE_NO_WARNING (var);
365 TREE_USED (copy) = 1;
366 DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
367 DECL_ATTRIBUTES (copy) = DECL_ATTRIBUTES (var);
369 return copy;
372 /* Given SSA_NAMEs NAME1 and NAME2, return true if they are candidates for
373 coalescing together, false otherwise.
375 This must stay consistent with var_map_base_init in tree-ssa-live.c. */
377 bool
378 gimple_can_coalesce_p (tree name1, tree name2)
380 /* First check the SSA_NAME's associated DECL. We only want to
381 coalesce if they have the same DECL or both have no associated DECL. */
382 tree var1 = SSA_NAME_VAR (name1);
383 tree var2 = SSA_NAME_VAR (name2);
384 var1 = (var1 && (!VAR_P (var1) || !DECL_IGNORED_P (var1))) ? var1 : NULL_TREE;
385 var2 = (var2 && (!VAR_P (var2) || !DECL_IGNORED_P (var2))) ? var2 : NULL_TREE;
386 if (var1 != var2)
387 return false;
389 /* Now check the types. If the types are the same, then we should
390 try to coalesce V1 and V2. */
391 tree t1 = TREE_TYPE (name1);
392 tree t2 = TREE_TYPE (name2);
393 if (t1 == t2)
394 return true;
396 /* If the types are not the same, check for a canonical type match. This
397 (for example) allows coalescing when the types are fundamentally the
398 same, but just have different names.
400 Note pointer types with different address spaces may have the same
401 canonical type. Those are rejected for coalescing by the
402 types_compatible_p check. */
403 if (TYPE_CANONICAL (t1)
404 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)
405 && types_compatible_p (t1, t2))
406 return true;
408 return false;
411 /* Strip off a legitimate source ending from the input string NAME of
412 length LEN. Rather than having to know the names used by all of
413 our front ends, we strip off an ending of a period followed by
414 up to five characters. (Java uses ".class".) */
416 static inline void
417 remove_suffix (char *name, int len)
419 int i;
421 for (i = 2; i < 8 && len > i; i++)
423 if (name[len - i] == '.')
425 name[len - i] = '\0';
426 break;
431 /* Create a new temporary name with PREFIX. Return an identifier. */
433 static GTY(()) unsigned int tmp_var_id_num;
435 tree
436 create_tmp_var_name (const char *prefix)
438 char *tmp_name;
440 if (prefix)
442 char *preftmp = ASTRDUP (prefix);
444 remove_suffix (preftmp, strlen (preftmp));
445 clean_symbol_name (preftmp);
447 prefix = preftmp;
450 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
451 return get_identifier (tmp_name);
454 /* Create a new temporary variable declaration of type TYPE.
455 Do NOT push it into the current binding. */
457 tree
458 create_tmp_var_raw (tree type, const char *prefix)
460 tree tmp_var;
462 tmp_var = build_decl (input_location,
463 VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
464 type);
466 /* The variable was declared by the compiler. */
467 DECL_ARTIFICIAL (tmp_var) = 1;
468 /* And we don't want debug info for it. */
469 DECL_IGNORED_P (tmp_var) = 1;
471 /* Make the variable writable. */
472 TREE_READONLY (tmp_var) = 0;
474 DECL_EXTERNAL (tmp_var) = 0;
475 TREE_STATIC (tmp_var) = 0;
476 TREE_USED (tmp_var) = 1;
478 return tmp_var;
481 /* Create a new temporary variable declaration of type TYPE. DO push the
482 variable into the current binding. Further, assume that this is called
483 only from gimplification or optimization, at which point the creation of
484 certain types are bugs. */
486 tree
487 create_tmp_var (tree type, const char *prefix)
489 tree tmp_var;
491 /* We don't allow types that are addressable (meaning we can't make copies),
492 or incomplete. We also used to reject every variable size objects here,
493 but now support those for which a constant upper bound can be obtained.
494 The processing for variable sizes is performed in gimple_add_tmp_var,
495 point at which it really matters and possibly reached via paths not going
496 through this function, e.g. after direct calls to create_tmp_var_raw. */
497 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
499 tmp_var = create_tmp_var_raw (type, prefix);
500 gimple_add_tmp_var (tmp_var);
501 return tmp_var;
504 /* Create a new temporary variable declaration of type TYPE by calling
505 create_tmp_var and if TYPE is a vector or a complex number, mark the new
506 temporary as gimple register. */
508 tree
509 create_tmp_reg (tree type, const char *prefix)
511 tree tmp;
513 tmp = create_tmp_var (type, prefix);
514 if (TREE_CODE (type) == COMPLEX_TYPE
515 || TREE_CODE (type) == VECTOR_TYPE)
516 DECL_GIMPLE_REG_P (tmp) = 1;
518 return tmp;
522 /* ----- Expression related ----- */
524 /* Extract the operands and code for expression EXPR into *SUBCODE_P,
525 *OP1_P, *OP2_P and *OP3_P respectively. */
527 void
528 extract_ops_from_tree_1 (tree expr, enum tree_code *subcode_p, tree *op1_p,
529 tree *op2_p, tree *op3_p)
531 enum gimple_rhs_class grhs_class;
533 *subcode_p = TREE_CODE (expr);
534 grhs_class = get_gimple_rhs_class (*subcode_p);
536 if (grhs_class == GIMPLE_TERNARY_RHS)
538 *op1_p = TREE_OPERAND (expr, 0);
539 *op2_p = TREE_OPERAND (expr, 1);
540 *op3_p = TREE_OPERAND (expr, 2);
542 else if (grhs_class == GIMPLE_BINARY_RHS)
544 *op1_p = TREE_OPERAND (expr, 0);
545 *op2_p = TREE_OPERAND (expr, 1);
546 *op3_p = NULL_TREE;
548 else if (grhs_class == GIMPLE_UNARY_RHS)
550 *op1_p = TREE_OPERAND (expr, 0);
551 *op2_p = NULL_TREE;
552 *op3_p = NULL_TREE;
554 else if (grhs_class == GIMPLE_SINGLE_RHS)
556 *op1_p = expr;
557 *op2_p = NULL_TREE;
558 *op3_p = NULL_TREE;
560 else
561 gcc_unreachable ();
564 /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
566 void
567 gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
568 tree *lhs_p, tree *rhs_p)
570 gcc_assert (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison
571 || TREE_CODE (cond) == TRUTH_NOT_EXPR
572 || is_gimple_min_invariant (cond)
573 || SSA_VAR_P (cond));
575 extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
577 /* Canonicalize conditionals of the form 'if (!VAL)'. */
578 if (*code_p == TRUTH_NOT_EXPR)
580 *code_p = EQ_EXPR;
581 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
582 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
584 /* Canonicalize conditionals of the form 'if (VAL)' */
585 else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
587 *code_p = NE_EXPR;
588 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
589 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
593 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
595 bool
596 is_gimple_lvalue (tree t)
598 return (is_gimple_addressable (t)
599 || TREE_CODE (t) == WITH_SIZE_EXPR
600 /* These are complex lvalues, but don't have addresses, so they
601 go here. */
602 || TREE_CODE (t) == BIT_FIELD_REF);
605 /* Return true if T is a GIMPLE condition. */
607 bool
608 is_gimple_condexpr (tree t)
610 return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
611 && !tree_could_throw_p (t)
612 && is_gimple_val (TREE_OPERAND (t, 0))
613 && is_gimple_val (TREE_OPERAND (t, 1))));
616 /* Return true if T is a gimple address. */
618 bool
619 is_gimple_address (const_tree t)
621 tree op;
623 if (TREE_CODE (t) != ADDR_EXPR)
624 return false;
626 op = TREE_OPERAND (t, 0);
627 while (handled_component_p (op))
629 if ((TREE_CODE (op) == ARRAY_REF
630 || TREE_CODE (op) == ARRAY_RANGE_REF)
631 && !is_gimple_val (TREE_OPERAND (op, 1)))
632 return false;
634 op = TREE_OPERAND (op, 0);
637 if (CONSTANT_CLASS_P (op) || TREE_CODE (op) == MEM_REF)
638 return true;
640 switch (TREE_CODE (op))
642 case PARM_DECL:
643 case RESULT_DECL:
644 case LABEL_DECL:
645 case FUNCTION_DECL:
646 case VAR_DECL:
647 case CONST_DECL:
648 return true;
650 default:
651 return false;
655 /* Return true if T is a gimple invariant address. */
657 bool
658 is_gimple_invariant_address (const_tree t)
660 const_tree op;
662 if (TREE_CODE (t) != ADDR_EXPR)
663 return false;
665 op = strip_invariant_refs (TREE_OPERAND (t, 0));
666 if (!op)
667 return false;
669 if (TREE_CODE (op) == MEM_REF)
671 const_tree op0 = TREE_OPERAND (op, 0);
672 return (TREE_CODE (op0) == ADDR_EXPR
673 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
674 || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
677 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
680 /* Return true if T is a gimple invariant address at IPA level
681 (so addresses of variables on stack are not allowed). */
683 bool
684 is_gimple_ip_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_ip_invariant_p (TREE_OPERAND (op0, 0))));
703 return CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op);
706 /* Return true if T is a GIMPLE minimal invariant. It's a restricted
707 form of function invariant. */
709 bool
710 is_gimple_min_invariant (const_tree t)
712 if (TREE_CODE (t) == ADDR_EXPR)
713 return is_gimple_invariant_address (t);
715 return is_gimple_constant (t);
718 /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
719 form of gimple minimal invariant. */
721 bool
722 is_gimple_ip_invariant (const_tree t)
724 if (TREE_CODE (t) == ADDR_EXPR)
725 return is_gimple_ip_invariant_address (t);
727 return is_gimple_constant (t);
730 /* Return true if T is a non-aggregate register variable. */
732 bool
733 is_gimple_reg (tree t)
735 if (virtual_operand_p (t))
736 return false;
738 if (TREE_CODE (t) == SSA_NAME)
739 return true;
741 if (!is_gimple_variable (t))
742 return false;
744 if (!is_gimple_reg_type (TREE_TYPE (t)))
745 return false;
747 /* A volatile decl is not acceptable because we can't reuse it as
748 needed. We need to copy it into a temp first. */
749 if (TREE_THIS_VOLATILE (t))
750 return false;
752 /* We define "registers" as things that can be renamed as needed,
753 which with our infrastructure does not apply to memory. */
754 if (needs_to_live_in_memory (t))
755 return false;
757 /* Hard register variables are an interesting case. For those that
758 are call-clobbered, we don't know where all the calls are, since
759 we don't (want to) take into account which operations will turn
760 into libcalls at the rtl level. For those that are call-saved,
761 we don't currently model the fact that calls may in fact change
762 global hard registers, nor do we examine ASM_CLOBBERS at the tree
763 level, and so miss variable changes that might imply. All around,
764 it seems safest to not do too much optimization with these at the
765 tree level at all. We'll have to rely on the rtl optimizers to
766 clean this up, as there we've got all the appropriate bits exposed. */
767 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
768 return false;
770 /* Complex and vector values must have been put into SSA-like form.
771 That is, no assignments to the individual components. */
772 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
773 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
774 return DECL_GIMPLE_REG_P (t);
776 return true;
780 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
782 bool
783 is_gimple_val (tree t)
785 /* Make loads from volatiles and memory vars explicit. */
786 if (is_gimple_variable (t)
787 && is_gimple_reg_type (TREE_TYPE (t))
788 && !is_gimple_reg (t))
789 return false;
791 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
794 /* Similarly, but accept hard registers as inputs to asm statements. */
796 bool
797 is_gimple_asm_val (tree t)
799 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
800 return true;
802 return is_gimple_val (t);
805 /* Return true if T is a GIMPLE minimal lvalue. */
807 bool
808 is_gimple_min_lval (tree t)
810 if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
811 return false;
812 return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF);
815 /* Return true if T is a valid function operand of a CALL_EXPR. */
817 bool
818 is_gimple_call_addr (tree t)
820 return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
823 /* Return true if T is a valid address operand of a MEM_REF. */
825 bool
826 is_gimple_mem_ref_addr (tree t)
828 return (is_gimple_reg (t)
829 || TREE_CODE (t) == INTEGER_CST
830 || (TREE_CODE (t) == ADDR_EXPR
831 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
832 || decl_address_invariant_p (TREE_OPERAND (t, 0)))));
835 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
836 form and we don't do any syntax checking. */
838 void
839 mark_addressable (tree x)
841 while (handled_component_p (x))
842 x = TREE_OPERAND (x, 0);
843 if (TREE_CODE (x) == MEM_REF
844 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
845 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
846 if (TREE_CODE (x) != VAR_DECL
847 && TREE_CODE (x) != PARM_DECL
848 && TREE_CODE (x) != RESULT_DECL)
849 return;
850 TREE_ADDRESSABLE (x) = 1;
852 /* Also mark the artificial SSA_NAME that points to the partition of X. */
853 if (TREE_CODE (x) == VAR_DECL
854 && !DECL_EXTERNAL (x)
855 && !TREE_STATIC (x)
856 && cfun->gimple_df != NULL
857 && cfun->gimple_df->decls_to_pointers != NULL)
859 void *namep
860 = pointer_map_contains (cfun->gimple_df->decls_to_pointers, x);
861 if (namep)
862 TREE_ADDRESSABLE (*(tree *)namep) = 1;
866 /* Returns true iff T is a valid RHS for an assignment to a renamed
867 user -- or front-end generated artificial -- variable. */
869 bool
870 is_gimple_reg_rhs (tree t)
872 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
875 #include "gt-gimple-expr.h"