* c/c-typeck.c (build_binary_op): Check for invalid comparison
[official-gcc.git] / gcc / gimple-expr.c
blob72132dd3a9b3c0206922cb56710fee9049ef84c0
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 "pointer-set.h"
28 #include "basic-block.h"
29 #include "tree-ssa-alias.h"
30 #include "internal-fn.h"
31 #include "tree-eh.h"
32 #include "gimple-expr.h"
33 #include "is-a.h"
34 #include "langhooks.h"
35 #include "gimple.h"
36 #include "stringpool.h"
37 #include "gimplify.h"
38 #include "stor-layout.h"
39 #include "demangle.h"
40 #include "gimple-ssa.h"
42 /* ----- Type related ----- */
44 /* Return true if the conversion from INNER_TYPE to OUTER_TYPE is a
45 useless type conversion, otherwise return false.
47 This function implicitly defines the middle-end type system. With
48 the notion of 'a < b' meaning that useless_type_conversion_p (a, b)
49 holds and 'a > b' meaning that useless_type_conversion_p (b, a) holds,
50 the following invariants shall be fulfilled:
52 1) useless_type_conversion_p is transitive.
53 If a < b and b < c then a < c.
55 2) useless_type_conversion_p is not symmetric.
56 From a < b does not follow a > b.
58 3) Types define the available set of operations applicable to values.
59 A type conversion is useless if the operations for the target type
60 is a subset of the operations for the source type. For example
61 casts to void* are useless, casts from void* are not (void* can't
62 be dereferenced or offsetted, but copied, hence its set of operations
63 is a strict subset of that of all other data pointer types). Casts
64 to const T* are useless (can't be written to), casts from const T*
65 to T* are not. */
67 bool
68 useless_type_conversion_p (tree outer_type, tree inner_type)
70 /* Do the following before stripping toplevel qualifiers. */
71 if (POINTER_TYPE_P (inner_type)
72 && POINTER_TYPE_P (outer_type))
74 int i_shared = upc_shared_type_p (TREE_TYPE (inner_type));
75 int o_shared = upc_shared_type_p (TREE_TYPE (outer_type));
77 /* Retain conversions from a UPC shared pointer to
78 a regular C pointer. */
79 if (!o_shared && i_shared)
80 return false;
82 /* Retain conversions between incompatible UPC shared pointers. */
83 if (o_shared && i_shared
84 && !lang_hooks.types_compatible_p (inner_type, outer_type))
85 return false;
87 /* Do not lose casts between pointers to different address spaces. */
88 if (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
89 != TYPE_ADDR_SPACE (TREE_TYPE (inner_type)))
90 return false;
93 /* From now on qualifiers on value types do not matter. */
94 inner_type = TYPE_MAIN_VARIANT (inner_type);
95 outer_type = TYPE_MAIN_VARIANT (outer_type);
97 if (inner_type == outer_type)
98 return true;
100 /* If we know the canonical types, compare them. */
101 if (TYPE_CANONICAL (inner_type)
102 && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type))
103 return true;
105 /* Changes in machine mode are never useless conversions unless we
106 deal with aggregate types in which case we defer to later checks. */
107 if (TYPE_MODE (inner_type) != TYPE_MODE (outer_type)
108 && !AGGREGATE_TYPE_P (inner_type))
109 return false;
111 /* If both the inner and outer types are integral types, then the
112 conversion is not necessary if they have the same mode and
113 signedness and precision, and both or neither are boolean. */
114 if (INTEGRAL_TYPE_P (inner_type)
115 && INTEGRAL_TYPE_P (outer_type))
117 /* Preserve changes in signedness or precision. */
118 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
119 || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
120 return false;
122 /* Preserve conversions to/from BOOLEAN_TYPE if types are not
123 of precision one. */
124 if (((TREE_CODE (inner_type) == BOOLEAN_TYPE)
125 != (TREE_CODE (outer_type) == BOOLEAN_TYPE))
126 && TYPE_PRECISION (outer_type) != 1)
127 return false;
129 /* We don't need to preserve changes in the types minimum or
130 maximum value in general as these do not generate code
131 unless the types precisions are different. */
132 return true;
135 /* Scalar floating point types with the same mode are compatible. */
136 else if (SCALAR_FLOAT_TYPE_P (inner_type)
137 && SCALAR_FLOAT_TYPE_P (outer_type))
138 return true;
140 /* Fixed point types with the same mode are compatible. */
141 else if (FIXED_POINT_TYPE_P (inner_type)
142 && FIXED_POINT_TYPE_P (outer_type))
143 return true;
145 /* We need to take special care recursing to pointed-to types. */
146 else if (POINTER_TYPE_P (inner_type)
147 && POINTER_TYPE_P (outer_type))
149 /* Do not lose casts to function pointer types. */
150 if ((TREE_CODE (TREE_TYPE (outer_type)) == FUNCTION_TYPE
151 || TREE_CODE (TREE_TYPE (outer_type)) == METHOD_TYPE)
152 && !(TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE
153 || TREE_CODE (TREE_TYPE (inner_type)) == METHOD_TYPE))
154 return false;
156 /* We do not care for const qualification of the pointed-to types
157 as const qualification has no semantic value to the middle-end. */
159 /* Otherwise pointers/references are equivalent. */
160 return true;
163 /* Recurse for complex types. */
164 else if (TREE_CODE (inner_type) == COMPLEX_TYPE
165 && TREE_CODE (outer_type) == COMPLEX_TYPE)
166 return useless_type_conversion_p (TREE_TYPE (outer_type),
167 TREE_TYPE (inner_type));
169 /* Recurse for vector types with the same number of subparts. */
170 else if (TREE_CODE (inner_type) == VECTOR_TYPE
171 && TREE_CODE (outer_type) == VECTOR_TYPE
172 && TYPE_PRECISION (inner_type) == TYPE_PRECISION (outer_type))
173 return useless_type_conversion_p (TREE_TYPE (outer_type),
174 TREE_TYPE (inner_type));
176 else if (TREE_CODE (inner_type) == ARRAY_TYPE
177 && TREE_CODE (outer_type) == ARRAY_TYPE)
179 /* Preserve string attributes. */
180 if (TYPE_STRING_FLAG (inner_type) != TYPE_STRING_FLAG (outer_type))
181 return false;
183 /* Conversions from array types with unknown extent to
184 array types with known extent are not useless. */
185 if (!TYPE_DOMAIN (inner_type)
186 && TYPE_DOMAIN (outer_type))
187 return false;
189 /* Nor are conversions from array types with non-constant size to
190 array types with constant size or to different size. */
191 if (TYPE_SIZE (outer_type)
192 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST
193 && (!TYPE_SIZE (inner_type)
194 || TREE_CODE (TYPE_SIZE (inner_type)) != INTEGER_CST
195 || !tree_int_cst_equal (TYPE_SIZE (outer_type),
196 TYPE_SIZE (inner_type))))
197 return false;
199 /* Check conversions between arrays with partially known extents.
200 If the array min/max values are constant they have to match.
201 Otherwise allow conversions to unknown and variable extents.
202 In particular this declares conversions that may change the
203 mode to BLKmode as useless. */
204 if (TYPE_DOMAIN (inner_type)
205 && TYPE_DOMAIN (outer_type)
206 && TYPE_DOMAIN (inner_type) != TYPE_DOMAIN (outer_type))
208 tree inner_min = TYPE_MIN_VALUE (TYPE_DOMAIN (inner_type));
209 tree outer_min = TYPE_MIN_VALUE (TYPE_DOMAIN (outer_type));
210 tree inner_max = TYPE_MAX_VALUE (TYPE_DOMAIN (inner_type));
211 tree outer_max = TYPE_MAX_VALUE (TYPE_DOMAIN (outer_type));
213 /* After gimplification a variable min/max value carries no
214 additional information compared to a NULL value. All that
215 matters has been lowered to be part of the IL. */
216 if (inner_min && TREE_CODE (inner_min) != INTEGER_CST)
217 inner_min = NULL_TREE;
218 if (outer_min && TREE_CODE (outer_min) != INTEGER_CST)
219 outer_min = NULL_TREE;
220 if (inner_max && TREE_CODE (inner_max) != INTEGER_CST)
221 inner_max = NULL_TREE;
222 if (outer_max && TREE_CODE (outer_max) != INTEGER_CST)
223 outer_max = NULL_TREE;
225 /* Conversions NULL / variable <- cst are useless, but not
226 the other way around. */
227 if (outer_min
228 && (!inner_min
229 || !tree_int_cst_equal (inner_min, outer_min)))
230 return false;
231 if (outer_max
232 && (!inner_max
233 || !tree_int_cst_equal (inner_max, outer_max)))
234 return false;
237 /* Recurse on the element check. */
238 return useless_type_conversion_p (TREE_TYPE (outer_type),
239 TREE_TYPE (inner_type));
242 else if ((TREE_CODE (inner_type) == FUNCTION_TYPE
243 || TREE_CODE (inner_type) == METHOD_TYPE)
244 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
246 tree outer_parm, inner_parm;
248 /* If the return types are not compatible bail out. */
249 if (!useless_type_conversion_p (TREE_TYPE (outer_type),
250 TREE_TYPE (inner_type)))
251 return false;
253 /* Method types should belong to a compatible base class. */
254 if (TREE_CODE (inner_type) == METHOD_TYPE
255 && !useless_type_conversion_p (TYPE_METHOD_BASETYPE (outer_type),
256 TYPE_METHOD_BASETYPE (inner_type)))
257 return false;
259 /* A conversion to an unprototyped argument list is ok. */
260 if (!prototype_p (outer_type))
261 return true;
263 /* If the unqualified argument types are compatible the conversion
264 is useless. */
265 if (TYPE_ARG_TYPES (outer_type) == TYPE_ARG_TYPES (inner_type))
266 return true;
268 for (outer_parm = TYPE_ARG_TYPES (outer_type),
269 inner_parm = TYPE_ARG_TYPES (inner_type);
270 outer_parm && inner_parm;
271 outer_parm = TREE_CHAIN (outer_parm),
272 inner_parm = TREE_CHAIN (inner_parm))
273 if (!useless_type_conversion_p
274 (TYPE_MAIN_VARIANT (TREE_VALUE (outer_parm)),
275 TYPE_MAIN_VARIANT (TREE_VALUE (inner_parm))))
276 return false;
278 /* If there is a mismatch in the number of arguments the functions
279 are not compatible. */
280 if (outer_parm || inner_parm)
281 return false;
283 /* Defer to the target if necessary. */
284 if (TYPE_ATTRIBUTES (inner_type) || TYPE_ATTRIBUTES (outer_type))
285 return comp_type_attributes (outer_type, inner_type) != 0;
287 return true;
290 /* For aggregates we rely on TYPE_CANONICAL exclusively and require
291 explicit conversions for types involving to be structurally
292 compared types. */
293 else if (AGGREGATE_TYPE_P (inner_type)
294 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
295 return false;
297 return false;
301 /* ----- Decl related ----- */
303 /* Set sequence SEQ to be the GIMPLE body for function FN. */
305 void
306 gimple_set_body (tree fndecl, gimple_seq seq)
308 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
309 if (fn == NULL)
311 /* If FNDECL still does not have a function structure associated
312 with it, then it does not make sense for it to receive a
313 GIMPLE body. */
314 gcc_assert (seq == NULL);
316 else
317 fn->gimple_body = seq;
321 /* Return the body of GIMPLE statements for function FN. After the
322 CFG pass, the function body doesn't exist anymore because it has
323 been split up into basic blocks. In this case, it returns
324 NULL. */
326 gimple_seq
327 gimple_body (tree fndecl)
329 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
330 return fn ? fn->gimple_body : NULL;
333 /* Return true when FNDECL has Gimple body either in unlowered
334 or CFG form. */
335 bool
336 gimple_has_body_p (tree fndecl)
338 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
339 return (gimple_body (fndecl) || (fn && fn->cfg));
342 /* Return a printable name for symbol DECL. */
344 const char *
345 gimple_decl_printable_name (tree decl, int verbosity)
347 if (!DECL_NAME (decl))
348 return NULL;
350 if (DECL_ASSEMBLER_NAME_SET_P (decl))
352 const char *str, *mangled_str;
353 int dmgl_opts = DMGL_NO_OPTS;
355 if (verbosity >= 2)
357 dmgl_opts = DMGL_VERBOSE
358 | DMGL_ANSI
359 | DMGL_GNU_V3
360 | DMGL_RET_POSTFIX;
361 if (TREE_CODE (decl) == FUNCTION_DECL)
362 dmgl_opts |= DMGL_PARAMS;
365 mangled_str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
366 str = cplus_demangle_v3 (mangled_str, dmgl_opts);
367 return (str) ? str : mangled_str;
370 return IDENTIFIER_POINTER (DECL_NAME (decl));
374 /* Create a new VAR_DECL and copy information from VAR to it. */
376 tree
377 copy_var_decl (tree var, tree name, tree type)
379 tree copy = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, name, type);
381 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (var);
382 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (var);
383 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (var);
384 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (var);
385 DECL_IGNORED_P (copy) = DECL_IGNORED_P (var);
386 DECL_CONTEXT (copy) = DECL_CONTEXT (var);
387 TREE_NO_WARNING (copy) = TREE_NO_WARNING (var);
388 TREE_USED (copy) = 1;
389 DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
390 DECL_ATTRIBUTES (copy) = DECL_ATTRIBUTES (var);
392 return copy;
395 /* Given SSA_NAMEs NAME1 and NAME2, return true if they are candidates for
396 coalescing together, false otherwise.
398 This must stay consistent with var_map_base_init in tree-ssa-live.c. */
400 bool
401 gimple_can_coalesce_p (tree name1, tree name2)
403 /* First check the SSA_NAME's associated DECL. We only want to
404 coalesce if they have the same DECL or both have no associated DECL. */
405 tree var1 = SSA_NAME_VAR (name1);
406 tree var2 = SSA_NAME_VAR (name2);
407 var1 = (var1 && (!VAR_P (var1) || !DECL_IGNORED_P (var1))) ? var1 : NULL_TREE;
408 var2 = (var2 && (!VAR_P (var2) || !DECL_IGNORED_P (var2))) ? var2 : NULL_TREE;
409 if (var1 != var2)
410 return false;
412 /* Now check the types. If the types are the same, then we should
413 try to coalesce V1 and V2. */
414 tree t1 = TREE_TYPE (name1);
415 tree t2 = TREE_TYPE (name2);
416 if (t1 == t2)
417 return true;
419 /* If the types are not the same, check for a canonical type match. This
420 (for example) allows coalescing when the types are fundamentally the
421 same, but just have different names.
423 Note pointer types with different address spaces may have the same
424 canonical type. Those are rejected for coalescing by the
425 types_compatible_p check. */
426 if (TYPE_CANONICAL (t1)
427 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)
428 && types_compatible_p (t1, t2))
429 return true;
431 return false;
434 /* Strip off a legitimate source ending from the input string NAME of
435 length LEN. Rather than having to know the names used by all of
436 our front ends, we strip off an ending of a period followed by
437 up to five characters. (Java uses ".class".) */
439 static inline void
440 remove_suffix (char *name, int len)
442 int i;
444 for (i = 2; i < 8 && len > i; i++)
446 if (name[len - i] == '.')
448 name[len - i] = '\0';
449 break;
454 /* Create a new temporary name with PREFIX. Return an identifier. */
456 static GTY(()) unsigned int tmp_var_id_num;
458 tree
459 create_tmp_var_name (const char *prefix)
461 char *tmp_name;
463 if (prefix)
465 char *preftmp = ASTRDUP (prefix);
467 remove_suffix (preftmp, strlen (preftmp));
468 clean_symbol_name (preftmp);
470 prefix = preftmp;
473 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
474 return get_identifier (tmp_name);
477 /* Create a new temporary variable declaration of type TYPE.
478 Do NOT push it into the current binding. */
480 tree
481 create_tmp_var_raw (tree type, const char *prefix)
483 tree tmp_var;
485 tmp_var = build_decl (input_location,
486 VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
487 type);
489 /* The variable was declared by the compiler. */
490 DECL_ARTIFICIAL (tmp_var) = 1;
491 /* And we don't want debug info for it. */
492 DECL_IGNORED_P (tmp_var) = 1;
494 /* Make the variable writable. */
495 TREE_READONLY (tmp_var) = 0;
497 DECL_EXTERNAL (tmp_var) = 0;
498 TREE_STATIC (tmp_var) = 0;
499 TREE_USED (tmp_var) = 1;
501 return tmp_var;
504 /* Create a new temporary variable declaration of type TYPE. DO push the
505 variable into the current binding. Further, assume that this is called
506 only from gimplification or optimization, at which point the creation of
507 certain types are bugs. */
509 tree
510 create_tmp_var (tree type, const char *prefix)
512 tree tmp_var;
514 /* We don't allow types that are addressable (meaning we can't make copies),
515 or incomplete. We also used to reject every variable size objects here,
516 but now support those for which a constant upper bound can be obtained.
517 The processing for variable sizes is performed in gimple_add_tmp_var,
518 point at which it really matters and possibly reached via paths not going
519 through this function, e.g. after direct calls to create_tmp_var_raw. */
520 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
522 tmp_var = create_tmp_var_raw (type, prefix);
523 gimple_add_tmp_var (tmp_var);
524 return tmp_var;
527 /* Create a new temporary variable declaration of type TYPE by calling
528 create_tmp_var and if TYPE is a vector or a complex number, mark the new
529 temporary as gimple register. */
531 tree
532 create_tmp_reg (tree type, const char *prefix)
534 tree tmp;
536 tmp = create_tmp_var (type, prefix);
537 if (TREE_CODE (type) == COMPLEX_TYPE
538 || TREE_CODE (type) == VECTOR_TYPE)
539 DECL_GIMPLE_REG_P (tmp) = 1;
541 return tmp;
544 /* Create a new temporary variable declaration of type TYPE by calling
545 create_tmp_var and if TYPE is a vector or a complex number, mark the new
546 temporary as gimple register. */
548 tree
549 create_tmp_reg_fn (struct function *fn, tree type, const char *prefix)
551 tree tmp;
553 tmp = create_tmp_var_raw (type, prefix);
554 gimple_add_tmp_var_fn (fn, tmp);
555 if (TREE_CODE (type) == COMPLEX_TYPE
556 || TREE_CODE (type) == VECTOR_TYPE)
557 DECL_GIMPLE_REG_P (tmp) = 1;
559 return tmp;
563 /* ----- Expression related ----- */
565 /* Extract the operands and code for expression EXPR into *SUBCODE_P,
566 *OP1_P, *OP2_P and *OP3_P respectively. */
568 void
569 extract_ops_from_tree_1 (tree expr, enum tree_code *subcode_p, tree *op1_p,
570 tree *op2_p, tree *op3_p)
572 enum gimple_rhs_class grhs_class;
574 *subcode_p = TREE_CODE (expr);
575 grhs_class = get_gimple_rhs_class (*subcode_p);
577 if (grhs_class == GIMPLE_TERNARY_RHS)
579 *op1_p = TREE_OPERAND (expr, 0);
580 *op2_p = TREE_OPERAND (expr, 1);
581 *op3_p = TREE_OPERAND (expr, 2);
583 else if (grhs_class == GIMPLE_BINARY_RHS)
585 *op1_p = TREE_OPERAND (expr, 0);
586 *op2_p = TREE_OPERAND (expr, 1);
587 *op3_p = NULL_TREE;
589 else if (grhs_class == GIMPLE_UNARY_RHS)
591 *op1_p = TREE_OPERAND (expr, 0);
592 *op2_p = NULL_TREE;
593 *op3_p = NULL_TREE;
595 else if (grhs_class == GIMPLE_SINGLE_RHS)
597 *op1_p = expr;
598 *op2_p = NULL_TREE;
599 *op3_p = NULL_TREE;
601 else
602 gcc_unreachable ();
605 /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
607 void
608 gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
609 tree *lhs_p, tree *rhs_p)
611 gcc_assert (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison
612 || TREE_CODE (cond) == TRUTH_NOT_EXPR
613 || is_gimple_min_invariant (cond)
614 || SSA_VAR_P (cond));
616 extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
618 /* Canonicalize conditionals of the form 'if (!VAL)'. */
619 if (*code_p == TRUTH_NOT_EXPR)
621 *code_p = EQ_EXPR;
622 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
623 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
625 /* Canonicalize conditionals of the form 'if (VAL)' */
626 else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
628 *code_p = NE_EXPR;
629 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
630 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
634 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
636 bool
637 is_gimple_lvalue (tree t)
639 return (is_gimple_addressable (t)
640 || TREE_CODE (t) == WITH_SIZE_EXPR
641 /* These are complex lvalues, but don't have addresses, so they
642 go here. */
643 || TREE_CODE (t) == BIT_FIELD_REF);
646 /* Return true if T is a GIMPLE condition. */
648 bool
649 is_gimple_condexpr (tree t)
651 return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
652 && !tree_could_throw_p (t)
653 && is_gimple_val (TREE_OPERAND (t, 0))
654 && is_gimple_val (TREE_OPERAND (t, 1))));
657 /* Return true if T is a gimple address. */
659 bool
660 is_gimple_address (const_tree t)
662 tree op;
664 if (TREE_CODE (t) != ADDR_EXPR)
665 return false;
667 op = TREE_OPERAND (t, 0);
668 while (handled_component_p (op))
670 if ((TREE_CODE (op) == ARRAY_REF
671 || TREE_CODE (op) == ARRAY_RANGE_REF)
672 && !is_gimple_val (TREE_OPERAND (op, 1)))
673 return false;
675 op = TREE_OPERAND (op, 0);
678 if (CONSTANT_CLASS_P (op) || TREE_CODE (op) == MEM_REF)
679 return true;
681 switch (TREE_CODE (op))
683 case PARM_DECL:
684 case RESULT_DECL:
685 case LABEL_DECL:
686 case FUNCTION_DECL:
687 case VAR_DECL:
688 case CONST_DECL:
689 return true;
691 default:
692 return false;
696 /* Return true if T is a gimple invariant address. */
698 bool
699 is_gimple_invariant_address (const_tree t)
701 const_tree op;
703 if (TREE_CODE (t) != ADDR_EXPR)
704 return false;
706 op = strip_invariant_refs (TREE_OPERAND (t, 0));
707 if (!op)
708 return false;
710 if (TREE_CODE (op) == MEM_REF)
712 const_tree op0 = TREE_OPERAND (op, 0);
713 return (TREE_CODE (op0) == ADDR_EXPR
714 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
715 || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
718 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
721 /* Return true if T is a gimple invariant address at IPA level
722 (so addresses of variables on stack are not allowed). */
724 bool
725 is_gimple_ip_invariant_address (const_tree t)
727 const_tree op;
729 if (TREE_CODE (t) != ADDR_EXPR)
730 return false;
732 op = strip_invariant_refs (TREE_OPERAND (t, 0));
733 if (!op)
734 return false;
736 if (TREE_CODE (op) == MEM_REF)
738 const_tree op0 = TREE_OPERAND (op, 0);
739 return (TREE_CODE (op0) == ADDR_EXPR
740 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
741 || decl_address_ip_invariant_p (TREE_OPERAND (op0, 0))));
744 return CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op);
747 /* Return true if T is a GIMPLE minimal invariant. It's a restricted
748 form of function invariant. */
750 bool
751 is_gimple_min_invariant (const_tree t)
753 if (TREE_CODE (t) == ADDR_EXPR)
754 return is_gimple_invariant_address (t);
756 return is_gimple_constant (t);
759 /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
760 form of gimple minimal invariant. */
762 bool
763 is_gimple_ip_invariant (const_tree t)
765 if (TREE_CODE (t) == ADDR_EXPR)
766 return is_gimple_ip_invariant_address (t);
768 return is_gimple_constant (t);
771 /* Return true if T is a non-aggregate register variable. */
773 bool
774 is_gimple_reg (tree t)
776 if (virtual_operand_p (t))
777 return false;
779 if (TREE_CODE (t) == SSA_NAME)
780 return true;
782 if (!is_gimple_variable (t))
783 return false;
785 if (!is_gimple_reg_type (TREE_TYPE (t)))
786 return false;
788 /* A volatile decl is not acceptable because we can't reuse it as
789 needed. We need to copy it into a temp first. */
790 if (TREE_THIS_VOLATILE (t))
791 return false;
793 /* We define "registers" as things that can be renamed as needed,
794 which with our infrastructure does not apply to memory. */
795 if (needs_to_live_in_memory (t))
796 return false;
798 /* Hard register variables are an interesting case. For those that
799 are call-clobbered, we don't know where all the calls are, since
800 we don't (want to) take into account which operations will turn
801 into libcalls at the rtl level. For those that are call-saved,
802 we don't currently model the fact that calls may in fact change
803 global hard registers, nor do we examine ASM_CLOBBERS at the tree
804 level, and so miss variable changes that might imply. All around,
805 it seems safest to not do too much optimization with these at the
806 tree level at all. We'll have to rely on the rtl optimizers to
807 clean this up, as there we've got all the appropriate bits exposed. */
808 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
809 return false;
811 /* Complex and vector values must have been put into SSA-like form.
812 That is, no assignments to the individual components. */
813 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
814 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
815 return DECL_GIMPLE_REG_P (t);
817 return true;
821 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
823 bool
824 is_gimple_val (tree t)
826 /* Make loads from volatiles and memory vars explicit. */
827 if (is_gimple_variable (t)
828 && is_gimple_reg_type (TREE_TYPE (t))
829 && !is_gimple_reg (t))
830 return false;
832 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
835 /* Similarly, but accept hard registers as inputs to asm statements. */
837 bool
838 is_gimple_asm_val (tree t)
840 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
841 return true;
843 return is_gimple_val (t);
846 /* Return true if T is a GIMPLE minimal lvalue. */
848 bool
849 is_gimple_min_lval (tree t)
851 if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
852 return false;
853 return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF);
856 /* Return true if T is a valid function operand of a CALL_EXPR. */
858 bool
859 is_gimple_call_addr (tree t)
861 return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
864 /* Return true if T is a valid address operand of a MEM_REF. */
866 bool
867 is_gimple_mem_ref_addr (tree t)
869 return (is_gimple_reg (t)
870 || TREE_CODE (t) == INTEGER_CST
871 || (TREE_CODE (t) == ADDR_EXPR
872 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
873 || decl_address_invariant_p (TREE_OPERAND (t, 0)))));
876 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
877 form and we don't do any syntax checking. */
879 void
880 mark_addressable (tree x)
882 while (handled_component_p (x))
883 x = TREE_OPERAND (x, 0);
884 if (TREE_CODE (x) == MEM_REF
885 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
886 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
887 if (TREE_CODE (x) != VAR_DECL
888 && TREE_CODE (x) != PARM_DECL
889 && TREE_CODE (x) != RESULT_DECL)
890 return;
891 TREE_ADDRESSABLE (x) = 1;
893 /* Also mark the artificial SSA_NAME that points to the partition of X. */
894 if (TREE_CODE (x) == VAR_DECL
895 && !DECL_EXTERNAL (x)
896 && !TREE_STATIC (x)
897 && cfun->gimple_df != NULL
898 && cfun->gimple_df->decls_to_pointers != NULL)
900 void *namep
901 = pointer_map_contains (cfun->gimple_df->decls_to_pointers, x);
902 if (namep)
903 TREE_ADDRESSABLE (*(tree *)namep) = 1;
907 /* Returns true iff T is a valid RHS for an assignment to a renamed
908 user -- or front-end generated artificial -- variable. */
910 bool
911 is_gimple_reg_rhs (tree t)
913 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
916 #include "gt-gimple-expr.h"