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