* jump.c: Remove prototypes for delete_computation and
[official-gcc.git] / gcc / tree.c
blobae20c2511e20242d7e160fa81a635fc5c0c43027
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, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* This file contains the low level primitives for operating on tree nodes,
24 including allocation, list operations, interning of identifiers,
25 construction of data type nodes and statement nodes,
26 and construction of type conversion nodes. It also contains
27 tables index by tree code that describe how to take apart
28 nodes of that code.
30 It is intended to be language-independent, but occasionally
31 calls language-dependent routines defined (for C) in typecheck.c. */
33 #include "config.h"
34 #include "system.h"
35 #include "coretypes.h"
36 #include "tm.h"
37 #include "flags.h"
38 #include "tree.h"
39 #include "real.h"
40 #include "tm_p.h"
41 #include "function.h"
42 #include "obstack.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "hashtab.h"
46 #include "output.h"
47 #include "target.h"
48 #include "langhooks.h"
49 #include "tree-iterator.h"
50 #include "basic-block.h"
51 #include "tree-flow.h"
52 #include "params.h"
53 #include "pointer-set.h"
55 /* Each tree code class has an associated string representation.
56 These must correspond to the tree_code_class entries. */
58 const char *const tree_code_class_strings[] =
60 "exceptional",
61 "constant",
62 "type",
63 "declaration",
64 "reference",
65 "comparison",
66 "unary",
67 "binary",
68 "statement",
69 "expression",
72 /* obstack.[ch] explicitly declined to prototype this. */
73 extern int _obstack_allocated_p (struct obstack *h, void *obj);
75 #ifdef GATHER_STATISTICS
76 /* Statistics-gathering stuff. */
78 int tree_node_counts[(int) all_kinds];
79 int tree_node_sizes[(int) all_kinds];
81 /* Keep in sync with tree.h:enum tree_node_kind. */
82 static const char * const tree_node_kind_names[] = {
83 "decls",
84 "types",
85 "blocks",
86 "stmts",
87 "refs",
88 "exprs",
89 "constants",
90 "identifiers",
91 "perm_tree_lists",
92 "temp_tree_lists",
93 "vecs",
94 "binfos",
95 "phi_nodes",
96 "ssa names",
97 "constructors",
98 "random kinds",
99 "lang_decl kinds",
100 "lang_type kinds",
101 "omp clauses",
102 "gimple statements"
104 #endif /* GATHER_STATISTICS */
106 /* Unique id for next decl created. */
107 static GTY(()) int next_decl_uid;
108 /* Unique id for next type created. */
109 static GTY(()) int next_type_uid = 1;
111 /* Since we cannot rehash a type after it is in the table, we have to
112 keep the hash code. */
114 struct type_hash GTY(())
116 unsigned long hash;
117 tree type;
120 /* Initial size of the hash table (rounded to next prime). */
121 #define TYPE_HASH_INITIAL_SIZE 1000
123 /* Now here is the hash table. When recording a type, it is added to
124 the slot whose index is the hash code. Note that the hash table is
125 used for several kinds of types (function types, array types and
126 array index range types, for now). While all these live in the
127 same table, they are completely independent, and the hash code is
128 computed differently for each of these. */
130 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
131 htab_t type_hash_table;
133 /* Hash table and temporary node for larger integer const values. */
134 static GTY (()) tree int_cst_node;
135 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
136 htab_t int_cst_hash_table;
138 /* General tree->tree mapping structure for use in hash tables. */
141 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
142 htab_t debug_expr_for_decl;
144 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
145 htab_t value_expr_for_decl;
147 static GTY ((if_marked ("tree_int_map_marked_p"), param_is (struct tree_int_map)))
148 htab_t init_priority_for_decl;
150 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
151 htab_t restrict_base_for_decl;
153 static void set_type_quals (tree, int);
154 static int type_hash_eq (const void *, const void *);
155 static hashval_t type_hash_hash (const void *);
156 static hashval_t int_cst_hash_hash (const void *);
157 static int int_cst_hash_eq (const void *, const void *);
158 static void print_type_hash_statistics (void);
159 static void print_debug_expr_statistics (void);
160 static void print_value_expr_statistics (void);
161 static int type_hash_marked_p (const void *);
162 static unsigned int type_hash_list (tree, hashval_t);
163 static unsigned int attribute_hash_list (tree, hashval_t);
165 tree global_trees[TI_MAX];
166 tree integer_types[itk_none];
168 unsigned char tree_contains_struct[256][64];
170 /* Number of operands for each OpenMP clause. */
171 unsigned const char omp_clause_num_ops[] =
173 0, /* OMP_CLAUSE_ERROR */
174 1, /* OMP_CLAUSE_PRIVATE */
175 1, /* OMP_CLAUSE_SHARED */
176 1, /* OMP_CLAUSE_FIRSTPRIVATE */
177 1, /* OMP_CLAUSE_LASTPRIVATE */
178 4, /* OMP_CLAUSE_REDUCTION */
179 1, /* OMP_CLAUSE_COPYIN */
180 1, /* OMP_CLAUSE_COPYPRIVATE */
181 1, /* OMP_CLAUSE_IF */
182 1, /* OMP_CLAUSE_NUM_THREADS */
183 1, /* OMP_CLAUSE_SCHEDULE */
184 0, /* OMP_CLAUSE_NOWAIT */
185 0, /* OMP_CLAUSE_ORDERED */
186 0 /* OMP_CLAUSE_DEFAULT */
189 const char * const omp_clause_code_name[] =
191 "error_clause",
192 "private",
193 "shared",
194 "firstprivate",
195 "lastprivate",
196 "reduction",
197 "copyin",
198 "copyprivate",
199 "if",
200 "num_threads",
201 "schedule",
202 "nowait",
203 "ordered",
204 "default"
207 /* Init tree.c. */
209 void
210 init_ttree (void)
212 /* Initialize the hash table of types. */
213 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
214 type_hash_eq, 0);
216 debug_expr_for_decl = htab_create_ggc (512, tree_map_hash,
217 tree_map_eq, 0);
219 value_expr_for_decl = htab_create_ggc (512, tree_map_hash,
220 tree_map_eq, 0);
221 init_priority_for_decl = htab_create_ggc (512, tree_int_map_hash,
222 tree_int_map_eq, 0);
223 restrict_base_for_decl = htab_create_ggc (256, tree_map_hash,
224 tree_map_eq, 0);
226 int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
227 int_cst_hash_eq, NULL);
229 int_cst_node = make_node (INTEGER_CST);
231 tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON] = 1;
232 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_NON_COMMON] = 1;
233 tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON] = 1;
236 tree_contains_struct[CONST_DECL][TS_DECL_COMMON] = 1;
237 tree_contains_struct[VAR_DECL][TS_DECL_COMMON] = 1;
238 tree_contains_struct[PARM_DECL][TS_DECL_COMMON] = 1;
239 tree_contains_struct[RESULT_DECL][TS_DECL_COMMON] = 1;
240 tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON] = 1;
241 tree_contains_struct[TYPE_DECL][TS_DECL_COMMON] = 1;
242 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON] = 1;
243 tree_contains_struct[LABEL_DECL][TS_DECL_COMMON] = 1;
244 tree_contains_struct[FIELD_DECL][TS_DECL_COMMON] = 1;
247 tree_contains_struct[CONST_DECL][TS_DECL_WRTL] = 1;
248 tree_contains_struct[VAR_DECL][TS_DECL_WRTL] = 1;
249 tree_contains_struct[PARM_DECL][TS_DECL_WRTL] = 1;
250 tree_contains_struct[RESULT_DECL][TS_DECL_WRTL] = 1;
251 tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL] = 1;
252 tree_contains_struct[LABEL_DECL][TS_DECL_WRTL] = 1;
254 tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL] = 1;
255 tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL] = 1;
256 tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL] = 1;
257 tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL] = 1;
258 tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL] = 1;
259 tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL] = 1;
260 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL] = 1;
261 tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL] = 1;
262 tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL] = 1;
263 tree_contains_struct[STRUCT_FIELD_TAG][TS_DECL_MINIMAL] = 1;
264 tree_contains_struct[NAME_MEMORY_TAG][TS_DECL_MINIMAL] = 1;
265 tree_contains_struct[SYMBOL_MEMORY_TAG][TS_DECL_MINIMAL] = 1;
266 tree_contains_struct[MEMORY_PARTITION_TAG][TS_DECL_MINIMAL] = 1;
268 tree_contains_struct[STRUCT_FIELD_TAG][TS_MEMORY_TAG] = 1;
269 tree_contains_struct[NAME_MEMORY_TAG][TS_MEMORY_TAG] = 1;
270 tree_contains_struct[SYMBOL_MEMORY_TAG][TS_MEMORY_TAG] = 1;
271 tree_contains_struct[MEMORY_PARTITION_TAG][TS_MEMORY_TAG] = 1;
273 tree_contains_struct[STRUCT_FIELD_TAG][TS_STRUCT_FIELD_TAG] = 1;
274 tree_contains_struct[MEMORY_PARTITION_TAG][TS_MEMORY_PARTITION_TAG] = 1;
276 tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS] = 1;
277 tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS] = 1;
278 tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS] = 1;
279 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_WITH_VIS] = 1;
281 tree_contains_struct[VAR_DECL][TS_VAR_DECL] = 1;
282 tree_contains_struct[FIELD_DECL][TS_FIELD_DECL] = 1;
283 tree_contains_struct[PARM_DECL][TS_PARM_DECL] = 1;
284 tree_contains_struct[LABEL_DECL][TS_LABEL_DECL] = 1;
285 tree_contains_struct[RESULT_DECL][TS_RESULT_DECL] = 1;
286 tree_contains_struct[CONST_DECL][TS_CONST_DECL] = 1;
287 tree_contains_struct[TYPE_DECL][TS_TYPE_DECL] = 1;
288 tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL] = 1;
290 lang_hooks.init_ts ();
294 /* The name of the object as the assembler will see it (but before any
295 translations made by ASM_OUTPUT_LABELREF). Often this is the same
296 as DECL_NAME. It is an IDENTIFIER_NODE. */
297 tree
298 decl_assembler_name (tree decl)
300 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
301 lang_hooks.set_decl_assembler_name (decl);
302 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
305 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
307 bool
308 decl_assembler_name_equal (tree decl, tree asmname)
310 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
312 if (decl_asmname == asmname)
313 return true;
315 /* If the target assembler name was set by the user, things are trickier.
316 We have a leading '*' to begin with. After that, it's arguable what
317 is the correct thing to do with -fleading-underscore. Arguably, we've
318 historically been doing the wrong thing in assemble_alias by always
319 printing the leading underscore. Since we're not changing that, make
320 sure user_label_prefix follows the '*' before matching. */
321 if (IDENTIFIER_POINTER (decl_asmname)[0] == '*')
323 const char *decl_str = IDENTIFIER_POINTER (decl_asmname) + 1;
324 size_t ulp_len = strlen (user_label_prefix);
326 if (ulp_len == 0)
328 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
329 decl_str += ulp_len;
330 else
331 return false;
333 return strcmp (decl_str, IDENTIFIER_POINTER (asmname)) == 0;
336 return false;
339 /* Compute the number of bytes occupied by a tree with code CODE.
340 This function cannot be used for TREE_VEC, PHI_NODE, or STRING_CST
341 codes, which are of variable length. */
342 size_t
343 tree_code_size (enum tree_code code)
345 switch (TREE_CODE_CLASS (code))
347 case tcc_declaration: /* A decl node */
349 switch (code)
351 case FIELD_DECL:
352 return sizeof (struct tree_field_decl);
353 case PARM_DECL:
354 return sizeof (struct tree_parm_decl);
355 case VAR_DECL:
356 return sizeof (struct tree_var_decl);
357 case LABEL_DECL:
358 return sizeof (struct tree_label_decl);
359 case RESULT_DECL:
360 return sizeof (struct tree_result_decl);
361 case CONST_DECL:
362 return sizeof (struct tree_const_decl);
363 case TYPE_DECL:
364 return sizeof (struct tree_type_decl);
365 case FUNCTION_DECL:
366 return sizeof (struct tree_function_decl);
367 case NAME_MEMORY_TAG:
368 case SYMBOL_MEMORY_TAG:
369 return sizeof (struct tree_memory_tag);
370 case STRUCT_FIELD_TAG:
371 return sizeof (struct tree_struct_field_tag);
372 case MEMORY_PARTITION_TAG:
373 return sizeof (struct tree_memory_partition_tag);
374 default:
375 return sizeof (struct tree_decl_non_common);
379 case tcc_type: /* a type node */
380 return sizeof (struct tree_type);
382 case tcc_reference: /* a reference */
383 case tcc_expression: /* an expression */
384 case tcc_statement: /* an expression with side effects */
385 case tcc_comparison: /* a comparison expression */
386 case tcc_unary: /* a unary arithmetic expression */
387 case tcc_binary: /* a binary arithmetic expression */
388 return (sizeof (struct tree_exp)
389 + (TREE_CODE_LENGTH (code) - 1) * sizeof (char *));
391 case tcc_gimple_stmt:
392 return (sizeof (struct gimple_stmt)
393 + (TREE_CODE_LENGTH (code) - 1) * sizeof (char *));
395 case tcc_constant: /* a constant */
396 switch (code)
398 case INTEGER_CST: return sizeof (struct tree_int_cst);
399 case REAL_CST: return sizeof (struct tree_real_cst);
400 case COMPLEX_CST: return sizeof (struct tree_complex);
401 case VECTOR_CST: return sizeof (struct tree_vector);
402 case STRING_CST: gcc_unreachable ();
403 default:
404 return lang_hooks.tree_size (code);
407 case tcc_exceptional: /* something random, like an identifier. */
408 switch (code)
410 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
411 case TREE_LIST: return sizeof (struct tree_list);
413 case ERROR_MARK:
414 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
416 case TREE_VEC:
417 case OMP_CLAUSE:
418 case PHI_NODE: gcc_unreachable ();
420 case SSA_NAME: return sizeof (struct tree_ssa_name);
422 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
423 case BLOCK: return sizeof (struct tree_block);
424 case VALUE_HANDLE: return sizeof (struct tree_value_handle);
425 case CONSTRUCTOR: return sizeof (struct tree_constructor);
427 default:
428 return lang_hooks.tree_size (code);
431 default:
432 gcc_unreachable ();
436 /* Compute the number of bytes occupied by NODE. This routine only
437 looks at TREE_CODE, except for PHI_NODE and TREE_VEC nodes. */
438 size_t
439 tree_size (tree node)
441 enum tree_code code = TREE_CODE (node);
442 switch (code)
444 case PHI_NODE:
445 return (sizeof (struct tree_phi_node)
446 + (PHI_ARG_CAPACITY (node) - 1) * sizeof (struct phi_arg_d));
448 case TREE_BINFO:
449 return (offsetof (struct tree_binfo, base_binfos)
450 + VEC_embedded_size (tree, BINFO_N_BASE_BINFOS (node)));
452 case TREE_VEC:
453 return (sizeof (struct tree_vec)
454 + (TREE_VEC_LENGTH (node) - 1) * sizeof(char *));
456 case STRING_CST:
457 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
459 case OMP_CLAUSE:
460 return (sizeof (struct tree_omp_clause)
461 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
462 * sizeof (tree));
464 default:
465 return tree_code_size (code);
469 /* Return a newly allocated node of code CODE. For decl and type
470 nodes, some other fields are initialized. The rest of the node is
471 initialized to zero. This function cannot be used for PHI_NODE,
472 TREE_VEC or OMP_CLAUSE nodes, which is enforced by asserts in
473 tree_code_size.
475 Achoo! I got a code in the node. */
477 tree
478 make_node_stat (enum tree_code code MEM_STAT_DECL)
480 tree t;
481 enum tree_code_class type = TREE_CODE_CLASS (code);
482 size_t length = tree_code_size (code);
483 #ifdef GATHER_STATISTICS
484 tree_node_kind kind;
486 switch (type)
488 case tcc_declaration: /* A decl node */
489 kind = d_kind;
490 break;
492 case tcc_type: /* a type node */
493 kind = t_kind;
494 break;
496 case tcc_statement: /* an expression with side effects */
497 kind = s_kind;
498 break;
500 case tcc_reference: /* a reference */
501 kind = r_kind;
502 break;
504 case tcc_expression: /* an expression */
505 case tcc_comparison: /* a comparison expression */
506 case tcc_unary: /* a unary arithmetic expression */
507 case tcc_binary: /* a binary arithmetic expression */
508 kind = e_kind;
509 break;
511 case tcc_constant: /* a constant */
512 kind = c_kind;
513 break;
515 case tcc_gimple_stmt:
516 kind = gimple_stmt_kind;
517 break;
519 case tcc_exceptional: /* something random, like an identifier. */
520 switch (code)
522 case IDENTIFIER_NODE:
523 kind = id_kind;
524 break;
526 case TREE_VEC:
527 kind = vec_kind;
528 break;
530 case TREE_BINFO:
531 kind = binfo_kind;
532 break;
534 case PHI_NODE:
535 kind = phi_kind;
536 break;
538 case SSA_NAME:
539 kind = ssa_name_kind;
540 break;
542 case BLOCK:
543 kind = b_kind;
544 break;
546 case CONSTRUCTOR:
547 kind = constr_kind;
548 break;
550 default:
551 kind = x_kind;
552 break;
554 break;
556 default:
557 gcc_unreachable ();
560 tree_node_counts[(int) kind]++;
561 tree_node_sizes[(int) kind] += length;
562 #endif
564 if (code == IDENTIFIER_NODE)
565 t = ggc_alloc_zone_pass_stat (length, &tree_id_zone);
566 else
567 t = ggc_alloc_zone_pass_stat (length, &tree_zone);
569 memset (t, 0, length);
571 TREE_SET_CODE (t, code);
573 switch (type)
575 case tcc_statement:
576 TREE_SIDE_EFFECTS (t) = 1;
577 break;
579 case tcc_declaration:
580 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
581 DECL_IN_SYSTEM_HEADER (t) = in_system_header;
582 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
584 if (code != FUNCTION_DECL)
585 DECL_ALIGN (t) = 1;
586 DECL_USER_ALIGN (t) = 0;
587 /* We have not yet computed the alias set for this declaration. */
588 DECL_POINTER_ALIAS_SET (t) = -1;
590 DECL_SOURCE_LOCATION (t) = input_location;
591 DECL_UID (t) = next_decl_uid++;
593 break;
595 case tcc_type:
596 TYPE_UID (t) = next_type_uid++;
597 TYPE_ALIGN (t) = BITS_PER_UNIT;
598 TYPE_USER_ALIGN (t) = 0;
599 TYPE_MAIN_VARIANT (t) = t;
600 TYPE_CANONICAL (t) = t;
602 /* Default to no attributes for type, but let target change that. */
603 TYPE_ATTRIBUTES (t) = NULL_TREE;
604 targetm.set_default_type_attributes (t);
606 /* We have not yet computed the alias set for this type. */
607 TYPE_ALIAS_SET (t) = -1;
608 break;
610 case tcc_constant:
611 TREE_CONSTANT (t) = 1;
612 TREE_INVARIANT (t) = 1;
613 break;
615 case tcc_expression:
616 switch (code)
618 case INIT_EXPR:
619 case MODIFY_EXPR:
620 case VA_ARG_EXPR:
621 case PREDECREMENT_EXPR:
622 case PREINCREMENT_EXPR:
623 case POSTDECREMENT_EXPR:
624 case POSTINCREMENT_EXPR:
625 /* All of these have side-effects, no matter what their
626 operands are. */
627 TREE_SIDE_EFFECTS (t) = 1;
628 break;
630 default:
631 break;
633 break;
635 case tcc_gimple_stmt:
636 switch (code)
638 case GIMPLE_MODIFY_STMT:
639 TREE_SIDE_EFFECTS (t) = 1;
640 break;
642 default:
643 break;
646 default:
647 /* Other classes need no special treatment. */
648 break;
651 return t;
654 /* Return a new node with the same contents as NODE except that its
655 TREE_CHAIN is zero and it has a fresh uid. */
657 tree
658 copy_node_stat (tree node MEM_STAT_DECL)
660 tree t;
661 enum tree_code code = TREE_CODE (node);
662 size_t length;
664 gcc_assert (code != STATEMENT_LIST);
666 length = tree_size (node);
667 t = ggc_alloc_zone_pass_stat (length, &tree_zone);
668 memcpy (t, node, length);
670 if (!GIMPLE_TUPLE_P (node))
671 TREE_CHAIN (t) = 0;
672 TREE_ASM_WRITTEN (t) = 0;
673 TREE_VISITED (t) = 0;
674 t->base.ann = 0;
676 if (TREE_CODE_CLASS (code) == tcc_declaration)
678 DECL_UID (t) = next_decl_uid++;
679 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
680 && DECL_HAS_VALUE_EXPR_P (node))
682 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
683 DECL_HAS_VALUE_EXPR_P (t) = 1;
685 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
687 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
688 DECL_HAS_INIT_PRIORITY_P (t) = 1;
690 if (TREE_CODE (node) == VAR_DECL && DECL_BASED_ON_RESTRICT_P (node))
692 SET_DECL_RESTRICT_BASE (t, DECL_GET_RESTRICT_BASE (node));
693 DECL_BASED_ON_RESTRICT_P (t) = 1;
696 else if (TREE_CODE_CLASS (code) == tcc_type)
698 TYPE_UID (t) = next_type_uid++;
699 /* The following is so that the debug code for
700 the copy is different from the original type.
701 The two statements usually duplicate each other
702 (because they clear fields of the same union),
703 but the optimizer should catch that. */
704 TYPE_SYMTAB_POINTER (t) = 0;
705 TYPE_SYMTAB_ADDRESS (t) = 0;
707 /* Do not copy the values cache. */
708 if (TYPE_CACHED_VALUES_P(t))
710 TYPE_CACHED_VALUES_P (t) = 0;
711 TYPE_CACHED_VALUES (t) = NULL_TREE;
715 return t;
718 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
719 For example, this can copy a list made of TREE_LIST nodes. */
721 tree
722 copy_list (tree list)
724 tree head;
725 tree prev, next;
727 if (list == 0)
728 return 0;
730 head = prev = copy_node (list);
731 next = TREE_CHAIN (list);
732 while (next)
734 TREE_CHAIN (prev) = copy_node (next);
735 prev = TREE_CHAIN (prev);
736 next = TREE_CHAIN (next);
738 return head;
742 /* Create an INT_CST node with a LOW value sign extended. */
744 tree
745 build_int_cst (tree type, HOST_WIDE_INT low)
747 /* Support legacy code. */
748 if (!type)
749 type = integer_type_node;
751 return build_int_cst_wide (type, low, low < 0 ? -1 : 0);
754 /* Create an INT_CST node with a LOW value zero extended. */
756 tree
757 build_int_cstu (tree type, unsigned HOST_WIDE_INT low)
759 return build_int_cst_wide (type, low, 0);
762 /* Create an INT_CST node with a LOW value in TYPE. The value is sign extended
763 if it is negative. This function is similar to build_int_cst, but
764 the extra bits outside of the type precision are cleared. Constants
765 with these extra bits may confuse the fold so that it detects overflows
766 even in cases when they do not occur, and in general should be avoided.
767 We cannot however make this a default behavior of build_int_cst without
768 more intrusive changes, since there are parts of gcc that rely on the extra
769 precision of the integer constants. */
771 tree
772 build_int_cst_type (tree type, HOST_WIDE_INT low)
774 unsigned HOST_WIDE_INT low1;
775 HOST_WIDE_INT hi;
777 gcc_assert (type);
779 fit_double_type (low, low < 0 ? -1 : 0, &low1, &hi, type);
781 return build_int_cst_wide (type, low1, hi);
784 /* Create an INT_CST node of TYPE and value HI:LOW. The value is truncated
785 and sign extended according to the value range of TYPE. */
787 tree
788 build_int_cst_wide_type (tree type,
789 unsigned HOST_WIDE_INT low, HOST_WIDE_INT high)
791 fit_double_type (low, high, &low, &high, type);
792 return build_int_cst_wide (type, low, high);
795 /* These are the hash table functions for the hash table of INTEGER_CST
796 nodes of a sizetype. */
798 /* Return the hash code code X, an INTEGER_CST. */
800 static hashval_t
801 int_cst_hash_hash (const void *x)
803 tree t = (tree) x;
805 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
806 ^ htab_hash_pointer (TREE_TYPE (t)));
809 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
810 is the same as that given by *Y, which is the same. */
812 static int
813 int_cst_hash_eq (const void *x, const void *y)
815 tree xt = (tree) x;
816 tree yt = (tree) y;
818 return (TREE_TYPE (xt) == TREE_TYPE (yt)
819 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
820 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
823 /* Create an INT_CST node of TYPE and value HI:LOW.
824 The returned node is always shared. For small integers we use a
825 per-type vector cache, for larger ones we use a single hash table. */
827 tree
828 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
830 tree t;
831 int ix = -1;
832 int limit = 0;
834 gcc_assert (type);
836 switch (TREE_CODE (type))
838 case POINTER_TYPE:
839 case REFERENCE_TYPE:
840 /* Cache NULL pointer. */
841 if (!hi && !low)
843 limit = 1;
844 ix = 0;
846 break;
848 case BOOLEAN_TYPE:
849 /* Cache false or true. */
850 limit = 2;
851 if (!hi && low < 2)
852 ix = low;
853 break;
855 case INTEGER_TYPE:
856 case OFFSET_TYPE:
857 if (TYPE_UNSIGNED (type))
859 /* Cache 0..N */
860 limit = INTEGER_SHARE_LIMIT;
861 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
862 ix = low;
864 else
866 /* Cache -1..N */
867 limit = INTEGER_SHARE_LIMIT + 1;
868 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
869 ix = low + 1;
870 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
871 ix = 0;
873 break;
875 case ENUMERAL_TYPE:
876 break;
878 default:
879 gcc_unreachable ();
882 if (ix >= 0)
884 /* Look for it in the type's vector of small shared ints. */
885 if (!TYPE_CACHED_VALUES_P (type))
887 TYPE_CACHED_VALUES_P (type) = 1;
888 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
891 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
892 if (t)
894 /* Make sure no one is clobbering the shared constant. */
895 gcc_assert (TREE_TYPE (t) == type);
896 gcc_assert (TREE_INT_CST_LOW (t) == low);
897 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
899 else
901 /* Create a new shared int. */
902 t = make_node (INTEGER_CST);
904 TREE_INT_CST_LOW (t) = low;
905 TREE_INT_CST_HIGH (t) = hi;
906 TREE_TYPE (t) = type;
908 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
911 else
913 /* Use the cache of larger shared ints. */
914 void **slot;
916 TREE_INT_CST_LOW (int_cst_node) = low;
917 TREE_INT_CST_HIGH (int_cst_node) = hi;
918 TREE_TYPE (int_cst_node) = type;
920 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
921 t = *slot;
922 if (!t)
924 /* Insert this one into the hash table. */
925 t = int_cst_node;
926 *slot = t;
927 /* Make a new node for next time round. */
928 int_cst_node = make_node (INTEGER_CST);
932 return t;
935 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
936 and the rest are zeros. */
938 tree
939 build_low_bits_mask (tree type, unsigned bits)
941 unsigned HOST_WIDE_INT low;
942 HOST_WIDE_INT high;
943 unsigned HOST_WIDE_INT all_ones = ~(unsigned HOST_WIDE_INT) 0;
945 gcc_assert (bits <= TYPE_PRECISION (type));
947 if (bits == TYPE_PRECISION (type)
948 && !TYPE_UNSIGNED (type))
950 /* Sign extended all-ones mask. */
951 low = all_ones;
952 high = -1;
954 else if (bits <= HOST_BITS_PER_WIDE_INT)
956 low = all_ones >> (HOST_BITS_PER_WIDE_INT - bits);
957 high = 0;
959 else
961 bits -= HOST_BITS_PER_WIDE_INT;
962 low = all_ones;
963 high = all_ones >> (HOST_BITS_PER_WIDE_INT - bits);
966 return build_int_cst_wide (type, low, high);
969 /* Checks that X is integer constant that can be expressed in (unsigned)
970 HOST_WIDE_INT without loss of precision. */
972 bool
973 cst_and_fits_in_hwi (tree x)
975 if (TREE_CODE (x) != INTEGER_CST)
976 return false;
978 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
979 return false;
981 return (TREE_INT_CST_HIGH (x) == 0
982 || TREE_INT_CST_HIGH (x) == -1);
985 /* Return a new VECTOR_CST node whose type is TYPE and whose values
986 are in a list pointed to by VALS. */
988 tree
989 build_vector (tree type, tree vals)
991 tree v = make_node (VECTOR_CST);
992 int over = 0;
993 tree link;
995 TREE_VECTOR_CST_ELTS (v) = vals;
996 TREE_TYPE (v) = type;
998 /* Iterate through elements and check for overflow. */
999 for (link = vals; link; link = TREE_CHAIN (link))
1001 tree value = TREE_VALUE (link);
1003 /* Don't crash if we get an address constant. */
1004 if (!CONSTANT_CLASS_P (value))
1005 continue;
1007 over |= TREE_OVERFLOW (value);
1010 TREE_OVERFLOW (v) = over;
1011 return v;
1014 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1015 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1017 tree
1018 build_vector_from_ctor (tree type, VEC(constructor_elt,gc) *v)
1020 tree list = NULL_TREE;
1021 unsigned HOST_WIDE_INT idx;
1022 tree value;
1024 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1025 list = tree_cons (NULL_TREE, value, list);
1026 return build_vector (type, nreverse (list));
1029 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1030 are in the VEC pointed to by VALS. */
1031 tree
1032 build_constructor (tree type, VEC(constructor_elt,gc) *vals)
1034 tree c = make_node (CONSTRUCTOR);
1035 TREE_TYPE (c) = type;
1036 CONSTRUCTOR_ELTS (c) = vals;
1037 return c;
1040 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1041 INDEX and VALUE. */
1042 tree
1043 build_constructor_single (tree type, tree index, tree value)
1045 VEC(constructor_elt,gc) *v;
1046 constructor_elt *elt;
1047 tree t;
1049 v = VEC_alloc (constructor_elt, gc, 1);
1050 elt = VEC_quick_push (constructor_elt, v, NULL);
1051 elt->index = index;
1052 elt->value = value;
1054 t = build_constructor (type, v);
1055 TREE_CONSTANT (t) = TREE_CONSTANT (value);
1056 return t;
1060 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1061 are in a list pointed to by VALS. */
1062 tree
1063 build_constructor_from_list (tree type, tree vals)
1065 tree t, val;
1066 VEC(constructor_elt,gc) *v = NULL;
1067 bool constant_p = true;
1069 if (vals)
1071 v = VEC_alloc (constructor_elt, gc, list_length (vals));
1072 for (t = vals; t; t = TREE_CHAIN (t))
1074 constructor_elt *elt = VEC_quick_push (constructor_elt, v, NULL);
1075 val = TREE_VALUE (t);
1076 elt->index = TREE_PURPOSE (t);
1077 elt->value = val;
1078 if (!TREE_CONSTANT (val))
1079 constant_p = false;
1083 t = build_constructor (type, v);
1084 TREE_CONSTANT (t) = constant_p;
1085 return t;
1089 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1091 tree
1092 build_real (tree type, REAL_VALUE_TYPE d)
1094 tree v;
1095 REAL_VALUE_TYPE *dp;
1096 int overflow = 0;
1098 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1099 Consider doing it via real_convert now. */
1101 v = make_node (REAL_CST);
1102 dp = ggc_alloc (sizeof (REAL_VALUE_TYPE));
1103 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1105 TREE_TYPE (v) = type;
1106 TREE_REAL_CST_PTR (v) = dp;
1107 TREE_OVERFLOW (v) = overflow;
1108 return v;
1111 /* Return a new REAL_CST node whose type is TYPE
1112 and whose value is the integer value of the INTEGER_CST node I. */
1114 REAL_VALUE_TYPE
1115 real_value_from_int_cst (tree type, tree i)
1117 REAL_VALUE_TYPE d;
1119 /* Clear all bits of the real value type so that we can later do
1120 bitwise comparisons to see if two values are the same. */
1121 memset (&d, 0, sizeof d);
1123 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1124 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1125 TYPE_UNSIGNED (TREE_TYPE (i)));
1126 return d;
1129 /* Given a tree representing an integer constant I, return a tree
1130 representing the same value as a floating-point constant of type TYPE. */
1132 tree
1133 build_real_from_int_cst (tree type, tree i)
1135 tree v;
1136 int overflow = TREE_OVERFLOW (i);
1138 v = build_real (type, real_value_from_int_cst (type, i));
1140 TREE_OVERFLOW (v) |= overflow;
1141 return v;
1144 /* Return a newly constructed STRING_CST node whose value is
1145 the LEN characters at STR.
1146 The TREE_TYPE is not initialized. */
1148 tree
1149 build_string (int len, const char *str)
1151 tree s;
1152 size_t length;
1154 /* Do not waste bytes provided by padding of struct tree_string. */
1155 length = len + offsetof (struct tree_string, str) + 1;
1157 #ifdef GATHER_STATISTICS
1158 tree_node_counts[(int) c_kind]++;
1159 tree_node_sizes[(int) c_kind] += length;
1160 #endif
1162 s = ggc_alloc_tree (length);
1164 memset (s, 0, sizeof (struct tree_common));
1165 TREE_SET_CODE (s, STRING_CST);
1166 TREE_CONSTANT (s) = 1;
1167 TREE_INVARIANT (s) = 1;
1168 TREE_STRING_LENGTH (s) = len;
1169 memcpy ((char *) TREE_STRING_POINTER (s), str, len);
1170 ((char *) TREE_STRING_POINTER (s))[len] = '\0';
1172 return s;
1175 /* Return a newly constructed COMPLEX_CST node whose value is
1176 specified by the real and imaginary parts REAL and IMAG.
1177 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1178 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1180 tree
1181 build_complex (tree type, tree real, tree imag)
1183 tree t = make_node (COMPLEX_CST);
1185 TREE_REALPART (t) = real;
1186 TREE_IMAGPART (t) = imag;
1187 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1188 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1189 return t;
1192 /* Return a constant of arithmetic type TYPE which is the
1193 multiplicative identity of the set TYPE. */
1195 tree
1196 build_one_cst (tree type)
1198 switch (TREE_CODE (type))
1200 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1201 case POINTER_TYPE: case REFERENCE_TYPE:
1202 case OFFSET_TYPE:
1203 return build_int_cst (type, 1);
1205 case REAL_TYPE:
1206 return build_real (type, dconst1);
1208 case VECTOR_TYPE:
1210 tree scalar, cst;
1211 int i;
1213 scalar = build_one_cst (TREE_TYPE (type));
1215 /* Create 'vect_cst_ = {cst,cst,...,cst}' */
1216 cst = NULL_TREE;
1217 for (i = TYPE_VECTOR_SUBPARTS (type); --i >= 0; )
1218 cst = tree_cons (NULL_TREE, scalar, cst);
1220 return build_vector (type, cst);
1223 case COMPLEX_TYPE:
1224 return build_complex (type,
1225 build_one_cst (TREE_TYPE (type)),
1226 fold_convert (TREE_TYPE (type), integer_zero_node));
1228 default:
1229 gcc_unreachable ();
1233 /* Build a BINFO with LEN language slots. */
1235 tree
1236 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1238 tree t;
1239 size_t length = (offsetof (struct tree_binfo, base_binfos)
1240 + VEC_embedded_size (tree, base_binfos));
1242 #ifdef GATHER_STATISTICS
1243 tree_node_counts[(int) binfo_kind]++;
1244 tree_node_sizes[(int) binfo_kind] += length;
1245 #endif
1247 t = ggc_alloc_zone_pass_stat (length, &tree_zone);
1249 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1251 TREE_SET_CODE (t, TREE_BINFO);
1253 VEC_embedded_init (tree, BINFO_BASE_BINFOS (t), base_binfos);
1255 return t;
1259 /* Build a newly constructed TREE_VEC node of length LEN. */
1261 tree
1262 make_tree_vec_stat (int len MEM_STAT_DECL)
1264 tree t;
1265 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1267 #ifdef GATHER_STATISTICS
1268 tree_node_counts[(int) vec_kind]++;
1269 tree_node_sizes[(int) vec_kind] += length;
1270 #endif
1272 t = ggc_alloc_zone_pass_stat (length, &tree_zone);
1274 memset (t, 0, length);
1276 TREE_SET_CODE (t, TREE_VEC);
1277 TREE_VEC_LENGTH (t) = len;
1279 return t;
1282 /* Return 1 if EXPR is the integer constant zero or a complex constant
1283 of zero. */
1286 integer_zerop (tree expr)
1288 STRIP_NOPS (expr);
1290 return ((TREE_CODE (expr) == INTEGER_CST
1291 && TREE_INT_CST_LOW (expr) == 0
1292 && TREE_INT_CST_HIGH (expr) == 0)
1293 || (TREE_CODE (expr) == COMPLEX_CST
1294 && integer_zerop (TREE_REALPART (expr))
1295 && integer_zerop (TREE_IMAGPART (expr))));
1298 /* Return 1 if EXPR is the integer constant one or the corresponding
1299 complex constant. */
1302 integer_onep (tree expr)
1304 STRIP_NOPS (expr);
1306 return ((TREE_CODE (expr) == INTEGER_CST
1307 && TREE_INT_CST_LOW (expr) == 1
1308 && TREE_INT_CST_HIGH (expr) == 0)
1309 || (TREE_CODE (expr) == COMPLEX_CST
1310 && integer_onep (TREE_REALPART (expr))
1311 && integer_zerop (TREE_IMAGPART (expr))));
1314 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1315 it contains. Likewise for the corresponding complex constant. */
1318 integer_all_onesp (tree expr)
1320 int prec;
1321 int uns;
1323 STRIP_NOPS (expr);
1325 if (TREE_CODE (expr) == COMPLEX_CST
1326 && integer_all_onesp (TREE_REALPART (expr))
1327 && integer_zerop (TREE_IMAGPART (expr)))
1328 return 1;
1330 else if (TREE_CODE (expr) != INTEGER_CST)
1331 return 0;
1333 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1334 if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1335 && TREE_INT_CST_HIGH (expr) == -1)
1336 return 1;
1337 if (!uns)
1338 return 0;
1340 /* Note that using TYPE_PRECISION here is wrong. We care about the
1341 actual bits, not the (arbitrary) range of the type. */
1342 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)));
1343 if (prec >= HOST_BITS_PER_WIDE_INT)
1345 HOST_WIDE_INT high_value;
1346 int shift_amount;
1348 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1350 /* Can not handle precisions greater than twice the host int size. */
1351 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
1352 if (shift_amount == HOST_BITS_PER_WIDE_INT)
1353 /* Shifting by the host word size is undefined according to the ANSI
1354 standard, so we must handle this as a special case. */
1355 high_value = -1;
1356 else
1357 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1359 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1360 && TREE_INT_CST_HIGH (expr) == high_value);
1362 else
1363 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1366 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1367 one bit on). */
1370 integer_pow2p (tree expr)
1372 int prec;
1373 HOST_WIDE_INT high, low;
1375 STRIP_NOPS (expr);
1377 if (TREE_CODE (expr) == COMPLEX_CST
1378 && integer_pow2p (TREE_REALPART (expr))
1379 && integer_zerop (TREE_IMAGPART (expr)))
1380 return 1;
1382 if (TREE_CODE (expr) != INTEGER_CST)
1383 return 0;
1385 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1386 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1387 high = TREE_INT_CST_HIGH (expr);
1388 low = TREE_INT_CST_LOW (expr);
1390 /* First clear all bits that are beyond the type's precision in case
1391 we've been sign extended. */
1393 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
1395 else if (prec > HOST_BITS_PER_WIDE_INT)
1396 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1397 else
1399 high = 0;
1400 if (prec < HOST_BITS_PER_WIDE_INT)
1401 low &= ~((HOST_WIDE_INT) (-1) << prec);
1404 if (high == 0 && low == 0)
1405 return 0;
1407 return ((high == 0 && (low & (low - 1)) == 0)
1408 || (low == 0 && (high & (high - 1)) == 0));
1411 /* Return 1 if EXPR is an integer constant other than zero or a
1412 complex constant other than zero. */
1415 integer_nonzerop (tree expr)
1417 STRIP_NOPS (expr);
1419 return ((TREE_CODE (expr) == INTEGER_CST
1420 && (TREE_INT_CST_LOW (expr) != 0
1421 || TREE_INT_CST_HIGH (expr) != 0))
1422 || (TREE_CODE (expr) == COMPLEX_CST
1423 && (integer_nonzerop (TREE_REALPART (expr))
1424 || integer_nonzerop (TREE_IMAGPART (expr)))));
1427 /* Return the power of two represented by a tree node known to be a
1428 power of two. */
1431 tree_log2 (tree expr)
1433 int prec;
1434 HOST_WIDE_INT high, low;
1436 STRIP_NOPS (expr);
1438 if (TREE_CODE (expr) == COMPLEX_CST)
1439 return tree_log2 (TREE_REALPART (expr));
1441 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1442 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1444 high = TREE_INT_CST_HIGH (expr);
1445 low = TREE_INT_CST_LOW (expr);
1447 /* First clear all bits that are beyond the type's precision in case
1448 we've been sign extended. */
1450 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
1452 else if (prec > HOST_BITS_PER_WIDE_INT)
1453 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1454 else
1456 high = 0;
1457 if (prec < HOST_BITS_PER_WIDE_INT)
1458 low &= ~((HOST_WIDE_INT) (-1) << prec);
1461 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
1462 : exact_log2 (low));
1465 /* Similar, but return the largest integer Y such that 2 ** Y is less
1466 than or equal to EXPR. */
1469 tree_floor_log2 (tree expr)
1471 int prec;
1472 HOST_WIDE_INT high, low;
1474 STRIP_NOPS (expr);
1476 if (TREE_CODE (expr) == COMPLEX_CST)
1477 return tree_log2 (TREE_REALPART (expr));
1479 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1480 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1482 high = TREE_INT_CST_HIGH (expr);
1483 low = TREE_INT_CST_LOW (expr);
1485 /* First clear all bits that are beyond the type's precision in case
1486 we've been sign extended. Ignore if type's precision hasn't been set
1487 since what we are doing is setting it. */
1489 if (prec == 2 * HOST_BITS_PER_WIDE_INT || prec == 0)
1491 else if (prec > HOST_BITS_PER_WIDE_INT)
1492 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1493 else
1495 high = 0;
1496 if (prec < HOST_BITS_PER_WIDE_INT)
1497 low &= ~((HOST_WIDE_INT) (-1) << prec);
1500 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
1501 : floor_log2 (low));
1504 /* Return 1 if EXPR is the real constant zero. */
1507 real_zerop (tree expr)
1509 STRIP_NOPS (expr);
1511 return ((TREE_CODE (expr) == REAL_CST
1512 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0))
1513 || (TREE_CODE (expr) == COMPLEX_CST
1514 && real_zerop (TREE_REALPART (expr))
1515 && real_zerop (TREE_IMAGPART (expr))));
1518 /* Return 1 if EXPR is the real constant one in real or complex form. */
1521 real_onep (tree expr)
1523 STRIP_NOPS (expr);
1525 return ((TREE_CODE (expr) == REAL_CST
1526 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1))
1527 || (TREE_CODE (expr) == COMPLEX_CST
1528 && real_onep (TREE_REALPART (expr))
1529 && real_zerop (TREE_IMAGPART (expr))));
1532 /* Return 1 if EXPR is the real constant two. */
1535 real_twop (tree expr)
1537 STRIP_NOPS (expr);
1539 return ((TREE_CODE (expr) == REAL_CST
1540 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2))
1541 || (TREE_CODE (expr) == COMPLEX_CST
1542 && real_twop (TREE_REALPART (expr))
1543 && real_zerop (TREE_IMAGPART (expr))));
1546 /* Return 1 if EXPR is the real constant minus one. */
1549 real_minus_onep (tree expr)
1551 STRIP_NOPS (expr);
1553 return ((TREE_CODE (expr) == REAL_CST
1554 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1))
1555 || (TREE_CODE (expr) == COMPLEX_CST
1556 && real_minus_onep (TREE_REALPART (expr))
1557 && real_zerop (TREE_IMAGPART (expr))));
1560 /* Nonzero if EXP is a constant or a cast of a constant. */
1563 really_constant_p (tree exp)
1565 /* This is not quite the same as STRIP_NOPS. It does more. */
1566 while (TREE_CODE (exp) == NOP_EXPR
1567 || TREE_CODE (exp) == CONVERT_EXPR
1568 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1569 exp = TREE_OPERAND (exp, 0);
1570 return TREE_CONSTANT (exp);
1573 /* Return first list element whose TREE_VALUE is ELEM.
1574 Return 0 if ELEM is not in LIST. */
1576 tree
1577 value_member (tree elem, tree list)
1579 while (list)
1581 if (elem == TREE_VALUE (list))
1582 return list;
1583 list = TREE_CHAIN (list);
1585 return NULL_TREE;
1588 /* Return first list element whose TREE_PURPOSE is ELEM.
1589 Return 0 if ELEM is not in LIST. */
1591 tree
1592 purpose_member (tree elem, tree list)
1594 while (list)
1596 if (elem == TREE_PURPOSE (list))
1597 return list;
1598 list = TREE_CHAIN (list);
1600 return NULL_TREE;
1603 /* Return nonzero if ELEM is part of the chain CHAIN. */
1606 chain_member (tree elem, tree chain)
1608 while (chain)
1610 if (elem == chain)
1611 return 1;
1612 chain = TREE_CHAIN (chain);
1615 return 0;
1618 /* Return the length of a chain of nodes chained through TREE_CHAIN.
1619 We expect a null pointer to mark the end of the chain.
1620 This is the Lisp primitive `length'. */
1623 list_length (tree t)
1625 tree p = t;
1626 #ifdef ENABLE_TREE_CHECKING
1627 tree q = t;
1628 #endif
1629 int len = 0;
1631 while (p)
1633 p = TREE_CHAIN (p);
1634 #ifdef ENABLE_TREE_CHECKING
1635 if (len % 2)
1636 q = TREE_CHAIN (q);
1637 gcc_assert (p != q);
1638 #endif
1639 len++;
1642 return len;
1645 /* Returns the number of FIELD_DECLs in TYPE. */
1648 fields_length (tree type)
1650 tree t = TYPE_FIELDS (type);
1651 int count = 0;
1653 for (; t; t = TREE_CHAIN (t))
1654 if (TREE_CODE (t) == FIELD_DECL)
1655 ++count;
1657 return count;
1660 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
1661 by modifying the last node in chain 1 to point to chain 2.
1662 This is the Lisp primitive `nconc'. */
1664 tree
1665 chainon (tree op1, tree op2)
1667 tree t1;
1669 if (!op1)
1670 return op2;
1671 if (!op2)
1672 return op1;
1674 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
1675 continue;
1676 TREE_CHAIN (t1) = op2;
1678 #ifdef ENABLE_TREE_CHECKING
1680 tree t2;
1681 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
1682 gcc_assert (t2 != t1);
1684 #endif
1686 return op1;
1689 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
1691 tree
1692 tree_last (tree chain)
1694 tree next;
1695 if (chain)
1696 while ((next = TREE_CHAIN (chain)))
1697 chain = next;
1698 return chain;
1701 /* Reverse the order of elements in the chain T,
1702 and return the new head of the chain (old last element). */
1704 tree
1705 nreverse (tree t)
1707 tree prev = 0, decl, next;
1708 for (decl = t; decl; decl = next)
1710 next = TREE_CHAIN (decl);
1711 TREE_CHAIN (decl) = prev;
1712 prev = decl;
1714 return prev;
1717 /* Return a newly created TREE_LIST node whose
1718 purpose and value fields are PARM and VALUE. */
1720 tree
1721 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
1723 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
1724 TREE_PURPOSE (t) = parm;
1725 TREE_VALUE (t) = value;
1726 return t;
1729 /* Return a newly created TREE_LIST node whose
1730 purpose and value fields are PURPOSE and VALUE
1731 and whose TREE_CHAIN is CHAIN. */
1733 tree
1734 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
1736 tree node;
1738 node = ggc_alloc_zone_pass_stat (sizeof (struct tree_list), &tree_zone);
1740 memset (node, 0, sizeof (struct tree_common));
1742 #ifdef GATHER_STATISTICS
1743 tree_node_counts[(int) x_kind]++;
1744 tree_node_sizes[(int) x_kind] += sizeof (struct tree_list);
1745 #endif
1747 TREE_SET_CODE (node, TREE_LIST);
1748 TREE_CHAIN (node) = chain;
1749 TREE_PURPOSE (node) = purpose;
1750 TREE_VALUE (node) = value;
1751 return node;
1755 /* Return the size nominally occupied by an object of type TYPE
1756 when it resides in memory. The value is measured in units of bytes,
1757 and its data type is that normally used for type sizes
1758 (which is the first type created by make_signed_type or
1759 make_unsigned_type). */
1761 tree
1762 size_in_bytes (tree type)
1764 tree t;
1766 if (type == error_mark_node)
1767 return integer_zero_node;
1769 type = TYPE_MAIN_VARIANT (type);
1770 t = TYPE_SIZE_UNIT (type);
1772 if (t == 0)
1774 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
1775 return size_zero_node;
1778 return t;
1781 /* Return the size of TYPE (in bytes) as a wide integer
1782 or return -1 if the size can vary or is larger than an integer. */
1784 HOST_WIDE_INT
1785 int_size_in_bytes (tree type)
1787 tree t;
1789 if (type == error_mark_node)
1790 return 0;
1792 type = TYPE_MAIN_VARIANT (type);
1793 t = TYPE_SIZE_UNIT (type);
1794 if (t == 0
1795 || TREE_CODE (t) != INTEGER_CST
1796 || TREE_INT_CST_HIGH (t) != 0
1797 /* If the result would appear negative, it's too big to represent. */
1798 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
1799 return -1;
1801 return TREE_INT_CST_LOW (t);
1804 /* Return the maximum size of TYPE (in bytes) as a wide integer
1805 or return -1 if the size can vary or is larger than an integer. */
1807 HOST_WIDE_INT
1808 max_int_size_in_bytes (tree type)
1810 HOST_WIDE_INT size = -1;
1811 tree size_tree;
1813 /* If this is an array type, check for a possible MAX_SIZE attached. */
1815 if (TREE_CODE (type) == ARRAY_TYPE)
1817 size_tree = TYPE_ARRAY_MAX_SIZE (type);
1819 if (size_tree && host_integerp (size_tree, 1))
1820 size = tree_low_cst (size_tree, 1);
1823 /* If we still haven't been able to get a size, see if the language
1824 can compute a maximum size. */
1826 if (size == -1)
1828 size_tree = lang_hooks.types.max_size (type);
1830 if (size_tree && host_integerp (size_tree, 1))
1831 size = tree_low_cst (size_tree, 1);
1834 return size;
1837 /* Return the bit position of FIELD, in bits from the start of the record.
1838 This is a tree of type bitsizetype. */
1840 tree
1841 bit_position (tree field)
1843 return bit_from_pos (DECL_FIELD_OFFSET (field),
1844 DECL_FIELD_BIT_OFFSET (field));
1847 /* Likewise, but return as an integer. It must be representable in
1848 that way (since it could be a signed value, we don't have the
1849 option of returning -1 like int_size_in_byte can. */
1851 HOST_WIDE_INT
1852 int_bit_position (tree field)
1854 return tree_low_cst (bit_position (field), 0);
1857 /* Return the byte position of FIELD, in bytes from the start of the record.
1858 This is a tree of type sizetype. */
1860 tree
1861 byte_position (tree field)
1863 return byte_from_pos (DECL_FIELD_OFFSET (field),
1864 DECL_FIELD_BIT_OFFSET (field));
1867 /* Likewise, but return as an integer. It must be representable in
1868 that way (since it could be a signed value, we don't have the
1869 option of returning -1 like int_size_in_byte can. */
1871 HOST_WIDE_INT
1872 int_byte_position (tree field)
1874 return tree_low_cst (byte_position (field), 0);
1877 /* Return the strictest alignment, in bits, that T is known to have. */
1879 unsigned int
1880 expr_align (tree t)
1882 unsigned int align0, align1;
1884 switch (TREE_CODE (t))
1886 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
1887 /* If we have conversions, we know that the alignment of the
1888 object must meet each of the alignments of the types. */
1889 align0 = expr_align (TREE_OPERAND (t, 0));
1890 align1 = TYPE_ALIGN (TREE_TYPE (t));
1891 return MAX (align0, align1);
1893 case MODIFY_EXPR:
1894 /* FIXME tuples: It is unclear to me if this function, which
1895 is only called from ADA, is called on gimple or non gimple
1896 trees. Let's assume it's from gimple trees unless we hit
1897 this abort. */
1898 gcc_unreachable ();
1900 case SAVE_EXPR: case COMPOUND_EXPR: case GIMPLE_MODIFY_STMT:
1901 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
1902 case CLEANUP_POINT_EXPR:
1903 /* These don't change the alignment of an object. */
1904 return expr_align (TREE_OPERAND (t, 0));
1906 case COND_EXPR:
1907 /* The best we can do is say that the alignment is the least aligned
1908 of the two arms. */
1909 align0 = expr_align (TREE_OPERAND (t, 1));
1910 align1 = expr_align (TREE_OPERAND (t, 2));
1911 return MIN (align0, align1);
1913 case LABEL_DECL: case CONST_DECL:
1914 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
1915 if (DECL_ALIGN (t) != 0)
1916 return DECL_ALIGN (t);
1917 break;
1919 case FUNCTION_DECL:
1920 return FUNCTION_BOUNDARY;
1922 default:
1923 break;
1926 /* Otherwise take the alignment from that of the type. */
1927 return TYPE_ALIGN (TREE_TYPE (t));
1930 /* Return, as a tree node, the number of elements for TYPE (which is an
1931 ARRAY_TYPE) minus one. This counts only elements of the top array. */
1933 tree
1934 array_type_nelts (tree type)
1936 tree index_type, min, max;
1938 /* If they did it with unspecified bounds, then we should have already
1939 given an error about it before we got here. */
1940 if (! TYPE_DOMAIN (type))
1941 return error_mark_node;
1943 index_type = TYPE_DOMAIN (type);
1944 min = TYPE_MIN_VALUE (index_type);
1945 max = TYPE_MAX_VALUE (index_type);
1947 return (integer_zerop (min)
1948 ? max
1949 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
1952 /* If arg is static -- a reference to an object in static storage -- then
1953 return the object. This is not the same as the C meaning of `static'.
1954 If arg isn't static, return NULL. */
1956 tree
1957 staticp (tree arg)
1959 switch (TREE_CODE (arg))
1961 case FUNCTION_DECL:
1962 /* Nested functions are static, even though taking their address will
1963 involve a trampoline as we unnest the nested function and create
1964 the trampoline on the tree level. */
1965 return arg;
1967 case VAR_DECL:
1968 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
1969 && ! DECL_THREAD_LOCAL_P (arg)
1970 && ! DECL_DLLIMPORT_P (arg)
1971 ? arg : NULL);
1973 case CONST_DECL:
1974 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
1975 ? arg : NULL);
1977 case CONSTRUCTOR:
1978 return TREE_STATIC (arg) ? arg : NULL;
1980 case LABEL_DECL:
1981 case STRING_CST:
1982 return arg;
1984 case COMPONENT_REF:
1985 /* If the thing being referenced is not a field, then it is
1986 something language specific. */
1987 if (TREE_CODE (TREE_OPERAND (arg, 1)) != FIELD_DECL)
1988 return (*lang_hooks.staticp) (arg);
1990 /* If we are referencing a bitfield, we can't evaluate an
1991 ADDR_EXPR at compile time and so it isn't a constant. */
1992 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
1993 return NULL;
1995 return staticp (TREE_OPERAND (arg, 0));
1997 case BIT_FIELD_REF:
1998 return NULL;
2000 case MISALIGNED_INDIRECT_REF:
2001 case ALIGN_INDIRECT_REF:
2002 case INDIRECT_REF:
2003 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2005 case ARRAY_REF:
2006 case ARRAY_RANGE_REF:
2007 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2008 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2009 return staticp (TREE_OPERAND (arg, 0));
2010 else
2011 return false;
2013 default:
2014 if ((unsigned int) TREE_CODE (arg)
2015 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
2016 return lang_hooks.staticp (arg);
2017 else
2018 return NULL;
2022 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2023 Do this to any expression which may be used in more than one place,
2024 but must be evaluated only once.
2026 Normally, expand_expr would reevaluate the expression each time.
2027 Calling save_expr produces something that is evaluated and recorded
2028 the first time expand_expr is called on it. Subsequent calls to
2029 expand_expr just reuse the recorded value.
2031 The call to expand_expr that generates code that actually computes
2032 the value is the first call *at compile time*. Subsequent calls
2033 *at compile time* generate code to use the saved value.
2034 This produces correct result provided that *at run time* control
2035 always flows through the insns made by the first expand_expr
2036 before reaching the other places where the save_expr was evaluated.
2037 You, the caller of save_expr, must make sure this is so.
2039 Constants, and certain read-only nodes, are returned with no
2040 SAVE_EXPR because that is safe. Expressions containing placeholders
2041 are not touched; see tree.def for an explanation of what these
2042 are used for. */
2044 tree
2045 save_expr (tree expr)
2047 tree t = fold (expr);
2048 tree inner;
2050 /* If the tree evaluates to a constant, then we don't want to hide that
2051 fact (i.e. this allows further folding, and direct checks for constants).
2052 However, a read-only object that has side effects cannot be bypassed.
2053 Since it is no problem to reevaluate literals, we just return the
2054 literal node. */
2055 inner = skip_simple_arithmetic (t);
2057 if (TREE_INVARIANT (inner)
2058 || (TREE_READONLY (inner) && ! TREE_SIDE_EFFECTS (inner))
2059 || TREE_CODE (inner) == SAVE_EXPR
2060 || TREE_CODE (inner) == ERROR_MARK)
2061 return t;
2063 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2064 it means that the size or offset of some field of an object depends on
2065 the value within another field.
2067 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2068 and some variable since it would then need to be both evaluated once and
2069 evaluated more than once. Front-ends must assure this case cannot
2070 happen by surrounding any such subexpressions in their own SAVE_EXPR
2071 and forcing evaluation at the proper time. */
2072 if (contains_placeholder_p (inner))
2073 return t;
2075 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
2077 /* This expression might be placed ahead of a jump to ensure that the
2078 value was computed on both sides of the jump. So make sure it isn't
2079 eliminated as dead. */
2080 TREE_SIDE_EFFECTS (t) = 1;
2081 TREE_INVARIANT (t) = 1;
2082 return t;
2085 /* Look inside EXPR and into any simple arithmetic operations. Return
2086 the innermost non-arithmetic node. */
2088 tree
2089 skip_simple_arithmetic (tree expr)
2091 tree inner;
2093 /* We don't care about whether this can be used as an lvalue in this
2094 context. */
2095 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
2096 expr = TREE_OPERAND (expr, 0);
2098 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
2099 a constant, it will be more efficient to not make another SAVE_EXPR since
2100 it will allow better simplification and GCSE will be able to merge the
2101 computations if they actually occur. */
2102 inner = expr;
2103 while (1)
2105 if (UNARY_CLASS_P (inner))
2106 inner = TREE_OPERAND (inner, 0);
2107 else if (BINARY_CLASS_P (inner))
2109 if (TREE_INVARIANT (TREE_OPERAND (inner, 1)))
2110 inner = TREE_OPERAND (inner, 0);
2111 else if (TREE_INVARIANT (TREE_OPERAND (inner, 0)))
2112 inner = TREE_OPERAND (inner, 1);
2113 else
2114 break;
2116 else
2117 break;
2120 return inner;
2123 /* Return which tree structure is used by T. */
2125 enum tree_node_structure_enum
2126 tree_node_structure (tree t)
2128 enum tree_code code = TREE_CODE (t);
2130 switch (TREE_CODE_CLASS (code))
2132 case tcc_declaration:
2134 switch (code)
2136 case FIELD_DECL:
2137 return TS_FIELD_DECL;
2138 case PARM_DECL:
2139 return TS_PARM_DECL;
2140 case VAR_DECL:
2141 return TS_VAR_DECL;
2142 case LABEL_DECL:
2143 return TS_LABEL_DECL;
2144 case RESULT_DECL:
2145 return TS_RESULT_DECL;
2146 case CONST_DECL:
2147 return TS_CONST_DECL;
2148 case TYPE_DECL:
2149 return TS_TYPE_DECL;
2150 case FUNCTION_DECL:
2151 return TS_FUNCTION_DECL;
2152 case SYMBOL_MEMORY_TAG:
2153 case NAME_MEMORY_TAG:
2154 case STRUCT_FIELD_TAG:
2155 case MEMORY_PARTITION_TAG:
2156 return TS_MEMORY_TAG;
2157 default:
2158 return TS_DECL_NON_COMMON;
2161 case tcc_type:
2162 return TS_TYPE;
2163 case tcc_reference:
2164 case tcc_comparison:
2165 case tcc_unary:
2166 case tcc_binary:
2167 case tcc_expression:
2168 case tcc_statement:
2169 return TS_EXP;
2170 case tcc_gimple_stmt:
2171 return TS_GIMPLE_STATEMENT;
2172 default: /* tcc_constant and tcc_exceptional */
2173 break;
2175 switch (code)
2177 /* tcc_constant cases. */
2178 case INTEGER_CST: return TS_INT_CST;
2179 case REAL_CST: return TS_REAL_CST;
2180 case COMPLEX_CST: return TS_COMPLEX;
2181 case VECTOR_CST: return TS_VECTOR;
2182 case STRING_CST: return TS_STRING;
2183 /* tcc_exceptional cases. */
2184 /* FIXME tuples: eventually this should be TS_BASE. For now, nothing
2185 returns TS_BASE. */
2186 case ERROR_MARK: return TS_COMMON;
2187 case IDENTIFIER_NODE: return TS_IDENTIFIER;
2188 case TREE_LIST: return TS_LIST;
2189 case TREE_VEC: return TS_VEC;
2190 case PHI_NODE: return TS_PHI_NODE;
2191 case SSA_NAME: return TS_SSA_NAME;
2192 case PLACEHOLDER_EXPR: return TS_COMMON;
2193 case STATEMENT_LIST: return TS_STATEMENT_LIST;
2194 case BLOCK: return TS_BLOCK;
2195 case CONSTRUCTOR: return TS_CONSTRUCTOR;
2196 case TREE_BINFO: return TS_BINFO;
2197 case VALUE_HANDLE: return TS_VALUE_HANDLE;
2198 case OMP_CLAUSE: return TS_OMP_CLAUSE;
2200 default:
2201 gcc_unreachable ();
2205 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
2206 or offset that depends on a field within a record. */
2208 bool
2209 contains_placeholder_p (tree exp)
2211 enum tree_code code;
2213 if (!exp)
2214 return 0;
2216 code = TREE_CODE (exp);
2217 if (code == PLACEHOLDER_EXPR)
2218 return 1;
2220 switch (TREE_CODE_CLASS (code))
2222 case tcc_reference:
2223 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
2224 position computations since they will be converted into a
2225 WITH_RECORD_EXPR involving the reference, which will assume
2226 here will be valid. */
2227 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2229 case tcc_exceptional:
2230 if (code == TREE_LIST)
2231 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
2232 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
2233 break;
2235 case tcc_unary:
2236 case tcc_binary:
2237 case tcc_comparison:
2238 case tcc_expression:
2239 switch (code)
2241 case COMPOUND_EXPR:
2242 /* Ignoring the first operand isn't quite right, but works best. */
2243 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
2245 case COND_EXPR:
2246 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2247 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
2248 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
2250 case CALL_EXPR:
2251 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
2253 default:
2254 break;
2257 switch (TREE_CODE_LENGTH (code))
2259 case 1:
2260 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2261 case 2:
2262 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2263 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
2264 default:
2265 return 0;
2268 default:
2269 return 0;
2271 return 0;
2274 /* Return true if any part of the computation of TYPE involves a
2275 PLACEHOLDER_EXPR. This includes size, bounds, qualifiers
2276 (for QUAL_UNION_TYPE) and field positions. */
2278 static bool
2279 type_contains_placeholder_1 (tree type)
2281 /* If the size contains a placeholder or the parent type (component type in
2282 the case of arrays) type involves a placeholder, this type does. */
2283 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
2284 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
2285 || (TREE_TYPE (type) != 0
2286 && type_contains_placeholder_p (TREE_TYPE (type))))
2287 return true;
2289 /* Now do type-specific checks. Note that the last part of the check above
2290 greatly limits what we have to do below. */
2291 switch (TREE_CODE (type))
2293 case VOID_TYPE:
2294 case COMPLEX_TYPE:
2295 case ENUMERAL_TYPE:
2296 case BOOLEAN_TYPE:
2297 case POINTER_TYPE:
2298 case OFFSET_TYPE:
2299 case REFERENCE_TYPE:
2300 case METHOD_TYPE:
2301 case FUNCTION_TYPE:
2302 case VECTOR_TYPE:
2303 return false;
2305 case INTEGER_TYPE:
2306 case REAL_TYPE:
2307 /* Here we just check the bounds. */
2308 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
2309 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
2311 case ARRAY_TYPE:
2312 /* We're already checked the component type (TREE_TYPE), so just check
2313 the index type. */
2314 return type_contains_placeholder_p (TYPE_DOMAIN (type));
2316 case RECORD_TYPE:
2317 case UNION_TYPE:
2318 case QUAL_UNION_TYPE:
2320 tree field;
2322 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2323 if (TREE_CODE (field) == FIELD_DECL
2324 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
2325 || (TREE_CODE (type) == QUAL_UNION_TYPE
2326 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
2327 || type_contains_placeholder_p (TREE_TYPE (field))))
2328 return true;
2330 return false;
2333 default:
2334 gcc_unreachable ();
2338 bool
2339 type_contains_placeholder_p (tree type)
2341 bool result;
2343 /* If the contains_placeholder_bits field has been initialized,
2344 then we know the answer. */
2345 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
2346 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
2348 /* Indicate that we've seen this type node, and the answer is false.
2349 This is what we want to return if we run into recursion via fields. */
2350 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
2352 /* Compute the real value. */
2353 result = type_contains_placeholder_1 (type);
2355 /* Store the real value. */
2356 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
2358 return result;
2361 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
2362 return a tree with all occurrences of references to F in a
2363 PLACEHOLDER_EXPR replaced by R. Note that we assume here that EXP
2364 contains only arithmetic expressions or a CALL_EXPR with a
2365 PLACEHOLDER_EXPR occurring only in its arglist. */
2367 tree
2368 substitute_in_expr (tree exp, tree f, tree r)
2370 enum tree_code code = TREE_CODE (exp);
2371 tree op0, op1, op2, op3;
2372 tree new;
2373 tree inner;
2375 /* We handle TREE_LIST and COMPONENT_REF separately. */
2376 if (code == TREE_LIST)
2378 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
2379 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
2380 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2381 return exp;
2383 return tree_cons (TREE_PURPOSE (exp), op1, op0);
2385 else if (code == COMPONENT_REF)
2387 /* If this expression is getting a value from a PLACEHOLDER_EXPR
2388 and it is the right field, replace it with R. */
2389 for (inner = TREE_OPERAND (exp, 0);
2390 REFERENCE_CLASS_P (inner);
2391 inner = TREE_OPERAND (inner, 0))
2393 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2394 && TREE_OPERAND (exp, 1) == f)
2395 return r;
2397 /* If this expression hasn't been completed let, leave it alone. */
2398 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && TREE_TYPE (inner) == 0)
2399 return exp;
2401 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2402 if (op0 == TREE_OPERAND (exp, 0))
2403 return exp;
2405 new = fold_build3 (COMPONENT_REF, TREE_TYPE (exp),
2406 op0, TREE_OPERAND (exp, 1), NULL_TREE);
2408 else
2409 switch (TREE_CODE_CLASS (code))
2411 case tcc_constant:
2412 case tcc_declaration:
2413 return exp;
2415 case tcc_exceptional:
2416 case tcc_unary:
2417 case tcc_binary:
2418 case tcc_comparison:
2419 case tcc_expression:
2420 case tcc_reference:
2421 switch (TREE_CODE_LENGTH (code))
2423 case 0:
2424 return exp;
2426 case 1:
2427 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2428 if (op0 == TREE_OPERAND (exp, 0))
2429 return exp;
2431 new = fold_build1 (code, TREE_TYPE (exp), op0);
2432 break;
2434 case 2:
2435 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2436 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2438 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2439 return exp;
2441 new = fold_build2 (code, TREE_TYPE (exp), op0, op1);
2442 break;
2444 case 3:
2445 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2446 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2447 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
2449 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2450 && op2 == TREE_OPERAND (exp, 2))
2451 return exp;
2453 new = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
2454 break;
2456 case 4:
2457 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2458 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2459 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
2460 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
2462 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2463 && op2 == TREE_OPERAND (exp, 2)
2464 && op3 == TREE_OPERAND (exp, 3))
2465 return exp;
2467 new = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
2468 break;
2470 default:
2471 gcc_unreachable ();
2473 break;
2475 default:
2476 gcc_unreachable ();
2479 TREE_READONLY (new) = TREE_READONLY (exp);
2480 return new;
2483 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
2484 for it within OBJ, a tree that is an object or a chain of references. */
2486 tree
2487 substitute_placeholder_in_expr (tree exp, tree obj)
2489 enum tree_code code = TREE_CODE (exp);
2490 tree op0, op1, op2, op3;
2492 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
2493 in the chain of OBJ. */
2494 if (code == PLACEHOLDER_EXPR)
2496 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
2497 tree elt;
2499 for (elt = obj; elt != 0;
2500 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
2501 || TREE_CODE (elt) == COND_EXPR)
2502 ? TREE_OPERAND (elt, 1)
2503 : (REFERENCE_CLASS_P (elt)
2504 || UNARY_CLASS_P (elt)
2505 || BINARY_CLASS_P (elt)
2506 || EXPRESSION_CLASS_P (elt))
2507 ? TREE_OPERAND (elt, 0) : 0))
2508 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
2509 return elt;
2511 for (elt = obj; elt != 0;
2512 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
2513 || TREE_CODE (elt) == COND_EXPR)
2514 ? TREE_OPERAND (elt, 1)
2515 : (REFERENCE_CLASS_P (elt)
2516 || UNARY_CLASS_P (elt)
2517 || BINARY_CLASS_P (elt)
2518 || EXPRESSION_CLASS_P (elt))
2519 ? TREE_OPERAND (elt, 0) : 0))
2520 if (POINTER_TYPE_P (TREE_TYPE (elt))
2521 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
2522 == need_type))
2523 return fold_build1 (INDIRECT_REF, need_type, elt);
2525 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
2526 survives until RTL generation, there will be an error. */
2527 return exp;
2530 /* TREE_LIST is special because we need to look at TREE_VALUE
2531 and TREE_CHAIN, not TREE_OPERANDS. */
2532 else if (code == TREE_LIST)
2534 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
2535 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
2536 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2537 return exp;
2539 return tree_cons (TREE_PURPOSE (exp), op1, op0);
2541 else
2542 switch (TREE_CODE_CLASS (code))
2544 case tcc_constant:
2545 case tcc_declaration:
2546 return exp;
2548 case tcc_exceptional:
2549 case tcc_unary:
2550 case tcc_binary:
2551 case tcc_comparison:
2552 case tcc_expression:
2553 case tcc_reference:
2554 case tcc_statement:
2555 switch (TREE_CODE_LENGTH (code))
2557 case 0:
2558 return exp;
2560 case 1:
2561 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2562 if (op0 == TREE_OPERAND (exp, 0))
2563 return exp;
2564 else
2565 return fold_build1 (code, TREE_TYPE (exp), op0);
2567 case 2:
2568 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2569 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2571 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2572 return exp;
2573 else
2574 return fold_build2 (code, TREE_TYPE (exp), op0, op1);
2576 case 3:
2577 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2578 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2579 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
2581 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2582 && op2 == TREE_OPERAND (exp, 2))
2583 return exp;
2584 else
2585 return fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
2587 case 4:
2588 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2589 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2590 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
2591 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
2593 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2594 && op2 == TREE_OPERAND (exp, 2)
2595 && op3 == TREE_OPERAND (exp, 3))
2596 return exp;
2597 else
2598 return fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
2600 default:
2601 gcc_unreachable ();
2603 break;
2605 default:
2606 gcc_unreachable ();
2610 /* Stabilize a reference so that we can use it any number of times
2611 without causing its operands to be evaluated more than once.
2612 Returns the stabilized reference. This works by means of save_expr,
2613 so see the caveats in the comments about save_expr.
2615 Also allows conversion expressions whose operands are references.
2616 Any other kind of expression is returned unchanged. */
2618 tree
2619 stabilize_reference (tree ref)
2621 tree result;
2622 enum tree_code code = TREE_CODE (ref);
2624 switch (code)
2626 case VAR_DECL:
2627 case PARM_DECL:
2628 case RESULT_DECL:
2629 /* No action is needed in this case. */
2630 return ref;
2632 case NOP_EXPR:
2633 case CONVERT_EXPR:
2634 case FLOAT_EXPR:
2635 case FIX_TRUNC_EXPR:
2636 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2637 break;
2639 case INDIRECT_REF:
2640 result = build_nt (INDIRECT_REF,
2641 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2642 break;
2644 case COMPONENT_REF:
2645 result = build_nt (COMPONENT_REF,
2646 stabilize_reference (TREE_OPERAND (ref, 0)),
2647 TREE_OPERAND (ref, 1), NULL_TREE);
2648 break;
2650 case BIT_FIELD_REF:
2651 result = build_nt (BIT_FIELD_REF,
2652 stabilize_reference (TREE_OPERAND (ref, 0)),
2653 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2654 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2655 break;
2657 case ARRAY_REF:
2658 result = build_nt (ARRAY_REF,
2659 stabilize_reference (TREE_OPERAND (ref, 0)),
2660 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2661 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
2662 break;
2664 case ARRAY_RANGE_REF:
2665 result = build_nt (ARRAY_RANGE_REF,
2666 stabilize_reference (TREE_OPERAND (ref, 0)),
2667 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2668 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
2669 break;
2671 case COMPOUND_EXPR:
2672 /* We cannot wrap the first expression in a SAVE_EXPR, as then
2673 it wouldn't be ignored. This matters when dealing with
2674 volatiles. */
2675 return stabilize_reference_1 (ref);
2677 /* If arg isn't a kind of lvalue we recognize, make no change.
2678 Caller should recognize the error for an invalid lvalue. */
2679 default:
2680 return ref;
2682 case ERROR_MARK:
2683 return error_mark_node;
2686 TREE_TYPE (result) = TREE_TYPE (ref);
2687 TREE_READONLY (result) = TREE_READONLY (ref);
2688 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
2689 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
2691 return result;
2694 /* Subroutine of stabilize_reference; this is called for subtrees of
2695 references. Any expression with side-effects must be put in a SAVE_EXPR
2696 to ensure that it is only evaluated once.
2698 We don't put SAVE_EXPR nodes around everything, because assigning very
2699 simple expressions to temporaries causes us to miss good opportunities
2700 for optimizations. Among other things, the opportunity to fold in the
2701 addition of a constant into an addressing mode often gets lost, e.g.
2702 "y[i+1] += x;". In general, we take the approach that we should not make
2703 an assignment unless we are forced into it - i.e., that any non-side effect
2704 operator should be allowed, and that cse should take care of coalescing
2705 multiple utterances of the same expression should that prove fruitful. */
2707 tree
2708 stabilize_reference_1 (tree e)
2710 tree result;
2711 enum tree_code code = TREE_CODE (e);
2713 /* We cannot ignore const expressions because it might be a reference
2714 to a const array but whose index contains side-effects. But we can
2715 ignore things that are actual constant or that already have been
2716 handled by this function. */
2718 if (TREE_INVARIANT (e))
2719 return e;
2721 switch (TREE_CODE_CLASS (code))
2723 case tcc_exceptional:
2724 case tcc_type:
2725 case tcc_declaration:
2726 case tcc_comparison:
2727 case tcc_statement:
2728 case tcc_expression:
2729 case tcc_reference:
2730 /* If the expression has side-effects, then encase it in a SAVE_EXPR
2731 so that it will only be evaluated once. */
2732 /* The reference (r) and comparison (<) classes could be handled as
2733 below, but it is generally faster to only evaluate them once. */
2734 if (TREE_SIDE_EFFECTS (e))
2735 return save_expr (e);
2736 return e;
2738 case tcc_constant:
2739 /* Constants need no processing. In fact, we should never reach
2740 here. */
2741 return e;
2743 case tcc_binary:
2744 /* Division is slow and tends to be compiled with jumps,
2745 especially the division by powers of 2 that is often
2746 found inside of an array reference. So do it just once. */
2747 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
2748 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
2749 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
2750 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
2751 return save_expr (e);
2752 /* Recursively stabilize each operand. */
2753 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
2754 stabilize_reference_1 (TREE_OPERAND (e, 1)));
2755 break;
2757 case tcc_unary:
2758 /* Recursively stabilize each operand. */
2759 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
2760 break;
2762 default:
2763 gcc_unreachable ();
2766 TREE_TYPE (result) = TREE_TYPE (e);
2767 TREE_READONLY (result) = TREE_READONLY (e);
2768 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
2769 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
2770 TREE_INVARIANT (result) = 1;
2772 return result;
2775 /* Low-level constructors for expressions. */
2777 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
2778 TREE_INVARIANT, and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
2780 void
2781 recompute_tree_invariant_for_addr_expr (tree t)
2783 tree node;
2784 bool tc = true, ti = true, se = false;
2786 /* We started out assuming this address is both invariant and constant, but
2787 does not have side effects. Now go down any handled components and see if
2788 any of them involve offsets that are either non-constant or non-invariant.
2789 Also check for side-effects.
2791 ??? Note that this code makes no attempt to deal with the case where
2792 taking the address of something causes a copy due to misalignment. */
2794 #define UPDATE_TITCSE(NODE) \
2795 do { tree _node = (NODE); \
2796 if (_node && !TREE_INVARIANT (_node)) ti = false; \
2797 if (_node && !TREE_CONSTANT (_node)) tc = false; \
2798 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
2800 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
2801 node = TREE_OPERAND (node, 0))
2803 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
2804 array reference (probably made temporarily by the G++ front end),
2805 so ignore all the operands. */
2806 if ((TREE_CODE (node) == ARRAY_REF
2807 || TREE_CODE (node) == ARRAY_RANGE_REF)
2808 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
2810 UPDATE_TITCSE (TREE_OPERAND (node, 1));
2811 if (TREE_OPERAND (node, 2))
2812 UPDATE_TITCSE (TREE_OPERAND (node, 2));
2813 if (TREE_OPERAND (node, 3))
2814 UPDATE_TITCSE (TREE_OPERAND (node, 3));
2816 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
2817 FIELD_DECL, apparently. The G++ front end can put something else
2818 there, at least temporarily. */
2819 else if (TREE_CODE (node) == COMPONENT_REF
2820 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
2822 if (TREE_OPERAND (node, 2))
2823 UPDATE_TITCSE (TREE_OPERAND (node, 2));
2825 else if (TREE_CODE (node) == BIT_FIELD_REF)
2826 UPDATE_TITCSE (TREE_OPERAND (node, 2));
2829 node = lang_hooks.expr_to_decl (node, &tc, &ti, &se);
2831 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
2832 the address, since &(*a)->b is a form of addition. If it's a decl, it's
2833 invariant and constant if the decl is static. It's also invariant if it's
2834 a decl in the current function. Taking the address of a volatile variable
2835 is not volatile. If it's a constant, the address is both invariant and
2836 constant. Otherwise it's neither. */
2837 if (TREE_CODE (node) == INDIRECT_REF)
2838 UPDATE_TITCSE (TREE_OPERAND (node, 0));
2839 else if (DECL_P (node))
2841 if (staticp (node))
2843 else if (decl_function_context (node) == current_function_decl
2844 /* Addresses of thread-local variables are invariant. */
2845 || (TREE_CODE (node) == VAR_DECL
2846 && DECL_THREAD_LOCAL_P (node)))
2847 tc = false;
2848 else
2849 ti = tc = false;
2851 else if (CONSTANT_CLASS_P (node))
2853 else
2855 ti = tc = false;
2856 se |= TREE_SIDE_EFFECTS (node);
2859 TREE_CONSTANT (t) = tc;
2860 TREE_INVARIANT (t) = ti;
2861 TREE_SIDE_EFFECTS (t) = se;
2862 #undef UPDATE_TITCSE
2865 /* Build an expression of code CODE, data type TYPE, and operands as
2866 specified. Expressions and reference nodes can be created this way.
2867 Constants, decls, types and misc nodes cannot be.
2869 We define 5 non-variadic functions, from 0 to 4 arguments. This is
2870 enough for all extant tree codes. */
2872 tree
2873 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
2875 tree t;
2877 gcc_assert (TREE_CODE_LENGTH (code) == 0);
2879 t = make_node_stat (code PASS_MEM_STAT);
2880 TREE_TYPE (t) = tt;
2882 return t;
2885 tree
2886 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
2888 int length = sizeof (struct tree_exp);
2889 #ifdef GATHER_STATISTICS
2890 tree_node_kind kind;
2891 #endif
2892 tree t;
2894 #ifdef GATHER_STATISTICS
2895 switch (TREE_CODE_CLASS (code))
2897 case tcc_statement: /* an expression with side effects */
2898 kind = s_kind;
2899 break;
2900 case tcc_reference: /* a reference */
2901 kind = r_kind;
2902 break;
2903 default:
2904 kind = e_kind;
2905 break;
2908 tree_node_counts[(int) kind]++;
2909 tree_node_sizes[(int) kind] += length;
2910 #endif
2912 gcc_assert (TREE_CODE_LENGTH (code) == 1);
2914 t = ggc_alloc_zone_pass_stat (length, &tree_zone);
2916 memset (t, 0, sizeof (struct tree_common));
2918 TREE_SET_CODE (t, code);
2920 TREE_TYPE (t) = type;
2921 #ifdef USE_MAPPED_LOCATION
2922 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
2923 #else
2924 SET_EXPR_LOCUS (t, NULL);
2925 #endif
2926 TREE_OPERAND (t, 0) = node;
2927 TREE_BLOCK (t) = NULL_TREE;
2928 if (node && !TYPE_P (node))
2930 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
2931 TREE_READONLY (t) = TREE_READONLY (node);
2934 if (TREE_CODE_CLASS (code) == tcc_statement)
2935 TREE_SIDE_EFFECTS (t) = 1;
2936 else switch (code)
2938 case VA_ARG_EXPR:
2939 /* All of these have side-effects, no matter what their
2940 operands are. */
2941 TREE_SIDE_EFFECTS (t) = 1;
2942 TREE_READONLY (t) = 0;
2943 break;
2945 case MISALIGNED_INDIRECT_REF:
2946 case ALIGN_INDIRECT_REF:
2947 case INDIRECT_REF:
2948 /* Whether a dereference is readonly has nothing to do with whether
2949 its operand is readonly. */
2950 TREE_READONLY (t) = 0;
2951 break;
2953 case ADDR_EXPR:
2954 if (node)
2955 recompute_tree_invariant_for_addr_expr (t);
2956 break;
2958 default:
2959 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
2960 && node && !TYPE_P (node)
2961 && TREE_CONSTANT (node))
2962 TREE_CONSTANT (t) = 1;
2963 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
2964 && node && TREE_INVARIANT (node))
2965 TREE_INVARIANT (t) = 1;
2966 if (TREE_CODE_CLASS (code) == tcc_reference
2967 && node && TREE_THIS_VOLATILE (node))
2968 TREE_THIS_VOLATILE (t) = 1;
2969 break;
2972 return t;
2975 #define PROCESS_ARG(N) \
2976 do { \
2977 TREE_OPERAND (t, N) = arg##N; \
2978 if (arg##N &&!TYPE_P (arg##N)) \
2980 if (TREE_SIDE_EFFECTS (arg##N)) \
2981 side_effects = 1; \
2982 if (!TREE_READONLY (arg##N)) \
2983 read_only = 0; \
2984 if (!TREE_CONSTANT (arg##N)) \
2985 constant = 0; \
2986 if (!TREE_INVARIANT (arg##N)) \
2987 invariant = 0; \
2989 } while (0)
2991 tree
2992 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
2994 bool constant, read_only, side_effects, invariant;
2995 tree t;
2997 gcc_assert (TREE_CODE_LENGTH (code) == 2);
2999 if (code == MODIFY_EXPR && cfun && cfun->gimplified)
3001 /* We should be talking GIMPLE_MODIFY_STMT by now. */
3002 gcc_unreachable ();
3005 /* FIXME tuples: For now let's be lazy; later we must rewrite all
3006 build2 calls to build2_gimple calls. */
3007 if (TREE_CODE_CLASS (code) == tcc_gimple_stmt)
3008 return build2_gimple (code, arg0, arg1);
3010 t = make_node_stat (code PASS_MEM_STAT);
3011 TREE_TYPE (t) = tt;
3013 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
3014 result based on those same flags for the arguments. But if the
3015 arguments aren't really even `tree' expressions, we shouldn't be trying
3016 to do this. */
3018 /* Expressions without side effects may be constant if their
3019 arguments are as well. */
3020 constant = (TREE_CODE_CLASS (code) == tcc_comparison
3021 || TREE_CODE_CLASS (code) == tcc_binary);
3022 read_only = 1;
3023 side_effects = TREE_SIDE_EFFECTS (t);
3024 invariant = constant;
3026 PROCESS_ARG(0);
3027 PROCESS_ARG(1);
3029 TREE_READONLY (t) = read_only;
3030 TREE_CONSTANT (t) = constant;
3031 TREE_INVARIANT (t) = invariant;
3032 TREE_SIDE_EFFECTS (t) = side_effects;
3033 TREE_THIS_VOLATILE (t)
3034 = (TREE_CODE_CLASS (code) == tcc_reference
3035 && arg0 && TREE_THIS_VOLATILE (arg0));
3037 return t;
3041 /* Similar as build2_stat, but for GIMPLE tuples. For convenience's sake,
3042 arguments and return type are trees. */
3044 tree
3045 build2_gimple_stat (enum tree_code code, tree arg0, tree arg1 MEM_STAT_DECL)
3047 bool side_effects;
3048 tree t;
3050 gcc_assert (TREE_CODE_LENGTH (code) == 2);
3052 t = make_node_stat (code PASS_MEM_STAT);
3054 side_effects = TREE_SIDE_EFFECTS (t);
3056 /* ?? We don't care about setting flags for tuples... */
3057 GIMPLE_STMT_OPERAND (t, 0) = arg0;
3058 GIMPLE_STMT_OPERAND (t, 1) = arg1;
3060 /* ...except perhaps side_effects and volatility. ?? */
3061 TREE_SIDE_EFFECTS (t) = side_effects;
3062 TREE_THIS_VOLATILE (t) = (TREE_CODE_CLASS (code) == tcc_reference
3063 && arg0 && TREE_THIS_VOLATILE (arg0));
3066 return t;
3069 tree
3070 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3071 tree arg2 MEM_STAT_DECL)
3073 bool constant, read_only, side_effects, invariant;
3074 tree t;
3076 gcc_assert (TREE_CODE_LENGTH (code) == 3);
3078 t = make_node_stat (code PASS_MEM_STAT);
3079 TREE_TYPE (t) = tt;
3081 side_effects = TREE_SIDE_EFFECTS (t);
3083 PROCESS_ARG(0);
3084 PROCESS_ARG(1);
3085 PROCESS_ARG(2);
3087 if (code == CALL_EXPR && !side_effects)
3089 tree node;
3090 int i;
3092 /* Calls have side-effects, except those to const or
3093 pure functions. */
3094 i = call_expr_flags (t);
3095 if (!(i & (ECF_CONST | ECF_PURE)))
3096 side_effects = 1;
3098 /* And even those have side-effects if their arguments do. */
3099 else for (node = arg1; node; node = TREE_CHAIN (node))
3100 if (TREE_SIDE_EFFECTS (TREE_VALUE (node)))
3102 side_effects = 1;
3103 break;
3107 TREE_SIDE_EFFECTS (t) = side_effects;
3108 TREE_THIS_VOLATILE (t)
3109 = (TREE_CODE_CLASS (code) == tcc_reference
3110 && arg0 && TREE_THIS_VOLATILE (arg0));
3112 return t;
3115 tree
3116 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3117 tree arg2, tree arg3 MEM_STAT_DECL)
3119 bool constant, read_only, side_effects, invariant;
3120 tree t;
3122 gcc_assert (TREE_CODE_LENGTH (code) == 4);
3124 t = make_node_stat (code PASS_MEM_STAT);
3125 TREE_TYPE (t) = tt;
3127 side_effects = TREE_SIDE_EFFECTS (t);
3129 PROCESS_ARG(0);
3130 PROCESS_ARG(1);
3131 PROCESS_ARG(2);
3132 PROCESS_ARG(3);
3134 TREE_SIDE_EFFECTS (t) = side_effects;
3135 TREE_THIS_VOLATILE (t)
3136 = (TREE_CODE_CLASS (code) == tcc_reference
3137 && arg0 && TREE_THIS_VOLATILE (arg0));
3139 return t;
3142 tree
3143 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3144 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
3146 bool constant, read_only, side_effects, invariant;
3147 tree t;
3149 gcc_assert (TREE_CODE_LENGTH (code) == 5);
3151 t = make_node_stat (code PASS_MEM_STAT);
3152 TREE_TYPE (t) = tt;
3154 side_effects = TREE_SIDE_EFFECTS (t);
3156 PROCESS_ARG(0);
3157 PROCESS_ARG(1);
3158 PROCESS_ARG(2);
3159 PROCESS_ARG(3);
3160 PROCESS_ARG(4);
3162 TREE_SIDE_EFFECTS (t) = side_effects;
3163 TREE_THIS_VOLATILE (t)
3164 = (TREE_CODE_CLASS (code) == tcc_reference
3165 && arg0 && TREE_THIS_VOLATILE (arg0));
3167 return t;
3170 tree
3171 build7_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3172 tree arg2, tree arg3, tree arg4, tree arg5,
3173 tree arg6 MEM_STAT_DECL)
3175 bool constant, read_only, side_effects, invariant;
3176 tree t;
3178 gcc_assert (code == TARGET_MEM_REF);
3180 t = make_node_stat (code PASS_MEM_STAT);
3181 TREE_TYPE (t) = tt;
3183 side_effects = TREE_SIDE_EFFECTS (t);
3185 PROCESS_ARG(0);
3186 PROCESS_ARG(1);
3187 PROCESS_ARG(2);
3188 PROCESS_ARG(3);
3189 PROCESS_ARG(4);
3190 PROCESS_ARG(5);
3191 PROCESS_ARG(6);
3193 TREE_SIDE_EFFECTS (t) = side_effects;
3194 TREE_THIS_VOLATILE (t) = 0;
3196 return t;
3199 /* Similar except don't specify the TREE_TYPE
3200 and leave the TREE_SIDE_EFFECTS as 0.
3201 It is permissible for arguments to be null,
3202 or even garbage if their values do not matter. */
3204 tree
3205 build_nt (enum tree_code code, ...)
3207 tree t;
3208 int length;
3209 int i;
3210 va_list p;
3212 va_start (p, code);
3214 t = make_node (code);
3215 length = TREE_CODE_LENGTH (code);
3217 for (i = 0; i < length; i++)
3218 TREE_OPERAND (t, i) = va_arg (p, tree);
3220 va_end (p);
3221 return t;
3224 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
3225 We do NOT enter this node in any sort of symbol table.
3227 layout_decl is used to set up the decl's storage layout.
3228 Other slots are initialized to 0 or null pointers. */
3230 tree
3231 build_decl_stat (enum tree_code code, tree name, tree type MEM_STAT_DECL)
3233 tree t;
3235 t = make_node_stat (code PASS_MEM_STAT);
3237 /* if (type == error_mark_node)
3238 type = integer_type_node; */
3239 /* That is not done, deliberately, so that having error_mark_node
3240 as the type can suppress useless errors in the use of this variable. */
3242 DECL_NAME (t) = name;
3243 TREE_TYPE (t) = type;
3245 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
3246 layout_decl (t, 0);
3247 else if (code == FUNCTION_DECL)
3248 DECL_MODE (t) = FUNCTION_MODE;
3250 return t;
3253 /* Builds and returns function declaration with NAME and TYPE. */
3255 tree
3256 build_fn_decl (const char *name, tree type)
3258 tree id = get_identifier (name);
3259 tree decl = build_decl (FUNCTION_DECL, id, type);
3261 DECL_EXTERNAL (decl) = 1;
3262 TREE_PUBLIC (decl) = 1;
3263 DECL_ARTIFICIAL (decl) = 1;
3264 TREE_NOTHROW (decl) = 1;
3266 return decl;
3270 /* BLOCK nodes are used to represent the structure of binding contours
3271 and declarations, once those contours have been exited and their contents
3272 compiled. This information is used for outputting debugging info. */
3274 tree
3275 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
3277 tree block = make_node (BLOCK);
3279 BLOCK_VARS (block) = vars;
3280 BLOCK_SUBBLOCKS (block) = subblocks;
3281 BLOCK_SUPERCONTEXT (block) = supercontext;
3282 BLOCK_CHAIN (block) = chain;
3283 return block;
3286 #if 1 /* ! defined(USE_MAPPED_LOCATION) */
3287 /* ??? gengtype doesn't handle conditionals */
3288 static GTY(()) source_locus last_annotated_node;
3289 #endif
3291 #ifdef USE_MAPPED_LOCATION
3293 expanded_location
3294 expand_location (source_location loc)
3296 expanded_location xloc;
3297 if (loc == 0)
3299 xloc.file = NULL;
3300 xloc.line = 0;
3301 xloc.column = 0;
3303 else
3305 const struct line_map *map = linemap_lookup (&line_table, loc);
3306 xloc.file = map->to_file;
3307 xloc.line = SOURCE_LINE (map, loc);
3308 xloc.column = SOURCE_COLUMN (map, loc);
3310 return xloc;
3313 #else
3315 /* Record the exact location where an expression or an identifier were
3316 encountered. */
3318 void
3319 annotate_with_file_line (tree node, const char *file, int line)
3321 /* Roughly one percent of the calls to this function are to annotate
3322 a node with the same information already attached to that node!
3323 Just return instead of wasting memory. */
3324 if (EXPR_LOCUS (node)
3325 && EXPR_LINENO (node) == line
3326 && (EXPR_FILENAME (node) == file
3327 || !strcmp (EXPR_FILENAME (node), file)))
3329 last_annotated_node = EXPR_LOCUS (node);
3330 return;
3333 /* In heavily macroized code (such as GCC itself) this single
3334 entry cache can reduce the number of allocations by more
3335 than half. */
3336 if (last_annotated_node
3337 && last_annotated_node->line == line
3338 && (last_annotated_node->file == file
3339 || !strcmp (last_annotated_node->file, file)))
3341 SET_EXPR_LOCUS (node, last_annotated_node);
3342 return;
3345 SET_EXPR_LOCUS (node, ggc_alloc (sizeof (location_t)));
3346 EXPR_LINENO (node) = line;
3347 EXPR_FILENAME (node) = file;
3348 last_annotated_node = EXPR_LOCUS (node);
3351 void
3352 annotate_with_locus (tree node, location_t locus)
3354 annotate_with_file_line (node, locus.file, locus.line);
3356 #endif
3358 /* Source location accessor functions. */
3361 /* The source location of this expression. Non-tree_exp nodes such as
3362 decls and constants can be shared among multiple locations, so
3363 return nothing. */
3364 location_t
3365 expr_location (tree node)
3367 #ifdef USE_MAPPED_LOCATION
3368 if (GIMPLE_STMT_P (node))
3369 return GIMPLE_STMT_LOCUS (node);
3370 return EXPR_P (node) ? node->exp.locus : UNKNOWN_LOCATION;
3371 #else
3372 if (GIMPLE_STMT_P (node))
3373 return EXPR_HAS_LOCATION (node)
3374 ? *GIMPLE_STMT_LOCUS (node) : UNKNOWN_LOCATION;
3375 return EXPR_HAS_LOCATION (node) ? *node->exp.locus : UNKNOWN_LOCATION;
3376 #endif
3379 void
3380 set_expr_location (tree node, location_t locus)
3382 #ifdef USE_MAPPED_LOCATION
3383 if (GIMPLE_STMT_P (node))
3384 GIMPLE_STMT_LOCUS (node) = locus;
3385 else
3386 EXPR_CHECK (node)->exp.locus = locus;
3387 #else
3388 annotate_with_locus (node, locus);
3389 #endif
3392 bool
3393 expr_has_location (tree node)
3395 #ifdef USE_MAPPED_LOCATION
3396 return expr_location (node) != UNKNOWN_LOCATION;
3397 #else
3398 return expr_locus (node) != NULL;
3399 #endif
3402 #ifdef USE_MAPPED_LOCATION
3403 source_location *
3404 #else
3405 source_locus
3406 #endif
3407 expr_locus (tree node)
3409 #ifdef USE_MAPPED_LOCATION
3410 if (GIMPLE_STMT_P (node))
3411 return &GIMPLE_STMT_LOCUS (node);
3412 return EXPR_P (node) ? &node->exp.locus : (location_t *) NULL;
3413 #else
3414 if (GIMPLE_STMT_P (node))
3415 return GIMPLE_STMT_LOCUS (node);
3416 /* ?? The cast below was originally "(location_t *)" in the macro,
3417 but that makes no sense. ?? */
3418 return EXPR_P (node) ? node->exp.locus : (source_locus) NULL;
3419 #endif
3422 void
3423 set_expr_locus (tree node,
3424 #ifdef USE_MAPPED_LOCATION
3425 source_location *loc
3426 #else
3427 source_locus loc
3428 #endif
3431 #ifdef USE_MAPPED_LOCATION
3432 if (loc == NULL)
3434 if (GIMPLE_STMT_P (node))
3435 GIMPLE_STMT_LOCUS (node) = UNKNOWN_LOCATION;
3436 else
3437 EXPR_CHECK (node)->exp.locus = UNKNOWN_LOCATION;
3439 else
3441 if (GIMPLE_STMT_P (node))
3442 GIMPLE_STMT_LOCUS (node) = *loc;
3443 else
3444 EXPR_CHECK (node)->exp.locus = *loc;
3446 #else
3447 if (GIMPLE_STMT_P (node))
3448 GIMPLE_STMT_LOCUS (node) = loc;
3449 else
3450 EXPR_CHECK (node)->exp.locus = loc;
3451 #endif
3454 const char **
3455 expr_filename (tree node)
3457 #ifdef USE_MAPPED_LOCATION
3458 if (GIMPLE_STMT_P (node))
3459 return &LOCATION_FILE (GIMPLE_STMT_LOCUS (node));
3460 return &LOCATION_FILE (EXPR_CHECK (node)->exp.locus);
3461 #else
3462 if (GIMPLE_STMT_P (node))
3463 return &GIMPLE_STMT_LOCUS (node)->file;
3464 return &(EXPR_CHECK (node)->exp.locus->file);
3465 #endif
3468 int *
3469 expr_lineno (tree node)
3471 #ifdef USE_MAPPED_LOCATION
3472 if (GIMPLE_STMT_P (node))
3473 return &LOCATION_LINE (GIMPLE_STMT_LOCUS (node));
3474 return &LOCATION_LINE (EXPR_CHECK (node)->exp.locus);
3475 #else
3476 if (GIMPLE_STMT_P (node))
3477 return &GIMPLE_STMT_LOCUS (node)->line;
3478 return &EXPR_CHECK (node)->exp.locus->line;
3479 #endif
3482 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
3483 is ATTRIBUTE. */
3485 tree
3486 build_decl_attribute_variant (tree ddecl, tree attribute)
3488 DECL_ATTRIBUTES (ddecl) = attribute;
3489 return ddecl;
3492 /* Borrowed from hashtab.c iterative_hash implementation. */
3493 #define mix(a,b,c) \
3495 a -= b; a -= c; a ^= (c>>13); \
3496 b -= c; b -= a; b ^= (a<< 8); \
3497 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
3498 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
3499 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
3500 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
3501 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
3502 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
3503 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
3507 /* Produce good hash value combining VAL and VAL2. */
3508 static inline hashval_t
3509 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
3511 /* the golden ratio; an arbitrary value. */
3512 hashval_t a = 0x9e3779b9;
3514 mix (a, val, val2);
3515 return val2;
3518 /* Produce good hash value combining PTR and VAL2. */
3519 static inline hashval_t
3520 iterative_hash_pointer (void *ptr, hashval_t val2)
3522 if (sizeof (ptr) == sizeof (hashval_t))
3523 return iterative_hash_hashval_t ((size_t) ptr, val2);
3524 else
3526 hashval_t a = (hashval_t) (size_t) ptr;
3527 /* Avoid warnings about shifting of more than the width of the type on
3528 hosts that won't execute this path. */
3529 int zero = 0;
3530 hashval_t b = (hashval_t) ((size_t) ptr >> (sizeof (hashval_t) * 8 + zero));
3531 mix (a, b, val2);
3532 return val2;
3536 /* Produce good hash value combining VAL and VAL2. */
3537 static inline hashval_t
3538 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
3540 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
3541 return iterative_hash_hashval_t (val, val2);
3542 else
3544 hashval_t a = (hashval_t) val;
3545 /* Avoid warnings about shifting of more than the width of the type on
3546 hosts that won't execute this path. */
3547 int zero = 0;
3548 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
3549 mix (a, b, val2);
3550 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
3552 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
3553 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
3554 mix (a, b, val2);
3556 return val2;
3560 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
3561 is ATTRIBUTE and its qualifiers are QUALS.
3563 Record such modified types already made so we don't make duplicates. */
3565 static tree
3566 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
3568 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
3570 hashval_t hashcode = 0;
3571 tree ntype;
3572 enum tree_code code = TREE_CODE (ttype);
3574 ntype = copy_node (ttype);
3576 TYPE_POINTER_TO (ntype) = 0;
3577 TYPE_REFERENCE_TO (ntype) = 0;
3578 TYPE_ATTRIBUTES (ntype) = attribute;
3580 if (TYPE_STRUCTURAL_EQUALITY_P (ttype))
3581 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
3582 else
3583 TYPE_CANONICAL (ntype)
3584 = build_qualified_type (TYPE_CANONICAL (ttype), quals);
3586 /* Create a new main variant of TYPE. */
3587 TYPE_MAIN_VARIANT (ntype) = ntype;
3588 TYPE_NEXT_VARIANT (ntype) = 0;
3589 set_type_quals (ntype, TYPE_UNQUALIFIED);
3591 hashcode = iterative_hash_object (code, hashcode);
3592 if (TREE_TYPE (ntype))
3593 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
3594 hashcode);
3595 hashcode = attribute_hash_list (attribute, hashcode);
3597 switch (TREE_CODE (ntype))
3599 case FUNCTION_TYPE:
3600 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
3601 break;
3602 case ARRAY_TYPE:
3603 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
3604 hashcode);
3605 break;
3606 case INTEGER_TYPE:
3607 hashcode = iterative_hash_object
3608 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
3609 hashcode = iterative_hash_object
3610 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
3611 break;
3612 case REAL_TYPE:
3614 unsigned int precision = TYPE_PRECISION (ntype);
3615 hashcode = iterative_hash_object (precision, hashcode);
3617 break;
3618 default:
3619 break;
3622 ntype = type_hash_canon (hashcode, ntype);
3624 /* If the target-dependent attributes make NTYPE different from
3625 its canonical type, we will need to use structural equality
3626 checks for this qualified type. */
3627 if (!targetm.comp_type_attributes (ntype, ttype))
3628 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
3630 ttype = build_qualified_type (ntype, quals);
3633 return ttype;
3637 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
3638 is ATTRIBUTE.
3640 Record such modified types already made so we don't make duplicates. */
3642 tree
3643 build_type_attribute_variant (tree ttype, tree attribute)
3645 return build_type_attribute_qual_variant (ttype, attribute,
3646 TYPE_QUALS (ttype));
3649 /* Return nonzero if IDENT is a valid name for attribute ATTR,
3650 or zero if not.
3652 We try both `text' and `__text__', ATTR may be either one. */
3653 /* ??? It might be a reasonable simplification to require ATTR to be only
3654 `text'. One might then also require attribute lists to be stored in
3655 their canonicalized form. */
3657 static int
3658 is_attribute_with_length_p (const char *attr, int attr_len, tree ident)
3660 int ident_len;
3661 const char *p;
3663 if (TREE_CODE (ident) != IDENTIFIER_NODE)
3664 return 0;
3666 p = IDENTIFIER_POINTER (ident);
3667 ident_len = IDENTIFIER_LENGTH (ident);
3669 if (ident_len == attr_len
3670 && strcmp (attr, p) == 0)
3671 return 1;
3673 /* If ATTR is `__text__', IDENT must be `text'; and vice versa. */
3674 if (attr[0] == '_')
3676 gcc_assert (attr[1] == '_');
3677 gcc_assert (attr[attr_len - 2] == '_');
3678 gcc_assert (attr[attr_len - 1] == '_');
3679 if (ident_len == attr_len - 4
3680 && strncmp (attr + 2, p, attr_len - 4) == 0)
3681 return 1;
3683 else
3685 if (ident_len == attr_len + 4
3686 && p[0] == '_' && p[1] == '_'
3687 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
3688 && strncmp (attr, p + 2, attr_len) == 0)
3689 return 1;
3692 return 0;
3695 /* Return nonzero if IDENT is a valid name for attribute ATTR,
3696 or zero if not.
3698 We try both `text' and `__text__', ATTR may be either one. */
3701 is_attribute_p (const char *attr, tree ident)
3703 return is_attribute_with_length_p (attr, strlen (attr), ident);
3706 /* Given an attribute name and a list of attributes, return a pointer to the
3707 attribute's list element if the attribute is part of the list, or NULL_TREE
3708 if not found. If the attribute appears more than once, this only
3709 returns the first occurrence; the TREE_CHAIN of the return value should
3710 be passed back in if further occurrences are wanted. */
3712 tree
3713 lookup_attribute (const char *attr_name, tree list)
3715 tree l;
3716 size_t attr_len = strlen (attr_name);
3718 for (l = list; l; l = TREE_CHAIN (l))
3720 gcc_assert (TREE_CODE (TREE_PURPOSE (l)) == IDENTIFIER_NODE);
3721 if (is_attribute_with_length_p (attr_name, attr_len, TREE_PURPOSE (l)))
3722 return l;
3725 return NULL_TREE;
3728 /* Remove any instances of attribute ATTR_NAME in LIST and return the
3729 modified list. */
3731 tree
3732 remove_attribute (const char *attr_name, tree list)
3734 tree *p;
3735 size_t attr_len = strlen (attr_name);
3737 for (p = &list; *p; )
3739 tree l = *p;
3740 gcc_assert (TREE_CODE (TREE_PURPOSE (l)) == IDENTIFIER_NODE);
3741 if (is_attribute_with_length_p (attr_name, attr_len, TREE_PURPOSE (l)))
3742 *p = TREE_CHAIN (l);
3743 else
3744 p = &TREE_CHAIN (l);
3747 return list;
3750 /* Return an attribute list that is the union of a1 and a2. */
3752 tree
3753 merge_attributes (tree a1, tree a2)
3755 tree attributes;
3757 /* Either one unset? Take the set one. */
3759 if ((attributes = a1) == 0)
3760 attributes = a2;
3762 /* One that completely contains the other? Take it. */
3764 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
3766 if (attribute_list_contained (a2, a1))
3767 attributes = a2;
3768 else
3770 /* Pick the longest list, and hang on the other list. */
3772 if (list_length (a1) < list_length (a2))
3773 attributes = a2, a2 = a1;
3775 for (; a2 != 0; a2 = TREE_CHAIN (a2))
3777 tree a;
3778 for (a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
3779 attributes);
3780 a != NULL_TREE;
3781 a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
3782 TREE_CHAIN (a)))
3784 if (TREE_VALUE (a) != NULL
3785 && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
3786 && TREE_VALUE (a2) != NULL
3787 && TREE_CODE (TREE_VALUE (a2)) == TREE_LIST)
3789 if (simple_cst_list_equal (TREE_VALUE (a),
3790 TREE_VALUE (a2)) == 1)
3791 break;
3793 else if (simple_cst_equal (TREE_VALUE (a),
3794 TREE_VALUE (a2)) == 1)
3795 break;
3797 if (a == NULL_TREE)
3799 a1 = copy_node (a2);
3800 TREE_CHAIN (a1) = attributes;
3801 attributes = a1;
3806 return attributes;
3809 /* Given types T1 and T2, merge their attributes and return
3810 the result. */
3812 tree
3813 merge_type_attributes (tree t1, tree t2)
3815 return merge_attributes (TYPE_ATTRIBUTES (t1),
3816 TYPE_ATTRIBUTES (t2));
3819 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
3820 the result. */
3822 tree
3823 merge_decl_attributes (tree olddecl, tree newdecl)
3825 return merge_attributes (DECL_ATTRIBUTES (olddecl),
3826 DECL_ATTRIBUTES (newdecl));
3829 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
3831 /* Specialization of merge_decl_attributes for various Windows targets.
3833 This handles the following situation:
3835 __declspec (dllimport) int foo;
3836 int foo;
3838 The second instance of `foo' nullifies the dllimport. */
3840 tree
3841 merge_dllimport_decl_attributes (tree old, tree new)
3843 tree a;
3844 int delete_dllimport_p = 1;
3846 /* What we need to do here is remove from `old' dllimport if it doesn't
3847 appear in `new'. dllimport behaves like extern: if a declaration is
3848 marked dllimport and a definition appears later, then the object
3849 is not dllimport'd. We also remove a `new' dllimport if the old list
3850 contains dllexport: dllexport always overrides dllimport, regardless
3851 of the order of declaration. */
3852 if (!VAR_OR_FUNCTION_DECL_P (new))
3853 delete_dllimport_p = 0;
3854 else if (DECL_DLLIMPORT_P (new)
3855 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
3857 DECL_DLLIMPORT_P (new) = 0;
3858 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
3859 "dllimport ignored", new);
3861 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new))
3863 /* Warn about overriding a symbol that has already been used. eg:
3864 extern int __attribute__ ((dllimport)) foo;
3865 int* bar () {return &foo;}
3866 int foo;
3868 if (TREE_USED (old))
3870 warning (0, "%q+D redeclared without dllimport attribute "
3871 "after being referenced with dll linkage", new);
3872 /* If we have used a variable's address with dllimport linkage,
3873 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
3874 decl may already have had TREE_INVARIANT and TREE_CONSTANT
3875 computed.
3876 We still remove the attribute so that assembler code refers
3877 to '&foo rather than '_imp__foo'. */
3878 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
3879 DECL_DLLIMPORT_P (new) = 1;
3882 /* Let an inline definition silently override the external reference,
3883 but otherwise warn about attribute inconsistency. */
3884 else if (TREE_CODE (new) == VAR_DECL
3885 || !DECL_DECLARED_INLINE_P (new))
3886 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
3887 "previous dllimport ignored", new);
3889 else
3890 delete_dllimport_p = 0;
3892 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new));
3894 if (delete_dllimport_p)
3896 tree prev, t;
3897 const size_t attr_len = strlen ("dllimport");
3899 /* Scan the list for dllimport and delete it. */
3900 for (prev = NULL_TREE, t = a; t; prev = t, t = TREE_CHAIN (t))
3902 if (is_attribute_with_length_p ("dllimport", attr_len,
3903 TREE_PURPOSE (t)))
3905 if (prev == NULL_TREE)
3906 a = TREE_CHAIN (a);
3907 else
3908 TREE_CHAIN (prev) = TREE_CHAIN (t);
3909 break;
3914 return a;
3917 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
3918 struct attribute_spec.handler. */
3920 tree
3921 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
3922 bool *no_add_attrs)
3924 tree node = *pnode;
3926 /* These attributes may apply to structure and union types being created,
3927 but otherwise should pass to the declaration involved. */
3928 if (!DECL_P (node))
3930 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
3931 | (int) ATTR_FLAG_ARRAY_NEXT))
3933 *no_add_attrs = true;
3934 return tree_cons (name, args, NULL_TREE);
3936 if (TREE_CODE (node) != RECORD_TYPE && TREE_CODE (node) != UNION_TYPE)
3938 warning (OPT_Wattributes, "%qs attribute ignored",
3939 IDENTIFIER_POINTER (name));
3940 *no_add_attrs = true;
3943 return NULL_TREE;
3946 if (TREE_CODE (node) != FUNCTION_DECL
3947 && TREE_CODE (node) != VAR_DECL)
3949 *no_add_attrs = true;
3950 warning (OPT_Wattributes, "%qs attribute ignored",
3951 IDENTIFIER_POINTER (name));
3952 return NULL_TREE;
3955 /* Report error on dllimport ambiguities seen now before they cause
3956 any damage. */
3957 else if (is_attribute_p ("dllimport", name))
3959 /* Honor any target-specific overrides. */
3960 if (!targetm.valid_dllimport_attribute_p (node))
3961 *no_add_attrs = true;
3963 else if (TREE_CODE (node) == FUNCTION_DECL
3964 && DECL_DECLARED_INLINE_P (node))
3966 warning (OPT_Wattributes, "inline function %q+D declared as "
3967 " dllimport: attribute ignored", node);
3968 *no_add_attrs = true;
3970 /* Like MS, treat definition of dllimported variables and
3971 non-inlined functions on declaration as syntax errors. */
3972 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
3974 error ("function %q+D definition is marked dllimport", node);
3975 *no_add_attrs = true;
3978 else if (TREE_CODE (node) == VAR_DECL)
3980 if (DECL_INITIAL (node))
3982 error ("variable %q+D definition is marked dllimport",
3983 node);
3984 *no_add_attrs = true;
3987 /* `extern' needn't be specified with dllimport.
3988 Specify `extern' now and hope for the best. Sigh. */
3989 DECL_EXTERNAL (node) = 1;
3990 /* Also, implicitly give dllimport'd variables declared within
3991 a function global scope, unless declared static. */
3992 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
3993 TREE_PUBLIC (node) = 1;
3996 if (*no_add_attrs == false)
3997 DECL_DLLIMPORT_P (node) = 1;
4000 /* Report error if symbol is not accessible at global scope. */
4001 if (!TREE_PUBLIC (node)
4002 && (TREE_CODE (node) == VAR_DECL
4003 || TREE_CODE (node) == FUNCTION_DECL))
4005 error ("external linkage required for symbol %q+D because of "
4006 "%qs attribute", node, IDENTIFIER_POINTER (name));
4007 *no_add_attrs = true;
4010 return NULL_TREE;
4013 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
4015 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
4016 of the various TYPE_QUAL values. */
4018 static void
4019 set_type_quals (tree type, int type_quals)
4021 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
4022 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
4023 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
4026 /* Returns true iff cand is equivalent to base with type_quals. */
4028 bool
4029 check_qualified_type (tree cand, tree base, int type_quals)
4031 return (TYPE_QUALS (cand) == type_quals
4032 && TYPE_NAME (cand) == TYPE_NAME (base)
4033 /* Apparently this is needed for Objective-C. */
4034 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
4035 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
4036 TYPE_ATTRIBUTES (base)));
4039 /* Return a version of the TYPE, qualified as indicated by the
4040 TYPE_QUALS, if one exists. If no qualified version exists yet,
4041 return NULL_TREE. */
4043 tree
4044 get_qualified_type (tree type, int type_quals)
4046 tree t;
4048 if (TYPE_QUALS (type) == type_quals)
4049 return type;
4051 /* Search the chain of variants to see if there is already one there just
4052 like the one we need to have. If so, use that existing one. We must
4053 preserve the TYPE_NAME, since there is code that depends on this. */
4054 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
4055 if (check_qualified_type (t, type, type_quals))
4056 return t;
4058 return NULL_TREE;
4061 /* Like get_qualified_type, but creates the type if it does not
4062 exist. This function never returns NULL_TREE. */
4064 tree
4065 build_qualified_type (tree type, int type_quals)
4067 tree t;
4069 /* See if we already have the appropriate qualified variant. */
4070 t = get_qualified_type (type, type_quals);
4072 /* If not, build it. */
4073 if (!t)
4075 t = build_variant_type_copy (type);
4076 set_type_quals (t, type_quals);
4078 if (TYPE_STRUCTURAL_EQUALITY_P (type))
4079 /* Propagate structural equality. */
4080 SET_TYPE_STRUCTURAL_EQUALITY (t);
4081 else if (TYPE_CANONICAL (type) != type)
4082 /* Build the underlying canonical type, since it is different
4083 from TYPE. */
4084 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
4085 type_quals);
4086 else
4087 /* T is its own canonical type. */
4088 TYPE_CANONICAL (t) = t;
4092 return t;
4095 /* Create a new distinct copy of TYPE. The new type is made its own
4096 MAIN_VARIANT. If TYPE requires structural equality checks, the
4097 resulting type requires structural equality checks; otherwise, its
4098 TYPE_CANONICAL points to itself. */
4100 tree
4101 build_distinct_type_copy (tree type)
4103 tree t = copy_node (type);
4105 TYPE_POINTER_TO (t) = 0;
4106 TYPE_REFERENCE_TO (t) = 0;
4108 /* Set the canonical type either to a new equivalence class, or
4109 propagate the need for structural equality checks. */
4110 if (TYPE_STRUCTURAL_EQUALITY_P (type))
4111 SET_TYPE_STRUCTURAL_EQUALITY (t);
4112 else
4113 TYPE_CANONICAL (t) = t;
4115 /* Make it its own variant. */
4116 TYPE_MAIN_VARIANT (t) = t;
4117 TYPE_NEXT_VARIANT (t) = 0;
4119 return t;
4122 /* Create a new variant of TYPE, equivalent but distinct. This is so
4123 the caller can modify it. TYPE_CANONICAL for the return type will
4124 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
4125 are considered equal by the language itself (or that both types
4126 require structural equality checks). */
4128 tree
4129 build_variant_type_copy (tree type)
4131 tree t, m = TYPE_MAIN_VARIANT (type);
4133 t = build_distinct_type_copy (type);
4135 /* Since we're building a variant, assume that it is a non-semantic
4136 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
4137 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
4139 /* Add the new type to the chain of variants of TYPE. */
4140 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
4141 TYPE_NEXT_VARIANT (m) = t;
4142 TYPE_MAIN_VARIANT (t) = m;
4144 return t;
4147 /* Return true if the from tree in both tree maps are equal. */
4150 tree_map_eq (const void *va, const void *vb)
4152 const struct tree_map *a = va, *b = vb;
4153 return (a->from == b->from);
4156 /* Hash a from tree in a tree_map. */
4158 unsigned int
4159 tree_map_hash (const void *item)
4161 return (((const struct tree_map *) item)->hash);
4164 /* Return true if this tree map structure is marked for garbage collection
4165 purposes. We simply return true if the from tree is marked, so that this
4166 structure goes away when the from tree goes away. */
4169 tree_map_marked_p (const void *p)
4171 tree from = ((struct tree_map *) p)->from;
4173 return ggc_marked_p (from);
4176 /* Return true if the trees in the tree_int_map *'s VA and VB are equal. */
4179 tree_int_map_eq (const void *va, const void *vb)
4181 const struct tree_int_map *a = va, *b = vb;
4182 return (a->from == b->from);
4185 /* Hash a from tree in the tree_int_map * ITEM. */
4187 unsigned int
4188 tree_int_map_hash (const void *item)
4190 return htab_hash_pointer (((const struct tree_int_map *)item)->from);
4193 /* Return true if this tree int map structure is marked for garbage collection
4194 purposes. We simply return true if the from tree_int_map *P's from tree is marked, so that this
4195 structure goes away when the from tree goes away. */
4198 tree_int_map_marked_p (const void *p)
4200 tree from = ((struct tree_int_map *) p)->from;
4202 return ggc_marked_p (from);
4204 /* Lookup an init priority for FROM, and return it if we find one. */
4206 unsigned short
4207 decl_init_priority_lookup (tree from)
4209 struct tree_int_map *h, in;
4210 in.from = from;
4212 h = htab_find_with_hash (init_priority_for_decl,
4213 &in, htab_hash_pointer (from));
4214 if (h)
4215 return h->to;
4216 return 0;
4219 /* Insert a mapping FROM->TO in the init priority hashtable. */
4221 void
4222 decl_init_priority_insert (tree from, unsigned short to)
4224 struct tree_int_map *h;
4225 void **loc;
4227 h = ggc_alloc (sizeof (struct tree_int_map));
4228 h->from = from;
4229 h->to = to;
4230 loc = htab_find_slot_with_hash (init_priority_for_decl, h,
4231 htab_hash_pointer (from), INSERT);
4232 *(struct tree_int_map **) loc = h;
4235 /* Look up a restrict qualified base decl for FROM. */
4237 tree
4238 decl_restrict_base_lookup (tree from)
4240 struct tree_map *h;
4241 struct tree_map in;
4243 in.from = from;
4244 h = htab_find_with_hash (restrict_base_for_decl, &in,
4245 htab_hash_pointer (from));
4246 return h ? h->to : NULL_TREE;
4249 /* Record the restrict qualified base TO for FROM. */
4251 void
4252 decl_restrict_base_insert (tree from, tree to)
4254 struct tree_map *h;
4255 void **loc;
4257 h = ggc_alloc (sizeof (struct tree_map));
4258 h->hash = htab_hash_pointer (from);
4259 h->from = from;
4260 h->to = to;
4261 loc = htab_find_slot_with_hash (restrict_base_for_decl, h, h->hash, INSERT);
4262 *(struct tree_map **) loc = h;
4265 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
4267 static void
4268 print_debug_expr_statistics (void)
4270 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
4271 (long) htab_size (debug_expr_for_decl),
4272 (long) htab_elements (debug_expr_for_decl),
4273 htab_collisions (debug_expr_for_decl));
4276 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
4278 static void
4279 print_value_expr_statistics (void)
4281 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
4282 (long) htab_size (value_expr_for_decl),
4283 (long) htab_elements (value_expr_for_decl),
4284 htab_collisions (value_expr_for_decl));
4287 /* Print out statistics for the RESTRICT_BASE_FOR_DECL hash table, but
4288 don't print anything if the table is empty. */
4290 static void
4291 print_restrict_base_statistics (void)
4293 if (htab_elements (restrict_base_for_decl) != 0)
4294 fprintf (stderr,
4295 "RESTRICT_BASE hash: size %ld, %ld elements, %f collisions\n",
4296 (long) htab_size (restrict_base_for_decl),
4297 (long) htab_elements (restrict_base_for_decl),
4298 htab_collisions (restrict_base_for_decl));
4301 /* Lookup a debug expression for FROM, and return it if we find one. */
4303 tree
4304 decl_debug_expr_lookup (tree from)
4306 struct tree_map *h, in;
4307 in.from = from;
4309 h = htab_find_with_hash (debug_expr_for_decl, &in, htab_hash_pointer (from));
4310 if (h)
4311 return h->to;
4312 return NULL_TREE;
4315 /* Insert a mapping FROM->TO in the debug expression hashtable. */
4317 void
4318 decl_debug_expr_insert (tree from, tree to)
4320 struct tree_map *h;
4321 void **loc;
4323 h = ggc_alloc (sizeof (struct tree_map));
4324 h->hash = htab_hash_pointer (from);
4325 h->from = from;
4326 h->to = to;
4327 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, h->hash, INSERT);
4328 *(struct tree_map **) loc = h;
4331 /* Lookup a value expression for FROM, and return it if we find one. */
4333 tree
4334 decl_value_expr_lookup (tree from)
4336 struct tree_map *h, in;
4337 in.from = from;
4339 h = htab_find_with_hash (value_expr_for_decl, &in, htab_hash_pointer (from));
4340 if (h)
4341 return h->to;
4342 return NULL_TREE;
4345 /* Insert a mapping FROM->TO in the value expression hashtable. */
4347 void
4348 decl_value_expr_insert (tree from, tree to)
4350 struct tree_map *h;
4351 void **loc;
4353 h = ggc_alloc (sizeof (struct tree_map));
4354 h->hash = htab_hash_pointer (from);
4355 h->from = from;
4356 h->to = to;
4357 loc = htab_find_slot_with_hash (value_expr_for_decl, h, h->hash, INSERT);
4358 *(struct tree_map **) loc = h;
4361 /* Hashing of types so that we don't make duplicates.
4362 The entry point is `type_hash_canon'. */
4364 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
4365 with types in the TREE_VALUE slots), by adding the hash codes
4366 of the individual types. */
4368 unsigned int
4369 type_hash_list (tree list, hashval_t hashcode)
4371 tree tail;
4373 for (tail = list; tail; tail = TREE_CHAIN (tail))
4374 if (TREE_VALUE (tail) != error_mark_node)
4375 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
4376 hashcode);
4378 return hashcode;
4381 /* These are the Hashtable callback functions. */
4383 /* Returns true iff the types are equivalent. */
4385 static int
4386 type_hash_eq (const void *va, const void *vb)
4388 const struct type_hash *a = va, *b = vb;
4390 /* First test the things that are the same for all types. */
4391 if (a->hash != b->hash
4392 || TREE_CODE (a->type) != TREE_CODE (b->type)
4393 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
4394 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
4395 TYPE_ATTRIBUTES (b->type))
4396 || TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
4397 || TYPE_MODE (a->type) != TYPE_MODE (b->type))
4398 return 0;
4400 switch (TREE_CODE (a->type))
4402 case VOID_TYPE:
4403 case COMPLEX_TYPE:
4404 case POINTER_TYPE:
4405 case REFERENCE_TYPE:
4406 return 1;
4408 case VECTOR_TYPE:
4409 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
4411 case ENUMERAL_TYPE:
4412 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
4413 && !(TYPE_VALUES (a->type)
4414 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
4415 && TYPE_VALUES (b->type)
4416 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
4417 && type_list_equal (TYPE_VALUES (a->type),
4418 TYPE_VALUES (b->type))))
4419 return 0;
4421 /* ... fall through ... */
4423 case INTEGER_TYPE:
4424 case REAL_TYPE:
4425 case BOOLEAN_TYPE:
4426 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
4427 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
4428 TYPE_MAX_VALUE (b->type)))
4429 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
4430 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
4431 TYPE_MIN_VALUE (b->type))));
4433 case OFFSET_TYPE:
4434 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
4436 case METHOD_TYPE:
4437 return (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
4438 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
4439 || (TYPE_ARG_TYPES (a->type)
4440 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
4441 && TYPE_ARG_TYPES (b->type)
4442 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
4443 && type_list_equal (TYPE_ARG_TYPES (a->type),
4444 TYPE_ARG_TYPES (b->type)))));
4446 case ARRAY_TYPE:
4447 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
4449 case RECORD_TYPE:
4450 case UNION_TYPE:
4451 case QUAL_UNION_TYPE:
4452 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
4453 || (TYPE_FIELDS (a->type)
4454 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
4455 && TYPE_FIELDS (b->type)
4456 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
4457 && type_list_equal (TYPE_FIELDS (a->type),
4458 TYPE_FIELDS (b->type))));
4460 case FUNCTION_TYPE:
4461 return (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
4462 || (TYPE_ARG_TYPES (a->type)
4463 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
4464 && TYPE_ARG_TYPES (b->type)
4465 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
4466 && type_list_equal (TYPE_ARG_TYPES (a->type),
4467 TYPE_ARG_TYPES (b->type))));
4469 default:
4470 return 0;
4474 /* Return the cached hash value. */
4476 static hashval_t
4477 type_hash_hash (const void *item)
4479 return ((const struct type_hash *) item)->hash;
4482 /* Look in the type hash table for a type isomorphic to TYPE.
4483 If one is found, return it. Otherwise return 0. */
4485 tree
4486 type_hash_lookup (hashval_t hashcode, tree type)
4488 struct type_hash *h, in;
4490 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
4491 must call that routine before comparing TYPE_ALIGNs. */
4492 layout_type (type);
4494 in.hash = hashcode;
4495 in.type = type;
4497 h = htab_find_with_hash (type_hash_table, &in, hashcode);
4498 if (h)
4499 return h->type;
4500 return NULL_TREE;
4503 /* Add an entry to the type-hash-table
4504 for a type TYPE whose hash code is HASHCODE. */
4506 void
4507 type_hash_add (hashval_t hashcode, tree type)
4509 struct type_hash *h;
4510 void **loc;
4512 h = ggc_alloc (sizeof (struct type_hash));
4513 h->hash = hashcode;
4514 h->type = type;
4515 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
4516 *(struct type_hash **) loc = h;
4519 /* Given TYPE, and HASHCODE its hash code, return the canonical
4520 object for an identical type if one already exists.
4521 Otherwise, return TYPE, and record it as the canonical object.
4523 To use this function, first create a type of the sort you want.
4524 Then compute its hash code from the fields of the type that
4525 make it different from other similar types.
4526 Then call this function and use the value. */
4528 tree
4529 type_hash_canon (unsigned int hashcode, tree type)
4531 tree t1;
4533 /* The hash table only contains main variants, so ensure that's what we're
4534 being passed. */
4535 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
4537 if (!lang_hooks.types.hash_types)
4538 return type;
4540 /* See if the type is in the hash table already. If so, return it.
4541 Otherwise, add the type. */
4542 t1 = type_hash_lookup (hashcode, type);
4543 if (t1 != 0)
4545 #ifdef GATHER_STATISTICS
4546 tree_node_counts[(int) t_kind]--;
4547 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
4548 #endif
4549 return t1;
4551 else
4553 type_hash_add (hashcode, type);
4554 return type;
4558 /* See if the data pointed to by the type hash table is marked. We consider
4559 it marked if the type is marked or if a debug type number or symbol
4560 table entry has been made for the type. This reduces the amount of
4561 debugging output and eliminates that dependency of the debug output on
4562 the number of garbage collections. */
4564 static int
4565 type_hash_marked_p (const void *p)
4567 tree type = ((struct type_hash *) p)->type;
4569 return ggc_marked_p (type) || TYPE_SYMTAB_POINTER (type);
4572 static void
4573 print_type_hash_statistics (void)
4575 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
4576 (long) htab_size (type_hash_table),
4577 (long) htab_elements (type_hash_table),
4578 htab_collisions (type_hash_table));
4581 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
4582 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
4583 by adding the hash codes of the individual attributes. */
4585 unsigned int
4586 attribute_hash_list (tree list, hashval_t hashcode)
4588 tree tail;
4590 for (tail = list; tail; tail = TREE_CHAIN (tail))
4591 /* ??? Do we want to add in TREE_VALUE too? */
4592 hashcode = iterative_hash_object
4593 (IDENTIFIER_HASH_VALUE (TREE_PURPOSE (tail)), hashcode);
4594 return hashcode;
4597 /* Given two lists of attributes, return true if list l2 is
4598 equivalent to l1. */
4601 attribute_list_equal (tree l1, tree l2)
4603 return attribute_list_contained (l1, l2)
4604 && attribute_list_contained (l2, l1);
4607 /* Given two lists of attributes, return true if list L2 is
4608 completely contained within L1. */
4609 /* ??? This would be faster if attribute names were stored in a canonicalized
4610 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
4611 must be used to show these elements are equivalent (which they are). */
4612 /* ??? It's not clear that attributes with arguments will always be handled
4613 correctly. */
4616 attribute_list_contained (tree l1, tree l2)
4618 tree t1, t2;
4620 /* First check the obvious, maybe the lists are identical. */
4621 if (l1 == l2)
4622 return 1;
4624 /* Maybe the lists are similar. */
4625 for (t1 = l1, t2 = l2;
4626 t1 != 0 && t2 != 0
4627 && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
4628 && TREE_VALUE (t1) == TREE_VALUE (t2);
4629 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
4631 /* Maybe the lists are equal. */
4632 if (t1 == 0 && t2 == 0)
4633 return 1;
4635 for (; t2 != 0; t2 = TREE_CHAIN (t2))
4637 tree attr;
4638 for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)), l1);
4639 attr != NULL_TREE;
4640 attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
4641 TREE_CHAIN (attr)))
4643 if (TREE_VALUE (t2) != NULL
4644 && TREE_CODE (TREE_VALUE (t2)) == TREE_LIST
4645 && TREE_VALUE (attr) != NULL
4646 && TREE_CODE (TREE_VALUE (attr)) == TREE_LIST)
4648 if (simple_cst_list_equal (TREE_VALUE (t2),
4649 TREE_VALUE (attr)) == 1)
4650 break;
4652 else if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) == 1)
4653 break;
4656 if (attr == 0)
4657 return 0;
4660 return 1;
4663 /* Given two lists of types
4664 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
4665 return 1 if the lists contain the same types in the same order.
4666 Also, the TREE_PURPOSEs must match. */
4669 type_list_equal (tree l1, tree l2)
4671 tree t1, t2;
4673 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
4674 if (TREE_VALUE (t1) != TREE_VALUE (t2)
4675 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
4676 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
4677 && (TREE_TYPE (TREE_PURPOSE (t1))
4678 == TREE_TYPE (TREE_PURPOSE (t2))))))
4679 return 0;
4681 return t1 == t2;
4684 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
4685 given by TYPE. If the argument list accepts variable arguments,
4686 then this function counts only the ordinary arguments. */
4689 type_num_arguments (tree type)
4691 int i = 0;
4692 tree t;
4694 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
4695 /* If the function does not take a variable number of arguments,
4696 the last element in the list will have type `void'. */
4697 if (VOID_TYPE_P (TREE_VALUE (t)))
4698 break;
4699 else
4700 ++i;
4702 return i;
4705 /* Nonzero if integer constants T1 and T2
4706 represent the same constant value. */
4709 tree_int_cst_equal (tree t1, tree t2)
4711 if (t1 == t2)
4712 return 1;
4714 if (t1 == 0 || t2 == 0)
4715 return 0;
4717 if (TREE_CODE (t1) == INTEGER_CST
4718 && TREE_CODE (t2) == INTEGER_CST
4719 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
4720 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
4721 return 1;
4723 return 0;
4726 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
4727 The precise way of comparison depends on their data type. */
4730 tree_int_cst_lt (tree t1, tree t2)
4732 if (t1 == t2)
4733 return 0;
4735 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
4737 int t1_sgn = tree_int_cst_sgn (t1);
4738 int t2_sgn = tree_int_cst_sgn (t2);
4740 if (t1_sgn < t2_sgn)
4741 return 1;
4742 else if (t1_sgn > t2_sgn)
4743 return 0;
4744 /* Otherwise, both are non-negative, so we compare them as
4745 unsigned just in case one of them would overflow a signed
4746 type. */
4748 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
4749 return INT_CST_LT (t1, t2);
4751 return INT_CST_LT_UNSIGNED (t1, t2);
4754 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
4757 tree_int_cst_compare (tree t1, tree t2)
4759 if (tree_int_cst_lt (t1, t2))
4760 return -1;
4761 else if (tree_int_cst_lt (t2, t1))
4762 return 1;
4763 else
4764 return 0;
4767 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
4768 the host. If POS is zero, the value can be represented in a single
4769 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
4770 be represented in a single unsigned HOST_WIDE_INT. */
4773 host_integerp (tree t, int pos)
4775 return (TREE_CODE (t) == INTEGER_CST
4776 && ((TREE_INT_CST_HIGH (t) == 0
4777 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
4778 || (! pos && TREE_INT_CST_HIGH (t) == -1
4779 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
4780 && !TYPE_UNSIGNED (TREE_TYPE (t)))
4781 || (pos && TREE_INT_CST_HIGH (t) == 0)));
4784 /* Return the HOST_WIDE_INT least significant bits of T if it is an
4785 INTEGER_CST and there is no overflow. POS is nonzero if the result must
4786 be non-negative. We must be able to satisfy the above conditions. */
4788 HOST_WIDE_INT
4789 tree_low_cst (tree t, int pos)
4791 gcc_assert (host_integerp (t, pos));
4792 return TREE_INT_CST_LOW (t);
4795 /* Return the most significant bit of the integer constant T. */
4798 tree_int_cst_msb (tree t)
4800 int prec;
4801 HOST_WIDE_INT h;
4802 unsigned HOST_WIDE_INT l;
4804 /* Note that using TYPE_PRECISION here is wrong. We care about the
4805 actual bits, not the (arbitrary) range of the type. */
4806 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
4807 rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
4808 2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
4809 return (l & 1) == 1;
4812 /* Return an indication of the sign of the integer constant T.
4813 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
4814 Note that -1 will never be returned if T's type is unsigned. */
4817 tree_int_cst_sgn (tree t)
4819 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
4820 return 0;
4821 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
4822 return 1;
4823 else if (TREE_INT_CST_HIGH (t) < 0)
4824 return -1;
4825 else
4826 return 1;
4829 /* Compare two constructor-element-type constants. Return 1 if the lists
4830 are known to be equal; otherwise return 0. */
4833 simple_cst_list_equal (tree l1, tree l2)
4835 while (l1 != NULL_TREE && l2 != NULL_TREE)
4837 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
4838 return 0;
4840 l1 = TREE_CHAIN (l1);
4841 l2 = TREE_CHAIN (l2);
4844 return l1 == l2;
4847 /* Return truthvalue of whether T1 is the same tree structure as T2.
4848 Return 1 if they are the same.
4849 Return 0 if they are understandably different.
4850 Return -1 if either contains tree structure not understood by
4851 this function. */
4854 simple_cst_equal (tree t1, tree t2)
4856 enum tree_code code1, code2;
4857 int cmp;
4858 int i;
4860 if (t1 == t2)
4861 return 1;
4862 if (t1 == 0 || t2 == 0)
4863 return 0;
4865 code1 = TREE_CODE (t1);
4866 code2 = TREE_CODE (t2);
4868 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
4870 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
4871 || code2 == NON_LVALUE_EXPR)
4872 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4873 else
4874 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
4877 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
4878 || code2 == NON_LVALUE_EXPR)
4879 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
4881 if (code1 != code2)
4882 return 0;
4884 switch (code1)
4886 case INTEGER_CST:
4887 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
4888 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
4890 case REAL_CST:
4891 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
4893 case STRING_CST:
4894 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
4895 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
4896 TREE_STRING_LENGTH (t1)));
4898 case CONSTRUCTOR:
4900 unsigned HOST_WIDE_INT idx;
4901 VEC(constructor_elt, gc) *v1 = CONSTRUCTOR_ELTS (t1);
4902 VEC(constructor_elt, gc) *v2 = CONSTRUCTOR_ELTS (t2);
4904 if (VEC_length (constructor_elt, v1) != VEC_length (constructor_elt, v2))
4905 return false;
4907 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
4908 /* ??? Should we handle also fields here? */
4909 if (!simple_cst_equal (VEC_index (constructor_elt, v1, idx)->value,
4910 VEC_index (constructor_elt, v2, idx)->value))
4911 return false;
4912 return true;
4915 case SAVE_EXPR:
4916 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4918 case CALL_EXPR:
4919 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4920 if (cmp <= 0)
4921 return cmp;
4922 return
4923 simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
4925 case TARGET_EXPR:
4926 /* Special case: if either target is an unallocated VAR_DECL,
4927 it means that it's going to be unified with whatever the
4928 TARGET_EXPR is really supposed to initialize, so treat it
4929 as being equivalent to anything. */
4930 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
4931 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
4932 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
4933 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
4934 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
4935 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
4936 cmp = 1;
4937 else
4938 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4940 if (cmp <= 0)
4941 return cmp;
4943 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
4945 case WITH_CLEANUP_EXPR:
4946 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4947 if (cmp <= 0)
4948 return cmp;
4950 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
4952 case COMPONENT_REF:
4953 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
4954 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4956 return 0;
4958 case VAR_DECL:
4959 case PARM_DECL:
4960 case CONST_DECL:
4961 case FUNCTION_DECL:
4962 return 0;
4964 default:
4965 break;
4968 /* This general rule works for most tree codes. All exceptions should be
4969 handled above. If this is a language-specific tree code, we can't
4970 trust what might be in the operand, so say we don't know
4971 the situation. */
4972 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
4973 return -1;
4975 switch (TREE_CODE_CLASS (code1))
4977 case tcc_unary:
4978 case tcc_binary:
4979 case tcc_comparison:
4980 case tcc_expression:
4981 case tcc_reference:
4982 case tcc_statement:
4983 cmp = 1;
4984 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
4986 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
4987 if (cmp <= 0)
4988 return cmp;
4991 return cmp;
4993 default:
4994 return -1;
4998 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
4999 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
5000 than U, respectively. */
5003 compare_tree_int (tree t, unsigned HOST_WIDE_INT u)
5005 if (tree_int_cst_sgn (t) < 0)
5006 return -1;
5007 else if (TREE_INT_CST_HIGH (t) != 0)
5008 return 1;
5009 else if (TREE_INT_CST_LOW (t) == u)
5010 return 0;
5011 else if (TREE_INT_CST_LOW (t) < u)
5012 return -1;
5013 else
5014 return 1;
5017 /* Return true if CODE represents an associative tree code. Otherwise
5018 return false. */
5019 bool
5020 associative_tree_code (enum tree_code code)
5022 switch (code)
5024 case BIT_IOR_EXPR:
5025 case BIT_AND_EXPR:
5026 case BIT_XOR_EXPR:
5027 case PLUS_EXPR:
5028 case MULT_EXPR:
5029 case MIN_EXPR:
5030 case MAX_EXPR:
5031 return true;
5033 default:
5034 break;
5036 return false;
5039 /* Return true if CODE represents a commutative tree code. Otherwise
5040 return false. */
5041 bool
5042 commutative_tree_code (enum tree_code code)
5044 switch (code)
5046 case PLUS_EXPR:
5047 case MULT_EXPR:
5048 case MIN_EXPR:
5049 case MAX_EXPR:
5050 case BIT_IOR_EXPR:
5051 case BIT_XOR_EXPR:
5052 case BIT_AND_EXPR:
5053 case NE_EXPR:
5054 case EQ_EXPR:
5055 case UNORDERED_EXPR:
5056 case ORDERED_EXPR:
5057 case UNEQ_EXPR:
5058 case LTGT_EXPR:
5059 case TRUTH_AND_EXPR:
5060 case TRUTH_XOR_EXPR:
5061 case TRUTH_OR_EXPR:
5062 return true;
5064 default:
5065 break;
5067 return false;
5070 /* Generate a hash value for an expression. This can be used iteratively
5071 by passing a previous result as the "val" argument.
5073 This function is intended to produce the same hash for expressions which
5074 would compare equal using operand_equal_p. */
5076 hashval_t
5077 iterative_hash_expr (tree t, hashval_t val)
5079 int i;
5080 enum tree_code code;
5081 char class;
5083 if (t == NULL_TREE)
5084 return iterative_hash_pointer (t, val);
5086 code = TREE_CODE (t);
5088 switch (code)
5090 /* Alas, constants aren't shared, so we can't rely on pointer
5091 identity. */
5092 case INTEGER_CST:
5093 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
5094 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
5095 case REAL_CST:
5097 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
5099 return iterative_hash_hashval_t (val2, val);
5101 case STRING_CST:
5102 return iterative_hash (TREE_STRING_POINTER (t),
5103 TREE_STRING_LENGTH (t), val);
5104 case COMPLEX_CST:
5105 val = iterative_hash_expr (TREE_REALPART (t), val);
5106 return iterative_hash_expr (TREE_IMAGPART (t), val);
5107 case VECTOR_CST:
5108 return iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
5110 case SSA_NAME:
5111 case VALUE_HANDLE:
5112 /* we can just compare by pointer. */
5113 return iterative_hash_pointer (t, val);
5115 case TREE_LIST:
5116 /* A list of expressions, for a CALL_EXPR or as the elements of a
5117 VECTOR_CST. */
5118 for (; t; t = TREE_CHAIN (t))
5119 val = iterative_hash_expr (TREE_VALUE (t), val);
5120 return val;
5121 case CONSTRUCTOR:
5123 unsigned HOST_WIDE_INT idx;
5124 tree field, value;
5125 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
5127 val = iterative_hash_expr (field, val);
5128 val = iterative_hash_expr (value, val);
5130 return val;
5132 case FUNCTION_DECL:
5133 /* When referring to a built-in FUNCTION_DECL, use the
5134 __builtin__ form. Otherwise nodes that compare equal
5135 according to operand_equal_p might get different
5136 hash codes. */
5137 if (DECL_BUILT_IN (t))
5139 val = iterative_hash_pointer (built_in_decls[DECL_FUNCTION_CODE (t)],
5140 val);
5141 return val;
5143 /* else FALL THROUGH */
5144 default:
5145 class = TREE_CODE_CLASS (code);
5147 if (class == tcc_declaration)
5149 /* DECL's have a unique ID */
5150 val = iterative_hash_host_wide_int (DECL_UID (t), val);
5152 else
5154 gcc_assert (IS_EXPR_CODE_CLASS (class));
5156 val = iterative_hash_object (code, val);
5158 /* Don't hash the type, that can lead to having nodes which
5159 compare equal according to operand_equal_p, but which
5160 have different hash codes. */
5161 if (code == NOP_EXPR
5162 || code == CONVERT_EXPR
5163 || code == NON_LVALUE_EXPR)
5165 /* Make sure to include signness in the hash computation. */
5166 val += TYPE_UNSIGNED (TREE_TYPE (t));
5167 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
5170 else if (commutative_tree_code (code))
5172 /* It's a commutative expression. We want to hash it the same
5173 however it appears. We do this by first hashing both operands
5174 and then rehashing based on the order of their independent
5175 hashes. */
5176 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
5177 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
5178 hashval_t t;
5180 if (one > two)
5181 t = one, one = two, two = t;
5183 val = iterative_hash_hashval_t (one, val);
5184 val = iterative_hash_hashval_t (two, val);
5186 else
5187 for (i = TREE_CODE_LENGTH (code) - 1; i >= 0; --i)
5188 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
5190 return val;
5191 break;
5195 /* Constructors for pointer, array and function types.
5196 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
5197 constructed by language-dependent code, not here.) */
5199 /* Construct, lay out and return the type of pointers to TO_TYPE with
5200 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
5201 reference all of memory. If such a type has already been
5202 constructed, reuse it. */
5204 tree
5205 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
5206 bool can_alias_all)
5208 tree t;
5210 if (to_type == error_mark_node)
5211 return error_mark_node;
5213 /* In some cases, languages will have things that aren't a POINTER_TYPE
5214 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
5215 In that case, return that type without regard to the rest of our
5216 operands.
5218 ??? This is a kludge, but consistent with the way this function has
5219 always operated and there doesn't seem to be a good way to avoid this
5220 at the moment. */
5221 if (TYPE_POINTER_TO (to_type) != 0
5222 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
5223 return TYPE_POINTER_TO (to_type);
5225 /* First, if we already have a type for pointers to TO_TYPE and it's
5226 the proper mode, use it. */
5227 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
5228 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
5229 return t;
5231 t = make_node (POINTER_TYPE);
5233 TREE_TYPE (t) = to_type;
5234 TYPE_MODE (t) = mode;
5235 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
5236 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
5237 TYPE_POINTER_TO (to_type) = t;
5239 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
5240 SET_TYPE_STRUCTURAL_EQUALITY (t);
5241 else if (TYPE_CANONICAL (to_type) != to_type)
5242 TYPE_CANONICAL (t)
5243 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
5244 mode, can_alias_all);
5246 /* Lay out the type. This function has many callers that are concerned
5247 with expression-construction, and this simplifies them all. */
5248 layout_type (t);
5250 return t;
5253 /* By default build pointers in ptr_mode. */
5255 tree
5256 build_pointer_type (tree to_type)
5258 return build_pointer_type_for_mode (to_type, ptr_mode, false);
5261 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
5263 tree
5264 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
5265 bool can_alias_all)
5267 tree t;
5269 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
5270 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
5271 In that case, return that type without regard to the rest of our
5272 operands.
5274 ??? This is a kludge, but consistent with the way this function has
5275 always operated and there doesn't seem to be a good way to avoid this
5276 at the moment. */
5277 if (TYPE_REFERENCE_TO (to_type) != 0
5278 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
5279 return TYPE_REFERENCE_TO (to_type);
5281 /* First, if we already have a type for pointers to TO_TYPE and it's
5282 the proper mode, use it. */
5283 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
5284 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
5285 return t;
5287 t = make_node (REFERENCE_TYPE);
5289 TREE_TYPE (t) = to_type;
5290 TYPE_MODE (t) = mode;
5291 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
5292 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
5293 TYPE_REFERENCE_TO (to_type) = t;
5295 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
5296 SET_TYPE_STRUCTURAL_EQUALITY (t);
5297 else if (TYPE_CANONICAL (to_type) != to_type)
5298 TYPE_CANONICAL (t)
5299 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
5300 mode, can_alias_all);
5302 layout_type (t);
5304 return t;
5308 /* Build the node for the type of references-to-TO_TYPE by default
5309 in ptr_mode. */
5311 tree
5312 build_reference_type (tree to_type)
5314 return build_reference_type_for_mode (to_type, ptr_mode, false);
5317 /* Build a type that is compatible with t but has no cv quals anywhere
5318 in its type, thus
5320 const char *const *const * -> char ***. */
5322 tree
5323 build_type_no_quals (tree t)
5325 switch (TREE_CODE (t))
5327 case POINTER_TYPE:
5328 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
5329 TYPE_MODE (t),
5330 TYPE_REF_CAN_ALIAS_ALL (t));
5331 case REFERENCE_TYPE:
5332 return
5333 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
5334 TYPE_MODE (t),
5335 TYPE_REF_CAN_ALIAS_ALL (t));
5336 default:
5337 return TYPE_MAIN_VARIANT (t);
5341 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
5342 MAXVAL should be the maximum value in the domain
5343 (one less than the length of the array).
5345 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
5346 We don't enforce this limit, that is up to caller (e.g. language front end).
5347 The limit exists because the result is a signed type and we don't handle
5348 sizes that use more than one HOST_WIDE_INT. */
5350 tree
5351 build_index_type (tree maxval)
5353 tree itype = make_node (INTEGER_TYPE);
5355 TREE_TYPE (itype) = sizetype;
5356 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
5357 TYPE_MIN_VALUE (itype) = size_zero_node;
5358 TYPE_MAX_VALUE (itype) = fold_convert (sizetype, maxval);
5359 TYPE_MODE (itype) = TYPE_MODE (sizetype);
5360 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
5361 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
5362 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
5363 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
5365 if (host_integerp (maxval, 1))
5366 return type_hash_canon (tree_low_cst (maxval, 1), itype);
5367 else
5369 /* Since we cannot hash this type, we need to compare it using
5370 structural equality checks. */
5371 SET_TYPE_STRUCTURAL_EQUALITY (itype);
5372 return itype;
5376 /* Builds a signed or unsigned integer type of precision PRECISION.
5377 Used for C bitfields whose precision does not match that of
5378 built-in target types. */
5379 tree
5380 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
5381 int unsignedp)
5383 tree itype = make_node (INTEGER_TYPE);
5385 TYPE_PRECISION (itype) = precision;
5387 if (unsignedp)
5388 fixup_unsigned_type (itype);
5389 else
5390 fixup_signed_type (itype);
5392 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
5393 return type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
5395 return itype;
5398 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
5399 ENUMERAL_TYPE or BOOLEAN_TYPE), with low bound LOWVAL and
5400 high bound HIGHVAL. If TYPE is NULL, sizetype is used. */
5402 tree
5403 build_range_type (tree type, tree lowval, tree highval)
5405 tree itype = make_node (INTEGER_TYPE);
5407 TREE_TYPE (itype) = type;
5408 if (type == NULL_TREE)
5409 type = sizetype;
5411 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
5412 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
5414 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
5415 TYPE_MODE (itype) = TYPE_MODE (type);
5416 TYPE_SIZE (itype) = TYPE_SIZE (type);
5417 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
5418 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
5419 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
5421 if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
5422 return type_hash_canon (tree_low_cst (highval, 0)
5423 - tree_low_cst (lowval, 0),
5424 itype);
5425 else
5426 return itype;
5429 /* Just like build_index_type, but takes lowval and highval instead
5430 of just highval (maxval). */
5432 tree
5433 build_index_2_type (tree lowval, tree highval)
5435 return build_range_type (sizetype, lowval, highval);
5438 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
5439 and number of elements specified by the range of values of INDEX_TYPE.
5440 If such a type has already been constructed, reuse it. */
5442 tree
5443 build_array_type (tree elt_type, tree index_type)
5445 tree t;
5446 hashval_t hashcode = 0;
5448 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
5450 error ("arrays of functions are not meaningful");
5451 elt_type = integer_type_node;
5454 t = make_node (ARRAY_TYPE);
5455 TREE_TYPE (t) = elt_type;
5456 TYPE_DOMAIN (t) = index_type;
5458 if (index_type == 0)
5460 tree save = t;
5461 hashcode = iterative_hash_object (TYPE_HASH (elt_type), hashcode);
5462 t = type_hash_canon (hashcode, t);
5463 if (save == t)
5464 layout_type (t);
5466 if (TYPE_CANONICAL (t) == t)
5468 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type))
5469 SET_TYPE_STRUCTURAL_EQUALITY (t);
5470 else if (TYPE_CANONICAL (elt_type) != elt_type)
5471 TYPE_CANONICAL (t)
5472 = build_array_type (TYPE_CANONICAL (elt_type), index_type);
5475 return t;
5478 hashcode = iterative_hash_object (TYPE_HASH (elt_type), hashcode);
5479 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
5480 t = type_hash_canon (hashcode, t);
5482 if (!COMPLETE_TYPE_P (t))
5483 layout_type (t);
5485 if (TYPE_CANONICAL (t) == t)
5487 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
5488 || TYPE_STRUCTURAL_EQUALITY_P (index_type))
5489 SET_TYPE_STRUCTURAL_EQUALITY (t);
5490 else if (TYPE_CANONICAL (elt_type) != elt_type
5491 || TYPE_CANONICAL (index_type) != index_type)
5492 TYPE_CANONICAL (t)
5493 = build_array_type (TYPE_CANONICAL (elt_type),
5494 TYPE_CANONICAL (index_type));
5497 return t;
5500 /* Return the TYPE of the elements comprising
5501 the innermost dimension of ARRAY. */
5503 tree
5504 get_inner_array_type (tree array)
5506 tree type = TREE_TYPE (array);
5508 while (TREE_CODE (type) == ARRAY_TYPE)
5509 type = TREE_TYPE (type);
5511 return type;
5514 /* Construct, lay out and return
5515 the type of functions returning type VALUE_TYPE
5516 given arguments of types ARG_TYPES.
5517 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
5518 are data type nodes for the arguments of the function.
5519 If such a type has already been constructed, reuse it. */
5521 tree
5522 build_function_type (tree value_type, tree arg_types)
5524 tree t;
5525 hashval_t hashcode = 0;
5527 if (TREE_CODE (value_type) == FUNCTION_TYPE)
5529 error ("function return type cannot be function");
5530 value_type = integer_type_node;
5533 /* Make a node of the sort we want. */
5534 t = make_node (FUNCTION_TYPE);
5535 TREE_TYPE (t) = value_type;
5536 TYPE_ARG_TYPES (t) = arg_types;
5538 /* We don't have canonicalization of function types, yet. */
5539 SET_TYPE_STRUCTURAL_EQUALITY (t);
5541 /* If we already have such a type, use the old one. */
5542 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
5543 hashcode = type_hash_list (arg_types, hashcode);
5544 t = type_hash_canon (hashcode, t);
5546 if (!COMPLETE_TYPE_P (t))
5547 layout_type (t);
5548 return t;
5551 /* Build a function type. The RETURN_TYPE is the type returned by the
5552 function. If additional arguments are provided, they are
5553 additional argument types. The list of argument types must always
5554 be terminated by NULL_TREE. */
5556 tree
5557 build_function_type_list (tree return_type, ...)
5559 tree t, args, last;
5560 va_list p;
5562 va_start (p, return_type);
5564 t = va_arg (p, tree);
5565 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (p, tree))
5566 args = tree_cons (NULL_TREE, t, args);
5568 if (args == NULL_TREE)
5569 args = void_list_node;
5570 else
5572 last = args;
5573 args = nreverse (args);
5574 TREE_CHAIN (last) = void_list_node;
5576 args = build_function_type (return_type, args);
5578 va_end (p);
5579 return args;
5582 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
5583 and ARGTYPES (a TREE_LIST) are the return type and arguments types
5584 for the method. An implicit additional parameter (of type
5585 pointer-to-BASETYPE) is added to the ARGTYPES. */
5587 tree
5588 build_method_type_directly (tree basetype,
5589 tree rettype,
5590 tree argtypes)
5592 tree t;
5593 tree ptype;
5594 int hashcode = 0;
5596 /* Make a node of the sort we want. */
5597 t = make_node (METHOD_TYPE);
5599 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
5600 TREE_TYPE (t) = rettype;
5601 ptype = build_pointer_type (basetype);
5603 /* The actual arglist for this function includes a "hidden" argument
5604 which is "this". Put it into the list of argument types. */
5605 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
5606 TYPE_ARG_TYPES (t) = argtypes;
5608 /* We don't have canonicalization of method types yet. */
5609 SET_TYPE_STRUCTURAL_EQUALITY (t);
5611 /* If we already have such a type, use the old one. */
5612 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
5613 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
5614 hashcode = type_hash_list (argtypes, hashcode);
5615 t = type_hash_canon (hashcode, t);
5617 if (!COMPLETE_TYPE_P (t))
5618 layout_type (t);
5620 return t;
5623 /* Construct, lay out and return the type of methods belonging to class
5624 BASETYPE and whose arguments and values are described by TYPE.
5625 If that type exists already, reuse it.
5626 TYPE must be a FUNCTION_TYPE node. */
5628 tree
5629 build_method_type (tree basetype, tree type)
5631 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
5633 return build_method_type_directly (basetype,
5634 TREE_TYPE (type),
5635 TYPE_ARG_TYPES (type));
5638 /* Construct, lay out and return the type of offsets to a value
5639 of type TYPE, within an object of type BASETYPE.
5640 If a suitable offset type exists already, reuse it. */
5642 tree
5643 build_offset_type (tree basetype, tree type)
5645 tree t;
5646 hashval_t hashcode = 0;
5648 /* Make a node of the sort we want. */
5649 t = make_node (OFFSET_TYPE);
5651 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
5652 TREE_TYPE (t) = type;
5654 /* If we already have such a type, use the old one. */
5655 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
5656 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
5657 t = type_hash_canon (hashcode, t);
5659 if (!COMPLETE_TYPE_P (t))
5660 layout_type (t);
5662 if (TYPE_CANONICAL (t) == t)
5664 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
5665 || TYPE_STRUCTURAL_EQUALITY_P (type))
5666 SET_TYPE_STRUCTURAL_EQUALITY (t);
5667 else if (TYPE_CANONICAL (basetype) != basetype
5668 || TYPE_CANONICAL (type) != type)
5669 TYPE_CANONICAL (t)
5670 = build_offset_type (TYPE_CANONICAL (basetype),
5671 TYPE_CANONICAL (type));
5674 return t;
5677 /* Create a complex type whose components are COMPONENT_TYPE. */
5679 tree
5680 build_complex_type (tree component_type)
5682 tree t;
5683 hashval_t hashcode;
5685 /* Make a node of the sort we want. */
5686 t = make_node (COMPLEX_TYPE);
5688 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
5690 /* If we already have such a type, use the old one. */
5691 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
5692 t = type_hash_canon (hashcode, t);
5694 if (!COMPLETE_TYPE_P (t))
5695 layout_type (t);
5697 if (TYPE_CANONICAL (t) == t)
5699 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
5700 SET_TYPE_STRUCTURAL_EQUALITY (t);
5701 else if (TYPE_CANONICAL (component_type) != component_type)
5702 TYPE_CANONICAL (t)
5703 = build_complex_type (TYPE_CANONICAL (component_type));
5706 /* If we are writing Dwarf2 output we need to create a name,
5707 since complex is a fundamental type. */
5708 if ((write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
5709 && ! TYPE_NAME (t))
5711 const char *name;
5712 if (component_type == char_type_node)
5713 name = "complex char";
5714 else if (component_type == signed_char_type_node)
5715 name = "complex signed char";
5716 else if (component_type == unsigned_char_type_node)
5717 name = "complex unsigned char";
5718 else if (component_type == short_integer_type_node)
5719 name = "complex short int";
5720 else if (component_type == short_unsigned_type_node)
5721 name = "complex short unsigned int";
5722 else if (component_type == integer_type_node)
5723 name = "complex int";
5724 else if (component_type == unsigned_type_node)
5725 name = "complex unsigned int";
5726 else if (component_type == long_integer_type_node)
5727 name = "complex long int";
5728 else if (component_type == long_unsigned_type_node)
5729 name = "complex long unsigned int";
5730 else if (component_type == long_long_integer_type_node)
5731 name = "complex long long int";
5732 else if (component_type == long_long_unsigned_type_node)
5733 name = "complex long long unsigned int";
5734 else
5735 name = 0;
5737 if (name != 0)
5738 TYPE_NAME (t) = get_identifier (name);
5741 return build_qualified_type (t, TYPE_QUALS (component_type));
5744 /* Return OP, stripped of any conversions to wider types as much as is safe.
5745 Converting the value back to OP's type makes a value equivalent to OP.
5747 If FOR_TYPE is nonzero, we return a value which, if converted to
5748 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
5750 If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
5751 narrowest type that can hold the value, even if they don't exactly fit.
5752 Otherwise, bit-field references are changed to a narrower type
5753 only if they can be fetched directly from memory in that type.
5755 OP must have integer, real or enumeral type. Pointers are not allowed!
5757 There are some cases where the obvious value we could return
5758 would regenerate to OP if converted to OP's type,
5759 but would not extend like OP to wider types.
5760 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
5761 For example, if OP is (unsigned short)(signed char)-1,
5762 we avoid returning (signed char)-1 if FOR_TYPE is int,
5763 even though extending that to an unsigned short would regenerate OP,
5764 since the result of extending (signed char)-1 to (int)
5765 is different from (int) OP. */
5767 tree
5768 get_unwidened (tree op, tree for_type)
5770 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
5771 tree type = TREE_TYPE (op);
5772 unsigned final_prec
5773 = TYPE_PRECISION (for_type != 0 ? for_type : type);
5774 int uns
5775 = (for_type != 0 && for_type != type
5776 && final_prec > TYPE_PRECISION (type)
5777 && TYPE_UNSIGNED (type));
5778 tree win = op;
5780 while (TREE_CODE (op) == NOP_EXPR
5781 || TREE_CODE (op) == CONVERT_EXPR)
5783 int bitschange;
5785 /* TYPE_PRECISION on vector types has different meaning
5786 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
5787 so avoid them here. */
5788 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
5789 break;
5791 bitschange = TYPE_PRECISION (TREE_TYPE (op))
5792 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
5794 /* Truncations are many-one so cannot be removed.
5795 Unless we are later going to truncate down even farther. */
5796 if (bitschange < 0
5797 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
5798 break;
5800 /* See what's inside this conversion. If we decide to strip it,
5801 we will set WIN. */
5802 op = TREE_OPERAND (op, 0);
5804 /* If we have not stripped any zero-extensions (uns is 0),
5805 we can strip any kind of extension.
5806 If we have previously stripped a zero-extension,
5807 only zero-extensions can safely be stripped.
5808 Any extension can be stripped if the bits it would produce
5809 are all going to be discarded later by truncating to FOR_TYPE. */
5811 if (bitschange > 0)
5813 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
5814 win = op;
5815 /* TYPE_UNSIGNED says whether this is a zero-extension.
5816 Let's avoid computing it if it does not affect WIN
5817 and if UNS will not be needed again. */
5818 if ((uns
5819 || TREE_CODE (op) == NOP_EXPR
5820 || TREE_CODE (op) == CONVERT_EXPR)
5821 && TYPE_UNSIGNED (TREE_TYPE (op)))
5823 uns = 1;
5824 win = op;
5829 if (TREE_CODE (op) == COMPONENT_REF
5830 /* Since type_for_size always gives an integer type. */
5831 && TREE_CODE (type) != REAL_TYPE
5832 /* Don't crash if field not laid out yet. */
5833 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
5834 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
5836 unsigned int innerprec
5837 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
5838 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
5839 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
5840 type = lang_hooks.types.type_for_size (innerprec, unsignedp);
5842 /* We can get this structure field in the narrowest type it fits in.
5843 If FOR_TYPE is 0, do this only for a field that matches the
5844 narrower type exactly and is aligned for it
5845 The resulting extension to its nominal type (a fullword type)
5846 must fit the same conditions as for other extensions. */
5848 if (type != 0
5849 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (op)))
5850 && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
5851 && (! uns || final_prec <= innerprec || unsignedp))
5853 win = build3 (COMPONENT_REF, type, TREE_OPERAND (op, 0),
5854 TREE_OPERAND (op, 1), NULL_TREE);
5855 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
5856 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
5860 return win;
5863 /* Return OP or a simpler expression for a narrower value
5864 which can be sign-extended or zero-extended to give back OP.
5865 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
5866 or 0 if the value should be sign-extended. */
5868 tree
5869 get_narrower (tree op, int *unsignedp_ptr)
5871 int uns = 0;
5872 int first = 1;
5873 tree win = op;
5874 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
5876 while (TREE_CODE (op) == NOP_EXPR)
5878 int bitschange
5879 = (TYPE_PRECISION (TREE_TYPE (op))
5880 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
5882 /* Truncations are many-one so cannot be removed. */
5883 if (bitschange < 0)
5884 break;
5886 /* See what's inside this conversion. If we decide to strip it,
5887 we will set WIN. */
5889 if (bitschange > 0)
5891 op = TREE_OPERAND (op, 0);
5892 /* An extension: the outermost one can be stripped,
5893 but remember whether it is zero or sign extension. */
5894 if (first)
5895 uns = TYPE_UNSIGNED (TREE_TYPE (op));
5896 /* Otherwise, if a sign extension has been stripped,
5897 only sign extensions can now be stripped;
5898 if a zero extension has been stripped, only zero-extensions. */
5899 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
5900 break;
5901 first = 0;
5903 else /* bitschange == 0 */
5905 /* A change in nominal type can always be stripped, but we must
5906 preserve the unsignedness. */
5907 if (first)
5908 uns = TYPE_UNSIGNED (TREE_TYPE (op));
5909 first = 0;
5910 op = TREE_OPERAND (op, 0);
5911 /* Keep trying to narrow, but don't assign op to win if it
5912 would turn an integral type into something else. */
5913 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
5914 continue;
5917 win = op;
5920 if (TREE_CODE (op) == COMPONENT_REF
5921 /* Since type_for_size always gives an integer type. */
5922 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
5923 /* Ensure field is laid out already. */
5924 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
5925 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
5927 unsigned HOST_WIDE_INT innerprec
5928 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
5929 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
5930 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
5931 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
5933 /* We can get this structure field in a narrower type that fits it,
5934 but the resulting extension to its nominal type (a fullword type)
5935 must satisfy the same conditions as for other extensions.
5937 Do this only for fields that are aligned (not bit-fields),
5938 because when bit-field insns will be used there is no
5939 advantage in doing this. */
5941 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
5942 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
5943 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
5944 && type != 0)
5946 if (first)
5947 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
5948 win = fold_convert (type, op);
5952 *unsignedp_ptr = uns;
5953 return win;
5956 /* Nonzero if integer constant C has a value that is permissible
5957 for type TYPE (an INTEGER_TYPE). */
5960 int_fits_type_p (tree c, tree type)
5962 tree type_low_bound = TYPE_MIN_VALUE (type);
5963 tree type_high_bound = TYPE_MAX_VALUE (type);
5964 bool ok_for_low_bound, ok_for_high_bound;
5965 unsigned HOST_WIDE_INT low;
5966 HOST_WIDE_INT high;
5968 /* If at least one bound of the type is a constant integer, we can check
5969 ourselves and maybe make a decision. If no such decision is possible, but
5970 this type is a subtype, try checking against that. Otherwise, use
5971 fit_double_type, which checks against the precision.
5973 Compute the status for each possibly constant bound, and return if we see
5974 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
5975 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
5976 for "constant known to fit". */
5978 /* Check if C >= type_low_bound. */
5979 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
5981 if (tree_int_cst_lt (c, type_low_bound))
5982 return 0;
5983 ok_for_low_bound = true;
5985 else
5986 ok_for_low_bound = false;
5988 /* Check if c <= type_high_bound. */
5989 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
5991 if (tree_int_cst_lt (type_high_bound, c))
5992 return 0;
5993 ok_for_high_bound = true;
5995 else
5996 ok_for_high_bound = false;
5998 /* If the constant fits both bounds, the result is known. */
5999 if (ok_for_low_bound && ok_for_high_bound)
6000 return 1;
6002 /* Perform some generic filtering which may allow making a decision
6003 even if the bounds are not constant. First, negative integers
6004 never fit in unsigned types, */
6005 if (TYPE_UNSIGNED (type) && tree_int_cst_sgn (c) < 0)
6006 return 0;
6008 /* Second, narrower types always fit in wider ones. */
6009 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
6010 return 1;
6012 /* Third, unsigned integers with top bit set never fit signed types. */
6013 if (! TYPE_UNSIGNED (type)
6014 && TYPE_UNSIGNED (TREE_TYPE (c))
6015 && tree_int_cst_msb (c))
6016 return 0;
6018 /* If we haven't been able to decide at this point, there nothing more we
6019 can check ourselves here. Look at the base type if we have one and it
6020 has the same precision. */
6021 if (TREE_CODE (type) == INTEGER_TYPE
6022 && TREE_TYPE (type) != 0
6023 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
6024 return int_fits_type_p (c, TREE_TYPE (type));
6026 /* Or to fit_double_type, if nothing else. */
6027 low = TREE_INT_CST_LOW (c);
6028 high = TREE_INT_CST_HIGH (c);
6029 return !fit_double_type (low, high, &low, &high, type);
6032 /* Subprogram of following function. Called by walk_tree.
6034 Return *TP if it is an automatic variable or parameter of the
6035 function passed in as DATA. */
6037 static tree
6038 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
6040 tree fn = (tree) data;
6042 if (TYPE_P (*tp))
6043 *walk_subtrees = 0;
6045 else if (DECL_P (*tp)
6046 && lang_hooks.tree_inlining.auto_var_in_fn_p (*tp, fn))
6047 return *tp;
6049 return NULL_TREE;
6052 /* Returns true if T is, contains, or refers to a type with variable
6053 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
6054 arguments, but not the return type. If FN is nonzero, only return
6055 true if a modifier of the type or position of FN is a variable or
6056 parameter inside FN.
6058 This concept is more general than that of C99 'variably modified types':
6059 in C99, a struct type is never variably modified because a VLA may not
6060 appear as a structure member. However, in GNU C code like:
6062 struct S { int i[f()]; };
6064 is valid, and other languages may define similar constructs. */
6066 bool
6067 variably_modified_type_p (tree type, tree fn)
6069 tree t;
6071 /* Test if T is either variable (if FN is zero) or an expression containing
6072 a variable in FN. */
6073 #define RETURN_TRUE_IF_VAR(T) \
6074 do { tree _t = (T); \
6075 if (_t && _t != error_mark_node && TREE_CODE (_t) != INTEGER_CST \
6076 && (!fn || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
6077 return true; } while (0)
6079 if (type == error_mark_node)
6080 return false;
6082 /* If TYPE itself has variable size, it is variably modified. */
6083 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
6084 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
6086 switch (TREE_CODE (type))
6088 case POINTER_TYPE:
6089 case REFERENCE_TYPE:
6090 case VECTOR_TYPE:
6091 if (variably_modified_type_p (TREE_TYPE (type), fn))
6092 return true;
6093 break;
6095 case FUNCTION_TYPE:
6096 case METHOD_TYPE:
6097 /* If TYPE is a function type, it is variably modified if the
6098 return type is variably modified. */
6099 if (variably_modified_type_p (TREE_TYPE (type), fn))
6100 return true;
6101 break;
6103 case INTEGER_TYPE:
6104 case REAL_TYPE:
6105 case ENUMERAL_TYPE:
6106 case BOOLEAN_TYPE:
6107 /* Scalar types are variably modified if their end points
6108 aren't constant. */
6109 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
6110 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
6111 break;
6113 case RECORD_TYPE:
6114 case UNION_TYPE:
6115 case QUAL_UNION_TYPE:
6116 /* We can't see if any of the fields are variably-modified by the
6117 definition we normally use, since that would produce infinite
6118 recursion via pointers. */
6119 /* This is variably modified if some field's type is. */
6120 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
6121 if (TREE_CODE (t) == FIELD_DECL)
6123 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
6124 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
6125 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
6127 if (TREE_CODE (type) == QUAL_UNION_TYPE)
6128 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
6130 break;
6132 case ARRAY_TYPE:
6133 /* Do not call ourselves to avoid infinite recursion. This is
6134 variably modified if the element type is. */
6135 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
6136 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
6137 break;
6139 default:
6140 break;
6143 /* The current language may have other cases to check, but in general,
6144 all other types are not variably modified. */
6145 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
6147 #undef RETURN_TRUE_IF_VAR
6150 /* Given a DECL or TYPE, return the scope in which it was declared, or
6151 NULL_TREE if there is no containing scope. */
6153 tree
6154 get_containing_scope (tree t)
6156 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
6159 /* Return the innermost context enclosing DECL that is
6160 a FUNCTION_DECL, or zero if none. */
6162 tree
6163 decl_function_context (tree decl)
6165 tree context;
6167 if (TREE_CODE (decl) == ERROR_MARK)
6168 return 0;
6170 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
6171 where we look up the function at runtime. Such functions always take
6172 a first argument of type 'pointer to real context'.
6174 C++ should really be fixed to use DECL_CONTEXT for the real context,
6175 and use something else for the "virtual context". */
6176 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
6177 context
6178 = TYPE_MAIN_VARIANT
6179 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6180 else
6181 context = DECL_CONTEXT (decl);
6183 while (context && TREE_CODE (context) != FUNCTION_DECL)
6185 if (TREE_CODE (context) == BLOCK)
6186 context = BLOCK_SUPERCONTEXT (context);
6187 else
6188 context = get_containing_scope (context);
6191 return context;
6194 /* Return the innermost context enclosing DECL that is
6195 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
6196 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
6198 tree
6199 decl_type_context (tree decl)
6201 tree context = DECL_CONTEXT (decl);
6203 while (context)
6204 switch (TREE_CODE (context))
6206 case NAMESPACE_DECL:
6207 case TRANSLATION_UNIT_DECL:
6208 return NULL_TREE;
6210 case RECORD_TYPE:
6211 case UNION_TYPE:
6212 case QUAL_UNION_TYPE:
6213 return context;
6215 case TYPE_DECL:
6216 case FUNCTION_DECL:
6217 context = DECL_CONTEXT (context);
6218 break;
6220 case BLOCK:
6221 context = BLOCK_SUPERCONTEXT (context);
6222 break;
6224 default:
6225 gcc_unreachable ();
6228 return NULL_TREE;
6231 /* CALL is a CALL_EXPR. Return the declaration for the function
6232 called, or NULL_TREE if the called function cannot be
6233 determined. */
6235 tree
6236 get_callee_fndecl (tree call)
6238 tree addr;
6240 if (call == error_mark_node)
6241 return call;
6243 /* It's invalid to call this function with anything but a
6244 CALL_EXPR. */
6245 gcc_assert (TREE_CODE (call) == CALL_EXPR);
6247 /* The first operand to the CALL is the address of the function
6248 called. */
6249 addr = TREE_OPERAND (call, 0);
6251 STRIP_NOPS (addr);
6253 /* If this is a readonly function pointer, extract its initial value. */
6254 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
6255 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
6256 && DECL_INITIAL (addr))
6257 addr = DECL_INITIAL (addr);
6259 /* If the address is just `&f' for some function `f', then we know
6260 that `f' is being called. */
6261 if (TREE_CODE (addr) == ADDR_EXPR
6262 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
6263 return TREE_OPERAND (addr, 0);
6265 /* We couldn't figure out what was being called. Maybe the front
6266 end has some idea. */
6267 return lang_hooks.lang_get_callee_fndecl (call);
6270 /* Print debugging information about tree nodes generated during the compile,
6271 and any language-specific information. */
6273 void
6274 dump_tree_statistics (void)
6276 #ifdef GATHER_STATISTICS
6277 int i;
6278 int total_nodes, total_bytes;
6279 #endif
6281 fprintf (stderr, "\n??? tree nodes created\n\n");
6282 #ifdef GATHER_STATISTICS
6283 fprintf (stderr, "Kind Nodes Bytes\n");
6284 fprintf (stderr, "---------------------------------------\n");
6285 total_nodes = total_bytes = 0;
6286 for (i = 0; i < (int) all_kinds; i++)
6288 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
6289 tree_node_counts[i], tree_node_sizes[i]);
6290 total_nodes += tree_node_counts[i];
6291 total_bytes += tree_node_sizes[i];
6293 fprintf (stderr, "---------------------------------------\n");
6294 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
6295 fprintf (stderr, "---------------------------------------\n");
6296 ssanames_print_statistics ();
6297 phinodes_print_statistics ();
6298 #else
6299 fprintf (stderr, "(No per-node statistics)\n");
6300 #endif
6301 print_type_hash_statistics ();
6302 print_debug_expr_statistics ();
6303 print_value_expr_statistics ();
6304 print_restrict_base_statistics ();
6305 lang_hooks.print_statistics ();
6308 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
6310 /* Generate a crc32 of a string. */
6312 unsigned
6313 crc32_string (unsigned chksum, const char *string)
6317 unsigned value = *string << 24;
6318 unsigned ix;
6320 for (ix = 8; ix--; value <<= 1)
6322 unsigned feedback;
6324 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
6325 chksum <<= 1;
6326 chksum ^= feedback;
6329 while (*string++);
6330 return chksum;
6333 /* P is a string that will be used in a symbol. Mask out any characters
6334 that are not valid in that context. */
6336 void
6337 clean_symbol_name (char *p)
6339 for (; *p; p++)
6340 if (! (ISALNUM (*p)
6341 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
6342 || *p == '$'
6343 #endif
6344 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
6345 || *p == '.'
6346 #endif
6348 *p = '_';
6351 /* Generate a name for a special-purpose function function.
6352 The generated name may need to be unique across the whole link.
6353 TYPE is some string to identify the purpose of this function to the
6354 linker or collect2; it must start with an uppercase letter,
6355 one of:
6356 I - for constructors
6357 D - for destructors
6358 N - for C++ anonymous namespaces
6359 F - for DWARF unwind frame information. */
6361 tree
6362 get_file_function_name (const char *type)
6364 char *buf;
6365 const char *p;
6366 char *q;
6368 /* If we already have a name we know to be unique, just use that. */
6369 if (first_global_object_name)
6370 p = first_global_object_name;
6371 /* If the target is handling the constructors/destructors, they
6372 will be local to this file and the name is only necessary for
6373 debugging purposes. */
6374 else if ((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
6376 const char *file = main_input_filename;
6377 if (! file)
6378 file = input_filename;
6379 /* Just use the file's basename, because the full pathname
6380 might be quite long. */
6381 p = strrchr (file, '/');
6382 if (p)
6383 p++;
6384 else
6385 p = file;
6386 p = q = ASTRDUP (p);
6387 clean_symbol_name (q);
6389 else
6391 /* Otherwise, the name must be unique across the entire link.
6392 We don't have anything that we know to be unique to this translation
6393 unit, so use what we do have and throw in some randomness. */
6394 unsigned len;
6395 const char *name = weak_global_object_name;
6396 const char *file = main_input_filename;
6398 if (! name)
6399 name = "";
6400 if (! file)
6401 file = input_filename;
6403 len = strlen (file);
6404 q = alloca (9 * 2 + len + 1);
6405 memcpy (q, file, len + 1);
6406 clean_symbol_name (q);
6408 sprintf (q + len, "_%08X_%08X", crc32_string (0, name),
6409 crc32_string (0, flag_random_seed));
6411 p = q;
6414 buf = alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p) + strlen (type));
6416 /* Set up the name of the file-level functions we may need.
6417 Use a global object (which is already required to be unique over
6418 the program) rather than the file name (which imposes extra
6419 constraints). */
6420 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
6422 return get_identifier (buf);
6425 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
6427 /* Complain that the tree code of NODE does not match the expected 0
6428 terminated list of trailing codes. The trailing code list can be
6429 empty, for a more vague error message. FILE, LINE, and FUNCTION
6430 are of the caller. */
6432 void
6433 tree_check_failed (const tree node, const char *file,
6434 int line, const char *function, ...)
6436 va_list args;
6437 char *buffer;
6438 unsigned length = 0;
6439 int code;
6441 va_start (args, function);
6442 while ((code = va_arg (args, int)))
6443 length += 4 + strlen (tree_code_name[code]);
6444 va_end (args);
6445 if (length)
6447 va_start (args, function);
6448 length += strlen ("expected ");
6449 buffer = alloca (length);
6450 length = 0;
6451 while ((code = va_arg (args, int)))
6453 const char *prefix = length ? " or " : "expected ";
6455 strcpy (buffer + length, prefix);
6456 length += strlen (prefix);
6457 strcpy (buffer + length, tree_code_name[code]);
6458 length += strlen (tree_code_name[code]);
6460 va_end (args);
6462 else
6463 buffer = (char *)"unexpected node";
6465 internal_error ("tree check: %s, have %s in %s, at %s:%d",
6466 buffer, tree_code_name[TREE_CODE (node)],
6467 function, trim_filename (file), line);
6470 /* Complain that the tree code of NODE does match the expected 0
6471 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
6472 the caller. */
6474 void
6475 tree_not_check_failed (const tree node, const char *file,
6476 int line, const char *function, ...)
6478 va_list args;
6479 char *buffer;
6480 unsigned length = 0;
6481 int code;
6483 va_start (args, function);
6484 while ((code = va_arg (args, int)))
6485 length += 4 + strlen (tree_code_name[code]);
6486 va_end (args);
6487 va_start (args, function);
6488 buffer = alloca (length);
6489 length = 0;
6490 while ((code = va_arg (args, int)))
6492 if (length)
6494 strcpy (buffer + length, " or ");
6495 length += 4;
6497 strcpy (buffer + length, tree_code_name[code]);
6498 length += strlen (tree_code_name[code]);
6500 va_end (args);
6502 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
6503 buffer, tree_code_name[TREE_CODE (node)],
6504 function, trim_filename (file), line);
6507 /* Similar to tree_check_failed, except that we check for a class of tree
6508 code, given in CL. */
6510 void
6511 tree_class_check_failed (const tree node, const enum tree_code_class cl,
6512 const char *file, int line, const char *function)
6514 internal_error
6515 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
6516 TREE_CODE_CLASS_STRING (cl),
6517 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
6518 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
6521 /* Similar to tree_check_failed, except that instead of specifying a
6522 dozen codes, use the knowledge that they're all sequential. */
6524 void
6525 tree_range_check_failed (const tree node, const char *file, int line,
6526 const char *function, enum tree_code c1,
6527 enum tree_code c2)
6529 char *buffer;
6530 unsigned length = 0;
6531 enum tree_code c;
6533 for (c = c1; c <= c2; ++c)
6534 length += 4 + strlen (tree_code_name[c]);
6536 length += strlen ("expected ");
6537 buffer = alloca (length);
6538 length = 0;
6540 for (c = c1; c <= c2; ++c)
6542 const char *prefix = length ? " or " : "expected ";
6544 strcpy (buffer + length, prefix);
6545 length += strlen (prefix);
6546 strcpy (buffer + length, tree_code_name[c]);
6547 length += strlen (tree_code_name[c]);
6550 internal_error ("tree check: %s, have %s in %s, at %s:%d",
6551 buffer, tree_code_name[TREE_CODE (node)],
6552 function, trim_filename (file), line);
6556 /* Similar to tree_check_failed, except that we check that a tree does
6557 not have the specified code, given in CL. */
6559 void
6560 tree_not_class_check_failed (const tree node, const enum tree_code_class cl,
6561 const char *file, int line, const char *function)
6563 internal_error
6564 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
6565 TREE_CODE_CLASS_STRING (cl),
6566 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
6567 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
6571 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
6573 void
6574 omp_clause_check_failed (const tree node, const char *file, int line,
6575 const char *function, enum omp_clause_code code)
6577 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
6578 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
6579 function, trim_filename (file), line);
6583 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
6585 void
6586 omp_clause_range_check_failed (const tree node, const char *file, int line,
6587 const char *function, enum omp_clause_code c1,
6588 enum omp_clause_code c2)
6590 char *buffer;
6591 unsigned length = 0;
6592 enum omp_clause_code c;
6594 for (c = c1; c <= c2; ++c)
6595 length += 4 + strlen (omp_clause_code_name[c]);
6597 length += strlen ("expected ");
6598 buffer = alloca (length);
6599 length = 0;
6601 for (c = c1; c <= c2; ++c)
6603 const char *prefix = length ? " or " : "expected ";
6605 strcpy (buffer + length, prefix);
6606 length += strlen (prefix);
6607 strcpy (buffer + length, omp_clause_code_name[c]);
6608 length += strlen (omp_clause_code_name[c]);
6611 internal_error ("tree check: %s, have %s in %s, at %s:%d",
6612 buffer, omp_clause_code_name[TREE_CODE (node)],
6613 function, trim_filename (file), line);
6617 #undef DEFTREESTRUCT
6618 #define DEFTREESTRUCT(VAL, NAME) NAME,
6620 static const char *ts_enum_names[] = {
6621 #include "treestruct.def"
6623 #undef DEFTREESTRUCT
6625 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
6627 /* Similar to tree_class_check_failed, except that we check for
6628 whether CODE contains the tree structure identified by EN. */
6630 void
6631 tree_contains_struct_check_failed (const tree node,
6632 const enum tree_node_structure_enum en,
6633 const char *file, int line,
6634 const char *function)
6636 internal_error
6637 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
6638 TS_ENUM_NAME(en),
6639 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
6643 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
6644 (dynamically sized) vector. */
6646 void
6647 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
6648 const char *function)
6650 internal_error
6651 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
6652 idx + 1, len, function, trim_filename (file), line);
6655 /* Similar to above, except that the check is for the bounds of a PHI_NODE's
6656 (dynamically sized) vector. */
6658 void
6659 phi_node_elt_check_failed (int idx, int len, const char *file, int line,
6660 const char *function)
6662 internal_error
6663 ("tree check: accessed elt %d of phi_node with %d elts in %s, at %s:%d",
6664 idx + 1, len, function, trim_filename (file), line);
6667 /* Similar to above, except that the check is for the bounds of the operand
6668 vector of an expression node. */
6670 void
6671 tree_operand_check_failed (int idx, enum tree_code code, const char *file,
6672 int line, const char *function)
6674 internal_error
6675 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
6676 idx + 1, tree_code_name[code], TREE_CODE_LENGTH (code),
6677 function, trim_filename (file), line);
6680 /* Similar to above, except that the check is for the number of
6681 operands of an OMP_CLAUSE node. */
6683 void
6684 omp_clause_operand_check_failed (int idx, tree t, const char *file,
6685 int line, const char *function)
6687 internal_error
6688 ("tree check: accessed operand %d of omp_clause %s with %d operands "
6689 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
6690 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
6691 trim_filename (file), line);
6693 #endif /* ENABLE_TREE_CHECKING */
6695 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
6696 and mapped to the machine mode MODE. Initialize its fields and build
6697 the information necessary for debugging output. */
6699 static tree
6700 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
6702 tree t;
6703 hashval_t hashcode = 0;
6705 /* Build a main variant, based on the main variant of the inner type, then
6706 use it to build the variant we return. */
6707 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
6708 && TYPE_MAIN_VARIANT (innertype) != innertype)
6709 return build_type_attribute_qual_variant (
6710 make_vector_type (TYPE_MAIN_VARIANT (innertype), nunits, mode),
6711 TYPE_ATTRIBUTES (innertype),
6712 TYPE_QUALS (innertype));
6714 t = make_node (VECTOR_TYPE);
6715 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
6716 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
6717 TYPE_MODE (t) = mode;
6718 TYPE_READONLY (t) = TYPE_READONLY (innertype);
6719 TYPE_VOLATILE (t) = TYPE_VOLATILE (innertype);
6721 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
6722 SET_TYPE_STRUCTURAL_EQUALITY (t);
6723 else if (TYPE_CANONICAL (innertype) != innertype
6724 || mode != VOIDmode)
6725 TYPE_CANONICAL (t)
6726 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
6728 layout_type (t);
6731 tree index = build_int_cst (NULL_TREE, nunits - 1);
6732 tree array = build_array_type (innertype, build_index_type (index));
6733 tree rt = make_node (RECORD_TYPE);
6735 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
6736 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
6737 layout_type (rt);
6738 TYPE_DEBUG_REPRESENTATION_TYPE (t) = rt;
6739 /* In dwarfout.c, type lookup uses TYPE_UID numbers. We want to output
6740 the representation type, and we want to find that die when looking up
6741 the vector type. This is most easily achieved by making the TYPE_UID
6742 numbers equal. */
6743 TYPE_UID (rt) = TYPE_UID (t);
6746 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
6747 hashcode = iterative_hash_host_wide_int (mode, hashcode);
6748 hashcode = iterative_hash_object (TYPE_HASH (innertype), hashcode);
6749 return type_hash_canon (hashcode, t);
6752 static tree
6753 make_or_reuse_type (unsigned size, int unsignedp)
6755 if (size == INT_TYPE_SIZE)
6756 return unsignedp ? unsigned_type_node : integer_type_node;
6757 if (size == CHAR_TYPE_SIZE)
6758 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
6759 if (size == SHORT_TYPE_SIZE)
6760 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
6761 if (size == LONG_TYPE_SIZE)
6762 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
6763 if (size == LONG_LONG_TYPE_SIZE)
6764 return (unsignedp ? long_long_unsigned_type_node
6765 : long_long_integer_type_node);
6767 if (unsignedp)
6768 return make_unsigned_type (size);
6769 else
6770 return make_signed_type (size);
6773 /* Create nodes for all integer types (and error_mark_node) using the sizes
6774 of C datatypes. The caller should call set_sizetype soon after calling
6775 this function to select one of the types as sizetype. */
6777 void
6778 build_common_tree_nodes (bool signed_char, bool signed_sizetype)
6780 error_mark_node = make_node (ERROR_MARK);
6781 TREE_TYPE (error_mark_node) = error_mark_node;
6783 initialize_sizetypes (signed_sizetype);
6785 /* Define both `signed char' and `unsigned char'. */
6786 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
6787 TYPE_STRING_FLAG (signed_char_type_node) = 1;
6788 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
6789 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
6791 /* Define `char', which is like either `signed char' or `unsigned char'
6792 but not the same as either. */
6793 char_type_node
6794 = (signed_char
6795 ? make_signed_type (CHAR_TYPE_SIZE)
6796 : make_unsigned_type (CHAR_TYPE_SIZE));
6797 TYPE_STRING_FLAG (char_type_node) = 1;
6799 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
6800 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
6801 integer_type_node = make_signed_type (INT_TYPE_SIZE);
6802 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
6803 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
6804 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
6805 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
6806 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
6808 /* Define a boolean type. This type only represents boolean values but
6809 may be larger than char depending on the value of BOOL_TYPE_SIZE.
6810 Front ends which want to override this size (i.e. Java) can redefine
6811 boolean_type_node before calling build_common_tree_nodes_2. */
6812 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6813 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
6814 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
6815 TYPE_PRECISION (boolean_type_node) = 1;
6817 /* Fill in the rest of the sized types. Reuse existing type nodes
6818 when possible. */
6819 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
6820 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
6821 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
6822 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
6823 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
6825 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
6826 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
6827 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
6828 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
6829 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
6831 access_public_node = get_identifier ("public");
6832 access_protected_node = get_identifier ("protected");
6833 access_private_node = get_identifier ("private");
6836 /* Call this function after calling build_common_tree_nodes and set_sizetype.
6837 It will create several other common tree nodes. */
6839 void
6840 build_common_tree_nodes_2 (int short_double)
6842 /* Define these next since types below may used them. */
6843 integer_zero_node = build_int_cst (NULL_TREE, 0);
6844 integer_one_node = build_int_cst (NULL_TREE, 1);
6845 integer_minus_one_node = build_int_cst (NULL_TREE, -1);
6847 size_zero_node = size_int (0);
6848 size_one_node = size_int (1);
6849 bitsize_zero_node = bitsize_int (0);
6850 bitsize_one_node = bitsize_int (1);
6851 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
6853 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
6854 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
6856 void_type_node = make_node (VOID_TYPE);
6857 layout_type (void_type_node);
6859 /* We are not going to have real types in C with less than byte alignment,
6860 so we might as well not have any types that claim to have it. */
6861 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
6862 TYPE_USER_ALIGN (void_type_node) = 0;
6864 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
6865 layout_type (TREE_TYPE (null_pointer_node));
6867 ptr_type_node = build_pointer_type (void_type_node);
6868 const_ptr_type_node
6869 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
6870 fileptr_type_node = ptr_type_node;
6872 float_type_node = make_node (REAL_TYPE);
6873 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
6874 layout_type (float_type_node);
6876 double_type_node = make_node (REAL_TYPE);
6877 if (short_double)
6878 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
6879 else
6880 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
6881 layout_type (double_type_node);
6883 long_double_type_node = make_node (REAL_TYPE);
6884 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
6885 layout_type (long_double_type_node);
6887 float_ptr_type_node = build_pointer_type (float_type_node);
6888 double_ptr_type_node = build_pointer_type (double_type_node);
6889 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
6890 integer_ptr_type_node = build_pointer_type (integer_type_node);
6892 /* Fixed size integer types. */
6893 uint32_type_node = build_nonstandard_integer_type (32, true);
6894 uint64_type_node = build_nonstandard_integer_type (64, true);
6896 /* Decimal float types. */
6897 dfloat32_type_node = make_node (REAL_TYPE);
6898 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
6899 layout_type (dfloat32_type_node);
6900 TYPE_MODE (dfloat32_type_node) = SDmode;
6901 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
6903 dfloat64_type_node = make_node (REAL_TYPE);
6904 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
6905 layout_type (dfloat64_type_node);
6906 TYPE_MODE (dfloat64_type_node) = DDmode;
6907 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
6909 dfloat128_type_node = make_node (REAL_TYPE);
6910 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
6911 layout_type (dfloat128_type_node);
6912 TYPE_MODE (dfloat128_type_node) = TDmode;
6913 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
6915 complex_integer_type_node = make_node (COMPLEX_TYPE);
6916 TREE_TYPE (complex_integer_type_node) = integer_type_node;
6917 layout_type (complex_integer_type_node);
6919 complex_float_type_node = make_node (COMPLEX_TYPE);
6920 TREE_TYPE (complex_float_type_node) = float_type_node;
6921 layout_type (complex_float_type_node);
6923 complex_double_type_node = make_node (COMPLEX_TYPE);
6924 TREE_TYPE (complex_double_type_node) = double_type_node;
6925 layout_type (complex_double_type_node);
6927 complex_long_double_type_node = make_node (COMPLEX_TYPE);
6928 TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
6929 layout_type (complex_long_double_type_node);
6932 tree t = targetm.build_builtin_va_list ();
6934 /* Many back-ends define record types without setting TYPE_NAME.
6935 If we copied the record type here, we'd keep the original
6936 record type without a name. This breaks name mangling. So,
6937 don't copy record types and let c_common_nodes_and_builtins()
6938 declare the type to be __builtin_va_list. */
6939 if (TREE_CODE (t) != RECORD_TYPE)
6940 t = build_variant_type_copy (t);
6942 va_list_type_node = t;
6946 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
6948 static void
6949 local_define_builtin (const char *name, tree type, enum built_in_function code,
6950 const char *library_name, int ecf_flags)
6952 tree decl;
6954 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
6955 library_name, NULL_TREE);
6956 if (ecf_flags & ECF_CONST)
6957 TREE_READONLY (decl) = 1;
6958 if (ecf_flags & ECF_PURE)
6959 DECL_IS_PURE (decl) = 1;
6960 if (ecf_flags & ECF_NORETURN)
6961 TREE_THIS_VOLATILE (decl) = 1;
6962 if (ecf_flags & ECF_NOTHROW)
6963 TREE_NOTHROW (decl) = 1;
6964 if (ecf_flags & ECF_MALLOC)
6965 DECL_IS_MALLOC (decl) = 1;
6967 built_in_decls[code] = decl;
6968 implicit_built_in_decls[code] = decl;
6971 /* Call this function after instantiating all builtins that the language
6972 front end cares about. This will build the rest of the builtins that
6973 are relied upon by the tree optimizers and the middle-end. */
6975 void
6976 build_common_builtin_nodes (void)
6978 tree tmp, ftype;
6980 if (built_in_decls[BUILT_IN_MEMCPY] == NULL
6981 || built_in_decls[BUILT_IN_MEMMOVE] == NULL)
6983 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
6984 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
6985 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
6986 ftype = build_function_type (ptr_type_node, tmp);
6988 if (built_in_decls[BUILT_IN_MEMCPY] == NULL)
6989 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
6990 "memcpy", ECF_NOTHROW);
6991 if (built_in_decls[BUILT_IN_MEMMOVE] == NULL)
6992 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
6993 "memmove", ECF_NOTHROW);
6996 if (built_in_decls[BUILT_IN_MEMCMP] == NULL)
6998 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
6999 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
7000 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
7001 ftype = build_function_type (integer_type_node, tmp);
7002 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
7003 "memcmp", ECF_PURE | ECF_NOTHROW);
7006 if (built_in_decls[BUILT_IN_MEMSET] == NULL)
7008 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7009 tmp = tree_cons (NULL_TREE, integer_type_node, tmp);
7010 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7011 ftype = build_function_type (ptr_type_node, tmp);
7012 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
7013 "memset", ECF_NOTHROW);
7016 if (built_in_decls[BUILT_IN_ALLOCA] == NULL)
7018 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7019 ftype = build_function_type (ptr_type_node, tmp);
7020 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
7021 "alloca", ECF_NOTHROW | ECF_MALLOC);
7024 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7025 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7026 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7027 ftype = build_function_type (void_type_node, tmp);
7028 local_define_builtin ("__builtin_init_trampoline", ftype,
7029 BUILT_IN_INIT_TRAMPOLINE,
7030 "__builtin_init_trampoline", ECF_NOTHROW);
7032 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7033 ftype = build_function_type (ptr_type_node, tmp);
7034 local_define_builtin ("__builtin_adjust_trampoline", ftype,
7035 BUILT_IN_ADJUST_TRAMPOLINE,
7036 "__builtin_adjust_trampoline",
7037 ECF_CONST | ECF_NOTHROW);
7039 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7040 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7041 ftype = build_function_type (void_type_node, tmp);
7042 local_define_builtin ("__builtin_nonlocal_goto", ftype,
7043 BUILT_IN_NONLOCAL_GOTO,
7044 "__builtin_nonlocal_goto",
7045 ECF_NORETURN | ECF_NOTHROW);
7047 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7048 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7049 ftype = build_function_type (void_type_node, tmp);
7050 local_define_builtin ("__builtin_setjmp_setup", ftype,
7051 BUILT_IN_SETJMP_SETUP,
7052 "__builtin_setjmp_setup", ECF_NOTHROW);
7054 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7055 ftype = build_function_type (ptr_type_node, tmp);
7056 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
7057 BUILT_IN_SETJMP_DISPATCHER,
7058 "__builtin_setjmp_dispatcher",
7059 ECF_PURE | ECF_NOTHROW);
7061 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7062 ftype = build_function_type (void_type_node, tmp);
7063 local_define_builtin ("__builtin_setjmp_receiver", ftype,
7064 BUILT_IN_SETJMP_RECEIVER,
7065 "__builtin_setjmp_receiver", ECF_NOTHROW);
7067 ftype = build_function_type (ptr_type_node, void_list_node);
7068 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
7069 "__builtin_stack_save", ECF_NOTHROW);
7071 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7072 ftype = build_function_type (void_type_node, tmp);
7073 local_define_builtin ("__builtin_stack_restore", ftype,
7074 BUILT_IN_STACK_RESTORE,
7075 "__builtin_stack_restore", ECF_NOTHROW);
7077 ftype = build_function_type (void_type_node, void_list_node);
7078 local_define_builtin ("__builtin_profile_func_enter", ftype,
7079 BUILT_IN_PROFILE_FUNC_ENTER, "profile_func_enter", 0);
7080 local_define_builtin ("__builtin_profile_func_exit", ftype,
7081 BUILT_IN_PROFILE_FUNC_EXIT, "profile_func_exit", 0);
7083 /* Complex multiplication and division. These are handled as builtins
7084 rather than optabs because emit_library_call_value doesn't support
7085 complex. Further, we can do slightly better with folding these
7086 beasties if the real and complex parts of the arguments are separate. */
7088 enum machine_mode mode;
7090 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
7092 char mode_name_buf[4], *q;
7093 const char *p;
7094 enum built_in_function mcode, dcode;
7095 tree type, inner_type;
7097 type = lang_hooks.types.type_for_mode (mode, 0);
7098 if (type == NULL)
7099 continue;
7100 inner_type = TREE_TYPE (type);
7102 tmp = tree_cons (NULL_TREE, inner_type, void_list_node);
7103 tmp = tree_cons (NULL_TREE, inner_type, tmp);
7104 tmp = tree_cons (NULL_TREE, inner_type, tmp);
7105 tmp = tree_cons (NULL_TREE, inner_type, tmp);
7106 ftype = build_function_type (type, tmp);
7108 mcode = BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
7109 dcode = BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
7111 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
7112 *q = TOLOWER (*p);
7113 *q = '\0';
7115 built_in_names[mcode] = concat ("__mul", mode_name_buf, "3", NULL);
7116 local_define_builtin (built_in_names[mcode], ftype, mcode,
7117 built_in_names[mcode], ECF_CONST | ECF_NOTHROW);
7119 built_in_names[dcode] = concat ("__div", mode_name_buf, "3", NULL);
7120 local_define_builtin (built_in_names[dcode], ftype, dcode,
7121 built_in_names[dcode], ECF_CONST | ECF_NOTHROW);
7126 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
7127 better way.
7129 If we requested a pointer to a vector, build up the pointers that
7130 we stripped off while looking for the inner type. Similarly for
7131 return values from functions.
7133 The argument TYPE is the top of the chain, and BOTTOM is the
7134 new type which we will point to. */
7136 tree
7137 reconstruct_complex_type (tree type, tree bottom)
7139 tree inner, outer;
7141 if (POINTER_TYPE_P (type))
7143 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7144 outer = build_pointer_type (inner);
7146 else if (TREE_CODE (type) == ARRAY_TYPE)
7148 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7149 outer = build_array_type (inner, TYPE_DOMAIN (type));
7151 else if (TREE_CODE (type) == FUNCTION_TYPE)
7153 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7154 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
7156 else if (TREE_CODE (type) == METHOD_TYPE)
7158 tree argtypes;
7159 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7160 /* The build_method_type_directly() routine prepends 'this' to argument list,
7161 so we must compensate by getting rid of it. */
7162 argtypes = TYPE_ARG_TYPES (type);
7163 outer = build_method_type_directly (TYPE_METHOD_BASETYPE (type),
7164 inner,
7165 TYPE_ARG_TYPES (type));
7166 TYPE_ARG_TYPES (outer) = argtypes;
7168 else
7169 return bottom;
7171 TYPE_READONLY (outer) = TYPE_READONLY (type);
7172 TYPE_VOLATILE (outer) = TYPE_VOLATILE (type);
7174 return outer;
7177 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
7178 the inner type. */
7179 tree
7180 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
7182 int nunits;
7184 switch (GET_MODE_CLASS (mode))
7186 case MODE_VECTOR_INT:
7187 case MODE_VECTOR_FLOAT:
7188 nunits = GET_MODE_NUNITS (mode);
7189 break;
7191 case MODE_INT:
7192 /* Check that there are no leftover bits. */
7193 gcc_assert (GET_MODE_BITSIZE (mode)
7194 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
7196 nunits = GET_MODE_BITSIZE (mode)
7197 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
7198 break;
7200 default:
7201 gcc_unreachable ();
7204 return make_vector_type (innertype, nunits, mode);
7207 /* Similarly, but takes the inner type and number of units, which must be
7208 a power of two. */
7210 tree
7211 build_vector_type (tree innertype, int nunits)
7213 return make_vector_type (innertype, nunits, VOIDmode);
7217 /* Build RESX_EXPR with given REGION_NUMBER. */
7218 tree
7219 build_resx (int region_number)
7221 tree t;
7222 t = build1 (RESX_EXPR, void_type_node,
7223 build_int_cst (NULL_TREE, region_number));
7224 return t;
7227 /* Given an initializer INIT, return TRUE if INIT is zero or some
7228 aggregate of zeros. Otherwise return FALSE. */
7229 bool
7230 initializer_zerop (tree init)
7232 tree elt;
7234 STRIP_NOPS (init);
7236 switch (TREE_CODE (init))
7238 case INTEGER_CST:
7239 return integer_zerop (init);
7241 case REAL_CST:
7242 /* ??? Note that this is not correct for C4X float formats. There,
7243 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
7244 negative exponent. */
7245 return real_zerop (init)
7246 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
7248 case COMPLEX_CST:
7249 return integer_zerop (init)
7250 || (real_zerop (init)
7251 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
7252 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
7254 case VECTOR_CST:
7255 for (elt = TREE_VECTOR_CST_ELTS (init); elt; elt = TREE_CHAIN (elt))
7256 if (!initializer_zerop (TREE_VALUE (elt)))
7257 return false;
7258 return true;
7260 case CONSTRUCTOR:
7262 unsigned HOST_WIDE_INT idx;
7264 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
7265 if (!initializer_zerop (elt))
7266 return false;
7267 return true;
7270 default:
7271 return false;
7275 /* Build an empty statement. */
7277 tree
7278 build_empty_stmt (void)
7280 return build1 (NOP_EXPR, void_type_node, size_zero_node);
7284 /* Build an OpenMP clause with code CODE. */
7286 tree
7287 build_omp_clause (enum omp_clause_code code)
7289 tree t;
7290 int size, length;
7292 length = omp_clause_num_ops[code];
7293 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
7295 t = ggc_alloc (size);
7296 memset (t, 0, size);
7297 TREE_SET_CODE (t, OMP_CLAUSE);
7298 OMP_CLAUSE_SET_CODE (t, code);
7300 #ifdef GATHER_STATISTICS
7301 tree_node_counts[(int) omp_clause_kind]++;
7302 tree_node_sizes[(int) omp_clause_kind] += size;
7303 #endif
7305 return t;
7309 /* Returns true if it is possible to prove that the index of
7310 an array access REF (an ARRAY_REF expression) falls into the
7311 array bounds. */
7313 bool
7314 in_array_bounds_p (tree ref)
7316 tree idx = TREE_OPERAND (ref, 1);
7317 tree min, max;
7319 if (TREE_CODE (idx) != INTEGER_CST)
7320 return false;
7322 min = array_ref_low_bound (ref);
7323 max = array_ref_up_bound (ref);
7324 if (!min
7325 || !max
7326 || TREE_CODE (min) != INTEGER_CST
7327 || TREE_CODE (max) != INTEGER_CST)
7328 return false;
7330 if (tree_int_cst_lt (idx, min)
7331 || tree_int_cst_lt (max, idx))
7332 return false;
7334 return true;
7337 /* Returns true if it is possible to prove that the range of
7338 an array access REF (an ARRAY_RANGE_REF expression) falls
7339 into the array bounds. */
7341 bool
7342 range_in_array_bounds_p (tree ref)
7344 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
7345 tree range_min, range_max, min, max;
7347 range_min = TYPE_MIN_VALUE (domain_type);
7348 range_max = TYPE_MAX_VALUE (domain_type);
7349 if (!range_min
7350 || !range_max
7351 || TREE_CODE (range_min) != INTEGER_CST
7352 || TREE_CODE (range_max) != INTEGER_CST)
7353 return false;
7355 min = array_ref_low_bound (ref);
7356 max = array_ref_up_bound (ref);
7357 if (!min
7358 || !max
7359 || TREE_CODE (min) != INTEGER_CST
7360 || TREE_CODE (max) != INTEGER_CST)
7361 return false;
7363 if (tree_int_cst_lt (range_min, min)
7364 || tree_int_cst_lt (max, range_max))
7365 return false;
7367 return true;
7370 /* Return true if T (assumed to be a DECL) is a global variable. */
7372 bool
7373 is_global_var (tree t)
7375 if (MTAG_P (t))
7376 return (TREE_STATIC (t) || MTAG_GLOBAL (t));
7377 else
7378 return (TREE_STATIC (t) || DECL_EXTERNAL (t));
7381 /* Return true if T (assumed to be a DECL) must be assigned a memory
7382 location. */
7384 bool
7385 needs_to_live_in_memory (tree t)
7387 if (TREE_CODE (t) == SSA_NAME)
7388 t = SSA_NAME_VAR (t);
7390 return (TREE_ADDRESSABLE (t)
7391 || is_global_var (t)
7392 || (TREE_CODE (t) == RESULT_DECL
7393 && aggregate_value_p (t, current_function_decl)));
7396 /* There are situations in which a language considers record types
7397 compatible which have different field lists. Decide if two fields
7398 are compatible. It is assumed that the parent records are compatible. */
7400 bool
7401 fields_compatible_p (tree f1, tree f2)
7403 if (!operand_equal_p (DECL_FIELD_BIT_OFFSET (f1),
7404 DECL_FIELD_BIT_OFFSET (f2), OEP_ONLY_CONST))
7405 return false;
7407 if (!operand_equal_p (DECL_FIELD_OFFSET (f1),
7408 DECL_FIELD_OFFSET (f2), OEP_ONLY_CONST))
7409 return false;
7411 if (!lang_hooks.types_compatible_p (TREE_TYPE (f1), TREE_TYPE (f2)))
7412 return false;
7414 return true;
7417 /* Locate within RECORD a field that is compatible with ORIG_FIELD. */
7419 tree
7420 find_compatible_field (tree record, tree orig_field)
7422 tree f;
7424 for (f = TYPE_FIELDS (record); f ; f = TREE_CHAIN (f))
7425 if (TREE_CODE (f) == FIELD_DECL
7426 && fields_compatible_p (f, orig_field))
7427 return f;
7429 /* ??? Why isn't this on the main fields list? */
7430 f = TYPE_VFIELD (record);
7431 if (f && TREE_CODE (f) == FIELD_DECL
7432 && fields_compatible_p (f, orig_field))
7433 return f;
7435 /* ??? We should abort here, but Java appears to do Bad Things
7436 with inherited fields. */
7437 return orig_field;
7440 /* Return value of a constant X. */
7442 HOST_WIDE_INT
7443 int_cst_value (tree x)
7445 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
7446 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
7447 bool negative = ((val >> (bits - 1)) & 1) != 0;
7449 gcc_assert (bits <= HOST_BITS_PER_WIDE_INT);
7451 if (negative)
7452 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
7453 else
7454 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
7456 return val;
7460 /* Returns unsigned variant of TYPE. */
7462 tree
7463 unsigned_type_for (tree type)
7465 if (POINTER_TYPE_P (type))
7466 return lang_hooks.types.unsigned_type (size_type_node);
7467 return lang_hooks.types.unsigned_type (type);
7470 /* Returns signed variant of TYPE. */
7472 tree
7473 signed_type_for (tree type)
7475 if (POINTER_TYPE_P (type))
7476 return lang_hooks.types.signed_type (size_type_node);
7477 return lang_hooks.types.signed_type (type);
7480 /* Returns the largest value obtainable by casting something in INNER type to
7481 OUTER type. */
7483 tree
7484 upper_bound_in_type (tree outer, tree inner)
7486 unsigned HOST_WIDE_INT lo, hi;
7487 unsigned int det = 0;
7488 unsigned oprec = TYPE_PRECISION (outer);
7489 unsigned iprec = TYPE_PRECISION (inner);
7490 unsigned prec;
7492 /* Compute a unique number for every combination. */
7493 det |= (oprec > iprec) ? 4 : 0;
7494 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
7495 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
7497 /* Determine the exponent to use. */
7498 switch (det)
7500 case 0:
7501 case 1:
7502 /* oprec <= iprec, outer: signed, inner: don't care. */
7503 prec = oprec - 1;
7504 break;
7505 case 2:
7506 case 3:
7507 /* oprec <= iprec, outer: unsigned, inner: don't care. */
7508 prec = oprec;
7509 break;
7510 case 4:
7511 /* oprec > iprec, outer: signed, inner: signed. */
7512 prec = iprec - 1;
7513 break;
7514 case 5:
7515 /* oprec > iprec, outer: signed, inner: unsigned. */
7516 prec = iprec;
7517 break;
7518 case 6:
7519 /* oprec > iprec, outer: unsigned, inner: signed. */
7520 prec = oprec;
7521 break;
7522 case 7:
7523 /* oprec > iprec, outer: unsigned, inner: unsigned. */
7524 prec = iprec;
7525 break;
7526 default:
7527 gcc_unreachable ();
7530 /* Compute 2^^prec - 1. */
7531 if (prec <= HOST_BITS_PER_WIDE_INT)
7533 hi = 0;
7534 lo = ((~(unsigned HOST_WIDE_INT) 0)
7535 >> (HOST_BITS_PER_WIDE_INT - prec));
7537 else
7539 hi = ((~(unsigned HOST_WIDE_INT) 0)
7540 >> (2 * HOST_BITS_PER_WIDE_INT - prec));
7541 lo = ~(unsigned HOST_WIDE_INT) 0;
7544 return build_int_cst_wide (outer, lo, hi);
7547 /* Returns the smallest value obtainable by casting something in INNER type to
7548 OUTER type. */
7550 tree
7551 lower_bound_in_type (tree outer, tree inner)
7553 unsigned HOST_WIDE_INT lo, hi;
7554 unsigned oprec = TYPE_PRECISION (outer);
7555 unsigned iprec = TYPE_PRECISION (inner);
7557 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
7558 and obtain 0. */
7559 if (TYPE_UNSIGNED (outer)
7560 /* If we are widening something of an unsigned type, OUTER type
7561 contains all values of INNER type. In particular, both INNER
7562 and OUTER types have zero in common. */
7563 || (oprec > iprec && TYPE_UNSIGNED (inner)))
7564 lo = hi = 0;
7565 else
7567 /* If we are widening a signed type to another signed type, we
7568 want to obtain -2^^(iprec-1). If we are keeping the
7569 precision or narrowing to a signed type, we want to obtain
7570 -2^(oprec-1). */
7571 unsigned prec = oprec > iprec ? iprec : oprec;
7573 if (prec <= HOST_BITS_PER_WIDE_INT)
7575 hi = ~(unsigned HOST_WIDE_INT) 0;
7576 lo = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
7578 else
7580 hi = ((~(unsigned HOST_WIDE_INT) 0)
7581 << (prec - HOST_BITS_PER_WIDE_INT - 1));
7582 lo = 0;
7586 return build_int_cst_wide (outer, lo, hi);
7589 /* Return nonzero if two operands that are suitable for PHI nodes are
7590 necessarily equal. Specifically, both ARG0 and ARG1 must be either
7591 SSA_NAME or invariant. Note that this is strictly an optimization.
7592 That is, callers of this function can directly call operand_equal_p
7593 and get the same result, only slower. */
7596 operand_equal_for_phi_arg_p (tree arg0, tree arg1)
7598 if (arg0 == arg1)
7599 return 1;
7600 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
7601 return 0;
7602 return operand_equal_p (arg0, arg1, 0);
7605 /* Returns number of zeros at the end of binary representation of X.
7607 ??? Use ffs if available? */
7609 tree
7610 num_ending_zeros (tree x)
7612 unsigned HOST_WIDE_INT fr, nfr;
7613 unsigned num, abits;
7614 tree type = TREE_TYPE (x);
7616 if (TREE_INT_CST_LOW (x) == 0)
7618 num = HOST_BITS_PER_WIDE_INT;
7619 fr = TREE_INT_CST_HIGH (x);
7621 else
7623 num = 0;
7624 fr = TREE_INT_CST_LOW (x);
7627 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
7629 nfr = fr >> abits;
7630 if (nfr << abits == fr)
7632 num += abits;
7633 fr = nfr;
7637 if (num > TYPE_PRECISION (type))
7638 num = TYPE_PRECISION (type);
7640 return build_int_cst_type (type, num);
7644 #define WALK_SUBTREE(NODE) \
7645 do \
7647 result = walk_tree (&(NODE), func, data, pset); \
7648 if (result) \
7649 return result; \
7651 while (0)
7653 /* This is a subroutine of walk_tree that walks field of TYPE that are to
7654 be walked whenever a type is seen in the tree. Rest of operands and return
7655 value are as for walk_tree. */
7657 static tree
7658 walk_type_fields (tree type, walk_tree_fn func, void *data,
7659 struct pointer_set_t *pset)
7661 tree result = NULL_TREE;
7663 switch (TREE_CODE (type))
7665 case POINTER_TYPE:
7666 case REFERENCE_TYPE:
7667 /* We have to worry about mutually recursive pointers. These can't
7668 be written in C. They can in Ada. It's pathological, but
7669 there's an ACATS test (c38102a) that checks it. Deal with this
7670 by checking if we're pointing to another pointer, that one
7671 points to another pointer, that one does too, and we have no htab.
7672 If so, get a hash table. We check three levels deep to avoid
7673 the cost of the hash table if we don't need one. */
7674 if (POINTER_TYPE_P (TREE_TYPE (type))
7675 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
7676 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
7677 && !pset)
7679 result = walk_tree_without_duplicates (&TREE_TYPE (type),
7680 func, data);
7681 if (result)
7682 return result;
7684 break;
7687 /* ... fall through ... */
7689 case COMPLEX_TYPE:
7690 WALK_SUBTREE (TREE_TYPE (type));
7691 break;
7693 case METHOD_TYPE:
7694 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
7696 /* Fall through. */
7698 case FUNCTION_TYPE:
7699 WALK_SUBTREE (TREE_TYPE (type));
7701 tree arg;
7703 /* We never want to walk into default arguments. */
7704 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
7705 WALK_SUBTREE (TREE_VALUE (arg));
7707 break;
7709 case ARRAY_TYPE:
7710 /* Don't follow this nodes's type if a pointer for fear that we'll
7711 have infinite recursion. Those types are uninteresting anyway. */
7712 if (!POINTER_TYPE_P (TREE_TYPE (type))
7713 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE)
7714 WALK_SUBTREE (TREE_TYPE (type));
7715 WALK_SUBTREE (TYPE_DOMAIN (type));
7716 break;
7718 case OFFSET_TYPE:
7719 WALK_SUBTREE (TREE_TYPE (type));
7720 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
7721 break;
7723 default:
7724 break;
7727 return NULL_TREE;
7730 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
7731 called with the DATA and the address of each sub-tree. If FUNC returns a
7732 non-NULL value, the traversal is stopped, and the value returned by FUNC
7733 is returned. If PSET is non-NULL it is used to record the nodes visited,
7734 and to avoid visiting a node more than once. */
7736 tree
7737 walk_tree (tree *tp, walk_tree_fn func, void *data, struct pointer_set_t *pset)
7739 enum tree_code code;
7740 int walk_subtrees;
7741 tree result;
7743 #define WALK_SUBTREE_TAIL(NODE) \
7744 do \
7746 tp = & (NODE); \
7747 goto tail_recurse; \
7749 while (0)
7751 tail_recurse:
7752 /* Skip empty subtrees. */
7753 if (!*tp)
7754 return NULL_TREE;
7756 /* Don't walk the same tree twice, if the user has requested
7757 that we avoid doing so. */
7758 if (pset && pointer_set_insert (pset, *tp))
7759 return NULL_TREE;
7761 /* Call the function. */
7762 walk_subtrees = 1;
7763 result = (*func) (tp, &walk_subtrees, data);
7765 /* If we found something, return it. */
7766 if (result)
7767 return result;
7769 code = TREE_CODE (*tp);
7771 /* Even if we didn't, FUNC may have decided that there was nothing
7772 interesting below this point in the tree. */
7773 if (!walk_subtrees)
7775 /* But we still need to check our siblings. */
7776 if (code == TREE_LIST)
7777 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
7778 else if (code == OMP_CLAUSE)
7779 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
7780 else
7781 return NULL_TREE;
7784 result = lang_hooks.tree_inlining.walk_subtrees (tp, &walk_subtrees, func,
7785 data, pset);
7786 if (result || !walk_subtrees)
7787 return result;
7789 switch (code)
7791 case ERROR_MARK:
7792 case IDENTIFIER_NODE:
7793 case INTEGER_CST:
7794 case REAL_CST:
7795 case VECTOR_CST:
7796 case STRING_CST:
7797 case BLOCK:
7798 case PLACEHOLDER_EXPR:
7799 case SSA_NAME:
7800 case FIELD_DECL:
7801 case RESULT_DECL:
7802 /* None of these have subtrees other than those already walked
7803 above. */
7804 break;
7806 case TREE_LIST:
7807 WALK_SUBTREE (TREE_VALUE (*tp));
7808 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
7809 break;
7811 case TREE_VEC:
7813 int len = TREE_VEC_LENGTH (*tp);
7815 if (len == 0)
7816 break;
7818 /* Walk all elements but the first. */
7819 while (--len)
7820 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
7822 /* Now walk the first one as a tail call. */
7823 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
7826 case COMPLEX_CST:
7827 WALK_SUBTREE (TREE_REALPART (*tp));
7828 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
7830 case CONSTRUCTOR:
7832 unsigned HOST_WIDE_INT idx;
7833 constructor_elt *ce;
7835 for (idx = 0;
7836 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (*tp), idx, ce);
7837 idx++)
7838 WALK_SUBTREE (ce->value);
7840 break;
7842 case SAVE_EXPR:
7843 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
7845 case BIND_EXPR:
7847 tree decl;
7848 for (decl = BIND_EXPR_VARS (*tp); decl; decl = TREE_CHAIN (decl))
7850 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
7851 into declarations that are just mentioned, rather than
7852 declared; they don't really belong to this part of the tree.
7853 And, we can see cycles: the initializer for a declaration
7854 can refer to the declaration itself. */
7855 WALK_SUBTREE (DECL_INITIAL (decl));
7856 WALK_SUBTREE (DECL_SIZE (decl));
7857 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
7859 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
7862 case STATEMENT_LIST:
7864 tree_stmt_iterator i;
7865 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
7866 WALK_SUBTREE (*tsi_stmt_ptr (i));
7868 break;
7870 case OMP_CLAUSE:
7871 switch (OMP_CLAUSE_CODE (*tp))
7873 case OMP_CLAUSE_PRIVATE:
7874 case OMP_CLAUSE_SHARED:
7875 case OMP_CLAUSE_FIRSTPRIVATE:
7876 case OMP_CLAUSE_LASTPRIVATE:
7877 case OMP_CLAUSE_COPYIN:
7878 case OMP_CLAUSE_COPYPRIVATE:
7879 case OMP_CLAUSE_IF:
7880 case OMP_CLAUSE_NUM_THREADS:
7881 case OMP_CLAUSE_SCHEDULE:
7882 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
7883 /* FALLTHRU */
7885 case OMP_CLAUSE_NOWAIT:
7886 case OMP_CLAUSE_ORDERED:
7887 case OMP_CLAUSE_DEFAULT:
7888 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
7890 case OMP_CLAUSE_REDUCTION:
7892 int i;
7893 for (i = 0; i < 4; i++)
7894 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
7895 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
7898 default:
7899 gcc_unreachable ();
7901 break;
7903 case TARGET_EXPR:
7905 int i, len;
7907 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
7908 But, we only want to walk once. */
7909 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
7910 for (i = 0; i < len; ++i)
7911 WALK_SUBTREE (TREE_OPERAND (*tp, i));
7912 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
7915 case DECL_EXPR:
7916 /* If this is a TYPE_DECL, walk into the fields of the type that it's
7917 defining. We only want to walk into these fields of a type in this
7918 case and not in the general case of a mere reference to the type.
7920 The criterion is as follows: if the field can be an expression, it
7921 must be walked only here. This should be in keeping with the fields
7922 that are directly gimplified in gimplify_type_sizes in order for the
7923 mark/copy-if-shared/unmark machinery of the gimplifier to work with
7924 variable-sized types.
7926 Note that DECLs get walked as part of processing the BIND_EXPR. */
7927 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
7929 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
7930 if (TREE_CODE (*type_p) == ERROR_MARK)
7931 return NULL_TREE;
7933 /* Call the function for the type. See if it returns anything or
7934 doesn't want us to continue. If we are to continue, walk both
7935 the normal fields and those for the declaration case. */
7936 result = (*func) (type_p, &walk_subtrees, data);
7937 if (result || !walk_subtrees)
7938 return result;
7940 result = walk_type_fields (*type_p, func, data, pset);
7941 if (result)
7942 return result;
7944 /* If this is a record type, also walk the fields. */
7945 if (TREE_CODE (*type_p) == RECORD_TYPE
7946 || TREE_CODE (*type_p) == UNION_TYPE
7947 || TREE_CODE (*type_p) == QUAL_UNION_TYPE)
7949 tree field;
7951 for (field = TYPE_FIELDS (*type_p); field;
7952 field = TREE_CHAIN (field))
7954 /* We'd like to look at the type of the field, but we can
7955 easily get infinite recursion. So assume it's pointed
7956 to elsewhere in the tree. Also, ignore things that
7957 aren't fields. */
7958 if (TREE_CODE (field) != FIELD_DECL)
7959 continue;
7961 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
7962 WALK_SUBTREE (DECL_SIZE (field));
7963 WALK_SUBTREE (DECL_SIZE_UNIT (field));
7964 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
7965 WALK_SUBTREE (DECL_QUALIFIER (field));
7969 /* Same for scalar types. */
7970 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
7971 || TREE_CODE (*type_p) == ENUMERAL_TYPE
7972 || TREE_CODE (*type_p) == INTEGER_TYPE
7973 || TREE_CODE (*type_p) == REAL_TYPE)
7975 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
7976 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
7979 WALK_SUBTREE (TYPE_SIZE (*type_p));
7980 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
7982 /* FALLTHRU */
7984 default:
7985 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
7986 || IS_GIMPLE_STMT_CODE_CLASS (TREE_CODE_CLASS (code)))
7988 int i, len;
7990 /* Walk over all the sub-trees of this operand. */
7991 len = TREE_CODE_LENGTH (code);
7993 /* Go through the subtrees. We need to do this in forward order so
7994 that the scope of a FOR_EXPR is handled properly. */
7995 if (len)
7997 for (i = 0; i < len - 1; ++i)
7998 WALK_SUBTREE (GENERIC_TREE_OPERAND (*tp, i));
7999 WALK_SUBTREE_TAIL (GENERIC_TREE_OPERAND (*tp, len - 1));
8002 /* If this is a type, walk the needed fields in the type. */
8003 else if (TYPE_P (*tp))
8004 return walk_type_fields (*tp, func, data, pset);
8005 break;
8008 /* We didn't find what we were looking for. */
8009 return NULL_TREE;
8011 #undef WALK_SUBTREE_TAIL
8013 #undef WALK_SUBTREE
8015 /* Like walk_tree, but does not walk duplicate nodes more than once. */
8017 tree
8018 walk_tree_without_duplicates (tree *tp, walk_tree_fn func, void *data)
8020 tree result;
8021 struct pointer_set_t *pset;
8023 pset = pointer_set_create ();
8024 result = walk_tree (tp, func, data, pset);
8025 pointer_set_destroy (pset);
8026 return result;
8030 /* Return true if STMT is an empty statement or contains nothing but
8031 empty statements. */
8033 bool
8034 empty_body_p (tree stmt)
8036 tree_stmt_iterator i;
8037 tree body;
8039 if (IS_EMPTY_STMT (stmt))
8040 return true;
8041 else if (TREE_CODE (stmt) == BIND_EXPR)
8042 body = BIND_EXPR_BODY (stmt);
8043 else if (TREE_CODE (stmt) == STATEMENT_LIST)
8044 body = stmt;
8045 else
8046 return false;
8048 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
8049 if (!empty_body_p (tsi_stmt (i)))
8050 return false;
8052 return true;
8055 tree *
8056 tree_block (tree t)
8058 char const c = TREE_CODE_CLASS (TREE_CODE (t));
8060 if (IS_EXPR_CODE_CLASS (c))
8061 return &t->exp.block;
8062 else if (IS_GIMPLE_STMT_CODE_CLASS (c))
8063 return &GIMPLE_STMT_BLOCK (t);
8064 gcc_unreachable ();
8065 return NULL;
8068 tree *
8069 generic_tree_operand (tree node, int i)
8071 if (GIMPLE_STMT_P (node))
8072 return &GIMPLE_STMT_OPERAND (node, i);
8073 return &TREE_OPERAND (node, i);
8076 tree *
8077 generic_tree_type (tree node)
8079 if (GIMPLE_STMT_P (node))
8080 return &void_type_node;
8081 return &TREE_TYPE (node);
8084 #include "gt-tree.h"