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