libfuncs.h (LTI_synchronize): New libfunc_index.
[official-gcc.git] / gcc / tree.c
blob91b2ec5c0a41ba43534d4d6d8c7c5c5e88c9d00e
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, 2008
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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file contains the low level primitives for operating on tree nodes,
23 including allocation, list operations, interning of identifiers,
24 construction of data type nodes and statement nodes,
25 and construction of type conversion nodes. It also contains
26 tables index by tree code that describe how to take apart
27 nodes of that code.
29 It is intended to be language-independent, but occasionally
30 calls language-dependent routines defined (for C) in typecheck.c. */
32 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "flags.h"
37 #include "tree.h"
38 #include "real.h"
39 #include "tm_p.h"
40 #include "function.h"
41 #include "obstack.h"
42 #include "toplev.h"
43 #include "ggc.h"
44 #include "hashtab.h"
45 #include "output.h"
46 #include "target.h"
47 #include "langhooks.h"
48 #include "tree-iterator.h"
49 #include "basic-block.h"
50 #include "tree-flow.h"
51 #include "params.h"
52 #include "pointer-set.h"
53 #include "fixed-value.h"
55 /* Tree code classes. */
57 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
58 #define END_OF_BASE_TREE_CODES tcc_exceptional,
60 const enum tree_code_class tree_code_type[] = {
61 #include "all-tree.def"
64 #undef DEFTREECODE
65 #undef END_OF_BASE_TREE_CODES
67 /* Table indexed by tree code giving number of expression
68 operands beyond the fixed part of the node structure.
69 Not used for types or decls. */
71 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
72 #define END_OF_BASE_TREE_CODES 0,
74 const unsigned char tree_code_length[] = {
75 #include "all-tree.def"
78 #undef DEFTREECODE
79 #undef END_OF_BASE_TREE_CODES
81 /* Names of tree components.
82 Used for printing out the tree and error messages. */
83 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
84 #define END_OF_BASE_TREE_CODES "@dummy",
86 const char *const tree_code_name[] = {
87 #include "all-tree.def"
90 #undef DEFTREECODE
91 #undef END_OF_BASE_TREE_CODES
93 /* Each tree code class has an associated string representation.
94 These must correspond to the tree_code_class entries. */
96 const char *const tree_code_class_strings[] =
98 "exceptional",
99 "constant",
100 "type",
101 "declaration",
102 "reference",
103 "comparison",
104 "unary",
105 "binary",
106 "statement",
107 "vl_exp",
108 "expression",
109 "gimple_stmt"
112 /* obstack.[ch] explicitly declined to prototype this. */
113 extern int _obstack_allocated_p (struct obstack *h, void *obj);
115 #ifdef GATHER_STATISTICS
116 /* Statistics-gathering stuff. */
118 int tree_node_counts[(int) all_kinds];
119 int tree_node_sizes[(int) all_kinds];
121 /* Keep in sync with tree.h:enum tree_node_kind. */
122 static const char * const tree_node_kind_names[] = {
123 "decls",
124 "types",
125 "blocks",
126 "stmts",
127 "refs",
128 "exprs",
129 "constants",
130 "identifiers",
131 "perm_tree_lists",
132 "temp_tree_lists",
133 "vecs",
134 "binfos",
135 "phi_nodes",
136 "ssa names",
137 "constructors",
138 "random kinds",
139 "lang_decl kinds",
140 "lang_type kinds",
141 "omp clauses",
142 "gimple statements"
144 #endif /* GATHER_STATISTICS */
146 /* Unique id for next decl created. */
147 static GTY(()) int next_decl_uid;
148 /* Unique id for next type created. */
149 static GTY(()) int next_type_uid = 1;
151 /* Since we cannot rehash a type after it is in the table, we have to
152 keep the hash code. */
154 struct type_hash GTY(())
156 unsigned long hash;
157 tree type;
160 /* Initial size of the hash table (rounded to next prime). */
161 #define TYPE_HASH_INITIAL_SIZE 1000
163 /* Now here is the hash table. When recording a type, it is added to
164 the slot whose index is the hash code. Note that the hash table is
165 used for several kinds of types (function types, array types and
166 array index range types, for now). While all these live in the
167 same table, they are completely independent, and the hash code is
168 computed differently for each of these. */
170 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
171 htab_t type_hash_table;
173 /* Hash table and temporary node for larger integer const values. */
174 static GTY (()) tree int_cst_node;
175 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
176 htab_t int_cst_hash_table;
178 /* General tree->tree mapping structure for use in hash tables. */
181 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
182 htab_t debug_expr_for_decl;
184 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
185 htab_t value_expr_for_decl;
187 static GTY ((if_marked ("tree_priority_map_marked_p"),
188 param_is (struct tree_priority_map)))
189 htab_t init_priority_for_decl;
191 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
192 htab_t restrict_base_for_decl;
194 static void set_type_quals (tree, int);
195 static int type_hash_eq (const void *, const void *);
196 static hashval_t type_hash_hash (const void *);
197 static hashval_t int_cst_hash_hash (const void *);
198 static int int_cst_hash_eq (const void *, const void *);
199 static void print_type_hash_statistics (void);
200 static void print_debug_expr_statistics (void);
201 static void print_value_expr_statistics (void);
202 static int type_hash_marked_p (const void *);
203 static unsigned int type_hash_list (const_tree, hashval_t);
204 static unsigned int attribute_hash_list (const_tree, hashval_t);
206 tree global_trees[TI_MAX];
207 tree integer_types[itk_none];
209 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
211 /* Number of operands for each OpenMP clause. */
212 unsigned const char omp_clause_num_ops[] =
214 0, /* OMP_CLAUSE_ERROR */
215 1, /* OMP_CLAUSE_PRIVATE */
216 1, /* OMP_CLAUSE_SHARED */
217 1, /* OMP_CLAUSE_FIRSTPRIVATE */
218 2, /* OMP_CLAUSE_LASTPRIVATE */
219 4, /* OMP_CLAUSE_REDUCTION */
220 1, /* OMP_CLAUSE_COPYIN */
221 1, /* OMP_CLAUSE_COPYPRIVATE */
222 1, /* OMP_CLAUSE_IF */
223 1, /* OMP_CLAUSE_NUM_THREADS */
224 1, /* OMP_CLAUSE_SCHEDULE */
225 0, /* OMP_CLAUSE_NOWAIT */
226 0, /* OMP_CLAUSE_ORDERED */
227 0, /* OMP_CLAUSE_DEFAULT */
228 3, /* OMP_CLAUSE_COLLAPSE */
229 0 /* OMP_CLAUSE_UNTIED */
232 const char * const omp_clause_code_name[] =
234 "error_clause",
235 "private",
236 "shared",
237 "firstprivate",
238 "lastprivate",
239 "reduction",
240 "copyin",
241 "copyprivate",
242 "if",
243 "num_threads",
244 "schedule",
245 "nowait",
246 "ordered",
247 "default",
248 "collapse",
249 "untied"
252 /* Init tree.c. */
254 void
255 init_ttree (void)
257 /* Initialize the hash table of types. */
258 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
259 type_hash_eq, 0);
261 debug_expr_for_decl = htab_create_ggc (512, tree_map_hash,
262 tree_map_eq, 0);
264 value_expr_for_decl = htab_create_ggc (512, tree_map_hash,
265 tree_map_eq, 0);
266 init_priority_for_decl = htab_create_ggc (512, tree_priority_map_hash,
267 tree_priority_map_eq, 0);
268 restrict_base_for_decl = htab_create_ggc (256, tree_map_hash,
269 tree_map_eq, 0);
271 int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
272 int_cst_hash_eq, NULL);
274 int_cst_node = make_node (INTEGER_CST);
276 tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON] = 1;
277 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_NON_COMMON] = 1;
278 tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON] = 1;
281 tree_contains_struct[CONST_DECL][TS_DECL_COMMON] = 1;
282 tree_contains_struct[VAR_DECL][TS_DECL_COMMON] = 1;
283 tree_contains_struct[PARM_DECL][TS_DECL_COMMON] = 1;
284 tree_contains_struct[RESULT_DECL][TS_DECL_COMMON] = 1;
285 tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON] = 1;
286 tree_contains_struct[TYPE_DECL][TS_DECL_COMMON] = 1;
287 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON] = 1;
288 tree_contains_struct[LABEL_DECL][TS_DECL_COMMON] = 1;
289 tree_contains_struct[FIELD_DECL][TS_DECL_COMMON] = 1;
292 tree_contains_struct[CONST_DECL][TS_DECL_WRTL] = 1;
293 tree_contains_struct[VAR_DECL][TS_DECL_WRTL] = 1;
294 tree_contains_struct[PARM_DECL][TS_DECL_WRTL] = 1;
295 tree_contains_struct[RESULT_DECL][TS_DECL_WRTL] = 1;
296 tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL] = 1;
297 tree_contains_struct[LABEL_DECL][TS_DECL_WRTL] = 1;
299 tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL] = 1;
300 tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL] = 1;
301 tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL] = 1;
302 tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL] = 1;
303 tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL] = 1;
304 tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL] = 1;
305 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL] = 1;
306 tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL] = 1;
307 tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL] = 1;
308 tree_contains_struct[NAME_MEMORY_TAG][TS_DECL_MINIMAL] = 1;
309 tree_contains_struct[SYMBOL_MEMORY_TAG][TS_DECL_MINIMAL] = 1;
310 tree_contains_struct[MEMORY_PARTITION_TAG][TS_DECL_MINIMAL] = 1;
312 tree_contains_struct[NAME_MEMORY_TAG][TS_MEMORY_TAG] = 1;
313 tree_contains_struct[SYMBOL_MEMORY_TAG][TS_MEMORY_TAG] = 1;
314 tree_contains_struct[MEMORY_PARTITION_TAG][TS_MEMORY_TAG] = 1;
316 tree_contains_struct[MEMORY_PARTITION_TAG][TS_MEMORY_PARTITION_TAG] = 1;
318 tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS] = 1;
319 tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS] = 1;
320 tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS] = 1;
321 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_WITH_VIS] = 1;
323 tree_contains_struct[VAR_DECL][TS_VAR_DECL] = 1;
324 tree_contains_struct[FIELD_DECL][TS_FIELD_DECL] = 1;
325 tree_contains_struct[PARM_DECL][TS_PARM_DECL] = 1;
326 tree_contains_struct[LABEL_DECL][TS_LABEL_DECL] = 1;
327 tree_contains_struct[RESULT_DECL][TS_RESULT_DECL] = 1;
328 tree_contains_struct[CONST_DECL][TS_CONST_DECL] = 1;
329 tree_contains_struct[TYPE_DECL][TS_TYPE_DECL] = 1;
330 tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL] = 1;
332 lang_hooks.init_ts ();
336 /* The name of the object as the assembler will see it (but before any
337 translations made by ASM_OUTPUT_LABELREF). Often this is the same
338 as DECL_NAME. It is an IDENTIFIER_NODE. */
339 tree
340 decl_assembler_name (tree decl)
342 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
343 lang_hooks.set_decl_assembler_name (decl);
344 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
347 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
349 bool
350 decl_assembler_name_equal (tree decl, tree asmname)
352 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
354 if (decl_asmname == asmname)
355 return true;
357 /* If the target assembler name was set by the user, things are trickier.
358 We have a leading '*' to begin with. After that, it's arguable what
359 is the correct thing to do with -fleading-underscore. Arguably, we've
360 historically been doing the wrong thing in assemble_alias by always
361 printing the leading underscore. Since we're not changing that, make
362 sure user_label_prefix follows the '*' before matching. */
363 if (IDENTIFIER_POINTER (decl_asmname)[0] == '*')
365 const char *decl_str = IDENTIFIER_POINTER (decl_asmname) + 1;
366 size_t ulp_len = strlen (user_label_prefix);
368 if (ulp_len == 0)
370 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
371 decl_str += ulp_len;
372 else
373 return false;
375 return strcmp (decl_str, IDENTIFIER_POINTER (asmname)) == 0;
378 return false;
381 /* Compute the number of bytes occupied by a tree with code CODE.
382 This function cannot be used for nodes that have variable sizes,
383 including TREE_VEC, PHI_NODE, STRING_CST, and CALL_EXPR. */
384 size_t
385 tree_code_size (enum tree_code code)
387 switch (TREE_CODE_CLASS (code))
389 case tcc_declaration: /* A decl node */
391 switch (code)
393 case FIELD_DECL:
394 return sizeof (struct tree_field_decl);
395 case PARM_DECL:
396 return sizeof (struct tree_parm_decl);
397 case VAR_DECL:
398 return sizeof (struct tree_var_decl);
399 case LABEL_DECL:
400 return sizeof (struct tree_label_decl);
401 case RESULT_DECL:
402 return sizeof (struct tree_result_decl);
403 case CONST_DECL:
404 return sizeof (struct tree_const_decl);
405 case TYPE_DECL:
406 return sizeof (struct tree_type_decl);
407 case FUNCTION_DECL:
408 return sizeof (struct tree_function_decl);
409 case NAME_MEMORY_TAG:
410 case SYMBOL_MEMORY_TAG:
411 return sizeof (struct tree_memory_tag);
412 case MEMORY_PARTITION_TAG:
413 return sizeof (struct tree_memory_partition_tag);
414 default:
415 return sizeof (struct tree_decl_non_common);
419 case tcc_type: /* a type node */
420 return sizeof (struct tree_type);
422 case tcc_reference: /* a reference */
423 case tcc_expression: /* an expression */
424 case tcc_statement: /* an expression with side effects */
425 case tcc_comparison: /* a comparison expression */
426 case tcc_unary: /* a unary arithmetic expression */
427 case tcc_binary: /* a binary arithmetic expression */
428 return (sizeof (struct tree_exp)
429 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
431 case tcc_gimple_stmt:
432 return (sizeof (struct gimple_stmt)
433 + (TREE_CODE_LENGTH (code) - 1) * sizeof (char *));
435 case tcc_constant: /* a constant */
436 switch (code)
438 case INTEGER_CST: return sizeof (struct tree_int_cst);
439 case REAL_CST: return sizeof (struct tree_real_cst);
440 case FIXED_CST: return sizeof (struct tree_fixed_cst);
441 case COMPLEX_CST: return sizeof (struct tree_complex);
442 case VECTOR_CST: return sizeof (struct tree_vector);
443 case STRING_CST: gcc_unreachable ();
444 default:
445 return lang_hooks.tree_size (code);
448 case tcc_exceptional: /* something random, like an identifier. */
449 switch (code)
451 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
452 case TREE_LIST: return sizeof (struct tree_list);
454 case ERROR_MARK:
455 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
457 case TREE_VEC:
458 case OMP_CLAUSE:
459 case PHI_NODE: gcc_unreachable ();
461 case SSA_NAME: return sizeof (struct tree_ssa_name);
463 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
464 case BLOCK: return sizeof (struct tree_block);
465 case VALUE_HANDLE: return sizeof (struct tree_value_handle);
466 case CONSTRUCTOR: return sizeof (struct tree_constructor);
468 default:
469 return lang_hooks.tree_size (code);
472 default:
473 gcc_unreachable ();
477 /* Compute the number of bytes occupied by NODE. This routine only
478 looks at TREE_CODE, except for those nodes that have variable sizes. */
479 size_t
480 tree_size (const_tree node)
482 const enum tree_code code = TREE_CODE (node);
483 switch (code)
485 case PHI_NODE:
486 return (sizeof (struct tree_phi_node)
487 + (PHI_ARG_CAPACITY (node) - 1) * sizeof (struct phi_arg_d));
489 case TREE_BINFO:
490 return (offsetof (struct tree_binfo, base_binfos)
491 + VEC_embedded_size (tree, BINFO_N_BASE_BINFOS (node)));
493 case TREE_VEC:
494 return (sizeof (struct tree_vec)
495 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
497 case STRING_CST:
498 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
500 case OMP_CLAUSE:
501 return (sizeof (struct tree_omp_clause)
502 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
503 * sizeof (tree));
505 default:
506 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
507 return (sizeof (struct tree_exp)
508 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
509 else
510 return tree_code_size (code);
514 /* Return a newly allocated node of code CODE. For decl and type
515 nodes, some other fields are initialized. The rest of the node is
516 initialized to zero. This function cannot be used for PHI_NODE,
517 TREE_VEC or OMP_CLAUSE nodes, which is enforced by asserts in
518 tree_code_size.
520 Achoo! I got a code in the node. */
522 tree
523 make_node_stat (enum tree_code code MEM_STAT_DECL)
525 tree t;
526 enum tree_code_class type = TREE_CODE_CLASS (code);
527 size_t length = tree_code_size (code);
528 #ifdef GATHER_STATISTICS
529 tree_node_kind kind;
531 switch (type)
533 case tcc_declaration: /* A decl node */
534 kind = d_kind;
535 break;
537 case tcc_type: /* a type node */
538 kind = t_kind;
539 break;
541 case tcc_statement: /* an expression with side effects */
542 kind = s_kind;
543 break;
545 case tcc_reference: /* a reference */
546 kind = r_kind;
547 break;
549 case tcc_expression: /* an expression */
550 case tcc_comparison: /* a comparison expression */
551 case tcc_unary: /* a unary arithmetic expression */
552 case tcc_binary: /* a binary arithmetic expression */
553 kind = e_kind;
554 break;
556 case tcc_constant: /* a constant */
557 kind = c_kind;
558 break;
560 case tcc_gimple_stmt:
561 kind = gimple_stmt_kind;
562 break;
564 case tcc_exceptional: /* something random, like an identifier. */
565 switch (code)
567 case IDENTIFIER_NODE:
568 kind = id_kind;
569 break;
571 case TREE_VEC:
572 kind = vec_kind;
573 break;
575 case TREE_BINFO:
576 kind = binfo_kind;
577 break;
579 case PHI_NODE:
580 kind = phi_kind;
581 break;
583 case SSA_NAME:
584 kind = ssa_name_kind;
585 break;
587 case BLOCK:
588 kind = b_kind;
589 break;
591 case CONSTRUCTOR:
592 kind = constr_kind;
593 break;
595 default:
596 kind = x_kind;
597 break;
599 break;
601 default:
602 gcc_unreachable ();
605 tree_node_counts[(int) kind]++;
606 tree_node_sizes[(int) kind] += length;
607 #endif
609 if (code == IDENTIFIER_NODE)
610 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_id_zone);
611 else
612 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
614 memset (t, 0, length);
616 TREE_SET_CODE (t, code);
618 switch (type)
620 case tcc_statement:
621 TREE_SIDE_EFFECTS (t) = 1;
622 break;
624 case tcc_declaration:
625 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
626 DECL_IN_SYSTEM_HEADER (t) = in_system_header;
627 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
629 if (code == FUNCTION_DECL)
631 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
632 DECL_MODE (t) = FUNCTION_MODE;
634 else
635 DECL_ALIGN (t) = 1;
636 /* We have not yet computed the alias set for this declaration. */
637 DECL_POINTER_ALIAS_SET (t) = -1;
639 DECL_SOURCE_LOCATION (t) = input_location;
640 DECL_UID (t) = next_decl_uid++;
642 break;
644 case tcc_type:
645 TYPE_UID (t) = next_type_uid++;
646 TYPE_ALIGN (t) = BITS_PER_UNIT;
647 TYPE_USER_ALIGN (t) = 0;
648 TYPE_MAIN_VARIANT (t) = t;
649 TYPE_CANONICAL (t) = t;
651 /* Default to no attributes for type, but let target change that. */
652 TYPE_ATTRIBUTES (t) = NULL_TREE;
653 targetm.set_default_type_attributes (t);
655 /* We have not yet computed the alias set for this type. */
656 TYPE_ALIAS_SET (t) = -1;
657 break;
659 case tcc_constant:
660 TREE_CONSTANT (t) = 1;
661 break;
663 case tcc_expression:
664 switch (code)
666 case INIT_EXPR:
667 case MODIFY_EXPR:
668 case VA_ARG_EXPR:
669 case PREDECREMENT_EXPR:
670 case PREINCREMENT_EXPR:
671 case POSTDECREMENT_EXPR:
672 case POSTINCREMENT_EXPR:
673 /* All of these have side-effects, no matter what their
674 operands are. */
675 TREE_SIDE_EFFECTS (t) = 1;
676 break;
678 default:
679 break;
681 break;
683 case tcc_gimple_stmt:
684 switch (code)
686 case GIMPLE_MODIFY_STMT:
687 TREE_SIDE_EFFECTS (t) = 1;
688 break;
690 default:
691 break;
694 default:
695 /* Other classes need no special treatment. */
696 break;
699 return t;
702 /* Return a new node with the same contents as NODE except that its
703 TREE_CHAIN is zero and it has a fresh uid. */
705 tree
706 copy_node_stat (tree node MEM_STAT_DECL)
708 tree t;
709 enum tree_code code = TREE_CODE (node);
710 size_t length;
712 gcc_assert (code != STATEMENT_LIST);
714 length = tree_size (node);
715 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
716 memcpy (t, node, length);
718 if (!GIMPLE_TUPLE_P (node))
719 TREE_CHAIN (t) = 0;
720 TREE_ASM_WRITTEN (t) = 0;
721 TREE_VISITED (t) = 0;
722 t->base.ann = 0;
724 if (TREE_CODE_CLASS (code) == tcc_declaration)
726 DECL_UID (t) = next_decl_uid++;
727 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
728 && DECL_HAS_VALUE_EXPR_P (node))
730 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
731 DECL_HAS_VALUE_EXPR_P (t) = 1;
733 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
735 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
736 DECL_HAS_INIT_PRIORITY_P (t) = 1;
738 if (TREE_CODE (node) == VAR_DECL && DECL_BASED_ON_RESTRICT_P (node))
740 SET_DECL_RESTRICT_BASE (t, DECL_GET_RESTRICT_BASE (node));
741 DECL_BASED_ON_RESTRICT_P (t) = 1;
744 else if (TREE_CODE_CLASS (code) == tcc_type)
746 TYPE_UID (t) = next_type_uid++;
747 /* The following is so that the debug code for
748 the copy is different from the original type.
749 The two statements usually duplicate each other
750 (because they clear fields of the same union),
751 but the optimizer should catch that. */
752 TYPE_SYMTAB_POINTER (t) = 0;
753 TYPE_SYMTAB_ADDRESS (t) = 0;
755 /* Do not copy the values cache. */
756 if (TYPE_CACHED_VALUES_P(t))
758 TYPE_CACHED_VALUES_P (t) = 0;
759 TYPE_CACHED_VALUES (t) = NULL_TREE;
763 return t;
766 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
767 For example, this can copy a list made of TREE_LIST nodes. */
769 tree
770 copy_list (tree list)
772 tree head;
773 tree prev, next;
775 if (list == 0)
776 return 0;
778 head = prev = copy_node (list);
779 next = TREE_CHAIN (list);
780 while (next)
782 TREE_CHAIN (prev) = copy_node (next);
783 prev = TREE_CHAIN (prev);
784 next = TREE_CHAIN (next);
786 return head;
790 /* Create an INT_CST node with a LOW value sign extended. */
792 tree
793 build_int_cst (tree type, HOST_WIDE_INT low)
795 /* Support legacy code. */
796 if (!type)
797 type = integer_type_node;
799 return build_int_cst_wide (type, low, low < 0 ? -1 : 0);
802 /* Create an INT_CST node with a LOW value zero extended. */
804 tree
805 build_int_cstu (tree type, unsigned HOST_WIDE_INT low)
807 return build_int_cst_wide (type, low, 0);
810 /* Create an INT_CST node with a LOW value in TYPE. The value is sign extended
811 if it is negative. This function is similar to build_int_cst, but
812 the extra bits outside of the type precision are cleared. Constants
813 with these extra bits may confuse the fold so that it detects overflows
814 even in cases when they do not occur, and in general should be avoided.
815 We cannot however make this a default behavior of build_int_cst without
816 more intrusive changes, since there are parts of gcc that rely on the extra
817 precision of the integer constants. */
819 tree
820 build_int_cst_type (tree type, HOST_WIDE_INT low)
822 unsigned HOST_WIDE_INT low1;
823 HOST_WIDE_INT hi;
825 gcc_assert (type);
827 fit_double_type (low, low < 0 ? -1 : 0, &low1, &hi, type);
829 return build_int_cst_wide (type, low1, hi);
832 /* Create an INT_CST node of TYPE and value HI:LOW. The value is truncated
833 and sign extended according to the value range of TYPE. */
835 tree
836 build_int_cst_wide_type (tree type,
837 unsigned HOST_WIDE_INT low, HOST_WIDE_INT high)
839 fit_double_type (low, high, &low, &high, type);
840 return build_int_cst_wide (type, low, high);
843 /* These are the hash table functions for the hash table of INTEGER_CST
844 nodes of a sizetype. */
846 /* Return the hash code code X, an INTEGER_CST. */
848 static hashval_t
849 int_cst_hash_hash (const void *x)
851 const_tree const t = (const_tree) x;
853 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
854 ^ htab_hash_pointer (TREE_TYPE (t)));
857 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
858 is the same as that given by *Y, which is the same. */
860 static int
861 int_cst_hash_eq (const void *x, const void *y)
863 const_tree const xt = (const_tree) x;
864 const_tree const yt = (const_tree) y;
866 return (TREE_TYPE (xt) == TREE_TYPE (yt)
867 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
868 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
871 /* Create an INT_CST node of TYPE and value HI:LOW.
872 The returned node is always shared. For small integers we use a
873 per-type vector cache, for larger ones we use a single hash table. */
875 tree
876 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
878 tree t;
879 int ix = -1;
880 int limit = 0;
882 gcc_assert (type);
884 switch (TREE_CODE (type))
886 case POINTER_TYPE:
887 case REFERENCE_TYPE:
888 /* Cache NULL pointer. */
889 if (!hi && !low)
891 limit = 1;
892 ix = 0;
894 break;
896 case BOOLEAN_TYPE:
897 /* Cache false or true. */
898 limit = 2;
899 if (!hi && low < 2)
900 ix = low;
901 break;
903 case INTEGER_TYPE:
904 case OFFSET_TYPE:
905 if (TYPE_UNSIGNED (type))
907 /* Cache 0..N */
908 limit = INTEGER_SHARE_LIMIT;
909 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
910 ix = low;
912 else
914 /* Cache -1..N */
915 limit = INTEGER_SHARE_LIMIT + 1;
916 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
917 ix = low + 1;
918 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
919 ix = 0;
921 break;
923 case ENUMERAL_TYPE:
924 break;
926 default:
927 gcc_unreachable ();
930 if (ix >= 0)
932 /* Look for it in the type's vector of small shared ints. */
933 if (!TYPE_CACHED_VALUES_P (type))
935 TYPE_CACHED_VALUES_P (type) = 1;
936 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
939 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
940 if (t)
942 /* Make sure no one is clobbering the shared constant. */
943 gcc_assert (TREE_TYPE (t) == type);
944 gcc_assert (TREE_INT_CST_LOW (t) == low);
945 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
947 else
949 /* Create a new shared int. */
950 t = make_node (INTEGER_CST);
952 TREE_INT_CST_LOW (t) = low;
953 TREE_INT_CST_HIGH (t) = hi;
954 TREE_TYPE (t) = type;
956 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
959 else
961 /* Use the cache of larger shared ints. */
962 void **slot;
964 TREE_INT_CST_LOW (int_cst_node) = low;
965 TREE_INT_CST_HIGH (int_cst_node) = hi;
966 TREE_TYPE (int_cst_node) = type;
968 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
969 t = (tree) *slot;
970 if (!t)
972 /* Insert this one into the hash table. */
973 t = int_cst_node;
974 *slot = t;
975 /* Make a new node for next time round. */
976 int_cst_node = make_node (INTEGER_CST);
980 return t;
983 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
984 and the rest are zeros. */
986 tree
987 build_low_bits_mask (tree type, unsigned bits)
989 unsigned HOST_WIDE_INT low;
990 HOST_WIDE_INT high;
991 unsigned HOST_WIDE_INT all_ones = ~(unsigned HOST_WIDE_INT) 0;
993 gcc_assert (bits <= TYPE_PRECISION (type));
995 if (bits == TYPE_PRECISION (type)
996 && !TYPE_UNSIGNED (type))
998 /* Sign extended all-ones mask. */
999 low = all_ones;
1000 high = -1;
1002 else if (bits <= HOST_BITS_PER_WIDE_INT)
1004 low = all_ones >> (HOST_BITS_PER_WIDE_INT - bits);
1005 high = 0;
1007 else
1009 bits -= HOST_BITS_PER_WIDE_INT;
1010 low = all_ones;
1011 high = all_ones >> (HOST_BITS_PER_WIDE_INT - bits);
1014 return build_int_cst_wide (type, low, high);
1017 /* Checks that X is integer constant that can be expressed in (unsigned)
1018 HOST_WIDE_INT without loss of precision. */
1020 bool
1021 cst_and_fits_in_hwi (const_tree x)
1023 if (TREE_CODE (x) != INTEGER_CST)
1024 return false;
1026 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1027 return false;
1029 return (TREE_INT_CST_HIGH (x) == 0
1030 || TREE_INT_CST_HIGH (x) == -1);
1033 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1034 are in a list pointed to by VALS. */
1036 tree
1037 build_vector (tree type, tree vals)
1039 tree v = make_node (VECTOR_CST);
1040 int over = 0;
1041 tree link;
1043 TREE_VECTOR_CST_ELTS (v) = vals;
1044 TREE_TYPE (v) = type;
1046 /* Iterate through elements and check for overflow. */
1047 for (link = vals; link; link = TREE_CHAIN (link))
1049 tree value = TREE_VALUE (link);
1051 /* Don't crash if we get an address constant. */
1052 if (!CONSTANT_CLASS_P (value))
1053 continue;
1055 over |= TREE_OVERFLOW (value);
1058 TREE_OVERFLOW (v) = over;
1059 return v;
1062 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1063 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1065 tree
1066 build_vector_from_ctor (tree type, VEC(constructor_elt,gc) *v)
1068 tree list = NULL_TREE;
1069 unsigned HOST_WIDE_INT idx;
1070 tree value;
1072 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1073 list = tree_cons (NULL_TREE, value, list);
1074 return build_vector (type, nreverse (list));
1077 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1078 are in the VEC pointed to by VALS. */
1079 tree
1080 build_constructor (tree type, VEC(constructor_elt,gc) *vals)
1082 tree c = make_node (CONSTRUCTOR);
1083 TREE_TYPE (c) = type;
1084 CONSTRUCTOR_ELTS (c) = vals;
1085 return c;
1088 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1089 INDEX and VALUE. */
1090 tree
1091 build_constructor_single (tree type, tree index, tree value)
1093 VEC(constructor_elt,gc) *v;
1094 constructor_elt *elt;
1095 tree t;
1097 v = VEC_alloc (constructor_elt, gc, 1);
1098 elt = VEC_quick_push (constructor_elt, v, NULL);
1099 elt->index = index;
1100 elt->value = value;
1102 t = build_constructor (type, v);
1103 TREE_CONSTANT (t) = TREE_CONSTANT (value);
1104 return t;
1108 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1109 are in a list pointed to by VALS. */
1110 tree
1111 build_constructor_from_list (tree type, tree vals)
1113 tree t, val;
1114 VEC(constructor_elt,gc) *v = NULL;
1115 bool constant_p = true;
1117 if (vals)
1119 v = VEC_alloc (constructor_elt, gc, list_length (vals));
1120 for (t = vals; t; t = TREE_CHAIN (t))
1122 constructor_elt *elt = VEC_quick_push (constructor_elt, v, NULL);
1123 val = TREE_VALUE (t);
1124 elt->index = TREE_PURPOSE (t);
1125 elt->value = val;
1126 if (!TREE_CONSTANT (val))
1127 constant_p = false;
1131 t = build_constructor (type, v);
1132 TREE_CONSTANT (t) = constant_p;
1133 return t;
1136 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1138 tree
1139 build_fixed (tree type, FIXED_VALUE_TYPE f)
1141 tree v;
1142 FIXED_VALUE_TYPE *fp;
1144 v = make_node (FIXED_CST);
1145 fp = GGC_NEW (FIXED_VALUE_TYPE);
1146 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1148 TREE_TYPE (v) = type;
1149 TREE_FIXED_CST_PTR (v) = fp;
1150 return v;
1153 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1155 tree
1156 build_real (tree type, REAL_VALUE_TYPE d)
1158 tree v;
1159 REAL_VALUE_TYPE *dp;
1160 int overflow = 0;
1162 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1163 Consider doing it via real_convert now. */
1165 v = make_node (REAL_CST);
1166 dp = GGC_NEW (REAL_VALUE_TYPE);
1167 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1169 TREE_TYPE (v) = type;
1170 TREE_REAL_CST_PTR (v) = dp;
1171 TREE_OVERFLOW (v) = overflow;
1172 return v;
1175 /* Return a new REAL_CST node whose type is TYPE
1176 and whose value is the integer value of the INTEGER_CST node I. */
1178 REAL_VALUE_TYPE
1179 real_value_from_int_cst (const_tree type, const_tree i)
1181 REAL_VALUE_TYPE d;
1183 /* Clear all bits of the real value type so that we can later do
1184 bitwise comparisons to see if two values are the same. */
1185 memset (&d, 0, sizeof d);
1187 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1188 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1189 TYPE_UNSIGNED (TREE_TYPE (i)));
1190 return d;
1193 /* Given a tree representing an integer constant I, return a tree
1194 representing the same value as a floating-point constant of type TYPE. */
1196 tree
1197 build_real_from_int_cst (tree type, const_tree i)
1199 tree v;
1200 int overflow = TREE_OVERFLOW (i);
1202 v = build_real (type, real_value_from_int_cst (type, i));
1204 TREE_OVERFLOW (v) |= overflow;
1205 return v;
1208 /* Return a newly constructed STRING_CST node whose value is
1209 the LEN characters at STR.
1210 The TREE_TYPE is not initialized. */
1212 tree
1213 build_string (int len, const char *str)
1215 tree s;
1216 size_t length;
1218 /* Do not waste bytes provided by padding of struct tree_string. */
1219 length = len + offsetof (struct tree_string, str) + 1;
1221 #ifdef GATHER_STATISTICS
1222 tree_node_counts[(int) c_kind]++;
1223 tree_node_sizes[(int) c_kind] += length;
1224 #endif
1226 s = ggc_alloc_tree (length);
1228 memset (s, 0, sizeof (struct tree_common));
1229 TREE_SET_CODE (s, STRING_CST);
1230 TREE_CONSTANT (s) = 1;
1231 TREE_STRING_LENGTH (s) = len;
1232 memcpy (s->string.str, str, len);
1233 s->string.str[len] = '\0';
1235 return s;
1238 /* Return a newly constructed COMPLEX_CST node whose value is
1239 specified by the real and imaginary parts REAL and IMAG.
1240 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1241 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1243 tree
1244 build_complex (tree type, tree real, tree imag)
1246 tree t = make_node (COMPLEX_CST);
1248 TREE_REALPART (t) = real;
1249 TREE_IMAGPART (t) = imag;
1250 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1251 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1252 return t;
1255 /* Return a constant of arithmetic type TYPE which is the
1256 multiplicative identity of the set TYPE. */
1258 tree
1259 build_one_cst (tree type)
1261 switch (TREE_CODE (type))
1263 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1264 case POINTER_TYPE: case REFERENCE_TYPE:
1265 case OFFSET_TYPE:
1266 return build_int_cst (type, 1);
1268 case REAL_TYPE:
1269 return build_real (type, dconst1);
1271 case FIXED_POINT_TYPE:
1272 /* We can only generate 1 for accum types. */
1273 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1274 return build_fixed (type, FCONST1(TYPE_MODE (type)));
1276 case VECTOR_TYPE:
1278 tree scalar, cst;
1279 int i;
1281 scalar = build_one_cst (TREE_TYPE (type));
1283 /* Create 'vect_cst_ = {cst,cst,...,cst}' */
1284 cst = NULL_TREE;
1285 for (i = TYPE_VECTOR_SUBPARTS (type); --i >= 0; )
1286 cst = tree_cons (NULL_TREE, scalar, cst);
1288 return build_vector (type, cst);
1291 case COMPLEX_TYPE:
1292 return build_complex (type,
1293 build_one_cst (TREE_TYPE (type)),
1294 fold_convert (TREE_TYPE (type), integer_zero_node));
1296 default:
1297 gcc_unreachable ();
1301 /* Build a BINFO with LEN language slots. */
1303 tree
1304 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1306 tree t;
1307 size_t length = (offsetof (struct tree_binfo, base_binfos)
1308 + VEC_embedded_size (tree, base_binfos));
1310 #ifdef GATHER_STATISTICS
1311 tree_node_counts[(int) binfo_kind]++;
1312 tree_node_sizes[(int) binfo_kind] += length;
1313 #endif
1315 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
1317 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1319 TREE_SET_CODE (t, TREE_BINFO);
1321 VEC_embedded_init (tree, BINFO_BASE_BINFOS (t), base_binfos);
1323 return t;
1327 /* Build a newly constructed TREE_VEC node of length LEN. */
1329 tree
1330 make_tree_vec_stat (int len MEM_STAT_DECL)
1332 tree t;
1333 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1335 #ifdef GATHER_STATISTICS
1336 tree_node_counts[(int) vec_kind]++;
1337 tree_node_sizes[(int) vec_kind] += length;
1338 #endif
1340 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
1342 memset (t, 0, length);
1344 TREE_SET_CODE (t, TREE_VEC);
1345 TREE_VEC_LENGTH (t) = len;
1347 return t;
1350 /* Return 1 if EXPR is the integer constant zero or a complex constant
1351 of zero. */
1354 integer_zerop (const_tree expr)
1356 STRIP_NOPS (expr);
1358 return ((TREE_CODE (expr) == INTEGER_CST
1359 && TREE_INT_CST_LOW (expr) == 0
1360 && TREE_INT_CST_HIGH (expr) == 0)
1361 || (TREE_CODE (expr) == COMPLEX_CST
1362 && integer_zerop (TREE_REALPART (expr))
1363 && integer_zerop (TREE_IMAGPART (expr))));
1366 /* Return 1 if EXPR is the integer constant one or the corresponding
1367 complex constant. */
1370 integer_onep (const_tree expr)
1372 STRIP_NOPS (expr);
1374 return ((TREE_CODE (expr) == INTEGER_CST
1375 && TREE_INT_CST_LOW (expr) == 1
1376 && TREE_INT_CST_HIGH (expr) == 0)
1377 || (TREE_CODE (expr) == COMPLEX_CST
1378 && integer_onep (TREE_REALPART (expr))
1379 && integer_zerop (TREE_IMAGPART (expr))));
1382 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1383 it contains. Likewise for the corresponding complex constant. */
1386 integer_all_onesp (const_tree expr)
1388 int prec;
1389 int uns;
1391 STRIP_NOPS (expr);
1393 if (TREE_CODE (expr) == COMPLEX_CST
1394 && integer_all_onesp (TREE_REALPART (expr))
1395 && integer_zerop (TREE_IMAGPART (expr)))
1396 return 1;
1398 else if (TREE_CODE (expr) != INTEGER_CST)
1399 return 0;
1401 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1402 if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1403 && TREE_INT_CST_HIGH (expr) == -1)
1404 return 1;
1405 if (!uns)
1406 return 0;
1408 /* Note that using TYPE_PRECISION here is wrong. We care about the
1409 actual bits, not the (arbitrary) range of the type. */
1410 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)));
1411 if (prec >= HOST_BITS_PER_WIDE_INT)
1413 HOST_WIDE_INT high_value;
1414 int shift_amount;
1416 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1418 /* Can not handle precisions greater than twice the host int size. */
1419 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
1420 if (shift_amount == HOST_BITS_PER_WIDE_INT)
1421 /* Shifting by the host word size is undefined according to the ANSI
1422 standard, so we must handle this as a special case. */
1423 high_value = -1;
1424 else
1425 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1427 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1428 && TREE_INT_CST_HIGH (expr) == high_value);
1430 else
1431 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1434 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1435 one bit on). */
1438 integer_pow2p (const_tree expr)
1440 int prec;
1441 HOST_WIDE_INT high, low;
1443 STRIP_NOPS (expr);
1445 if (TREE_CODE (expr) == COMPLEX_CST
1446 && integer_pow2p (TREE_REALPART (expr))
1447 && integer_zerop (TREE_IMAGPART (expr)))
1448 return 1;
1450 if (TREE_CODE (expr) != INTEGER_CST)
1451 return 0;
1453 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1454 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1455 high = TREE_INT_CST_HIGH (expr);
1456 low = TREE_INT_CST_LOW (expr);
1458 /* First clear all bits that are beyond the type's precision in case
1459 we've been sign extended. */
1461 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
1463 else if (prec > HOST_BITS_PER_WIDE_INT)
1464 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1465 else
1467 high = 0;
1468 if (prec < HOST_BITS_PER_WIDE_INT)
1469 low &= ~((HOST_WIDE_INT) (-1) << prec);
1472 if (high == 0 && low == 0)
1473 return 0;
1475 return ((high == 0 && (low & (low - 1)) == 0)
1476 || (low == 0 && (high & (high - 1)) == 0));
1479 /* Return 1 if EXPR is an integer constant other than zero or a
1480 complex constant other than zero. */
1483 integer_nonzerop (const_tree expr)
1485 STRIP_NOPS (expr);
1487 return ((TREE_CODE (expr) == INTEGER_CST
1488 && (TREE_INT_CST_LOW (expr) != 0
1489 || TREE_INT_CST_HIGH (expr) != 0))
1490 || (TREE_CODE (expr) == COMPLEX_CST
1491 && (integer_nonzerop (TREE_REALPART (expr))
1492 || integer_nonzerop (TREE_IMAGPART (expr)))));
1495 /* Return 1 if EXPR is the fixed-point constant zero. */
1498 fixed_zerop (const_tree expr)
1500 return (TREE_CODE (expr) == FIXED_CST
1501 && double_int_zero_p (TREE_FIXED_CST (expr).data));
1504 /* Return the power of two represented by a tree node known to be a
1505 power of two. */
1508 tree_log2 (const_tree expr)
1510 int prec;
1511 HOST_WIDE_INT high, low;
1513 STRIP_NOPS (expr);
1515 if (TREE_CODE (expr) == COMPLEX_CST)
1516 return tree_log2 (TREE_REALPART (expr));
1518 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1519 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1521 high = TREE_INT_CST_HIGH (expr);
1522 low = TREE_INT_CST_LOW (expr);
1524 /* First clear all bits that are beyond the type's precision in case
1525 we've been sign extended. */
1527 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
1529 else if (prec > HOST_BITS_PER_WIDE_INT)
1530 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1531 else
1533 high = 0;
1534 if (prec < HOST_BITS_PER_WIDE_INT)
1535 low &= ~((HOST_WIDE_INT) (-1) << prec);
1538 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
1539 : exact_log2 (low));
1542 /* Similar, but return the largest integer Y such that 2 ** Y is less
1543 than or equal to EXPR. */
1546 tree_floor_log2 (const_tree expr)
1548 int prec;
1549 HOST_WIDE_INT high, low;
1551 STRIP_NOPS (expr);
1553 if (TREE_CODE (expr) == COMPLEX_CST)
1554 return tree_log2 (TREE_REALPART (expr));
1556 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1557 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1559 high = TREE_INT_CST_HIGH (expr);
1560 low = TREE_INT_CST_LOW (expr);
1562 /* First clear all bits that are beyond the type's precision in case
1563 we've been sign extended. Ignore if type's precision hasn't been set
1564 since what we are doing is setting it. */
1566 if (prec == 2 * HOST_BITS_PER_WIDE_INT || prec == 0)
1568 else if (prec > HOST_BITS_PER_WIDE_INT)
1569 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1570 else
1572 high = 0;
1573 if (prec < HOST_BITS_PER_WIDE_INT)
1574 low &= ~((HOST_WIDE_INT) (-1) << prec);
1577 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
1578 : floor_log2 (low));
1581 /* Return 1 if EXPR is the real constant zero. */
1584 real_zerop (const_tree expr)
1586 STRIP_NOPS (expr);
1588 return ((TREE_CODE (expr) == REAL_CST
1589 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0))
1590 || (TREE_CODE (expr) == COMPLEX_CST
1591 && real_zerop (TREE_REALPART (expr))
1592 && real_zerop (TREE_IMAGPART (expr))));
1595 /* Return 1 if EXPR is the real constant one in real or complex form. */
1598 real_onep (const_tree expr)
1600 STRIP_NOPS (expr);
1602 return ((TREE_CODE (expr) == REAL_CST
1603 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1))
1604 || (TREE_CODE (expr) == COMPLEX_CST
1605 && real_onep (TREE_REALPART (expr))
1606 && real_zerop (TREE_IMAGPART (expr))));
1609 /* Return 1 if EXPR is the real constant two. */
1612 real_twop (const_tree expr)
1614 STRIP_NOPS (expr);
1616 return ((TREE_CODE (expr) == REAL_CST
1617 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2))
1618 || (TREE_CODE (expr) == COMPLEX_CST
1619 && real_twop (TREE_REALPART (expr))
1620 && real_zerop (TREE_IMAGPART (expr))));
1623 /* Return 1 if EXPR is the real constant minus one. */
1626 real_minus_onep (const_tree expr)
1628 STRIP_NOPS (expr);
1630 return ((TREE_CODE (expr) == REAL_CST
1631 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1))
1632 || (TREE_CODE (expr) == COMPLEX_CST
1633 && real_minus_onep (TREE_REALPART (expr))
1634 && real_zerop (TREE_IMAGPART (expr))));
1637 /* Nonzero if EXP is a constant or a cast of a constant. */
1640 really_constant_p (const_tree exp)
1642 /* This is not quite the same as STRIP_NOPS. It does more. */
1643 while (CONVERT_EXPR_P (exp)
1644 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1645 exp = TREE_OPERAND (exp, 0);
1646 return TREE_CONSTANT (exp);
1649 /* Return first list element whose TREE_VALUE is ELEM.
1650 Return 0 if ELEM is not in LIST. */
1652 tree
1653 value_member (tree elem, tree list)
1655 while (list)
1657 if (elem == TREE_VALUE (list))
1658 return list;
1659 list = TREE_CHAIN (list);
1661 return NULL_TREE;
1664 /* Return first list element whose TREE_PURPOSE is ELEM.
1665 Return 0 if ELEM is not in LIST. */
1667 tree
1668 purpose_member (const_tree elem, tree list)
1670 while (list)
1672 if (elem == TREE_PURPOSE (list))
1673 return list;
1674 list = TREE_CHAIN (list);
1676 return NULL_TREE;
1679 /* Return nonzero if ELEM is part of the chain CHAIN. */
1682 chain_member (const_tree elem, const_tree chain)
1684 while (chain)
1686 if (elem == chain)
1687 return 1;
1688 chain = TREE_CHAIN (chain);
1691 return 0;
1694 /* Return the length of a chain of nodes chained through TREE_CHAIN.
1695 We expect a null pointer to mark the end of the chain.
1696 This is the Lisp primitive `length'. */
1699 list_length (const_tree t)
1701 const_tree p = t;
1702 #ifdef ENABLE_TREE_CHECKING
1703 const_tree q = t;
1704 #endif
1705 int len = 0;
1707 while (p)
1709 p = TREE_CHAIN (p);
1710 #ifdef ENABLE_TREE_CHECKING
1711 if (len % 2)
1712 q = TREE_CHAIN (q);
1713 gcc_assert (p != q);
1714 #endif
1715 len++;
1718 return len;
1721 /* Returns the number of FIELD_DECLs in TYPE. */
1724 fields_length (const_tree type)
1726 tree t = TYPE_FIELDS (type);
1727 int count = 0;
1729 for (; t; t = TREE_CHAIN (t))
1730 if (TREE_CODE (t) == FIELD_DECL)
1731 ++count;
1733 return count;
1736 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
1737 by modifying the last node in chain 1 to point to chain 2.
1738 This is the Lisp primitive `nconc'. */
1740 tree
1741 chainon (tree op1, tree op2)
1743 tree t1;
1745 if (!op1)
1746 return op2;
1747 if (!op2)
1748 return op1;
1750 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
1751 continue;
1752 TREE_CHAIN (t1) = op2;
1754 #ifdef ENABLE_TREE_CHECKING
1756 tree t2;
1757 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
1758 gcc_assert (t2 != t1);
1760 #endif
1762 return op1;
1765 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
1767 tree
1768 tree_last (tree chain)
1770 tree next;
1771 if (chain)
1772 while ((next = TREE_CHAIN (chain)))
1773 chain = next;
1774 return chain;
1777 /* Reverse the order of elements in the chain T,
1778 and return the new head of the chain (old last element). */
1780 tree
1781 nreverse (tree t)
1783 tree prev = 0, decl, next;
1784 for (decl = t; decl; decl = next)
1786 next = TREE_CHAIN (decl);
1787 TREE_CHAIN (decl) = prev;
1788 prev = decl;
1790 return prev;
1793 /* Return a newly created TREE_LIST node whose
1794 purpose and value fields are PARM and VALUE. */
1796 tree
1797 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
1799 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
1800 TREE_PURPOSE (t) = parm;
1801 TREE_VALUE (t) = value;
1802 return t;
1805 /* Return a newly created TREE_LIST node whose
1806 purpose and value fields are PURPOSE and VALUE
1807 and whose TREE_CHAIN is CHAIN. */
1809 tree
1810 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
1812 tree node;
1814 node = (tree) ggc_alloc_zone_pass_stat (sizeof (struct tree_list), &tree_zone);
1816 memset (node, 0, sizeof (struct tree_common));
1818 #ifdef GATHER_STATISTICS
1819 tree_node_counts[(int) x_kind]++;
1820 tree_node_sizes[(int) x_kind] += sizeof (struct tree_list);
1821 #endif
1823 TREE_SET_CODE (node, TREE_LIST);
1824 TREE_CHAIN (node) = chain;
1825 TREE_PURPOSE (node) = purpose;
1826 TREE_VALUE (node) = value;
1827 return node;
1831 /* Return the size nominally occupied by an object of type TYPE
1832 when it resides in memory. The value is measured in units of bytes,
1833 and its data type is that normally used for type sizes
1834 (which is the first type created by make_signed_type or
1835 make_unsigned_type). */
1837 tree
1838 size_in_bytes (const_tree type)
1840 tree t;
1842 if (type == error_mark_node)
1843 return integer_zero_node;
1845 type = TYPE_MAIN_VARIANT (type);
1846 t = TYPE_SIZE_UNIT (type);
1848 if (t == 0)
1850 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
1851 return size_zero_node;
1854 return t;
1857 /* Return the size of TYPE (in bytes) as a wide integer
1858 or return -1 if the size can vary or is larger than an integer. */
1860 HOST_WIDE_INT
1861 int_size_in_bytes (const_tree type)
1863 tree t;
1865 if (type == error_mark_node)
1866 return 0;
1868 type = TYPE_MAIN_VARIANT (type);
1869 t = TYPE_SIZE_UNIT (type);
1870 if (t == 0
1871 || TREE_CODE (t) != INTEGER_CST
1872 || TREE_INT_CST_HIGH (t) != 0
1873 /* If the result would appear negative, it's too big to represent. */
1874 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
1875 return -1;
1877 return TREE_INT_CST_LOW (t);
1880 /* Return the maximum size of TYPE (in bytes) as a wide integer
1881 or return -1 if the size can vary or is larger than an integer. */
1883 HOST_WIDE_INT
1884 max_int_size_in_bytes (const_tree type)
1886 HOST_WIDE_INT size = -1;
1887 tree size_tree;
1889 /* If this is an array type, check for a possible MAX_SIZE attached. */
1891 if (TREE_CODE (type) == ARRAY_TYPE)
1893 size_tree = TYPE_ARRAY_MAX_SIZE (type);
1895 if (size_tree && host_integerp (size_tree, 1))
1896 size = tree_low_cst (size_tree, 1);
1899 /* If we still haven't been able to get a size, see if the language
1900 can compute a maximum size. */
1902 if (size == -1)
1904 size_tree = lang_hooks.types.max_size (type);
1906 if (size_tree && host_integerp (size_tree, 1))
1907 size = tree_low_cst (size_tree, 1);
1910 return size;
1913 /* Return the bit position of FIELD, in bits from the start of the record.
1914 This is a tree of type bitsizetype. */
1916 tree
1917 bit_position (const_tree field)
1919 return bit_from_pos (DECL_FIELD_OFFSET (field),
1920 DECL_FIELD_BIT_OFFSET (field));
1923 /* Likewise, but return as an integer. It must be representable in
1924 that way (since it could be a signed value, we don't have the
1925 option of returning -1 like int_size_in_byte can. */
1927 HOST_WIDE_INT
1928 int_bit_position (const_tree field)
1930 return tree_low_cst (bit_position (field), 0);
1933 /* Return the byte position of FIELD, in bytes from the start of the record.
1934 This is a tree of type sizetype. */
1936 tree
1937 byte_position (const_tree field)
1939 return byte_from_pos (DECL_FIELD_OFFSET (field),
1940 DECL_FIELD_BIT_OFFSET (field));
1943 /* Likewise, but return as an integer. It must be representable in
1944 that way (since it could be a signed value, we don't have the
1945 option of returning -1 like int_size_in_byte can. */
1947 HOST_WIDE_INT
1948 int_byte_position (const_tree field)
1950 return tree_low_cst (byte_position (field), 0);
1953 /* Return the strictest alignment, in bits, that T is known to have. */
1955 unsigned int
1956 expr_align (const_tree t)
1958 unsigned int align0, align1;
1960 switch (TREE_CODE (t))
1962 CASE_CONVERT: case NON_LVALUE_EXPR:
1963 /* If we have conversions, we know that the alignment of the
1964 object must meet each of the alignments of the types. */
1965 align0 = expr_align (TREE_OPERAND (t, 0));
1966 align1 = TYPE_ALIGN (TREE_TYPE (t));
1967 return MAX (align0, align1);
1969 case GIMPLE_MODIFY_STMT:
1970 /* We should never ask for the alignment of a gimple statement. */
1971 gcc_unreachable ();
1973 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
1974 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
1975 case CLEANUP_POINT_EXPR:
1976 /* These don't change the alignment of an object. */
1977 return expr_align (TREE_OPERAND (t, 0));
1979 case COND_EXPR:
1980 /* The best we can do is say that the alignment is the least aligned
1981 of the two arms. */
1982 align0 = expr_align (TREE_OPERAND (t, 1));
1983 align1 = expr_align (TREE_OPERAND (t, 2));
1984 return MIN (align0, align1);
1986 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
1987 meaningfully, it's always 1. */
1988 case LABEL_DECL: case CONST_DECL:
1989 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
1990 case FUNCTION_DECL:
1991 gcc_assert (DECL_ALIGN (t) != 0);
1992 return DECL_ALIGN (t);
1994 default:
1995 break;
1998 /* Otherwise take the alignment from that of the type. */
1999 return TYPE_ALIGN (TREE_TYPE (t));
2002 /* Return, as a tree node, the number of elements for TYPE (which is an
2003 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2005 tree
2006 array_type_nelts (const_tree type)
2008 tree index_type, min, max;
2010 /* If they did it with unspecified bounds, then we should have already
2011 given an error about it before we got here. */
2012 if (! TYPE_DOMAIN (type))
2013 return error_mark_node;
2015 index_type = TYPE_DOMAIN (type);
2016 min = TYPE_MIN_VALUE (index_type);
2017 max = TYPE_MAX_VALUE (index_type);
2019 return (integer_zerop (min)
2020 ? max
2021 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2024 /* If arg is static -- a reference to an object in static storage -- then
2025 return the object. This is not the same as the C meaning of `static'.
2026 If arg isn't static, return NULL. */
2028 tree
2029 staticp (tree arg)
2031 switch (TREE_CODE (arg))
2033 case FUNCTION_DECL:
2034 /* Nested functions are static, even though taking their address will
2035 involve a trampoline as we unnest the nested function and create
2036 the trampoline on the tree level. */
2037 return arg;
2039 case VAR_DECL:
2040 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2041 && ! DECL_THREAD_LOCAL_P (arg)
2042 && ! DECL_DLLIMPORT_P (arg)
2043 ? arg : NULL);
2045 case CONST_DECL:
2046 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2047 ? arg : NULL);
2049 case CONSTRUCTOR:
2050 return TREE_STATIC (arg) ? arg : NULL;
2052 case LABEL_DECL:
2053 case STRING_CST:
2054 return arg;
2056 case COMPONENT_REF:
2057 /* If the thing being referenced is not a field, then it is
2058 something language specific. */
2059 if (TREE_CODE (TREE_OPERAND (arg, 1)) != FIELD_DECL)
2060 return (*lang_hooks.staticp) (arg);
2062 /* If we are referencing a bitfield, we can't evaluate an
2063 ADDR_EXPR at compile time and so it isn't a constant. */
2064 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2065 return NULL;
2067 return staticp (TREE_OPERAND (arg, 0));
2069 case BIT_FIELD_REF:
2070 return NULL;
2072 case MISALIGNED_INDIRECT_REF:
2073 case ALIGN_INDIRECT_REF:
2074 case INDIRECT_REF:
2075 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2077 case ARRAY_REF:
2078 case ARRAY_RANGE_REF:
2079 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2080 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2081 return staticp (TREE_OPERAND (arg, 0));
2082 else
2083 return false;
2085 default:
2086 if ((unsigned int) TREE_CODE (arg)
2087 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
2088 return lang_hooks.staticp (arg);
2089 else
2090 return NULL;
2097 /* Return whether OP is a DECL whose address is function-invariant. */
2099 bool
2100 decl_address_invariant_p (const_tree op)
2102 /* The conditions below are slightly less strict than the one in
2103 staticp. */
2105 switch (TREE_CODE (op))
2107 case PARM_DECL:
2108 case RESULT_DECL:
2109 case LABEL_DECL:
2110 case FUNCTION_DECL:
2111 return true;
2113 case VAR_DECL:
2114 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2115 && !DECL_DLLIMPORT_P (op))
2116 || DECL_THREAD_LOCAL_P (op)
2117 || DECL_CONTEXT (op) == current_function_decl
2118 || decl_function_context (op) == current_function_decl)
2119 return true;
2120 break;
2122 case CONST_DECL:
2123 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2124 || decl_function_context (op) == current_function_decl)
2125 return true;
2126 break;
2128 default:
2129 break;
2132 return false;
2136 /* Return true if T is function-invariant (internal function, does
2137 not handle arithmetic; that's handled in skip_simple_arithmetic and
2138 tree_invariant_p). */
2140 static bool tree_invariant_p (tree t);
2142 static bool
2143 tree_invariant_p_1 (tree t)
2145 tree op;
2147 if (TREE_CONSTANT (t)
2148 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2149 return true;
2151 switch (TREE_CODE (t))
2153 case SAVE_EXPR:
2154 return true;
2156 case ADDR_EXPR:
2157 op = TREE_OPERAND (t, 0);
2158 while (handled_component_p (op))
2160 switch (TREE_CODE (op))
2162 case ARRAY_REF:
2163 case ARRAY_RANGE_REF:
2164 if (!tree_invariant_p (TREE_OPERAND (op, 1))
2165 || TREE_OPERAND (op, 2) != NULL_TREE
2166 || TREE_OPERAND (op, 3) != NULL_TREE)
2167 return false;
2168 break;
2170 case COMPONENT_REF:
2171 if (TREE_OPERAND (op, 2) != NULL_TREE)
2172 return false;
2173 break;
2175 default:;
2177 op = TREE_OPERAND (op, 0);
2180 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2182 default:
2183 break;
2186 return false;
2189 /* Return true if T is function-invariant. */
2191 static bool
2192 tree_invariant_p (tree t)
2194 tree inner = skip_simple_arithmetic (t);
2195 return tree_invariant_p_1 (inner);
2198 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2199 Do this to any expression which may be used in more than one place,
2200 but must be evaluated only once.
2202 Normally, expand_expr would reevaluate the expression each time.
2203 Calling save_expr produces something that is evaluated and recorded
2204 the first time expand_expr is called on it. Subsequent calls to
2205 expand_expr just reuse the recorded value.
2207 The call to expand_expr that generates code that actually computes
2208 the value is the first call *at compile time*. Subsequent calls
2209 *at compile time* generate code to use the saved value.
2210 This produces correct result provided that *at run time* control
2211 always flows through the insns made by the first expand_expr
2212 before reaching the other places where the save_expr was evaluated.
2213 You, the caller of save_expr, must make sure this is so.
2215 Constants, and certain read-only nodes, are returned with no
2216 SAVE_EXPR because that is safe. Expressions containing placeholders
2217 are not touched; see tree.def for an explanation of what these
2218 are used for. */
2220 tree
2221 save_expr (tree expr)
2223 tree t = fold (expr);
2224 tree inner;
2226 /* If the tree evaluates to a constant, then we don't want to hide that
2227 fact (i.e. this allows further folding, and direct checks for constants).
2228 However, a read-only object that has side effects cannot be bypassed.
2229 Since it is no problem to reevaluate literals, we just return the
2230 literal node. */
2231 inner = skip_simple_arithmetic (t);
2232 if (TREE_CODE (inner) == ERROR_MARK)
2233 return inner;
2235 if (tree_invariant_p_1 (inner))
2236 return t;
2238 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2239 it means that the size or offset of some field of an object depends on
2240 the value within another field.
2242 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2243 and some variable since it would then need to be both evaluated once and
2244 evaluated more than once. Front-ends must assure this case cannot
2245 happen by surrounding any such subexpressions in their own SAVE_EXPR
2246 and forcing evaluation at the proper time. */
2247 if (contains_placeholder_p (inner))
2248 return t;
2250 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
2252 /* This expression might be placed ahead of a jump to ensure that the
2253 value was computed on both sides of the jump. So make sure it isn't
2254 eliminated as dead. */
2255 TREE_SIDE_EFFECTS (t) = 1;
2256 return t;
2259 /* Look inside EXPR and into any simple arithmetic operations. Return
2260 the innermost non-arithmetic node. */
2262 tree
2263 skip_simple_arithmetic (tree expr)
2265 tree inner;
2267 /* We don't care about whether this can be used as an lvalue in this
2268 context. */
2269 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
2270 expr = TREE_OPERAND (expr, 0);
2272 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
2273 a constant, it will be more efficient to not make another SAVE_EXPR since
2274 it will allow better simplification and GCSE will be able to merge the
2275 computations if they actually occur. */
2276 inner = expr;
2277 while (1)
2279 if (UNARY_CLASS_P (inner))
2280 inner = TREE_OPERAND (inner, 0);
2281 else if (BINARY_CLASS_P (inner))
2283 if (tree_invariant_p (TREE_OPERAND (inner, 1)))
2284 inner = TREE_OPERAND (inner, 0);
2285 else if (tree_invariant_p (TREE_OPERAND (inner, 0)))
2286 inner = TREE_OPERAND (inner, 1);
2287 else
2288 break;
2290 else
2291 break;
2294 return inner;
2297 /* Return which tree structure is used by T. */
2299 enum tree_node_structure_enum
2300 tree_node_structure (const_tree t)
2302 const enum tree_code code = TREE_CODE (t);
2304 switch (TREE_CODE_CLASS (code))
2306 case tcc_declaration:
2308 switch (code)
2310 case FIELD_DECL:
2311 return TS_FIELD_DECL;
2312 case PARM_DECL:
2313 return TS_PARM_DECL;
2314 case VAR_DECL:
2315 return TS_VAR_DECL;
2316 case LABEL_DECL:
2317 return TS_LABEL_DECL;
2318 case RESULT_DECL:
2319 return TS_RESULT_DECL;
2320 case CONST_DECL:
2321 return TS_CONST_DECL;
2322 case TYPE_DECL:
2323 return TS_TYPE_DECL;
2324 case FUNCTION_DECL:
2325 return TS_FUNCTION_DECL;
2326 case SYMBOL_MEMORY_TAG:
2327 case NAME_MEMORY_TAG:
2328 case MEMORY_PARTITION_TAG:
2329 return TS_MEMORY_TAG;
2330 default:
2331 return TS_DECL_NON_COMMON;
2334 case tcc_type:
2335 return TS_TYPE;
2336 case tcc_reference:
2337 case tcc_comparison:
2338 case tcc_unary:
2339 case tcc_binary:
2340 case tcc_expression:
2341 case tcc_statement:
2342 case tcc_vl_exp:
2343 return TS_EXP;
2344 case tcc_gimple_stmt:
2345 return TS_GIMPLE_STATEMENT;
2346 default: /* tcc_constant and tcc_exceptional */
2347 break;
2349 switch (code)
2351 /* tcc_constant cases. */
2352 case INTEGER_CST: return TS_INT_CST;
2353 case REAL_CST: return TS_REAL_CST;
2354 case FIXED_CST: return TS_FIXED_CST;
2355 case COMPLEX_CST: return TS_COMPLEX;
2356 case VECTOR_CST: return TS_VECTOR;
2357 case STRING_CST: return TS_STRING;
2358 /* tcc_exceptional cases. */
2359 /* FIXME tuples: eventually this should be TS_BASE. For now, nothing
2360 returns TS_BASE. */
2361 case ERROR_MARK: return TS_COMMON;
2362 case IDENTIFIER_NODE: return TS_IDENTIFIER;
2363 case TREE_LIST: return TS_LIST;
2364 case TREE_VEC: return TS_VEC;
2365 case PHI_NODE: return TS_PHI_NODE;
2366 case SSA_NAME: return TS_SSA_NAME;
2367 case PLACEHOLDER_EXPR: return TS_COMMON;
2368 case STATEMENT_LIST: return TS_STATEMENT_LIST;
2369 case BLOCK: return TS_BLOCK;
2370 case CONSTRUCTOR: return TS_CONSTRUCTOR;
2371 case TREE_BINFO: return TS_BINFO;
2372 case VALUE_HANDLE: return TS_VALUE_HANDLE;
2373 case OMP_CLAUSE: return TS_OMP_CLAUSE;
2375 default:
2376 gcc_unreachable ();
2380 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
2381 or offset that depends on a field within a record. */
2383 bool
2384 contains_placeholder_p (const_tree exp)
2386 enum tree_code code;
2388 if (!exp)
2389 return 0;
2391 code = TREE_CODE (exp);
2392 if (code == PLACEHOLDER_EXPR)
2393 return 1;
2395 switch (TREE_CODE_CLASS (code))
2397 case tcc_reference:
2398 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
2399 position computations since they will be converted into a
2400 WITH_RECORD_EXPR involving the reference, which will assume
2401 here will be valid. */
2402 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2404 case tcc_exceptional:
2405 if (code == TREE_LIST)
2406 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
2407 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
2408 break;
2410 case tcc_unary:
2411 case tcc_binary:
2412 case tcc_comparison:
2413 case tcc_expression:
2414 switch (code)
2416 case COMPOUND_EXPR:
2417 /* Ignoring the first operand isn't quite right, but works best. */
2418 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
2420 case COND_EXPR:
2421 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2422 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
2423 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
2425 default:
2426 break;
2429 switch (TREE_CODE_LENGTH (code))
2431 case 1:
2432 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2433 case 2:
2434 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2435 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
2436 default:
2437 return 0;
2440 case tcc_vl_exp:
2441 switch (code)
2443 case CALL_EXPR:
2445 const_tree arg;
2446 const_call_expr_arg_iterator iter;
2447 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
2448 if (CONTAINS_PLACEHOLDER_P (arg))
2449 return 1;
2450 return 0;
2452 default:
2453 return 0;
2456 default:
2457 return 0;
2459 return 0;
2462 /* Return true if any part of the computation of TYPE involves a
2463 PLACEHOLDER_EXPR. This includes size, bounds, qualifiers
2464 (for QUAL_UNION_TYPE) and field positions. */
2466 static bool
2467 type_contains_placeholder_1 (const_tree type)
2469 /* If the size contains a placeholder or the parent type (component type in
2470 the case of arrays) type involves a placeholder, this type does. */
2471 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
2472 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
2473 || (TREE_TYPE (type) != 0
2474 && type_contains_placeholder_p (TREE_TYPE (type))))
2475 return true;
2477 /* Now do type-specific checks. Note that the last part of the check above
2478 greatly limits what we have to do below. */
2479 switch (TREE_CODE (type))
2481 case VOID_TYPE:
2482 case COMPLEX_TYPE:
2483 case ENUMERAL_TYPE:
2484 case BOOLEAN_TYPE:
2485 case POINTER_TYPE:
2486 case OFFSET_TYPE:
2487 case REFERENCE_TYPE:
2488 case METHOD_TYPE:
2489 case FUNCTION_TYPE:
2490 case VECTOR_TYPE:
2491 return false;
2493 case INTEGER_TYPE:
2494 case REAL_TYPE:
2495 case FIXED_POINT_TYPE:
2496 /* Here we just check the bounds. */
2497 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
2498 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
2500 case ARRAY_TYPE:
2501 /* We're already checked the component type (TREE_TYPE), so just check
2502 the index type. */
2503 return type_contains_placeholder_p (TYPE_DOMAIN (type));
2505 case RECORD_TYPE:
2506 case UNION_TYPE:
2507 case QUAL_UNION_TYPE:
2509 tree field;
2511 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2512 if (TREE_CODE (field) == FIELD_DECL
2513 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
2514 || (TREE_CODE (type) == QUAL_UNION_TYPE
2515 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
2516 || type_contains_placeholder_p (TREE_TYPE (field))))
2517 return true;
2519 return false;
2522 default:
2523 gcc_unreachable ();
2527 bool
2528 type_contains_placeholder_p (tree type)
2530 bool result;
2532 /* If the contains_placeholder_bits field has been initialized,
2533 then we know the answer. */
2534 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
2535 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
2537 /* Indicate that we've seen this type node, and the answer is false.
2538 This is what we want to return if we run into recursion via fields. */
2539 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
2541 /* Compute the real value. */
2542 result = type_contains_placeholder_1 (type);
2544 /* Store the real value. */
2545 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
2547 return result;
2550 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
2551 return a tree with all occurrences of references to F in a
2552 PLACEHOLDER_EXPR replaced by R. Note that we assume here that EXP
2553 contains only arithmetic expressions or a CALL_EXPR with a
2554 PLACEHOLDER_EXPR occurring only in its arglist. */
2556 tree
2557 substitute_in_expr (tree exp, tree f, tree r)
2559 enum tree_code code = TREE_CODE (exp);
2560 tree op0, op1, op2, op3;
2561 tree new, inner;
2563 /* We handle TREE_LIST and COMPONENT_REF separately. */
2564 if (code == TREE_LIST)
2566 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
2567 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
2568 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2569 return exp;
2571 return tree_cons (TREE_PURPOSE (exp), op1, op0);
2573 else if (code == COMPONENT_REF)
2575 /* If this expression is getting a value from a PLACEHOLDER_EXPR
2576 and it is the right field, replace it with R. */
2577 for (inner = TREE_OPERAND (exp, 0);
2578 REFERENCE_CLASS_P (inner);
2579 inner = TREE_OPERAND (inner, 0))
2581 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2582 && TREE_OPERAND (exp, 1) == f)
2583 return r;
2585 /* If this expression hasn't been completed let, leave it alone. */
2586 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && TREE_TYPE (inner) == 0)
2587 return exp;
2589 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2590 if (op0 == TREE_OPERAND (exp, 0))
2591 return exp;
2593 new = fold_build3 (COMPONENT_REF, TREE_TYPE (exp),
2594 op0, TREE_OPERAND (exp, 1), NULL_TREE);
2596 else
2597 switch (TREE_CODE_CLASS (code))
2599 case tcc_constant:
2600 case tcc_declaration:
2601 return exp;
2603 case tcc_exceptional:
2604 case tcc_unary:
2605 case tcc_binary:
2606 case tcc_comparison:
2607 case tcc_expression:
2608 case tcc_reference:
2609 switch (TREE_CODE_LENGTH (code))
2611 case 0:
2612 return exp;
2614 case 1:
2615 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2616 if (op0 == TREE_OPERAND (exp, 0))
2617 return exp;
2619 new = fold_build1 (code, TREE_TYPE (exp), op0);
2620 break;
2622 case 2:
2623 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2624 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2626 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2627 return exp;
2629 new = fold_build2 (code, TREE_TYPE (exp), op0, op1);
2630 break;
2632 case 3:
2633 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2634 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2635 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
2637 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2638 && op2 == TREE_OPERAND (exp, 2))
2639 return exp;
2641 new = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
2642 break;
2644 case 4:
2645 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2646 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2647 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
2648 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
2650 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2651 && op2 == TREE_OPERAND (exp, 2)
2652 && op3 == TREE_OPERAND (exp, 3))
2653 return exp;
2655 new = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
2656 break;
2658 default:
2659 gcc_unreachable ();
2661 break;
2663 case tcc_vl_exp:
2665 tree copy = NULL_TREE;
2666 int i;
2668 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
2670 tree op = TREE_OPERAND (exp, i);
2671 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
2672 if (new_op != op)
2674 if (!copy)
2675 copy = copy_node (exp);
2676 TREE_OPERAND (copy, i) = new_op;
2680 if (copy)
2681 new = fold (copy);
2682 else
2683 return exp;
2685 break;
2687 default:
2688 gcc_unreachable ();
2691 TREE_READONLY (new) = TREE_READONLY (exp);
2692 return new;
2695 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
2696 for it within OBJ, a tree that is an object or a chain of references. */
2698 tree
2699 substitute_placeholder_in_expr (tree exp, tree obj)
2701 enum tree_code code = TREE_CODE (exp);
2702 tree op0, op1, op2, op3;
2704 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
2705 in the chain of OBJ. */
2706 if (code == PLACEHOLDER_EXPR)
2708 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
2709 tree elt;
2711 for (elt = obj; elt != 0;
2712 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
2713 || TREE_CODE (elt) == COND_EXPR)
2714 ? TREE_OPERAND (elt, 1)
2715 : (REFERENCE_CLASS_P (elt)
2716 || UNARY_CLASS_P (elt)
2717 || BINARY_CLASS_P (elt)
2718 || VL_EXP_CLASS_P (elt)
2719 || EXPRESSION_CLASS_P (elt))
2720 ? TREE_OPERAND (elt, 0) : 0))
2721 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
2722 return elt;
2724 for (elt = obj; elt != 0;
2725 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
2726 || TREE_CODE (elt) == COND_EXPR)
2727 ? TREE_OPERAND (elt, 1)
2728 : (REFERENCE_CLASS_P (elt)
2729 || UNARY_CLASS_P (elt)
2730 || BINARY_CLASS_P (elt)
2731 || VL_EXP_CLASS_P (elt)
2732 || EXPRESSION_CLASS_P (elt))
2733 ? TREE_OPERAND (elt, 0) : 0))
2734 if (POINTER_TYPE_P (TREE_TYPE (elt))
2735 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
2736 == need_type))
2737 return fold_build1 (INDIRECT_REF, need_type, elt);
2739 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
2740 survives until RTL generation, there will be an error. */
2741 return exp;
2744 /* TREE_LIST is special because we need to look at TREE_VALUE
2745 and TREE_CHAIN, not TREE_OPERANDS. */
2746 else if (code == TREE_LIST)
2748 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
2749 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
2750 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2751 return exp;
2753 return tree_cons (TREE_PURPOSE (exp), op1, op0);
2755 else
2756 switch (TREE_CODE_CLASS (code))
2758 case tcc_constant:
2759 case tcc_declaration:
2760 return exp;
2762 case tcc_exceptional:
2763 case tcc_unary:
2764 case tcc_binary:
2765 case tcc_comparison:
2766 case tcc_expression:
2767 case tcc_reference:
2768 case tcc_statement:
2769 switch (TREE_CODE_LENGTH (code))
2771 case 0:
2772 return exp;
2774 case 1:
2775 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2776 if (op0 == TREE_OPERAND (exp, 0))
2777 return exp;
2778 else
2779 return fold_build1 (code, TREE_TYPE (exp), op0);
2781 case 2:
2782 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2783 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2785 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2786 return exp;
2787 else
2788 return fold_build2 (code, TREE_TYPE (exp), op0, op1);
2790 case 3:
2791 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2792 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2793 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
2795 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2796 && op2 == TREE_OPERAND (exp, 2))
2797 return exp;
2798 else
2799 return fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
2801 case 4:
2802 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2803 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2804 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
2805 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
2807 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2808 && op2 == TREE_OPERAND (exp, 2)
2809 && op3 == TREE_OPERAND (exp, 3))
2810 return exp;
2811 else
2812 return fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
2814 default:
2815 gcc_unreachable ();
2817 break;
2819 case tcc_vl_exp:
2821 tree copy = NULL_TREE;
2822 int i;
2824 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
2826 tree op = TREE_OPERAND (exp, i);
2827 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
2828 if (new_op != op)
2830 if (!copy)
2831 copy = copy_node (exp);
2832 TREE_OPERAND (copy, i) = new_op;
2836 if (copy)
2837 return fold (copy);
2838 else
2839 return exp;
2842 default:
2843 gcc_unreachable ();
2847 /* Stabilize a reference so that we can use it any number of times
2848 without causing its operands to be evaluated more than once.
2849 Returns the stabilized reference. This works by means of save_expr,
2850 so see the caveats in the comments about save_expr.
2852 Also allows conversion expressions whose operands are references.
2853 Any other kind of expression is returned unchanged. */
2855 tree
2856 stabilize_reference (tree ref)
2858 tree result;
2859 enum tree_code code = TREE_CODE (ref);
2861 switch (code)
2863 case VAR_DECL:
2864 case PARM_DECL:
2865 case RESULT_DECL:
2866 /* No action is needed in this case. */
2867 return ref;
2869 CASE_CONVERT:
2870 case FLOAT_EXPR:
2871 case FIX_TRUNC_EXPR:
2872 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2873 break;
2875 case INDIRECT_REF:
2876 result = build_nt (INDIRECT_REF,
2877 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2878 break;
2880 case COMPONENT_REF:
2881 result = build_nt (COMPONENT_REF,
2882 stabilize_reference (TREE_OPERAND (ref, 0)),
2883 TREE_OPERAND (ref, 1), NULL_TREE);
2884 break;
2886 case BIT_FIELD_REF:
2887 result = build_nt (BIT_FIELD_REF,
2888 stabilize_reference (TREE_OPERAND (ref, 0)),
2889 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2890 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2891 break;
2893 case ARRAY_REF:
2894 result = build_nt (ARRAY_REF,
2895 stabilize_reference (TREE_OPERAND (ref, 0)),
2896 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2897 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
2898 break;
2900 case ARRAY_RANGE_REF:
2901 result = build_nt (ARRAY_RANGE_REF,
2902 stabilize_reference (TREE_OPERAND (ref, 0)),
2903 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2904 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
2905 break;
2907 case COMPOUND_EXPR:
2908 /* We cannot wrap the first expression in a SAVE_EXPR, as then
2909 it wouldn't be ignored. This matters when dealing with
2910 volatiles. */
2911 return stabilize_reference_1 (ref);
2913 /* If arg isn't a kind of lvalue we recognize, make no change.
2914 Caller should recognize the error for an invalid lvalue. */
2915 default:
2916 return ref;
2918 case ERROR_MARK:
2919 return error_mark_node;
2922 TREE_TYPE (result) = TREE_TYPE (ref);
2923 TREE_READONLY (result) = TREE_READONLY (ref);
2924 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
2925 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
2927 return result;
2930 /* Subroutine of stabilize_reference; this is called for subtrees of
2931 references. Any expression with side-effects must be put in a SAVE_EXPR
2932 to ensure that it is only evaluated once.
2934 We don't put SAVE_EXPR nodes around everything, because assigning very
2935 simple expressions to temporaries causes us to miss good opportunities
2936 for optimizations. Among other things, the opportunity to fold in the
2937 addition of a constant into an addressing mode often gets lost, e.g.
2938 "y[i+1] += x;". In general, we take the approach that we should not make
2939 an assignment unless we are forced into it - i.e., that any non-side effect
2940 operator should be allowed, and that cse should take care of coalescing
2941 multiple utterances of the same expression should that prove fruitful. */
2943 tree
2944 stabilize_reference_1 (tree e)
2946 tree result;
2947 enum tree_code code = TREE_CODE (e);
2949 /* We cannot ignore const expressions because it might be a reference
2950 to a const array but whose index contains side-effects. But we can
2951 ignore things that are actual constant or that already have been
2952 handled by this function. */
2954 if (tree_invariant_p (e))
2955 return e;
2957 switch (TREE_CODE_CLASS (code))
2959 case tcc_exceptional:
2960 case tcc_type:
2961 case tcc_declaration:
2962 case tcc_comparison:
2963 case tcc_statement:
2964 case tcc_expression:
2965 case tcc_reference:
2966 case tcc_vl_exp:
2967 /* If the expression has side-effects, then encase it in a SAVE_EXPR
2968 so that it will only be evaluated once. */
2969 /* The reference (r) and comparison (<) classes could be handled as
2970 below, but it is generally faster to only evaluate them once. */
2971 if (TREE_SIDE_EFFECTS (e))
2972 return save_expr (e);
2973 return e;
2975 case tcc_constant:
2976 /* Constants need no processing. In fact, we should never reach
2977 here. */
2978 return e;
2980 case tcc_binary:
2981 /* Division is slow and tends to be compiled with jumps,
2982 especially the division by powers of 2 that is often
2983 found inside of an array reference. So do it just once. */
2984 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
2985 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
2986 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
2987 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
2988 return save_expr (e);
2989 /* Recursively stabilize each operand. */
2990 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
2991 stabilize_reference_1 (TREE_OPERAND (e, 1)));
2992 break;
2994 case tcc_unary:
2995 /* Recursively stabilize each operand. */
2996 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
2997 break;
2999 default:
3000 gcc_unreachable ();
3003 TREE_TYPE (result) = TREE_TYPE (e);
3004 TREE_READONLY (result) = TREE_READONLY (e);
3005 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3006 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3008 return result;
3011 /* Low-level constructors for expressions. */
3013 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
3014 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
3016 void
3017 recompute_tree_invariant_for_addr_expr (tree t)
3019 tree node;
3020 bool tc = true, se = false;
3022 /* We started out assuming this address is both invariant and constant, but
3023 does not have side effects. Now go down any handled components and see if
3024 any of them involve offsets that are either non-constant or non-invariant.
3025 Also check for side-effects.
3027 ??? Note that this code makes no attempt to deal with the case where
3028 taking the address of something causes a copy due to misalignment. */
3030 #define UPDATE_FLAGS(NODE) \
3031 do { tree _node = (NODE); \
3032 if (_node && !TREE_CONSTANT (_node)) tc = false; \
3033 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3035 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3036 node = TREE_OPERAND (node, 0))
3038 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3039 array reference (probably made temporarily by the G++ front end),
3040 so ignore all the operands. */
3041 if ((TREE_CODE (node) == ARRAY_REF
3042 || TREE_CODE (node) == ARRAY_RANGE_REF)
3043 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
3045 UPDATE_FLAGS (TREE_OPERAND (node, 1));
3046 if (TREE_OPERAND (node, 2))
3047 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3048 if (TREE_OPERAND (node, 3))
3049 UPDATE_FLAGS (TREE_OPERAND (node, 3));
3051 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
3052 FIELD_DECL, apparently. The G++ front end can put something else
3053 there, at least temporarily. */
3054 else if (TREE_CODE (node) == COMPONENT_REF
3055 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
3057 if (TREE_OPERAND (node, 2))
3058 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3060 else if (TREE_CODE (node) == BIT_FIELD_REF)
3061 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3064 node = lang_hooks.expr_to_decl (node, &tc, &se);
3066 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
3067 the address, since &(*a)->b is a form of addition. If it's a constant, the
3068 address is constant too. If it's a decl, its address is constant if the
3069 decl is static. Everything else is not constant and, furthermore,
3070 taking the address of a volatile variable is not volatile. */
3071 if (TREE_CODE (node) == INDIRECT_REF)
3072 UPDATE_FLAGS (TREE_OPERAND (node, 0));
3073 else if (CONSTANT_CLASS_P (node))
3075 else if (DECL_P (node))
3076 tc &= (staticp (node) != NULL_TREE);
3077 else
3079 tc = false;
3080 se |= TREE_SIDE_EFFECTS (node);
3084 TREE_CONSTANT (t) = tc;
3085 TREE_SIDE_EFFECTS (t) = se;
3086 #undef UPDATE_FLAGS
3089 /* Build an expression of code CODE, data type TYPE, and operands as
3090 specified. Expressions and reference nodes can be created this way.
3091 Constants, decls, types and misc nodes cannot be.
3093 We define 5 non-variadic functions, from 0 to 4 arguments. This is
3094 enough for all extant tree codes. */
3096 tree
3097 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
3099 tree t;
3101 gcc_assert (TREE_CODE_LENGTH (code) == 0);
3103 t = make_node_stat (code PASS_MEM_STAT);
3104 TREE_TYPE (t) = tt;
3106 return t;
3109 tree
3110 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
3112 int length = sizeof (struct tree_exp);
3113 #ifdef GATHER_STATISTICS
3114 tree_node_kind kind;
3115 #endif
3116 tree t;
3118 #ifdef GATHER_STATISTICS
3119 switch (TREE_CODE_CLASS (code))
3121 case tcc_statement: /* an expression with side effects */
3122 kind = s_kind;
3123 break;
3124 case tcc_reference: /* a reference */
3125 kind = r_kind;
3126 break;
3127 default:
3128 kind = e_kind;
3129 break;
3132 tree_node_counts[(int) kind]++;
3133 tree_node_sizes[(int) kind] += length;
3134 #endif
3136 gcc_assert (TREE_CODE_LENGTH (code) == 1);
3138 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
3140 memset (t, 0, sizeof (struct tree_common));
3142 TREE_SET_CODE (t, code);
3144 TREE_TYPE (t) = type;
3145 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
3146 TREE_OPERAND (t, 0) = node;
3147 TREE_BLOCK (t) = NULL_TREE;
3148 if (node && !TYPE_P (node))
3150 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
3151 TREE_READONLY (t) = TREE_READONLY (node);
3154 if (TREE_CODE_CLASS (code) == tcc_statement)
3155 TREE_SIDE_EFFECTS (t) = 1;
3156 else switch (code)
3158 case VA_ARG_EXPR:
3159 /* All of these have side-effects, no matter what their
3160 operands are. */
3161 TREE_SIDE_EFFECTS (t) = 1;
3162 TREE_READONLY (t) = 0;
3163 break;
3165 case MISALIGNED_INDIRECT_REF:
3166 case ALIGN_INDIRECT_REF:
3167 case INDIRECT_REF:
3168 /* Whether a dereference is readonly has nothing to do with whether
3169 its operand is readonly. */
3170 TREE_READONLY (t) = 0;
3171 break;
3173 case ADDR_EXPR:
3174 if (node)
3175 recompute_tree_invariant_for_addr_expr (t);
3176 break;
3178 default:
3179 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
3180 && node && !TYPE_P (node)
3181 && TREE_CONSTANT (node))
3182 TREE_CONSTANT (t) = 1;
3183 if (TREE_CODE_CLASS (code) == tcc_reference
3184 && node && TREE_THIS_VOLATILE (node))
3185 TREE_THIS_VOLATILE (t) = 1;
3186 break;
3189 return t;
3192 #define PROCESS_ARG(N) \
3193 do { \
3194 TREE_OPERAND (t, N) = arg##N; \
3195 if (arg##N &&!TYPE_P (arg##N)) \
3197 if (TREE_SIDE_EFFECTS (arg##N)) \
3198 side_effects = 1; \
3199 if (!TREE_READONLY (arg##N)) \
3200 read_only = 0; \
3201 if (!TREE_CONSTANT (arg##N)) \
3202 constant = 0; \
3204 } while (0)
3206 tree
3207 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
3209 bool constant, read_only, side_effects;
3210 tree t;
3212 gcc_assert (TREE_CODE_LENGTH (code) == 2);
3214 #if 1
3215 /* FIXME tuples: Statement's aren't expressions! */
3216 if (code == GIMPLE_MODIFY_STMT)
3217 return build_gimple_modify_stmt_stat (arg0, arg1 PASS_MEM_STAT);
3218 #else
3219 /* Must use build_gimple_modify_stmt to construct GIMPLE_MODIFY_STMTs. */
3220 gcc_assert (code != GIMPLE_MODIFY_STMT);
3221 #endif
3223 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
3224 && arg0 && arg1 && tt && POINTER_TYPE_P (tt))
3225 gcc_assert (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST);
3227 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
3228 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
3229 && INTEGRAL_TYPE_P (TREE_TYPE (arg1))
3230 && useless_type_conversion_p (sizetype, TREE_TYPE (arg1)));
3232 t = make_node_stat (code PASS_MEM_STAT);
3233 TREE_TYPE (t) = tt;
3235 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
3236 result based on those same flags for the arguments. But if the
3237 arguments aren't really even `tree' expressions, we shouldn't be trying
3238 to do this. */
3240 /* Expressions without side effects may be constant if their
3241 arguments are as well. */
3242 constant = (TREE_CODE_CLASS (code) == tcc_comparison
3243 || TREE_CODE_CLASS (code) == tcc_binary);
3244 read_only = 1;
3245 side_effects = TREE_SIDE_EFFECTS (t);
3247 PROCESS_ARG(0);
3248 PROCESS_ARG(1);
3250 TREE_READONLY (t) = read_only;
3251 TREE_CONSTANT (t) = constant;
3252 TREE_SIDE_EFFECTS (t) = side_effects;
3253 TREE_THIS_VOLATILE (t)
3254 = (TREE_CODE_CLASS (code) == tcc_reference
3255 && arg0 && TREE_THIS_VOLATILE (arg0));
3257 return t;
3261 /* Build a GIMPLE_MODIFY_STMT node. This tree code doesn't have a
3262 type, so we can't use build2 (a.k.a. build2_stat). */
3264 tree
3265 build_gimple_modify_stmt_stat (tree arg0, tree arg1 MEM_STAT_DECL)
3267 tree t;
3269 t = make_node_stat (GIMPLE_MODIFY_STMT PASS_MEM_STAT);
3270 /* ?? We don't care about setting flags for tuples... */
3271 GIMPLE_STMT_OPERAND (t, 0) = arg0;
3272 GIMPLE_STMT_OPERAND (t, 1) = arg1;
3273 return t;
3276 tree
3277 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3278 tree arg2 MEM_STAT_DECL)
3280 bool constant, read_only, side_effects;
3281 tree t;
3283 gcc_assert (TREE_CODE_LENGTH (code) == 3);
3284 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3286 t = make_node_stat (code PASS_MEM_STAT);
3287 TREE_TYPE (t) = tt;
3289 /* As a special exception, if COND_EXPR has NULL branches, we
3290 assume that it is a gimple statement and always consider
3291 it to have side effects. */
3292 if (code == COND_EXPR
3293 && tt == void_type_node
3294 && arg1 == NULL_TREE
3295 && arg2 == NULL_TREE)
3296 side_effects = true;
3297 else
3298 side_effects = TREE_SIDE_EFFECTS (t);
3300 PROCESS_ARG(0);
3301 PROCESS_ARG(1);
3302 PROCESS_ARG(2);
3304 TREE_SIDE_EFFECTS (t) = side_effects;
3305 TREE_THIS_VOLATILE (t)
3306 = (TREE_CODE_CLASS (code) == tcc_reference
3307 && arg0 && TREE_THIS_VOLATILE (arg0));
3309 return t;
3312 tree
3313 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3314 tree arg2, tree arg3 MEM_STAT_DECL)
3316 bool constant, read_only, side_effects;
3317 tree t;
3319 gcc_assert (TREE_CODE_LENGTH (code) == 4);
3321 t = make_node_stat (code PASS_MEM_STAT);
3322 TREE_TYPE (t) = tt;
3324 side_effects = TREE_SIDE_EFFECTS (t);
3326 PROCESS_ARG(0);
3327 PROCESS_ARG(1);
3328 PROCESS_ARG(2);
3329 PROCESS_ARG(3);
3331 TREE_SIDE_EFFECTS (t) = side_effects;
3332 TREE_THIS_VOLATILE (t)
3333 = (TREE_CODE_CLASS (code) == tcc_reference
3334 && arg0 && TREE_THIS_VOLATILE (arg0));
3336 return t;
3339 tree
3340 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3341 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
3343 bool constant, read_only, side_effects;
3344 tree t;
3346 gcc_assert (TREE_CODE_LENGTH (code) == 5);
3348 t = make_node_stat (code PASS_MEM_STAT);
3349 TREE_TYPE (t) = tt;
3351 side_effects = TREE_SIDE_EFFECTS (t);
3353 PROCESS_ARG(0);
3354 PROCESS_ARG(1);
3355 PROCESS_ARG(2);
3356 PROCESS_ARG(3);
3357 PROCESS_ARG(4);
3359 TREE_SIDE_EFFECTS (t) = side_effects;
3360 TREE_THIS_VOLATILE (t)
3361 = (TREE_CODE_CLASS (code) == tcc_reference
3362 && arg0 && TREE_THIS_VOLATILE (arg0));
3364 return t;
3367 tree
3368 build7_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3369 tree arg2, tree arg3, tree arg4, tree arg5,
3370 tree arg6 MEM_STAT_DECL)
3372 bool constant, read_only, side_effects;
3373 tree t;
3375 gcc_assert (code == TARGET_MEM_REF);
3377 t = make_node_stat (code PASS_MEM_STAT);
3378 TREE_TYPE (t) = tt;
3380 side_effects = TREE_SIDE_EFFECTS (t);
3382 PROCESS_ARG(0);
3383 PROCESS_ARG(1);
3384 PROCESS_ARG(2);
3385 PROCESS_ARG(3);
3386 PROCESS_ARG(4);
3387 PROCESS_ARG(5);
3388 PROCESS_ARG(6);
3390 TREE_SIDE_EFFECTS (t) = side_effects;
3391 TREE_THIS_VOLATILE (t) = 0;
3393 return t;
3396 /* Similar except don't specify the TREE_TYPE
3397 and leave the TREE_SIDE_EFFECTS as 0.
3398 It is permissible for arguments to be null,
3399 or even garbage if their values do not matter. */
3401 tree
3402 build_nt (enum tree_code code, ...)
3404 tree t;
3405 int length;
3406 int i;
3407 va_list p;
3409 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3411 va_start (p, code);
3413 t = make_node (code);
3414 length = TREE_CODE_LENGTH (code);
3416 for (i = 0; i < length; i++)
3417 TREE_OPERAND (t, i) = va_arg (p, tree);
3419 va_end (p);
3420 return t;
3423 /* Similar to build_nt, but for creating a CALL_EXPR object with
3424 ARGLIST passed as a list. */
3426 tree
3427 build_nt_call_list (tree fn, tree arglist)
3429 tree t;
3430 int i;
3432 t = build_vl_exp (CALL_EXPR, list_length (arglist) + 3);
3433 CALL_EXPR_FN (t) = fn;
3434 CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
3435 for (i = 0; arglist; arglist = TREE_CHAIN (arglist), i++)
3436 CALL_EXPR_ARG (t, i) = TREE_VALUE (arglist);
3437 return t;
3440 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
3441 We do NOT enter this node in any sort of symbol table.
3443 layout_decl is used to set up the decl's storage layout.
3444 Other slots are initialized to 0 or null pointers. */
3446 tree
3447 build_decl_stat (enum tree_code code, tree name, tree type MEM_STAT_DECL)
3449 tree t;
3451 t = make_node_stat (code PASS_MEM_STAT);
3453 /* if (type == error_mark_node)
3454 type = integer_type_node; */
3455 /* That is not done, deliberately, so that having error_mark_node
3456 as the type can suppress useless errors in the use of this variable. */
3458 DECL_NAME (t) = name;
3459 TREE_TYPE (t) = type;
3461 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
3462 layout_decl (t, 0);
3464 return t;
3467 /* Builds and returns function declaration with NAME and TYPE. */
3469 tree
3470 build_fn_decl (const char *name, tree type)
3472 tree id = get_identifier (name);
3473 tree decl = build_decl (FUNCTION_DECL, id, type);
3475 DECL_EXTERNAL (decl) = 1;
3476 TREE_PUBLIC (decl) = 1;
3477 DECL_ARTIFICIAL (decl) = 1;
3478 TREE_NOTHROW (decl) = 1;
3480 return decl;
3484 /* BLOCK nodes are used to represent the structure of binding contours
3485 and declarations, once those contours have been exited and their contents
3486 compiled. This information is used for outputting debugging info. */
3488 tree
3489 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
3491 tree block = make_node (BLOCK);
3493 BLOCK_VARS (block) = vars;
3494 BLOCK_SUBBLOCKS (block) = subblocks;
3495 BLOCK_SUPERCONTEXT (block) = supercontext;
3496 BLOCK_CHAIN (block) = chain;
3497 return block;
3500 expanded_location
3501 expand_location (source_location loc)
3503 expanded_location xloc;
3504 if (loc == 0)
3506 xloc.file = NULL;
3507 xloc.line = 0;
3508 xloc.column = 0;
3510 else
3512 const struct line_map *map = linemap_lookup (line_table, loc);
3513 xloc.file = map->to_file;
3514 xloc.line = SOURCE_LINE (map, loc);
3515 xloc.column = SOURCE_COLUMN (map, loc);
3517 return xloc;
3521 /* Source location accessor functions. */
3524 /* The source location of this expression. Non-tree_exp nodes such as
3525 decls and constants can be shared among multiple locations, so
3526 return nothing. */
3527 location_t
3528 expr_location (const_tree node)
3530 if (GIMPLE_STMT_P (node))
3531 return GIMPLE_STMT_LOCUS (node);
3532 return EXPR_P (node) ? node->exp.locus : UNKNOWN_LOCATION;
3535 void
3536 set_expr_location (tree node, location_t locus)
3538 if (GIMPLE_STMT_P (node))
3539 GIMPLE_STMT_LOCUS (node) = locus;
3540 else
3541 EXPR_CHECK (node)->exp.locus = locus;
3544 bool
3545 expr_has_location (const_tree node)
3547 return expr_location (node) != UNKNOWN_LOCATION;
3550 source_location *
3551 expr_locus (const_tree node)
3553 if (GIMPLE_STMT_P (node))
3554 return CONST_CAST (source_location *, &GIMPLE_STMT_LOCUS (node));
3555 return (EXPR_P (node)
3556 ? CONST_CAST (source_location *, &node->exp.locus)
3557 : (source_location *) NULL);
3560 void
3561 set_expr_locus (tree node, source_location *loc)
3563 if (loc == NULL)
3565 if (GIMPLE_STMT_P (node))
3566 GIMPLE_STMT_LOCUS (node) = UNKNOWN_LOCATION;
3567 else
3568 EXPR_CHECK (node)->exp.locus = UNKNOWN_LOCATION;
3570 else
3572 if (GIMPLE_STMT_P (node))
3573 GIMPLE_STMT_LOCUS (node) = *loc;
3574 else
3575 EXPR_CHECK (node)->exp.locus = *loc;
3579 /* Return the file name of the location of NODE. */
3580 const char *
3581 expr_filename (const_tree node)
3583 if (GIMPLE_STMT_P (node))
3584 return LOCATION_FILE (GIMPLE_STMT_LOCUS (node));
3585 return LOCATION_FILE (EXPR_CHECK (node)->exp.locus);
3588 /* Return the line number of the location of NODE. */
3590 expr_lineno (const_tree node)
3592 if (GIMPLE_STMT_P (node))
3593 return LOCATION_LINE (GIMPLE_STMT_LOCUS (node));
3594 return LOCATION_LINE (EXPR_CHECK (node)->exp.locus);
3598 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
3599 is ATTRIBUTE. */
3601 tree
3602 build_decl_attribute_variant (tree ddecl, tree attribute)
3604 DECL_ATTRIBUTES (ddecl) = attribute;
3605 return ddecl;
3608 /* Borrowed from hashtab.c iterative_hash implementation. */
3609 #define mix(a,b,c) \
3611 a -= b; a -= c; a ^= (c>>13); \
3612 b -= c; b -= a; b ^= (a<< 8); \
3613 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
3614 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
3615 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
3616 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
3617 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
3618 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
3619 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
3623 /* Produce good hash value combining VAL and VAL2. */
3624 static inline hashval_t
3625 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
3627 /* the golden ratio; an arbitrary value. */
3628 hashval_t a = 0x9e3779b9;
3630 mix (a, val, val2);
3631 return val2;
3634 /* Produce good hash value combining PTR and VAL2. */
3635 static inline hashval_t
3636 iterative_hash_pointer (const void *ptr, hashval_t val2)
3638 if (sizeof (ptr) == sizeof (hashval_t))
3639 return iterative_hash_hashval_t ((size_t) ptr, val2);
3640 else
3642 hashval_t a = (hashval_t) (size_t) ptr;
3643 /* Avoid warnings about shifting of more than the width of the type on
3644 hosts that won't execute this path. */
3645 int zero = 0;
3646 hashval_t b = (hashval_t) ((size_t) ptr >> (sizeof (hashval_t) * 8 + zero));
3647 mix (a, b, val2);
3648 return val2;
3652 /* Produce good hash value combining VAL and VAL2. */
3653 static inline hashval_t
3654 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
3656 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
3657 return iterative_hash_hashval_t (val, val2);
3658 else
3660 hashval_t a = (hashval_t) val;
3661 /* Avoid warnings about shifting of more than the width of the type on
3662 hosts that won't execute this path. */
3663 int zero = 0;
3664 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
3665 mix (a, b, val2);
3666 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
3668 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
3669 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
3670 mix (a, b, val2);
3672 return val2;
3676 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
3677 is ATTRIBUTE and its qualifiers are QUALS.
3679 Record such modified types already made so we don't make duplicates. */
3681 static tree
3682 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
3684 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
3686 hashval_t hashcode = 0;
3687 tree ntype;
3688 enum tree_code code = TREE_CODE (ttype);
3690 /* Building a distinct copy of a tagged type is inappropriate; it
3691 causes breakage in code that expects there to be a one-to-one
3692 relationship between a struct and its fields.
3693 build_duplicate_type is another solution (as used in
3694 handle_transparent_union_attribute), but that doesn't play well
3695 with the stronger C++ type identity model. */
3696 if (TREE_CODE (ttype) == RECORD_TYPE
3697 || TREE_CODE (ttype) == UNION_TYPE
3698 || TREE_CODE (ttype) == QUAL_UNION_TYPE
3699 || TREE_CODE (ttype) == ENUMERAL_TYPE)
3701 warning (OPT_Wattributes,
3702 "ignoring attributes applied to %qT after definition",
3703 TYPE_MAIN_VARIANT (ttype));
3704 return build_qualified_type (ttype, quals);
3707 ntype = build_distinct_type_copy (ttype);
3709 TYPE_ATTRIBUTES (ntype) = attribute;
3710 set_type_quals (ntype, TYPE_UNQUALIFIED);
3712 hashcode = iterative_hash_object (code, hashcode);
3713 if (TREE_TYPE (ntype))
3714 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
3715 hashcode);
3716 hashcode = attribute_hash_list (attribute, hashcode);
3718 switch (TREE_CODE (ntype))
3720 case FUNCTION_TYPE:
3721 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
3722 break;
3723 case ARRAY_TYPE:
3724 if (TYPE_DOMAIN (ntype))
3725 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
3726 hashcode);
3727 break;
3728 case INTEGER_TYPE:
3729 hashcode = iterative_hash_object
3730 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
3731 hashcode = iterative_hash_object
3732 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
3733 break;
3734 case REAL_TYPE:
3735 case FIXED_POINT_TYPE:
3737 unsigned int precision = TYPE_PRECISION (ntype);
3738 hashcode = iterative_hash_object (precision, hashcode);
3740 break;
3741 default:
3742 break;
3745 ntype = type_hash_canon (hashcode, ntype);
3747 /* If the target-dependent attributes make NTYPE different from
3748 its canonical type, we will need to use structural equality
3749 checks for this qualified type. */
3750 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
3751 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
3752 || !targetm.comp_type_attributes (ntype, ttype))
3753 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
3754 else
3755 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
3757 ttype = build_qualified_type (ntype, quals);
3759 else if (TYPE_QUALS (ttype) != quals)
3760 ttype = build_qualified_type (ttype, quals);
3762 return ttype;
3766 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
3767 is ATTRIBUTE.
3769 Record such modified types already made so we don't make duplicates. */
3771 tree
3772 build_type_attribute_variant (tree ttype, tree attribute)
3774 return build_type_attribute_qual_variant (ttype, attribute,
3775 TYPE_QUALS (ttype));
3778 /* Return nonzero if IDENT is a valid name for attribute ATTR,
3779 or zero if not.
3781 We try both `text' and `__text__', ATTR may be either one. */
3782 /* ??? It might be a reasonable simplification to require ATTR to be only
3783 `text'. One might then also require attribute lists to be stored in
3784 their canonicalized form. */
3786 static int
3787 is_attribute_with_length_p (const char *attr, int attr_len, const_tree ident)
3789 int ident_len;
3790 const char *p;
3792 if (TREE_CODE (ident) != IDENTIFIER_NODE)
3793 return 0;
3795 p = IDENTIFIER_POINTER (ident);
3796 ident_len = IDENTIFIER_LENGTH (ident);
3798 if (ident_len == attr_len
3799 && strcmp (attr, p) == 0)
3800 return 1;
3802 /* If ATTR is `__text__', IDENT must be `text'; and vice versa. */
3803 if (attr[0] == '_')
3805 gcc_assert (attr[1] == '_');
3806 gcc_assert (attr[attr_len - 2] == '_');
3807 gcc_assert (attr[attr_len - 1] == '_');
3808 if (ident_len == attr_len - 4
3809 && strncmp (attr + 2, p, attr_len - 4) == 0)
3810 return 1;
3812 else
3814 if (ident_len == attr_len + 4
3815 && p[0] == '_' && p[1] == '_'
3816 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
3817 && strncmp (attr, p + 2, attr_len) == 0)
3818 return 1;
3821 return 0;
3824 /* Return nonzero if IDENT is a valid name for attribute ATTR,
3825 or zero if not.
3827 We try both `text' and `__text__', ATTR may be either one. */
3830 is_attribute_p (const char *attr, const_tree ident)
3832 return is_attribute_with_length_p (attr, strlen (attr), ident);
3835 /* Given an attribute name and a list of attributes, return a pointer to the
3836 attribute's list element if the attribute is part of the list, or NULL_TREE
3837 if not found. If the attribute appears more than once, this only
3838 returns the first occurrence; the TREE_CHAIN of the return value should
3839 be passed back in if further occurrences are wanted. */
3841 tree
3842 lookup_attribute (const char *attr_name, tree list)
3844 tree l;
3845 size_t attr_len = strlen (attr_name);
3847 for (l = list; l; l = TREE_CHAIN (l))
3849 gcc_assert (TREE_CODE (TREE_PURPOSE (l)) == IDENTIFIER_NODE);
3850 if (is_attribute_with_length_p (attr_name, attr_len, TREE_PURPOSE (l)))
3851 return l;
3853 return NULL_TREE;
3856 /* Remove any instances of attribute ATTR_NAME in LIST and return the
3857 modified list. */
3859 tree
3860 remove_attribute (const char *attr_name, tree list)
3862 tree *p;
3863 size_t attr_len = strlen (attr_name);
3865 for (p = &list; *p; )
3867 tree l = *p;
3868 gcc_assert (TREE_CODE (TREE_PURPOSE (l)) == IDENTIFIER_NODE);
3869 if (is_attribute_with_length_p (attr_name, attr_len, TREE_PURPOSE (l)))
3870 *p = TREE_CHAIN (l);
3871 else
3872 p = &TREE_CHAIN (l);
3875 return list;
3878 /* Return an attribute list that is the union of a1 and a2. */
3880 tree
3881 merge_attributes (tree a1, tree a2)
3883 tree attributes;
3885 /* Either one unset? Take the set one. */
3887 if ((attributes = a1) == 0)
3888 attributes = a2;
3890 /* One that completely contains the other? Take it. */
3892 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
3894 if (attribute_list_contained (a2, a1))
3895 attributes = a2;
3896 else
3898 /* Pick the longest list, and hang on the other list. */
3900 if (list_length (a1) < list_length (a2))
3901 attributes = a2, a2 = a1;
3903 for (; a2 != 0; a2 = TREE_CHAIN (a2))
3905 tree a;
3906 for (a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
3907 attributes);
3908 a != NULL_TREE;
3909 a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
3910 TREE_CHAIN (a)))
3912 if (TREE_VALUE (a) != NULL
3913 && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
3914 && TREE_VALUE (a2) != NULL
3915 && TREE_CODE (TREE_VALUE (a2)) == TREE_LIST)
3917 if (simple_cst_list_equal (TREE_VALUE (a),
3918 TREE_VALUE (a2)) == 1)
3919 break;
3921 else if (simple_cst_equal (TREE_VALUE (a),
3922 TREE_VALUE (a2)) == 1)
3923 break;
3925 if (a == NULL_TREE)
3927 a1 = copy_node (a2);
3928 TREE_CHAIN (a1) = attributes;
3929 attributes = a1;
3934 return attributes;
3937 /* Given types T1 and T2, merge their attributes and return
3938 the result. */
3940 tree
3941 merge_type_attributes (tree t1, tree t2)
3943 return merge_attributes (TYPE_ATTRIBUTES (t1),
3944 TYPE_ATTRIBUTES (t2));
3947 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
3948 the result. */
3950 tree
3951 merge_decl_attributes (tree olddecl, tree newdecl)
3953 return merge_attributes (DECL_ATTRIBUTES (olddecl),
3954 DECL_ATTRIBUTES (newdecl));
3957 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
3959 /* Specialization of merge_decl_attributes for various Windows targets.
3961 This handles the following situation:
3963 __declspec (dllimport) int foo;
3964 int foo;
3966 The second instance of `foo' nullifies the dllimport. */
3968 tree
3969 merge_dllimport_decl_attributes (tree old, tree new)
3971 tree a;
3972 int delete_dllimport_p = 1;
3974 /* What we need to do here is remove from `old' dllimport if it doesn't
3975 appear in `new'. dllimport behaves like extern: if a declaration is
3976 marked dllimport and a definition appears later, then the object
3977 is not dllimport'd. We also remove a `new' dllimport if the old list
3978 contains dllexport: dllexport always overrides dllimport, regardless
3979 of the order of declaration. */
3980 if (!VAR_OR_FUNCTION_DECL_P (new))
3981 delete_dllimport_p = 0;
3982 else if (DECL_DLLIMPORT_P (new)
3983 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
3985 DECL_DLLIMPORT_P (new) = 0;
3986 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
3987 "dllimport ignored", new);
3989 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new))
3991 /* Warn about overriding a symbol that has already been used, e.g.:
3992 extern int __attribute__ ((dllimport)) foo;
3993 int* bar () {return &foo;}
3994 int foo;
3996 if (TREE_USED (old))
3998 warning (0, "%q+D redeclared without dllimport attribute "
3999 "after being referenced with dll linkage", new);
4000 /* If we have used a variable's address with dllimport linkage,
4001 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
4002 decl may already have had TREE_CONSTANT computed.
4003 We still remove the attribute so that assembler code refers
4004 to '&foo rather than '_imp__foo'. */
4005 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
4006 DECL_DLLIMPORT_P (new) = 1;
4009 /* Let an inline definition silently override the external reference,
4010 but otherwise warn about attribute inconsistency. */
4011 else if (TREE_CODE (new) == VAR_DECL
4012 || !DECL_DECLARED_INLINE_P (new))
4013 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
4014 "previous dllimport ignored", new);
4016 else
4017 delete_dllimport_p = 0;
4019 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new));
4021 if (delete_dllimport_p)
4023 tree prev, t;
4024 const size_t attr_len = strlen ("dllimport");
4026 /* Scan the list for dllimport and delete it. */
4027 for (prev = NULL_TREE, t = a; t; prev = t, t = TREE_CHAIN (t))
4029 if (is_attribute_with_length_p ("dllimport", attr_len,
4030 TREE_PURPOSE (t)))
4032 if (prev == NULL_TREE)
4033 a = TREE_CHAIN (a);
4034 else
4035 TREE_CHAIN (prev) = TREE_CHAIN (t);
4036 break;
4041 return a;
4044 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
4045 struct attribute_spec.handler. */
4047 tree
4048 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
4049 bool *no_add_attrs)
4051 tree node = *pnode;
4053 /* These attributes may apply to structure and union types being created,
4054 but otherwise should pass to the declaration involved. */
4055 if (!DECL_P (node))
4057 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
4058 | (int) ATTR_FLAG_ARRAY_NEXT))
4060 *no_add_attrs = true;
4061 return tree_cons (name, args, NULL_TREE);
4063 if (TREE_CODE (node) == RECORD_TYPE
4064 || TREE_CODE (node) == UNION_TYPE)
4066 node = TYPE_NAME (node);
4067 if (!node)
4068 return NULL_TREE;
4070 else
4072 warning (OPT_Wattributes, "%qs attribute ignored",
4073 IDENTIFIER_POINTER (name));
4074 *no_add_attrs = true;
4075 return NULL_TREE;
4079 if (TREE_CODE (node) != FUNCTION_DECL
4080 && TREE_CODE (node) != VAR_DECL
4081 && TREE_CODE (node) != TYPE_DECL)
4083 *no_add_attrs = true;
4084 warning (OPT_Wattributes, "%qs attribute ignored",
4085 IDENTIFIER_POINTER (name));
4086 return NULL_TREE;
4089 if (TREE_CODE (node) == TYPE_DECL
4090 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
4091 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
4093 *no_add_attrs = true;
4094 warning (OPT_Wattributes, "%qs attribute ignored",
4095 IDENTIFIER_POINTER (name));
4096 return NULL_TREE;
4099 /* Report error on dllimport ambiguities seen now before they cause
4100 any damage. */
4101 else if (is_attribute_p ("dllimport", name))
4103 /* Honor any target-specific overrides. */
4104 if (!targetm.valid_dllimport_attribute_p (node))
4105 *no_add_attrs = true;
4107 else if (TREE_CODE (node) == FUNCTION_DECL
4108 && DECL_DECLARED_INLINE_P (node))
4110 warning (OPT_Wattributes, "inline function %q+D declared as "
4111 " dllimport: attribute ignored", node);
4112 *no_add_attrs = true;
4114 /* Like MS, treat definition of dllimported variables and
4115 non-inlined functions on declaration as syntax errors. */
4116 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
4118 error ("function %q+D definition is marked dllimport", node);
4119 *no_add_attrs = true;
4122 else if (TREE_CODE (node) == VAR_DECL)
4124 if (DECL_INITIAL (node))
4126 error ("variable %q+D definition is marked dllimport",
4127 node);
4128 *no_add_attrs = true;
4131 /* `extern' needn't be specified with dllimport.
4132 Specify `extern' now and hope for the best. Sigh. */
4133 DECL_EXTERNAL (node) = 1;
4134 /* Also, implicitly give dllimport'd variables declared within
4135 a function global scope, unless declared static. */
4136 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
4137 TREE_PUBLIC (node) = 1;
4140 if (*no_add_attrs == false)
4141 DECL_DLLIMPORT_P (node) = 1;
4144 /* Report error if symbol is not accessible at global scope. */
4145 if (!TREE_PUBLIC (node)
4146 && (TREE_CODE (node) == VAR_DECL
4147 || TREE_CODE (node) == FUNCTION_DECL))
4149 error ("external linkage required for symbol %q+D because of "
4150 "%qs attribute", node, IDENTIFIER_POINTER (name));
4151 *no_add_attrs = true;
4154 /* A dllexport'd entity must have default visibility so that other
4155 program units (shared libraries or the main executable) can see
4156 it. A dllimport'd entity must have default visibility so that
4157 the linker knows that undefined references within this program
4158 unit can be resolved by the dynamic linker. */
4159 if (!*no_add_attrs)
4161 if (DECL_VISIBILITY_SPECIFIED (node)
4162 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
4163 error ("%qs implies default visibility, but %qD has already "
4164 "been declared with a different visibility",
4165 IDENTIFIER_POINTER (name), node);
4166 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
4167 DECL_VISIBILITY_SPECIFIED (node) = 1;
4170 return NULL_TREE;
4173 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
4175 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
4176 of the various TYPE_QUAL values. */
4178 static void
4179 set_type_quals (tree type, int type_quals)
4181 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
4182 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
4183 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
4186 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
4188 bool
4189 check_qualified_type (const_tree cand, const_tree base, int type_quals)
4191 return (TYPE_QUALS (cand) == type_quals
4192 && TYPE_NAME (cand) == TYPE_NAME (base)
4193 /* Apparently this is needed for Objective-C. */
4194 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
4195 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
4196 TYPE_ATTRIBUTES (base)));
4199 /* Return a version of the TYPE, qualified as indicated by the
4200 TYPE_QUALS, if one exists. If no qualified version exists yet,
4201 return NULL_TREE. */
4203 tree
4204 get_qualified_type (tree type, int type_quals)
4206 tree t;
4208 if (TYPE_QUALS (type) == type_quals)
4209 return type;
4211 /* Search the chain of variants to see if there is already one there just
4212 like the one we need to have. If so, use that existing one. We must
4213 preserve the TYPE_NAME, since there is code that depends on this. */
4214 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
4215 if (check_qualified_type (t, type, type_quals))
4216 return t;
4218 return NULL_TREE;
4221 /* Like get_qualified_type, but creates the type if it does not
4222 exist. This function never returns NULL_TREE. */
4224 tree
4225 build_qualified_type (tree type, int type_quals)
4227 tree t;
4229 /* See if we already have the appropriate qualified variant. */
4230 t = get_qualified_type (type, type_quals);
4232 /* If not, build it. */
4233 if (!t)
4235 t = build_variant_type_copy (type);
4236 set_type_quals (t, type_quals);
4238 if (TYPE_STRUCTURAL_EQUALITY_P (type))
4239 /* Propagate structural equality. */
4240 SET_TYPE_STRUCTURAL_EQUALITY (t);
4241 else if (TYPE_CANONICAL (type) != type)
4242 /* Build the underlying canonical type, since it is different
4243 from TYPE. */
4244 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
4245 type_quals);
4246 else
4247 /* T is its own canonical type. */
4248 TYPE_CANONICAL (t) = t;
4252 return t;
4255 /* Create a new distinct copy of TYPE. The new type is made its own
4256 MAIN_VARIANT. If TYPE requires structural equality checks, the
4257 resulting type requires structural equality checks; otherwise, its
4258 TYPE_CANONICAL points to itself. */
4260 tree
4261 build_distinct_type_copy (tree type)
4263 tree t = copy_node (type);
4265 TYPE_POINTER_TO (t) = 0;
4266 TYPE_REFERENCE_TO (t) = 0;
4268 /* Set the canonical type either to a new equivalence class, or
4269 propagate the need for structural equality checks. */
4270 if (TYPE_STRUCTURAL_EQUALITY_P (type))
4271 SET_TYPE_STRUCTURAL_EQUALITY (t);
4272 else
4273 TYPE_CANONICAL (t) = t;
4275 /* Make it its own variant. */
4276 TYPE_MAIN_VARIANT (t) = t;
4277 TYPE_NEXT_VARIANT (t) = 0;
4279 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
4280 whose TREE_TYPE is not t. This can also happen in the Ada
4281 frontend when using subtypes. */
4283 return t;
4286 /* Create a new variant of TYPE, equivalent but distinct. This is so
4287 the caller can modify it. TYPE_CANONICAL for the return type will
4288 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
4289 are considered equal by the language itself (or that both types
4290 require structural equality checks). */
4292 tree
4293 build_variant_type_copy (tree type)
4295 tree t, m = TYPE_MAIN_VARIANT (type);
4297 t = build_distinct_type_copy (type);
4299 /* Since we're building a variant, assume that it is a non-semantic
4300 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
4301 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
4303 /* Add the new type to the chain of variants of TYPE. */
4304 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
4305 TYPE_NEXT_VARIANT (m) = t;
4306 TYPE_MAIN_VARIANT (t) = m;
4308 return t;
4311 /* Return true if the from tree in both tree maps are equal. */
4314 tree_map_base_eq (const void *va, const void *vb)
4316 const struct tree_map_base *const a = (const struct tree_map_base *) va,
4317 *const b = (const struct tree_map_base *) vb;
4318 return (a->from == b->from);
4321 /* Hash a from tree in a tree_map. */
4323 unsigned int
4324 tree_map_base_hash (const void *item)
4326 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
4329 /* Return true if this tree map structure is marked for garbage collection
4330 purposes. We simply return true if the from tree is marked, so that this
4331 structure goes away when the from tree goes away. */
4334 tree_map_base_marked_p (const void *p)
4336 return ggc_marked_p (((const struct tree_map_base *) p)->from);
4339 unsigned int
4340 tree_map_hash (const void *item)
4342 return (((const struct tree_map *) item)->hash);
4345 /* Return the initialization priority for DECL. */
4347 priority_type
4348 decl_init_priority_lookup (tree decl)
4350 struct tree_priority_map *h;
4351 struct tree_map_base in;
4353 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
4354 in.from = decl;
4355 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
4356 return h ? h->init : DEFAULT_INIT_PRIORITY;
4359 /* Return the finalization priority for DECL. */
4361 priority_type
4362 decl_fini_priority_lookup (tree decl)
4364 struct tree_priority_map *h;
4365 struct tree_map_base in;
4367 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4368 in.from = decl;
4369 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
4370 return h ? h->fini : DEFAULT_INIT_PRIORITY;
4373 /* Return the initialization and finalization priority information for
4374 DECL. If there is no previous priority information, a freshly
4375 allocated structure is returned. */
4377 static struct tree_priority_map *
4378 decl_priority_info (tree decl)
4380 struct tree_priority_map in;
4381 struct tree_priority_map *h;
4382 void **loc;
4384 in.base.from = decl;
4385 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
4386 h = (struct tree_priority_map *) *loc;
4387 if (!h)
4389 h = GGC_CNEW (struct tree_priority_map);
4390 *loc = h;
4391 h->base.from = decl;
4392 h->init = DEFAULT_INIT_PRIORITY;
4393 h->fini = DEFAULT_INIT_PRIORITY;
4396 return h;
4399 /* Set the initialization priority for DECL to PRIORITY. */
4401 void
4402 decl_init_priority_insert (tree decl, priority_type priority)
4404 struct tree_priority_map *h;
4406 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
4407 h = decl_priority_info (decl);
4408 h->init = priority;
4411 /* Set the finalization priority for DECL to PRIORITY. */
4413 void
4414 decl_fini_priority_insert (tree decl, priority_type priority)
4416 struct tree_priority_map *h;
4418 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4419 h = decl_priority_info (decl);
4420 h->fini = priority;
4423 /* Look up a restrict qualified base decl for FROM. */
4425 tree
4426 decl_restrict_base_lookup (tree from)
4428 struct tree_map *h;
4429 struct tree_map in;
4431 in.base.from = from;
4432 h = (struct tree_map *) htab_find_with_hash (restrict_base_for_decl, &in,
4433 htab_hash_pointer (from));
4434 return h ? h->to : NULL_TREE;
4437 /* Record the restrict qualified base TO for FROM. */
4439 void
4440 decl_restrict_base_insert (tree from, tree to)
4442 struct tree_map *h;
4443 void **loc;
4445 h = GGC_NEW (struct tree_map);
4446 h->hash = htab_hash_pointer (from);
4447 h->base.from = from;
4448 h->to = to;
4449 loc = htab_find_slot_with_hash (restrict_base_for_decl, h, h->hash, INSERT);
4450 *(struct tree_map **) loc = h;
4453 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
4455 static void
4456 print_debug_expr_statistics (void)
4458 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
4459 (long) htab_size (debug_expr_for_decl),
4460 (long) htab_elements (debug_expr_for_decl),
4461 htab_collisions (debug_expr_for_decl));
4464 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
4466 static void
4467 print_value_expr_statistics (void)
4469 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
4470 (long) htab_size (value_expr_for_decl),
4471 (long) htab_elements (value_expr_for_decl),
4472 htab_collisions (value_expr_for_decl));
4475 /* Print out statistics for the RESTRICT_BASE_FOR_DECL hash table, but
4476 don't print anything if the table is empty. */
4478 static void
4479 print_restrict_base_statistics (void)
4481 if (htab_elements (restrict_base_for_decl) != 0)
4482 fprintf (stderr,
4483 "RESTRICT_BASE hash: size %ld, %ld elements, %f collisions\n",
4484 (long) htab_size (restrict_base_for_decl),
4485 (long) htab_elements (restrict_base_for_decl),
4486 htab_collisions (restrict_base_for_decl));
4489 /* Lookup a debug expression for FROM, and return it if we find one. */
4491 tree
4492 decl_debug_expr_lookup (tree from)
4494 struct tree_map *h, in;
4495 in.base.from = from;
4497 h = (struct tree_map *) htab_find_with_hash (debug_expr_for_decl, &in,
4498 htab_hash_pointer (from));
4499 if (h)
4500 return h->to;
4501 return NULL_TREE;
4504 /* Insert a mapping FROM->TO in the debug expression hashtable. */
4506 void
4507 decl_debug_expr_insert (tree from, tree to)
4509 struct tree_map *h;
4510 void **loc;
4512 h = GGC_NEW (struct tree_map);
4513 h->hash = htab_hash_pointer (from);
4514 h->base.from = from;
4515 h->to = to;
4516 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, h->hash, INSERT);
4517 *(struct tree_map **) loc = h;
4520 /* Lookup a value expression for FROM, and return it if we find one. */
4522 tree
4523 decl_value_expr_lookup (tree from)
4525 struct tree_map *h, in;
4526 in.base.from = from;
4528 h = (struct tree_map *) htab_find_with_hash (value_expr_for_decl, &in,
4529 htab_hash_pointer (from));
4530 if (h)
4531 return h->to;
4532 return NULL_TREE;
4535 /* Insert a mapping FROM->TO in the value expression hashtable. */
4537 void
4538 decl_value_expr_insert (tree from, tree to)
4540 struct tree_map *h;
4541 void **loc;
4543 h = GGC_NEW (struct tree_map);
4544 h->hash = htab_hash_pointer (from);
4545 h->base.from = from;
4546 h->to = to;
4547 loc = htab_find_slot_with_hash (value_expr_for_decl, h, h->hash, INSERT);
4548 *(struct tree_map **) loc = h;
4551 /* Hashing of types so that we don't make duplicates.
4552 The entry point is `type_hash_canon'. */
4554 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
4555 with types in the TREE_VALUE slots), by adding the hash codes
4556 of the individual types. */
4558 static unsigned int
4559 type_hash_list (const_tree list, hashval_t hashcode)
4561 const_tree tail;
4563 for (tail = list; tail; tail = TREE_CHAIN (tail))
4564 if (TREE_VALUE (tail) != error_mark_node)
4565 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
4566 hashcode);
4568 return hashcode;
4571 /* These are the Hashtable callback functions. */
4573 /* Returns true iff the types are equivalent. */
4575 static int
4576 type_hash_eq (const void *va, const void *vb)
4578 const struct type_hash *const a = (const struct type_hash *) va,
4579 *const b = (const struct type_hash *) vb;
4581 /* First test the things that are the same for all types. */
4582 if (a->hash != b->hash
4583 || TREE_CODE (a->type) != TREE_CODE (b->type)
4584 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
4585 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
4586 TYPE_ATTRIBUTES (b->type))
4587 || TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
4588 || TYPE_MODE (a->type) != TYPE_MODE (b->type))
4589 return 0;
4591 switch (TREE_CODE (a->type))
4593 case VOID_TYPE:
4594 case COMPLEX_TYPE:
4595 case POINTER_TYPE:
4596 case REFERENCE_TYPE:
4597 return 1;
4599 case VECTOR_TYPE:
4600 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
4602 case ENUMERAL_TYPE:
4603 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
4604 && !(TYPE_VALUES (a->type)
4605 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
4606 && TYPE_VALUES (b->type)
4607 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
4608 && type_list_equal (TYPE_VALUES (a->type),
4609 TYPE_VALUES (b->type))))
4610 return 0;
4612 /* ... fall through ... */
4614 case INTEGER_TYPE:
4615 case REAL_TYPE:
4616 case BOOLEAN_TYPE:
4617 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
4618 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
4619 TYPE_MAX_VALUE (b->type)))
4620 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
4621 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
4622 TYPE_MIN_VALUE (b->type))));
4624 case FIXED_POINT_TYPE:
4625 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
4627 case OFFSET_TYPE:
4628 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
4630 case METHOD_TYPE:
4631 return (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
4632 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
4633 || (TYPE_ARG_TYPES (a->type)
4634 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
4635 && TYPE_ARG_TYPES (b->type)
4636 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
4637 && type_list_equal (TYPE_ARG_TYPES (a->type),
4638 TYPE_ARG_TYPES (b->type)))));
4640 case ARRAY_TYPE:
4641 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
4643 case RECORD_TYPE:
4644 case UNION_TYPE:
4645 case QUAL_UNION_TYPE:
4646 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
4647 || (TYPE_FIELDS (a->type)
4648 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
4649 && TYPE_FIELDS (b->type)
4650 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
4651 && type_list_equal (TYPE_FIELDS (a->type),
4652 TYPE_FIELDS (b->type))));
4654 case FUNCTION_TYPE:
4655 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
4656 || (TYPE_ARG_TYPES (a->type)
4657 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
4658 && TYPE_ARG_TYPES (b->type)
4659 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
4660 && type_list_equal (TYPE_ARG_TYPES (a->type),
4661 TYPE_ARG_TYPES (b->type))))
4662 break;
4663 return 0;
4665 default:
4666 return 0;
4669 if (lang_hooks.types.type_hash_eq != NULL)
4670 return lang_hooks.types.type_hash_eq (a->type, b->type);
4672 return 1;
4675 /* Return the cached hash value. */
4677 static hashval_t
4678 type_hash_hash (const void *item)
4680 return ((const struct type_hash *) item)->hash;
4683 /* Look in the type hash table for a type isomorphic to TYPE.
4684 If one is found, return it. Otherwise return 0. */
4686 tree
4687 type_hash_lookup (hashval_t hashcode, tree type)
4689 struct type_hash *h, in;
4691 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
4692 must call that routine before comparing TYPE_ALIGNs. */
4693 layout_type (type);
4695 in.hash = hashcode;
4696 in.type = type;
4698 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
4699 hashcode);
4700 if (h)
4701 return h->type;
4702 return NULL_TREE;
4705 /* Add an entry to the type-hash-table
4706 for a type TYPE whose hash code is HASHCODE. */
4708 void
4709 type_hash_add (hashval_t hashcode, tree type)
4711 struct type_hash *h;
4712 void **loc;
4714 h = GGC_NEW (struct type_hash);
4715 h->hash = hashcode;
4716 h->type = type;
4717 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
4718 *loc = (void *)h;
4721 /* Given TYPE, and HASHCODE its hash code, return the canonical
4722 object for an identical type if one already exists.
4723 Otherwise, return TYPE, and record it as the canonical object.
4725 To use this function, first create a type of the sort you want.
4726 Then compute its hash code from the fields of the type that
4727 make it different from other similar types.
4728 Then call this function and use the value. */
4730 tree
4731 type_hash_canon (unsigned int hashcode, tree type)
4733 tree t1;
4735 /* The hash table only contains main variants, so ensure that's what we're
4736 being passed. */
4737 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
4739 if (!lang_hooks.types.hash_types)
4740 return type;
4742 /* See if the type is in the hash table already. If so, return it.
4743 Otherwise, add the type. */
4744 t1 = type_hash_lookup (hashcode, type);
4745 if (t1 != 0)
4747 #ifdef GATHER_STATISTICS
4748 tree_node_counts[(int) t_kind]--;
4749 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
4750 #endif
4751 return t1;
4753 else
4755 type_hash_add (hashcode, type);
4756 return type;
4760 /* See if the data pointed to by the type hash table is marked. We consider
4761 it marked if the type is marked or if a debug type number or symbol
4762 table entry has been made for the type. This reduces the amount of
4763 debugging output and eliminates that dependency of the debug output on
4764 the number of garbage collections. */
4766 static int
4767 type_hash_marked_p (const void *p)
4769 const_tree const type = ((const struct type_hash *) p)->type;
4771 return ggc_marked_p (type) || TYPE_SYMTAB_POINTER (type);
4774 static void
4775 print_type_hash_statistics (void)
4777 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
4778 (long) htab_size (type_hash_table),
4779 (long) htab_elements (type_hash_table),
4780 htab_collisions (type_hash_table));
4783 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
4784 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
4785 by adding the hash codes of the individual attributes. */
4787 static unsigned int
4788 attribute_hash_list (const_tree list, hashval_t hashcode)
4790 const_tree tail;
4792 for (tail = list; tail; tail = TREE_CHAIN (tail))
4793 /* ??? Do we want to add in TREE_VALUE too? */
4794 hashcode = iterative_hash_object
4795 (IDENTIFIER_HASH_VALUE (TREE_PURPOSE (tail)), hashcode);
4796 return hashcode;
4799 /* Given two lists of attributes, return true if list l2 is
4800 equivalent to l1. */
4803 attribute_list_equal (const_tree l1, const_tree l2)
4805 return attribute_list_contained (l1, l2)
4806 && attribute_list_contained (l2, l1);
4809 /* Given two lists of attributes, return true if list L2 is
4810 completely contained within L1. */
4811 /* ??? This would be faster if attribute names were stored in a canonicalized
4812 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
4813 must be used to show these elements are equivalent (which they are). */
4814 /* ??? It's not clear that attributes with arguments will always be handled
4815 correctly. */
4818 attribute_list_contained (const_tree l1, const_tree l2)
4820 const_tree t1, t2;
4822 /* First check the obvious, maybe the lists are identical. */
4823 if (l1 == l2)
4824 return 1;
4826 /* Maybe the lists are similar. */
4827 for (t1 = l1, t2 = l2;
4828 t1 != 0 && t2 != 0
4829 && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
4830 && TREE_VALUE (t1) == TREE_VALUE (t2);
4831 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
4833 /* Maybe the lists are equal. */
4834 if (t1 == 0 && t2 == 0)
4835 return 1;
4837 for (; t2 != 0; t2 = TREE_CHAIN (t2))
4839 const_tree attr;
4840 /* This CONST_CAST is okay because lookup_attribute does not
4841 modify its argument and the return value is assigned to a
4842 const_tree. */
4843 for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
4844 CONST_CAST_TREE(l1));
4845 attr != NULL_TREE;
4846 attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
4847 TREE_CHAIN (attr)))
4849 if (TREE_VALUE (t2) != NULL
4850 && TREE_CODE (TREE_VALUE (t2)) == TREE_LIST
4851 && TREE_VALUE (attr) != NULL
4852 && TREE_CODE (TREE_VALUE (attr)) == TREE_LIST)
4854 if (simple_cst_list_equal (TREE_VALUE (t2),
4855 TREE_VALUE (attr)) == 1)
4856 break;
4858 else if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) == 1)
4859 break;
4862 if (attr == 0)
4863 return 0;
4866 return 1;
4869 /* Given two lists of types
4870 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
4871 return 1 if the lists contain the same types in the same order.
4872 Also, the TREE_PURPOSEs must match. */
4875 type_list_equal (const_tree l1, const_tree l2)
4877 const_tree t1, t2;
4879 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
4880 if (TREE_VALUE (t1) != TREE_VALUE (t2)
4881 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
4882 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
4883 && (TREE_TYPE (TREE_PURPOSE (t1))
4884 == TREE_TYPE (TREE_PURPOSE (t2))))))
4885 return 0;
4887 return t1 == t2;
4890 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
4891 given by TYPE. If the argument list accepts variable arguments,
4892 then this function counts only the ordinary arguments. */
4895 type_num_arguments (const_tree type)
4897 int i = 0;
4898 tree t;
4900 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
4901 /* If the function does not take a variable number of arguments,
4902 the last element in the list will have type `void'. */
4903 if (VOID_TYPE_P (TREE_VALUE (t)))
4904 break;
4905 else
4906 ++i;
4908 return i;
4911 /* Nonzero if integer constants T1 and T2
4912 represent the same constant value. */
4915 tree_int_cst_equal (const_tree t1, const_tree t2)
4917 if (t1 == t2)
4918 return 1;
4920 if (t1 == 0 || t2 == 0)
4921 return 0;
4923 if (TREE_CODE (t1) == INTEGER_CST
4924 && TREE_CODE (t2) == INTEGER_CST
4925 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
4926 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
4927 return 1;
4929 return 0;
4932 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
4933 The precise way of comparison depends on their data type. */
4936 tree_int_cst_lt (const_tree t1, const_tree t2)
4938 if (t1 == t2)
4939 return 0;
4941 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
4943 int t1_sgn = tree_int_cst_sgn (t1);
4944 int t2_sgn = tree_int_cst_sgn (t2);
4946 if (t1_sgn < t2_sgn)
4947 return 1;
4948 else if (t1_sgn > t2_sgn)
4949 return 0;
4950 /* Otherwise, both are non-negative, so we compare them as
4951 unsigned just in case one of them would overflow a signed
4952 type. */
4954 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
4955 return INT_CST_LT (t1, t2);
4957 return INT_CST_LT_UNSIGNED (t1, t2);
4960 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
4963 tree_int_cst_compare (const_tree t1, const_tree t2)
4965 if (tree_int_cst_lt (t1, t2))
4966 return -1;
4967 else if (tree_int_cst_lt (t2, t1))
4968 return 1;
4969 else
4970 return 0;
4973 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
4974 the host. If POS is zero, the value can be represented in a single
4975 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
4976 be represented in a single unsigned HOST_WIDE_INT. */
4979 host_integerp (const_tree t, int pos)
4981 return (TREE_CODE (t) == INTEGER_CST
4982 && ((TREE_INT_CST_HIGH (t) == 0
4983 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
4984 || (! pos && TREE_INT_CST_HIGH (t) == -1
4985 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
4986 && (!TYPE_UNSIGNED (TREE_TYPE (t))
4987 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
4988 && TYPE_IS_SIZETYPE (TREE_TYPE (t)))))
4989 || (pos && TREE_INT_CST_HIGH (t) == 0)));
4992 /* Return the HOST_WIDE_INT least significant bits of T if it is an
4993 INTEGER_CST and there is no overflow. POS is nonzero if the result must
4994 be non-negative. We must be able to satisfy the above conditions. */
4996 HOST_WIDE_INT
4997 tree_low_cst (const_tree t, int pos)
4999 gcc_assert (host_integerp (t, pos));
5000 return TREE_INT_CST_LOW (t);
5003 /* Return the most significant bit of the integer constant T. */
5006 tree_int_cst_msb (const_tree t)
5008 int prec;
5009 HOST_WIDE_INT h;
5010 unsigned HOST_WIDE_INT l;
5012 /* Note that using TYPE_PRECISION here is wrong. We care about the
5013 actual bits, not the (arbitrary) range of the type. */
5014 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
5015 rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
5016 2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
5017 return (l & 1) == 1;
5020 /* Return an indication of the sign of the integer constant T.
5021 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
5022 Note that -1 will never be returned if T's type is unsigned. */
5025 tree_int_cst_sgn (const_tree t)
5027 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
5028 return 0;
5029 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
5030 return 1;
5031 else if (TREE_INT_CST_HIGH (t) < 0)
5032 return -1;
5033 else
5034 return 1;
5037 /* Compare two constructor-element-type constants. Return 1 if the lists
5038 are known to be equal; otherwise return 0. */
5041 simple_cst_list_equal (const_tree l1, const_tree l2)
5043 while (l1 != NULL_TREE && l2 != NULL_TREE)
5045 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
5046 return 0;
5048 l1 = TREE_CHAIN (l1);
5049 l2 = TREE_CHAIN (l2);
5052 return l1 == l2;
5055 /* Return truthvalue of whether T1 is the same tree structure as T2.
5056 Return 1 if they are the same.
5057 Return 0 if they are understandably different.
5058 Return -1 if either contains tree structure not understood by
5059 this function. */
5062 simple_cst_equal (const_tree t1, const_tree t2)
5064 enum tree_code code1, code2;
5065 int cmp;
5066 int i;
5068 if (t1 == t2)
5069 return 1;
5070 if (t1 == 0 || t2 == 0)
5071 return 0;
5073 code1 = TREE_CODE (t1);
5074 code2 = TREE_CODE (t2);
5076 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
5078 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
5079 || code2 == NON_LVALUE_EXPR)
5080 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5081 else
5082 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
5085 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
5086 || code2 == NON_LVALUE_EXPR)
5087 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
5089 if (code1 != code2)
5090 return 0;
5092 switch (code1)
5094 case INTEGER_CST:
5095 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
5096 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
5098 case REAL_CST:
5099 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
5101 case FIXED_CST:
5102 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
5104 case STRING_CST:
5105 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
5106 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
5107 TREE_STRING_LENGTH (t1)));
5109 case CONSTRUCTOR:
5111 unsigned HOST_WIDE_INT idx;
5112 VEC(constructor_elt, gc) *v1 = CONSTRUCTOR_ELTS (t1);
5113 VEC(constructor_elt, gc) *v2 = CONSTRUCTOR_ELTS (t2);
5115 if (VEC_length (constructor_elt, v1) != VEC_length (constructor_elt, v2))
5116 return false;
5118 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
5119 /* ??? Should we handle also fields here? */
5120 if (!simple_cst_equal (VEC_index (constructor_elt, v1, idx)->value,
5121 VEC_index (constructor_elt, v2, idx)->value))
5122 return false;
5123 return true;
5126 case SAVE_EXPR:
5127 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5129 case CALL_EXPR:
5130 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
5131 if (cmp <= 0)
5132 return cmp;
5133 if (call_expr_nargs (t1) != call_expr_nargs (t2))
5134 return 0;
5136 const_tree arg1, arg2;
5137 const_call_expr_arg_iterator iter1, iter2;
5138 for (arg1 = first_const_call_expr_arg (t1, &iter1),
5139 arg2 = first_const_call_expr_arg (t2, &iter2);
5140 arg1 && arg2;
5141 arg1 = next_const_call_expr_arg (&iter1),
5142 arg2 = next_const_call_expr_arg (&iter2))
5144 cmp = simple_cst_equal (arg1, arg2);
5145 if (cmp <= 0)
5146 return cmp;
5148 return arg1 == arg2;
5151 case TARGET_EXPR:
5152 /* Special case: if either target is an unallocated VAR_DECL,
5153 it means that it's going to be unified with whatever the
5154 TARGET_EXPR is really supposed to initialize, so treat it
5155 as being equivalent to anything. */
5156 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
5157 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
5158 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
5159 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
5160 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
5161 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
5162 cmp = 1;
5163 else
5164 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5166 if (cmp <= 0)
5167 return cmp;
5169 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
5171 case WITH_CLEANUP_EXPR:
5172 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5173 if (cmp <= 0)
5174 return cmp;
5176 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
5178 case COMPONENT_REF:
5179 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
5180 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5182 return 0;
5184 case VAR_DECL:
5185 case PARM_DECL:
5186 case CONST_DECL:
5187 case FUNCTION_DECL:
5188 return 0;
5190 default:
5191 break;
5194 /* This general rule works for most tree codes. All exceptions should be
5195 handled above. If this is a language-specific tree code, we can't
5196 trust what might be in the operand, so say we don't know
5197 the situation. */
5198 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
5199 return -1;
5201 switch (TREE_CODE_CLASS (code1))
5203 case tcc_unary:
5204 case tcc_binary:
5205 case tcc_comparison:
5206 case tcc_expression:
5207 case tcc_reference:
5208 case tcc_statement:
5209 cmp = 1;
5210 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
5212 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
5213 if (cmp <= 0)
5214 return cmp;
5217 return cmp;
5219 default:
5220 return -1;
5224 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
5225 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
5226 than U, respectively. */
5229 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
5231 if (tree_int_cst_sgn (t) < 0)
5232 return -1;
5233 else if (TREE_INT_CST_HIGH (t) != 0)
5234 return 1;
5235 else if (TREE_INT_CST_LOW (t) == u)
5236 return 0;
5237 else if (TREE_INT_CST_LOW (t) < u)
5238 return -1;
5239 else
5240 return 1;
5243 /* Return true if CODE represents an associative tree code. Otherwise
5244 return false. */
5245 bool
5246 associative_tree_code (enum tree_code code)
5248 switch (code)
5250 case BIT_IOR_EXPR:
5251 case BIT_AND_EXPR:
5252 case BIT_XOR_EXPR:
5253 case PLUS_EXPR:
5254 case MULT_EXPR:
5255 case MIN_EXPR:
5256 case MAX_EXPR:
5257 return true;
5259 default:
5260 break;
5262 return false;
5265 /* Return true if CODE represents a commutative tree code. Otherwise
5266 return false. */
5267 bool
5268 commutative_tree_code (enum tree_code code)
5270 switch (code)
5272 case PLUS_EXPR:
5273 case MULT_EXPR:
5274 case MIN_EXPR:
5275 case MAX_EXPR:
5276 case BIT_IOR_EXPR:
5277 case BIT_XOR_EXPR:
5278 case BIT_AND_EXPR:
5279 case NE_EXPR:
5280 case EQ_EXPR:
5281 case UNORDERED_EXPR:
5282 case ORDERED_EXPR:
5283 case UNEQ_EXPR:
5284 case LTGT_EXPR:
5285 case TRUTH_AND_EXPR:
5286 case TRUTH_XOR_EXPR:
5287 case TRUTH_OR_EXPR:
5288 return true;
5290 default:
5291 break;
5293 return false;
5296 /* Generate a hash value for an expression. This can be used iteratively
5297 by passing a previous result as the "val" argument.
5299 This function is intended to produce the same hash for expressions which
5300 would compare equal using operand_equal_p. */
5302 hashval_t
5303 iterative_hash_expr (const_tree t, hashval_t val)
5305 int i;
5306 enum tree_code code;
5307 char class;
5309 if (t == NULL_TREE)
5310 return iterative_hash_pointer (t, val);
5312 code = TREE_CODE (t);
5314 switch (code)
5316 /* Alas, constants aren't shared, so we can't rely on pointer
5317 identity. */
5318 case INTEGER_CST:
5319 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
5320 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
5321 case REAL_CST:
5323 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
5325 return iterative_hash_hashval_t (val2, val);
5327 case FIXED_CST:
5329 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
5331 return iterative_hash_hashval_t (val2, val);
5333 case STRING_CST:
5334 return iterative_hash (TREE_STRING_POINTER (t),
5335 TREE_STRING_LENGTH (t), val);
5336 case COMPLEX_CST:
5337 val = iterative_hash_expr (TREE_REALPART (t), val);
5338 return iterative_hash_expr (TREE_IMAGPART (t), val);
5339 case VECTOR_CST:
5340 return iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
5342 case SSA_NAME:
5343 case VALUE_HANDLE:
5344 /* we can just compare by pointer. */
5345 return iterative_hash_pointer (t, val);
5347 case TREE_LIST:
5348 /* A list of expressions, for a CALL_EXPR or as the elements of a
5349 VECTOR_CST. */
5350 for (; t; t = TREE_CHAIN (t))
5351 val = iterative_hash_expr (TREE_VALUE (t), val);
5352 return val;
5353 case CONSTRUCTOR:
5355 unsigned HOST_WIDE_INT idx;
5356 tree field, value;
5357 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
5359 val = iterative_hash_expr (field, val);
5360 val = iterative_hash_expr (value, val);
5362 return val;
5364 case FUNCTION_DECL:
5365 /* When referring to a built-in FUNCTION_DECL, use the
5366 __builtin__ form. Otherwise nodes that compare equal
5367 according to operand_equal_p might get different
5368 hash codes. */
5369 if (DECL_BUILT_IN (t))
5371 val = iterative_hash_pointer (built_in_decls[DECL_FUNCTION_CODE (t)],
5372 val);
5373 return val;
5375 /* else FALL THROUGH */
5376 default:
5377 class = TREE_CODE_CLASS (code);
5379 if (class == tcc_declaration)
5381 /* DECL's have a unique ID */
5382 val = iterative_hash_host_wide_int (DECL_UID (t), val);
5384 else
5386 gcc_assert (IS_EXPR_CODE_CLASS (class));
5388 val = iterative_hash_object (code, val);
5390 /* Don't hash the type, that can lead to having nodes which
5391 compare equal according to operand_equal_p, but which
5392 have different hash codes. */
5393 if (code == NOP_EXPR
5394 || code == CONVERT_EXPR
5395 || code == NON_LVALUE_EXPR)
5397 /* Make sure to include signness in the hash computation. */
5398 val += TYPE_UNSIGNED (TREE_TYPE (t));
5399 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
5402 else if (commutative_tree_code (code))
5404 /* It's a commutative expression. We want to hash it the same
5405 however it appears. We do this by first hashing both operands
5406 and then rehashing based on the order of their independent
5407 hashes. */
5408 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
5409 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
5410 hashval_t t;
5412 if (one > two)
5413 t = one, one = two, two = t;
5415 val = iterative_hash_hashval_t (one, val);
5416 val = iterative_hash_hashval_t (two, val);
5418 else
5419 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
5420 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
5422 return val;
5423 break;
5427 /* Constructors for pointer, array and function types.
5428 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
5429 constructed by language-dependent code, not here.) */
5431 /* Construct, lay out and return the type of pointers to TO_TYPE with
5432 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
5433 reference all of memory. If such a type has already been
5434 constructed, reuse it. */
5436 tree
5437 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
5438 bool can_alias_all)
5440 tree t;
5442 if (to_type == error_mark_node)
5443 return error_mark_node;
5445 /* In some cases, languages will have things that aren't a POINTER_TYPE
5446 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
5447 In that case, return that type without regard to the rest of our
5448 operands.
5450 ??? This is a kludge, but consistent with the way this function has
5451 always operated and there doesn't seem to be a good way to avoid this
5452 at the moment. */
5453 if (TYPE_POINTER_TO (to_type) != 0
5454 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
5455 return TYPE_POINTER_TO (to_type);
5457 /* First, if we already have a type for pointers to TO_TYPE and it's
5458 the proper mode, use it. */
5459 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
5460 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
5461 return t;
5463 t = make_node (POINTER_TYPE);
5465 TREE_TYPE (t) = to_type;
5466 TYPE_MODE (t) = mode;
5467 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
5468 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
5469 TYPE_POINTER_TO (to_type) = t;
5471 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
5472 SET_TYPE_STRUCTURAL_EQUALITY (t);
5473 else if (TYPE_CANONICAL (to_type) != to_type)
5474 TYPE_CANONICAL (t)
5475 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
5476 mode, can_alias_all);
5478 /* Lay out the type. This function has many callers that are concerned
5479 with expression-construction, and this simplifies them all. */
5480 layout_type (t);
5482 return t;
5485 /* By default build pointers in ptr_mode. */
5487 tree
5488 build_pointer_type (tree to_type)
5490 return build_pointer_type_for_mode (to_type, ptr_mode, false);
5493 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
5495 tree
5496 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
5497 bool can_alias_all)
5499 tree t;
5501 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
5502 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
5503 In that case, return that type without regard to the rest of our
5504 operands.
5506 ??? This is a kludge, but consistent with the way this function has
5507 always operated and there doesn't seem to be a good way to avoid this
5508 at the moment. */
5509 if (TYPE_REFERENCE_TO (to_type) != 0
5510 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
5511 return TYPE_REFERENCE_TO (to_type);
5513 /* First, if we already have a type for pointers to TO_TYPE and it's
5514 the proper mode, use it. */
5515 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
5516 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
5517 return t;
5519 t = make_node (REFERENCE_TYPE);
5521 TREE_TYPE (t) = to_type;
5522 TYPE_MODE (t) = mode;
5523 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
5524 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
5525 TYPE_REFERENCE_TO (to_type) = t;
5527 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
5528 SET_TYPE_STRUCTURAL_EQUALITY (t);
5529 else if (TYPE_CANONICAL (to_type) != to_type)
5530 TYPE_CANONICAL (t)
5531 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
5532 mode, can_alias_all);
5534 layout_type (t);
5536 return t;
5540 /* Build the node for the type of references-to-TO_TYPE by default
5541 in ptr_mode. */
5543 tree
5544 build_reference_type (tree to_type)
5546 return build_reference_type_for_mode (to_type, ptr_mode, false);
5549 /* Build a type that is compatible with t but has no cv quals anywhere
5550 in its type, thus
5552 const char *const *const * -> char ***. */
5554 tree
5555 build_type_no_quals (tree t)
5557 switch (TREE_CODE (t))
5559 case POINTER_TYPE:
5560 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
5561 TYPE_MODE (t),
5562 TYPE_REF_CAN_ALIAS_ALL (t));
5563 case REFERENCE_TYPE:
5564 return
5565 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
5566 TYPE_MODE (t),
5567 TYPE_REF_CAN_ALIAS_ALL (t));
5568 default:
5569 return TYPE_MAIN_VARIANT (t);
5573 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
5574 MAXVAL should be the maximum value in the domain
5575 (one less than the length of the array).
5577 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
5578 We don't enforce this limit, that is up to caller (e.g. language front end).
5579 The limit exists because the result is a signed type and we don't handle
5580 sizes that use more than one HOST_WIDE_INT. */
5582 tree
5583 build_index_type (tree maxval)
5585 tree itype = make_node (INTEGER_TYPE);
5587 TREE_TYPE (itype) = sizetype;
5588 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
5589 TYPE_MIN_VALUE (itype) = size_zero_node;
5590 TYPE_MAX_VALUE (itype) = fold_convert (sizetype, maxval);
5591 TYPE_MODE (itype) = TYPE_MODE (sizetype);
5592 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
5593 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
5594 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
5595 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
5597 if (host_integerp (maxval, 1))
5598 return type_hash_canon (tree_low_cst (maxval, 1), itype);
5599 else
5601 /* Since we cannot hash this type, we need to compare it using
5602 structural equality checks. */
5603 SET_TYPE_STRUCTURAL_EQUALITY (itype);
5604 return itype;
5608 /* Builds a signed or unsigned integer type of precision PRECISION.
5609 Used for C bitfields whose precision does not match that of
5610 built-in target types. */
5611 tree
5612 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
5613 int unsignedp)
5615 tree itype = make_node (INTEGER_TYPE);
5617 TYPE_PRECISION (itype) = precision;
5619 if (unsignedp)
5620 fixup_unsigned_type (itype);
5621 else
5622 fixup_signed_type (itype);
5624 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
5625 return type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
5627 return itype;
5630 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
5631 ENUMERAL_TYPE or BOOLEAN_TYPE), with low bound LOWVAL and
5632 high bound HIGHVAL. If TYPE is NULL, sizetype is used. */
5634 tree
5635 build_range_type (tree type, tree lowval, tree highval)
5637 tree itype = make_node (INTEGER_TYPE);
5639 TREE_TYPE (itype) = type;
5640 if (type == NULL_TREE)
5641 type = sizetype;
5643 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
5644 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
5646 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
5647 TYPE_MODE (itype) = TYPE_MODE (type);
5648 TYPE_SIZE (itype) = TYPE_SIZE (type);
5649 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
5650 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
5651 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
5653 if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
5654 return type_hash_canon (tree_low_cst (highval, 0)
5655 - tree_low_cst (lowval, 0),
5656 itype);
5657 else
5658 return itype;
5661 /* Just like build_index_type, but takes lowval and highval instead
5662 of just highval (maxval). */
5664 tree
5665 build_index_2_type (tree lowval, tree highval)
5667 return build_range_type (sizetype, lowval, highval);
5670 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
5671 and number of elements specified by the range of values of INDEX_TYPE.
5672 If such a type has already been constructed, reuse it. */
5674 tree
5675 build_array_type (tree elt_type, tree index_type)
5677 tree t;
5678 hashval_t hashcode = 0;
5680 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
5682 error ("arrays of functions are not meaningful");
5683 elt_type = integer_type_node;
5686 t = make_node (ARRAY_TYPE);
5687 TREE_TYPE (t) = elt_type;
5688 TYPE_DOMAIN (t) = index_type;
5690 if (index_type == 0)
5692 tree save = t;
5693 hashcode = iterative_hash_object (TYPE_HASH (elt_type), hashcode);
5694 t = type_hash_canon (hashcode, t);
5695 if (save == t)
5696 layout_type (t);
5698 if (TYPE_CANONICAL (t) == t)
5700 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type))
5701 SET_TYPE_STRUCTURAL_EQUALITY (t);
5702 else if (TYPE_CANONICAL (elt_type) != elt_type)
5703 TYPE_CANONICAL (t)
5704 = build_array_type (TYPE_CANONICAL (elt_type), index_type);
5707 return t;
5710 hashcode = iterative_hash_object (TYPE_HASH (elt_type), hashcode);
5711 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
5712 t = type_hash_canon (hashcode, t);
5714 if (!COMPLETE_TYPE_P (t))
5715 layout_type (t);
5717 if (TYPE_CANONICAL (t) == t)
5719 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
5720 || TYPE_STRUCTURAL_EQUALITY_P (index_type))
5721 SET_TYPE_STRUCTURAL_EQUALITY (t);
5722 else if (TYPE_CANONICAL (elt_type) != elt_type
5723 || TYPE_CANONICAL (index_type) != index_type)
5724 TYPE_CANONICAL (t)
5725 = build_array_type (TYPE_CANONICAL (elt_type),
5726 TYPE_CANONICAL (index_type));
5729 return t;
5732 /* Recursively examines the array elements of TYPE, until a non-array
5733 element type is found. */
5735 tree
5736 strip_array_types (tree type)
5738 while (TREE_CODE (type) == ARRAY_TYPE)
5739 type = TREE_TYPE (type);
5741 return type;
5744 /* Computes the canonical argument types from the argument type list
5745 ARGTYPES.
5747 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
5748 on entry to this function, or if any of the ARGTYPES are
5749 structural.
5751 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
5752 true on entry to this function, or if any of the ARGTYPES are
5753 non-canonical.
5755 Returns a canonical argument list, which may be ARGTYPES when the
5756 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
5757 true) or would not differ from ARGTYPES. */
5759 static tree
5760 maybe_canonicalize_argtypes(tree argtypes,
5761 bool *any_structural_p,
5762 bool *any_noncanonical_p)
5764 tree arg;
5765 bool any_noncanonical_argtypes_p = false;
5767 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
5769 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
5770 /* Fail gracefully by stating that the type is structural. */
5771 *any_structural_p = true;
5772 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
5773 *any_structural_p = true;
5774 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
5775 || TREE_PURPOSE (arg))
5776 /* If the argument has a default argument, we consider it
5777 non-canonical even though the type itself is canonical.
5778 That way, different variants of function and method types
5779 with default arguments will all point to the variant with
5780 no defaults as their canonical type. */
5781 any_noncanonical_argtypes_p = true;
5784 if (*any_structural_p)
5785 return argtypes;
5787 if (any_noncanonical_argtypes_p)
5789 /* Build the canonical list of argument types. */
5790 tree canon_argtypes = NULL_TREE;
5791 bool is_void = false;
5793 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
5795 if (arg == void_list_node)
5796 is_void = true;
5797 else
5798 canon_argtypes = tree_cons (NULL_TREE,
5799 TYPE_CANONICAL (TREE_VALUE (arg)),
5800 canon_argtypes);
5803 canon_argtypes = nreverse (canon_argtypes);
5804 if (is_void)
5805 canon_argtypes = chainon (canon_argtypes, void_list_node);
5807 /* There is a non-canonical type. */
5808 *any_noncanonical_p = true;
5809 return canon_argtypes;
5812 /* The canonical argument types are the same as ARGTYPES. */
5813 return argtypes;
5816 /* Construct, lay out and return
5817 the type of functions returning type VALUE_TYPE
5818 given arguments of types ARG_TYPES.
5819 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
5820 are data type nodes for the arguments of the function.
5821 If such a type has already been constructed, reuse it. */
5823 tree
5824 build_function_type (tree value_type, tree arg_types)
5826 tree t;
5827 hashval_t hashcode = 0;
5828 bool any_structural_p, any_noncanonical_p;
5829 tree canon_argtypes;
5831 if (TREE_CODE (value_type) == FUNCTION_TYPE)
5833 error ("function return type cannot be function");
5834 value_type = integer_type_node;
5837 /* Make a node of the sort we want. */
5838 t = make_node (FUNCTION_TYPE);
5839 TREE_TYPE (t) = value_type;
5840 TYPE_ARG_TYPES (t) = arg_types;
5842 /* If we already have such a type, use the old one. */
5843 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
5844 hashcode = type_hash_list (arg_types, hashcode);
5845 t = type_hash_canon (hashcode, t);
5847 /* Set up the canonical type. */
5848 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
5849 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
5850 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
5851 &any_structural_p,
5852 &any_noncanonical_p);
5853 if (any_structural_p)
5854 SET_TYPE_STRUCTURAL_EQUALITY (t);
5855 else if (any_noncanonical_p)
5856 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
5857 canon_argtypes);
5859 if (!COMPLETE_TYPE_P (t))
5860 layout_type (t);
5861 return t;
5864 /* Build a function type. The RETURN_TYPE is the type returned by the
5865 function. If additional arguments are provided, they are
5866 additional argument types. The list of argument types must always
5867 be terminated by NULL_TREE. */
5869 tree
5870 build_function_type_list (tree return_type, ...)
5872 tree t, args, last;
5873 va_list p;
5875 va_start (p, return_type);
5877 t = va_arg (p, tree);
5878 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (p, tree))
5879 args = tree_cons (NULL_TREE, t, args);
5881 if (args == NULL_TREE)
5882 args = void_list_node;
5883 else
5885 last = args;
5886 args = nreverse (args);
5887 TREE_CHAIN (last) = void_list_node;
5889 args = build_function_type (return_type, args);
5891 va_end (p);
5892 return args;
5895 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
5896 and ARGTYPES (a TREE_LIST) are the return type and arguments types
5897 for the method. An implicit additional parameter (of type
5898 pointer-to-BASETYPE) is added to the ARGTYPES. */
5900 tree
5901 build_method_type_directly (tree basetype,
5902 tree rettype,
5903 tree argtypes)
5905 tree t;
5906 tree ptype;
5907 int hashcode = 0;
5908 bool any_structural_p, any_noncanonical_p;
5909 tree canon_argtypes;
5911 /* Make a node of the sort we want. */
5912 t = make_node (METHOD_TYPE);
5914 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
5915 TREE_TYPE (t) = rettype;
5916 ptype = build_pointer_type (basetype);
5918 /* The actual arglist for this function includes a "hidden" argument
5919 which is "this". Put it into the list of argument types. */
5920 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
5921 TYPE_ARG_TYPES (t) = argtypes;
5923 /* If we already have such a type, use the old one. */
5924 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
5925 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
5926 hashcode = type_hash_list (argtypes, hashcode);
5927 t = type_hash_canon (hashcode, t);
5929 /* Set up the canonical type. */
5930 any_structural_p
5931 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
5932 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
5933 any_noncanonical_p
5934 = (TYPE_CANONICAL (basetype) != basetype
5935 || TYPE_CANONICAL (rettype) != rettype);
5936 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
5937 &any_structural_p,
5938 &any_noncanonical_p);
5939 if (any_structural_p)
5940 SET_TYPE_STRUCTURAL_EQUALITY (t);
5941 else if (any_noncanonical_p)
5942 TYPE_CANONICAL (t)
5943 = build_method_type_directly (TYPE_CANONICAL (basetype),
5944 TYPE_CANONICAL (rettype),
5945 canon_argtypes);
5946 if (!COMPLETE_TYPE_P (t))
5947 layout_type (t);
5949 return t;
5952 /* Construct, lay out and return the type of methods belonging to class
5953 BASETYPE and whose arguments and values are described by TYPE.
5954 If that type exists already, reuse it.
5955 TYPE must be a FUNCTION_TYPE node. */
5957 tree
5958 build_method_type (tree basetype, tree type)
5960 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
5962 return build_method_type_directly (basetype,
5963 TREE_TYPE (type),
5964 TYPE_ARG_TYPES (type));
5967 /* Construct, lay out and return the type of offsets to a value
5968 of type TYPE, within an object of type BASETYPE.
5969 If a suitable offset type exists already, reuse it. */
5971 tree
5972 build_offset_type (tree basetype, tree type)
5974 tree t;
5975 hashval_t hashcode = 0;
5977 /* Make a node of the sort we want. */
5978 t = make_node (OFFSET_TYPE);
5980 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
5981 TREE_TYPE (t) = type;
5983 /* If we already have such a type, use the old one. */
5984 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
5985 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
5986 t = type_hash_canon (hashcode, t);
5988 if (!COMPLETE_TYPE_P (t))
5989 layout_type (t);
5991 if (TYPE_CANONICAL (t) == t)
5993 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
5994 || TYPE_STRUCTURAL_EQUALITY_P (type))
5995 SET_TYPE_STRUCTURAL_EQUALITY (t);
5996 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
5997 || TYPE_CANONICAL (type) != type)
5998 TYPE_CANONICAL (t)
5999 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
6000 TYPE_CANONICAL (type));
6003 return t;
6006 /* Create a complex type whose components are COMPONENT_TYPE. */
6008 tree
6009 build_complex_type (tree component_type)
6011 tree t;
6012 hashval_t hashcode;
6014 /* Make a node of the sort we want. */
6015 t = make_node (COMPLEX_TYPE);
6017 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
6019 /* If we already have such a type, use the old one. */
6020 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
6021 t = type_hash_canon (hashcode, t);
6023 if (!COMPLETE_TYPE_P (t))
6024 layout_type (t);
6026 if (TYPE_CANONICAL (t) == t)
6028 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
6029 SET_TYPE_STRUCTURAL_EQUALITY (t);
6030 else if (TYPE_CANONICAL (component_type) != component_type)
6031 TYPE_CANONICAL (t)
6032 = build_complex_type (TYPE_CANONICAL (component_type));
6035 /* We need to create a name, since complex is a fundamental type. */
6036 if (! TYPE_NAME (t))
6038 const char *name;
6039 if (component_type == char_type_node)
6040 name = "complex char";
6041 else if (component_type == signed_char_type_node)
6042 name = "complex signed char";
6043 else if (component_type == unsigned_char_type_node)
6044 name = "complex unsigned char";
6045 else if (component_type == short_integer_type_node)
6046 name = "complex short int";
6047 else if (component_type == short_unsigned_type_node)
6048 name = "complex short unsigned int";
6049 else if (component_type == integer_type_node)
6050 name = "complex int";
6051 else if (component_type == unsigned_type_node)
6052 name = "complex unsigned int";
6053 else if (component_type == long_integer_type_node)
6054 name = "complex long int";
6055 else if (component_type == long_unsigned_type_node)
6056 name = "complex long unsigned int";
6057 else if (component_type == long_long_integer_type_node)
6058 name = "complex long long int";
6059 else if (component_type == long_long_unsigned_type_node)
6060 name = "complex long long unsigned int";
6061 else
6062 name = 0;
6064 if (name != 0)
6065 TYPE_NAME (t) = build_decl (TYPE_DECL, get_identifier (name), t);
6068 return build_qualified_type (t, TYPE_QUALS (component_type));
6071 /* Return OP, stripped of any conversions to wider types as much as is safe.
6072 Converting the value back to OP's type makes a value equivalent to OP.
6074 If FOR_TYPE is nonzero, we return a value which, if converted to
6075 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
6077 OP must have integer, real or enumeral type. Pointers are not allowed!
6079 There are some cases where the obvious value we could return
6080 would regenerate to OP if converted to OP's type,
6081 but would not extend like OP to wider types.
6082 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
6083 For example, if OP is (unsigned short)(signed char)-1,
6084 we avoid returning (signed char)-1 if FOR_TYPE is int,
6085 even though extending that to an unsigned short would regenerate OP,
6086 since the result of extending (signed char)-1 to (int)
6087 is different from (int) OP. */
6089 tree
6090 get_unwidened (tree op, tree for_type)
6092 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
6093 tree type = TREE_TYPE (op);
6094 unsigned final_prec
6095 = TYPE_PRECISION (for_type != 0 ? for_type : type);
6096 int uns
6097 = (for_type != 0 && for_type != type
6098 && final_prec > TYPE_PRECISION (type)
6099 && TYPE_UNSIGNED (type));
6100 tree win = op;
6102 while (CONVERT_EXPR_P (op))
6104 int bitschange;
6106 /* TYPE_PRECISION on vector types has different meaning
6107 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
6108 so avoid them here. */
6109 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
6110 break;
6112 bitschange = TYPE_PRECISION (TREE_TYPE (op))
6113 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
6115 /* Truncations are many-one so cannot be removed.
6116 Unless we are later going to truncate down even farther. */
6117 if (bitschange < 0
6118 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
6119 break;
6121 /* See what's inside this conversion. If we decide to strip it,
6122 we will set WIN. */
6123 op = TREE_OPERAND (op, 0);
6125 /* If we have not stripped any zero-extensions (uns is 0),
6126 we can strip any kind of extension.
6127 If we have previously stripped a zero-extension,
6128 only zero-extensions can safely be stripped.
6129 Any extension can be stripped if the bits it would produce
6130 are all going to be discarded later by truncating to FOR_TYPE. */
6132 if (bitschange > 0)
6134 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
6135 win = op;
6136 /* TYPE_UNSIGNED says whether this is a zero-extension.
6137 Let's avoid computing it if it does not affect WIN
6138 and if UNS will not be needed again. */
6139 if ((uns
6140 || CONVERT_EXPR_P (op))
6141 && TYPE_UNSIGNED (TREE_TYPE (op)))
6143 uns = 1;
6144 win = op;
6149 return win;
6152 /* Return OP or a simpler expression for a narrower value
6153 which can be sign-extended or zero-extended to give back OP.
6154 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
6155 or 0 if the value should be sign-extended. */
6157 tree
6158 get_narrower (tree op, int *unsignedp_ptr)
6160 int uns = 0;
6161 int first = 1;
6162 tree win = op;
6163 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
6165 while (TREE_CODE (op) == NOP_EXPR)
6167 int bitschange
6168 = (TYPE_PRECISION (TREE_TYPE (op))
6169 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
6171 /* Truncations are many-one so cannot be removed. */
6172 if (bitschange < 0)
6173 break;
6175 /* See what's inside this conversion. If we decide to strip it,
6176 we will set WIN. */
6178 if (bitschange > 0)
6180 op = TREE_OPERAND (op, 0);
6181 /* An extension: the outermost one can be stripped,
6182 but remember whether it is zero or sign extension. */
6183 if (first)
6184 uns = TYPE_UNSIGNED (TREE_TYPE (op));
6185 /* Otherwise, if a sign extension has been stripped,
6186 only sign extensions can now be stripped;
6187 if a zero extension has been stripped, only zero-extensions. */
6188 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
6189 break;
6190 first = 0;
6192 else /* bitschange == 0 */
6194 /* A change in nominal type can always be stripped, but we must
6195 preserve the unsignedness. */
6196 if (first)
6197 uns = TYPE_UNSIGNED (TREE_TYPE (op));
6198 first = 0;
6199 op = TREE_OPERAND (op, 0);
6200 /* Keep trying to narrow, but don't assign op to win if it
6201 would turn an integral type into something else. */
6202 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
6203 continue;
6206 win = op;
6209 if (TREE_CODE (op) == COMPONENT_REF
6210 /* Since type_for_size always gives an integer type. */
6211 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
6212 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
6213 /* Ensure field is laid out already. */
6214 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
6215 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
6217 unsigned HOST_WIDE_INT innerprec
6218 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
6219 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
6220 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
6221 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
6223 /* We can get this structure field in a narrower type that fits it,
6224 but the resulting extension to its nominal type (a fullword type)
6225 must satisfy the same conditions as for other extensions.
6227 Do this only for fields that are aligned (not bit-fields),
6228 because when bit-field insns will be used there is no
6229 advantage in doing this. */
6231 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
6232 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
6233 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
6234 && type != 0)
6236 if (first)
6237 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
6238 win = fold_convert (type, op);
6242 *unsignedp_ptr = uns;
6243 return win;
6246 /* Nonzero if integer constant C has a value that is permissible
6247 for type TYPE (an INTEGER_TYPE). */
6250 int_fits_type_p (const_tree c, const_tree type)
6252 tree type_low_bound = TYPE_MIN_VALUE (type);
6253 tree type_high_bound = TYPE_MAX_VALUE (type);
6254 bool ok_for_low_bound, ok_for_high_bound;
6255 unsigned HOST_WIDE_INT low;
6256 HOST_WIDE_INT high;
6258 /* If at least one bound of the type is a constant integer, we can check
6259 ourselves and maybe make a decision. If no such decision is possible, but
6260 this type is a subtype, try checking against that. Otherwise, use
6261 fit_double_type, which checks against the precision.
6263 Compute the status for each possibly constant bound, and return if we see
6264 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
6265 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
6266 for "constant known to fit". */
6268 /* Check if C >= type_low_bound. */
6269 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
6271 if (tree_int_cst_lt (c, type_low_bound))
6272 return 0;
6273 ok_for_low_bound = true;
6275 else
6276 ok_for_low_bound = false;
6278 /* Check if c <= type_high_bound. */
6279 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
6281 if (tree_int_cst_lt (type_high_bound, c))
6282 return 0;
6283 ok_for_high_bound = true;
6285 else
6286 ok_for_high_bound = false;
6288 /* If the constant fits both bounds, the result is known. */
6289 if (ok_for_low_bound && ok_for_high_bound)
6290 return 1;
6292 /* Perform some generic filtering which may allow making a decision
6293 even if the bounds are not constant. First, negative integers
6294 never fit in unsigned types, */
6295 if (TYPE_UNSIGNED (type) && tree_int_cst_sgn (c) < 0)
6296 return 0;
6298 /* Second, narrower types always fit in wider ones. */
6299 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
6300 return 1;
6302 /* Third, unsigned integers with top bit set never fit signed types. */
6303 if (! TYPE_UNSIGNED (type)
6304 && TYPE_UNSIGNED (TREE_TYPE (c))
6305 && tree_int_cst_msb (c))
6306 return 0;
6308 /* If we haven't been able to decide at this point, there nothing more we
6309 can check ourselves here. Look at the base type if we have one and it
6310 has the same precision. */
6311 if (TREE_CODE (type) == INTEGER_TYPE
6312 && TREE_TYPE (type) != 0
6313 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
6314 return int_fits_type_p (c, TREE_TYPE (type));
6316 /* Or to fit_double_type, if nothing else. */
6317 low = TREE_INT_CST_LOW (c);
6318 high = TREE_INT_CST_HIGH (c);
6319 return !fit_double_type (low, high, &low, &high, type);
6322 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
6323 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
6324 represented (assuming two's-complement arithmetic) within the bit
6325 precision of the type are returned instead. */
6327 void
6328 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
6330 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
6331 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
6332 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
6333 TYPE_UNSIGNED (type));
6334 else
6336 if (TYPE_UNSIGNED (type))
6337 mpz_set_ui (min, 0);
6338 else
6340 double_int mn;
6341 mn = double_int_mask (TYPE_PRECISION (type) - 1);
6342 mn = double_int_sext (double_int_add (mn, double_int_one),
6343 TYPE_PRECISION (type));
6344 mpz_set_double_int (min, mn, false);
6348 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
6349 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
6350 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
6351 TYPE_UNSIGNED (type));
6352 else
6354 if (TYPE_UNSIGNED (type))
6355 mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type)),
6356 true);
6357 else
6358 mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type) - 1),
6359 true);
6363 /* auto_var_in_fn_p is called to determine whether VAR is an automatic
6364 variable defined in function FN. */
6366 bool
6367 auto_var_in_fn_p (const_tree var, const_tree fn)
6369 return (DECL_P (var) && DECL_CONTEXT (var) == fn
6370 && (((TREE_CODE (var) == VAR_DECL || TREE_CODE (var) == PARM_DECL)
6371 && ! TREE_STATIC (var))
6372 || TREE_CODE (var) == LABEL_DECL
6373 || TREE_CODE (var) == RESULT_DECL));
6376 /* Subprogram of following function. Called by walk_tree.
6378 Return *TP if it is an automatic variable or parameter of the
6379 function passed in as DATA. */
6381 static tree
6382 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
6384 tree fn = (tree) data;
6386 if (TYPE_P (*tp))
6387 *walk_subtrees = 0;
6389 else if (DECL_P (*tp)
6390 && auto_var_in_fn_p (*tp, fn))
6391 return *tp;
6393 return NULL_TREE;
6396 /* Returns true if T is, contains, or refers to a type with variable
6397 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
6398 arguments, but not the return type. If FN is nonzero, only return
6399 true if a modifier of the type or position of FN is a variable or
6400 parameter inside FN.
6402 This concept is more general than that of C99 'variably modified types':
6403 in C99, a struct type is never variably modified because a VLA may not
6404 appear as a structure member. However, in GNU C code like:
6406 struct S { int i[f()]; };
6408 is valid, and other languages may define similar constructs. */
6410 bool
6411 variably_modified_type_p (tree type, tree fn)
6413 tree t;
6415 /* Test if T is either variable (if FN is zero) or an expression containing
6416 a variable in FN. */
6417 #define RETURN_TRUE_IF_VAR(T) \
6418 do { tree _t = (T); \
6419 if (_t && _t != error_mark_node && TREE_CODE (_t) != INTEGER_CST \
6420 && (!fn || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
6421 return true; } while (0)
6423 if (type == error_mark_node)
6424 return false;
6426 /* If TYPE itself has variable size, it is variably modified. */
6427 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
6428 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
6430 switch (TREE_CODE (type))
6432 case POINTER_TYPE:
6433 case REFERENCE_TYPE:
6434 case VECTOR_TYPE:
6435 if (variably_modified_type_p (TREE_TYPE (type), fn))
6436 return true;
6437 break;
6439 case FUNCTION_TYPE:
6440 case METHOD_TYPE:
6441 /* If TYPE is a function type, it is variably modified if the
6442 return type is variably modified. */
6443 if (variably_modified_type_p (TREE_TYPE (type), fn))
6444 return true;
6445 break;
6447 case INTEGER_TYPE:
6448 case REAL_TYPE:
6449 case FIXED_POINT_TYPE:
6450 case ENUMERAL_TYPE:
6451 case BOOLEAN_TYPE:
6452 /* Scalar types are variably modified if their end points
6453 aren't constant. */
6454 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
6455 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
6456 break;
6458 case RECORD_TYPE:
6459 case UNION_TYPE:
6460 case QUAL_UNION_TYPE:
6461 /* We can't see if any of the fields are variably-modified by the
6462 definition we normally use, since that would produce infinite
6463 recursion via pointers. */
6464 /* This is variably modified if some field's type is. */
6465 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
6466 if (TREE_CODE (t) == FIELD_DECL)
6468 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
6469 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
6470 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
6472 if (TREE_CODE (type) == QUAL_UNION_TYPE)
6473 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
6475 break;
6477 case ARRAY_TYPE:
6478 /* Do not call ourselves to avoid infinite recursion. This is
6479 variably modified if the element type is. */
6480 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
6481 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
6482 break;
6484 default:
6485 break;
6488 /* The current language may have other cases to check, but in general,
6489 all other types are not variably modified. */
6490 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
6492 #undef RETURN_TRUE_IF_VAR
6495 /* Given a DECL or TYPE, return the scope in which it was declared, or
6496 NULL_TREE if there is no containing scope. */
6498 tree
6499 get_containing_scope (const_tree t)
6501 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
6504 /* Return the innermost context enclosing DECL that is
6505 a FUNCTION_DECL, or zero if none. */
6507 tree
6508 decl_function_context (const_tree decl)
6510 tree context;
6512 if (TREE_CODE (decl) == ERROR_MARK)
6513 return 0;
6515 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
6516 where we look up the function at runtime. Such functions always take
6517 a first argument of type 'pointer to real context'.
6519 C++ should really be fixed to use DECL_CONTEXT for the real context,
6520 and use something else for the "virtual context". */
6521 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
6522 context
6523 = TYPE_MAIN_VARIANT
6524 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6525 else
6526 context = DECL_CONTEXT (decl);
6528 while (context && TREE_CODE (context) != FUNCTION_DECL)
6530 if (TREE_CODE (context) == BLOCK)
6531 context = BLOCK_SUPERCONTEXT (context);
6532 else
6533 context = get_containing_scope (context);
6536 return context;
6539 /* Return the innermost context enclosing DECL that is
6540 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
6541 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
6543 tree
6544 decl_type_context (const_tree decl)
6546 tree context = DECL_CONTEXT (decl);
6548 while (context)
6549 switch (TREE_CODE (context))
6551 case NAMESPACE_DECL:
6552 case TRANSLATION_UNIT_DECL:
6553 return NULL_TREE;
6555 case RECORD_TYPE:
6556 case UNION_TYPE:
6557 case QUAL_UNION_TYPE:
6558 return context;
6560 case TYPE_DECL:
6561 case FUNCTION_DECL:
6562 context = DECL_CONTEXT (context);
6563 break;
6565 case BLOCK:
6566 context = BLOCK_SUPERCONTEXT (context);
6567 break;
6569 default:
6570 gcc_unreachable ();
6573 return NULL_TREE;
6576 /* CALL is a CALL_EXPR. Return the declaration for the function
6577 called, or NULL_TREE if the called function cannot be
6578 determined. */
6580 tree
6581 get_callee_fndecl (const_tree call)
6583 tree addr;
6585 if (call == error_mark_node)
6586 return error_mark_node;
6588 /* It's invalid to call this function with anything but a
6589 CALL_EXPR. */
6590 gcc_assert (TREE_CODE (call) == CALL_EXPR);
6592 /* The first operand to the CALL is the address of the function
6593 called. */
6594 addr = CALL_EXPR_FN (call);
6596 STRIP_NOPS (addr);
6598 /* If this is a readonly function pointer, extract its initial value. */
6599 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
6600 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
6601 && DECL_INITIAL (addr))
6602 addr = DECL_INITIAL (addr);
6604 /* If the address is just `&f' for some function `f', then we know
6605 that `f' is being called. */
6606 if (TREE_CODE (addr) == ADDR_EXPR
6607 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
6608 return TREE_OPERAND (addr, 0);
6610 /* We couldn't figure out what was being called. Maybe the front
6611 end has some idea. */
6612 return lang_hooks.lang_get_callee_fndecl (call);
6615 /* Print debugging information about tree nodes generated during the compile,
6616 and any language-specific information. */
6618 void
6619 dump_tree_statistics (void)
6621 #ifdef GATHER_STATISTICS
6622 int i;
6623 int total_nodes, total_bytes;
6624 #endif
6626 fprintf (stderr, "\n??? tree nodes created\n\n");
6627 #ifdef GATHER_STATISTICS
6628 fprintf (stderr, "Kind Nodes Bytes\n");
6629 fprintf (stderr, "---------------------------------------\n");
6630 total_nodes = total_bytes = 0;
6631 for (i = 0; i < (int) all_kinds; i++)
6633 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
6634 tree_node_counts[i], tree_node_sizes[i]);
6635 total_nodes += tree_node_counts[i];
6636 total_bytes += tree_node_sizes[i];
6638 fprintf (stderr, "---------------------------------------\n");
6639 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
6640 fprintf (stderr, "---------------------------------------\n");
6641 ssanames_print_statistics ();
6642 phinodes_print_statistics ();
6643 #else
6644 fprintf (stderr, "(No per-node statistics)\n");
6645 #endif
6646 print_type_hash_statistics ();
6647 print_debug_expr_statistics ();
6648 print_value_expr_statistics ();
6649 print_restrict_base_statistics ();
6650 lang_hooks.print_statistics ();
6653 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
6655 /* Generate a crc32 of a string. */
6657 unsigned
6658 crc32_string (unsigned chksum, const char *string)
6662 unsigned value = *string << 24;
6663 unsigned ix;
6665 for (ix = 8; ix--; value <<= 1)
6667 unsigned feedback;
6669 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
6670 chksum <<= 1;
6671 chksum ^= feedback;
6674 while (*string++);
6675 return chksum;
6678 /* P is a string that will be used in a symbol. Mask out any characters
6679 that are not valid in that context. */
6681 void
6682 clean_symbol_name (char *p)
6684 for (; *p; p++)
6685 if (! (ISALNUM (*p)
6686 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
6687 || *p == '$'
6688 #endif
6689 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
6690 || *p == '.'
6691 #endif
6693 *p = '_';
6696 /* Generate a name for a special-purpose function function.
6697 The generated name may need to be unique across the whole link.
6698 TYPE is some string to identify the purpose of this function to the
6699 linker or collect2; it must start with an uppercase letter,
6700 one of:
6701 I - for constructors
6702 D - for destructors
6703 N - for C++ anonymous namespaces
6704 F - for DWARF unwind frame information. */
6706 tree
6707 get_file_function_name (const char *type)
6709 char *buf;
6710 const char *p;
6711 char *q;
6713 /* If we already have a name we know to be unique, just use that. */
6714 if (first_global_object_name)
6715 p = first_global_object_name;
6716 /* If the target is handling the constructors/destructors, they
6717 will be local to this file and the name is only necessary for
6718 debugging purposes. */
6719 else if ((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
6721 const char *file = main_input_filename;
6722 if (! file)
6723 file = input_filename;
6724 /* Just use the file's basename, because the full pathname
6725 might be quite long. */
6726 p = strrchr (file, '/');
6727 if (p)
6728 p++;
6729 else
6730 p = file;
6731 p = q = ASTRDUP (p);
6732 clean_symbol_name (q);
6734 else
6736 /* Otherwise, the name must be unique across the entire link.
6737 We don't have anything that we know to be unique to this translation
6738 unit, so use what we do have and throw in some randomness. */
6739 unsigned len;
6740 const char *name = weak_global_object_name;
6741 const char *file = main_input_filename;
6743 if (! name)
6744 name = "";
6745 if (! file)
6746 file = input_filename;
6748 len = strlen (file);
6749 q = (char *) alloca (9 * 2 + len + 1);
6750 memcpy (q, file, len + 1);
6751 clean_symbol_name (q);
6753 sprintf (q + len, "_%08X_%08X", crc32_string (0, name),
6754 crc32_string (0, get_random_seed (false)));
6756 p = q;
6759 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
6760 + strlen (type));
6762 /* Set up the name of the file-level functions we may need.
6763 Use a global object (which is already required to be unique over
6764 the program) rather than the file name (which imposes extra
6765 constraints). */
6766 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
6768 return get_identifier (buf);
6771 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
6773 /* Complain that the tree code of NODE does not match the expected 0
6774 terminated list of trailing codes. The trailing code list can be
6775 empty, for a more vague error message. FILE, LINE, and FUNCTION
6776 are of the caller. */
6778 void
6779 tree_check_failed (const_tree node, const char *file,
6780 int line, const char *function, ...)
6782 va_list args;
6783 const char *buffer;
6784 unsigned length = 0;
6785 int code;
6787 va_start (args, function);
6788 while ((code = va_arg (args, int)))
6789 length += 4 + strlen (tree_code_name[code]);
6790 va_end (args);
6791 if (length)
6793 char *tmp;
6794 va_start (args, function);
6795 length += strlen ("expected ");
6796 buffer = tmp = (char *) alloca (length);
6797 length = 0;
6798 while ((code = va_arg (args, int)))
6800 const char *prefix = length ? " or " : "expected ";
6802 strcpy (tmp + length, prefix);
6803 length += strlen (prefix);
6804 strcpy (tmp + length, tree_code_name[code]);
6805 length += strlen (tree_code_name[code]);
6807 va_end (args);
6809 else
6810 buffer = "unexpected node";
6812 internal_error ("tree check: %s, have %s in %s, at %s:%d",
6813 buffer, tree_code_name[TREE_CODE (node)],
6814 function, trim_filename (file), line);
6817 /* Complain that the tree code of NODE does match the expected 0
6818 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
6819 the caller. */
6821 void
6822 tree_not_check_failed (const_tree node, const char *file,
6823 int line, const char *function, ...)
6825 va_list args;
6826 char *buffer;
6827 unsigned length = 0;
6828 int code;
6830 va_start (args, function);
6831 while ((code = va_arg (args, int)))
6832 length += 4 + strlen (tree_code_name[code]);
6833 va_end (args);
6834 va_start (args, function);
6835 buffer = (char *) alloca (length);
6836 length = 0;
6837 while ((code = va_arg (args, int)))
6839 if (length)
6841 strcpy (buffer + length, " or ");
6842 length += 4;
6844 strcpy (buffer + length, tree_code_name[code]);
6845 length += strlen (tree_code_name[code]);
6847 va_end (args);
6849 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
6850 buffer, tree_code_name[TREE_CODE (node)],
6851 function, trim_filename (file), line);
6854 /* Similar to tree_check_failed, except that we check for a class of tree
6855 code, given in CL. */
6857 void
6858 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
6859 const char *file, int line, const char *function)
6861 internal_error
6862 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
6863 TREE_CODE_CLASS_STRING (cl),
6864 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
6865 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
6868 /* Similar to tree_check_failed, except that instead of specifying a
6869 dozen codes, use the knowledge that they're all sequential. */
6871 void
6872 tree_range_check_failed (const_tree node, const char *file, int line,
6873 const char *function, enum tree_code c1,
6874 enum tree_code c2)
6876 char *buffer;
6877 unsigned length = 0;
6878 enum tree_code c;
6880 for (c = c1; c <= c2; ++c)
6881 length += 4 + strlen (tree_code_name[c]);
6883 length += strlen ("expected ");
6884 buffer = (char *) alloca (length);
6885 length = 0;
6887 for (c = c1; c <= c2; ++c)
6889 const char *prefix = length ? " or " : "expected ";
6891 strcpy (buffer + length, prefix);
6892 length += strlen (prefix);
6893 strcpy (buffer + length, tree_code_name[c]);
6894 length += strlen (tree_code_name[c]);
6897 internal_error ("tree check: %s, have %s in %s, at %s:%d",
6898 buffer, tree_code_name[TREE_CODE (node)],
6899 function, trim_filename (file), line);
6903 /* Similar to tree_check_failed, except that we check that a tree does
6904 not have the specified code, given in CL. */
6906 void
6907 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
6908 const char *file, int line, const char *function)
6910 internal_error
6911 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
6912 TREE_CODE_CLASS_STRING (cl),
6913 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
6914 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
6918 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
6920 void
6921 omp_clause_check_failed (const_tree node, const char *file, int line,
6922 const char *function, enum omp_clause_code code)
6924 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
6925 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
6926 function, trim_filename (file), line);
6930 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
6932 void
6933 omp_clause_range_check_failed (const_tree node, const char *file, int line,
6934 const char *function, enum omp_clause_code c1,
6935 enum omp_clause_code c2)
6937 char *buffer;
6938 unsigned length = 0;
6939 enum omp_clause_code c;
6941 for (c = c1; c <= c2; ++c)
6942 length += 4 + strlen (omp_clause_code_name[c]);
6944 length += strlen ("expected ");
6945 buffer = (char *) alloca (length);
6946 length = 0;
6948 for (c = c1; c <= c2; ++c)
6950 const char *prefix = length ? " or " : "expected ";
6952 strcpy (buffer + length, prefix);
6953 length += strlen (prefix);
6954 strcpy (buffer + length, omp_clause_code_name[c]);
6955 length += strlen (omp_clause_code_name[c]);
6958 internal_error ("tree check: %s, have %s in %s, at %s:%d",
6959 buffer, omp_clause_code_name[TREE_CODE (node)],
6960 function, trim_filename (file), line);
6964 #undef DEFTREESTRUCT
6965 #define DEFTREESTRUCT(VAL, NAME) NAME,
6967 static const char *ts_enum_names[] = {
6968 #include "treestruct.def"
6970 #undef DEFTREESTRUCT
6972 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
6974 /* Similar to tree_class_check_failed, except that we check for
6975 whether CODE contains the tree structure identified by EN. */
6977 void
6978 tree_contains_struct_check_failed (const_tree node,
6979 const enum tree_node_structure_enum en,
6980 const char *file, int line,
6981 const char *function)
6983 internal_error
6984 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
6985 TS_ENUM_NAME(en),
6986 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
6990 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
6991 (dynamically sized) vector. */
6993 void
6994 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
6995 const char *function)
6997 internal_error
6998 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
6999 idx + 1, len, function, trim_filename (file), line);
7002 /* Similar to above, except that the check is for the bounds of a PHI_NODE's
7003 (dynamically sized) vector. */
7005 void
7006 phi_node_elt_check_failed (int idx, int len, const char *file, int line,
7007 const char *function)
7009 internal_error
7010 ("tree check: accessed elt %d of phi_node with %d elts in %s, at %s:%d",
7011 idx + 1, len, function, trim_filename (file), line);
7014 /* Similar to above, except that the check is for the bounds of the operand
7015 vector of an expression node EXP. */
7017 void
7018 tree_operand_check_failed (int idx, const_tree exp, const char *file,
7019 int line, const char *function)
7021 int code = TREE_CODE (exp);
7022 internal_error
7023 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
7024 idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
7025 function, trim_filename (file), line);
7028 /* Similar to above, except that the check is for the number of
7029 operands of an OMP_CLAUSE node. */
7031 void
7032 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
7033 int line, const char *function)
7035 internal_error
7036 ("tree check: accessed operand %d of omp_clause %s with %d operands "
7037 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
7038 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
7039 trim_filename (file), line);
7041 #endif /* ENABLE_TREE_CHECKING */
7043 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
7044 and mapped to the machine mode MODE. Initialize its fields and build
7045 the information necessary for debugging output. */
7047 static tree
7048 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
7050 tree t;
7051 hashval_t hashcode = 0;
7053 /* Build a main variant, based on the main variant of the inner type, then
7054 use it to build the variant we return. */
7055 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
7056 && TYPE_MAIN_VARIANT (innertype) != innertype)
7057 return build_type_attribute_qual_variant (
7058 make_vector_type (TYPE_MAIN_VARIANT (innertype), nunits, mode),
7059 TYPE_ATTRIBUTES (innertype),
7060 TYPE_QUALS (innertype));
7062 t = make_node (VECTOR_TYPE);
7063 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
7064 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
7065 TYPE_MODE (t) = mode;
7066 TYPE_READONLY (t) = TYPE_READONLY (innertype);
7067 TYPE_VOLATILE (t) = TYPE_VOLATILE (innertype);
7069 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
7070 SET_TYPE_STRUCTURAL_EQUALITY (t);
7071 else if (TYPE_CANONICAL (innertype) != innertype
7072 || mode != VOIDmode)
7073 TYPE_CANONICAL (t)
7074 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
7076 layout_type (t);
7079 tree index = build_int_cst (NULL_TREE, nunits - 1);
7080 tree array = build_array_type (innertype, build_index_type (index));
7081 tree rt = make_node (RECORD_TYPE);
7083 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
7084 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
7085 layout_type (rt);
7086 TYPE_DEBUG_REPRESENTATION_TYPE (t) = rt;
7087 /* In dwarfout.c, type lookup uses TYPE_UID numbers. We want to output
7088 the representation type, and we want to find that die when looking up
7089 the vector type. This is most easily achieved by making the TYPE_UID
7090 numbers equal. */
7091 TYPE_UID (rt) = TYPE_UID (t);
7094 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
7095 hashcode = iterative_hash_host_wide_int (mode, hashcode);
7096 hashcode = iterative_hash_object (TYPE_HASH (innertype), hashcode);
7097 return type_hash_canon (hashcode, t);
7100 static tree
7101 make_or_reuse_type (unsigned size, int unsignedp)
7103 if (size == INT_TYPE_SIZE)
7104 return unsignedp ? unsigned_type_node : integer_type_node;
7105 if (size == CHAR_TYPE_SIZE)
7106 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
7107 if (size == SHORT_TYPE_SIZE)
7108 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
7109 if (size == LONG_TYPE_SIZE)
7110 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
7111 if (size == LONG_LONG_TYPE_SIZE)
7112 return (unsignedp ? long_long_unsigned_type_node
7113 : long_long_integer_type_node);
7115 if (unsignedp)
7116 return make_unsigned_type (size);
7117 else
7118 return make_signed_type (size);
7121 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
7123 static tree
7124 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
7126 if (satp)
7128 if (size == SHORT_FRACT_TYPE_SIZE)
7129 return unsignedp ? sat_unsigned_short_fract_type_node
7130 : sat_short_fract_type_node;
7131 if (size == FRACT_TYPE_SIZE)
7132 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
7133 if (size == LONG_FRACT_TYPE_SIZE)
7134 return unsignedp ? sat_unsigned_long_fract_type_node
7135 : sat_long_fract_type_node;
7136 if (size == LONG_LONG_FRACT_TYPE_SIZE)
7137 return unsignedp ? sat_unsigned_long_long_fract_type_node
7138 : sat_long_long_fract_type_node;
7140 else
7142 if (size == SHORT_FRACT_TYPE_SIZE)
7143 return unsignedp ? unsigned_short_fract_type_node
7144 : short_fract_type_node;
7145 if (size == FRACT_TYPE_SIZE)
7146 return unsignedp ? unsigned_fract_type_node : fract_type_node;
7147 if (size == LONG_FRACT_TYPE_SIZE)
7148 return unsignedp ? unsigned_long_fract_type_node
7149 : long_fract_type_node;
7150 if (size == LONG_LONG_FRACT_TYPE_SIZE)
7151 return unsignedp ? unsigned_long_long_fract_type_node
7152 : long_long_fract_type_node;
7155 return make_fract_type (size, unsignedp, satp);
7158 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
7160 static tree
7161 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
7163 if (satp)
7165 if (size == SHORT_ACCUM_TYPE_SIZE)
7166 return unsignedp ? sat_unsigned_short_accum_type_node
7167 : sat_short_accum_type_node;
7168 if (size == ACCUM_TYPE_SIZE)
7169 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
7170 if (size == LONG_ACCUM_TYPE_SIZE)
7171 return unsignedp ? sat_unsigned_long_accum_type_node
7172 : sat_long_accum_type_node;
7173 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
7174 return unsignedp ? sat_unsigned_long_long_accum_type_node
7175 : sat_long_long_accum_type_node;
7177 else
7179 if (size == SHORT_ACCUM_TYPE_SIZE)
7180 return unsignedp ? unsigned_short_accum_type_node
7181 : short_accum_type_node;
7182 if (size == ACCUM_TYPE_SIZE)
7183 return unsignedp ? unsigned_accum_type_node : accum_type_node;
7184 if (size == LONG_ACCUM_TYPE_SIZE)
7185 return unsignedp ? unsigned_long_accum_type_node
7186 : long_accum_type_node;
7187 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
7188 return unsignedp ? unsigned_long_long_accum_type_node
7189 : long_long_accum_type_node;
7192 return make_accum_type (size, unsignedp, satp);
7195 /* Create nodes for all integer types (and error_mark_node) using the sizes
7196 of C datatypes. The caller should call set_sizetype soon after calling
7197 this function to select one of the types as sizetype. */
7199 void
7200 build_common_tree_nodes (bool signed_char, bool signed_sizetype)
7202 error_mark_node = make_node (ERROR_MARK);
7203 TREE_TYPE (error_mark_node) = error_mark_node;
7205 initialize_sizetypes (signed_sizetype);
7207 /* Define both `signed char' and `unsigned char'. */
7208 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
7209 TYPE_STRING_FLAG (signed_char_type_node) = 1;
7210 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
7211 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
7213 /* Define `char', which is like either `signed char' or `unsigned char'
7214 but not the same as either. */
7215 char_type_node
7216 = (signed_char
7217 ? make_signed_type (CHAR_TYPE_SIZE)
7218 : make_unsigned_type (CHAR_TYPE_SIZE));
7219 TYPE_STRING_FLAG (char_type_node) = 1;
7221 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
7222 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
7223 integer_type_node = make_signed_type (INT_TYPE_SIZE);
7224 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
7225 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
7226 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
7227 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
7228 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
7230 /* Define a boolean type. This type only represents boolean values but
7231 may be larger than char depending on the value of BOOL_TYPE_SIZE.
7232 Front ends which want to override this size (i.e. Java) can redefine
7233 boolean_type_node before calling build_common_tree_nodes_2. */
7234 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
7235 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
7236 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
7237 TYPE_PRECISION (boolean_type_node) = 1;
7239 /* Fill in the rest of the sized types. Reuse existing type nodes
7240 when possible. */
7241 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
7242 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
7243 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
7244 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
7245 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
7247 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
7248 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
7249 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
7250 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
7251 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
7253 access_public_node = get_identifier ("public");
7254 access_protected_node = get_identifier ("protected");
7255 access_private_node = get_identifier ("private");
7258 /* Call this function after calling build_common_tree_nodes and set_sizetype.
7259 It will create several other common tree nodes. */
7261 void
7262 build_common_tree_nodes_2 (int short_double)
7264 /* Define these next since types below may used them. */
7265 integer_zero_node = build_int_cst (NULL_TREE, 0);
7266 integer_one_node = build_int_cst (NULL_TREE, 1);
7267 integer_minus_one_node = build_int_cst (NULL_TREE, -1);
7269 size_zero_node = size_int (0);
7270 size_one_node = size_int (1);
7271 bitsize_zero_node = bitsize_int (0);
7272 bitsize_one_node = bitsize_int (1);
7273 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
7275 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
7276 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
7278 void_type_node = make_node (VOID_TYPE);
7279 layout_type (void_type_node);
7281 /* We are not going to have real types in C with less than byte alignment,
7282 so we might as well not have any types that claim to have it. */
7283 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
7284 TYPE_USER_ALIGN (void_type_node) = 0;
7286 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
7287 layout_type (TREE_TYPE (null_pointer_node));
7289 ptr_type_node = build_pointer_type (void_type_node);
7290 const_ptr_type_node
7291 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
7292 fileptr_type_node = ptr_type_node;
7294 float_type_node = make_node (REAL_TYPE);
7295 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
7296 layout_type (float_type_node);
7298 double_type_node = make_node (REAL_TYPE);
7299 if (short_double)
7300 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
7301 else
7302 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
7303 layout_type (double_type_node);
7305 long_double_type_node = make_node (REAL_TYPE);
7306 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
7307 layout_type (long_double_type_node);
7309 float_ptr_type_node = build_pointer_type (float_type_node);
7310 double_ptr_type_node = build_pointer_type (double_type_node);
7311 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
7312 integer_ptr_type_node = build_pointer_type (integer_type_node);
7314 /* Fixed size integer types. */
7315 uint32_type_node = build_nonstandard_integer_type (32, true);
7316 uint64_type_node = build_nonstandard_integer_type (64, true);
7318 /* Decimal float types. */
7319 dfloat32_type_node = make_node (REAL_TYPE);
7320 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
7321 layout_type (dfloat32_type_node);
7322 TYPE_MODE (dfloat32_type_node) = SDmode;
7323 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
7325 dfloat64_type_node = make_node (REAL_TYPE);
7326 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
7327 layout_type (dfloat64_type_node);
7328 TYPE_MODE (dfloat64_type_node) = DDmode;
7329 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
7331 dfloat128_type_node = make_node (REAL_TYPE);
7332 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
7333 layout_type (dfloat128_type_node);
7334 TYPE_MODE (dfloat128_type_node) = TDmode;
7335 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
7337 complex_integer_type_node = build_complex_type (integer_type_node);
7338 complex_float_type_node = build_complex_type (float_type_node);
7339 complex_double_type_node = build_complex_type (double_type_node);
7340 complex_long_double_type_node = build_complex_type (long_double_type_node);
7342 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
7343 #define MAKE_FIXED_TYPE_NODE(KIND,WIDTH,SIZE) \
7344 sat_ ## WIDTH ## KIND ## _type_node = \
7345 make_sat_signed_ ## KIND ## _type (SIZE); \
7346 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
7347 make_sat_unsigned_ ## KIND ## _type (SIZE); \
7348 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
7349 unsigned_ ## WIDTH ## KIND ## _type_node = \
7350 make_unsigned_ ## KIND ## _type (SIZE);
7352 /* Make fixed-point type nodes based on four different widths. */
7353 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
7354 MAKE_FIXED_TYPE_NODE (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
7355 MAKE_FIXED_TYPE_NODE (N1, , N2 ## _TYPE_SIZE) \
7356 MAKE_FIXED_TYPE_NODE (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
7357 MAKE_FIXED_TYPE_NODE (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
7359 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
7360 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
7361 NAME ## _type_node = \
7362 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
7363 u ## NAME ## _type_node = \
7364 make_or_reuse_unsigned_ ## KIND ## _type \
7365 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
7366 sat_ ## NAME ## _type_node = \
7367 make_or_reuse_sat_signed_ ## KIND ## _type \
7368 (GET_MODE_BITSIZE (MODE ## mode)); \
7369 sat_u ## NAME ## _type_node = \
7370 make_or_reuse_sat_unsigned_ ## KIND ## _type \
7371 (GET_MODE_BITSIZE (U ## MODE ## mode));
7373 /* Fixed-point type and mode nodes. */
7374 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
7375 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
7376 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
7377 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
7378 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
7379 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
7380 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
7381 MAKE_FIXED_MODE_NODE (accum, ha, HA)
7382 MAKE_FIXED_MODE_NODE (accum, sa, SA)
7383 MAKE_FIXED_MODE_NODE (accum, da, DA)
7384 MAKE_FIXED_MODE_NODE (accum, ta, TA)
7387 tree t = targetm.build_builtin_va_list ();
7389 /* Many back-ends define record types without setting TYPE_NAME.
7390 If we copied the record type here, we'd keep the original
7391 record type without a name. This breaks name mangling. So,
7392 don't copy record types and let c_common_nodes_and_builtins()
7393 declare the type to be __builtin_va_list. */
7394 if (TREE_CODE (t) != RECORD_TYPE)
7395 t = build_variant_type_copy (t);
7397 va_list_type_node = t;
7401 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
7403 static void
7404 local_define_builtin (const char *name, tree type, enum built_in_function code,
7405 const char *library_name, int ecf_flags)
7407 tree decl;
7409 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
7410 library_name, NULL_TREE);
7411 if (ecf_flags & ECF_CONST)
7412 TREE_READONLY (decl) = 1;
7413 if (ecf_flags & ECF_PURE)
7414 DECL_PURE_P (decl) = 1;
7415 if (ecf_flags & ECF_LOOPING_CONST_OR_PURE)
7416 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
7417 if (ecf_flags & ECF_NORETURN)
7418 TREE_THIS_VOLATILE (decl) = 1;
7419 if (ecf_flags & ECF_NOTHROW)
7420 TREE_NOTHROW (decl) = 1;
7421 if (ecf_flags & ECF_MALLOC)
7422 DECL_IS_MALLOC (decl) = 1;
7424 built_in_decls[code] = decl;
7425 implicit_built_in_decls[code] = decl;
7428 /* Call this function after instantiating all builtins that the language
7429 front end cares about. This will build the rest of the builtins that
7430 are relied upon by the tree optimizers and the middle-end. */
7432 void
7433 build_common_builtin_nodes (void)
7435 tree tmp, ftype;
7437 if (built_in_decls[BUILT_IN_MEMCPY] == NULL
7438 || built_in_decls[BUILT_IN_MEMMOVE] == NULL)
7440 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7441 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
7442 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7443 ftype = build_function_type (ptr_type_node, tmp);
7445 if (built_in_decls[BUILT_IN_MEMCPY] == NULL)
7446 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
7447 "memcpy", ECF_NOTHROW);
7448 if (built_in_decls[BUILT_IN_MEMMOVE] == NULL)
7449 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
7450 "memmove", ECF_NOTHROW);
7453 if (built_in_decls[BUILT_IN_MEMCMP] == NULL)
7455 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7456 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
7457 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
7458 ftype = build_function_type (integer_type_node, tmp);
7459 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
7460 "memcmp", ECF_PURE | ECF_NOTHROW);
7463 if (built_in_decls[BUILT_IN_MEMSET] == NULL)
7465 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7466 tmp = tree_cons (NULL_TREE, integer_type_node, tmp);
7467 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7468 ftype = build_function_type (ptr_type_node, tmp);
7469 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
7470 "memset", ECF_NOTHROW);
7473 if (built_in_decls[BUILT_IN_ALLOCA] == NULL)
7475 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7476 ftype = build_function_type (ptr_type_node, tmp);
7477 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
7478 "alloca", ECF_NOTHROW | ECF_MALLOC);
7481 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7482 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7483 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7484 ftype = build_function_type (void_type_node, tmp);
7485 local_define_builtin ("__builtin_init_trampoline", ftype,
7486 BUILT_IN_INIT_TRAMPOLINE,
7487 "__builtin_init_trampoline", ECF_NOTHROW);
7489 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7490 ftype = build_function_type (ptr_type_node, tmp);
7491 local_define_builtin ("__builtin_adjust_trampoline", ftype,
7492 BUILT_IN_ADJUST_TRAMPOLINE,
7493 "__builtin_adjust_trampoline",
7494 ECF_CONST | ECF_NOTHROW);
7496 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7497 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7498 ftype = build_function_type (void_type_node, tmp);
7499 local_define_builtin ("__builtin_nonlocal_goto", ftype,
7500 BUILT_IN_NONLOCAL_GOTO,
7501 "__builtin_nonlocal_goto",
7502 ECF_NORETURN | ECF_NOTHROW);
7504 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7505 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7506 ftype = build_function_type (void_type_node, tmp);
7507 local_define_builtin ("__builtin_setjmp_setup", ftype,
7508 BUILT_IN_SETJMP_SETUP,
7509 "__builtin_setjmp_setup", ECF_NOTHROW);
7511 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7512 ftype = build_function_type (ptr_type_node, tmp);
7513 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
7514 BUILT_IN_SETJMP_DISPATCHER,
7515 "__builtin_setjmp_dispatcher",
7516 ECF_PURE | ECF_NOTHROW);
7518 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7519 ftype = build_function_type (void_type_node, tmp);
7520 local_define_builtin ("__builtin_setjmp_receiver", ftype,
7521 BUILT_IN_SETJMP_RECEIVER,
7522 "__builtin_setjmp_receiver", ECF_NOTHROW);
7524 ftype = build_function_type (ptr_type_node, void_list_node);
7525 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
7526 "__builtin_stack_save", ECF_NOTHROW);
7528 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7529 ftype = build_function_type (void_type_node, tmp);
7530 local_define_builtin ("__builtin_stack_restore", ftype,
7531 BUILT_IN_STACK_RESTORE,
7532 "__builtin_stack_restore", ECF_NOTHROW);
7534 ftype = build_function_type (void_type_node, void_list_node);
7535 local_define_builtin ("__builtin_profile_func_enter", ftype,
7536 BUILT_IN_PROFILE_FUNC_ENTER, "profile_func_enter", 0);
7537 local_define_builtin ("__builtin_profile_func_exit", ftype,
7538 BUILT_IN_PROFILE_FUNC_EXIT, "profile_func_exit", 0);
7540 /* Complex multiplication and division. These are handled as builtins
7541 rather than optabs because emit_library_call_value doesn't support
7542 complex. Further, we can do slightly better with folding these
7543 beasties if the real and complex parts of the arguments are separate. */
7545 enum machine_mode mode;
7547 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
7549 char mode_name_buf[4], *q;
7550 const char *p;
7551 enum built_in_function mcode, dcode;
7552 tree type, inner_type;
7554 type = lang_hooks.types.type_for_mode (mode, 0);
7555 if (type == NULL)
7556 continue;
7557 inner_type = TREE_TYPE (type);
7559 tmp = tree_cons (NULL_TREE, inner_type, void_list_node);
7560 tmp = tree_cons (NULL_TREE, inner_type, tmp);
7561 tmp = tree_cons (NULL_TREE, inner_type, tmp);
7562 tmp = tree_cons (NULL_TREE, inner_type, tmp);
7563 ftype = build_function_type (type, tmp);
7565 mcode = BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
7566 dcode = BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
7568 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
7569 *q = TOLOWER (*p);
7570 *q = '\0';
7572 built_in_names[mcode] = concat ("__mul", mode_name_buf, "3", NULL);
7573 local_define_builtin (built_in_names[mcode], ftype, mcode,
7574 built_in_names[mcode], ECF_CONST | ECF_NOTHROW);
7576 built_in_names[dcode] = concat ("__div", mode_name_buf, "3", NULL);
7577 local_define_builtin (built_in_names[dcode], ftype, dcode,
7578 built_in_names[dcode], ECF_CONST | ECF_NOTHROW);
7583 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
7584 better way.
7586 If we requested a pointer to a vector, build up the pointers that
7587 we stripped off while looking for the inner type. Similarly for
7588 return values from functions.
7590 The argument TYPE is the top of the chain, and BOTTOM is the
7591 new type which we will point to. */
7593 tree
7594 reconstruct_complex_type (tree type, tree bottom)
7596 tree inner, outer;
7598 if (TREE_CODE (type) == POINTER_TYPE)
7600 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7601 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
7602 TYPE_REF_CAN_ALIAS_ALL (type));
7604 else if (TREE_CODE (type) == REFERENCE_TYPE)
7606 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7607 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
7608 TYPE_REF_CAN_ALIAS_ALL (type));
7610 else if (TREE_CODE (type) == ARRAY_TYPE)
7612 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7613 outer = build_array_type (inner, TYPE_DOMAIN (type));
7615 else if (TREE_CODE (type) == FUNCTION_TYPE)
7617 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7618 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
7620 else if (TREE_CODE (type) == METHOD_TYPE)
7622 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7623 /* The build_method_type_directly() routine prepends 'this' to argument list,
7624 so we must compensate by getting rid of it. */
7625 outer
7626 = build_method_type_directly
7627 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
7628 inner,
7629 TREE_CHAIN (TYPE_ARG_TYPES (type)));
7631 else if (TREE_CODE (type) == OFFSET_TYPE)
7633 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7634 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
7636 else
7637 return bottom;
7639 return build_qualified_type (outer, TYPE_QUALS (type));
7642 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
7643 the inner type. */
7644 tree
7645 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
7647 int nunits;
7649 switch (GET_MODE_CLASS (mode))
7651 case MODE_VECTOR_INT:
7652 case MODE_VECTOR_FLOAT:
7653 case MODE_VECTOR_FRACT:
7654 case MODE_VECTOR_UFRACT:
7655 case MODE_VECTOR_ACCUM:
7656 case MODE_VECTOR_UACCUM:
7657 nunits = GET_MODE_NUNITS (mode);
7658 break;
7660 case MODE_INT:
7661 /* Check that there are no leftover bits. */
7662 gcc_assert (GET_MODE_BITSIZE (mode)
7663 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
7665 nunits = GET_MODE_BITSIZE (mode)
7666 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
7667 break;
7669 default:
7670 gcc_unreachable ();
7673 return make_vector_type (innertype, nunits, mode);
7676 /* Similarly, but takes the inner type and number of units, which must be
7677 a power of two. */
7679 tree
7680 build_vector_type (tree innertype, int nunits)
7682 return make_vector_type (innertype, nunits, VOIDmode);
7686 /* Build RESX_EXPR with given REGION_NUMBER. */
7687 tree
7688 build_resx (int region_number)
7690 tree t;
7691 t = build1 (RESX_EXPR, void_type_node,
7692 build_int_cst (NULL_TREE, region_number));
7693 return t;
7696 /* Given an initializer INIT, return TRUE if INIT is zero or some
7697 aggregate of zeros. Otherwise return FALSE. */
7698 bool
7699 initializer_zerop (const_tree init)
7701 tree elt;
7703 STRIP_NOPS (init);
7705 switch (TREE_CODE (init))
7707 case INTEGER_CST:
7708 return integer_zerop (init);
7710 case REAL_CST:
7711 /* ??? Note that this is not correct for C4X float formats. There,
7712 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
7713 negative exponent. */
7714 return real_zerop (init)
7715 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
7717 case FIXED_CST:
7718 return fixed_zerop (init);
7720 case COMPLEX_CST:
7721 return integer_zerop (init)
7722 || (real_zerop (init)
7723 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
7724 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
7726 case VECTOR_CST:
7727 for (elt = TREE_VECTOR_CST_ELTS (init); elt; elt = TREE_CHAIN (elt))
7728 if (!initializer_zerop (TREE_VALUE (elt)))
7729 return false;
7730 return true;
7732 case CONSTRUCTOR:
7734 unsigned HOST_WIDE_INT idx;
7736 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
7737 if (!initializer_zerop (elt))
7738 return false;
7739 return true;
7742 default:
7743 return false;
7747 /* Build an empty statement. */
7749 tree
7750 build_empty_stmt (void)
7752 return build1 (NOP_EXPR, void_type_node, size_zero_node);
7756 /* Build an OpenMP clause with code CODE. */
7758 tree
7759 build_omp_clause (enum omp_clause_code code)
7761 tree t;
7762 int size, length;
7764 length = omp_clause_num_ops[code];
7765 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
7767 t = GGC_NEWVAR (union tree_node, size);
7768 memset (t, 0, size);
7769 TREE_SET_CODE (t, OMP_CLAUSE);
7770 OMP_CLAUSE_SET_CODE (t, code);
7772 #ifdef GATHER_STATISTICS
7773 tree_node_counts[(int) omp_clause_kind]++;
7774 tree_node_sizes[(int) omp_clause_kind] += size;
7775 #endif
7777 return t;
7780 /* Set various status flags when building a CALL_EXPR object T. */
7782 static void
7783 process_call_operands (tree t)
7785 bool side_effects;
7787 side_effects = TREE_SIDE_EFFECTS (t);
7788 if (!side_effects)
7790 int i, n;
7791 n = TREE_OPERAND_LENGTH (t);
7792 for (i = 1; i < n; i++)
7794 tree op = TREE_OPERAND (t, i);
7795 if (op && TREE_SIDE_EFFECTS (op))
7797 side_effects = 1;
7798 break;
7802 if (!side_effects)
7804 int i;
7806 /* Calls have side-effects, except those to const or
7807 pure functions. */
7808 i = call_expr_flags (t);
7809 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
7810 side_effects = 1;
7812 TREE_SIDE_EFFECTS (t) = side_effects;
7815 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
7816 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
7817 Except for the CODE and operand count field, other storage for the
7818 object is initialized to zeros. */
7820 tree
7821 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
7823 tree t;
7824 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
7826 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
7827 gcc_assert (len >= 1);
7829 #ifdef GATHER_STATISTICS
7830 tree_node_counts[(int) e_kind]++;
7831 tree_node_sizes[(int) e_kind] += length;
7832 #endif
7834 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
7836 memset (t, 0, length);
7838 TREE_SET_CODE (t, code);
7840 /* Can't use TREE_OPERAND to store the length because if checking is
7841 enabled, it will try to check the length before we store it. :-P */
7842 t->exp.operands[0] = build_int_cst (sizetype, len);
7844 return t;
7848 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE
7849 and FN and a null static chain slot. ARGLIST is a TREE_LIST of the
7850 arguments. */
7852 tree
7853 build_call_list (tree return_type, tree fn, tree arglist)
7855 tree t;
7856 int i;
7858 t = build_vl_exp (CALL_EXPR, list_length (arglist) + 3);
7859 TREE_TYPE (t) = return_type;
7860 CALL_EXPR_FN (t) = fn;
7861 CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
7862 for (i = 0; arglist; arglist = TREE_CHAIN (arglist), i++)
7863 CALL_EXPR_ARG (t, i) = TREE_VALUE (arglist);
7864 process_call_operands (t);
7865 return t;
7868 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
7869 FN and a null static chain slot. NARGS is the number of call arguments
7870 which are specified as "..." arguments. */
7872 tree
7873 build_call_nary (tree return_type, tree fn, int nargs, ...)
7875 tree ret;
7876 va_list args;
7877 va_start (args, nargs);
7878 ret = build_call_valist (return_type, fn, nargs, args);
7879 va_end (args);
7880 return ret;
7883 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
7884 FN and a null static chain slot. NARGS is the number of call arguments
7885 which are specified as a va_list ARGS. */
7887 tree
7888 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
7890 tree t;
7891 int i;
7893 t = build_vl_exp (CALL_EXPR, nargs + 3);
7894 TREE_TYPE (t) = return_type;
7895 CALL_EXPR_FN (t) = fn;
7896 CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
7897 for (i = 0; i < nargs; i++)
7898 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
7899 process_call_operands (t);
7900 return t;
7903 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
7904 FN and a null static chain slot. NARGS is the number of call arguments
7905 which are specified as a tree array ARGS. */
7907 tree
7908 build_call_array (tree return_type, tree fn, int nargs, tree *args)
7910 tree t;
7911 int i;
7913 t = build_vl_exp (CALL_EXPR, nargs + 3);
7914 TREE_TYPE (t) = return_type;
7915 CALL_EXPR_FN (t) = fn;
7916 CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
7917 for (i = 0; i < nargs; i++)
7918 CALL_EXPR_ARG (t, i) = args[i];
7919 process_call_operands (t);
7920 return t;
7924 /* Returns true if it is possible to prove that the index of
7925 an array access REF (an ARRAY_REF expression) falls into the
7926 array bounds. */
7928 bool
7929 in_array_bounds_p (tree ref)
7931 tree idx = TREE_OPERAND (ref, 1);
7932 tree min, max;
7934 if (TREE_CODE (idx) != INTEGER_CST)
7935 return false;
7937 min = array_ref_low_bound (ref);
7938 max = array_ref_up_bound (ref);
7939 if (!min
7940 || !max
7941 || TREE_CODE (min) != INTEGER_CST
7942 || TREE_CODE (max) != INTEGER_CST)
7943 return false;
7945 if (tree_int_cst_lt (idx, min)
7946 || tree_int_cst_lt (max, idx))
7947 return false;
7949 return true;
7952 /* Returns true if it is possible to prove that the range of
7953 an array access REF (an ARRAY_RANGE_REF expression) falls
7954 into the array bounds. */
7956 bool
7957 range_in_array_bounds_p (tree ref)
7959 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
7960 tree range_min, range_max, min, max;
7962 range_min = TYPE_MIN_VALUE (domain_type);
7963 range_max = TYPE_MAX_VALUE (domain_type);
7964 if (!range_min
7965 || !range_max
7966 || TREE_CODE (range_min) != INTEGER_CST
7967 || TREE_CODE (range_max) != INTEGER_CST)
7968 return false;
7970 min = array_ref_low_bound (ref);
7971 max = array_ref_up_bound (ref);
7972 if (!min
7973 || !max
7974 || TREE_CODE (min) != INTEGER_CST
7975 || TREE_CODE (max) != INTEGER_CST)
7976 return false;
7978 if (tree_int_cst_lt (range_min, min)
7979 || tree_int_cst_lt (max, range_max))
7980 return false;
7982 return true;
7985 /* Return true if T (assumed to be a DECL) must be assigned a memory
7986 location. */
7988 bool
7989 needs_to_live_in_memory (const_tree t)
7991 if (TREE_CODE (t) == SSA_NAME)
7992 t = SSA_NAME_VAR (t);
7994 return (TREE_ADDRESSABLE (t)
7995 || is_global_var (t)
7996 || (TREE_CODE (t) == RESULT_DECL
7997 && aggregate_value_p (t, current_function_decl)));
8000 /* There are situations in which a language considers record types
8001 compatible which have different field lists. Decide if two fields
8002 are compatible. It is assumed that the parent records are compatible. */
8004 bool
8005 fields_compatible_p (const_tree f1, const_tree f2)
8007 if (!operand_equal_p (DECL_FIELD_BIT_OFFSET (f1),
8008 DECL_FIELD_BIT_OFFSET (f2), OEP_ONLY_CONST))
8009 return false;
8011 if (!operand_equal_p (DECL_FIELD_OFFSET (f1),
8012 DECL_FIELD_OFFSET (f2), OEP_ONLY_CONST))
8013 return false;
8015 if (!types_compatible_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8016 return false;
8018 return true;
8021 /* Locate within RECORD a field that is compatible with ORIG_FIELD. */
8023 tree
8024 find_compatible_field (tree record, tree orig_field)
8026 tree f;
8028 for (f = TYPE_FIELDS (record); f ; f = TREE_CHAIN (f))
8029 if (TREE_CODE (f) == FIELD_DECL
8030 && fields_compatible_p (f, orig_field))
8031 return f;
8033 /* ??? Why isn't this on the main fields list? */
8034 f = TYPE_VFIELD (record);
8035 if (f && TREE_CODE (f) == FIELD_DECL
8036 && fields_compatible_p (f, orig_field))
8037 return f;
8039 /* ??? We should abort here, but Java appears to do Bad Things
8040 with inherited fields. */
8041 return orig_field;
8044 /* Return value of a constant X and sign-extend it. */
8046 HOST_WIDE_INT
8047 int_cst_value (const_tree x)
8049 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
8050 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
8052 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
8053 gcc_assert (TREE_INT_CST_HIGH (x) == 0
8054 || TREE_INT_CST_HIGH (x) == -1);
8056 if (bits < HOST_BITS_PER_WIDE_INT)
8058 bool negative = ((val >> (bits - 1)) & 1) != 0;
8059 if (negative)
8060 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
8061 else
8062 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
8065 return val;
8068 /* If TYPE is an integral type, return an equivalent type which is
8069 unsigned iff UNSIGNEDP is true. If TYPE is not an integral type,
8070 return TYPE itself. */
8072 tree
8073 signed_or_unsigned_type_for (int unsignedp, tree type)
8075 tree t = type;
8076 if (POINTER_TYPE_P (type))
8077 t = size_type_node;
8079 if (!INTEGRAL_TYPE_P (t) || TYPE_UNSIGNED (t) == unsignedp)
8080 return t;
8082 return lang_hooks.types.type_for_size (TYPE_PRECISION (t), unsignedp);
8085 /* Returns unsigned variant of TYPE. */
8087 tree
8088 unsigned_type_for (tree type)
8090 return signed_or_unsigned_type_for (1, type);
8093 /* Returns signed variant of TYPE. */
8095 tree
8096 signed_type_for (tree type)
8098 return signed_or_unsigned_type_for (0, type);
8101 /* Returns the largest value obtainable by casting something in INNER type to
8102 OUTER type. */
8104 tree
8105 upper_bound_in_type (tree outer, tree inner)
8107 unsigned HOST_WIDE_INT lo, hi;
8108 unsigned int det = 0;
8109 unsigned oprec = TYPE_PRECISION (outer);
8110 unsigned iprec = TYPE_PRECISION (inner);
8111 unsigned prec;
8113 /* Compute a unique number for every combination. */
8114 det |= (oprec > iprec) ? 4 : 0;
8115 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
8116 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
8118 /* Determine the exponent to use. */
8119 switch (det)
8121 case 0:
8122 case 1:
8123 /* oprec <= iprec, outer: signed, inner: don't care. */
8124 prec = oprec - 1;
8125 break;
8126 case 2:
8127 case 3:
8128 /* oprec <= iprec, outer: unsigned, inner: don't care. */
8129 prec = oprec;
8130 break;
8131 case 4:
8132 /* oprec > iprec, outer: signed, inner: signed. */
8133 prec = iprec - 1;
8134 break;
8135 case 5:
8136 /* oprec > iprec, outer: signed, inner: unsigned. */
8137 prec = iprec;
8138 break;
8139 case 6:
8140 /* oprec > iprec, outer: unsigned, inner: signed. */
8141 prec = oprec;
8142 break;
8143 case 7:
8144 /* oprec > iprec, outer: unsigned, inner: unsigned. */
8145 prec = iprec;
8146 break;
8147 default:
8148 gcc_unreachable ();
8151 /* Compute 2^^prec - 1. */
8152 if (prec <= HOST_BITS_PER_WIDE_INT)
8154 hi = 0;
8155 lo = ((~(unsigned HOST_WIDE_INT) 0)
8156 >> (HOST_BITS_PER_WIDE_INT - prec));
8158 else
8160 hi = ((~(unsigned HOST_WIDE_INT) 0)
8161 >> (2 * HOST_BITS_PER_WIDE_INT - prec));
8162 lo = ~(unsigned HOST_WIDE_INT) 0;
8165 return build_int_cst_wide (outer, lo, hi);
8168 /* Returns the smallest value obtainable by casting something in INNER type to
8169 OUTER type. */
8171 tree
8172 lower_bound_in_type (tree outer, tree inner)
8174 unsigned HOST_WIDE_INT lo, hi;
8175 unsigned oprec = TYPE_PRECISION (outer);
8176 unsigned iprec = TYPE_PRECISION (inner);
8178 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
8179 and obtain 0. */
8180 if (TYPE_UNSIGNED (outer)
8181 /* If we are widening something of an unsigned type, OUTER type
8182 contains all values of INNER type. In particular, both INNER
8183 and OUTER types have zero in common. */
8184 || (oprec > iprec && TYPE_UNSIGNED (inner)))
8185 lo = hi = 0;
8186 else
8188 /* If we are widening a signed type to another signed type, we
8189 want to obtain -2^^(iprec-1). If we are keeping the
8190 precision or narrowing to a signed type, we want to obtain
8191 -2^(oprec-1). */
8192 unsigned prec = oprec > iprec ? iprec : oprec;
8194 if (prec <= HOST_BITS_PER_WIDE_INT)
8196 hi = ~(unsigned HOST_WIDE_INT) 0;
8197 lo = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
8199 else
8201 hi = ((~(unsigned HOST_WIDE_INT) 0)
8202 << (prec - HOST_BITS_PER_WIDE_INT - 1));
8203 lo = 0;
8207 return build_int_cst_wide (outer, lo, hi);
8210 /* Return nonzero if two operands that are suitable for PHI nodes are
8211 necessarily equal. Specifically, both ARG0 and ARG1 must be either
8212 SSA_NAME or invariant. Note that this is strictly an optimization.
8213 That is, callers of this function can directly call operand_equal_p
8214 and get the same result, only slower. */
8217 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
8219 if (arg0 == arg1)
8220 return 1;
8221 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
8222 return 0;
8223 return operand_equal_p (arg0, arg1, 0);
8226 /* Returns number of zeros at the end of binary representation of X.
8228 ??? Use ffs if available? */
8230 tree
8231 num_ending_zeros (const_tree x)
8233 unsigned HOST_WIDE_INT fr, nfr;
8234 unsigned num, abits;
8235 tree type = TREE_TYPE (x);
8237 if (TREE_INT_CST_LOW (x) == 0)
8239 num = HOST_BITS_PER_WIDE_INT;
8240 fr = TREE_INT_CST_HIGH (x);
8242 else
8244 num = 0;
8245 fr = TREE_INT_CST_LOW (x);
8248 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
8250 nfr = fr >> abits;
8251 if (nfr << abits == fr)
8253 num += abits;
8254 fr = nfr;
8258 if (num > TYPE_PRECISION (type))
8259 num = TYPE_PRECISION (type);
8261 return build_int_cst_type (type, num);
8265 #define WALK_SUBTREE(NODE) \
8266 do \
8268 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
8269 if (result) \
8270 return result; \
8272 while (0)
8274 /* This is a subroutine of walk_tree that walks field of TYPE that are to
8275 be walked whenever a type is seen in the tree. Rest of operands and return
8276 value are as for walk_tree. */
8278 static tree
8279 walk_type_fields (tree type, walk_tree_fn func, void *data,
8280 struct pointer_set_t *pset, walk_tree_lh lh)
8282 tree result = NULL_TREE;
8284 switch (TREE_CODE (type))
8286 case POINTER_TYPE:
8287 case REFERENCE_TYPE:
8288 /* We have to worry about mutually recursive pointers. These can't
8289 be written in C. They can in Ada. It's pathological, but
8290 there's an ACATS test (c38102a) that checks it. Deal with this
8291 by checking if we're pointing to another pointer, that one
8292 points to another pointer, that one does too, and we have no htab.
8293 If so, get a hash table. We check three levels deep to avoid
8294 the cost of the hash table if we don't need one. */
8295 if (POINTER_TYPE_P (TREE_TYPE (type))
8296 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
8297 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
8298 && !pset)
8300 result = walk_tree_without_duplicates (&TREE_TYPE (type),
8301 func, data);
8302 if (result)
8303 return result;
8305 break;
8308 /* ... fall through ... */
8310 case COMPLEX_TYPE:
8311 WALK_SUBTREE (TREE_TYPE (type));
8312 break;
8314 case METHOD_TYPE:
8315 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
8317 /* Fall through. */
8319 case FUNCTION_TYPE:
8320 WALK_SUBTREE (TREE_TYPE (type));
8322 tree arg;
8324 /* We never want to walk into default arguments. */
8325 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
8326 WALK_SUBTREE (TREE_VALUE (arg));
8328 break;
8330 case ARRAY_TYPE:
8331 /* Don't follow this nodes's type if a pointer for fear that
8332 we'll have infinite recursion. If we have a PSET, then we
8333 need not fear. */
8334 if (pset
8335 || (!POINTER_TYPE_P (TREE_TYPE (type))
8336 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
8337 WALK_SUBTREE (TREE_TYPE (type));
8338 WALK_SUBTREE (TYPE_DOMAIN (type));
8339 break;
8341 case OFFSET_TYPE:
8342 WALK_SUBTREE (TREE_TYPE (type));
8343 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
8344 break;
8346 default:
8347 break;
8350 return NULL_TREE;
8353 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
8354 called with the DATA and the address of each sub-tree. If FUNC returns a
8355 non-NULL value, the traversal is stopped, and the value returned by FUNC
8356 is returned. If PSET is non-NULL it is used to record the nodes visited,
8357 and to avoid visiting a node more than once. */
8359 tree
8360 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
8361 struct pointer_set_t *pset, walk_tree_lh lh)
8363 enum tree_code code;
8364 int walk_subtrees;
8365 tree result;
8367 #define WALK_SUBTREE_TAIL(NODE) \
8368 do \
8370 tp = & (NODE); \
8371 goto tail_recurse; \
8373 while (0)
8375 tail_recurse:
8376 /* Skip empty subtrees. */
8377 if (!*tp)
8378 return NULL_TREE;
8380 /* Don't walk the same tree twice, if the user has requested
8381 that we avoid doing so. */
8382 if (pset && pointer_set_insert (pset, *tp))
8383 return NULL_TREE;
8385 /* Call the function. */
8386 walk_subtrees = 1;
8387 result = (*func) (tp, &walk_subtrees, data);
8389 /* If we found something, return it. */
8390 if (result)
8391 return result;
8393 code = TREE_CODE (*tp);
8395 /* Even if we didn't, FUNC may have decided that there was nothing
8396 interesting below this point in the tree. */
8397 if (!walk_subtrees)
8399 /* But we still need to check our siblings. */
8400 if (code == TREE_LIST)
8401 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
8402 else if (code == OMP_CLAUSE)
8403 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8404 else
8405 return NULL_TREE;
8408 if (lh)
8410 result = (*lh) (tp, &walk_subtrees, func, data, pset);
8411 if (result || !walk_subtrees)
8412 return result;
8415 switch (code)
8417 case ERROR_MARK:
8418 case IDENTIFIER_NODE:
8419 case INTEGER_CST:
8420 case REAL_CST:
8421 case FIXED_CST:
8422 case VECTOR_CST:
8423 case STRING_CST:
8424 case BLOCK:
8425 case PLACEHOLDER_EXPR:
8426 case SSA_NAME:
8427 case FIELD_DECL:
8428 case RESULT_DECL:
8429 /* None of these have subtrees other than those already walked
8430 above. */
8431 break;
8433 case TREE_LIST:
8434 WALK_SUBTREE (TREE_VALUE (*tp));
8435 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
8436 break;
8438 case TREE_VEC:
8440 int len = TREE_VEC_LENGTH (*tp);
8442 if (len == 0)
8443 break;
8445 /* Walk all elements but the first. */
8446 while (--len)
8447 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
8449 /* Now walk the first one as a tail call. */
8450 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
8453 case COMPLEX_CST:
8454 WALK_SUBTREE (TREE_REALPART (*tp));
8455 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
8457 case CONSTRUCTOR:
8459 unsigned HOST_WIDE_INT idx;
8460 constructor_elt *ce;
8462 for (idx = 0;
8463 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (*tp), idx, ce);
8464 idx++)
8465 WALK_SUBTREE (ce->value);
8467 break;
8469 case SAVE_EXPR:
8470 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
8472 case BIND_EXPR:
8474 tree decl;
8475 for (decl = BIND_EXPR_VARS (*tp); decl; decl = TREE_CHAIN (decl))
8477 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
8478 into declarations that are just mentioned, rather than
8479 declared; they don't really belong to this part of the tree.
8480 And, we can see cycles: the initializer for a declaration
8481 can refer to the declaration itself. */
8482 WALK_SUBTREE (DECL_INITIAL (decl));
8483 WALK_SUBTREE (DECL_SIZE (decl));
8484 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
8486 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
8489 case STATEMENT_LIST:
8491 tree_stmt_iterator i;
8492 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
8493 WALK_SUBTREE (*tsi_stmt_ptr (i));
8495 break;
8497 case OMP_CLAUSE:
8498 switch (OMP_CLAUSE_CODE (*tp))
8500 case OMP_CLAUSE_PRIVATE:
8501 case OMP_CLAUSE_SHARED:
8502 case OMP_CLAUSE_FIRSTPRIVATE:
8503 case OMP_CLAUSE_COPYIN:
8504 case OMP_CLAUSE_COPYPRIVATE:
8505 case OMP_CLAUSE_IF:
8506 case OMP_CLAUSE_NUM_THREADS:
8507 case OMP_CLAUSE_SCHEDULE:
8508 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
8509 /* FALLTHRU */
8511 case OMP_CLAUSE_NOWAIT:
8512 case OMP_CLAUSE_ORDERED:
8513 case OMP_CLAUSE_DEFAULT:
8514 case OMP_CLAUSE_UNTIED:
8515 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8517 case OMP_CLAUSE_LASTPRIVATE:
8518 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
8519 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
8520 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8522 case OMP_CLAUSE_COLLAPSE:
8524 int i;
8525 for (i = 0; i < 3; i++)
8526 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
8527 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8530 case OMP_CLAUSE_REDUCTION:
8532 int i;
8533 for (i = 0; i < 4; i++)
8534 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
8535 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8538 default:
8539 gcc_unreachable ();
8541 break;
8543 case TARGET_EXPR:
8545 int i, len;
8547 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
8548 But, we only want to walk once. */
8549 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
8550 for (i = 0; i < len; ++i)
8551 WALK_SUBTREE (TREE_OPERAND (*tp, i));
8552 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
8555 case DECL_EXPR:
8556 /* If this is a TYPE_DECL, walk into the fields of the type that it's
8557 defining. We only want to walk into these fields of a type in this
8558 case and not in the general case of a mere reference to the type.
8560 The criterion is as follows: if the field can be an expression, it
8561 must be walked only here. This should be in keeping with the fields
8562 that are directly gimplified in gimplify_type_sizes in order for the
8563 mark/copy-if-shared/unmark machinery of the gimplifier to work with
8564 variable-sized types.
8566 Note that DECLs get walked as part of processing the BIND_EXPR. */
8567 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
8569 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
8570 if (TREE_CODE (*type_p) == ERROR_MARK)
8571 return NULL_TREE;
8573 /* Call the function for the type. See if it returns anything or
8574 doesn't want us to continue. If we are to continue, walk both
8575 the normal fields and those for the declaration case. */
8576 result = (*func) (type_p, &walk_subtrees, data);
8577 if (result || !walk_subtrees)
8578 return result;
8580 result = walk_type_fields (*type_p, func, data, pset, lh);
8581 if (result)
8582 return result;
8584 /* If this is a record type, also walk the fields. */
8585 if (TREE_CODE (*type_p) == RECORD_TYPE
8586 || TREE_CODE (*type_p) == UNION_TYPE
8587 || TREE_CODE (*type_p) == QUAL_UNION_TYPE)
8589 tree field;
8591 for (field = TYPE_FIELDS (*type_p); field;
8592 field = TREE_CHAIN (field))
8594 /* We'd like to look at the type of the field, but we can
8595 easily get infinite recursion. So assume it's pointed
8596 to elsewhere in the tree. Also, ignore things that
8597 aren't fields. */
8598 if (TREE_CODE (field) != FIELD_DECL)
8599 continue;
8601 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
8602 WALK_SUBTREE (DECL_SIZE (field));
8603 WALK_SUBTREE (DECL_SIZE_UNIT (field));
8604 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
8605 WALK_SUBTREE (DECL_QUALIFIER (field));
8609 /* Same for scalar types. */
8610 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
8611 || TREE_CODE (*type_p) == ENUMERAL_TYPE
8612 || TREE_CODE (*type_p) == INTEGER_TYPE
8613 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
8614 || TREE_CODE (*type_p) == REAL_TYPE)
8616 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
8617 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
8620 WALK_SUBTREE (TYPE_SIZE (*type_p));
8621 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
8623 /* FALLTHRU */
8625 default:
8626 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
8627 || IS_GIMPLE_STMT_CODE_CLASS (TREE_CODE_CLASS (code)))
8629 int i, len;
8631 /* Walk over all the sub-trees of this operand. */
8632 len = TREE_OPERAND_LENGTH (*tp);
8634 /* Go through the subtrees. We need to do this in forward order so
8635 that the scope of a FOR_EXPR is handled properly. */
8636 if (len)
8638 for (i = 0; i < len - 1; ++i)
8639 WALK_SUBTREE (GENERIC_TREE_OPERAND (*tp, i));
8640 WALK_SUBTREE_TAIL (GENERIC_TREE_OPERAND (*tp, len - 1));
8643 /* If this is a type, walk the needed fields in the type. */
8644 else if (TYPE_P (*tp))
8645 return walk_type_fields (*tp, func, data, pset, lh);
8646 break;
8649 /* We didn't find what we were looking for. */
8650 return NULL_TREE;
8652 #undef WALK_SUBTREE_TAIL
8654 #undef WALK_SUBTREE
8656 /* Like walk_tree, but does not walk duplicate nodes more than once. */
8658 tree
8659 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
8660 walk_tree_lh lh)
8662 tree result;
8663 struct pointer_set_t *pset;
8665 pset = pointer_set_create ();
8666 result = walk_tree_1 (tp, func, data, pset, lh);
8667 pointer_set_destroy (pset);
8668 return result;
8672 /* Return true if STMT is an empty statement or contains nothing but
8673 empty statements. */
8675 bool
8676 empty_body_p (tree stmt)
8678 tree_stmt_iterator i;
8679 tree body;
8681 if (IS_EMPTY_STMT (stmt))
8682 return true;
8683 else if (TREE_CODE (stmt) == BIND_EXPR)
8684 body = BIND_EXPR_BODY (stmt);
8685 else if (TREE_CODE (stmt) == STATEMENT_LIST)
8686 body = stmt;
8687 else
8688 return false;
8690 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
8691 if (!empty_body_p (tsi_stmt (i)))
8692 return false;
8694 return true;
8697 tree *
8698 tree_block (tree t)
8700 char const c = TREE_CODE_CLASS (TREE_CODE (t));
8702 if (IS_EXPR_CODE_CLASS (c))
8703 return &t->exp.block;
8704 else if (IS_GIMPLE_STMT_CODE_CLASS (c))
8705 return &GIMPLE_STMT_BLOCK (t);
8706 gcc_unreachable ();
8707 return NULL;
8710 tree *
8711 generic_tree_operand (tree node, int i)
8713 if (GIMPLE_STMT_P (node))
8714 return &GIMPLE_STMT_OPERAND (node, i);
8715 return &TREE_OPERAND (node, i);
8718 tree *
8719 generic_tree_type (tree node)
8721 if (GIMPLE_STMT_P (node))
8722 return &void_type_node;
8723 return &TREE_TYPE (node);
8726 /* Build and return a TREE_LIST of arguments in the CALL_EXPR exp.
8727 FIXME: don't use this function. It exists for compatibility with
8728 the old representation of CALL_EXPRs where a list was used to hold the
8729 arguments. Places that currently extract the arglist from a CALL_EXPR
8730 ought to be rewritten to use the CALL_EXPR itself. */
8731 tree
8732 call_expr_arglist (tree exp)
8734 tree arglist = NULL_TREE;
8735 int i;
8736 for (i = call_expr_nargs (exp) - 1; i >= 0; i--)
8737 arglist = tree_cons (NULL_TREE, CALL_EXPR_ARG (exp, i), arglist);
8738 return arglist;
8741 /* Return true if TYPE has a variable argument list. */
8743 bool
8744 stdarg_p (tree fntype)
8746 function_args_iterator args_iter;
8747 tree n = NULL_TREE, t;
8749 if (!fntype)
8750 return false;
8752 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
8754 n = t;
8757 return n != NULL_TREE && n != void_type_node;
8760 /* Return true if TYPE has a prototype. */
8762 bool
8763 prototype_p (tree fntype)
8765 tree t;
8767 gcc_assert (fntype != NULL_TREE);
8769 t = TYPE_ARG_TYPES (fntype);
8770 return (t != NULL_TREE);
8773 /* Return the number of arguments that a function has. */
8776 function_args_count (tree fntype)
8778 function_args_iterator args_iter;
8779 tree t;
8780 int num = 0;
8782 if (fntype)
8784 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
8786 num++;
8790 return num;
8793 /* If BLOCK is inlined from an __attribute__((__artificial__))
8794 routine, return pointer to location from where it has been
8795 called. */
8796 location_t *
8797 block_nonartificial_location (tree block)
8799 location_t *ret = NULL;
8801 while (block && TREE_CODE (block) == BLOCK
8802 && BLOCK_ABSTRACT_ORIGIN (block))
8804 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
8806 while (TREE_CODE (ao) == BLOCK && BLOCK_ABSTRACT_ORIGIN (ao))
8807 ao = BLOCK_ABSTRACT_ORIGIN (ao);
8809 if (TREE_CODE (ao) == FUNCTION_DECL)
8811 /* If AO is an artificial inline, point RET to the
8812 call site locus at which it has been inlined and continue
8813 the loop, in case AO's caller is also an artificial
8814 inline. */
8815 if (DECL_DECLARED_INLINE_P (ao)
8816 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
8817 ret = &BLOCK_SOURCE_LOCATION (block);
8818 else
8819 break;
8821 else if (TREE_CODE (ao) != BLOCK)
8822 break;
8824 block = BLOCK_SUPERCONTEXT (block);
8826 return ret;
8829 #include "gt-tree.h"