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