Merge from tree-ssa-branch (tree-ssa-cfg-merge-20031001)
[official-gcc.git] / gcc / tree.c
blob2fe85234030c560c93f3003d0a3f7cacf969718c
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* This file contains the low level primitives for operating on tree nodes,
23 including allocation, list operations, interning of identifiers,
24 construction of data type nodes and statement nodes,
25 and construction of type conversion nodes. It also contains
26 tables index by tree code that describe how to take apart
27 nodes of that code.
29 It is intended to be language-independent, but occasionally
30 calls language-dependent routines defined (for C) in typecheck.c. */
32 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "flags.h"
37 #include "tree.h"
38 #include "real.h"
39 #include "tm_p.h"
40 #include "function.h"
41 #include "obstack.h"
42 #include "toplev.h"
43 #include "ggc.h"
44 #include "hashtab.h"
45 #include "output.h"
46 #include "target.h"
47 #include "langhooks.h"
48 #include "tree-iterator.h"
49 #include "basic-block.h"
50 #include "tree-flow.h"
52 /* obstack.[ch] explicitly declined to prototype this. */
53 extern int _obstack_allocated_p (struct obstack *h, void *obj);
55 #ifdef GATHER_STATISTICS
56 /* Statistics-gathering stuff. */
58 int tree_node_counts[(int) all_kinds];
59 int tree_node_sizes[(int) all_kinds];
61 /* Keep in sync with tree.h:enum tree_node_kind. */
62 static const char * const tree_node_kind_names[] = {
63 "decls",
64 "types",
65 "blocks",
66 "stmts",
67 "refs",
68 "exprs",
69 "constants",
70 "identifiers",
71 "perm_tree_lists",
72 "temp_tree_lists",
73 "vecs",
74 "phi_nodes",
75 "ssa names",
76 "random kinds",
77 "lang_decl kinds",
78 "lang_type kinds"
80 #endif /* GATHER_STATISTICS */
82 /* Unique id for next decl created. */
83 static GTY(()) int next_decl_uid;
84 /* Unique id for next type created. */
85 static GTY(()) int next_type_uid = 1;
87 /* Since we cannot rehash a type after it is in the table, we have to
88 keep the hash code. */
90 struct type_hash GTY(())
92 unsigned long hash;
93 tree type;
96 /* Initial size of the hash table (rounded to next prime). */
97 #define TYPE_HASH_INITIAL_SIZE 1000
99 /* Now here is the hash table. When recording a type, it is added to
100 the slot whose index is the hash code. Note that the hash table is
101 used for several kinds of types (function types, array types and
102 array index range types, for now). While all these live in the
103 same table, they are completely independent, and the hash code is
104 computed differently for each of these. */
106 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
107 htab_t type_hash_table;
109 static void set_type_quals (tree, int);
110 static int type_hash_eq (const void *, const void *);
111 static hashval_t type_hash_hash (const void *);
112 static void print_type_hash_statistics (void);
113 static void finish_vector_type (tree);
114 static tree make_vector (enum machine_mode, tree, int);
115 static int type_hash_marked_p (const void *);
117 tree global_trees[TI_MAX];
118 tree integer_types[itk_none];
120 /* Init tree.c. */
122 void
123 init_ttree (void)
125 /* Initialize the hash table of types. */
126 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
127 type_hash_eq, 0);
131 /* The name of the object as the assembler will see it (but before any
132 translations made by ASM_OUTPUT_LABELREF). Often this is the same
133 as DECL_NAME. It is an IDENTIFIER_NODE. */
134 tree
135 decl_assembler_name (tree decl)
137 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
138 (*lang_hooks.set_decl_assembler_name) (decl);
139 return DECL_CHECK (decl)->decl.assembler_name;
142 /* Compute the number of bytes occupied by 'node'. This routine only
143 looks at TREE_CODE and, if the code is TREE_VEC, TREE_VEC_LENGTH. */
144 size_t
145 tree_size (tree node)
147 enum tree_code code = TREE_CODE (node);
149 switch (TREE_CODE_CLASS (code))
151 case 'd': /* A decl node */
152 return sizeof (struct tree_decl);
154 case 't': /* a type node */
155 return sizeof (struct tree_type);
157 case 'b': /* a lexical block node */
158 return sizeof (struct tree_block);
160 case 'r': /* a reference */
161 case 'e': /* an expression */
162 case 's': /* an expression with side effects */
163 case '<': /* a comparison expression */
164 case '1': /* a unary arithmetic expression */
165 case '2': /* a binary arithmetic expression */
166 return (sizeof (struct tree_exp)
167 + TREE_CODE_LENGTH (code) * sizeof (char *) - sizeof (char *));
169 case 'c': /* a constant */
170 switch (code)
172 case INTEGER_CST: return sizeof (struct tree_int_cst);
173 case REAL_CST: return sizeof (struct tree_real_cst);
174 case COMPLEX_CST: return sizeof (struct tree_complex);
175 case VECTOR_CST: return sizeof (struct tree_vector);
176 case STRING_CST: return sizeof (struct tree_string);
177 default:
178 return (*lang_hooks.tree_size) (code);
181 case 'x': /* something random, like an identifier. */
182 switch (code)
184 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
185 case TREE_LIST: return sizeof (struct tree_list);
186 case TREE_VEC: return (sizeof (struct tree_vec)
187 + TREE_VEC_LENGTH(node) * sizeof(char *)
188 - sizeof (char *));
190 case ERROR_MARK:
191 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
193 case PHI_NODE: return (sizeof (struct tree_phi_node)
194 + (PHI_ARG_CAPACITY (node) - 1) *
195 sizeof (struct phi_arg_d));
197 case EPHI_NODE: return (sizeof (struct tree_ephi_node)
198 + (EPHI_ARG_CAPACITY (node) - 1) *
199 sizeof (struct ephi_arg_d));
201 case SSA_NAME: return sizeof (struct tree_ssa_name);
202 case EUSE_NODE: return sizeof (struct tree_euse_node);
204 case ELEFT_NODE:
205 case EKILL_NODE:
206 case EEXIT_NODE: return sizeof (struct tree_eref_common);
208 default:
209 return (*lang_hooks.tree_size) (code);
212 default:
213 abort ();
217 /* Return a newly allocated node of code CODE.
218 For decl and type nodes, some other fields are initialized.
219 The rest of the node is initialized to zero.
221 Achoo! I got a code in the node. */
223 tree
224 make_node (enum tree_code code)
226 tree t;
227 int type = TREE_CODE_CLASS (code);
228 size_t length;
229 #ifdef GATHER_STATISTICS
230 tree_node_kind kind;
231 #endif
232 struct tree_common ttmp;
234 /* We can't allocate a TREE_VEC or PHI_NODE without knowing how many elements
235 it will have. */
236 if (code == TREE_VEC || code == PHI_NODE || code == EPHI_NODE)
237 abort ();
239 TREE_SET_CODE ((tree)&ttmp, code);
240 length = tree_size ((tree)&ttmp);
242 #ifdef GATHER_STATISTICS
243 switch (type)
245 case 'd': /* A decl node */
246 kind = d_kind;
247 break;
249 case 't': /* a type node */
250 kind = t_kind;
251 break;
253 case 'b': /* a lexical block */
254 kind = b_kind;
255 break;
257 case 's': /* an expression with side effects */
258 kind = s_kind;
259 break;
261 case 'r': /* a reference */
262 kind = r_kind;
263 break;
265 case 'e': /* an expression */
266 case '<': /* a comparison expression */
267 case '1': /* a unary arithmetic expression */
268 case '2': /* a binary arithmetic expression */
269 kind = e_kind;
270 break;
272 case 'c': /* a constant */
273 kind = c_kind;
274 break;
276 case 'x': /* something random, like an identifier. */
277 if (code == IDENTIFIER_NODE)
278 kind = id_kind;
279 else if (code == TREE_VEC)
280 kind = vec_kind;
281 else if (code == PHI_NODE)
282 kind = phi_kind;
283 else if (code == SSA_NAME)
284 kind = ssa_name_kind;
285 else
286 kind = x_kind;
287 break;
289 default:
290 abort ();
293 tree_node_counts[(int) kind]++;
294 tree_node_sizes[(int) kind] += length;
295 #endif
297 t = ggc_alloc_tree (length);
299 memset (t, 0, length);
301 TREE_SET_CODE (t, code);
303 switch (type)
305 case 's':
306 TREE_SIDE_EFFECTS (t) = 1;
307 break;
309 case 'd':
310 if (code != FUNCTION_DECL)
311 DECL_ALIGN (t) = 1;
312 DECL_USER_ALIGN (t) = 0;
313 DECL_IN_SYSTEM_HEADER (t) = in_system_header;
314 DECL_SOURCE_LOCATION (t) = input_location;
315 DECL_UID (t) = next_decl_uid++;
317 /* We have not yet computed the alias set for this declaration. */
318 DECL_POINTER_ALIAS_SET (t) = -1;
319 break;
321 case 't':
322 TYPE_UID (t) = next_type_uid++;
323 TYPE_ALIGN (t) = char_type_node ? TYPE_ALIGN (char_type_node) : 0;
324 TYPE_USER_ALIGN (t) = 0;
325 TYPE_MAIN_VARIANT (t) = t;
327 /* Default to no attributes for type, but let target change that. */
328 TYPE_ATTRIBUTES (t) = NULL_TREE;
329 (*targetm.set_default_type_attributes) (t);
331 /* We have not yet computed the alias set for this type. */
332 TYPE_ALIAS_SET (t) = -1;
333 break;
335 case 'c':
336 TREE_CONSTANT (t) = 1;
337 break;
339 case 'e':
340 switch (code)
342 case INIT_EXPR:
343 case MODIFY_EXPR:
344 case VA_ARG_EXPR:
345 case RTL_EXPR:
346 case PREDECREMENT_EXPR:
347 case PREINCREMENT_EXPR:
348 case POSTDECREMENT_EXPR:
349 case POSTINCREMENT_EXPR:
350 /* All of these have side-effects, no matter what their
351 operands are. */
352 TREE_SIDE_EFFECTS (t) = 1;
353 break;
355 default:
356 break;
358 break;
361 return t;
364 /* Return a new node with the same contents as NODE except that its
365 TREE_CHAIN is zero and it has a fresh uid. */
367 tree
368 copy_node (tree node)
370 tree t;
371 enum tree_code code = TREE_CODE (node);
372 size_t length;
374 length = tree_size (node);
375 t = ggc_alloc_tree (length);
376 memcpy (t, node, length);
378 TREE_CHAIN (t) = 0;
379 TREE_ASM_WRITTEN (t) = 0;
380 TREE_VISITED (t) = 0;
381 t->common.ann = 0;
383 if (TREE_CODE_CLASS (code) == 'd')
384 DECL_UID (t) = next_decl_uid++;
385 else if (TREE_CODE_CLASS (code) == 't')
387 TYPE_UID (t) = next_type_uid++;
388 /* The following is so that the debug code for
389 the copy is different from the original type.
390 The two statements usually duplicate each other
391 (because they clear fields of the same union),
392 but the optimizer should catch that. */
393 TYPE_SYMTAB_POINTER (t) = 0;
394 TYPE_SYMTAB_ADDRESS (t) = 0;
397 return t;
400 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
401 For example, this can copy a list made of TREE_LIST nodes. */
403 tree
404 copy_list (tree list)
406 tree head;
407 tree prev, next;
409 if (list == 0)
410 return 0;
412 head = prev = copy_node (list);
413 next = TREE_CHAIN (list);
414 while (next)
416 TREE_CHAIN (prev) = copy_node (next);
417 prev = TREE_CHAIN (prev);
418 next = TREE_CHAIN (next);
420 return head;
424 /* Return a newly constructed INTEGER_CST node whose constant value
425 is specified by the two ints LOW and HI.
426 The TREE_TYPE is set to `int'.
428 This function should be used via the `build_int_2' macro. */
430 tree
431 build_int_2_wide (unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
433 tree t = make_node (INTEGER_CST);
435 TREE_INT_CST_LOW (t) = low;
436 TREE_INT_CST_HIGH (t) = hi;
437 TREE_TYPE (t) = integer_type_node;
438 return t;
441 /* Return a new VECTOR_CST node whose type is TYPE and whose values
442 are in a list pointed by VALS. */
444 tree
445 build_vector (tree type, tree vals)
447 tree v = make_node (VECTOR_CST);
448 int over1 = 0, over2 = 0;
449 tree link;
451 TREE_VECTOR_CST_ELTS (v) = vals;
452 TREE_TYPE (v) = type;
454 /* Iterate through elements and check for overflow. */
455 for (link = vals; link; link = TREE_CHAIN (link))
457 tree value = TREE_VALUE (link);
459 over1 |= TREE_OVERFLOW (value);
460 over2 |= TREE_CONSTANT_OVERFLOW (value);
463 TREE_OVERFLOW (v) = over1;
464 TREE_CONSTANT_OVERFLOW (v) = over2;
466 return v;
469 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
470 are in a list pointed to by VALS. */
471 tree
472 build_constructor (tree type, tree vals)
474 tree c = make_node (CONSTRUCTOR);
475 TREE_TYPE (c) = type;
476 CONSTRUCTOR_ELTS (c) = vals;
478 /* ??? May not be necessary. Mirrors what build does. */
479 if (vals)
481 TREE_SIDE_EFFECTS (c) = TREE_SIDE_EFFECTS (vals);
482 TREE_READONLY (c) = TREE_READONLY (vals);
483 TREE_CONSTANT (c) = TREE_CONSTANT (vals);
485 else
486 TREE_CONSTANT (c) = 0; /* safe side */
488 return c;
491 /* Return a new REAL_CST node whose type is TYPE and value is D. */
493 tree
494 build_real (tree type, REAL_VALUE_TYPE d)
496 tree v;
497 REAL_VALUE_TYPE *dp;
498 int overflow = 0;
500 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
501 Consider doing it via real_convert now. */
503 v = make_node (REAL_CST);
504 dp = ggc_alloc (sizeof (REAL_VALUE_TYPE));
505 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
507 TREE_TYPE (v) = type;
508 TREE_REAL_CST_PTR (v) = dp;
509 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
510 return v;
513 /* Return a new REAL_CST node whose type is TYPE
514 and whose value is the integer value of the INTEGER_CST node I. */
516 REAL_VALUE_TYPE
517 real_value_from_int_cst (tree type ATTRIBUTE_UNUSED, tree i)
519 REAL_VALUE_TYPE d;
521 /* Clear all bits of the real value type so that we can later do
522 bitwise comparisons to see if two values are the same. */
523 memset (&d, 0, sizeof d);
525 if (! TREE_UNSIGNED (TREE_TYPE (i)))
526 REAL_VALUE_FROM_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
527 TYPE_MODE (type));
528 else
529 REAL_VALUE_FROM_UNSIGNED_INT (d, TREE_INT_CST_LOW (i),
530 TREE_INT_CST_HIGH (i), TYPE_MODE (type));
531 return d;
534 /* Given a tree representing an integer constant I, return a tree
535 representing the same value as a floating-point constant of type TYPE. */
537 tree
538 build_real_from_int_cst (tree type, tree i)
540 tree v;
541 int overflow = TREE_OVERFLOW (i);
543 v = build_real (type, real_value_from_int_cst (type, i));
545 TREE_OVERFLOW (v) |= overflow;
546 TREE_CONSTANT_OVERFLOW (v) |= overflow;
547 return v;
550 /* Return a newly constructed STRING_CST node whose value is
551 the LEN characters at STR.
552 The TREE_TYPE is not initialized. */
554 tree
555 build_string (int len, const char *str)
557 tree s = make_node (STRING_CST);
559 TREE_STRING_LENGTH (s) = len;
560 TREE_STRING_POINTER (s) = ggc_alloc_string (str, len);
562 return s;
565 /* Return a newly constructed COMPLEX_CST node whose value is
566 specified by the real and imaginary parts REAL and IMAG.
567 Both REAL and IMAG should be constant nodes. TYPE, if specified,
568 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
570 tree
571 build_complex (tree type, tree real, tree imag)
573 tree t = make_node (COMPLEX_CST);
575 TREE_REALPART (t) = real;
576 TREE_IMAGPART (t) = imag;
577 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
578 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
579 TREE_CONSTANT_OVERFLOW (t)
580 = TREE_CONSTANT_OVERFLOW (real) | TREE_CONSTANT_OVERFLOW (imag);
581 return t;
584 /* Build a newly constructed TREE_VEC node of length LEN. */
586 tree
587 make_tree_vec (int len)
589 tree t;
590 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
592 #ifdef GATHER_STATISTICS
593 tree_node_counts[(int) vec_kind]++;
594 tree_node_sizes[(int) vec_kind] += length;
595 #endif
597 t = ggc_alloc_tree (length);
599 memset (t, 0, length);
600 TREE_SET_CODE (t, TREE_VEC);
601 TREE_VEC_LENGTH (t) = len;
603 return t;
606 /* Return 1 if EXPR is the integer constant zero or a complex constant
607 of zero. */
610 integer_zerop (tree expr)
612 STRIP_NOPS (expr);
614 return ((TREE_CODE (expr) == INTEGER_CST
615 && ! TREE_CONSTANT_OVERFLOW (expr)
616 && TREE_INT_CST_LOW (expr) == 0
617 && TREE_INT_CST_HIGH (expr) == 0)
618 || (TREE_CODE (expr) == COMPLEX_CST
619 && integer_zerop (TREE_REALPART (expr))
620 && integer_zerop (TREE_IMAGPART (expr))));
623 /* Return 1 if EXPR is the integer constant one or the corresponding
624 complex constant. */
627 integer_onep (tree expr)
629 STRIP_NOPS (expr);
631 return ((TREE_CODE (expr) == INTEGER_CST
632 && ! TREE_CONSTANT_OVERFLOW (expr)
633 && TREE_INT_CST_LOW (expr) == 1
634 && TREE_INT_CST_HIGH (expr) == 0)
635 || (TREE_CODE (expr) == COMPLEX_CST
636 && integer_onep (TREE_REALPART (expr))
637 && integer_zerop (TREE_IMAGPART (expr))));
640 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
641 it contains. Likewise for the corresponding complex constant. */
644 integer_all_onesp (tree expr)
646 int prec;
647 int uns;
649 STRIP_NOPS (expr);
651 if (TREE_CODE (expr) == COMPLEX_CST
652 && integer_all_onesp (TREE_REALPART (expr))
653 && integer_zerop (TREE_IMAGPART (expr)))
654 return 1;
656 else if (TREE_CODE (expr) != INTEGER_CST
657 || TREE_CONSTANT_OVERFLOW (expr))
658 return 0;
660 uns = TREE_UNSIGNED (TREE_TYPE (expr));
661 if (!uns)
662 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
663 && TREE_INT_CST_HIGH (expr) == -1);
665 /* Note that using TYPE_PRECISION here is wrong. We care about the
666 actual bits, not the (arbitrary) range of the type. */
667 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)));
668 if (prec >= HOST_BITS_PER_WIDE_INT)
670 HOST_WIDE_INT high_value;
671 int shift_amount;
673 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
675 if (shift_amount > HOST_BITS_PER_WIDE_INT)
676 /* Can not handle precisions greater than twice the host int size. */
677 abort ();
678 else if (shift_amount == HOST_BITS_PER_WIDE_INT)
679 /* Shifting by the host word size is undefined according to the ANSI
680 standard, so we must handle this as a special case. */
681 high_value = -1;
682 else
683 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
685 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
686 && TREE_INT_CST_HIGH (expr) == high_value);
688 else
689 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
692 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
693 one bit on). */
696 integer_pow2p (tree expr)
698 int prec;
699 HOST_WIDE_INT high, low;
701 STRIP_NOPS (expr);
703 if (TREE_CODE (expr) == COMPLEX_CST
704 && integer_pow2p (TREE_REALPART (expr))
705 && integer_zerop (TREE_IMAGPART (expr)))
706 return 1;
708 if (TREE_CODE (expr) != INTEGER_CST || TREE_CONSTANT_OVERFLOW (expr))
709 return 0;
711 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
712 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
713 high = TREE_INT_CST_HIGH (expr);
714 low = TREE_INT_CST_LOW (expr);
716 /* First clear all bits that are beyond the type's precision in case
717 we've been sign extended. */
719 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
721 else if (prec > HOST_BITS_PER_WIDE_INT)
722 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
723 else
725 high = 0;
726 if (prec < HOST_BITS_PER_WIDE_INT)
727 low &= ~((HOST_WIDE_INT) (-1) << prec);
730 if (high == 0 && low == 0)
731 return 0;
733 return ((high == 0 && (low & (low - 1)) == 0)
734 || (low == 0 && (high & (high - 1)) == 0));
737 /* Return 1 if EXPR is an integer constant other than zero or a
738 complex constant other than zero. */
741 integer_nonzerop (tree expr)
743 STRIP_NOPS (expr);
745 return ((TREE_CODE (expr) == INTEGER_CST
746 && ! TREE_CONSTANT_OVERFLOW (expr)
747 && (TREE_INT_CST_LOW (expr) != 0
748 || TREE_INT_CST_HIGH (expr) != 0))
749 || (TREE_CODE (expr) == COMPLEX_CST
750 && (integer_nonzerop (TREE_REALPART (expr))
751 || integer_nonzerop (TREE_IMAGPART (expr)))));
754 /* Return the power of two represented by a tree node known to be a
755 power of two. */
758 tree_log2 (tree expr)
760 int prec;
761 HOST_WIDE_INT high, low;
763 STRIP_NOPS (expr);
765 if (TREE_CODE (expr) == COMPLEX_CST)
766 return tree_log2 (TREE_REALPART (expr));
768 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
769 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
771 high = TREE_INT_CST_HIGH (expr);
772 low = TREE_INT_CST_LOW (expr);
774 /* First clear all bits that are beyond the type's precision in case
775 we've been sign extended. */
777 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
779 else if (prec > HOST_BITS_PER_WIDE_INT)
780 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
781 else
783 high = 0;
784 if (prec < HOST_BITS_PER_WIDE_INT)
785 low &= ~((HOST_WIDE_INT) (-1) << prec);
788 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
789 : exact_log2 (low));
792 /* Similar, but return the largest integer Y such that 2 ** Y is less
793 than or equal to EXPR. */
796 tree_floor_log2 (tree expr)
798 int prec;
799 HOST_WIDE_INT high, low;
801 STRIP_NOPS (expr);
803 if (TREE_CODE (expr) == COMPLEX_CST)
804 return tree_log2 (TREE_REALPART (expr));
806 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
807 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
809 high = TREE_INT_CST_HIGH (expr);
810 low = TREE_INT_CST_LOW (expr);
812 /* First clear all bits that are beyond the type's precision in case
813 we've been sign extended. Ignore if type's precision hasn't been set
814 since what we are doing is setting it. */
816 if (prec == 2 * HOST_BITS_PER_WIDE_INT || prec == 0)
818 else if (prec > HOST_BITS_PER_WIDE_INT)
819 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
820 else
822 high = 0;
823 if (prec < HOST_BITS_PER_WIDE_INT)
824 low &= ~((HOST_WIDE_INT) (-1) << prec);
827 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
828 : floor_log2 (low));
831 /* Return 1 if EXPR is the real constant zero. */
834 real_zerop (tree expr)
836 STRIP_NOPS (expr);
838 return ((TREE_CODE (expr) == REAL_CST
839 && ! TREE_CONSTANT_OVERFLOW (expr)
840 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0))
841 || (TREE_CODE (expr) == COMPLEX_CST
842 && real_zerop (TREE_REALPART (expr))
843 && real_zerop (TREE_IMAGPART (expr))));
846 /* Return 1 if EXPR is the real constant one in real or complex form. */
849 real_onep (tree expr)
851 STRIP_NOPS (expr);
853 return ((TREE_CODE (expr) == REAL_CST
854 && ! TREE_CONSTANT_OVERFLOW (expr)
855 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1))
856 || (TREE_CODE (expr) == COMPLEX_CST
857 && real_onep (TREE_REALPART (expr))
858 && real_zerop (TREE_IMAGPART (expr))));
861 /* Return 1 if EXPR is the real constant two. */
864 real_twop (tree expr)
866 STRIP_NOPS (expr);
868 return ((TREE_CODE (expr) == REAL_CST
869 && ! TREE_CONSTANT_OVERFLOW (expr)
870 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2))
871 || (TREE_CODE (expr) == COMPLEX_CST
872 && real_twop (TREE_REALPART (expr))
873 && real_zerop (TREE_IMAGPART (expr))));
876 /* Return 1 if EXPR is the real constant minus one. */
879 real_minus_onep (tree expr)
881 STRIP_NOPS (expr);
883 return ((TREE_CODE (expr) == REAL_CST
884 && ! TREE_CONSTANT_OVERFLOW (expr)
885 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1))
886 || (TREE_CODE (expr) == COMPLEX_CST
887 && real_minus_onep (TREE_REALPART (expr))
888 && real_zerop (TREE_IMAGPART (expr))));
891 /* Nonzero if EXP is a constant or a cast of a constant. */
894 really_constant_p (tree exp)
896 /* This is not quite the same as STRIP_NOPS. It does more. */
897 while (TREE_CODE (exp) == NOP_EXPR
898 || TREE_CODE (exp) == CONVERT_EXPR
899 || TREE_CODE (exp) == NON_LVALUE_EXPR)
900 exp = TREE_OPERAND (exp, 0);
901 return TREE_CONSTANT (exp);
904 /* Return first list element whose TREE_VALUE is ELEM.
905 Return 0 if ELEM is not in LIST. */
907 tree
908 value_member (tree elem, tree list)
910 while (list)
912 if (elem == TREE_VALUE (list))
913 return list;
914 list = TREE_CHAIN (list);
916 return NULL_TREE;
919 /* Return first list element whose TREE_PURPOSE is ELEM.
920 Return 0 if ELEM is not in LIST. */
922 tree
923 purpose_member (tree elem, tree list)
925 while (list)
927 if (elem == TREE_PURPOSE (list))
928 return list;
929 list = TREE_CHAIN (list);
931 return NULL_TREE;
934 /* Return first list element whose BINFO_TYPE is ELEM.
935 Return 0 if ELEM is not in LIST. */
937 tree
938 binfo_member (tree elem, tree list)
940 while (list)
942 if (elem == BINFO_TYPE (list))
943 return list;
944 list = TREE_CHAIN (list);
946 return NULL_TREE;
949 /* Return nonzero if ELEM is part of the chain CHAIN. */
952 chain_member (tree elem, tree chain)
954 while (chain)
956 if (elem == chain)
957 return 1;
958 chain = TREE_CHAIN (chain);
961 return 0;
964 /* Return the length of a chain of nodes chained through TREE_CHAIN.
965 We expect a null pointer to mark the end of the chain.
966 This is the Lisp primitive `length'. */
969 list_length (tree t)
971 tree tail;
972 int len = 0;
974 for (tail = t; tail; tail = TREE_CHAIN (tail))
975 len++;
977 return len;
980 /* Returns the number of FIELD_DECLs in TYPE. */
983 fields_length (tree type)
985 tree t = TYPE_FIELDS (type);
986 int count = 0;
988 for (; t; t = TREE_CHAIN (t))
989 if (TREE_CODE (t) == FIELD_DECL)
990 ++count;
992 return count;
995 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
996 by modifying the last node in chain 1 to point to chain 2.
997 This is the Lisp primitive `nconc'. */
999 tree
1000 chainon (tree op1, tree op2)
1002 tree t1;
1004 if (!op1)
1005 return op2;
1006 if (!op2)
1007 return op1;
1009 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
1010 continue;
1011 TREE_CHAIN (t1) = op2;
1013 #ifdef ENABLE_TREE_CHECKING
1015 tree t2;
1016 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
1017 if (t2 == t1)
1018 abort (); /* Circularity created. */
1020 #endif
1022 return op1;
1025 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
1027 tree
1028 tree_last (tree chain)
1030 tree next;
1031 if (chain)
1032 while ((next = TREE_CHAIN (chain)))
1033 chain = next;
1034 return chain;
1037 /* Reverse the order of elements in the chain T,
1038 and return the new head of the chain (old last element). */
1040 tree
1041 nreverse (tree t)
1043 tree prev = 0, decl, next;
1044 for (decl = t; decl; decl = next)
1046 next = TREE_CHAIN (decl);
1047 TREE_CHAIN (decl) = prev;
1048 prev = decl;
1050 return prev;
1053 /* Return a newly created TREE_LIST node whose
1054 purpose and value fields are PARM and VALUE. */
1056 tree
1057 build_tree_list (tree parm, tree value)
1059 tree t = make_node (TREE_LIST);
1060 TREE_PURPOSE (t) = parm;
1061 TREE_VALUE (t) = value;
1062 return t;
1065 /* Return a newly created TREE_LIST node whose
1066 purpose and value fields are PURPOSE and VALUE
1067 and whose TREE_CHAIN is CHAIN. */
1069 tree
1070 tree_cons (tree purpose, tree value, tree chain)
1072 tree node;
1074 node = ggc_alloc_tree (sizeof (struct tree_list));
1076 memset (node, 0, sizeof (struct tree_common));
1078 #ifdef GATHER_STATISTICS
1079 tree_node_counts[(int) x_kind]++;
1080 tree_node_sizes[(int) x_kind] += sizeof (struct tree_list);
1081 #endif
1083 TREE_SET_CODE (node, TREE_LIST);
1084 TREE_CHAIN (node) = chain;
1085 TREE_PURPOSE (node) = purpose;
1086 TREE_VALUE (node) = value;
1087 return node;
1090 /* Return the first expression in a sequence of COMPOUND_EXPRs. */
1092 tree
1093 expr_first (tree expr)
1095 if (expr == NULL_TREE)
1096 return expr;
1097 while (TREE_CODE (expr) == COMPOUND_EXPR)
1098 expr = TREE_OPERAND (expr, 0);
1099 return expr;
1102 /* Return the last expression in a sequence of COMPOUND_EXPRs. */
1104 tree
1105 expr_last (tree expr)
1107 if (expr == NULL_TREE)
1108 return expr;
1109 while (TREE_CODE (expr) == COMPOUND_EXPR)
1110 expr = TREE_OPERAND (expr, 1);
1111 return expr;
1114 /* Return the number of subexpressions in a sequence of COMPOUND_EXPRs. */
1117 expr_length (tree expr)
1119 int len = 0;
1121 if (expr == NULL_TREE)
1122 return 0;
1123 for (; TREE_CODE (expr) == COMPOUND_EXPR; expr = TREE_OPERAND (expr, 1))
1124 len += expr_length (TREE_OPERAND (expr, 0));
1125 ++len;
1126 return len;
1129 /* Return the size nominally occupied by an object of type TYPE
1130 when it resides in memory. The value is measured in units of bytes,
1131 and its data type is that normally used for type sizes
1132 (which is the first type created by make_signed_type or
1133 make_unsigned_type). */
1135 tree
1136 size_in_bytes (tree type)
1138 tree t;
1140 if (type == error_mark_node)
1141 return integer_zero_node;
1143 type = TYPE_MAIN_VARIANT (type);
1144 t = TYPE_SIZE_UNIT (type);
1146 if (t == 0)
1148 (*lang_hooks.types.incomplete_type_error) (NULL_TREE, type);
1149 return size_zero_node;
1152 if (TREE_CODE (t) == INTEGER_CST)
1153 force_fit_type (t, 0);
1155 return t;
1158 /* Return the size of TYPE (in bytes) as a wide integer
1159 or return -1 if the size can vary or is larger than an integer. */
1161 HOST_WIDE_INT
1162 int_size_in_bytes (tree type)
1164 tree t;
1166 if (type == error_mark_node)
1167 return 0;
1169 type = TYPE_MAIN_VARIANT (type);
1170 t = TYPE_SIZE_UNIT (type);
1171 if (t == 0
1172 || TREE_CODE (t) != INTEGER_CST
1173 || TREE_OVERFLOW (t)
1174 || TREE_INT_CST_HIGH (t) != 0
1175 /* If the result would appear negative, it's too big to represent. */
1176 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
1177 return -1;
1179 return TREE_INT_CST_LOW (t);
1182 /* Return the bit position of FIELD, in bits from the start of the record.
1183 This is a tree of type bitsizetype. */
1185 tree
1186 bit_position (tree field)
1188 return bit_from_pos (DECL_FIELD_OFFSET (field),
1189 DECL_FIELD_BIT_OFFSET (field));
1192 /* Likewise, but return as an integer. Abort if it cannot be represented
1193 in that way (since it could be a signed value, we don't have the option
1194 of returning -1 like int_size_in_byte can. */
1196 HOST_WIDE_INT
1197 int_bit_position (tree field)
1199 return tree_low_cst (bit_position (field), 0);
1202 /* Return the byte position of FIELD, in bytes from the start of the record.
1203 This is a tree of type sizetype. */
1205 tree
1206 byte_position (tree field)
1208 return byte_from_pos (DECL_FIELD_OFFSET (field),
1209 DECL_FIELD_BIT_OFFSET (field));
1212 /* Likewise, but return as an integer. Abort if it cannot be represented
1213 in that way (since it could be a signed value, we don't have the option
1214 of returning -1 like int_size_in_byte can. */
1216 HOST_WIDE_INT
1217 int_byte_position (tree field)
1219 return tree_low_cst (byte_position (field), 0);
1222 /* Return the strictest alignment, in bits, that T is known to have. */
1224 unsigned int
1225 expr_align (tree t)
1227 unsigned int align0, align1;
1229 switch (TREE_CODE (t))
1231 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
1232 /* If we have conversions, we know that the alignment of the
1233 object must meet each of the alignments of the types. */
1234 align0 = expr_align (TREE_OPERAND (t, 0));
1235 align1 = TYPE_ALIGN (TREE_TYPE (t));
1236 return MAX (align0, align1);
1238 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
1239 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
1240 case WITH_RECORD_EXPR: case CLEANUP_POINT_EXPR: case UNSAVE_EXPR:
1241 /* These don't change the alignment of an object. */
1242 return expr_align (TREE_OPERAND (t, 0));
1244 case COND_EXPR:
1245 /* The best we can do is say that the alignment is the least aligned
1246 of the two arms. */
1247 align0 = expr_align (TREE_OPERAND (t, 1));
1248 align1 = expr_align (TREE_OPERAND (t, 2));
1249 return MIN (align0, align1);
1251 case LABEL_DECL: case CONST_DECL:
1252 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
1253 if (DECL_ALIGN (t) != 0)
1254 return DECL_ALIGN (t);
1255 break;
1257 case FUNCTION_DECL:
1258 return FUNCTION_BOUNDARY;
1260 default:
1261 break;
1264 /* Otherwise take the alignment from that of the type. */
1265 return TYPE_ALIGN (TREE_TYPE (t));
1268 /* Return, as a tree node, the number of elements for TYPE (which is an
1269 ARRAY_TYPE) minus one. This counts only elements of the top array. */
1271 tree
1272 array_type_nelts (tree type)
1274 tree index_type, min, max;
1276 /* If they did it with unspecified bounds, then we should have already
1277 given an error about it before we got here. */
1278 if (! TYPE_DOMAIN (type))
1279 return error_mark_node;
1281 index_type = TYPE_DOMAIN (type);
1282 min = TYPE_MIN_VALUE (index_type);
1283 max = TYPE_MAX_VALUE (index_type);
1285 return (integer_zerop (min)
1286 ? max
1287 : fold (build (MINUS_EXPR, TREE_TYPE (max), max, min)));
1290 /* Return nonzero if arg is static -- a reference to an object in
1291 static storage. This is not the same as the C meaning of `static'. */
1294 staticp (tree arg)
1296 switch (TREE_CODE (arg))
1298 case FUNCTION_DECL:
1299 /* Nested functions aren't static, since taking their address
1300 involves a trampoline. */
1301 return ((decl_function_context (arg) == 0 || DECL_NO_STATIC_CHAIN (arg))
1302 && ! DECL_NON_ADDR_CONST_P (arg));
1304 case VAR_DECL:
1305 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
1306 && ! DECL_THREAD_LOCAL (arg)
1307 && ! DECL_NON_ADDR_CONST_P (arg));
1309 case CONSTRUCTOR:
1310 return TREE_STATIC (arg);
1312 case LABEL_DECL:
1313 case STRING_CST:
1314 return 1;
1316 /* If we are referencing a bitfield, we can't evaluate an
1317 ADDR_EXPR at compile time and so it isn't a constant. */
1318 case COMPONENT_REF:
1319 return (! DECL_BIT_FIELD (TREE_OPERAND (arg, 1))
1320 && staticp (TREE_OPERAND (arg, 0)));
1322 case BIT_FIELD_REF:
1323 return 0;
1325 #if 0
1326 /* This case is technically correct, but results in setting
1327 TREE_CONSTANT on ADDR_EXPRs that cannot be evaluated at
1328 compile time. */
1329 case INDIRECT_REF:
1330 return TREE_CONSTANT (TREE_OPERAND (arg, 0));
1331 #endif
1333 case ARRAY_REF:
1334 case ARRAY_RANGE_REF:
1335 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
1336 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
1337 return staticp (TREE_OPERAND (arg, 0));
1339 default:
1340 if ((unsigned int) TREE_CODE (arg)
1341 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
1342 return (*lang_hooks.staticp) (arg);
1343 else
1344 return 0;
1348 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
1349 Do this to any expression which may be used in more than one place,
1350 but must be evaluated only once.
1352 Normally, expand_expr would reevaluate the expression each time.
1353 Calling save_expr produces something that is evaluated and recorded
1354 the first time expand_expr is called on it. Subsequent calls to
1355 expand_expr just reuse the recorded value.
1357 The call to expand_expr that generates code that actually computes
1358 the value is the first call *at compile time*. Subsequent calls
1359 *at compile time* generate code to use the saved value.
1360 This produces correct result provided that *at run time* control
1361 always flows through the insns made by the first expand_expr
1362 before reaching the other places where the save_expr was evaluated.
1363 You, the caller of save_expr, must make sure this is so.
1365 Constants, and certain read-only nodes, are returned with no
1366 SAVE_EXPR because that is safe. Expressions containing placeholders
1367 are not touched; see tree.def for an explanation of what these
1368 are used for. */
1370 tree
1371 save_expr (tree expr)
1373 tree t = fold (expr);
1374 tree inner;
1376 /* If the tree evaluates to a constant, then we don't want to hide that
1377 fact (i.e. this allows further folding, and direct checks for constants).
1378 However, a read-only object that has side effects cannot be bypassed.
1379 Since it is no problem to reevaluate literals, we just return the
1380 literal node. */
1381 inner = skip_simple_arithmetic (t);
1382 if (TREE_CONSTANT (inner)
1383 || (TREE_READONLY (inner) && ! TREE_SIDE_EFFECTS (inner))
1384 || TREE_CODE (inner) == SAVE_EXPR
1385 || TREE_CODE (inner) == ERROR_MARK)
1386 return t;
1388 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
1389 it means that the size or offset of some field of an object depends on
1390 the value within another field.
1392 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
1393 and some variable since it would then need to be both evaluated once and
1394 evaluated more than once. Front-ends must assure this case cannot
1395 happen by surrounding any such subexpressions in their own SAVE_EXPR
1396 and forcing evaluation at the proper time. */
1397 if (contains_placeholder_p (inner))
1398 return t;
1400 t = build (SAVE_EXPR, TREE_TYPE (expr), t, current_function_decl, NULL_TREE);
1402 /* This expression might be placed ahead of a jump to ensure that the
1403 value was computed on both sides of the jump. So make sure it isn't
1404 eliminated as dead. */
1405 TREE_SIDE_EFFECTS (t) = 1;
1406 TREE_READONLY (t) = 1;
1407 return t;
1410 /* Look inside EXPR and into any simple arithmetic operations. Return
1411 the innermost non-arithmetic node. */
1413 tree
1414 skip_simple_arithmetic (tree expr)
1416 tree inner;
1418 /* We don't care about whether this can be used as an lvalue in this
1419 context. */
1420 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
1421 expr = TREE_OPERAND (expr, 0);
1423 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
1424 a constant, it will be more efficient to not make another SAVE_EXPR since
1425 it will allow better simplification and GCSE will be able to merge the
1426 computations if they actually occur. */
1427 inner = expr;
1428 while (1)
1430 if (TREE_CODE_CLASS (TREE_CODE (inner)) == '1')
1431 inner = TREE_OPERAND (inner, 0);
1432 else if (TREE_CODE_CLASS (TREE_CODE (inner)) == '2')
1434 if (TREE_CONSTANT (TREE_OPERAND (inner, 1)))
1435 inner = TREE_OPERAND (inner, 0);
1436 else if (TREE_CONSTANT (TREE_OPERAND (inner, 0)))
1437 inner = TREE_OPERAND (inner, 1);
1438 else
1439 break;
1441 else
1442 break;
1445 return inner;
1448 /* Return TRUE if EXPR is a SAVE_EXPR or wraps simple arithmetic around a
1449 SAVE_EXPR. Return FALSE otherwise. */
1451 bool
1452 saved_expr_p (tree expr)
1454 return TREE_CODE (skip_simple_arithmetic (expr)) == SAVE_EXPR;
1457 /* Arrange for an expression to be expanded multiple independent
1458 times. This is useful for cleanup actions, as the backend can
1459 expand them multiple times in different places. */
1461 tree
1462 unsave_expr (tree expr)
1464 tree t;
1466 /* If this is already protected, no sense in protecting it again. */
1467 if (TREE_CODE (expr) == UNSAVE_EXPR)
1468 return expr;
1470 t = build1 (UNSAVE_EXPR, TREE_TYPE (expr), expr);
1471 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (expr);
1472 return t;
1475 /* Returns the index of the first non-tree operand for CODE, or the number
1476 of operands if all are trees. */
1479 first_rtl_op (enum tree_code code)
1481 switch (code)
1483 case SAVE_EXPR:
1484 return 2;
1485 case GOTO_SUBROUTINE_EXPR:
1486 case RTL_EXPR:
1487 return 0;
1488 case WITH_CLEANUP_EXPR:
1489 return 2;
1490 default:
1491 return TREE_CODE_LENGTH (code);
1495 /* Return which tree structure is used by T. */
1497 enum tree_node_structure_enum
1498 tree_node_structure (tree t)
1500 enum tree_code code = TREE_CODE (t);
1502 switch (TREE_CODE_CLASS (code))
1504 case 'd': return TS_DECL;
1505 case 't': return TS_TYPE;
1506 case 'b': return TS_BLOCK;
1507 case 'r': case '<': case '1': case '2': case 'e': case 's':
1508 return TS_EXP;
1509 default: /* 'c' and 'x' */
1510 break;
1512 switch (code)
1514 /* 'c' cases. */
1515 case INTEGER_CST: return TS_INT_CST;
1516 case REAL_CST: return TS_REAL_CST;
1517 case COMPLEX_CST: return TS_COMPLEX;
1518 case VECTOR_CST: return TS_VECTOR;
1519 case STRING_CST: return TS_STRING;
1520 /* 'x' cases. */
1521 case ERROR_MARK: return TS_COMMON;
1522 case IDENTIFIER_NODE: return TS_IDENTIFIER;
1523 case TREE_LIST: return TS_LIST;
1524 case TREE_VEC: return TS_VEC;
1525 case PHI_NODE: return TS_PHI_NODE;
1526 case EPHI_NODE: return TS_EPHI_NODE;
1527 case EUSE_NODE: return TS_EUSE_NODE;
1528 case ELEFT_NODE: return TS_EREF_NODE;
1529 case EKILL_NODE: return TS_EREF_NODE;
1530 case EEXIT_NODE: return TS_EREF_NODE;
1531 case SSA_NAME: return TS_SSA_NAME;
1532 case PLACEHOLDER_EXPR: return TS_COMMON;
1534 default:
1535 abort ();
1539 /* Perform any modifications to EXPR required when it is unsaved. Does
1540 not recurse into EXPR's subtrees. */
1542 void
1543 unsave_expr_1 (tree expr)
1545 switch (TREE_CODE (expr))
1547 case SAVE_EXPR:
1548 if (! SAVE_EXPR_PERSISTENT_P (expr))
1549 SAVE_EXPR_RTL (expr) = 0;
1550 break;
1552 case TARGET_EXPR:
1553 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
1554 It's OK for this to happen if it was part of a subtree that
1555 isn't immediately expanded, such as operand 2 of another
1556 TARGET_EXPR. */
1557 if (TREE_OPERAND (expr, 1))
1558 break;
1560 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
1561 TREE_OPERAND (expr, 3) = NULL_TREE;
1562 break;
1564 case RTL_EXPR:
1565 /* I don't yet know how to emit a sequence multiple times. */
1566 if (RTL_EXPR_SEQUENCE (expr) != 0)
1567 abort ();
1568 break;
1570 default:
1571 break;
1575 /* Return 0 if it is safe to evaluate EXPR multiple times,
1576 return 1 if it is safe if EXPR is unsaved afterward, or
1577 return 2 if it is completely unsafe.
1579 This assumes that CALL_EXPRs and TARGET_EXPRs are never replicated in
1580 an expression tree, so that it safe to unsave them and the surrounding
1581 context will be correct.
1583 SAVE_EXPRs basically *only* appear replicated in an expression tree,
1584 occasionally across the whole of a function. It is therefore only
1585 safe to unsave a SAVE_EXPR if you know that all occurrences appear
1586 below the UNSAVE_EXPR.
1588 RTL_EXPRs consume their rtl during evaluation. It is therefore
1589 never possible to unsave them. */
1592 unsafe_for_reeval (tree expr)
1594 int unsafeness = 0;
1595 enum tree_code code;
1596 int i, tmp, tmp2;
1597 tree exp;
1598 int first_rtl;
1600 if (expr == NULL_TREE)
1601 return 1;
1603 code = TREE_CODE (expr);
1604 first_rtl = first_rtl_op (code);
1606 switch (code)
1608 case SAVE_EXPR:
1609 case RTL_EXPR:
1610 return 2;
1612 /* A label can only be emitted once. */
1613 case LABEL_EXPR:
1614 return 1;
1616 case BIND_EXPR:
1617 unsafeness = 1;
1618 break;
1620 case TREE_LIST:
1621 for (exp = expr; exp != 0; exp = TREE_CHAIN (exp))
1623 tmp = unsafe_for_reeval (TREE_VALUE (exp));
1624 unsafeness = MAX (tmp, unsafeness);
1627 return unsafeness;
1629 case CALL_EXPR:
1630 tmp2 = unsafe_for_reeval (TREE_OPERAND (expr, 0));
1631 tmp = unsafe_for_reeval (TREE_OPERAND (expr, 1));
1632 return MAX (MAX (tmp, 1), tmp2);
1634 case TARGET_EXPR:
1635 unsafeness = 1;
1636 break;
1638 default:
1639 tmp = (*lang_hooks.unsafe_for_reeval) (expr);
1640 if (tmp >= 0)
1641 return tmp;
1642 break;
1645 switch (TREE_CODE_CLASS (code))
1647 case 'c': /* a constant */
1648 case 't': /* a type node */
1649 case 'x': /* something random, like an identifier or an ERROR_MARK. */
1650 case 'd': /* A decl node */
1651 case 'b': /* A block node */
1652 return 0;
1654 case 'e': /* an expression */
1655 case 'r': /* a reference */
1656 case 's': /* an expression with side effects */
1657 case '<': /* a comparison expression */
1658 case '2': /* a binary arithmetic expression */
1659 case '1': /* a unary arithmetic expression */
1660 for (i = first_rtl - 1; i >= 0; i--)
1662 tmp = unsafe_for_reeval (TREE_OPERAND (expr, i));
1663 unsafeness = MAX (tmp, unsafeness);
1666 return unsafeness;
1668 default:
1669 return 2;
1673 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
1674 or offset that depends on a field within a record. */
1676 bool
1677 contains_placeholder_p (tree exp)
1679 enum tree_code code;
1680 int result;
1682 if (!exp)
1683 return 0;
1685 /* If we have a WITH_RECORD_EXPR, it "cancels" any PLACEHOLDER_EXPR
1686 in it since it is supplying a value for it. */
1687 code = TREE_CODE (exp);
1688 if (code == WITH_RECORD_EXPR)
1689 return 0;
1690 else if (code == PLACEHOLDER_EXPR)
1691 return 1;
1693 switch (TREE_CODE_CLASS (code))
1695 case 'r':
1696 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
1697 position computations since they will be converted into a
1698 WITH_RECORD_EXPR involving the reference, which will assume
1699 here will be valid. */
1700 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
1702 case 'x':
1703 if (code == TREE_LIST)
1704 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
1705 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
1706 break;
1708 case '1':
1709 case '2': case '<':
1710 case 'e':
1711 switch (code)
1713 case COMPOUND_EXPR:
1714 /* Ignoring the first operand isn't quite right, but works best. */
1715 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
1717 case RTL_EXPR:
1718 case CONSTRUCTOR:
1719 return 0;
1721 case COND_EXPR:
1722 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
1723 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
1724 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
1726 case SAVE_EXPR:
1727 /* If we already know this doesn't have a placeholder, don't
1728 check again. */
1729 if (SAVE_EXPR_NOPLACEHOLDER (exp) || SAVE_EXPR_RTL (exp) != 0)
1730 return 0;
1732 SAVE_EXPR_NOPLACEHOLDER (exp) = 1;
1733 result = CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
1734 if (result)
1735 SAVE_EXPR_NOPLACEHOLDER (exp) = 0;
1737 return result;
1739 case CALL_EXPR:
1740 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
1742 default:
1743 break;
1746 switch (TREE_CODE_LENGTH (code))
1748 case 1:
1749 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
1750 case 2:
1751 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
1752 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
1753 default:
1754 return 0;
1757 default:
1758 return 0;
1760 return 0;
1763 /* Return 1 if any part of the computation of TYPE involves a PLACEHOLDER_EXPR.
1764 This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and field
1765 positions. */
1767 bool
1768 type_contains_placeholder_p (tree type)
1770 /* If the size contains a placeholder or the parent type (component type in
1771 the case of arrays) type involves a placeholder, this type does. */
1772 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
1773 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
1774 || (TREE_TYPE (type) != 0
1775 && type_contains_placeholder_p (TREE_TYPE (type))))
1776 return 1;
1778 /* Now do type-specific checks. Note that the last part of the check above
1779 greatly limits what we have to do below. */
1780 switch (TREE_CODE (type))
1782 case VOID_TYPE:
1783 case COMPLEX_TYPE:
1784 case VECTOR_TYPE:
1785 case ENUMERAL_TYPE:
1786 case BOOLEAN_TYPE:
1787 case CHAR_TYPE:
1788 case POINTER_TYPE:
1789 case OFFSET_TYPE:
1790 case REFERENCE_TYPE:
1791 case METHOD_TYPE:
1792 case FILE_TYPE:
1793 case FUNCTION_TYPE:
1794 return 0;
1796 case INTEGER_TYPE:
1797 case REAL_TYPE:
1798 /* Here we just check the bounds. */
1799 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
1800 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
1802 case ARRAY_TYPE:
1803 case SET_TYPE:
1804 /* We're already checked the component type (TREE_TYPE), so just check
1805 the index type. */
1806 return type_contains_placeholder_p (TYPE_DOMAIN (type));
1808 case RECORD_TYPE:
1809 case UNION_TYPE:
1810 case QUAL_UNION_TYPE:
1812 static tree seen_types = 0;
1813 tree field;
1814 bool ret = 0;
1816 /* We have to be careful here that we don't end up in infinite
1817 recursions due to a field of a type being a pointer to that type
1818 or to a mutually-recursive type. So we store a list of record
1819 types that we've seen and see if this type is in them. To save
1820 memory, we don't use a list for just one type. Here we check
1821 whether we've seen this type before and store it if not. */
1822 if (seen_types == 0)
1823 seen_types = type;
1824 else if (TREE_CODE (seen_types) != TREE_LIST)
1826 if (seen_types == type)
1827 return 0;
1829 seen_types = tree_cons (NULL_TREE, type,
1830 build_tree_list (NULL_TREE, seen_types));
1832 else
1834 if (value_member (type, seen_types) != 0)
1835 return 0;
1837 seen_types = tree_cons (NULL_TREE, type, seen_types);
1840 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1841 if (TREE_CODE (field) == FIELD_DECL
1842 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
1843 || (TREE_CODE (type) == QUAL_UNION_TYPE
1844 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
1845 || type_contains_placeholder_p (TREE_TYPE (field))))
1847 ret = true;
1848 break;
1851 /* Now remove us from seen_types and return the result. */
1852 if (seen_types == type)
1853 seen_types = 0;
1854 else
1855 seen_types = TREE_CHAIN (seen_types);
1857 return ret;
1860 default:
1861 abort ();
1865 /* Return 1 if EXP contains any expressions that produce cleanups for an
1866 outer scope to deal with. Used by fold. */
1869 has_cleanups (tree exp)
1871 int i, nops, cmp;
1873 if (! TREE_SIDE_EFFECTS (exp))
1874 return 0;
1876 switch (TREE_CODE (exp))
1878 case TARGET_EXPR:
1879 case GOTO_SUBROUTINE_EXPR:
1880 case WITH_CLEANUP_EXPR:
1881 return 1;
1883 case CLEANUP_POINT_EXPR:
1884 return 0;
1886 case CALL_EXPR:
1887 for (exp = TREE_OPERAND (exp, 1); exp; exp = TREE_CHAIN (exp))
1889 cmp = has_cleanups (TREE_VALUE (exp));
1890 if (cmp)
1891 return cmp;
1893 return 0;
1895 default:
1896 break;
1899 /* This general rule works for most tree codes. All exceptions should be
1900 handled above. If this is a language-specific tree code, we can't
1901 trust what might be in the operand, so say we don't know
1902 the situation. */
1903 if ((int) TREE_CODE (exp) >= (int) LAST_AND_UNUSED_TREE_CODE)
1904 return -1;
1906 nops = first_rtl_op (TREE_CODE (exp));
1907 for (i = 0; i < nops; i++)
1908 if (TREE_OPERAND (exp, i) != 0)
1910 int type = TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (exp, i)));
1911 if (type == 'e' || type == '<' || type == '1' || type == '2'
1912 || type == 'r' || type == 's')
1914 cmp = has_cleanups (TREE_OPERAND (exp, i));
1915 if (cmp)
1916 return cmp;
1920 return 0;
1923 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
1924 return a tree with all occurrences of references to F in a
1925 PLACEHOLDER_EXPR replaced by R. Note that we assume here that EXP
1926 contains only arithmetic expressions or a CALL_EXPR with a
1927 PLACEHOLDER_EXPR occurring only in its arglist. */
1929 tree
1930 substitute_in_expr (tree exp, tree f, tree r)
1932 enum tree_code code = TREE_CODE (exp);
1933 tree op0, op1, op2;
1934 tree new;
1935 tree inner;
1937 switch (TREE_CODE_CLASS (code))
1939 case 'c':
1940 case 'd':
1941 return exp;
1943 case 'x':
1944 if (code == PLACEHOLDER_EXPR)
1945 return exp;
1946 else if (code == TREE_LIST)
1948 op0 = (TREE_CHAIN (exp) == 0
1949 ? 0 : substitute_in_expr (TREE_CHAIN (exp), f, r));
1950 op1 = substitute_in_expr (TREE_VALUE (exp), f, r);
1951 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
1952 return exp;
1954 return tree_cons (TREE_PURPOSE (exp), op1, op0);
1957 abort ();
1959 case '1':
1960 case '2':
1961 case '<':
1962 case 'e':
1963 switch (TREE_CODE_LENGTH (code))
1965 case 1:
1966 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
1967 if (op0 == TREE_OPERAND (exp, 0))
1968 return exp;
1970 if (code == NON_LVALUE_EXPR)
1971 return op0;
1973 new = fold (build1 (code, TREE_TYPE (exp), op0));
1974 break;
1976 case 2:
1977 /* An RTL_EXPR cannot contain a PLACEHOLDER_EXPR; a CONSTRUCTOR
1978 could, but we don't support it. */
1979 if (code == RTL_EXPR)
1980 return exp;
1981 else if (code == CONSTRUCTOR)
1982 abort ();
1984 op0 = TREE_OPERAND (exp, 0);
1985 op1 = TREE_OPERAND (exp, 1);
1986 if (CONTAINS_PLACEHOLDER_P (op0))
1987 op0 = substitute_in_expr (op0, f, r);
1988 if (CONTAINS_PLACEHOLDER_P (op1))
1989 op1 = substitute_in_expr (op1, f, r);
1991 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
1992 return exp;
1994 new = fold (build (code, TREE_TYPE (exp), op0, op1));
1995 break;
1997 case 3:
1998 /* It cannot be that anything inside a SAVE_EXPR contains a
1999 PLACEHOLDER_EXPR. */
2000 if (code == SAVE_EXPR)
2001 return exp;
2003 else if (code == CALL_EXPR)
2005 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
2006 if (op1 == TREE_OPERAND (exp, 1))
2007 return exp;
2009 return build (code, TREE_TYPE (exp),
2010 TREE_OPERAND (exp, 0), op1, NULL_TREE);
2013 else if (code != COND_EXPR)
2014 abort ();
2016 op0 = TREE_OPERAND (exp, 0);
2017 op1 = TREE_OPERAND (exp, 1);
2018 op2 = TREE_OPERAND (exp, 2);
2020 if (CONTAINS_PLACEHOLDER_P (op0))
2021 op0 = substitute_in_expr (op0, f, r);
2022 if (CONTAINS_PLACEHOLDER_P (op1))
2023 op1 = substitute_in_expr (op1, f, r);
2024 if (CONTAINS_PLACEHOLDER_P (op2))
2025 op2 = substitute_in_expr (op2, f, r);
2027 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2028 && op2 == TREE_OPERAND (exp, 2))
2029 return exp;
2031 new = fold (build (code, TREE_TYPE (exp), op0, op1, op2));
2032 break;
2034 default:
2035 abort ();
2038 break;
2040 case 'r':
2041 switch (code)
2043 case COMPONENT_REF:
2044 /* If this expression is getting a value from a PLACEHOLDER_EXPR
2045 and it is the right field, replace it with R. */
2046 for (inner = TREE_OPERAND (exp, 0);
2047 TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
2048 inner = TREE_OPERAND (inner, 0))
2050 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2051 && TREE_OPERAND (exp, 1) == f)
2052 return r;
2054 /* If this expression hasn't been completed let, leave it
2055 alone. */
2056 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2057 && TREE_TYPE (inner) == 0)
2058 return exp;
2060 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2061 if (op0 == TREE_OPERAND (exp, 0))
2062 return exp;
2064 new = fold (build (code, TREE_TYPE (exp), op0,
2065 TREE_OPERAND (exp, 1)));
2066 break;
2068 case BIT_FIELD_REF:
2069 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2070 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
2071 op2 = substitute_in_expr (TREE_OPERAND (exp, 2), f, r);
2072 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2073 && op2 == TREE_OPERAND (exp, 2))
2074 return exp;
2076 new = fold (build (code, TREE_TYPE (exp), op0, op1, op2));
2077 break;
2079 case INDIRECT_REF:
2080 case BUFFER_REF:
2081 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2082 if (op0 == TREE_OPERAND (exp, 0))
2083 return exp;
2085 new = fold (build1 (code, TREE_TYPE (exp), op0));
2086 break;
2088 default:
2089 abort ();
2091 break;
2093 default:
2094 abort ();
2097 TREE_READONLY (new) = TREE_READONLY (exp);
2098 return new;
2101 /* Stabilize a reference so that we can use it any number of times
2102 without causing its operands to be evaluated more than once.
2103 Returns the stabilized reference. This works by means of save_expr,
2104 so see the caveats in the comments about save_expr.
2106 Also allows conversion expressions whose operands are references.
2107 Any other kind of expression is returned unchanged. */
2109 tree
2110 stabilize_reference (tree ref)
2112 tree result;
2113 enum tree_code code = TREE_CODE (ref);
2115 switch (code)
2117 case VAR_DECL:
2118 case PARM_DECL:
2119 case RESULT_DECL:
2120 /* No action is needed in this case. */
2121 return ref;
2123 case NOP_EXPR:
2124 case CONVERT_EXPR:
2125 case FLOAT_EXPR:
2126 case FIX_TRUNC_EXPR:
2127 case FIX_FLOOR_EXPR:
2128 case FIX_ROUND_EXPR:
2129 case FIX_CEIL_EXPR:
2130 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2131 break;
2133 case INDIRECT_REF:
2134 result = build_nt (INDIRECT_REF,
2135 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2136 break;
2138 case COMPONENT_REF:
2139 result = build_nt (COMPONENT_REF,
2140 stabilize_reference (TREE_OPERAND (ref, 0)),
2141 TREE_OPERAND (ref, 1));
2142 break;
2144 case BIT_FIELD_REF:
2145 result = build_nt (BIT_FIELD_REF,
2146 stabilize_reference (TREE_OPERAND (ref, 0)),
2147 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2148 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2149 break;
2151 case ARRAY_REF:
2152 result = build_nt (ARRAY_REF,
2153 stabilize_reference (TREE_OPERAND (ref, 0)),
2154 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2155 break;
2157 case ARRAY_RANGE_REF:
2158 result = build_nt (ARRAY_RANGE_REF,
2159 stabilize_reference (TREE_OPERAND (ref, 0)),
2160 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2161 break;
2163 case COMPOUND_EXPR:
2164 /* We cannot wrap the first expression in a SAVE_EXPR, as then
2165 it wouldn't be ignored. This matters when dealing with
2166 volatiles. */
2167 return stabilize_reference_1 (ref);
2169 case RTL_EXPR:
2170 result = build1 (INDIRECT_REF, TREE_TYPE (ref),
2171 save_expr (build1 (ADDR_EXPR,
2172 build_pointer_type (TREE_TYPE (ref)),
2173 ref)));
2174 break;
2176 /* If arg isn't a kind of lvalue we recognize, make no change.
2177 Caller should recognize the error for an invalid lvalue. */
2178 default:
2179 return ref;
2181 case ERROR_MARK:
2182 return error_mark_node;
2185 TREE_TYPE (result) = TREE_TYPE (ref);
2186 TREE_READONLY (result) = TREE_READONLY (ref);
2187 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
2188 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
2190 return result;
2193 /* Subroutine of stabilize_reference; this is called for subtrees of
2194 references. Any expression with side-effects must be put in a SAVE_EXPR
2195 to ensure that it is only evaluated once.
2197 We don't put SAVE_EXPR nodes around everything, because assigning very
2198 simple expressions to temporaries causes us to miss good opportunities
2199 for optimizations. Among other things, the opportunity to fold in the
2200 addition of a constant into an addressing mode often gets lost, e.g.
2201 "y[i+1] += x;". In general, we take the approach that we should not make
2202 an assignment unless we are forced into it - i.e., that any non-side effect
2203 operator should be allowed, and that cse should take care of coalescing
2204 multiple utterances of the same expression should that prove fruitful. */
2206 tree
2207 stabilize_reference_1 (tree e)
2209 tree result;
2210 enum tree_code code = TREE_CODE (e);
2212 /* We cannot ignore const expressions because it might be a reference
2213 to a const array but whose index contains side-effects. But we can
2214 ignore things that are actual constant or that already have been
2215 handled by this function. */
2217 if (TREE_CONSTANT (e) || code == SAVE_EXPR)
2218 return e;
2220 switch (TREE_CODE_CLASS (code))
2222 case 'x':
2223 case 't':
2224 case 'd':
2225 case 'b':
2226 case '<':
2227 case 's':
2228 case 'e':
2229 case 'r':
2230 /* If the expression has side-effects, then encase it in a SAVE_EXPR
2231 so that it will only be evaluated once. */
2232 /* The reference (r) and comparison (<) classes could be handled as
2233 below, but it is generally faster to only evaluate them once. */
2234 if (TREE_SIDE_EFFECTS (e))
2235 return save_expr (e);
2236 return e;
2238 case 'c':
2239 /* Constants need no processing. In fact, we should never reach
2240 here. */
2241 return e;
2243 case '2':
2244 /* Division is slow and tends to be compiled with jumps,
2245 especially the division by powers of 2 that is often
2246 found inside of an array reference. So do it just once. */
2247 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
2248 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
2249 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
2250 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
2251 return save_expr (e);
2252 /* Recursively stabilize each operand. */
2253 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
2254 stabilize_reference_1 (TREE_OPERAND (e, 1)));
2255 break;
2257 case '1':
2258 /* Recursively stabilize each operand. */
2259 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
2260 break;
2262 default:
2263 abort ();
2266 TREE_TYPE (result) = TREE_TYPE (e);
2267 TREE_READONLY (result) = TREE_READONLY (e);
2268 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
2269 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
2271 return result;
2274 /* Low-level constructors for expressions. */
2276 /* Build an expression of code CODE, data type TYPE,
2277 and operands as specified by the arguments ARG1 and following arguments.
2278 Expressions and reference nodes can be created this way.
2279 Constants, decls, types and misc nodes cannot be. */
2281 tree
2282 build (enum tree_code code, tree tt, ...)
2284 tree t;
2285 int length;
2286 int i;
2287 int fro;
2288 int constant;
2289 va_list p;
2291 va_start (p, tt);
2293 t = make_node (code);
2294 length = TREE_CODE_LENGTH (code);
2295 TREE_TYPE (t) = tt;
2297 /* FIXME maybe give expr a locus here? */
2299 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
2300 result based on those same flags for the arguments. But if the
2301 arguments aren't really even `tree' expressions, we shouldn't be trying
2302 to do this. */
2303 fro = first_rtl_op (code);
2305 /* Expressions without side effects may be constant if their
2306 arguments are as well. */
2307 constant = (TREE_CODE_CLASS (code) == '<'
2308 || TREE_CODE_CLASS (code) == '1'
2309 || TREE_CODE_CLASS (code) == '2'
2310 || TREE_CODE_CLASS (code) == 'c');
2312 if (length == 2)
2314 /* This is equivalent to the loop below, but faster. */
2315 tree arg0 = va_arg (p, tree);
2316 tree arg1 = va_arg (p, tree);
2318 TREE_OPERAND (t, 0) = arg0;
2319 TREE_OPERAND (t, 1) = arg1;
2320 TREE_READONLY (t) = 1;
2321 if (arg0 && fro > 0)
2323 if (TREE_SIDE_EFFECTS (arg0))
2324 TREE_SIDE_EFFECTS (t) = 1;
2325 if (!TREE_READONLY (arg0))
2326 TREE_READONLY (t) = 0;
2327 if (!TREE_CONSTANT (arg0))
2328 constant = 0;
2331 if (arg1 && fro > 1)
2333 if (TREE_SIDE_EFFECTS (arg1))
2334 TREE_SIDE_EFFECTS (t) = 1;
2335 if (!TREE_READONLY (arg1))
2336 TREE_READONLY (t) = 0;
2337 if (!TREE_CONSTANT (arg1))
2338 constant = 0;
2341 else if (length == 1)
2343 tree arg0 = va_arg (p, tree);
2345 /* The only one-operand cases we handle here are those with side-effects.
2346 Others are handled with build1. So don't bother checked if the
2347 arg has side-effects since we'll already have set it.
2349 ??? This really should use build1 too. */
2350 if (TREE_CODE_CLASS (code) != 's')
2351 abort ();
2352 TREE_OPERAND (t, 0) = arg0;
2354 else
2356 for (i = 0; i < length; i++)
2358 tree operand = va_arg (p, tree);
2360 TREE_OPERAND (t, i) = operand;
2361 if (operand && fro > i)
2363 if (TREE_SIDE_EFFECTS (operand))
2364 TREE_SIDE_EFFECTS (t) = 1;
2365 if (!TREE_CONSTANT (operand))
2366 constant = 0;
2370 va_end (p);
2372 TREE_CONSTANT (t) = constant;
2374 if (code == CALL_EXPR && !TREE_SIDE_EFFECTS (t))
2376 /* Calls have side-effects, except those to const or
2377 pure functions. */
2378 tree fn = get_callee_fndecl (t);
2380 if (!fn || (!DECL_IS_PURE (fn) && !TREE_READONLY (fn)))
2381 TREE_SIDE_EFFECTS (t) = 1;
2384 return t;
2387 /* Same as above, but only builds for unary operators.
2388 Saves lions share of calls to `build'; cuts down use
2389 of varargs, which is expensive for RISC machines. */
2391 tree
2392 build1 (enum tree_code code, tree type, tree node)
2394 int length = sizeof (struct tree_exp);
2395 #ifdef GATHER_STATISTICS
2396 tree_node_kind kind;
2397 #endif
2398 tree t;
2400 #ifdef GATHER_STATISTICS
2401 switch (TREE_CODE_CLASS (code))
2403 case 's': /* an expression with side effects */
2404 kind = s_kind;
2405 break;
2406 case 'r': /* a reference */
2407 kind = r_kind;
2408 break;
2409 default:
2410 kind = e_kind;
2411 break;
2414 tree_node_counts[(int) kind]++;
2415 tree_node_sizes[(int) kind] += length;
2416 #endif
2418 #ifdef ENABLE_CHECKING
2419 if (TREE_CODE_CLASS (code) == '2'
2420 || TREE_CODE_CLASS (code) == '<'
2421 || TREE_CODE_LENGTH (code) != 1)
2422 abort ();
2423 #endif /* ENABLE_CHECKING */
2425 t = ggc_alloc_tree (length);
2427 memset (t, 0, sizeof (struct tree_common));
2429 TREE_SET_CODE (t, code);
2431 TREE_TYPE (t) = type;
2432 /* FIXME maybe give expr a locus here? */
2433 SET_EXPR_LOCUS (t, NULL);
2434 TREE_COMPLEXITY (t) = 0;
2435 TREE_OPERAND (t, 0) = node;
2436 if (node && first_rtl_op (code) != 0)
2438 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
2439 TREE_READONLY (t) = TREE_READONLY (node);
2442 if (TREE_CODE_CLASS (code) == 's')
2443 TREE_SIDE_EFFECTS (t) = 1;
2444 else switch (code)
2446 case INIT_EXPR:
2447 case MODIFY_EXPR:
2448 case VA_ARG_EXPR:
2449 case RTL_EXPR:
2450 case PREDECREMENT_EXPR:
2451 case PREINCREMENT_EXPR:
2452 case POSTDECREMENT_EXPR:
2453 case POSTINCREMENT_EXPR:
2454 /* All of these have side-effects, no matter what their
2455 operands are. */
2456 TREE_SIDE_EFFECTS (t) = 1;
2457 TREE_READONLY (t) = 0;
2458 break;
2460 case INDIRECT_REF:
2461 /* Whether a dereference is readonly has nothing to do with whether
2462 its operand is readonly. */
2463 TREE_READONLY (t) = 0;
2464 break;
2466 default:
2467 if (TREE_CODE_CLASS (code) == '1' && node && TREE_CONSTANT (node))
2468 TREE_CONSTANT (t) = 1;
2469 break;
2472 return t;
2475 /* Similar except don't specify the TREE_TYPE
2476 and leave the TREE_SIDE_EFFECTS as 0.
2477 It is permissible for arguments to be null,
2478 or even garbage if their values do not matter. */
2480 tree
2481 build_nt (enum tree_code code, ...)
2483 tree t;
2484 int length;
2485 int i;
2486 va_list p;
2488 va_start (p, code);
2490 t = make_node (code);
2491 length = TREE_CODE_LENGTH (code);
2493 for (i = 0; i < length; i++)
2494 TREE_OPERAND (t, i) = va_arg (p, tree);
2496 va_end (p);
2497 return t;
2500 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
2501 We do NOT enter this node in any sort of symbol table.
2503 layout_decl is used to set up the decl's storage layout.
2504 Other slots are initialized to 0 or null pointers. */
2506 tree
2507 build_decl (enum tree_code code, tree name, tree type)
2509 tree t;
2511 t = make_node (code);
2513 /* if (type == error_mark_node)
2514 type = integer_type_node; */
2515 /* That is not done, deliberately, so that having error_mark_node
2516 as the type can suppress useless errors in the use of this variable. */
2518 DECL_NAME (t) = name;
2519 TREE_TYPE (t) = type;
2521 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
2522 layout_decl (t, 0);
2523 else if (code == FUNCTION_DECL)
2524 DECL_MODE (t) = FUNCTION_MODE;
2526 return t;
2529 /* BLOCK nodes are used to represent the structure of binding contours
2530 and declarations, once those contours have been exited and their contents
2531 compiled. This information is used for outputting debugging info. */
2533 tree
2534 build_block (tree vars, tree tags ATTRIBUTE_UNUSED, tree subblocks,
2535 tree supercontext, tree chain)
2537 tree block = make_node (BLOCK);
2539 BLOCK_VARS (block) = vars;
2540 BLOCK_SUBBLOCKS (block) = subblocks;
2541 BLOCK_SUPERCONTEXT (block) = supercontext;
2542 BLOCK_CHAIN (block) = chain;
2543 return block;
2546 static GTY(()) tree last_annotated_node;
2548 /* Record the exact location where an expression or an identifier were
2549 encountered. */
2550 void
2551 annotate_with_file_line (tree node, const char *file, int line)
2553 /* Roughly one percent of the calls to this function are to annotate
2554 a node with the same information already attached to that node!
2555 Just return instead of wasting memory. */
2556 if (EXPR_LOCUS (node)
2557 && (EXPR_FILENAME (node) == file
2558 || ! strcmp (EXPR_FILENAME (node), file))
2559 && EXPR_LINENO (node) == line)
2561 last_annotated_node = node;
2562 return;
2565 /* In heavily macroized code (such as GCC itself) this single
2566 entry cache can reduce the number of allocations by more
2567 than half. */
2568 if (last_annotated_node
2569 && EXPR_LOCUS (last_annotated_node)
2570 && (EXPR_FILENAME (last_annotated_node) == file
2571 || ! strcmp (EXPR_FILENAME (last_annotated_node), file))
2572 && EXPR_LINENO (last_annotated_node) == line)
2574 SET_EXPR_LOCUS (node, EXPR_LOCUS (last_annotated_node));
2575 return;
2578 SET_EXPR_LOCUS (node, ggc_alloc (sizeof (location_t)));
2579 EXPR_LINENO (node) = line;
2580 EXPR_FILENAME (node) = file;
2581 last_annotated_node = node;
2584 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
2585 is ATTRIBUTE. */
2587 tree
2588 build_decl_attribute_variant (tree ddecl, tree attribute)
2590 DECL_ATTRIBUTES (ddecl) = attribute;
2591 return ddecl;
2594 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
2595 is ATTRIBUTE.
2597 Record such modified types already made so we don't make duplicates. */
2599 tree
2600 build_type_attribute_variant (tree ttype, tree attribute)
2602 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
2604 unsigned int hashcode;
2605 tree ntype;
2607 ntype = copy_node (ttype);
2609 TYPE_POINTER_TO (ntype) = 0;
2610 TYPE_REFERENCE_TO (ntype) = 0;
2611 TYPE_ATTRIBUTES (ntype) = attribute;
2613 /* Create a new main variant of TYPE. */
2614 TYPE_MAIN_VARIANT (ntype) = ntype;
2615 TYPE_NEXT_VARIANT (ntype) = 0;
2616 set_type_quals (ntype, TYPE_UNQUALIFIED);
2618 hashcode = (TYPE_HASH (TREE_CODE (ntype))
2619 + TYPE_HASH (TREE_TYPE (ntype))
2620 + attribute_hash_list (attribute));
2622 switch (TREE_CODE (ntype))
2624 case FUNCTION_TYPE:
2625 hashcode += TYPE_HASH (TYPE_ARG_TYPES (ntype));
2626 break;
2627 case ARRAY_TYPE:
2628 hashcode += TYPE_HASH (TYPE_DOMAIN (ntype));
2629 break;
2630 case INTEGER_TYPE:
2631 hashcode += TYPE_HASH (TYPE_MAX_VALUE (ntype));
2632 break;
2633 case REAL_TYPE:
2634 hashcode += TYPE_HASH (TYPE_PRECISION (ntype));
2635 break;
2636 default:
2637 break;
2640 ntype = type_hash_canon (hashcode, ntype);
2641 ttype = build_qualified_type (ntype, TYPE_QUALS (ttype));
2644 return ttype;
2647 /* Return nonzero if IDENT is a valid name for attribute ATTR,
2648 or zero if not.
2650 We try both `text' and `__text__', ATTR may be either one. */
2651 /* ??? It might be a reasonable simplification to require ATTR to be only
2652 `text'. One might then also require attribute lists to be stored in
2653 their canonicalized form. */
2656 is_attribute_p (const char *attr, tree ident)
2658 int ident_len, attr_len;
2659 const char *p;
2661 if (TREE_CODE (ident) != IDENTIFIER_NODE)
2662 return 0;
2664 if (strcmp (attr, IDENTIFIER_POINTER (ident)) == 0)
2665 return 1;
2667 p = IDENTIFIER_POINTER (ident);
2668 ident_len = strlen (p);
2669 attr_len = strlen (attr);
2671 /* If ATTR is `__text__', IDENT must be `text'; and vice versa. */
2672 if (attr[0] == '_')
2674 if (attr[1] != '_'
2675 || attr[attr_len - 2] != '_'
2676 || attr[attr_len - 1] != '_')
2677 abort ();
2678 if (ident_len == attr_len - 4
2679 && strncmp (attr + 2, p, attr_len - 4) == 0)
2680 return 1;
2682 else
2684 if (ident_len == attr_len + 4
2685 && p[0] == '_' && p[1] == '_'
2686 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
2687 && strncmp (attr, p + 2, attr_len) == 0)
2688 return 1;
2691 return 0;
2694 /* Given an attribute name and a list of attributes, return a pointer to the
2695 attribute's list element if the attribute is part of the list, or NULL_TREE
2696 if not found. If the attribute appears more than once, this only
2697 returns the first occurrence; the TREE_CHAIN of the return value should
2698 be passed back in if further occurrences are wanted. */
2700 tree
2701 lookup_attribute (const char *attr_name, tree list)
2703 tree l;
2705 for (l = list; l; l = TREE_CHAIN (l))
2707 if (TREE_CODE (TREE_PURPOSE (l)) != IDENTIFIER_NODE)
2708 abort ();
2709 if (is_attribute_p (attr_name, TREE_PURPOSE (l)))
2710 return l;
2713 return NULL_TREE;
2716 /* Return an attribute list that is the union of a1 and a2. */
2718 tree
2719 merge_attributes (tree a1, tree a2)
2721 tree attributes;
2723 /* Either one unset? Take the set one. */
2725 if ((attributes = a1) == 0)
2726 attributes = a2;
2728 /* One that completely contains the other? Take it. */
2730 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
2732 if (attribute_list_contained (a2, a1))
2733 attributes = a2;
2734 else
2736 /* Pick the longest list, and hang on the other list. */
2738 if (list_length (a1) < list_length (a2))
2739 attributes = a2, a2 = a1;
2741 for (; a2 != 0; a2 = TREE_CHAIN (a2))
2743 tree a;
2744 for (a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
2745 attributes);
2746 a != NULL_TREE;
2747 a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
2748 TREE_CHAIN (a)))
2750 if (simple_cst_equal (TREE_VALUE (a), TREE_VALUE (a2)) == 1)
2751 break;
2753 if (a == NULL_TREE)
2755 a1 = copy_node (a2);
2756 TREE_CHAIN (a1) = attributes;
2757 attributes = a1;
2762 return attributes;
2765 /* Given types T1 and T2, merge their attributes and return
2766 the result. */
2768 tree
2769 merge_type_attributes (tree t1, tree t2)
2771 return merge_attributes (TYPE_ATTRIBUTES (t1),
2772 TYPE_ATTRIBUTES (t2));
2775 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
2776 the result. */
2778 tree
2779 merge_decl_attributes (tree olddecl, tree newdecl)
2781 return merge_attributes (DECL_ATTRIBUTES (olddecl),
2782 DECL_ATTRIBUTES (newdecl));
2785 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
2787 /* Specialization of merge_decl_attributes for various Windows targets.
2789 This handles the following situation:
2791 __declspec (dllimport) int foo;
2792 int foo;
2794 The second instance of `foo' nullifies the dllimport. */
2796 tree
2797 merge_dllimport_decl_attributes (tree old, tree new)
2799 tree a;
2800 int delete_dllimport_p;
2802 old = DECL_ATTRIBUTES (old);
2803 new = DECL_ATTRIBUTES (new);
2805 /* What we need to do here is remove from `old' dllimport if it doesn't
2806 appear in `new'. dllimport behaves like extern: if a declaration is
2807 marked dllimport and a definition appears later, then the object
2808 is not dllimport'd. */
2809 if (lookup_attribute ("dllimport", old) != NULL_TREE
2810 && lookup_attribute ("dllimport", new) == NULL_TREE)
2811 delete_dllimport_p = 1;
2812 else
2813 delete_dllimport_p = 0;
2815 a = merge_attributes (old, new);
2817 if (delete_dllimport_p)
2819 tree prev, t;
2821 /* Scan the list for dllimport and delete it. */
2822 for (prev = NULL_TREE, t = a; t; prev = t, t = TREE_CHAIN (t))
2824 if (is_attribute_p ("dllimport", TREE_PURPOSE (t)))
2826 if (prev == NULL_TREE)
2827 a = TREE_CHAIN (a);
2828 else
2829 TREE_CHAIN (prev) = TREE_CHAIN (t);
2830 break;
2835 return a;
2838 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
2840 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
2841 of the various TYPE_QUAL values. */
2843 static void
2844 set_type_quals (tree type, int type_quals)
2846 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
2847 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
2848 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
2851 /* Return a version of the TYPE, qualified as indicated by the
2852 TYPE_QUALS, if one exists. If no qualified version exists yet,
2853 return NULL_TREE. */
2855 tree
2856 get_qualified_type (tree type, int type_quals)
2858 tree t;
2860 /* Search the chain of variants to see if there is already one there just
2861 like the one we need to have. If so, use that existing one. We must
2862 preserve the TYPE_NAME, since there is code that depends on this. */
2863 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2864 if (TYPE_QUALS (t) == type_quals && TYPE_NAME (t) == TYPE_NAME (type)
2865 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
2866 && attribute_list_equal (TYPE_ATTRIBUTES (t), TYPE_ATTRIBUTES (type)))
2867 return t;
2869 return NULL_TREE;
2872 /* Like get_qualified_type, but creates the type if it does not
2873 exist. This function never returns NULL_TREE. */
2875 tree
2876 build_qualified_type (tree type, int type_quals)
2878 tree t;
2880 /* See if we already have the appropriate qualified variant. */
2881 t = get_qualified_type (type, type_quals);
2883 /* If not, build it. */
2884 if (!t)
2886 t = build_type_copy (type);
2887 set_type_quals (t, type_quals);
2890 return t;
2893 /* Create a new variant of TYPE, equivalent but distinct.
2894 This is so the caller can modify it. */
2896 tree
2897 build_type_copy (tree type)
2899 tree t, m = TYPE_MAIN_VARIANT (type);
2901 t = copy_node (type);
2903 TYPE_POINTER_TO (t) = 0;
2904 TYPE_REFERENCE_TO (t) = 0;
2906 /* Add this type to the chain of variants of TYPE. */
2907 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
2908 TYPE_NEXT_VARIANT (m) = t;
2910 return t;
2913 /* Hashing of types so that we don't make duplicates.
2914 The entry point is `type_hash_canon'. */
2916 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
2917 with types in the TREE_VALUE slots), by adding the hash codes
2918 of the individual types. */
2920 unsigned int
2921 type_hash_list (tree list)
2923 unsigned int hashcode;
2924 tree tail;
2926 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
2927 hashcode += TYPE_HASH (TREE_VALUE (tail));
2929 return hashcode;
2932 /* These are the Hashtable callback functions. */
2934 /* Returns true if the types are equal. */
2936 static int
2937 type_hash_eq (const void *va, const void *vb)
2939 const struct type_hash *a = va, *b = vb;
2940 if (a->hash == b->hash
2941 && TREE_CODE (a->type) == TREE_CODE (b->type)
2942 && TREE_TYPE (a->type) == TREE_TYPE (b->type)
2943 && attribute_list_equal (TYPE_ATTRIBUTES (a->type),
2944 TYPE_ATTRIBUTES (b->type))
2945 && TYPE_ALIGN (a->type) == TYPE_ALIGN (b->type)
2946 && (TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
2947 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
2948 TYPE_MAX_VALUE (b->type)))
2949 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
2950 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
2951 TYPE_MIN_VALUE (b->type)))
2952 /* Note that TYPE_DOMAIN is TYPE_ARG_TYPES for FUNCTION_TYPE. */
2953 && (TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type)
2954 || (TYPE_DOMAIN (a->type)
2955 && TREE_CODE (TYPE_DOMAIN (a->type)) == TREE_LIST
2956 && TYPE_DOMAIN (b->type)
2957 && TREE_CODE (TYPE_DOMAIN (b->type)) == TREE_LIST
2958 && type_list_equal (TYPE_DOMAIN (a->type),
2959 TYPE_DOMAIN (b->type)))))
2960 return 1;
2961 return 0;
2964 /* Return the cached hash value. */
2966 static hashval_t
2967 type_hash_hash (const void *item)
2969 return ((const struct type_hash *) item)->hash;
2972 /* Look in the type hash table for a type isomorphic to TYPE.
2973 If one is found, return it. Otherwise return 0. */
2975 tree
2976 type_hash_lookup (unsigned int hashcode, tree type)
2978 struct type_hash *h, in;
2980 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
2981 must call that routine before comparing TYPE_ALIGNs. */
2982 layout_type (type);
2984 in.hash = hashcode;
2985 in.type = type;
2987 h = htab_find_with_hash (type_hash_table, &in, hashcode);
2988 if (h)
2989 return h->type;
2990 return NULL_TREE;
2993 /* Add an entry to the type-hash-table
2994 for a type TYPE whose hash code is HASHCODE. */
2996 void
2997 type_hash_add (unsigned int hashcode, tree type)
2999 struct type_hash *h;
3000 void **loc;
3002 h = ggc_alloc (sizeof (struct type_hash));
3003 h->hash = hashcode;
3004 h->type = type;
3005 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
3006 *(struct type_hash **) loc = h;
3009 /* Given TYPE, and HASHCODE its hash code, return the canonical
3010 object for an identical type if one already exists.
3011 Otherwise, return TYPE, and record it as the canonical object
3012 if it is a permanent object.
3014 To use this function, first create a type of the sort you want.
3015 Then compute its hash code from the fields of the type that
3016 make it different from other similar types.
3017 Then call this function and use the value.
3018 This function frees the type you pass in if it is a duplicate. */
3020 /* Set to 1 to debug without canonicalization. Never set by program. */
3021 int debug_no_type_hash = 0;
3023 tree
3024 type_hash_canon (unsigned int hashcode, tree type)
3026 tree t1;
3028 if (debug_no_type_hash)
3029 return type;
3031 /* See if the type is in the hash table already. If so, return it.
3032 Otherwise, add the type. */
3033 t1 = type_hash_lookup (hashcode, type);
3034 if (t1 != 0)
3036 #ifdef GATHER_STATISTICS
3037 tree_node_counts[(int) t_kind]--;
3038 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
3039 #endif
3040 return t1;
3042 else
3044 type_hash_add (hashcode, type);
3045 return type;
3049 /* See if the data pointed to by the type hash table is marked. We consider
3050 it marked if the type is marked or if a debug type number or symbol
3051 table entry has been made for the type. This reduces the amount of
3052 debugging output and eliminates that dependency of the debug output on
3053 the number of garbage collections. */
3055 static int
3056 type_hash_marked_p (const void *p)
3058 tree type = ((struct type_hash *) p)->type;
3060 return ggc_marked_p (type) || TYPE_SYMTAB_POINTER (type);
3063 static void
3064 print_type_hash_statistics (void)
3066 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
3067 (long) htab_size (type_hash_table),
3068 (long) htab_elements (type_hash_table),
3069 htab_collisions (type_hash_table));
3072 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
3073 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
3074 by adding the hash codes of the individual attributes. */
3076 unsigned int
3077 attribute_hash_list (tree list)
3079 unsigned int hashcode;
3080 tree tail;
3082 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
3083 /* ??? Do we want to add in TREE_VALUE too? */
3084 hashcode += TYPE_HASH (TREE_PURPOSE (tail));
3085 return hashcode;
3088 /* Given two lists of attributes, return true if list l2 is
3089 equivalent to l1. */
3092 attribute_list_equal (tree l1, tree l2)
3094 return attribute_list_contained (l1, l2)
3095 && attribute_list_contained (l2, l1);
3098 /* Given two lists of attributes, return true if list L2 is
3099 completely contained within L1. */
3100 /* ??? This would be faster if attribute names were stored in a canonicalized
3101 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
3102 must be used to show these elements are equivalent (which they are). */
3103 /* ??? It's not clear that attributes with arguments will always be handled
3104 correctly. */
3107 attribute_list_contained (tree l1, tree l2)
3109 tree t1, t2;
3111 /* First check the obvious, maybe the lists are identical. */
3112 if (l1 == l2)
3113 return 1;
3115 /* Maybe the lists are similar. */
3116 for (t1 = l1, t2 = l2;
3117 t1 != 0 && t2 != 0
3118 && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
3119 && TREE_VALUE (t1) == TREE_VALUE (t2);
3120 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
3122 /* Maybe the lists are equal. */
3123 if (t1 == 0 && t2 == 0)
3124 return 1;
3126 for (; t2 != 0; t2 = TREE_CHAIN (t2))
3128 tree attr;
3129 for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)), l1);
3130 attr != NULL_TREE;
3131 attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
3132 TREE_CHAIN (attr)))
3134 if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) == 1)
3135 break;
3138 if (attr == 0)
3139 return 0;
3141 if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) != 1)
3142 return 0;
3145 return 1;
3148 /* Given two lists of types
3149 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
3150 return 1 if the lists contain the same types in the same order.
3151 Also, the TREE_PURPOSEs must match. */
3154 type_list_equal (tree l1, tree l2)
3156 tree t1, t2;
3158 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
3159 if (TREE_VALUE (t1) != TREE_VALUE (t2)
3160 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
3161 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
3162 && (TREE_TYPE (TREE_PURPOSE (t1))
3163 == TREE_TYPE (TREE_PURPOSE (t2))))))
3164 return 0;
3166 return t1 == t2;
3169 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
3170 given by TYPE. If the argument list accepts variable arguments,
3171 then this function counts only the ordinary arguments. */
3174 type_num_arguments (tree type)
3176 int i = 0;
3177 tree t;
3179 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
3180 /* If the function does not take a variable number of arguments,
3181 the last element in the list will have type `void'. */
3182 if (VOID_TYPE_P (TREE_VALUE (t)))
3183 break;
3184 else
3185 ++i;
3187 return i;
3190 /* Nonzero if integer constants T1 and T2
3191 represent the same constant value. */
3194 tree_int_cst_equal (tree t1, tree t2)
3196 if (t1 == t2)
3197 return 1;
3199 if (t1 == 0 || t2 == 0)
3200 return 0;
3202 if (TREE_CODE (t1) == INTEGER_CST
3203 && TREE_CODE (t2) == INTEGER_CST
3204 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3205 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
3206 return 1;
3208 return 0;
3211 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
3212 The precise way of comparison depends on their data type. */
3215 tree_int_cst_lt (tree t1, tree t2)
3217 if (t1 == t2)
3218 return 0;
3220 if (TREE_UNSIGNED (TREE_TYPE (t1)) != TREE_UNSIGNED (TREE_TYPE (t2)))
3222 int t1_sgn = tree_int_cst_sgn (t1);
3223 int t2_sgn = tree_int_cst_sgn (t2);
3225 if (t1_sgn < t2_sgn)
3226 return 1;
3227 else if (t1_sgn > t2_sgn)
3228 return 0;
3229 /* Otherwise, both are non-negative, so we compare them as
3230 unsigned just in case one of them would overflow a signed
3231 type. */
3233 else if (! TREE_UNSIGNED (TREE_TYPE (t1)))
3234 return INT_CST_LT (t1, t2);
3236 return INT_CST_LT_UNSIGNED (t1, t2);
3239 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
3242 tree_int_cst_compare (tree t1, tree t2)
3244 if (tree_int_cst_lt (t1, t2))
3245 return -1;
3246 else if (tree_int_cst_lt (t2, t1))
3247 return 1;
3248 else
3249 return 0;
3252 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
3253 the host. If POS is zero, the value can be represented in a single
3254 HOST_WIDE_INT. If POS is nonzero, the value must be positive and can
3255 be represented in a single unsigned HOST_WIDE_INT. */
3258 host_integerp (tree t, int pos)
3260 return (TREE_CODE (t) == INTEGER_CST
3261 && ! TREE_OVERFLOW (t)
3262 && ((TREE_INT_CST_HIGH (t) == 0
3263 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
3264 || (! pos && TREE_INT_CST_HIGH (t) == -1
3265 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
3266 && ! TREE_UNSIGNED (TREE_TYPE (t)))
3267 || (pos && TREE_INT_CST_HIGH (t) == 0)));
3270 /* Return the HOST_WIDE_INT least significant bits of T if it is an
3271 INTEGER_CST and there is no overflow. POS is nonzero if the result must
3272 be positive. Abort if we cannot satisfy the above conditions. */
3274 HOST_WIDE_INT
3275 tree_low_cst (tree t, int pos)
3277 if (host_integerp (t, pos))
3278 return TREE_INT_CST_LOW (t);
3279 else
3280 abort ();
3283 /* Return the most significant bit of the integer constant T. */
3286 tree_int_cst_msb (tree t)
3288 int prec;
3289 HOST_WIDE_INT h;
3290 unsigned HOST_WIDE_INT l;
3292 /* Note that using TYPE_PRECISION here is wrong. We care about the
3293 actual bits, not the (arbitrary) range of the type. */
3294 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
3295 rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
3296 2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
3297 return (l & 1) == 1;
3300 /* Return an indication of the sign of the integer constant T.
3301 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
3302 Note that -1 will never be returned it T's type is unsigned. */
3305 tree_int_cst_sgn (tree t)
3307 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
3308 return 0;
3309 else if (TREE_UNSIGNED (TREE_TYPE (t)))
3310 return 1;
3311 else if (TREE_INT_CST_HIGH (t) < 0)
3312 return -1;
3313 else
3314 return 1;
3317 /* Compare two constructor-element-type constants. Return 1 if the lists
3318 are known to be equal; otherwise return 0. */
3321 simple_cst_list_equal (tree l1, tree l2)
3323 while (l1 != NULL_TREE && l2 != NULL_TREE)
3325 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
3326 return 0;
3328 l1 = TREE_CHAIN (l1);
3329 l2 = TREE_CHAIN (l2);
3332 return l1 == l2;
3335 /* Return truthvalue of whether T1 is the same tree structure as T2.
3336 Return 1 if they are the same.
3337 Return 0 if they are understandably different.
3338 Return -1 if either contains tree structure not understood by
3339 this function. */
3342 simple_cst_equal (tree t1, tree t2)
3344 enum tree_code code1, code2;
3345 int cmp;
3346 int i;
3348 if (t1 == t2)
3349 return 1;
3350 if (t1 == 0 || t2 == 0)
3351 return 0;
3353 code1 = TREE_CODE (t1);
3354 code2 = TREE_CODE (t2);
3356 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
3358 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3359 || code2 == NON_LVALUE_EXPR)
3360 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3361 else
3362 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
3365 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3366 || code2 == NON_LVALUE_EXPR)
3367 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
3369 if (code1 != code2)
3370 return 0;
3372 switch (code1)
3374 case INTEGER_CST:
3375 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3376 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
3378 case REAL_CST:
3379 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3381 case STRING_CST:
3382 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3383 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3384 TREE_STRING_LENGTH (t1)));
3386 case CONSTRUCTOR:
3387 return simple_cst_list_equal (CONSTRUCTOR_ELTS (t1),
3388 CONSTRUCTOR_ELTS (t2));
3390 case SAVE_EXPR:
3391 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3393 case CALL_EXPR:
3394 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3395 if (cmp <= 0)
3396 return cmp;
3397 return
3398 simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3400 case TARGET_EXPR:
3401 /* Special case: if either target is an unallocated VAR_DECL,
3402 it means that it's going to be unified with whatever the
3403 TARGET_EXPR is really supposed to initialize, so treat it
3404 as being equivalent to anything. */
3405 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
3406 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
3407 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
3408 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
3409 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
3410 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
3411 cmp = 1;
3412 else
3413 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3415 if (cmp <= 0)
3416 return cmp;
3418 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3420 case WITH_CLEANUP_EXPR:
3421 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3422 if (cmp <= 0)
3423 return cmp;
3425 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
3427 case COMPONENT_REF:
3428 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
3429 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3431 return 0;
3433 case VAR_DECL:
3434 case PARM_DECL:
3435 case CONST_DECL:
3436 case FUNCTION_DECL:
3437 return 0;
3439 default:
3440 break;
3443 /* This general rule works for most tree codes. All exceptions should be
3444 handled above. If this is a language-specific tree code, we can't
3445 trust what might be in the operand, so say we don't know
3446 the situation. */
3447 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
3448 return -1;
3450 switch (TREE_CODE_CLASS (code1))
3452 case '1':
3453 case '2':
3454 case '<':
3455 case 'e':
3456 case 'r':
3457 case 's':
3458 cmp = 1;
3459 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
3461 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
3462 if (cmp <= 0)
3463 return cmp;
3466 return cmp;
3468 default:
3469 return -1;
3473 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
3474 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
3475 than U, respectively. */
3478 compare_tree_int (tree t, unsigned HOST_WIDE_INT u)
3480 if (tree_int_cst_sgn (t) < 0)
3481 return -1;
3482 else if (TREE_INT_CST_HIGH (t) != 0)
3483 return 1;
3484 else if (TREE_INT_CST_LOW (t) == u)
3485 return 0;
3486 else if (TREE_INT_CST_LOW (t) < u)
3487 return -1;
3488 else
3489 return 1;
3492 /* Return true if CODE represents an associative tree code. Otherwise
3493 return false. */
3494 bool
3495 associative_tree_code (enum tree_code code)
3497 return (code == BIT_IOR_EXPR
3498 || code == BIT_AND_EXPR
3499 || code == BIT_XOR_EXPR
3500 || code == PLUS_EXPR
3501 || code == MINUS_EXPR
3502 || code == MULT_EXPR
3503 || code == LSHIFT_EXPR
3504 || code == RSHIFT_EXPR
3505 || code == MIN_EXPR
3506 || code == MAX_EXPR);
3509 /* Return true if CODE represents an commutative tree code. Otherwise
3510 return false. */
3511 bool
3512 commutative_tree_code (enum tree_code code)
3514 return (code == PLUS_EXPR
3515 || code == MULT_EXPR
3516 || code == MIN_EXPR
3517 || code == MAX_EXPR
3518 || code == BIT_IOR_EXPR
3519 || code == BIT_XOR_EXPR
3520 || code == BIT_AND_EXPR
3521 || code == NE_EXPR
3522 || code == EQ_EXPR);
3525 /* Generate a hash value for an expression. This can be used iteratively
3526 by passing a previous result as the "val" argument.
3528 This function is intended to produce the same hash for expressions which
3529 would compare equal using operand_equal_p. */
3531 hashval_t
3532 iterative_hash_expr (tree t, hashval_t val)
3534 int i;
3535 enum tree_code code;
3536 char class;
3538 if (t == NULL_TREE)
3539 return iterative_hash_object (t, val);
3541 code = TREE_CODE (t);
3542 class = TREE_CODE_CLASS (code);
3544 if (class == 'd')
3546 /* Decls we can just compare by pointer. */
3547 val = iterative_hash_object (t, val);
3549 else if (class == 'c')
3551 /* Alas, constants aren't shared, so we can't rely on pointer
3552 identity. */
3553 if (code == INTEGER_CST)
3555 val = iterative_hash_object (TREE_INT_CST_LOW (t), val);
3556 val = iterative_hash_object (TREE_INT_CST_HIGH (t), val);
3558 else if (code == REAL_CST)
3559 val = iterative_hash (TREE_REAL_CST_PTR (t),
3560 sizeof (REAL_VALUE_TYPE), val);
3561 else if (code == STRING_CST)
3562 val = iterative_hash (TREE_STRING_POINTER (t),
3563 TREE_STRING_LENGTH (t), val);
3564 else if (code == COMPLEX_CST)
3566 val = iterative_hash_expr (TREE_REALPART (t), val);
3567 val = iterative_hash_expr (TREE_IMAGPART (t), val);
3569 else if (code == VECTOR_CST)
3570 val = iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
3571 else
3572 abort ();
3574 else if (IS_EXPR_CODE_CLASS (class))
3576 val = iterative_hash_object (code, val);
3578 /* Don't hash the type, that can lead to having nodes which
3579 compare equal according to operand_equal_p, but which
3580 have different hash codes. */
3581 if (code == NOP_EXPR
3582 || code == CONVERT_EXPR
3583 || code == NON_LVALUE_EXPR)
3585 /* Make sure to include signness in the hash computation. */
3586 val += TREE_UNSIGNED (TREE_TYPE (t));
3587 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
3590 if (commutative_tree_code (code))
3592 /* It's a commutative expression. We want to hash it the same
3593 however it appears. We do this by first hashing both operands
3594 and then rehashing based on the order of their independent
3595 hashes. */
3596 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
3597 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
3598 hashval_t t;
3600 if (one > two)
3601 t = one, one = two, two = t;
3603 val = iterative_hash_object (one, val);
3604 val = iterative_hash_object (two, val);
3606 else
3607 for (i = first_rtl_op (code) - 1; i >= 0; --i)
3608 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
3610 else if (code == TREE_LIST)
3612 /* A list of expressions, for a CALL_EXPR or as the elements of a
3613 VECTOR_CST. */
3614 for (; t; t = TREE_CHAIN (t))
3615 val = iterative_hash_expr (TREE_VALUE (t), val);
3617 else if (code == SSA_NAME)
3619 val = iterative_hash_object (SSA_NAME_VERSION (t), val);
3620 val = iterative_hash_expr (SSA_NAME_VAR (t), val);
3622 else
3623 abort ();
3625 return val;
3628 /* Constructors for pointer, array and function types.
3629 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
3630 constructed by language-dependent code, not here.) */
3632 /* Construct, lay out and return the type of pointers to TO_TYPE
3633 with mode MODE. If such a type has already been constructed,
3634 reuse it. */
3636 tree
3637 build_pointer_type_for_mode (tree to_type, enum machine_mode mode)
3639 tree t = TYPE_POINTER_TO (to_type);
3641 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3642 if (t != 0 && mode == ptr_mode)
3643 return t;
3645 t = make_node (POINTER_TYPE);
3647 TREE_TYPE (t) = to_type;
3648 TYPE_MODE (t) = mode;
3650 /* Record this type as the pointer to TO_TYPE. */
3651 if (mode == ptr_mode)
3652 TYPE_POINTER_TO (to_type) = t;
3654 /* Lay out the type. This function has many callers that are concerned
3655 with expression-construction, and this simplifies them all.
3656 Also, it guarantees the TYPE_SIZE is in the same obstack as the type. */
3657 layout_type (t);
3659 return t;
3662 /* By default build pointers in ptr_mode. */
3664 tree
3665 build_pointer_type (tree to_type)
3667 return build_pointer_type_for_mode (to_type, ptr_mode);
3670 /* Construct, lay out and return the type of references to TO_TYPE
3671 with mode MODE. If such a type has already been constructed,
3672 reuse it. */
3674 tree
3675 build_reference_type_for_mode (tree to_type, enum machine_mode mode)
3677 tree t = TYPE_REFERENCE_TO (to_type);
3679 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3680 if (t != 0 && mode == ptr_mode)
3681 return t;
3683 t = make_node (REFERENCE_TYPE);
3685 TREE_TYPE (t) = to_type;
3686 TYPE_MODE (t) = mode;
3688 /* Record this type as the pointer to TO_TYPE. */
3689 if (mode == ptr_mode)
3690 TYPE_REFERENCE_TO (to_type) = t;
3692 layout_type (t);
3694 return t;
3698 /* Build the node for the type of references-to-TO_TYPE by default
3699 in ptr_mode. */
3701 tree
3702 build_reference_type (tree to_type)
3704 return build_reference_type_for_mode (to_type, ptr_mode);
3707 /* Build a type that is compatible with t but has no cv quals anywhere
3708 in its type, thus
3710 const char *const *const * -> char ***. */
3712 tree
3713 build_type_no_quals (tree t)
3715 switch (TREE_CODE (t))
3717 case POINTER_TYPE:
3718 return build_pointer_type (build_type_no_quals (TREE_TYPE (t)));
3719 case REFERENCE_TYPE:
3720 return build_reference_type (build_type_no_quals (TREE_TYPE (t)));
3721 default:
3722 return TYPE_MAIN_VARIANT (t);
3726 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
3727 MAXVAL should be the maximum value in the domain
3728 (one less than the length of the array).
3730 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
3731 We don't enforce this limit, that is up to caller (e.g. language front end).
3732 The limit exists because the result is a signed type and we don't handle
3733 sizes that use more than one HOST_WIDE_INT. */
3735 tree
3736 build_index_type (tree maxval)
3738 tree itype = make_node (INTEGER_TYPE);
3740 TREE_TYPE (itype) = sizetype;
3741 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
3742 TYPE_MIN_VALUE (itype) = size_zero_node;
3743 TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
3744 TYPE_MODE (itype) = TYPE_MODE (sizetype);
3745 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
3746 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
3747 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
3748 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
3750 if (host_integerp (maxval, 1))
3751 return type_hash_canon (tree_low_cst (maxval, 1), itype);
3752 else
3753 return itype;
3756 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
3757 ENUMERAL_TYPE, BOOLEAN_TYPE, or CHAR_TYPE), with
3758 low bound LOWVAL and high bound HIGHVAL.
3759 if TYPE==NULL_TREE, sizetype is used. */
3761 tree
3762 build_range_type (tree type, tree lowval, tree highval)
3764 tree itype = make_node (INTEGER_TYPE);
3766 TREE_TYPE (itype) = type;
3767 if (type == NULL_TREE)
3768 type = sizetype;
3770 TYPE_MIN_VALUE (itype) = convert (type, lowval);
3771 TYPE_MAX_VALUE (itype) = highval ? convert (type, highval) : NULL;
3773 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
3774 TYPE_MODE (itype) = TYPE_MODE (type);
3775 TYPE_SIZE (itype) = TYPE_SIZE (type);
3776 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
3777 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
3778 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
3780 if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
3781 return type_hash_canon (tree_low_cst (highval, 0)
3782 - tree_low_cst (lowval, 0),
3783 itype);
3784 else
3785 return itype;
3788 /* Just like build_index_type, but takes lowval and highval instead
3789 of just highval (maxval). */
3791 tree
3792 build_index_2_type (tree lowval, tree highval)
3794 return build_range_type (sizetype, lowval, highval);
3797 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
3798 and number of elements specified by the range of values of INDEX_TYPE.
3799 If such a type has already been constructed, reuse it. */
3801 tree
3802 build_array_type (tree elt_type, tree index_type)
3804 tree t;
3805 unsigned int hashcode;
3807 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
3809 error ("arrays of functions are not meaningful");
3810 elt_type = integer_type_node;
3813 /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
3814 build_pointer_type (elt_type);
3816 /* Allocate the array after the pointer type,
3817 in case we free it in type_hash_canon. */
3818 t = make_node (ARRAY_TYPE);
3819 TREE_TYPE (t) = elt_type;
3820 TYPE_DOMAIN (t) = index_type;
3822 if (index_type == 0)
3824 return t;
3827 hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
3828 t = type_hash_canon (hashcode, t);
3830 if (!COMPLETE_TYPE_P (t))
3831 layout_type (t);
3832 return t;
3835 /* Return the TYPE of the elements comprising
3836 the innermost dimension of ARRAY. */
3838 tree
3839 get_inner_array_type (tree array)
3841 tree type = TREE_TYPE (array);
3843 while (TREE_CODE (type) == ARRAY_TYPE)
3844 type = TREE_TYPE (type);
3846 return type;
3849 /* Construct, lay out and return
3850 the type of functions returning type VALUE_TYPE
3851 given arguments of types ARG_TYPES.
3852 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
3853 are data type nodes for the arguments of the function.
3854 If such a type has already been constructed, reuse it. */
3856 tree
3857 build_function_type (tree value_type, tree arg_types)
3859 tree t;
3860 unsigned int hashcode;
3862 if (TREE_CODE (value_type) == FUNCTION_TYPE)
3864 error ("function return type cannot be function");
3865 value_type = integer_type_node;
3868 /* Make a node of the sort we want. */
3869 t = make_node (FUNCTION_TYPE);
3870 TREE_TYPE (t) = value_type;
3871 TYPE_ARG_TYPES (t) = arg_types;
3873 /* If we already have such a type, use the old one and free this one. */
3874 hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
3875 t = type_hash_canon (hashcode, t);
3877 if (!COMPLETE_TYPE_P (t))
3878 layout_type (t);
3879 return t;
3882 /* Build a function type. The RETURN_TYPE is the type returned by the
3883 function. If additional arguments are provided, they are
3884 additional argument types. The list of argument types must always
3885 be terminated by NULL_TREE. */
3887 tree
3888 build_function_type_list (tree return_type, ...)
3890 tree t, args, last;
3891 va_list p;
3893 va_start (p, return_type);
3895 t = va_arg (p, tree);
3896 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (p, tree))
3897 args = tree_cons (NULL_TREE, t, args);
3899 last = args;
3900 args = nreverse (args);
3901 TREE_CHAIN (last) = void_list_node;
3902 args = build_function_type (return_type, args);
3904 va_end (p);
3905 return args;
3908 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
3909 and ARGTYPES (a TREE_LIST) are the return type and arguments types
3910 for the method. An implicit additional parameter (of type
3911 pointer-to-BASETYPE) is added to the ARGTYPES. */
3913 tree
3914 build_method_type_directly (tree basetype,
3915 tree rettype,
3916 tree argtypes)
3918 tree t;
3919 tree ptype;
3920 int hashcode;
3922 /* Make a node of the sort we want. */
3923 t = make_node (METHOD_TYPE);
3925 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3926 TREE_TYPE (t) = rettype;
3927 ptype = build_pointer_type (basetype);
3929 /* The actual arglist for this function includes a "hidden" argument
3930 which is "this". Put it into the list of argument types. */
3931 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
3932 TYPE_ARG_TYPES (t) = argtypes;
3934 /* If we already have such a type, use the old one and free this one.
3935 Note that it also frees up the above cons cell if found. */
3936 hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) +
3937 type_hash_list (argtypes);
3939 t = type_hash_canon (hashcode, t);
3941 if (!COMPLETE_TYPE_P (t))
3942 layout_type (t);
3944 return t;
3947 /* Construct, lay out and return the type of methods belonging to class
3948 BASETYPE and whose arguments and values are described by TYPE.
3949 If that type exists already, reuse it.
3950 TYPE must be a FUNCTION_TYPE node. */
3952 tree
3953 build_method_type (tree basetype, tree type)
3955 if (TREE_CODE (type) != FUNCTION_TYPE)
3956 abort ();
3958 return build_method_type_directly (basetype,
3959 TREE_TYPE (type),
3960 TYPE_ARG_TYPES (type));
3963 /* Construct, lay out and return the type of offsets to a value
3964 of type TYPE, within an object of type BASETYPE.
3965 If a suitable offset type exists already, reuse it. */
3967 tree
3968 build_offset_type (tree basetype, tree type)
3970 tree t;
3971 unsigned int hashcode;
3973 /* Make a node of the sort we want. */
3974 t = make_node (OFFSET_TYPE);
3976 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3977 TREE_TYPE (t) = type;
3979 /* If we already have such a type, use the old one and free this one. */
3980 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
3981 t = type_hash_canon (hashcode, t);
3983 if (!COMPLETE_TYPE_P (t))
3984 layout_type (t);
3986 return t;
3989 /* Create a complex type whose components are COMPONENT_TYPE. */
3991 tree
3992 build_complex_type (tree component_type)
3994 tree t;
3995 unsigned int hashcode;
3997 /* Make a node of the sort we want. */
3998 t = make_node (COMPLEX_TYPE);
4000 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
4001 set_type_quals (t, TYPE_QUALS (component_type));
4003 /* If we already have such a type, use the old one and free this one. */
4004 hashcode = TYPE_HASH (component_type);
4005 t = type_hash_canon (hashcode, t);
4007 if (!COMPLETE_TYPE_P (t))
4008 layout_type (t);
4010 /* If we are writing Dwarf2 output we need to create a name,
4011 since complex is a fundamental type. */
4012 if ((write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
4013 && ! TYPE_NAME (t))
4015 const char *name;
4016 if (component_type == char_type_node)
4017 name = "complex char";
4018 else if (component_type == signed_char_type_node)
4019 name = "complex signed char";
4020 else if (component_type == unsigned_char_type_node)
4021 name = "complex unsigned char";
4022 else if (component_type == short_integer_type_node)
4023 name = "complex short int";
4024 else if (component_type == short_unsigned_type_node)
4025 name = "complex short unsigned int";
4026 else if (component_type == integer_type_node)
4027 name = "complex int";
4028 else if (component_type == unsigned_type_node)
4029 name = "complex unsigned int";
4030 else if (component_type == long_integer_type_node)
4031 name = "complex long int";
4032 else if (component_type == long_unsigned_type_node)
4033 name = "complex long unsigned int";
4034 else if (component_type == long_long_integer_type_node)
4035 name = "complex long long int";
4036 else if (component_type == long_long_unsigned_type_node)
4037 name = "complex long long unsigned int";
4038 else
4039 name = 0;
4041 if (name != 0)
4042 TYPE_NAME (t) = get_identifier (name);
4045 return t;
4048 /* Return OP, stripped of any conversions to wider types as much as is safe.
4049 Converting the value back to OP's type makes a value equivalent to OP.
4051 If FOR_TYPE is nonzero, we return a value which, if converted to
4052 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
4054 If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
4055 narrowest type that can hold the value, even if they don't exactly fit.
4056 Otherwise, bit-field references are changed to a narrower type
4057 only if they can be fetched directly from memory in that type.
4059 OP must have integer, real or enumeral type. Pointers are not allowed!
4061 There are some cases where the obvious value we could return
4062 would regenerate to OP if converted to OP's type,
4063 but would not extend like OP to wider types.
4064 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
4065 For example, if OP is (unsigned short)(signed char)-1,
4066 we avoid returning (signed char)-1 if FOR_TYPE is int,
4067 even though extending that to an unsigned short would regenerate OP,
4068 since the result of extending (signed char)-1 to (int)
4069 is different from (int) OP. */
4071 tree
4072 get_unwidened (tree op, tree for_type)
4074 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
4075 tree type = TREE_TYPE (op);
4076 unsigned final_prec
4077 = TYPE_PRECISION (for_type != 0 ? for_type : type);
4078 int uns
4079 = (for_type != 0 && for_type != type
4080 && final_prec > TYPE_PRECISION (type)
4081 && TREE_UNSIGNED (type));
4082 tree win = op;
4084 while (TREE_CODE (op) == NOP_EXPR)
4086 int bitschange
4087 = TYPE_PRECISION (TREE_TYPE (op))
4088 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
4090 /* Truncations are many-one so cannot be removed.
4091 Unless we are later going to truncate down even farther. */
4092 if (bitschange < 0
4093 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
4094 break;
4096 /* See what's inside this conversion. If we decide to strip it,
4097 we will set WIN. */
4098 op = TREE_OPERAND (op, 0);
4100 /* If we have not stripped any zero-extensions (uns is 0),
4101 we can strip any kind of extension.
4102 If we have previously stripped a zero-extension,
4103 only zero-extensions can safely be stripped.
4104 Any extension can be stripped if the bits it would produce
4105 are all going to be discarded later by truncating to FOR_TYPE. */
4107 if (bitschange > 0)
4109 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
4110 win = op;
4111 /* TREE_UNSIGNED says whether this is a zero-extension.
4112 Let's avoid computing it if it does not affect WIN
4113 and if UNS will not be needed again. */
4114 if ((uns || TREE_CODE (op) == NOP_EXPR)
4115 && TREE_UNSIGNED (TREE_TYPE (op)))
4117 uns = 1;
4118 win = op;
4123 if (TREE_CODE (op) == COMPONENT_REF
4124 /* Since type_for_size always gives an integer type. */
4125 && TREE_CODE (type) != REAL_TYPE
4126 /* Don't crash if field not laid out yet. */
4127 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
4128 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
4130 unsigned int innerprec
4131 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4132 int unsignedp = TREE_UNSIGNED (TREE_OPERAND (op, 1));
4133 type = (*lang_hooks.types.type_for_size) (innerprec, unsignedp);
4135 /* We can get this structure field in the narrowest type it fits in.
4136 If FOR_TYPE is 0, do this only for a field that matches the
4137 narrower type exactly and is aligned for it
4138 The resulting extension to its nominal type (a fullword type)
4139 must fit the same conditions as for other extensions. */
4141 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
4142 && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
4143 && (! uns || final_prec <= innerprec || unsignedp)
4144 && type != 0)
4146 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4147 TREE_OPERAND (op, 1));
4148 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4149 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4153 return win;
4156 /* Return OP or a simpler expression for a narrower value
4157 which can be sign-extended or zero-extended to give back OP.
4158 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
4159 or 0 if the value should be sign-extended. */
4161 tree
4162 get_narrower (tree op, int *unsignedp_ptr)
4164 int uns = 0;
4165 int first = 1;
4166 tree win = op;
4168 while (TREE_CODE (op) == NOP_EXPR)
4170 int bitschange
4171 = (TYPE_PRECISION (TREE_TYPE (op))
4172 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
4174 /* Truncations are many-one so cannot be removed. */
4175 if (bitschange < 0)
4176 break;
4178 /* See what's inside this conversion. If we decide to strip it,
4179 we will set WIN. */
4181 if (bitschange > 0)
4183 op = TREE_OPERAND (op, 0);
4184 /* An extension: the outermost one can be stripped,
4185 but remember whether it is zero or sign extension. */
4186 if (first)
4187 uns = TREE_UNSIGNED (TREE_TYPE (op));
4188 /* Otherwise, if a sign extension has been stripped,
4189 only sign extensions can now be stripped;
4190 if a zero extension has been stripped, only zero-extensions. */
4191 else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
4192 break;
4193 first = 0;
4195 else /* bitschange == 0 */
4197 /* A change in nominal type can always be stripped, but we must
4198 preserve the unsignedness. */
4199 if (first)
4200 uns = TREE_UNSIGNED (TREE_TYPE (op));
4201 first = 0;
4202 op = TREE_OPERAND (op, 0);
4205 win = op;
4208 if (TREE_CODE (op) == COMPONENT_REF
4209 /* Since type_for_size always gives an integer type. */
4210 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
4211 /* Ensure field is laid out already. */
4212 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0)
4214 unsigned HOST_WIDE_INT innerprec
4215 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4216 tree type = (*lang_hooks.types.type_for_size) (innerprec,
4217 TREE_UNSIGNED (op));
4219 /* We can get this structure field in a narrower type that fits it,
4220 but the resulting extension to its nominal type (a fullword type)
4221 must satisfy the same conditions as for other extensions.
4223 Do this only for fields that are aligned (not bit-fields),
4224 because when bit-field insns will be used there is no
4225 advantage in doing this. */
4227 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
4228 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
4229 && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
4230 && type != 0)
4232 if (first)
4233 uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
4234 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4235 TREE_OPERAND (op, 1));
4236 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4237 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4240 *unsignedp_ptr = uns;
4241 return win;
4244 /* Nonzero if integer constant C has a value that is permissible
4245 for type TYPE (an INTEGER_TYPE). */
4248 int_fits_type_p (tree c, tree type)
4250 tree type_low_bound = TYPE_MIN_VALUE (type);
4251 tree type_high_bound = TYPE_MAX_VALUE (type);
4252 int ok_for_low_bound, ok_for_high_bound;
4254 /* Perform some generic filtering first, which may allow making a decision
4255 even if the bounds are not constant. First, negative integers never fit
4256 in unsigned types, */
4257 if ((TREE_UNSIGNED (type) && tree_int_cst_sgn (c) < 0)
4258 /* Also, unsigned integers with top bit set never fit signed types. */
4259 || (! TREE_UNSIGNED (type)
4260 && TREE_UNSIGNED (TREE_TYPE (c)) && tree_int_cst_msb (c)))
4261 return 0;
4263 /* If at least one bound of the type is a constant integer, we can check
4264 ourselves and maybe make a decision. If no such decision is possible, but
4265 this type is a subtype, try checking against that. Otherwise, use
4266 force_fit_type, which checks against the precision.
4268 Compute the status for each possibly constant bound, and return if we see
4269 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
4270 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
4271 for "constant known to fit". */
4273 ok_for_low_bound = -1;
4274 ok_for_high_bound = -1;
4276 /* Check if C >= type_low_bound. */
4277 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
4279 ok_for_low_bound = ! tree_int_cst_lt (c, type_low_bound);
4280 if (! ok_for_low_bound)
4281 return 0;
4284 /* Check if c <= type_high_bound. */
4285 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
4287 ok_for_high_bound = ! tree_int_cst_lt (type_high_bound, c);
4288 if (! ok_for_high_bound)
4289 return 0;
4292 /* If the constant fits both bounds, the result is known. */
4293 if (ok_for_low_bound == 1 && ok_for_high_bound == 1)
4294 return 1;
4296 /* If we haven't been able to decide at this point, there nothing more we
4297 can check ourselves here. Look at the base type if we have one. */
4298 else if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != 0)
4299 return int_fits_type_p (c, TREE_TYPE (type));
4301 /* Or to force_fit_type, if nothing else. */
4302 else
4304 c = copy_node (c);
4305 TREE_TYPE (c) = type;
4306 return !force_fit_type (c, 0);
4310 /* Returns true if T is, contains, or refers to a type with variable
4311 size. This concept is more general than that of C99 'variably
4312 modified types': in C99, a struct type is never variably modified
4313 because a VLA may not appear as a structure member. However, in
4314 GNU C code like:
4316 struct S { int i[f()]; };
4318 is valid, and other languages may define similar constructs. */
4320 bool
4321 variably_modified_type_p (tree type)
4323 if (type == error_mark_node)
4324 return false;
4326 /* If TYPE itself has variable size, it is variably modified.
4328 We do not yet have a representation of the C99 '[*]' syntax.
4329 When a representation is chosen, this function should be modified
4330 to test for that case as well. */
4331 if (TYPE_SIZE (type)
4332 && TYPE_SIZE (type) != error_mark_node
4333 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4334 return true;
4336 /* If TYPE is a pointer or reference, it is variably modified if
4337 the type pointed to is variably modified. */
4338 if ((TREE_CODE (type) == POINTER_TYPE
4339 || TREE_CODE (type) == REFERENCE_TYPE)
4340 && variably_modified_type_p (TREE_TYPE (type)))
4341 return true;
4343 /* If TYPE is an array, it is variably modified if the array
4344 elements are. (Note that the VLA case has already been checked
4345 above.) */
4346 if (TREE_CODE (type) == ARRAY_TYPE
4347 && variably_modified_type_p (TREE_TYPE (type)))
4348 return true;
4350 /* If TYPE is a function type, it is variably modified if any of the
4351 parameters or the return type are variably modified. */
4352 if (TREE_CODE (type) == FUNCTION_TYPE
4353 || TREE_CODE (type) == METHOD_TYPE)
4355 tree parm;
4357 if (variably_modified_type_p (TREE_TYPE (type)))
4358 return true;
4359 for (parm = TYPE_ARG_TYPES (type);
4360 parm && parm != void_list_node;
4361 parm = TREE_CHAIN (parm))
4362 if (variably_modified_type_p (TREE_VALUE (parm)))
4363 return true;
4366 /* The current language may have other cases to check, but in general,
4367 all other types are not variably modified. */
4368 return (*lang_hooks.tree_inlining.var_mod_type_p) (type);
4371 /* Given a DECL or TYPE, return the scope in which it was declared, or
4372 NULL_TREE if there is no containing scope. */
4374 tree
4375 get_containing_scope (tree t)
4377 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
4380 /* Return the innermost context enclosing DECL that is
4381 a FUNCTION_DECL, or zero if none. */
4383 tree
4384 decl_function_context (tree decl)
4386 tree context;
4388 if (TREE_CODE (decl) == ERROR_MARK)
4389 return 0;
4391 if (TREE_CODE (decl) == SAVE_EXPR)
4392 context = SAVE_EXPR_CONTEXT (decl);
4394 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
4395 where we look up the function at runtime. Such functions always take
4396 a first argument of type 'pointer to real context'.
4398 C++ should really be fixed to use DECL_CONTEXT for the real context,
4399 and use something else for the "virtual context". */
4400 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
4401 context
4402 = TYPE_MAIN_VARIANT
4403 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4404 else
4405 context = DECL_CONTEXT (decl);
4407 while (context && TREE_CODE (context) != FUNCTION_DECL)
4409 if (TREE_CODE (context) == BLOCK)
4410 context = BLOCK_SUPERCONTEXT (context);
4411 else
4412 context = get_containing_scope (context);
4415 return context;
4418 /* Return the innermost context enclosing DECL that is
4419 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
4420 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
4422 tree
4423 decl_type_context (tree decl)
4425 tree context = DECL_CONTEXT (decl);
4427 while (context)
4428 switch (TREE_CODE (context))
4430 case NAMESPACE_DECL:
4431 case TRANSLATION_UNIT_DECL:
4432 return NULL_TREE;
4434 case RECORD_TYPE:
4435 case UNION_TYPE:
4436 case QUAL_UNION_TYPE:
4437 return context;
4439 case TYPE_DECL:
4440 case FUNCTION_DECL:
4441 context = DECL_CONTEXT (context);
4442 break;
4444 case BLOCK:
4445 context = BLOCK_SUPERCONTEXT (context);
4446 break;
4448 default:
4449 abort ();
4452 return NULL_TREE;
4455 /* CALL is a CALL_EXPR. Return the declaration for the function
4456 called, or NULL_TREE if the called function cannot be
4457 determined. */
4459 tree
4460 get_callee_fndecl (tree call)
4462 tree addr;
4464 /* It's invalid to call this function with anything but a
4465 CALL_EXPR. */
4466 if (TREE_CODE (call) != CALL_EXPR)
4467 abort ();
4469 /* The first operand to the CALL is the address of the function
4470 called. */
4471 addr = TREE_OPERAND (call, 0);
4473 STRIP_NOPS (addr);
4475 /* If this is a readonly function pointer, extract its initial value. */
4476 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
4477 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
4478 && DECL_INITIAL (addr))
4479 addr = DECL_INITIAL (addr);
4481 /* If the address is just `&f' for some function `f', then we know
4482 that `f' is being called. */
4483 if (TREE_CODE (addr) == ADDR_EXPR
4484 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
4485 return TREE_OPERAND (addr, 0);
4487 /* We couldn't figure out what was being called. */
4488 return NULL_TREE;
4491 /* Print debugging information about tree nodes generated during the compile,
4492 and any language-specific information. */
4494 void
4495 dump_tree_statistics (void)
4497 #ifdef GATHER_STATISTICS
4498 int i;
4499 int total_nodes, total_bytes;
4500 #endif
4502 fprintf (stderr, "\n??? tree nodes created\n\n");
4503 #ifdef GATHER_STATISTICS
4504 fprintf (stderr, "Kind Nodes Bytes\n");
4505 fprintf (stderr, "---------------------------------------\n");
4506 total_nodes = total_bytes = 0;
4507 for (i = 0; i < (int) all_kinds; i++)
4509 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
4510 tree_node_counts[i], tree_node_sizes[i]);
4511 total_nodes += tree_node_counts[i];
4512 total_bytes += tree_node_sizes[i];
4514 fprintf (stderr, "---------------------------------------\n");
4515 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
4516 fprintf (stderr, "---------------------------------------\n");
4517 #else
4518 fprintf (stderr, "(No per-node statistics)\n");
4519 #endif
4520 print_type_hash_statistics ();
4521 (*lang_hooks.print_statistics) ();
4524 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
4526 /* Generate a crc32 of a string. */
4528 unsigned
4529 crc32_string (unsigned chksum, const char *string)
4533 unsigned value = *string << 24;
4534 unsigned ix;
4536 for (ix = 8; ix--; value <<= 1)
4538 unsigned feedback;
4540 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
4541 chksum <<= 1;
4542 chksum ^= feedback;
4545 while (*string++);
4546 return chksum;
4549 /* P is a string that will be used in a symbol. Mask out any characters
4550 that are not valid in that context. */
4552 void
4553 clean_symbol_name (char *p)
4555 for (; *p; p++)
4556 if (! (ISALNUM (*p)
4557 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
4558 || *p == '$'
4559 #endif
4560 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
4561 || *p == '.'
4562 #endif
4564 *p = '_';
4567 /* Generate a name for a function unique to this translation unit.
4568 TYPE is some string to identify the purpose of this function to the
4569 linker or collect2. */
4571 tree
4572 get_file_function_name_long (const char *type)
4574 char *buf;
4575 const char *p;
4576 char *q;
4578 if (first_global_object_name)
4579 p = first_global_object_name;
4580 else
4582 /* We don't have anything that we know to be unique to this translation
4583 unit, so use what we do have and throw in some randomness. */
4584 unsigned len;
4585 const char *name = weak_global_object_name;
4586 const char *file = main_input_filename;
4588 if (! name)
4589 name = "";
4590 if (! file)
4591 file = input_filename;
4593 len = strlen (file);
4594 q = alloca (9 * 2 + len + 1);
4595 memcpy (q, file, len + 1);
4596 clean_symbol_name (q);
4598 sprintf (q + len, "_%08X_%08X", crc32_string (0, name),
4599 crc32_string (0, flag_random_seed));
4601 p = q;
4604 buf = alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p) + strlen (type));
4606 /* Set up the name of the file-level functions we may need.
4607 Use a global object (which is already required to be unique over
4608 the program) rather than the file name (which imposes extra
4609 constraints). */
4610 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
4612 return get_identifier (buf);
4615 /* If KIND=='I', return a suitable global initializer (constructor) name.
4616 If KIND=='D', return a suitable global clean-up (destructor) name. */
4618 tree
4619 get_file_function_name (int kind)
4621 char p[2];
4623 p[0] = kind;
4624 p[1] = 0;
4626 return get_file_function_name_long (p);
4629 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4630 The result is placed in BUFFER (which has length BIT_SIZE),
4631 with one bit in each char ('\000' or '\001').
4633 If the constructor is constant, NULL_TREE is returned.
4634 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4636 tree
4637 get_set_constructor_bits (tree init, char *buffer, int bit_size)
4639 int i;
4640 tree vals;
4641 HOST_WIDE_INT domain_min
4642 = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (init))), 0);
4643 tree non_const_bits = NULL_TREE;
4645 for (i = 0; i < bit_size; i++)
4646 buffer[i] = 0;
4648 for (vals = TREE_OPERAND (init, 1);
4649 vals != NULL_TREE; vals = TREE_CHAIN (vals))
4651 if (!host_integerp (TREE_VALUE (vals), 0)
4652 || (TREE_PURPOSE (vals) != NULL_TREE
4653 && !host_integerp (TREE_PURPOSE (vals), 0)))
4654 non_const_bits
4655 = tree_cons (TREE_PURPOSE (vals), TREE_VALUE (vals), non_const_bits);
4656 else if (TREE_PURPOSE (vals) != NULL_TREE)
4658 /* Set a range of bits to ones. */
4659 HOST_WIDE_INT lo_index
4660 = tree_low_cst (TREE_PURPOSE (vals), 0) - domain_min;
4661 HOST_WIDE_INT hi_index
4662 = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
4664 if (lo_index < 0 || lo_index >= bit_size
4665 || hi_index < 0 || hi_index >= bit_size)
4666 abort ();
4667 for (; lo_index <= hi_index; lo_index++)
4668 buffer[lo_index] = 1;
4670 else
4672 /* Set a single bit to one. */
4673 HOST_WIDE_INT index
4674 = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
4675 if (index < 0 || index >= bit_size)
4677 error ("invalid initializer for bit string");
4678 return NULL_TREE;
4680 buffer[index] = 1;
4683 return non_const_bits;
4686 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4687 The result is placed in BUFFER (which is an array of bytes).
4688 If the constructor is constant, NULL_TREE is returned.
4689 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4691 tree
4692 get_set_constructor_bytes (tree init, unsigned char *buffer, int wd_size)
4694 int i;
4695 int set_word_size = BITS_PER_UNIT;
4696 int bit_size = wd_size * set_word_size;
4697 int bit_pos = 0;
4698 unsigned char *bytep = buffer;
4699 char *bit_buffer = alloca (bit_size);
4700 tree non_const_bits = get_set_constructor_bits (init, bit_buffer, bit_size);
4702 for (i = 0; i < wd_size; i++)
4703 buffer[i] = 0;
4705 for (i = 0; i < bit_size; i++)
4707 if (bit_buffer[i])
4709 if (BYTES_BIG_ENDIAN)
4710 *bytep |= (1 << (set_word_size - 1 - bit_pos));
4711 else
4712 *bytep |= 1 << bit_pos;
4714 bit_pos++;
4715 if (bit_pos >= set_word_size)
4716 bit_pos = 0, bytep++;
4718 return non_const_bits;
4721 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4722 /* Complain that the tree code of NODE does not match the expected CODE.
4723 FILE, LINE, and FUNCTION are of the caller. */
4725 void
4726 tree_check_failed (const tree node, enum tree_code code, const char *file,
4727 int line, const char *function)
4729 internal_error ("tree check: expected %s, have %s in %s, at %s:%d",
4730 tree_code_name[code], tree_code_name[TREE_CODE (node)],
4731 function, trim_filename (file), line);
4734 /* Similar to above, except that we check for a class of tree
4735 code, given in CL. */
4737 void
4738 tree_class_check_failed (const tree node, int cl, const char *file,
4739 int line, const char *function)
4741 internal_error
4742 ("tree check: expected class '%c', have '%c' (%s) in %s, at %s:%d",
4743 cl, TREE_CODE_CLASS (TREE_CODE (node)),
4744 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
4747 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
4748 (dynamically sized) vector. */
4750 void
4751 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
4752 const char *function)
4754 internal_error
4755 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
4756 idx + 1, len, function, trim_filename (file), line);
4759 /* Similar to above, except that the check is for the bounds of a EPHI_NODE's
4760 (dynamically sized) vector. */
4762 void
4763 ephi_node_elt_check_failed (int idx, int len, const char *file, int line,
4764 const char *function)
4766 internal_error
4767 ("tree check: accessed elt %d of ephi_node with %d elts in %s, at %s:%d",
4768 idx + 1, len, function, trim_filename (file), line);
4771 /* Similar to above, except that the check is for the bounds of a PHI_NODE's
4772 (dynamically sized) vector. */
4774 void
4775 phi_node_elt_check_failed (int idx, int len, const char *file, int line,
4776 const char *function)
4778 internal_error
4779 ("tree check: accessed elt %d of phi_node with %d elts in %s, at %s:%d",
4780 idx + 1, len, function, trim_filename (file), line);
4783 /* Similar to above, except that the check is for the bounds of the operand
4784 vector of an expression node. */
4786 void
4787 tree_operand_check_failed (int idx, enum tree_code code, const char *file,
4788 int line, const char *function)
4790 internal_error
4791 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
4792 idx + 1, tree_code_name[code], TREE_CODE_LENGTH (code),
4793 function, trim_filename (file), line);
4795 #endif /* ENABLE_TREE_CHECKING */
4797 /* For a new vector type node T, build the information necessary for
4798 debugging output. */
4800 static void
4801 finish_vector_type (tree t)
4803 layout_type (t);
4806 tree index = build_int_2 (TYPE_VECTOR_SUBPARTS (t) - 1, 0);
4807 tree array = build_array_type (TREE_TYPE (t),
4808 build_index_type (index));
4809 tree rt = make_node (RECORD_TYPE);
4811 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
4812 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
4813 layout_type (rt);
4814 TYPE_DEBUG_REPRESENTATION_TYPE (t) = rt;
4815 /* In dwarfout.c, type lookup uses TYPE_UID numbers. We want to output
4816 the representation type, and we want to find that die when looking up
4817 the vector type. This is most easily achieved by making the TYPE_UID
4818 numbers equal. */
4819 TYPE_UID (rt) = TYPE_UID (t);
4823 /* Create nodes for all integer types (and error_mark_node) using the sizes
4824 of C datatypes. The caller should call set_sizetype soon after calling
4825 this function to select one of the types as sizetype. */
4827 void
4828 build_common_tree_nodes (int signed_char)
4830 error_mark_node = make_node (ERROR_MARK);
4831 TREE_TYPE (error_mark_node) = error_mark_node;
4833 initialize_sizetypes ();
4835 /* Define both `signed char' and `unsigned char'. */
4836 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
4837 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
4839 /* Define `char', which is like either `signed char' or `unsigned char'
4840 but not the same as either. */
4841 char_type_node
4842 = (signed_char
4843 ? make_signed_type (CHAR_TYPE_SIZE)
4844 : make_unsigned_type (CHAR_TYPE_SIZE));
4846 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
4847 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
4848 integer_type_node = make_signed_type (INT_TYPE_SIZE);
4849 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
4850 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
4851 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
4852 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
4853 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
4855 /* Define a boolean type. This type only represents boolean values but
4856 may be larger than char depending on the value of BOOL_TYPE_SIZE.
4857 Front ends which want to override this size (i.e. Java) can redefine
4858 boolean_type_node before calling build_common_tree_nodes_2. */
4859 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
4860 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
4861 TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
4862 TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
4863 TYPE_PRECISION (boolean_type_node) = 1;
4865 intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
4866 intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
4867 intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
4868 intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
4869 intTI_type_node = make_signed_type (GET_MODE_BITSIZE (TImode));
4871 unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
4872 unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
4873 unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
4874 unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
4875 unsigned_intTI_type_node = make_unsigned_type (GET_MODE_BITSIZE (TImode));
4878 /* Call this function after calling build_common_tree_nodes and set_sizetype.
4879 It will create several other common tree nodes. */
4881 void
4882 build_common_tree_nodes_2 (int short_double)
4884 /* Define these next since types below may used them. */
4885 integer_zero_node = build_int_2 (0, 0);
4886 integer_one_node = build_int_2 (1, 0);
4887 integer_minus_one_node = build_int_2 (-1, -1);
4889 size_zero_node = size_int (0);
4890 size_one_node = size_int (1);
4891 bitsize_zero_node = bitsize_int (0);
4892 bitsize_one_node = bitsize_int (1);
4893 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
4895 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
4896 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
4898 void_type_node = make_node (VOID_TYPE);
4899 layout_type (void_type_node);
4901 /* We are not going to have real types in C with less than byte alignment,
4902 so we might as well not have any types that claim to have it. */
4903 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
4904 TYPE_USER_ALIGN (void_type_node) = 0;
4906 null_pointer_node = build_int_2 (0, 0);
4907 TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
4908 layout_type (TREE_TYPE (null_pointer_node));
4910 ptr_type_node = build_pointer_type (void_type_node);
4911 const_ptr_type_node
4912 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
4914 float_type_node = make_node (REAL_TYPE);
4915 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
4916 layout_type (float_type_node);
4918 double_type_node = make_node (REAL_TYPE);
4919 if (short_double)
4920 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
4921 else
4922 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
4923 layout_type (double_type_node);
4925 long_double_type_node = make_node (REAL_TYPE);
4926 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
4927 layout_type (long_double_type_node);
4929 float_ptr_type_node = build_pointer_type (float_type_node);
4930 double_ptr_type_node = build_pointer_type (double_type_node);
4931 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
4932 integer_ptr_type_node = build_pointer_type (integer_type_node);
4934 complex_integer_type_node = make_node (COMPLEX_TYPE);
4935 TREE_TYPE (complex_integer_type_node) = integer_type_node;
4936 layout_type (complex_integer_type_node);
4938 complex_float_type_node = make_node (COMPLEX_TYPE);
4939 TREE_TYPE (complex_float_type_node) = float_type_node;
4940 layout_type (complex_float_type_node);
4942 complex_double_type_node = make_node (COMPLEX_TYPE);
4943 TREE_TYPE (complex_double_type_node) = double_type_node;
4944 layout_type (complex_double_type_node);
4946 complex_long_double_type_node = make_node (COMPLEX_TYPE);
4947 TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
4948 layout_type (complex_long_double_type_node);
4951 tree t;
4952 BUILD_VA_LIST_TYPE (t);
4954 /* Many back-ends define record types without setting TYPE_NAME.
4955 If we copied the record type here, we'd keep the original
4956 record type without a name. This breaks name mangling. So,
4957 don't copy record types and let c_common_nodes_and_builtins()
4958 declare the type to be __builtin_va_list. */
4959 if (TREE_CODE (t) != RECORD_TYPE)
4960 t = build_type_copy (t);
4962 va_list_type_node = t;
4965 unsigned_V4SI_type_node
4966 = make_vector (V4SImode, unsigned_intSI_type_node, 1);
4967 unsigned_V2HI_type_node
4968 = make_vector (V2HImode, unsigned_intHI_type_node, 1);
4969 unsigned_V2SI_type_node
4970 = make_vector (V2SImode, unsigned_intSI_type_node, 1);
4971 unsigned_V2DI_type_node
4972 = make_vector (V2DImode, unsigned_intDI_type_node, 1);
4973 unsigned_V4HI_type_node
4974 = make_vector (V4HImode, unsigned_intHI_type_node, 1);
4975 unsigned_V8QI_type_node
4976 = make_vector (V8QImode, unsigned_intQI_type_node, 1);
4977 unsigned_V8HI_type_node
4978 = make_vector (V8HImode, unsigned_intHI_type_node, 1);
4979 unsigned_V16QI_type_node
4980 = make_vector (V16QImode, unsigned_intQI_type_node, 1);
4981 unsigned_V1DI_type_node
4982 = make_vector (V1DImode, unsigned_intDI_type_node, 1);
4984 V16SF_type_node = make_vector (V16SFmode, float_type_node, 0);
4985 V4SF_type_node = make_vector (V4SFmode, float_type_node, 0);
4986 V4SI_type_node = make_vector (V4SImode, intSI_type_node, 0);
4987 V2HI_type_node = make_vector (V2HImode, intHI_type_node, 0);
4988 V2SI_type_node = make_vector (V2SImode, intSI_type_node, 0);
4989 V2DI_type_node = make_vector (V2DImode, intDI_type_node, 0);
4990 V4HI_type_node = make_vector (V4HImode, intHI_type_node, 0);
4991 V8QI_type_node = make_vector (V8QImode, intQI_type_node, 0);
4992 V8HI_type_node = make_vector (V8HImode, intHI_type_node, 0);
4993 V2SF_type_node = make_vector (V2SFmode, float_type_node, 0);
4994 V2DF_type_node = make_vector (V2DFmode, double_type_node, 0);
4995 V16QI_type_node = make_vector (V16QImode, intQI_type_node, 0);
4996 V1DI_type_node = make_vector (V1DImode, intDI_type_node, 0);
4997 V4DF_type_node = make_vector (V4DFmode, double_type_node, 0);
5000 /* Returns a vector tree node given a vector mode, the inner type, and
5001 the signness. */
5003 static tree
5004 make_vector (enum machine_mode mode, tree innertype, int unsignedp)
5006 tree t;
5008 t = make_node (VECTOR_TYPE);
5009 TREE_TYPE (t) = innertype;
5010 TYPE_MODE (t) = mode;
5011 TREE_UNSIGNED (TREE_TYPE (t)) = unsignedp;
5012 finish_vector_type (t);
5014 return t;
5017 /* Given an initializer INIT, return TRUE if INIT is zero or some
5018 aggregate of zeros. Otherwise return FALSE. */
5020 bool
5021 initializer_zerop (tree init)
5023 STRIP_NOPS (init);
5025 switch (TREE_CODE (init))
5027 case INTEGER_CST:
5028 return integer_zerop (init);
5029 case REAL_CST:
5030 return real_zerop (init)
5031 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
5032 case COMPLEX_CST:
5033 return integer_zerop (init)
5034 || (real_zerop (init)
5035 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
5036 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
5037 case CONSTRUCTOR:
5039 if (AGGREGATE_TYPE_P (TREE_TYPE (init)))
5041 tree aggr_init = CONSTRUCTOR_ELTS (init);
5043 while (aggr_init)
5045 if (! initializer_zerop (TREE_VALUE (aggr_init)))
5046 return false;
5047 aggr_init = TREE_CHAIN (aggr_init);
5049 return true;
5051 return false;
5053 default:
5054 return false;
5058 void
5059 add_var_to_bind_expr (tree bind_expr, tree var)
5061 BIND_EXPR_VARS (bind_expr)
5062 = chainon (BIND_EXPR_VARS (bind_expr), var);
5063 if (BIND_EXPR_BLOCK (bind_expr))
5064 BLOCK_VARS (BIND_EXPR_BLOCK (bind_expr))
5065 = BIND_EXPR_VARS (bind_expr);
5069 /* Return a new PHI_NODE for variable VAR and LEN number of arguments. */
5071 tree
5072 make_phi_node (tree var, int len)
5074 tree phi;
5075 int size;
5077 size = sizeof (struct tree_phi_node) + (len - 1) * sizeof (struct phi_arg_d);
5079 #ifdef GATHER_STATISTICS
5080 tree_node_counts[(int) phi_kind]++;
5081 tree_node_sizes[(int) phi_kind] += size;
5082 #endif
5084 phi = ggc_alloc_tree (size);
5085 memset ((PTR) phi, 0, size);
5087 TREE_SET_CODE (phi, PHI_NODE);
5088 PHI_NUM_ARGS (phi) = 0;
5089 PHI_ARG_CAPACITY (phi) = len;
5090 PHI_RESULT (phi) = make_ssa_name (var, phi);
5092 return phi;
5096 /* Resize an existing PHI node. The only way is up. Return the
5097 possibly relocated phi. */
5099 void
5100 resize_phi_node (tree *phi, int len)
5102 int size;
5103 tree new_phi;
5104 int i, old_len;
5106 #ifdef ENABLE_CHECKING
5107 if (len < PHI_ARG_CAPACITY (*phi))
5108 abort ();
5109 #endif
5111 size = sizeof (struct tree_phi_node) + (len - 1) * sizeof (struct phi_arg_d);
5112 new_phi = ggc_realloc (*phi, size);
5113 old_len = PHI_ARG_CAPACITY (new_phi);
5114 PHI_ARG_CAPACITY (new_phi) = len;
5116 for (i = old_len; i < len; i++)
5118 PHI_ARG_DEF (new_phi, i) = NULL_TREE;
5119 PHI_ARG_EDGE (new_phi, i) = NULL;
5122 *phi = new_phi;
5126 /* Return an SSA_NAME node for variable VAR defined in statement STMT.
5127 STMT may be an empty statement for artificial references (e.g., default
5128 definitions created when a variable is used without a preceding
5129 definition). */
5131 tree
5132 make_ssa_name (tree var, tree stmt)
5134 tree t;
5136 #if defined ENABLE_CHECKING
5137 if ((!DECL_P (var)
5138 && TREE_CODE (var) != INDIRECT_REF)
5139 || (!IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (stmt)))
5140 && TREE_CODE (stmt) != PHI_NODE))
5141 abort ();
5142 #endif
5144 t = make_node (SSA_NAME);
5146 TREE_TYPE (t) = TREE_TYPE (var);
5147 SSA_NAME_VAR (t) = var;
5148 SSA_NAME_DEF_STMT (t) = stmt;
5149 SSA_NAME_VERSION (t) = next_ssa_version++;
5151 return t;
5155 /* Build a VDEF_EXPR node for variable VAR. This creates the pseudo
5156 assignment VAR = VDEF <VAR>. The SSA builder is responsible for
5157 creating the new SSA name for the result and rewriting the RHS with the
5158 appropriate reaching definition. */
5160 tree
5161 build_vdef_expr (tree var)
5163 if (!DECL_P (var) && TREE_CODE (var) != INDIRECT_REF)
5164 abort ();
5166 return build (VDEF_EXPR, TREE_TYPE (var), var, var);
5170 /* Build an empty statement. */
5172 tree
5173 build_empty_stmt (void)
5175 return build1 (NOP_EXPR, void_type_node, size_zero_node);
5179 /* Links a stmt before the current stmt. */
5181 void
5182 tsi_link_before (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
5184 tree ce;
5186 if (mode == TSI_CHAIN_START || mode == TSI_CHAIN_END)
5187 abort ();
5188 if (mode == TSI_CONTINUE_LINKING)
5189 mode = TSI_NEW_STMT;
5191 /* Build a new CE which points to the current node. */
5192 ce = build (COMPOUND_EXPR, void_type_node, t, *i->tp);
5194 /* Make the parent pointer point to this new node. At this point, the
5195 iterator will be pointing to the new node we just inserted. */
5196 *i->tp = ce;
5198 /* Update the iterator to points to the address of the next ptr in our new
5199 node, which is the current stmt again. */
5200 if (mode == TSI_SAME_STMT)
5201 i->tp = &TREE_OPERAND (ce, 1);
5204 /* Links a stmt after the current stmt. */
5206 void
5207 tsi_link_after (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
5209 tree ce;
5210 tree next, cur = *i->tp;
5212 if (mode == TSI_CHAIN_START || mode == TSI_CHAIN_END)
5213 abort ();
5214 if (mode == TSI_CONTINUE_LINKING)
5215 mode = TSI_NEW_STMT;
5217 /* If this node is empty, we can just add it. */
5218 if (cur == NULL)
5220 *i->tp = t;
5223 /* If this node isnt a COMPUND_EXPR, we need to insert a CE node. */
5224 else if (TREE_CODE (cur) != COMPOUND_EXPR)
5226 /* Create a new node with the current stmt on the left, and the new
5227 stmt on the right. */
5228 ce = build (COMPOUND_EXPR, void_type_node, cur, t);
5230 /* Update link to point to this CE node. */
5231 *i->tp = ce;
5233 /* Change new iterator to point to the new stmt. */
5234 if (mode == TSI_NEW_STMT)
5235 i->tp = &TREE_OPERAND (ce, 1);
5237 else
5239 next = TREE_OPERAND (cur, 1);
5241 /* Create a new node with the same 'next' link as the current one. */
5242 ce = build (COMPOUND_EXPR, void_type_node, t, next);
5244 #if 0
5245 /* bsi_... iterators should be used for this!!! */
5246 /* If the 'next' statement is a COMPOUND_EXPR and was the first
5247 statement of a basic block, we need to adjust the 'head_tree_p'
5248 field of that block because we are about to move the statement one
5249 position down in the CE chain. */
5251 basic_block bb = bb_for_stmt (next);
5252 if (bb && bb->head_tree_p == &TREE_OPERAND (cur, 1))
5254 /* We may also need to update end_tree_p. */
5255 if (bb->head_tree_p == bb->end_tree_p)
5256 bb->end_tree_p = &TREE_OPERAND (ce, 1);
5257 bb->head_tree_p = &TREE_OPERAND (ce, 1);
5260 #endif
5262 /* Make the current one's 'next' link point to our new stmt. */
5263 TREE_OPERAND (cur, 1) = ce;
5265 /* Update the iterator to the new stmt. */
5266 if (mode == TSI_NEW_STMT)
5267 i->tp = &TREE_OPERAND (cur, 1);
5271 /* Links a chain of statements T before the current stmt. */
5273 void
5274 tsi_link_chain_before (tree_stmt_iterator *i, tree t,
5275 enum tsi_iterator_update mode)
5277 tree *last;
5279 if (mode == TSI_NEW_STMT)
5280 abort ();
5281 if (mode == TSI_CONTINUE_LINKING)
5282 mode = TSI_CHAIN_START;
5284 for (last = &t;
5285 TREE_CODE (*last) == COMPOUND_EXPR;
5286 last = &TREE_OPERAND (*last, 1))
5287 continue;
5289 /* Build a new CE which points to the current node. */
5290 *last = build (COMPOUND_EXPR, void_type_node, *last, *i->tp);
5292 /* Make the parent pointer point to this new node. At this point, the
5293 iterator will be pointing to the new node we just inserted. */
5294 *i->tp = t;
5296 /* Update the iterator to points to the address of the next ptr in our new
5297 node, which is the current stmt again. */
5298 if (mode == TSI_SAME_STMT)
5299 i->tp = &TREE_OPERAND (*last, 1);
5300 else if (mode == TSI_CHAIN_END)
5301 i->tp = last != &t ? last : i->tp;
5304 /* Links a chain of statements T after the current stmt. */
5306 void
5307 tsi_link_chain_after (tree_stmt_iterator *i, tree t,
5308 enum tsi_iterator_update mode)
5310 tree ce;
5311 tree next, *nextp;
5312 tree *last;
5313 tree cur = *i->tp;
5315 if (mode == TSI_NEW_STMT)
5316 abort ();
5317 if (mode == TSI_CONTINUE_LINKING)
5318 mode = TSI_CHAIN_END;
5320 /* If this node is empty, we can just add it. */
5321 if (cur == NULL)
5323 *i->tp = t;
5325 if (mode == TSI_CHAIN_END)
5327 cur = t;
5328 last = i->tp;
5329 while (TREE_CODE (cur) == COMPOUND_EXPR)
5331 last = &TREE_OPERAND (cur, 1);
5332 cur = *last;
5334 i->tp = last;
5338 /* If this node isnt a COMPUND_EXPR, we need to insert a CE node. */
5339 else if (TREE_CODE (cur) != COMPOUND_EXPR)
5341 /* Create a new node with the current stmt on the left, and the new
5342 stmt on the right. */
5343 ce = build (COMPOUND_EXPR, void_type_node, cur, t);
5345 /* Update link to point to this CE node. */
5346 *i->tp = ce;
5348 /* Change new iterator to point to the new stmt. */
5349 if (mode == TSI_CHAIN_START)
5350 i->tp = &TREE_OPERAND (ce, 1);
5351 else if (mode == TSI_CHAIN_END)
5353 cur = ce;
5354 last = i->tp;
5355 while (TREE_CODE (cur) == COMPOUND_EXPR)
5357 last = &TREE_OPERAND (cur, 1);
5358 cur = *last;
5360 i->tp = last;
5363 else
5365 for (last = &t;
5366 TREE_CODE (*last) == COMPOUND_EXPR;
5367 last = &TREE_OPERAND (*last, 1))
5368 continue;
5370 nextp = &TREE_OPERAND (cur, 1);
5371 next = *nextp;
5373 /* Create a new node with the same 'next' link as the current one. */
5374 ce = build (COMPOUND_EXPR, void_type_node, *last, next);
5376 #if 0
5377 /* bsi_... iterators should be used for this!!! */
5378 /* If the 'next' statement is a COMPOUND_EXPR and was the first
5379 statement of a basic block, we need to adjust the 'head_tree_p'
5380 field of that block because we are about to move the statement one
5381 position down in the CE chain. */
5383 basic_block bb = bb_for_stmt (next);
5384 if (bb && bb->head_tree_p == nextp)
5386 /* We may also need to update end_tree_p. */
5387 if (bb->head_tree_p == bb->end_tree_p)
5388 bb->end_tree_p = &TREE_OPERAND (ce, 1);
5389 bb->head_tree_p = &TREE_OPERAND (ce, 1);
5392 #endif
5394 *last = ce;
5395 TREE_OPERAND (cur, 1) = t;
5397 /* Update the iterator to the new stmt. */
5398 if (mode == TSI_CHAIN_START)
5399 i->tp = nextp;
5400 else if (mode == TSI_CHAIN_END)
5401 i->tp = (last != &t ? last : nextp);
5405 /* Remove a stmt from the tree list. The iterator is updated to point to
5406 the next stmt. */
5408 void
5409 tsi_delink (tree_stmt_iterator *i)
5411 tree cur = *i->tp;
5413 if (TREE_CODE (cur) == COMPOUND_EXPR)
5415 /* All that is needed here is to change the parent to point to the next
5416 stmt in the chain. */
5417 *i->tp = TREE_OPERAND (cur, 1);
5419 else
5421 /* This stmt is the last link in the CE chain, but we're screwed because
5422 there isn't access to the node itself. the choices are either adding a
5423 NULL link to the right side of the CE node, which is bad, or replacing
5424 this node with an empty statement. Choose the latter for now, and
5425 make the iterator return NULL, so that no further iterating takes
5426 place. */
5427 *i->tp = build_empty_stmt ();
5428 i->tp = NULL;
5432 /* Create a new stmt list beginning with this stmt. The anchor is used mostly
5433 to keep the garbage collector from collecting the root node, and to give
5434 the list a place to start. */
5436 tree_stmt_iterator
5437 tsi_new_stmt_list (tree t, tree_stmt_anchor *anchor)
5439 tree_stmt_iterator i;
5441 *anchor = t;
5442 i.tp = anchor;
5444 return i;
5447 /* Return an iterator which begins at this anchor. */
5449 tree_stmt_iterator
5450 tsi_stmt_list_head (tree_stmt_anchor anchor)
5452 tree_stmt_iterator i;
5454 if (anchor == EMPTY_ANCHOR)
5455 i.tp = NULL;
5456 else
5457 i.tp = &anchor;
5459 return i;
5462 /* Return true if the chain of statements starting with T is empty. */
5464 bool
5465 body_is_empty (tree t)
5467 tree_stmt_iterator i;
5469 if (IS_EMPTY_STMT (t))
5470 return true;
5472 for (i = tsi_start (&t); !tsi_end_p (i); tsi_next (&i))
5473 if (tsi_stmt (i))
5474 return false;
5476 return true;
5478 bool
5479 is_essa_node (tree t)
5481 if (TREE_CODE (t) == ELEFT_NODE || TREE_CODE (t) == EPHI_NODE
5482 || TREE_CODE (t) == EUSE_NODE || TREE_CODE (t) == EEXIT_NODE
5483 || TREE_CODE (t) == EKILL_NODE)
5484 return true;
5485 return false;
5488 #include "gt-tree.h"