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