PR fortran/38830
[official-gcc.git] / gcc / tree.c
blob295358c527b71b41ac6f18622624d6ce0b4cd18b
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, 2009
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"
111 /* obstack.[ch] explicitly declined to prototype this. */
112 extern int _obstack_allocated_p (struct obstack *h, void *obj);
114 #ifdef GATHER_STATISTICS
115 /* Statistics-gathering stuff. */
117 int tree_node_counts[(int) all_kinds];
118 int tree_node_sizes[(int) all_kinds];
120 /* Keep in sync with tree.h:enum tree_node_kind. */
121 static const char * const tree_node_kind_names[] = {
122 "decls",
123 "types",
124 "blocks",
125 "stmts",
126 "refs",
127 "exprs",
128 "constants",
129 "identifiers",
130 "perm_tree_lists",
131 "temp_tree_lists",
132 "vecs",
133 "binfos",
134 "ssa names",
135 "constructors",
136 "random kinds",
137 "lang_decl kinds",
138 "lang_type kinds",
139 "omp clauses",
141 #endif /* GATHER_STATISTICS */
143 /* Unique id for next decl created. */
144 static GTY(()) int next_decl_uid;
145 /* Unique id for next type created. */
146 static GTY(()) int next_type_uid = 1;
148 /* Since we cannot rehash a type after it is in the table, we have to
149 keep the hash code. */
151 struct GTY(()) type_hash {
152 unsigned long hash;
153 tree type;
156 /* Initial size of the hash table (rounded to next prime). */
157 #define TYPE_HASH_INITIAL_SIZE 1000
159 /* Now here is the hash table. When recording a type, it is added to
160 the slot whose index is the hash code. Note that the hash table is
161 used for several kinds of types (function types, array types and
162 array index range types, for now). While all these live in the
163 same table, they are completely independent, and the hash code is
164 computed differently for each of these. */
166 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
167 htab_t type_hash_table;
169 /* Hash table and temporary node for larger integer const values. */
170 static GTY (()) tree int_cst_node;
171 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
172 htab_t int_cst_hash_table;
174 /* Hash table for optimization flags and target option flags. Use the same
175 hash table for both sets of options. Nodes for building the current
176 optimization and target option nodes. The assumption is most of the time
177 the options created will already be in the hash table, so we avoid
178 allocating and freeing up a node repeatably. */
179 static GTY (()) tree cl_optimization_node;
180 static GTY (()) tree cl_target_option_node;
181 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
182 htab_t cl_option_hash_table;
184 /* General tree->tree mapping structure for use in hash tables. */
187 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
188 htab_t debug_expr_for_decl;
190 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
191 htab_t value_expr_for_decl;
193 static GTY ((if_marked ("tree_priority_map_marked_p"),
194 param_is (struct tree_priority_map)))
195 htab_t init_priority_for_decl;
197 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
198 htab_t restrict_base_for_decl;
200 static void set_type_quals (tree, int);
201 static int type_hash_eq (const void *, const void *);
202 static hashval_t type_hash_hash (const void *);
203 static hashval_t int_cst_hash_hash (const void *);
204 static int int_cst_hash_eq (const void *, const void *);
205 static hashval_t cl_option_hash_hash (const void *);
206 static int cl_option_hash_eq (const void *, const void *);
207 static void print_type_hash_statistics (void);
208 static void print_debug_expr_statistics (void);
209 static void print_value_expr_statistics (void);
210 static int type_hash_marked_p (const void *);
211 static unsigned int type_hash_list (const_tree, hashval_t);
212 static unsigned int attribute_hash_list (const_tree, hashval_t);
214 tree global_trees[TI_MAX];
215 tree integer_types[itk_none];
217 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
219 /* Number of operands for each OpenMP clause. */
220 unsigned const char omp_clause_num_ops[] =
222 0, /* OMP_CLAUSE_ERROR */
223 1, /* OMP_CLAUSE_PRIVATE */
224 1, /* OMP_CLAUSE_SHARED */
225 1, /* OMP_CLAUSE_FIRSTPRIVATE */
226 2, /* OMP_CLAUSE_LASTPRIVATE */
227 4, /* OMP_CLAUSE_REDUCTION */
228 1, /* OMP_CLAUSE_COPYIN */
229 1, /* OMP_CLAUSE_COPYPRIVATE */
230 1, /* OMP_CLAUSE_IF */
231 1, /* OMP_CLAUSE_NUM_THREADS */
232 1, /* OMP_CLAUSE_SCHEDULE */
233 0, /* OMP_CLAUSE_NOWAIT */
234 0, /* OMP_CLAUSE_ORDERED */
235 0, /* OMP_CLAUSE_DEFAULT */
236 3, /* OMP_CLAUSE_COLLAPSE */
237 0 /* OMP_CLAUSE_UNTIED */
240 const char * const omp_clause_code_name[] =
242 "error_clause",
243 "private",
244 "shared",
245 "firstprivate",
246 "lastprivate",
247 "reduction",
248 "copyin",
249 "copyprivate",
250 "if",
251 "num_threads",
252 "schedule",
253 "nowait",
254 "ordered",
255 "default",
256 "collapse",
257 "untied"
260 /* Init tree.c. */
262 void
263 init_ttree (void)
265 /* Initialize the hash table of types. */
266 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
267 type_hash_eq, 0);
269 debug_expr_for_decl = htab_create_ggc (512, tree_map_hash,
270 tree_map_eq, 0);
272 value_expr_for_decl = htab_create_ggc (512, tree_map_hash,
273 tree_map_eq, 0);
274 init_priority_for_decl = htab_create_ggc (512, tree_priority_map_hash,
275 tree_priority_map_eq, 0);
276 restrict_base_for_decl = htab_create_ggc (256, tree_map_hash,
277 tree_map_eq, 0);
279 int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
280 int_cst_hash_eq, NULL);
282 int_cst_node = make_node (INTEGER_CST);
284 cl_option_hash_table = htab_create_ggc (64, cl_option_hash_hash,
285 cl_option_hash_eq, NULL);
287 cl_optimization_node = make_node (OPTIMIZATION_NODE);
288 cl_target_option_node = make_node (TARGET_OPTION_NODE);
290 tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON] = 1;
291 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_NON_COMMON] = 1;
292 tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON] = 1;
295 tree_contains_struct[CONST_DECL][TS_DECL_COMMON] = 1;
296 tree_contains_struct[VAR_DECL][TS_DECL_COMMON] = 1;
297 tree_contains_struct[PARM_DECL][TS_DECL_COMMON] = 1;
298 tree_contains_struct[RESULT_DECL][TS_DECL_COMMON] = 1;
299 tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON] = 1;
300 tree_contains_struct[TYPE_DECL][TS_DECL_COMMON] = 1;
301 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON] = 1;
302 tree_contains_struct[LABEL_DECL][TS_DECL_COMMON] = 1;
303 tree_contains_struct[FIELD_DECL][TS_DECL_COMMON] = 1;
306 tree_contains_struct[CONST_DECL][TS_DECL_WRTL] = 1;
307 tree_contains_struct[VAR_DECL][TS_DECL_WRTL] = 1;
308 tree_contains_struct[PARM_DECL][TS_DECL_WRTL] = 1;
309 tree_contains_struct[RESULT_DECL][TS_DECL_WRTL] = 1;
310 tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL] = 1;
311 tree_contains_struct[LABEL_DECL][TS_DECL_WRTL] = 1;
313 tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL] = 1;
314 tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL] = 1;
315 tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL] = 1;
316 tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL] = 1;
317 tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL] = 1;
318 tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL] = 1;
319 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL] = 1;
320 tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL] = 1;
321 tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL] = 1;
323 tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS] = 1;
324 tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS] = 1;
325 tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS] = 1;
326 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_WITH_VIS] = 1;
328 tree_contains_struct[VAR_DECL][TS_VAR_DECL] = 1;
329 tree_contains_struct[FIELD_DECL][TS_FIELD_DECL] = 1;
330 tree_contains_struct[PARM_DECL][TS_PARM_DECL] = 1;
331 tree_contains_struct[LABEL_DECL][TS_LABEL_DECL] = 1;
332 tree_contains_struct[RESULT_DECL][TS_RESULT_DECL] = 1;
333 tree_contains_struct[CONST_DECL][TS_CONST_DECL] = 1;
334 tree_contains_struct[TYPE_DECL][TS_TYPE_DECL] = 1;
335 tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL] = 1;
336 tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL] = 1;
337 tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON] = 1;
339 lang_hooks.init_ts ();
343 /* The name of the object as the assembler will see it (but before any
344 translations made by ASM_OUTPUT_LABELREF). Often this is the same
345 as DECL_NAME. It is an IDENTIFIER_NODE. */
346 tree
347 decl_assembler_name (tree decl)
349 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
350 lang_hooks.set_decl_assembler_name (decl);
351 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
354 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
356 bool
357 decl_assembler_name_equal (tree decl, const_tree asmname)
359 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
360 const char *decl_str;
361 const char *asmname_str;
362 bool test = false;
364 if (decl_asmname == asmname)
365 return true;
367 decl_str = IDENTIFIER_POINTER (decl_asmname);
368 asmname_str = IDENTIFIER_POINTER (asmname);
371 /* If the target assembler name was set by the user, things are trickier.
372 We have a leading '*' to begin with. After that, it's arguable what
373 is the correct thing to do with -fleading-underscore. Arguably, we've
374 historically been doing the wrong thing in assemble_alias by always
375 printing the leading underscore. Since we're not changing that, make
376 sure user_label_prefix follows the '*' before matching. */
377 if (decl_str[0] == '*')
379 size_t ulp_len = strlen (user_label_prefix);
381 decl_str ++;
383 if (ulp_len == 0)
384 test = true;
385 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
386 decl_str += ulp_len, test=true;
387 else
388 decl_str --;
390 if (asmname_str[0] == '*')
392 size_t ulp_len = strlen (user_label_prefix);
394 asmname_str ++;
396 if (ulp_len == 0)
397 test = true;
398 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
399 asmname_str += ulp_len, test=true;
400 else
401 asmname_str --;
404 if (!test)
405 return false;
406 return strcmp (decl_str, asmname_str) == 0;
409 /* Hash asmnames ignoring the user specified marks. */
411 hashval_t
412 decl_assembler_name_hash (const_tree asmname)
414 if (IDENTIFIER_POINTER (asmname)[0] == '*')
416 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
417 size_t ulp_len = strlen (user_label_prefix);
419 if (ulp_len == 0)
421 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
422 decl_str += ulp_len;
424 return htab_hash_string (decl_str);
427 return htab_hash_string (IDENTIFIER_POINTER (asmname));
430 /* Compute the number of bytes occupied by a tree with code CODE.
431 This function cannot be used for nodes that have variable sizes,
432 including TREE_VEC, STRING_CST, and CALL_EXPR. */
433 size_t
434 tree_code_size (enum tree_code code)
436 switch (TREE_CODE_CLASS (code))
438 case tcc_declaration: /* A decl node */
440 switch (code)
442 case FIELD_DECL:
443 return sizeof (struct tree_field_decl);
444 case PARM_DECL:
445 return sizeof (struct tree_parm_decl);
446 case VAR_DECL:
447 return sizeof (struct tree_var_decl);
448 case LABEL_DECL:
449 return sizeof (struct tree_label_decl);
450 case RESULT_DECL:
451 return sizeof (struct tree_result_decl);
452 case CONST_DECL:
453 return sizeof (struct tree_const_decl);
454 case TYPE_DECL:
455 return sizeof (struct tree_type_decl);
456 case FUNCTION_DECL:
457 return sizeof (struct tree_function_decl);
458 default:
459 return sizeof (struct tree_decl_non_common);
463 case tcc_type: /* a type node */
464 return sizeof (struct tree_type);
466 case tcc_reference: /* a reference */
467 case tcc_expression: /* an expression */
468 case tcc_statement: /* an expression with side effects */
469 case tcc_comparison: /* a comparison expression */
470 case tcc_unary: /* a unary arithmetic expression */
471 case tcc_binary: /* a binary arithmetic expression */
472 return (sizeof (struct tree_exp)
473 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
475 case tcc_constant: /* a constant */
476 switch (code)
478 case INTEGER_CST: return sizeof (struct tree_int_cst);
479 case REAL_CST: return sizeof (struct tree_real_cst);
480 case FIXED_CST: return sizeof (struct tree_fixed_cst);
481 case COMPLEX_CST: return sizeof (struct tree_complex);
482 case VECTOR_CST: return sizeof (struct tree_vector);
483 case STRING_CST: gcc_unreachable ();
484 default:
485 return lang_hooks.tree_size (code);
488 case tcc_exceptional: /* something random, like an identifier. */
489 switch (code)
491 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
492 case TREE_LIST: return sizeof (struct tree_list);
494 case ERROR_MARK:
495 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
497 case TREE_VEC:
498 case OMP_CLAUSE: gcc_unreachable ();
500 case SSA_NAME: return sizeof (struct tree_ssa_name);
502 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
503 case BLOCK: return sizeof (struct tree_block);
504 case CONSTRUCTOR: return sizeof (struct tree_constructor);
505 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
506 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
508 default:
509 return lang_hooks.tree_size (code);
512 default:
513 gcc_unreachable ();
517 /* Compute the number of bytes occupied by NODE. This routine only
518 looks at TREE_CODE, except for those nodes that have variable sizes. */
519 size_t
520 tree_size (const_tree node)
522 const enum tree_code code = TREE_CODE (node);
523 switch (code)
525 case TREE_BINFO:
526 return (offsetof (struct tree_binfo, base_binfos)
527 + VEC_embedded_size (tree, BINFO_N_BASE_BINFOS (node)));
529 case TREE_VEC:
530 return (sizeof (struct tree_vec)
531 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
533 case STRING_CST:
534 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
536 case OMP_CLAUSE:
537 return (sizeof (struct tree_omp_clause)
538 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
539 * sizeof (tree));
541 default:
542 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
543 return (sizeof (struct tree_exp)
544 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
545 else
546 return tree_code_size (code);
550 /* Return a newly allocated node of code CODE. For decl and type
551 nodes, some other fields are initialized. The rest of the node is
552 initialized to zero. This function cannot be used for TREE_VEC or
553 OMP_CLAUSE nodes, which is enforced by asserts in tree_code_size.
555 Achoo! I got a code in the node. */
557 tree
558 make_node_stat (enum tree_code code MEM_STAT_DECL)
560 tree t;
561 enum tree_code_class type = TREE_CODE_CLASS (code);
562 size_t length = tree_code_size (code);
563 #ifdef GATHER_STATISTICS
564 tree_node_kind kind;
566 switch (type)
568 case tcc_declaration: /* A decl node */
569 kind = d_kind;
570 break;
572 case tcc_type: /* a type node */
573 kind = t_kind;
574 break;
576 case tcc_statement: /* an expression with side effects */
577 kind = s_kind;
578 break;
580 case tcc_reference: /* a reference */
581 kind = r_kind;
582 break;
584 case tcc_expression: /* an expression */
585 case tcc_comparison: /* a comparison expression */
586 case tcc_unary: /* a unary arithmetic expression */
587 case tcc_binary: /* a binary arithmetic expression */
588 kind = e_kind;
589 break;
591 case tcc_constant: /* a constant */
592 kind = c_kind;
593 break;
595 case tcc_exceptional: /* something random, like an identifier. */
596 switch (code)
598 case IDENTIFIER_NODE:
599 kind = id_kind;
600 break;
602 case TREE_VEC:
603 kind = vec_kind;
604 break;
606 case TREE_BINFO:
607 kind = binfo_kind;
608 break;
610 case SSA_NAME:
611 kind = ssa_name_kind;
612 break;
614 case BLOCK:
615 kind = b_kind;
616 break;
618 case CONSTRUCTOR:
619 kind = constr_kind;
620 break;
622 default:
623 kind = x_kind;
624 break;
626 break;
628 default:
629 gcc_unreachable ();
632 tree_node_counts[(int) kind]++;
633 tree_node_sizes[(int) kind] += length;
634 #endif
636 if (code == IDENTIFIER_NODE)
637 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_id_zone);
638 else
639 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
641 memset (t, 0, length);
643 TREE_SET_CODE (t, code);
645 switch (type)
647 case tcc_statement:
648 TREE_SIDE_EFFECTS (t) = 1;
649 break;
651 case tcc_declaration:
652 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
654 if (code == FUNCTION_DECL)
656 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
657 DECL_MODE (t) = FUNCTION_MODE;
659 else
660 DECL_ALIGN (t) = 1;
661 /* We have not yet computed the alias set for this declaration. */
662 DECL_POINTER_ALIAS_SET (t) = -1;
664 DECL_SOURCE_LOCATION (t) = input_location;
665 DECL_UID (t) = next_decl_uid++;
667 break;
669 case tcc_type:
670 TYPE_UID (t) = next_type_uid++;
671 TYPE_ALIGN (t) = BITS_PER_UNIT;
672 TYPE_USER_ALIGN (t) = 0;
673 TYPE_MAIN_VARIANT (t) = t;
674 TYPE_CANONICAL (t) = t;
676 /* Default to no attributes for type, but let target change that. */
677 TYPE_ATTRIBUTES (t) = NULL_TREE;
678 targetm.set_default_type_attributes (t);
680 /* We have not yet computed the alias set for this type. */
681 TYPE_ALIAS_SET (t) = -1;
682 break;
684 case tcc_constant:
685 TREE_CONSTANT (t) = 1;
686 break;
688 case tcc_expression:
689 switch (code)
691 case INIT_EXPR:
692 case MODIFY_EXPR:
693 case VA_ARG_EXPR:
694 case PREDECREMENT_EXPR:
695 case PREINCREMENT_EXPR:
696 case POSTDECREMENT_EXPR:
697 case POSTINCREMENT_EXPR:
698 /* All of these have side-effects, no matter what their
699 operands are. */
700 TREE_SIDE_EFFECTS (t) = 1;
701 break;
703 default:
704 break;
706 break;
708 default:
709 /* Other classes need no special treatment. */
710 break;
713 return t;
716 /* Return a new node with the same contents as NODE except that its
717 TREE_CHAIN is zero and it has a fresh uid. */
719 tree
720 copy_node_stat (tree node MEM_STAT_DECL)
722 tree t;
723 enum tree_code code = TREE_CODE (node);
724 size_t length;
726 gcc_assert (code != STATEMENT_LIST);
728 length = tree_size (node);
729 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
730 memcpy (t, node, length);
732 TREE_CHAIN (t) = 0;
733 TREE_ASM_WRITTEN (t) = 0;
734 TREE_VISITED (t) = 0;
735 t->base.ann = 0;
737 if (TREE_CODE_CLASS (code) == tcc_declaration)
739 DECL_UID (t) = next_decl_uid++;
740 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
741 && DECL_HAS_VALUE_EXPR_P (node))
743 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
744 DECL_HAS_VALUE_EXPR_P (t) = 1;
746 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
748 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
749 DECL_HAS_INIT_PRIORITY_P (t) = 1;
751 if (TREE_CODE (node) == VAR_DECL && DECL_BASED_ON_RESTRICT_P (node))
753 SET_DECL_RESTRICT_BASE (t, DECL_GET_RESTRICT_BASE (node));
754 DECL_BASED_ON_RESTRICT_P (t) = 1;
757 else if (TREE_CODE_CLASS (code) == tcc_type)
759 TYPE_UID (t) = next_type_uid++;
760 /* The following is so that the debug code for
761 the copy is different from the original type.
762 The two statements usually duplicate each other
763 (because they clear fields of the same union),
764 but the optimizer should catch that. */
765 TYPE_SYMTAB_POINTER (t) = 0;
766 TYPE_SYMTAB_ADDRESS (t) = 0;
768 /* Do not copy the values cache. */
769 if (TYPE_CACHED_VALUES_P(t))
771 TYPE_CACHED_VALUES_P (t) = 0;
772 TYPE_CACHED_VALUES (t) = NULL_TREE;
776 return t;
779 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
780 For example, this can copy a list made of TREE_LIST nodes. */
782 tree
783 copy_list (tree list)
785 tree head;
786 tree prev, next;
788 if (list == 0)
789 return 0;
791 head = prev = copy_node (list);
792 next = TREE_CHAIN (list);
793 while (next)
795 TREE_CHAIN (prev) = copy_node (next);
796 prev = TREE_CHAIN (prev);
797 next = TREE_CHAIN (next);
799 return head;
803 /* Create an INT_CST node with a LOW value sign extended. */
805 tree
806 build_int_cst (tree type, HOST_WIDE_INT low)
808 /* Support legacy code. */
809 if (!type)
810 type = integer_type_node;
812 return build_int_cst_wide (type, low, low < 0 ? -1 : 0);
815 /* Create an INT_CST node with a LOW value zero extended. */
817 tree
818 build_int_cstu (tree type, unsigned HOST_WIDE_INT low)
820 return build_int_cst_wide (type, low, 0);
823 /* Create an INT_CST node with a LOW value in TYPE. The value is sign extended
824 if it is negative. This function is similar to build_int_cst, but
825 the extra bits outside of the type precision are cleared. Constants
826 with these extra bits may confuse the fold so that it detects overflows
827 even in cases when they do not occur, and in general should be avoided.
828 We cannot however make this a default behavior of build_int_cst without
829 more intrusive changes, since there are parts of gcc that rely on the extra
830 precision of the integer constants. */
832 tree
833 build_int_cst_type (tree type, HOST_WIDE_INT low)
835 unsigned HOST_WIDE_INT low1;
836 HOST_WIDE_INT hi;
838 gcc_assert (type);
840 fit_double_type (low, low < 0 ? -1 : 0, &low1, &hi, type);
842 return build_int_cst_wide (type, low1, hi);
845 /* Create an INT_CST node of TYPE and value HI:LOW. The value is truncated
846 and sign extended according to the value range of TYPE. */
848 tree
849 build_int_cst_wide_type (tree type,
850 unsigned HOST_WIDE_INT low, HOST_WIDE_INT high)
852 fit_double_type (low, high, &low, &high, type);
853 return build_int_cst_wide (type, low, high);
856 /* These are the hash table functions for the hash table of INTEGER_CST
857 nodes of a sizetype. */
859 /* Return the hash code code X, an INTEGER_CST. */
861 static hashval_t
862 int_cst_hash_hash (const void *x)
864 const_tree const t = (const_tree) x;
866 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
867 ^ htab_hash_pointer (TREE_TYPE (t)));
870 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
871 is the same as that given by *Y, which is the same. */
873 static int
874 int_cst_hash_eq (const void *x, const void *y)
876 const_tree const xt = (const_tree) x;
877 const_tree const yt = (const_tree) y;
879 return (TREE_TYPE (xt) == TREE_TYPE (yt)
880 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
881 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
884 /* Create an INT_CST node of TYPE and value HI:LOW.
885 The returned node is always shared. For small integers we use a
886 per-type vector cache, for larger ones we use a single hash table. */
888 tree
889 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
891 tree t;
892 int ix = -1;
893 int limit = 0;
895 gcc_assert (type);
897 switch (TREE_CODE (type))
899 case POINTER_TYPE:
900 case REFERENCE_TYPE:
901 /* Cache NULL pointer. */
902 if (!hi && !low)
904 limit = 1;
905 ix = 0;
907 break;
909 case BOOLEAN_TYPE:
910 /* Cache false or true. */
911 limit = 2;
912 if (!hi && low < 2)
913 ix = low;
914 break;
916 case INTEGER_TYPE:
917 case OFFSET_TYPE:
918 if (TYPE_UNSIGNED (type))
920 /* Cache 0..N */
921 limit = INTEGER_SHARE_LIMIT;
922 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
923 ix = low;
925 else
927 /* Cache -1..N */
928 limit = INTEGER_SHARE_LIMIT + 1;
929 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
930 ix = low + 1;
931 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
932 ix = 0;
934 break;
936 case ENUMERAL_TYPE:
937 break;
939 default:
940 gcc_unreachable ();
943 if (ix >= 0)
945 /* Look for it in the type's vector of small shared ints. */
946 if (!TYPE_CACHED_VALUES_P (type))
948 TYPE_CACHED_VALUES_P (type) = 1;
949 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
952 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
953 if (t)
955 /* Make sure no one is clobbering the shared constant. */
956 gcc_assert (TREE_TYPE (t) == type);
957 gcc_assert (TREE_INT_CST_LOW (t) == low);
958 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
960 else
962 /* Create a new shared int. */
963 t = make_node (INTEGER_CST);
965 TREE_INT_CST_LOW (t) = low;
966 TREE_INT_CST_HIGH (t) = hi;
967 TREE_TYPE (t) = type;
969 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
972 else
974 /* Use the cache of larger shared ints. */
975 void **slot;
977 TREE_INT_CST_LOW (int_cst_node) = low;
978 TREE_INT_CST_HIGH (int_cst_node) = hi;
979 TREE_TYPE (int_cst_node) = type;
981 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
982 t = (tree) *slot;
983 if (!t)
985 /* Insert this one into the hash table. */
986 t = int_cst_node;
987 *slot = t;
988 /* Make a new node for next time round. */
989 int_cst_node = make_node (INTEGER_CST);
993 return t;
996 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
997 and the rest are zeros. */
999 tree
1000 build_low_bits_mask (tree type, unsigned bits)
1002 unsigned HOST_WIDE_INT low;
1003 HOST_WIDE_INT high;
1004 unsigned HOST_WIDE_INT all_ones = ~(unsigned HOST_WIDE_INT) 0;
1006 gcc_assert (bits <= TYPE_PRECISION (type));
1008 if (bits == TYPE_PRECISION (type)
1009 && !TYPE_UNSIGNED (type))
1011 /* Sign extended all-ones mask. */
1012 low = all_ones;
1013 high = -1;
1015 else if (bits <= HOST_BITS_PER_WIDE_INT)
1017 low = all_ones >> (HOST_BITS_PER_WIDE_INT - bits);
1018 high = 0;
1020 else
1022 bits -= HOST_BITS_PER_WIDE_INT;
1023 low = all_ones;
1024 high = all_ones >> (HOST_BITS_PER_WIDE_INT - bits);
1027 return build_int_cst_wide (type, low, high);
1030 /* Checks that X is integer constant that can be expressed in (unsigned)
1031 HOST_WIDE_INT without loss of precision. */
1033 bool
1034 cst_and_fits_in_hwi (const_tree x)
1036 if (TREE_CODE (x) != INTEGER_CST)
1037 return false;
1039 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1040 return false;
1042 return (TREE_INT_CST_HIGH (x) == 0
1043 || TREE_INT_CST_HIGH (x) == -1);
1046 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1047 are in a list pointed to by VALS. */
1049 tree
1050 build_vector (tree type, tree vals)
1052 tree v = make_node (VECTOR_CST);
1053 int over = 0;
1054 tree link;
1056 TREE_VECTOR_CST_ELTS (v) = vals;
1057 TREE_TYPE (v) = type;
1059 /* Iterate through elements and check for overflow. */
1060 for (link = vals; link; link = TREE_CHAIN (link))
1062 tree value = TREE_VALUE (link);
1064 /* Don't crash if we get an address constant. */
1065 if (!CONSTANT_CLASS_P (value))
1066 continue;
1068 over |= TREE_OVERFLOW (value);
1071 TREE_OVERFLOW (v) = over;
1072 return v;
1075 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1076 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1078 tree
1079 build_vector_from_ctor (tree type, VEC(constructor_elt,gc) *v)
1081 tree list = NULL_TREE;
1082 unsigned HOST_WIDE_INT idx;
1083 tree value;
1085 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1086 list = tree_cons (NULL_TREE, value, list);
1087 return build_vector (type, nreverse (list));
1090 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1091 are in the VEC pointed to by VALS. */
1092 tree
1093 build_constructor (tree type, VEC(constructor_elt,gc) *vals)
1095 tree c = make_node (CONSTRUCTOR);
1096 TREE_TYPE (c) = type;
1097 CONSTRUCTOR_ELTS (c) = vals;
1098 return c;
1101 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1102 INDEX and VALUE. */
1103 tree
1104 build_constructor_single (tree type, tree index, tree value)
1106 VEC(constructor_elt,gc) *v;
1107 constructor_elt *elt;
1108 tree t;
1110 v = VEC_alloc (constructor_elt, gc, 1);
1111 elt = VEC_quick_push (constructor_elt, v, NULL);
1112 elt->index = index;
1113 elt->value = value;
1115 t = build_constructor (type, v);
1116 TREE_CONSTANT (t) = TREE_CONSTANT (value);
1117 return t;
1121 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1122 are in a list pointed to by VALS. */
1123 tree
1124 build_constructor_from_list (tree type, tree vals)
1126 tree t, val;
1127 VEC(constructor_elt,gc) *v = NULL;
1128 bool constant_p = true;
1130 if (vals)
1132 v = VEC_alloc (constructor_elt, gc, list_length (vals));
1133 for (t = vals; t; t = TREE_CHAIN (t))
1135 constructor_elt *elt = VEC_quick_push (constructor_elt, v, NULL);
1136 val = TREE_VALUE (t);
1137 elt->index = TREE_PURPOSE (t);
1138 elt->value = val;
1139 if (!TREE_CONSTANT (val))
1140 constant_p = false;
1144 t = build_constructor (type, v);
1145 TREE_CONSTANT (t) = constant_p;
1146 return t;
1149 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1151 tree
1152 build_fixed (tree type, FIXED_VALUE_TYPE f)
1154 tree v;
1155 FIXED_VALUE_TYPE *fp;
1157 v = make_node (FIXED_CST);
1158 fp = GGC_NEW (FIXED_VALUE_TYPE);
1159 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1161 TREE_TYPE (v) = type;
1162 TREE_FIXED_CST_PTR (v) = fp;
1163 return v;
1166 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1168 tree
1169 build_real (tree type, REAL_VALUE_TYPE d)
1171 tree v;
1172 REAL_VALUE_TYPE *dp;
1173 int overflow = 0;
1175 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1176 Consider doing it via real_convert now. */
1178 v = make_node (REAL_CST);
1179 dp = GGC_NEW (REAL_VALUE_TYPE);
1180 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1182 TREE_TYPE (v) = type;
1183 TREE_REAL_CST_PTR (v) = dp;
1184 TREE_OVERFLOW (v) = overflow;
1185 return v;
1188 /* Return a new REAL_CST node whose type is TYPE
1189 and whose value is the integer value of the INTEGER_CST node I. */
1191 REAL_VALUE_TYPE
1192 real_value_from_int_cst (const_tree type, const_tree i)
1194 REAL_VALUE_TYPE d;
1196 /* Clear all bits of the real value type so that we can later do
1197 bitwise comparisons to see if two values are the same. */
1198 memset (&d, 0, sizeof d);
1200 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1201 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1202 TYPE_UNSIGNED (TREE_TYPE (i)));
1203 return d;
1206 /* Given a tree representing an integer constant I, return a tree
1207 representing the same value as a floating-point constant of type TYPE. */
1209 tree
1210 build_real_from_int_cst (tree type, const_tree i)
1212 tree v;
1213 int overflow = TREE_OVERFLOW (i);
1215 v = build_real (type, real_value_from_int_cst (type, i));
1217 TREE_OVERFLOW (v) |= overflow;
1218 return v;
1221 /* Return a newly constructed STRING_CST node whose value is
1222 the LEN characters at STR.
1223 The TREE_TYPE is not initialized. */
1225 tree
1226 build_string (int len, const char *str)
1228 tree s;
1229 size_t length;
1231 /* Do not waste bytes provided by padding of struct tree_string. */
1232 length = len + offsetof (struct tree_string, str) + 1;
1234 #ifdef GATHER_STATISTICS
1235 tree_node_counts[(int) c_kind]++;
1236 tree_node_sizes[(int) c_kind] += length;
1237 #endif
1239 s = ggc_alloc_tree (length);
1241 memset (s, 0, sizeof (struct tree_common));
1242 TREE_SET_CODE (s, STRING_CST);
1243 TREE_CONSTANT (s) = 1;
1244 TREE_STRING_LENGTH (s) = len;
1245 memcpy (s->string.str, str, len);
1246 s->string.str[len] = '\0';
1248 return s;
1251 /* Return a newly constructed COMPLEX_CST node whose value is
1252 specified by the real and imaginary parts REAL and IMAG.
1253 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1254 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1256 tree
1257 build_complex (tree type, tree real, tree imag)
1259 tree t = make_node (COMPLEX_CST);
1261 TREE_REALPART (t) = real;
1262 TREE_IMAGPART (t) = imag;
1263 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1264 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1265 return t;
1268 /* Return a constant of arithmetic type TYPE which is the
1269 multiplicative identity of the set TYPE. */
1271 tree
1272 build_one_cst (tree type)
1274 switch (TREE_CODE (type))
1276 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1277 case POINTER_TYPE: case REFERENCE_TYPE:
1278 case OFFSET_TYPE:
1279 return build_int_cst (type, 1);
1281 case REAL_TYPE:
1282 return build_real (type, dconst1);
1284 case FIXED_POINT_TYPE:
1285 /* We can only generate 1 for accum types. */
1286 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1287 return build_fixed (type, FCONST1(TYPE_MODE (type)));
1289 case VECTOR_TYPE:
1291 tree scalar, cst;
1292 int i;
1294 scalar = build_one_cst (TREE_TYPE (type));
1296 /* Create 'vect_cst_ = {cst,cst,...,cst}' */
1297 cst = NULL_TREE;
1298 for (i = TYPE_VECTOR_SUBPARTS (type); --i >= 0; )
1299 cst = tree_cons (NULL_TREE, scalar, cst);
1301 return build_vector (type, cst);
1304 case COMPLEX_TYPE:
1305 return build_complex (type,
1306 build_one_cst (TREE_TYPE (type)),
1307 fold_convert (TREE_TYPE (type), integer_zero_node));
1309 default:
1310 gcc_unreachable ();
1314 /* Build a BINFO with LEN language slots. */
1316 tree
1317 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1319 tree t;
1320 size_t length = (offsetof (struct tree_binfo, base_binfos)
1321 + VEC_embedded_size (tree, base_binfos));
1323 #ifdef GATHER_STATISTICS
1324 tree_node_counts[(int) binfo_kind]++;
1325 tree_node_sizes[(int) binfo_kind] += length;
1326 #endif
1328 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
1330 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1332 TREE_SET_CODE (t, TREE_BINFO);
1334 VEC_embedded_init (tree, BINFO_BASE_BINFOS (t), base_binfos);
1336 return t;
1340 /* Build a newly constructed TREE_VEC node of length LEN. */
1342 tree
1343 make_tree_vec_stat (int len MEM_STAT_DECL)
1345 tree t;
1346 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1348 #ifdef GATHER_STATISTICS
1349 tree_node_counts[(int) vec_kind]++;
1350 tree_node_sizes[(int) vec_kind] += length;
1351 #endif
1353 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
1355 memset (t, 0, length);
1357 TREE_SET_CODE (t, TREE_VEC);
1358 TREE_VEC_LENGTH (t) = len;
1360 return t;
1363 /* Return 1 if EXPR is the integer constant zero or a complex constant
1364 of zero. */
1367 integer_zerop (const_tree expr)
1369 STRIP_NOPS (expr);
1371 return ((TREE_CODE (expr) == INTEGER_CST
1372 && TREE_INT_CST_LOW (expr) == 0
1373 && TREE_INT_CST_HIGH (expr) == 0)
1374 || (TREE_CODE (expr) == COMPLEX_CST
1375 && integer_zerop (TREE_REALPART (expr))
1376 && integer_zerop (TREE_IMAGPART (expr))));
1379 /* Return 1 if EXPR is the integer constant one or the corresponding
1380 complex constant. */
1383 integer_onep (const_tree expr)
1385 STRIP_NOPS (expr);
1387 return ((TREE_CODE (expr) == INTEGER_CST
1388 && TREE_INT_CST_LOW (expr) == 1
1389 && TREE_INT_CST_HIGH (expr) == 0)
1390 || (TREE_CODE (expr) == COMPLEX_CST
1391 && integer_onep (TREE_REALPART (expr))
1392 && integer_zerop (TREE_IMAGPART (expr))));
1395 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1396 it contains. Likewise for the corresponding complex constant. */
1399 integer_all_onesp (const_tree expr)
1401 int prec;
1402 int uns;
1404 STRIP_NOPS (expr);
1406 if (TREE_CODE (expr) == COMPLEX_CST
1407 && integer_all_onesp (TREE_REALPART (expr))
1408 && integer_zerop (TREE_IMAGPART (expr)))
1409 return 1;
1411 else if (TREE_CODE (expr) != INTEGER_CST)
1412 return 0;
1414 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1415 if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1416 && TREE_INT_CST_HIGH (expr) == -1)
1417 return 1;
1418 if (!uns)
1419 return 0;
1421 /* Note that using TYPE_PRECISION here is wrong. We care about the
1422 actual bits, not the (arbitrary) range of the type. */
1423 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)));
1424 if (prec >= HOST_BITS_PER_WIDE_INT)
1426 HOST_WIDE_INT high_value;
1427 int shift_amount;
1429 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1431 /* Can not handle precisions greater than twice the host int size. */
1432 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
1433 if (shift_amount == HOST_BITS_PER_WIDE_INT)
1434 /* Shifting by the host word size is undefined according to the ANSI
1435 standard, so we must handle this as a special case. */
1436 high_value = -1;
1437 else
1438 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1440 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1441 && TREE_INT_CST_HIGH (expr) == high_value);
1443 else
1444 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1447 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1448 one bit on). */
1451 integer_pow2p (const_tree expr)
1453 int prec;
1454 HOST_WIDE_INT high, low;
1456 STRIP_NOPS (expr);
1458 if (TREE_CODE (expr) == COMPLEX_CST
1459 && integer_pow2p (TREE_REALPART (expr))
1460 && integer_zerop (TREE_IMAGPART (expr)))
1461 return 1;
1463 if (TREE_CODE (expr) != INTEGER_CST)
1464 return 0;
1466 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1467 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1468 high = TREE_INT_CST_HIGH (expr);
1469 low = TREE_INT_CST_LOW (expr);
1471 /* First clear all bits that are beyond the type's precision in case
1472 we've been sign extended. */
1474 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
1476 else if (prec > HOST_BITS_PER_WIDE_INT)
1477 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1478 else
1480 high = 0;
1481 if (prec < HOST_BITS_PER_WIDE_INT)
1482 low &= ~((HOST_WIDE_INT) (-1) << prec);
1485 if (high == 0 && low == 0)
1486 return 0;
1488 return ((high == 0 && (low & (low - 1)) == 0)
1489 || (low == 0 && (high & (high - 1)) == 0));
1492 /* Return 1 if EXPR is an integer constant other than zero or a
1493 complex constant other than zero. */
1496 integer_nonzerop (const_tree expr)
1498 STRIP_NOPS (expr);
1500 return ((TREE_CODE (expr) == INTEGER_CST
1501 && (TREE_INT_CST_LOW (expr) != 0
1502 || TREE_INT_CST_HIGH (expr) != 0))
1503 || (TREE_CODE (expr) == COMPLEX_CST
1504 && (integer_nonzerop (TREE_REALPART (expr))
1505 || integer_nonzerop (TREE_IMAGPART (expr)))));
1508 /* Return 1 if EXPR is the fixed-point constant zero. */
1511 fixed_zerop (const_tree expr)
1513 return (TREE_CODE (expr) == FIXED_CST
1514 && double_int_zero_p (TREE_FIXED_CST (expr).data));
1517 /* Return the power of two represented by a tree node known to be a
1518 power of two. */
1521 tree_log2 (const_tree expr)
1523 int prec;
1524 HOST_WIDE_INT high, low;
1526 STRIP_NOPS (expr);
1528 if (TREE_CODE (expr) == COMPLEX_CST)
1529 return tree_log2 (TREE_REALPART (expr));
1531 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1532 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1534 high = TREE_INT_CST_HIGH (expr);
1535 low = TREE_INT_CST_LOW (expr);
1537 /* First clear all bits that are beyond the type's precision in case
1538 we've been sign extended. */
1540 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
1542 else if (prec > HOST_BITS_PER_WIDE_INT)
1543 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1544 else
1546 high = 0;
1547 if (prec < HOST_BITS_PER_WIDE_INT)
1548 low &= ~((HOST_WIDE_INT) (-1) << prec);
1551 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
1552 : exact_log2 (low));
1555 /* Similar, but return the largest integer Y such that 2 ** Y is less
1556 than or equal to EXPR. */
1559 tree_floor_log2 (const_tree expr)
1561 int prec;
1562 HOST_WIDE_INT high, low;
1564 STRIP_NOPS (expr);
1566 if (TREE_CODE (expr) == COMPLEX_CST)
1567 return tree_log2 (TREE_REALPART (expr));
1569 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1570 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1572 high = TREE_INT_CST_HIGH (expr);
1573 low = TREE_INT_CST_LOW (expr);
1575 /* First clear all bits that are beyond the type's precision in case
1576 we've been sign extended. Ignore if type's precision hasn't been set
1577 since what we are doing is setting it. */
1579 if (prec == 2 * HOST_BITS_PER_WIDE_INT || prec == 0)
1581 else if (prec > HOST_BITS_PER_WIDE_INT)
1582 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1583 else
1585 high = 0;
1586 if (prec < HOST_BITS_PER_WIDE_INT)
1587 low &= ~((HOST_WIDE_INT) (-1) << prec);
1590 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
1591 : floor_log2 (low));
1594 /* Return 1 if EXPR is the real constant zero. */
1597 real_zerop (const_tree expr)
1599 STRIP_NOPS (expr);
1601 return ((TREE_CODE (expr) == REAL_CST
1602 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0))
1603 || (TREE_CODE (expr) == COMPLEX_CST
1604 && real_zerop (TREE_REALPART (expr))
1605 && real_zerop (TREE_IMAGPART (expr))));
1608 /* Return 1 if EXPR is the real constant one in real or complex form. */
1611 real_onep (const_tree expr)
1613 STRIP_NOPS (expr);
1615 return ((TREE_CODE (expr) == REAL_CST
1616 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1))
1617 || (TREE_CODE (expr) == COMPLEX_CST
1618 && real_onep (TREE_REALPART (expr))
1619 && real_zerop (TREE_IMAGPART (expr))));
1622 /* Return 1 if EXPR is the real constant two. */
1625 real_twop (const_tree expr)
1627 STRIP_NOPS (expr);
1629 return ((TREE_CODE (expr) == REAL_CST
1630 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2))
1631 || (TREE_CODE (expr) == COMPLEX_CST
1632 && real_twop (TREE_REALPART (expr))
1633 && real_zerop (TREE_IMAGPART (expr))));
1636 /* Return 1 if EXPR is the real constant minus one. */
1639 real_minus_onep (const_tree expr)
1641 STRIP_NOPS (expr);
1643 return ((TREE_CODE (expr) == REAL_CST
1644 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1))
1645 || (TREE_CODE (expr) == COMPLEX_CST
1646 && real_minus_onep (TREE_REALPART (expr))
1647 && real_zerop (TREE_IMAGPART (expr))));
1650 /* Nonzero if EXP is a constant or a cast of a constant. */
1653 really_constant_p (const_tree exp)
1655 /* This is not quite the same as STRIP_NOPS. It does more. */
1656 while (CONVERT_EXPR_P (exp)
1657 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1658 exp = TREE_OPERAND (exp, 0);
1659 return TREE_CONSTANT (exp);
1662 /* Return first list element whose TREE_VALUE is ELEM.
1663 Return 0 if ELEM is not in LIST. */
1665 tree
1666 value_member (tree elem, tree list)
1668 while (list)
1670 if (elem == TREE_VALUE (list))
1671 return list;
1672 list = TREE_CHAIN (list);
1674 return NULL_TREE;
1677 /* Return first list element whose TREE_PURPOSE is ELEM.
1678 Return 0 if ELEM is not in LIST. */
1680 tree
1681 purpose_member (const_tree elem, tree list)
1683 while (list)
1685 if (elem == TREE_PURPOSE (list))
1686 return list;
1687 list = TREE_CHAIN (list);
1689 return NULL_TREE;
1692 /* Return nonzero if ELEM is part of the chain CHAIN. */
1695 chain_member (const_tree elem, const_tree chain)
1697 while (chain)
1699 if (elem == chain)
1700 return 1;
1701 chain = TREE_CHAIN (chain);
1704 return 0;
1707 /* Return the length of a chain of nodes chained through TREE_CHAIN.
1708 We expect a null pointer to mark the end of the chain.
1709 This is the Lisp primitive `length'. */
1712 list_length (const_tree t)
1714 const_tree p = t;
1715 #ifdef ENABLE_TREE_CHECKING
1716 const_tree q = t;
1717 #endif
1718 int len = 0;
1720 while (p)
1722 p = TREE_CHAIN (p);
1723 #ifdef ENABLE_TREE_CHECKING
1724 if (len % 2)
1725 q = TREE_CHAIN (q);
1726 gcc_assert (p != q);
1727 #endif
1728 len++;
1731 return len;
1734 /* Returns the number of FIELD_DECLs in TYPE. */
1737 fields_length (const_tree type)
1739 tree t = TYPE_FIELDS (type);
1740 int count = 0;
1742 for (; t; t = TREE_CHAIN (t))
1743 if (TREE_CODE (t) == FIELD_DECL)
1744 ++count;
1746 return count;
1749 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
1750 by modifying the last node in chain 1 to point to chain 2.
1751 This is the Lisp primitive `nconc'. */
1753 tree
1754 chainon (tree op1, tree op2)
1756 tree t1;
1758 if (!op1)
1759 return op2;
1760 if (!op2)
1761 return op1;
1763 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
1764 continue;
1765 TREE_CHAIN (t1) = op2;
1767 #ifdef ENABLE_TREE_CHECKING
1769 tree t2;
1770 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
1771 gcc_assert (t2 != t1);
1773 #endif
1775 return op1;
1778 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
1780 tree
1781 tree_last (tree chain)
1783 tree next;
1784 if (chain)
1785 while ((next = TREE_CHAIN (chain)))
1786 chain = next;
1787 return chain;
1790 /* Return the node in a chain of nodes whose value is x, NULL if not found. */
1792 tree
1793 tree_find_value (tree chain, tree x)
1795 tree list;
1796 for (list = chain; list; list = TREE_CHAIN (list))
1797 if (TREE_VALUE (list) == x)
1798 return list;
1799 return NULL;
1802 /* Reverse the order of elements in the chain T,
1803 and return the new head of the chain (old last element). */
1805 tree
1806 nreverse (tree t)
1808 tree prev = 0, decl, next;
1809 for (decl = t; decl; decl = next)
1811 next = TREE_CHAIN (decl);
1812 TREE_CHAIN (decl) = prev;
1813 prev = decl;
1815 return prev;
1818 /* Return a newly created TREE_LIST node whose
1819 purpose and value fields are PARM and VALUE. */
1821 tree
1822 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
1824 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
1825 TREE_PURPOSE (t) = parm;
1826 TREE_VALUE (t) = value;
1827 return t;
1830 /* Return a newly created TREE_LIST node whose
1831 purpose and value fields are PURPOSE and VALUE
1832 and whose TREE_CHAIN is CHAIN. */
1834 tree
1835 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
1837 tree node;
1839 node = (tree) ggc_alloc_zone_pass_stat (sizeof (struct tree_list), &tree_zone);
1841 memset (node, 0, sizeof (struct tree_common));
1843 #ifdef GATHER_STATISTICS
1844 tree_node_counts[(int) x_kind]++;
1845 tree_node_sizes[(int) x_kind] += sizeof (struct tree_list);
1846 #endif
1848 TREE_SET_CODE (node, TREE_LIST);
1849 TREE_CHAIN (node) = chain;
1850 TREE_PURPOSE (node) = purpose;
1851 TREE_VALUE (node) = value;
1852 return node;
1855 /* Return the elements of a CONSTRUCTOR as a TREE_LIST. */
1857 tree
1858 ctor_to_list (tree ctor)
1860 tree list = NULL_TREE;
1861 tree *p = &list;
1862 unsigned ix;
1863 tree purpose, val;
1865 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), ix, purpose, val)
1867 *p = build_tree_list (purpose, val);
1868 p = &TREE_CHAIN (*p);
1871 return list;
1874 /* Return the size nominally occupied by an object of type TYPE
1875 when it resides in memory. The value is measured in units of bytes,
1876 and its data type is that normally used for type sizes
1877 (which is the first type created by make_signed_type or
1878 make_unsigned_type). */
1880 tree
1881 size_in_bytes (const_tree type)
1883 tree t;
1885 if (type == error_mark_node)
1886 return integer_zero_node;
1888 type = TYPE_MAIN_VARIANT (type);
1889 t = TYPE_SIZE_UNIT (type);
1891 if (t == 0)
1893 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
1894 return size_zero_node;
1897 return t;
1900 /* Return the size of TYPE (in bytes) as a wide integer
1901 or return -1 if the size can vary or is larger than an integer. */
1903 HOST_WIDE_INT
1904 int_size_in_bytes (const_tree type)
1906 tree t;
1908 if (type == error_mark_node)
1909 return 0;
1911 type = TYPE_MAIN_VARIANT (type);
1912 t = TYPE_SIZE_UNIT (type);
1913 if (t == 0
1914 || TREE_CODE (t) != INTEGER_CST
1915 || TREE_INT_CST_HIGH (t) != 0
1916 /* If the result would appear negative, it's too big to represent. */
1917 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
1918 return -1;
1920 return TREE_INT_CST_LOW (t);
1923 /* Return the maximum size of TYPE (in bytes) as a wide integer
1924 or return -1 if the size can vary or is larger than an integer. */
1926 HOST_WIDE_INT
1927 max_int_size_in_bytes (const_tree type)
1929 HOST_WIDE_INT size = -1;
1930 tree size_tree;
1932 /* If this is an array type, check for a possible MAX_SIZE attached. */
1934 if (TREE_CODE (type) == ARRAY_TYPE)
1936 size_tree = TYPE_ARRAY_MAX_SIZE (type);
1938 if (size_tree && host_integerp (size_tree, 1))
1939 size = tree_low_cst (size_tree, 1);
1942 /* If we still haven't been able to get a size, see if the language
1943 can compute a maximum size. */
1945 if (size == -1)
1947 size_tree = lang_hooks.types.max_size (type);
1949 if (size_tree && host_integerp (size_tree, 1))
1950 size = tree_low_cst (size_tree, 1);
1953 return size;
1956 /* Return the bit position of FIELD, in bits from the start of the record.
1957 This is a tree of type bitsizetype. */
1959 tree
1960 bit_position (const_tree field)
1962 return bit_from_pos (DECL_FIELD_OFFSET (field),
1963 DECL_FIELD_BIT_OFFSET (field));
1966 /* Likewise, but return as an integer. It must be representable in
1967 that way (since it could be a signed value, we don't have the
1968 option of returning -1 like int_size_in_byte can. */
1970 HOST_WIDE_INT
1971 int_bit_position (const_tree field)
1973 return tree_low_cst (bit_position (field), 0);
1976 /* Return the byte position of FIELD, in bytes from the start of the record.
1977 This is a tree of type sizetype. */
1979 tree
1980 byte_position (const_tree field)
1982 return byte_from_pos (DECL_FIELD_OFFSET (field),
1983 DECL_FIELD_BIT_OFFSET (field));
1986 /* Likewise, but return as an integer. It must be representable in
1987 that way (since it could be a signed value, we don't have the
1988 option of returning -1 like int_size_in_byte can. */
1990 HOST_WIDE_INT
1991 int_byte_position (const_tree field)
1993 return tree_low_cst (byte_position (field), 0);
1996 /* Return the strictest alignment, in bits, that T is known to have. */
1998 unsigned int
1999 expr_align (const_tree t)
2001 unsigned int align0, align1;
2003 switch (TREE_CODE (t))
2005 CASE_CONVERT: case NON_LVALUE_EXPR:
2006 /* If we have conversions, we know that the alignment of the
2007 object must meet each of the alignments of the types. */
2008 align0 = expr_align (TREE_OPERAND (t, 0));
2009 align1 = TYPE_ALIGN (TREE_TYPE (t));
2010 return MAX (align0, align1);
2012 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2013 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2014 case CLEANUP_POINT_EXPR:
2015 /* These don't change the alignment of an object. */
2016 return expr_align (TREE_OPERAND (t, 0));
2018 case COND_EXPR:
2019 /* The best we can do is say that the alignment is the least aligned
2020 of the two arms. */
2021 align0 = expr_align (TREE_OPERAND (t, 1));
2022 align1 = expr_align (TREE_OPERAND (t, 2));
2023 return MIN (align0, align1);
2025 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2026 meaningfully, it's always 1. */
2027 case LABEL_DECL: case CONST_DECL:
2028 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2029 case FUNCTION_DECL:
2030 gcc_assert (DECL_ALIGN (t) != 0);
2031 return DECL_ALIGN (t);
2033 default:
2034 break;
2037 /* Otherwise take the alignment from that of the type. */
2038 return TYPE_ALIGN (TREE_TYPE (t));
2041 /* Return, as a tree node, the number of elements for TYPE (which is an
2042 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2044 tree
2045 array_type_nelts (const_tree type)
2047 tree index_type, min, max;
2049 /* If they did it with unspecified bounds, then we should have already
2050 given an error about it before we got here. */
2051 if (! TYPE_DOMAIN (type))
2052 return error_mark_node;
2054 index_type = TYPE_DOMAIN (type);
2055 min = TYPE_MIN_VALUE (index_type);
2056 max = TYPE_MAX_VALUE (index_type);
2058 return (integer_zerop (min)
2059 ? max
2060 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2063 /* If arg is static -- a reference to an object in static storage -- then
2064 return the object. This is not the same as the C meaning of `static'.
2065 If arg isn't static, return NULL. */
2067 tree
2068 staticp (tree arg)
2070 switch (TREE_CODE (arg))
2072 case FUNCTION_DECL:
2073 /* Nested functions are static, even though taking their address will
2074 involve a trampoline as we unnest the nested function and create
2075 the trampoline on the tree level. */
2076 return arg;
2078 case VAR_DECL:
2079 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2080 && ! DECL_THREAD_LOCAL_P (arg)
2081 && ! DECL_DLLIMPORT_P (arg)
2082 ? arg : NULL);
2084 case CONST_DECL:
2085 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2086 ? arg : NULL);
2088 case CONSTRUCTOR:
2089 return TREE_STATIC (arg) ? arg : NULL;
2091 case LABEL_DECL:
2092 case STRING_CST:
2093 return arg;
2095 case COMPONENT_REF:
2096 /* If the thing being referenced is not a field, then it is
2097 something language specific. */
2098 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2100 /* If we are referencing a bitfield, we can't evaluate an
2101 ADDR_EXPR at compile time and so it isn't a constant. */
2102 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2103 return NULL;
2105 return staticp (TREE_OPERAND (arg, 0));
2107 case BIT_FIELD_REF:
2108 return NULL;
2110 case MISALIGNED_INDIRECT_REF:
2111 case ALIGN_INDIRECT_REF:
2112 case INDIRECT_REF:
2113 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2115 case ARRAY_REF:
2116 case ARRAY_RANGE_REF:
2117 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2118 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2119 return staticp (TREE_OPERAND (arg, 0));
2120 else
2121 return NULL;
2123 case COMPOUND_LITERAL_EXPR:
2124 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2126 default:
2127 return NULL;
2134 /* Return whether OP is a DECL whose address is function-invariant. */
2136 bool
2137 decl_address_invariant_p (const_tree op)
2139 /* The conditions below are slightly less strict than the one in
2140 staticp. */
2142 switch (TREE_CODE (op))
2144 case PARM_DECL:
2145 case RESULT_DECL:
2146 case LABEL_DECL:
2147 case FUNCTION_DECL:
2148 return true;
2150 case VAR_DECL:
2151 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2152 && !DECL_DLLIMPORT_P (op))
2153 || DECL_THREAD_LOCAL_P (op)
2154 || DECL_CONTEXT (op) == current_function_decl
2155 || decl_function_context (op) == current_function_decl)
2156 return true;
2157 break;
2159 case CONST_DECL:
2160 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2161 || decl_function_context (op) == current_function_decl)
2162 return true;
2163 break;
2165 default:
2166 break;
2169 return false;
2172 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2174 bool
2175 decl_address_ip_invariant_p (const_tree op)
2177 /* The conditions below are slightly less strict than the one in
2178 staticp. */
2180 switch (TREE_CODE (op))
2182 case LABEL_DECL:
2183 case FUNCTION_DECL:
2184 case STRING_CST:
2185 return true;
2187 case VAR_DECL:
2188 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2189 && !DECL_DLLIMPORT_P (op))
2190 || DECL_THREAD_LOCAL_P (op))
2191 return true;
2192 break;
2194 case CONST_DECL:
2195 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2196 return true;
2197 break;
2199 default:
2200 break;
2203 return false;
2207 /* Return true if T is function-invariant (internal function, does
2208 not handle arithmetic; that's handled in skip_simple_arithmetic and
2209 tree_invariant_p). */
2211 static bool tree_invariant_p (tree t);
2213 static bool
2214 tree_invariant_p_1 (tree t)
2216 tree op;
2218 if (TREE_CONSTANT (t)
2219 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2220 return true;
2222 switch (TREE_CODE (t))
2224 case SAVE_EXPR:
2225 return true;
2227 case ADDR_EXPR:
2228 op = TREE_OPERAND (t, 0);
2229 while (handled_component_p (op))
2231 switch (TREE_CODE (op))
2233 case ARRAY_REF:
2234 case ARRAY_RANGE_REF:
2235 if (!tree_invariant_p (TREE_OPERAND (op, 1))
2236 || TREE_OPERAND (op, 2) != NULL_TREE
2237 || TREE_OPERAND (op, 3) != NULL_TREE)
2238 return false;
2239 break;
2241 case COMPONENT_REF:
2242 if (TREE_OPERAND (op, 2) != NULL_TREE)
2243 return false;
2244 break;
2246 default:;
2248 op = TREE_OPERAND (op, 0);
2251 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2253 default:
2254 break;
2257 return false;
2260 /* Return true if T is function-invariant. */
2262 static bool
2263 tree_invariant_p (tree t)
2265 tree inner = skip_simple_arithmetic (t);
2266 return tree_invariant_p_1 (inner);
2269 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2270 Do this to any expression which may be used in more than one place,
2271 but must be evaluated only once.
2273 Normally, expand_expr would reevaluate the expression each time.
2274 Calling save_expr produces something that is evaluated and recorded
2275 the first time expand_expr is called on it. Subsequent calls to
2276 expand_expr just reuse the recorded value.
2278 The call to expand_expr that generates code that actually computes
2279 the value is the first call *at compile time*. Subsequent calls
2280 *at compile time* generate code to use the saved value.
2281 This produces correct result provided that *at run time* control
2282 always flows through the insns made by the first expand_expr
2283 before reaching the other places where the save_expr was evaluated.
2284 You, the caller of save_expr, must make sure this is so.
2286 Constants, and certain read-only nodes, are returned with no
2287 SAVE_EXPR because that is safe. Expressions containing placeholders
2288 are not touched; see tree.def for an explanation of what these
2289 are used for. */
2291 tree
2292 save_expr (tree expr)
2294 tree t = fold (expr);
2295 tree inner;
2297 /* If the tree evaluates to a constant, then we don't want to hide that
2298 fact (i.e. this allows further folding, and direct checks for constants).
2299 However, a read-only object that has side effects cannot be bypassed.
2300 Since it is no problem to reevaluate literals, we just return the
2301 literal node. */
2302 inner = skip_simple_arithmetic (t);
2303 if (TREE_CODE (inner) == ERROR_MARK)
2304 return inner;
2306 if (tree_invariant_p_1 (inner))
2307 return t;
2309 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2310 it means that the size or offset of some field of an object depends on
2311 the value within another field.
2313 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2314 and some variable since it would then need to be both evaluated once and
2315 evaluated more than once. Front-ends must assure this case cannot
2316 happen by surrounding any such subexpressions in their own SAVE_EXPR
2317 and forcing evaluation at the proper time. */
2318 if (contains_placeholder_p (inner))
2319 return t;
2321 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
2323 /* This expression might be placed ahead of a jump to ensure that the
2324 value was computed on both sides of the jump. So make sure it isn't
2325 eliminated as dead. */
2326 TREE_SIDE_EFFECTS (t) = 1;
2327 return t;
2330 /* Look inside EXPR and into any simple arithmetic operations. Return
2331 the innermost non-arithmetic node. */
2333 tree
2334 skip_simple_arithmetic (tree expr)
2336 tree inner;
2338 /* We don't care about whether this can be used as an lvalue in this
2339 context. */
2340 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
2341 expr = TREE_OPERAND (expr, 0);
2343 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
2344 a constant, it will be more efficient to not make another SAVE_EXPR since
2345 it will allow better simplification and GCSE will be able to merge the
2346 computations if they actually occur. */
2347 inner = expr;
2348 while (1)
2350 if (UNARY_CLASS_P (inner))
2351 inner = TREE_OPERAND (inner, 0);
2352 else if (BINARY_CLASS_P (inner))
2354 if (tree_invariant_p (TREE_OPERAND (inner, 1)))
2355 inner = TREE_OPERAND (inner, 0);
2356 else if (tree_invariant_p (TREE_OPERAND (inner, 0)))
2357 inner = TREE_OPERAND (inner, 1);
2358 else
2359 break;
2361 else
2362 break;
2365 return inner;
2368 /* Return which tree structure is used by T. */
2370 enum tree_node_structure_enum
2371 tree_node_structure (const_tree t)
2373 const enum tree_code code = TREE_CODE (t);
2375 switch (TREE_CODE_CLASS (code))
2377 case tcc_declaration:
2379 switch (code)
2381 case FIELD_DECL:
2382 return TS_FIELD_DECL;
2383 case PARM_DECL:
2384 return TS_PARM_DECL;
2385 case VAR_DECL:
2386 return TS_VAR_DECL;
2387 case LABEL_DECL:
2388 return TS_LABEL_DECL;
2389 case RESULT_DECL:
2390 return TS_RESULT_DECL;
2391 case CONST_DECL:
2392 return TS_CONST_DECL;
2393 case TYPE_DECL:
2394 return TS_TYPE_DECL;
2395 case FUNCTION_DECL:
2396 return TS_FUNCTION_DECL;
2397 default:
2398 return TS_DECL_NON_COMMON;
2401 case tcc_type:
2402 return TS_TYPE;
2403 case tcc_reference:
2404 case tcc_comparison:
2405 case tcc_unary:
2406 case tcc_binary:
2407 case tcc_expression:
2408 case tcc_statement:
2409 case tcc_vl_exp:
2410 return TS_EXP;
2411 default: /* tcc_constant and tcc_exceptional */
2412 break;
2414 switch (code)
2416 /* tcc_constant cases. */
2417 case INTEGER_CST: return TS_INT_CST;
2418 case REAL_CST: return TS_REAL_CST;
2419 case FIXED_CST: return TS_FIXED_CST;
2420 case COMPLEX_CST: return TS_COMPLEX;
2421 case VECTOR_CST: return TS_VECTOR;
2422 case STRING_CST: return TS_STRING;
2423 /* tcc_exceptional cases. */
2424 case ERROR_MARK: return TS_COMMON;
2425 case IDENTIFIER_NODE: return TS_IDENTIFIER;
2426 case TREE_LIST: return TS_LIST;
2427 case TREE_VEC: return TS_VEC;
2428 case SSA_NAME: return TS_SSA_NAME;
2429 case PLACEHOLDER_EXPR: return TS_COMMON;
2430 case STATEMENT_LIST: return TS_STATEMENT_LIST;
2431 case BLOCK: return TS_BLOCK;
2432 case CONSTRUCTOR: return TS_CONSTRUCTOR;
2433 case TREE_BINFO: return TS_BINFO;
2434 case OMP_CLAUSE: return TS_OMP_CLAUSE;
2435 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
2436 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
2438 default:
2439 gcc_unreachable ();
2443 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
2444 or offset that depends on a field within a record. */
2446 bool
2447 contains_placeholder_p (const_tree exp)
2449 enum tree_code code;
2451 if (!exp)
2452 return 0;
2454 code = TREE_CODE (exp);
2455 if (code == PLACEHOLDER_EXPR)
2456 return 1;
2458 switch (TREE_CODE_CLASS (code))
2460 case tcc_reference:
2461 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
2462 position computations since they will be converted into a
2463 WITH_RECORD_EXPR involving the reference, which will assume
2464 here will be valid. */
2465 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2467 case tcc_exceptional:
2468 if (code == TREE_LIST)
2469 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
2470 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
2471 break;
2473 case tcc_unary:
2474 case tcc_binary:
2475 case tcc_comparison:
2476 case tcc_expression:
2477 switch (code)
2479 case COMPOUND_EXPR:
2480 /* Ignoring the first operand isn't quite right, but works best. */
2481 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
2483 case COND_EXPR:
2484 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2485 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
2486 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
2488 case SAVE_EXPR:
2489 /* The save_expr function never wraps anything containing
2490 a PLACEHOLDER_EXPR. */
2491 return 0;
2493 default:
2494 break;
2497 switch (TREE_CODE_LENGTH (code))
2499 case 1:
2500 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2501 case 2:
2502 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2503 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
2504 default:
2505 return 0;
2508 case tcc_vl_exp:
2509 switch (code)
2511 case CALL_EXPR:
2513 const_tree arg;
2514 const_call_expr_arg_iterator iter;
2515 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
2516 if (CONTAINS_PLACEHOLDER_P (arg))
2517 return 1;
2518 return 0;
2520 default:
2521 return 0;
2524 default:
2525 return 0;
2527 return 0;
2530 /* Return true if any part of the computation of TYPE involves a
2531 PLACEHOLDER_EXPR. This includes size, bounds, qualifiers
2532 (for QUAL_UNION_TYPE) and field positions. */
2534 static bool
2535 type_contains_placeholder_1 (const_tree type)
2537 /* If the size contains a placeholder or the parent type (component type in
2538 the case of arrays) type involves a placeholder, this type does. */
2539 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
2540 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
2541 || (TREE_TYPE (type) != 0
2542 && type_contains_placeholder_p (TREE_TYPE (type))))
2543 return true;
2545 /* Now do type-specific checks. Note that the last part of the check above
2546 greatly limits what we have to do below. */
2547 switch (TREE_CODE (type))
2549 case VOID_TYPE:
2550 case COMPLEX_TYPE:
2551 case ENUMERAL_TYPE:
2552 case BOOLEAN_TYPE:
2553 case POINTER_TYPE:
2554 case OFFSET_TYPE:
2555 case REFERENCE_TYPE:
2556 case METHOD_TYPE:
2557 case FUNCTION_TYPE:
2558 case VECTOR_TYPE:
2559 return false;
2561 case INTEGER_TYPE:
2562 case REAL_TYPE:
2563 case FIXED_POINT_TYPE:
2564 /* Here we just check the bounds. */
2565 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
2566 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
2568 case ARRAY_TYPE:
2569 /* We're already checked the component type (TREE_TYPE), so just check
2570 the index type. */
2571 return type_contains_placeholder_p (TYPE_DOMAIN (type));
2573 case RECORD_TYPE:
2574 case UNION_TYPE:
2575 case QUAL_UNION_TYPE:
2577 tree field;
2579 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2580 if (TREE_CODE (field) == FIELD_DECL
2581 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
2582 || (TREE_CODE (type) == QUAL_UNION_TYPE
2583 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
2584 || type_contains_placeholder_p (TREE_TYPE (field))))
2585 return true;
2587 return false;
2590 default:
2591 gcc_unreachable ();
2595 bool
2596 type_contains_placeholder_p (tree type)
2598 bool result;
2600 /* If the contains_placeholder_bits field has been initialized,
2601 then we know the answer. */
2602 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
2603 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
2605 /* Indicate that we've seen this type node, and the answer is false.
2606 This is what we want to return if we run into recursion via fields. */
2607 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
2609 /* Compute the real value. */
2610 result = type_contains_placeholder_1 (type);
2612 /* Store the real value. */
2613 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
2615 return result;
2618 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
2619 return a tree with all occurrences of references to F in a
2620 PLACEHOLDER_EXPR replaced by R. Note that we assume here that EXP
2621 contains only arithmetic expressions or a CALL_EXPR with a
2622 PLACEHOLDER_EXPR occurring only in its arglist. */
2624 tree
2625 substitute_in_expr (tree exp, tree f, tree r)
2627 enum tree_code code = TREE_CODE (exp);
2628 tree op0, op1, op2, op3;
2629 tree new_tree, inner;
2631 /* We handle TREE_LIST and COMPONENT_REF separately. */
2632 if (code == TREE_LIST)
2634 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
2635 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
2636 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2637 return exp;
2639 return tree_cons (TREE_PURPOSE (exp), op1, op0);
2641 else if (code == COMPONENT_REF)
2643 /* If this expression is getting a value from a PLACEHOLDER_EXPR
2644 and it is the right field, replace it with R. */
2645 for (inner = TREE_OPERAND (exp, 0);
2646 REFERENCE_CLASS_P (inner);
2647 inner = TREE_OPERAND (inner, 0))
2649 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2650 && TREE_OPERAND (exp, 1) == f)
2651 return r;
2653 /* If this expression hasn't been completed let, leave it alone. */
2654 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && TREE_TYPE (inner) == 0)
2655 return exp;
2657 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2658 if (op0 == TREE_OPERAND (exp, 0))
2659 return exp;
2661 new_tree = fold_build3 (COMPONENT_REF, TREE_TYPE (exp),
2662 op0, TREE_OPERAND (exp, 1), NULL_TREE);
2664 else
2665 switch (TREE_CODE_CLASS (code))
2667 case tcc_constant:
2668 case tcc_declaration:
2669 return exp;
2671 case tcc_exceptional:
2672 case tcc_unary:
2673 case tcc_binary:
2674 case tcc_comparison:
2675 case tcc_expression:
2676 case tcc_reference:
2677 switch (TREE_CODE_LENGTH (code))
2679 case 0:
2680 return exp;
2682 case 1:
2683 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2684 if (op0 == TREE_OPERAND (exp, 0))
2685 return exp;
2687 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
2688 break;
2690 case 2:
2691 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2692 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2694 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2695 return exp;
2697 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
2698 break;
2700 case 3:
2701 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2702 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2703 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
2705 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2706 && op2 == TREE_OPERAND (exp, 2))
2707 return exp;
2709 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
2710 break;
2712 case 4:
2713 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2714 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2715 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
2716 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
2718 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2719 && op2 == TREE_OPERAND (exp, 2)
2720 && op3 == TREE_OPERAND (exp, 3))
2721 return exp;
2723 new_tree = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
2724 break;
2726 default:
2727 gcc_unreachable ();
2729 break;
2731 case tcc_vl_exp:
2733 tree copy = NULL_TREE;
2734 int i;
2736 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
2738 tree op = TREE_OPERAND (exp, i);
2739 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
2740 if (new_op != op)
2742 if (!copy)
2743 copy = copy_node (exp);
2744 TREE_OPERAND (copy, i) = new_op;
2748 if (copy)
2749 new_tree = fold (copy);
2750 else
2751 return exp;
2753 break;
2755 default:
2756 gcc_unreachable ();
2759 TREE_READONLY (new_tree) = TREE_READONLY (exp);
2760 return new_tree;
2763 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
2764 for it within OBJ, a tree that is an object or a chain of references. */
2766 tree
2767 substitute_placeholder_in_expr (tree exp, tree obj)
2769 enum tree_code code = TREE_CODE (exp);
2770 tree op0, op1, op2, op3;
2772 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
2773 in the chain of OBJ. */
2774 if (code == PLACEHOLDER_EXPR)
2776 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
2777 tree elt;
2779 for (elt = obj; elt != 0;
2780 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
2781 || TREE_CODE (elt) == COND_EXPR)
2782 ? TREE_OPERAND (elt, 1)
2783 : (REFERENCE_CLASS_P (elt)
2784 || UNARY_CLASS_P (elt)
2785 || BINARY_CLASS_P (elt)
2786 || VL_EXP_CLASS_P (elt)
2787 || EXPRESSION_CLASS_P (elt))
2788 ? TREE_OPERAND (elt, 0) : 0))
2789 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
2790 return elt;
2792 for (elt = obj; elt != 0;
2793 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
2794 || TREE_CODE (elt) == COND_EXPR)
2795 ? TREE_OPERAND (elt, 1)
2796 : (REFERENCE_CLASS_P (elt)
2797 || UNARY_CLASS_P (elt)
2798 || BINARY_CLASS_P (elt)
2799 || VL_EXP_CLASS_P (elt)
2800 || EXPRESSION_CLASS_P (elt))
2801 ? TREE_OPERAND (elt, 0) : 0))
2802 if (POINTER_TYPE_P (TREE_TYPE (elt))
2803 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
2804 == need_type))
2805 return fold_build1 (INDIRECT_REF, need_type, elt);
2807 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
2808 survives until RTL generation, there will be an error. */
2809 return exp;
2812 /* TREE_LIST is special because we need to look at TREE_VALUE
2813 and TREE_CHAIN, not TREE_OPERANDS. */
2814 else if (code == TREE_LIST)
2816 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
2817 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
2818 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2819 return exp;
2821 return tree_cons (TREE_PURPOSE (exp), op1, op0);
2823 else
2824 switch (TREE_CODE_CLASS (code))
2826 case tcc_constant:
2827 case tcc_declaration:
2828 return exp;
2830 case tcc_exceptional:
2831 case tcc_unary:
2832 case tcc_binary:
2833 case tcc_comparison:
2834 case tcc_expression:
2835 case tcc_reference:
2836 case tcc_statement:
2837 switch (TREE_CODE_LENGTH (code))
2839 case 0:
2840 return exp;
2842 case 1:
2843 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2844 if (op0 == TREE_OPERAND (exp, 0))
2845 return exp;
2846 else
2847 return fold_build1 (code, TREE_TYPE (exp), op0);
2849 case 2:
2850 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2851 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2853 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2854 return exp;
2855 else
2856 return fold_build2 (code, TREE_TYPE (exp), op0, op1);
2858 case 3:
2859 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2860 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2861 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
2863 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2864 && op2 == TREE_OPERAND (exp, 2))
2865 return exp;
2866 else
2867 return fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
2869 case 4:
2870 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2871 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2872 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
2873 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
2875 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2876 && op2 == TREE_OPERAND (exp, 2)
2877 && op3 == TREE_OPERAND (exp, 3))
2878 return exp;
2879 else
2880 return fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
2882 default:
2883 gcc_unreachable ();
2885 break;
2887 case tcc_vl_exp:
2889 tree copy = NULL_TREE;
2890 int i;
2892 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
2894 tree op = TREE_OPERAND (exp, i);
2895 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
2896 if (new_op != op)
2898 if (!copy)
2899 copy = copy_node (exp);
2900 TREE_OPERAND (copy, i) = new_op;
2904 if (copy)
2905 return fold (copy);
2906 else
2907 return exp;
2910 default:
2911 gcc_unreachable ();
2915 /* Stabilize a reference so that we can use it any number of times
2916 without causing its operands to be evaluated more than once.
2917 Returns the stabilized reference. This works by means of save_expr,
2918 so see the caveats in the comments about save_expr.
2920 Also allows conversion expressions whose operands are references.
2921 Any other kind of expression is returned unchanged. */
2923 tree
2924 stabilize_reference (tree ref)
2926 tree result;
2927 enum tree_code code = TREE_CODE (ref);
2929 switch (code)
2931 case VAR_DECL:
2932 case PARM_DECL:
2933 case RESULT_DECL:
2934 /* No action is needed in this case. */
2935 return ref;
2937 CASE_CONVERT:
2938 case FLOAT_EXPR:
2939 case FIX_TRUNC_EXPR:
2940 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2941 break;
2943 case INDIRECT_REF:
2944 result = build_nt (INDIRECT_REF,
2945 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2946 break;
2948 case COMPONENT_REF:
2949 result = build_nt (COMPONENT_REF,
2950 stabilize_reference (TREE_OPERAND (ref, 0)),
2951 TREE_OPERAND (ref, 1), NULL_TREE);
2952 break;
2954 case BIT_FIELD_REF:
2955 result = build_nt (BIT_FIELD_REF,
2956 stabilize_reference (TREE_OPERAND (ref, 0)),
2957 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2958 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2959 break;
2961 case ARRAY_REF:
2962 result = build_nt (ARRAY_REF,
2963 stabilize_reference (TREE_OPERAND (ref, 0)),
2964 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2965 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
2966 break;
2968 case ARRAY_RANGE_REF:
2969 result = build_nt (ARRAY_RANGE_REF,
2970 stabilize_reference (TREE_OPERAND (ref, 0)),
2971 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2972 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
2973 break;
2975 case COMPOUND_EXPR:
2976 /* We cannot wrap the first expression in a SAVE_EXPR, as then
2977 it wouldn't be ignored. This matters when dealing with
2978 volatiles. */
2979 return stabilize_reference_1 (ref);
2981 /* If arg isn't a kind of lvalue we recognize, make no change.
2982 Caller should recognize the error for an invalid lvalue. */
2983 default:
2984 return ref;
2986 case ERROR_MARK:
2987 return error_mark_node;
2990 TREE_TYPE (result) = TREE_TYPE (ref);
2991 TREE_READONLY (result) = TREE_READONLY (ref);
2992 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
2993 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
2995 return result;
2998 /* Subroutine of stabilize_reference; this is called for subtrees of
2999 references. Any expression with side-effects must be put in a SAVE_EXPR
3000 to ensure that it is only evaluated once.
3002 We don't put SAVE_EXPR nodes around everything, because assigning very
3003 simple expressions to temporaries causes us to miss good opportunities
3004 for optimizations. Among other things, the opportunity to fold in the
3005 addition of a constant into an addressing mode often gets lost, e.g.
3006 "y[i+1] += x;". In general, we take the approach that we should not make
3007 an assignment unless we are forced into it - i.e., that any non-side effect
3008 operator should be allowed, and that cse should take care of coalescing
3009 multiple utterances of the same expression should that prove fruitful. */
3011 tree
3012 stabilize_reference_1 (tree e)
3014 tree result;
3015 enum tree_code code = TREE_CODE (e);
3017 /* We cannot ignore const expressions because it might be a reference
3018 to a const array but whose index contains side-effects. But we can
3019 ignore things that are actual constant or that already have been
3020 handled by this function. */
3022 if (tree_invariant_p (e))
3023 return e;
3025 switch (TREE_CODE_CLASS (code))
3027 case tcc_exceptional:
3028 case tcc_type:
3029 case tcc_declaration:
3030 case tcc_comparison:
3031 case tcc_statement:
3032 case tcc_expression:
3033 case tcc_reference:
3034 case tcc_vl_exp:
3035 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3036 so that it will only be evaluated once. */
3037 /* The reference (r) and comparison (<) classes could be handled as
3038 below, but it is generally faster to only evaluate them once. */
3039 if (TREE_SIDE_EFFECTS (e))
3040 return save_expr (e);
3041 return e;
3043 case tcc_constant:
3044 /* Constants need no processing. In fact, we should never reach
3045 here. */
3046 return e;
3048 case tcc_binary:
3049 /* Division is slow and tends to be compiled with jumps,
3050 especially the division by powers of 2 that is often
3051 found inside of an array reference. So do it just once. */
3052 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3053 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3054 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3055 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3056 return save_expr (e);
3057 /* Recursively stabilize each operand. */
3058 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3059 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3060 break;
3062 case tcc_unary:
3063 /* Recursively stabilize each operand. */
3064 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3065 break;
3067 default:
3068 gcc_unreachable ();
3071 TREE_TYPE (result) = TREE_TYPE (e);
3072 TREE_READONLY (result) = TREE_READONLY (e);
3073 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3074 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3076 return result;
3079 /* Low-level constructors for expressions. */
3081 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
3082 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
3084 void
3085 recompute_tree_invariant_for_addr_expr (tree t)
3087 tree node;
3088 bool tc = true, se = false;
3090 /* We started out assuming this address is both invariant and constant, but
3091 does not have side effects. Now go down any handled components and see if
3092 any of them involve offsets that are either non-constant or non-invariant.
3093 Also check for side-effects.
3095 ??? Note that this code makes no attempt to deal with the case where
3096 taking the address of something causes a copy due to misalignment. */
3098 #define UPDATE_FLAGS(NODE) \
3099 do { tree _node = (NODE); \
3100 if (_node && !TREE_CONSTANT (_node)) tc = false; \
3101 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3103 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3104 node = TREE_OPERAND (node, 0))
3106 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3107 array reference (probably made temporarily by the G++ front end),
3108 so ignore all the operands. */
3109 if ((TREE_CODE (node) == ARRAY_REF
3110 || TREE_CODE (node) == ARRAY_RANGE_REF)
3111 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
3113 UPDATE_FLAGS (TREE_OPERAND (node, 1));
3114 if (TREE_OPERAND (node, 2))
3115 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3116 if (TREE_OPERAND (node, 3))
3117 UPDATE_FLAGS (TREE_OPERAND (node, 3));
3119 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
3120 FIELD_DECL, apparently. The G++ front end can put something else
3121 there, at least temporarily. */
3122 else if (TREE_CODE (node) == COMPONENT_REF
3123 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
3125 if (TREE_OPERAND (node, 2))
3126 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3128 else if (TREE_CODE (node) == BIT_FIELD_REF)
3129 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3132 node = lang_hooks.expr_to_decl (node, &tc, &se);
3134 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
3135 the address, since &(*a)->b is a form of addition. If it's a constant, the
3136 address is constant too. If it's a decl, its address is constant if the
3137 decl is static. Everything else is not constant and, furthermore,
3138 taking the address of a volatile variable is not volatile. */
3139 if (TREE_CODE (node) == INDIRECT_REF)
3140 UPDATE_FLAGS (TREE_OPERAND (node, 0));
3141 else if (CONSTANT_CLASS_P (node))
3143 else if (DECL_P (node))
3144 tc &= (staticp (node) != NULL_TREE);
3145 else
3147 tc = false;
3148 se |= TREE_SIDE_EFFECTS (node);
3152 TREE_CONSTANT (t) = tc;
3153 TREE_SIDE_EFFECTS (t) = se;
3154 #undef UPDATE_FLAGS
3157 /* Build an expression of code CODE, data type TYPE, and operands as
3158 specified. Expressions and reference nodes can be created this way.
3159 Constants, decls, types and misc nodes cannot be.
3161 We define 5 non-variadic functions, from 0 to 4 arguments. This is
3162 enough for all extant tree codes. */
3164 tree
3165 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
3167 tree t;
3169 gcc_assert (TREE_CODE_LENGTH (code) == 0);
3171 t = make_node_stat (code PASS_MEM_STAT);
3172 TREE_TYPE (t) = tt;
3174 return t;
3177 tree
3178 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
3180 int length = sizeof (struct tree_exp);
3181 #ifdef GATHER_STATISTICS
3182 tree_node_kind kind;
3183 #endif
3184 tree t;
3186 #ifdef GATHER_STATISTICS
3187 switch (TREE_CODE_CLASS (code))
3189 case tcc_statement: /* an expression with side effects */
3190 kind = s_kind;
3191 break;
3192 case tcc_reference: /* a reference */
3193 kind = r_kind;
3194 break;
3195 default:
3196 kind = e_kind;
3197 break;
3200 tree_node_counts[(int) kind]++;
3201 tree_node_sizes[(int) kind] += length;
3202 #endif
3204 gcc_assert (TREE_CODE_LENGTH (code) == 1);
3206 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
3208 memset (t, 0, sizeof (struct tree_common));
3210 TREE_SET_CODE (t, code);
3212 TREE_TYPE (t) = type;
3213 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
3214 TREE_OPERAND (t, 0) = node;
3215 TREE_BLOCK (t) = NULL_TREE;
3216 if (node && !TYPE_P (node))
3218 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
3219 TREE_READONLY (t) = TREE_READONLY (node);
3222 if (TREE_CODE_CLASS (code) == tcc_statement)
3223 TREE_SIDE_EFFECTS (t) = 1;
3224 else switch (code)
3226 case VA_ARG_EXPR:
3227 /* All of these have side-effects, no matter what their
3228 operands are. */
3229 TREE_SIDE_EFFECTS (t) = 1;
3230 TREE_READONLY (t) = 0;
3231 break;
3233 case MISALIGNED_INDIRECT_REF:
3234 case ALIGN_INDIRECT_REF:
3235 case INDIRECT_REF:
3236 /* Whether a dereference is readonly has nothing to do with whether
3237 its operand is readonly. */
3238 TREE_READONLY (t) = 0;
3239 break;
3241 case ADDR_EXPR:
3242 if (node)
3243 recompute_tree_invariant_for_addr_expr (t);
3244 break;
3246 default:
3247 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
3248 && node && !TYPE_P (node)
3249 && TREE_CONSTANT (node))
3250 TREE_CONSTANT (t) = 1;
3251 if (TREE_CODE_CLASS (code) == tcc_reference
3252 && node && TREE_THIS_VOLATILE (node))
3253 TREE_THIS_VOLATILE (t) = 1;
3254 break;
3257 return t;
3260 #define PROCESS_ARG(N) \
3261 do { \
3262 TREE_OPERAND (t, N) = arg##N; \
3263 if (arg##N &&!TYPE_P (arg##N)) \
3265 if (TREE_SIDE_EFFECTS (arg##N)) \
3266 side_effects = 1; \
3267 if (!TREE_READONLY (arg##N)) \
3268 read_only = 0; \
3269 if (!TREE_CONSTANT (arg##N)) \
3270 constant = 0; \
3272 } while (0)
3274 tree
3275 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
3277 bool constant, read_only, side_effects;
3278 tree t;
3280 gcc_assert (TREE_CODE_LENGTH (code) == 2);
3282 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
3283 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
3284 /* When sizetype precision doesn't match that of pointers
3285 we need to be able to build explicit extensions or truncations
3286 of the offset argument. */
3287 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
3288 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
3289 && TREE_CODE (arg1) == INTEGER_CST);
3291 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
3292 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
3293 && INTEGRAL_TYPE_P (TREE_TYPE (arg1))
3294 && useless_type_conversion_p (sizetype, TREE_TYPE (arg1)));
3296 t = make_node_stat (code PASS_MEM_STAT);
3297 TREE_TYPE (t) = tt;
3299 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
3300 result based on those same flags for the arguments. But if the
3301 arguments aren't really even `tree' expressions, we shouldn't be trying
3302 to do this. */
3304 /* Expressions without side effects may be constant if their
3305 arguments are as well. */
3306 constant = (TREE_CODE_CLASS (code) == tcc_comparison
3307 || TREE_CODE_CLASS (code) == tcc_binary);
3308 read_only = 1;
3309 side_effects = TREE_SIDE_EFFECTS (t);
3311 PROCESS_ARG(0);
3312 PROCESS_ARG(1);
3314 TREE_READONLY (t) = read_only;
3315 TREE_CONSTANT (t) = constant;
3316 TREE_SIDE_EFFECTS (t) = side_effects;
3317 TREE_THIS_VOLATILE (t)
3318 = (TREE_CODE_CLASS (code) == tcc_reference
3319 && arg0 && TREE_THIS_VOLATILE (arg0));
3321 return t;
3325 tree
3326 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3327 tree arg2 MEM_STAT_DECL)
3329 bool constant, read_only, side_effects;
3330 tree t;
3332 gcc_assert (TREE_CODE_LENGTH (code) == 3);
3333 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3335 t = make_node_stat (code PASS_MEM_STAT);
3336 TREE_TYPE (t) = tt;
3338 /* As a special exception, if COND_EXPR has NULL branches, we
3339 assume that it is a gimple statement and always consider
3340 it to have side effects. */
3341 if (code == COND_EXPR
3342 && tt == void_type_node
3343 && arg1 == NULL_TREE
3344 && arg2 == NULL_TREE)
3345 side_effects = true;
3346 else
3347 side_effects = TREE_SIDE_EFFECTS (t);
3349 PROCESS_ARG(0);
3350 PROCESS_ARG(1);
3351 PROCESS_ARG(2);
3353 TREE_SIDE_EFFECTS (t) = side_effects;
3354 TREE_THIS_VOLATILE (t)
3355 = (TREE_CODE_CLASS (code) == tcc_reference
3356 && arg0 && TREE_THIS_VOLATILE (arg0));
3358 return t;
3361 tree
3362 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3363 tree arg2, tree arg3 MEM_STAT_DECL)
3365 bool constant, read_only, side_effects;
3366 tree t;
3368 gcc_assert (TREE_CODE_LENGTH (code) == 4);
3370 t = make_node_stat (code PASS_MEM_STAT);
3371 TREE_TYPE (t) = tt;
3373 side_effects = TREE_SIDE_EFFECTS (t);
3375 PROCESS_ARG(0);
3376 PROCESS_ARG(1);
3377 PROCESS_ARG(2);
3378 PROCESS_ARG(3);
3380 TREE_SIDE_EFFECTS (t) = side_effects;
3381 TREE_THIS_VOLATILE (t)
3382 = (TREE_CODE_CLASS (code) == tcc_reference
3383 && arg0 && TREE_THIS_VOLATILE (arg0));
3385 return t;
3388 tree
3389 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3390 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
3392 bool constant, read_only, side_effects;
3393 tree t;
3395 gcc_assert (TREE_CODE_LENGTH (code) == 5);
3397 t = make_node_stat (code PASS_MEM_STAT);
3398 TREE_TYPE (t) = tt;
3400 side_effects = TREE_SIDE_EFFECTS (t);
3402 PROCESS_ARG(0);
3403 PROCESS_ARG(1);
3404 PROCESS_ARG(2);
3405 PROCESS_ARG(3);
3406 PROCESS_ARG(4);
3408 TREE_SIDE_EFFECTS (t) = side_effects;
3409 TREE_THIS_VOLATILE (t)
3410 = (TREE_CODE_CLASS (code) == tcc_reference
3411 && arg0 && TREE_THIS_VOLATILE (arg0));
3413 return t;
3416 tree
3417 build6_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3418 tree arg2, tree arg3, tree arg4, tree arg5 MEM_STAT_DECL)
3420 bool constant, read_only, side_effects;
3421 tree t;
3423 gcc_assert (code == TARGET_MEM_REF);
3425 t = make_node_stat (code PASS_MEM_STAT);
3426 TREE_TYPE (t) = tt;
3428 side_effects = TREE_SIDE_EFFECTS (t);
3430 PROCESS_ARG(0);
3431 PROCESS_ARG(1);
3432 PROCESS_ARG(2);
3433 PROCESS_ARG(3);
3434 PROCESS_ARG(4);
3435 PROCESS_ARG(5);
3437 TREE_SIDE_EFFECTS (t) = side_effects;
3438 TREE_THIS_VOLATILE (t) = 0;
3440 return t;
3443 /* Similar except don't specify the TREE_TYPE
3444 and leave the TREE_SIDE_EFFECTS as 0.
3445 It is permissible for arguments to be null,
3446 or even garbage if their values do not matter. */
3448 tree
3449 build_nt (enum tree_code code, ...)
3451 tree t;
3452 int length;
3453 int i;
3454 va_list p;
3456 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3458 va_start (p, code);
3460 t = make_node (code);
3461 length = TREE_CODE_LENGTH (code);
3463 for (i = 0; i < length; i++)
3464 TREE_OPERAND (t, i) = va_arg (p, tree);
3466 va_end (p);
3467 return t;
3470 /* Similar to build_nt, but for creating a CALL_EXPR object with
3471 ARGLIST passed as a list. */
3473 tree
3474 build_nt_call_list (tree fn, tree arglist)
3476 tree t;
3477 int i;
3479 t = build_vl_exp (CALL_EXPR, list_length (arglist) + 3);
3480 CALL_EXPR_FN (t) = fn;
3481 CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
3482 for (i = 0; arglist; arglist = TREE_CHAIN (arglist), i++)
3483 CALL_EXPR_ARG (t, i) = TREE_VALUE (arglist);
3484 return t;
3487 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
3488 We do NOT enter this node in any sort of symbol table.
3490 layout_decl is used to set up the decl's storage layout.
3491 Other slots are initialized to 0 or null pointers. */
3493 tree
3494 build_decl_stat (enum tree_code code, tree name, tree type MEM_STAT_DECL)
3496 tree t;
3498 t = make_node_stat (code PASS_MEM_STAT);
3500 /* if (type == error_mark_node)
3501 type = integer_type_node; */
3502 /* That is not done, deliberately, so that having error_mark_node
3503 as the type can suppress useless errors in the use of this variable. */
3505 DECL_NAME (t) = name;
3506 TREE_TYPE (t) = type;
3508 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
3509 layout_decl (t, 0);
3511 return t;
3514 /* Builds and returns function declaration with NAME and TYPE. */
3516 tree
3517 build_fn_decl (const char *name, tree type)
3519 tree id = get_identifier (name);
3520 tree decl = build_decl (FUNCTION_DECL, id, type);
3522 DECL_EXTERNAL (decl) = 1;
3523 TREE_PUBLIC (decl) = 1;
3524 DECL_ARTIFICIAL (decl) = 1;
3525 TREE_NOTHROW (decl) = 1;
3527 return decl;
3531 /* BLOCK nodes are used to represent the structure of binding contours
3532 and declarations, once those contours have been exited and their contents
3533 compiled. This information is used for outputting debugging info. */
3535 tree
3536 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
3538 tree block = make_node (BLOCK);
3540 BLOCK_VARS (block) = vars;
3541 BLOCK_SUBBLOCKS (block) = subblocks;
3542 BLOCK_SUPERCONTEXT (block) = supercontext;
3543 BLOCK_CHAIN (block) = chain;
3544 return block;
3547 expanded_location
3548 expand_location (source_location loc)
3550 expanded_location xloc;
3551 if (loc == 0)
3553 xloc.file = NULL;
3554 xloc.line = 0;
3555 xloc.column = 0;
3556 xloc.sysp = 0;
3558 else
3560 const struct line_map *map = linemap_lookup (line_table, loc);
3561 xloc.file = map->to_file;
3562 xloc.line = SOURCE_LINE (map, loc);
3563 xloc.column = SOURCE_COLUMN (map, loc);
3564 xloc.sysp = map->sysp != 0;
3566 return xloc;
3570 /* Source location accessor functions. */
3573 void
3574 set_expr_locus (tree node, source_location *loc)
3576 if (loc == NULL)
3577 EXPR_CHECK (node)->exp.locus = UNKNOWN_LOCATION;
3578 else
3579 EXPR_CHECK (node)->exp.locus = *loc;
3582 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
3584 LOC is the location to use in tree T. */
3586 void
3587 protected_set_expr_location (tree t, location_t loc)
3589 if (t && CAN_HAVE_LOCATION_P (t))
3590 SET_EXPR_LOCATION (t, loc);
3593 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
3594 is ATTRIBUTE. */
3596 tree
3597 build_decl_attribute_variant (tree ddecl, tree attribute)
3599 DECL_ATTRIBUTES (ddecl) = attribute;
3600 return ddecl;
3603 /* Borrowed from hashtab.c iterative_hash implementation. */
3604 #define mix(a,b,c) \
3606 a -= b; a -= c; a ^= (c>>13); \
3607 b -= c; b -= a; b ^= (a<< 8); \
3608 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
3609 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
3610 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
3611 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
3612 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
3613 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
3614 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
3618 /* Produce good hash value combining VAL and VAL2. */
3619 hashval_t
3620 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
3622 /* the golden ratio; an arbitrary value. */
3623 hashval_t a = 0x9e3779b9;
3625 mix (a, val, val2);
3626 return val2;
3629 /* Produce good hash value combining PTR and VAL2. */
3630 static inline hashval_t
3631 iterative_hash_pointer (const void *ptr, hashval_t val2)
3633 if (sizeof (ptr) == sizeof (hashval_t))
3634 return iterative_hash_hashval_t ((size_t) ptr, val2);
3635 else
3637 hashval_t a = (hashval_t) (size_t) ptr;
3638 /* Avoid warnings about shifting of more than the width of the type on
3639 hosts that won't execute this path. */
3640 int zero = 0;
3641 hashval_t b = (hashval_t) ((size_t) ptr >> (sizeof (hashval_t) * 8 + zero));
3642 mix (a, b, val2);
3643 return val2;
3647 /* Produce good hash value combining VAL and VAL2. */
3648 static inline hashval_t
3649 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
3651 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
3652 return iterative_hash_hashval_t (val, val2);
3653 else
3655 hashval_t a = (hashval_t) val;
3656 /* Avoid warnings about shifting of more than the width of the type on
3657 hosts that won't execute this path. */
3658 int zero = 0;
3659 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
3660 mix (a, b, val2);
3661 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
3663 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
3664 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
3665 mix (a, b, val2);
3667 return val2;
3671 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
3672 is ATTRIBUTE and its qualifiers are QUALS.
3674 Record such modified types already made so we don't make duplicates. */
3676 static tree
3677 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
3679 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
3681 hashval_t hashcode = 0;
3682 tree ntype;
3683 enum tree_code code = TREE_CODE (ttype);
3685 /* Building a distinct copy of a tagged type is inappropriate; it
3686 causes breakage in code that expects there to be a one-to-one
3687 relationship between a struct and its fields.
3688 build_duplicate_type is another solution (as used in
3689 handle_transparent_union_attribute), but that doesn't play well
3690 with the stronger C++ type identity model. */
3691 if (TREE_CODE (ttype) == RECORD_TYPE
3692 || TREE_CODE (ttype) == UNION_TYPE
3693 || TREE_CODE (ttype) == QUAL_UNION_TYPE
3694 || TREE_CODE (ttype) == ENUMERAL_TYPE)
3696 warning (OPT_Wattributes,
3697 "ignoring attributes applied to %qT after definition",
3698 TYPE_MAIN_VARIANT (ttype));
3699 return build_qualified_type (ttype, quals);
3702 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
3703 ntype = build_distinct_type_copy (ttype);
3705 TYPE_ATTRIBUTES (ntype) = attribute;
3707 hashcode = iterative_hash_object (code, hashcode);
3708 if (TREE_TYPE (ntype))
3709 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
3710 hashcode);
3711 hashcode = attribute_hash_list (attribute, hashcode);
3713 switch (TREE_CODE (ntype))
3715 case FUNCTION_TYPE:
3716 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
3717 break;
3718 case ARRAY_TYPE:
3719 if (TYPE_DOMAIN (ntype))
3720 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
3721 hashcode);
3722 break;
3723 case INTEGER_TYPE:
3724 hashcode = iterative_hash_object
3725 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
3726 hashcode = iterative_hash_object
3727 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
3728 break;
3729 case REAL_TYPE:
3730 case FIXED_POINT_TYPE:
3732 unsigned int precision = TYPE_PRECISION (ntype);
3733 hashcode = iterative_hash_object (precision, hashcode);
3735 break;
3736 default:
3737 break;
3740 ntype = type_hash_canon (hashcode, ntype);
3742 /* If the target-dependent attributes make NTYPE different from
3743 its canonical type, we will need to use structural equality
3744 checks for this type. */
3745 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
3746 || !targetm.comp_type_attributes (ntype, ttype))
3747 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
3748 else if (TYPE_CANONICAL (ntype) == ntype)
3749 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
3751 ttype = build_qualified_type (ntype, quals);
3753 else if (TYPE_QUALS (ttype) != quals)
3754 ttype = build_qualified_type (ttype, quals);
3756 return ttype;
3760 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
3761 is ATTRIBUTE.
3763 Record such modified types already made so we don't make duplicates. */
3765 tree
3766 build_type_attribute_variant (tree ttype, tree attribute)
3768 return build_type_attribute_qual_variant (ttype, attribute,
3769 TYPE_QUALS (ttype));
3772 /* Return nonzero if IDENT is a valid name for attribute ATTR,
3773 or zero if not.
3775 We try both `text' and `__text__', ATTR may be either one. */
3776 /* ??? It might be a reasonable simplification to require ATTR to be only
3777 `text'. One might then also require attribute lists to be stored in
3778 their canonicalized form. */
3780 static int
3781 is_attribute_with_length_p (const char *attr, int attr_len, const_tree ident)
3783 int ident_len;
3784 const char *p;
3786 if (TREE_CODE (ident) != IDENTIFIER_NODE)
3787 return 0;
3789 p = IDENTIFIER_POINTER (ident);
3790 ident_len = IDENTIFIER_LENGTH (ident);
3792 if (ident_len == attr_len
3793 && strcmp (attr, p) == 0)
3794 return 1;
3796 /* If ATTR is `__text__', IDENT must be `text'; and vice versa. */
3797 if (attr[0] == '_')
3799 gcc_assert (attr[1] == '_');
3800 gcc_assert (attr[attr_len - 2] == '_');
3801 gcc_assert (attr[attr_len - 1] == '_');
3802 if (ident_len == attr_len - 4
3803 && strncmp (attr + 2, p, attr_len - 4) == 0)
3804 return 1;
3806 else
3808 if (ident_len == attr_len + 4
3809 && p[0] == '_' && p[1] == '_'
3810 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
3811 && strncmp (attr, p + 2, attr_len) == 0)
3812 return 1;
3815 return 0;
3818 /* Return nonzero if IDENT is a valid name for attribute ATTR,
3819 or zero if not.
3821 We try both `text' and `__text__', ATTR may be either one. */
3824 is_attribute_p (const char *attr, const_tree ident)
3826 return is_attribute_with_length_p (attr, strlen (attr), ident);
3829 /* Given an attribute name and a list of attributes, return a pointer to the
3830 attribute's list element if the attribute is part of the list, or NULL_TREE
3831 if not found. If the attribute appears more than once, this only
3832 returns the first occurrence; the TREE_CHAIN of the return value should
3833 be passed back in if further occurrences are wanted. */
3835 tree
3836 lookup_attribute (const char *attr_name, tree list)
3838 tree l;
3839 size_t attr_len = strlen (attr_name);
3841 for (l = list; l; l = TREE_CHAIN (l))
3843 gcc_assert (TREE_CODE (TREE_PURPOSE (l)) == IDENTIFIER_NODE);
3844 if (is_attribute_with_length_p (attr_name, attr_len, TREE_PURPOSE (l)))
3845 return l;
3847 return NULL_TREE;
3850 /* Remove any instances of attribute ATTR_NAME in LIST and return the
3851 modified list. */
3853 tree
3854 remove_attribute (const char *attr_name, tree list)
3856 tree *p;
3857 size_t attr_len = strlen (attr_name);
3859 for (p = &list; *p; )
3861 tree l = *p;
3862 gcc_assert (TREE_CODE (TREE_PURPOSE (l)) == IDENTIFIER_NODE);
3863 if (is_attribute_with_length_p (attr_name, attr_len, TREE_PURPOSE (l)))
3864 *p = TREE_CHAIN (l);
3865 else
3866 p = &TREE_CHAIN (l);
3869 return list;
3872 /* Return an attribute list that is the union of a1 and a2. */
3874 tree
3875 merge_attributes (tree a1, tree a2)
3877 tree attributes;
3879 /* Either one unset? Take the set one. */
3881 if ((attributes = a1) == 0)
3882 attributes = a2;
3884 /* One that completely contains the other? Take it. */
3886 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
3888 if (attribute_list_contained (a2, a1))
3889 attributes = a2;
3890 else
3892 /* Pick the longest list, and hang on the other list. */
3894 if (list_length (a1) < list_length (a2))
3895 attributes = a2, a2 = a1;
3897 for (; a2 != 0; a2 = TREE_CHAIN (a2))
3899 tree a;
3900 for (a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
3901 attributes);
3902 a != NULL_TREE;
3903 a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
3904 TREE_CHAIN (a)))
3906 if (TREE_VALUE (a) != NULL
3907 && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
3908 && TREE_VALUE (a2) != NULL
3909 && TREE_CODE (TREE_VALUE (a2)) == TREE_LIST)
3911 if (simple_cst_list_equal (TREE_VALUE (a),
3912 TREE_VALUE (a2)) == 1)
3913 break;
3915 else if (simple_cst_equal (TREE_VALUE (a),
3916 TREE_VALUE (a2)) == 1)
3917 break;
3919 if (a == NULL_TREE)
3921 a1 = copy_node (a2);
3922 TREE_CHAIN (a1) = attributes;
3923 attributes = a1;
3928 return attributes;
3931 /* Given types T1 and T2, merge their attributes and return
3932 the result. */
3934 tree
3935 merge_type_attributes (tree t1, tree t2)
3937 return merge_attributes (TYPE_ATTRIBUTES (t1),
3938 TYPE_ATTRIBUTES (t2));
3941 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
3942 the result. */
3944 tree
3945 merge_decl_attributes (tree olddecl, tree newdecl)
3947 return merge_attributes (DECL_ATTRIBUTES (olddecl),
3948 DECL_ATTRIBUTES (newdecl));
3951 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
3953 /* Specialization of merge_decl_attributes for various Windows targets.
3955 This handles the following situation:
3957 __declspec (dllimport) int foo;
3958 int foo;
3960 The second instance of `foo' nullifies the dllimport. */
3962 tree
3963 merge_dllimport_decl_attributes (tree old, tree new_tree)
3965 tree a;
3966 int delete_dllimport_p = 1;
3968 /* What we need to do here is remove from `old' dllimport if it doesn't
3969 appear in `new'. dllimport behaves like extern: if a declaration is
3970 marked dllimport and a definition appears later, then the object
3971 is not dllimport'd. We also remove a `new' dllimport if the old list
3972 contains dllexport: dllexport always overrides dllimport, regardless
3973 of the order of declaration. */
3974 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
3975 delete_dllimport_p = 0;
3976 else if (DECL_DLLIMPORT_P (new_tree)
3977 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
3979 DECL_DLLIMPORT_P (new_tree) = 0;
3980 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
3981 "dllimport ignored", new_tree);
3983 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
3985 /* Warn about overriding a symbol that has already been used, e.g.:
3986 extern int __attribute__ ((dllimport)) foo;
3987 int* bar () {return &foo;}
3988 int foo;
3990 if (TREE_USED (old))
3992 warning (0, "%q+D redeclared without dllimport attribute "
3993 "after being referenced with dll linkage", new_tree);
3994 /* If we have used a variable's address with dllimport linkage,
3995 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
3996 decl may already have had TREE_CONSTANT computed.
3997 We still remove the attribute so that assembler code refers
3998 to '&foo rather than '_imp__foo'. */
3999 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
4000 DECL_DLLIMPORT_P (new_tree) = 1;
4003 /* Let an inline definition silently override the external reference,
4004 but otherwise warn about attribute inconsistency. */
4005 else if (TREE_CODE (new_tree) == VAR_DECL
4006 || !DECL_DECLARED_INLINE_P (new_tree))
4007 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
4008 "previous dllimport ignored", new_tree);
4010 else
4011 delete_dllimport_p = 0;
4013 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
4015 if (delete_dllimport_p)
4017 tree prev, t;
4018 const size_t attr_len = strlen ("dllimport");
4020 /* Scan the list for dllimport and delete it. */
4021 for (prev = NULL_TREE, t = a; t; prev = t, t = TREE_CHAIN (t))
4023 if (is_attribute_with_length_p ("dllimport", attr_len,
4024 TREE_PURPOSE (t)))
4026 if (prev == NULL_TREE)
4027 a = TREE_CHAIN (a);
4028 else
4029 TREE_CHAIN (prev) = TREE_CHAIN (t);
4030 break;
4035 return a;
4038 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
4039 struct attribute_spec.handler. */
4041 tree
4042 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
4043 bool *no_add_attrs)
4045 tree node = *pnode;
4047 /* These attributes may apply to structure and union types being created,
4048 but otherwise should pass to the declaration involved. */
4049 if (!DECL_P (node))
4051 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
4052 | (int) ATTR_FLAG_ARRAY_NEXT))
4054 *no_add_attrs = true;
4055 return tree_cons (name, args, NULL_TREE);
4057 if (TREE_CODE (node) == RECORD_TYPE
4058 || TREE_CODE (node) == UNION_TYPE)
4060 node = TYPE_NAME (node);
4061 if (!node)
4062 return NULL_TREE;
4064 else
4066 warning (OPT_Wattributes, "%qE attribute ignored",
4067 name);
4068 *no_add_attrs = true;
4069 return NULL_TREE;
4073 if (TREE_CODE (node) != FUNCTION_DECL
4074 && TREE_CODE (node) != VAR_DECL
4075 && TREE_CODE (node) != TYPE_DECL)
4077 *no_add_attrs = true;
4078 warning (OPT_Wattributes, "%qE attribute ignored",
4079 name);
4080 return NULL_TREE;
4083 if (TREE_CODE (node) == TYPE_DECL
4084 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
4085 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
4087 *no_add_attrs = true;
4088 warning (OPT_Wattributes, "%qE attribute ignored",
4089 name);
4090 return NULL_TREE;
4093 /* Report error on dllimport ambiguities seen now before they cause
4094 any damage. */
4095 else if (is_attribute_p ("dllimport", name))
4097 /* Honor any target-specific overrides. */
4098 if (!targetm.valid_dllimport_attribute_p (node))
4099 *no_add_attrs = true;
4101 else if (TREE_CODE (node) == FUNCTION_DECL
4102 && DECL_DECLARED_INLINE_P (node))
4104 warning (OPT_Wattributes, "inline function %q+D declared as "
4105 " dllimport: attribute ignored", node);
4106 *no_add_attrs = true;
4108 /* Like MS, treat definition of dllimported variables and
4109 non-inlined functions on declaration as syntax errors. */
4110 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
4112 error ("function %q+D definition is marked dllimport", node);
4113 *no_add_attrs = true;
4116 else if (TREE_CODE (node) == VAR_DECL)
4118 if (DECL_INITIAL (node))
4120 error ("variable %q+D definition is marked dllimport",
4121 node);
4122 *no_add_attrs = true;
4125 /* `extern' needn't be specified with dllimport.
4126 Specify `extern' now and hope for the best. Sigh. */
4127 DECL_EXTERNAL (node) = 1;
4128 /* Also, implicitly give dllimport'd variables declared within
4129 a function global scope, unless declared static. */
4130 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
4131 TREE_PUBLIC (node) = 1;
4134 if (*no_add_attrs == false)
4135 DECL_DLLIMPORT_P (node) = 1;
4138 /* Report error if symbol is not accessible at global scope. */
4139 if (!TREE_PUBLIC (node)
4140 && (TREE_CODE (node) == VAR_DECL
4141 || TREE_CODE (node) == FUNCTION_DECL))
4143 error ("external linkage required for symbol %q+D because of "
4144 "%qE attribute", node, name);
4145 *no_add_attrs = true;
4148 /* A dllexport'd entity must have default visibility so that other
4149 program units (shared libraries or the main executable) can see
4150 it. A dllimport'd entity must have default visibility so that
4151 the linker knows that undefined references within this program
4152 unit can be resolved by the dynamic linker. */
4153 if (!*no_add_attrs)
4155 if (DECL_VISIBILITY_SPECIFIED (node)
4156 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
4157 error ("%qE implies default visibility, but %qD has already "
4158 "been declared with a different visibility",
4159 name, node);
4160 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
4161 DECL_VISIBILITY_SPECIFIED (node) = 1;
4164 return NULL_TREE;
4167 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
4169 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
4170 of the various TYPE_QUAL values. */
4172 static void
4173 set_type_quals (tree type, int type_quals)
4175 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
4176 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
4177 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
4180 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
4182 bool
4183 check_qualified_type (const_tree cand, const_tree base, int type_quals)
4185 return (TYPE_QUALS (cand) == type_quals
4186 && TYPE_NAME (cand) == TYPE_NAME (base)
4187 /* Apparently this is needed for Objective-C. */
4188 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
4189 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
4190 TYPE_ATTRIBUTES (base)));
4193 /* Return a version of the TYPE, qualified as indicated by the
4194 TYPE_QUALS, if one exists. If no qualified version exists yet,
4195 return NULL_TREE. */
4197 tree
4198 get_qualified_type (tree type, int type_quals)
4200 tree t;
4202 if (TYPE_QUALS (type) == type_quals)
4203 return type;
4205 /* Search the chain of variants to see if there is already one there just
4206 like the one we need to have. If so, use that existing one. We must
4207 preserve the TYPE_NAME, since there is code that depends on this. */
4208 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
4209 if (check_qualified_type (t, type, type_quals))
4210 return t;
4212 return NULL_TREE;
4215 /* Like get_qualified_type, but creates the type if it does not
4216 exist. This function never returns NULL_TREE. */
4218 tree
4219 build_qualified_type (tree type, int type_quals)
4221 tree t;
4223 /* See if we already have the appropriate qualified variant. */
4224 t = get_qualified_type (type, type_quals);
4226 /* If not, build it. */
4227 if (!t)
4229 t = build_variant_type_copy (type);
4230 set_type_quals (t, type_quals);
4232 if (TYPE_STRUCTURAL_EQUALITY_P (type))
4233 /* Propagate structural equality. */
4234 SET_TYPE_STRUCTURAL_EQUALITY (t);
4235 else if (TYPE_CANONICAL (type) != type)
4236 /* Build the underlying canonical type, since it is different
4237 from TYPE. */
4238 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
4239 type_quals);
4240 else
4241 /* T is its own canonical type. */
4242 TYPE_CANONICAL (t) = t;
4246 return t;
4249 /* Create a new distinct copy of TYPE. The new type is made its own
4250 MAIN_VARIANT. If TYPE requires structural equality checks, the
4251 resulting type requires structural equality checks; otherwise, its
4252 TYPE_CANONICAL points to itself. */
4254 tree
4255 build_distinct_type_copy (tree type)
4257 tree t = copy_node (type);
4259 TYPE_POINTER_TO (t) = 0;
4260 TYPE_REFERENCE_TO (t) = 0;
4262 /* Set the canonical type either to a new equivalence class, or
4263 propagate the need for structural equality checks. */
4264 if (TYPE_STRUCTURAL_EQUALITY_P (type))
4265 SET_TYPE_STRUCTURAL_EQUALITY (t);
4266 else
4267 TYPE_CANONICAL (t) = t;
4269 /* Make it its own variant. */
4270 TYPE_MAIN_VARIANT (t) = t;
4271 TYPE_NEXT_VARIANT (t) = 0;
4273 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
4274 whose TREE_TYPE is not t. This can also happen in the Ada
4275 frontend when using subtypes. */
4277 return t;
4280 /* Create a new variant of TYPE, equivalent but distinct. This is so
4281 the caller can modify it. TYPE_CANONICAL for the return type will
4282 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
4283 are considered equal by the language itself (or that both types
4284 require structural equality checks). */
4286 tree
4287 build_variant_type_copy (tree type)
4289 tree t, m = TYPE_MAIN_VARIANT (type);
4291 t = build_distinct_type_copy (type);
4293 /* Since we're building a variant, assume that it is a non-semantic
4294 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
4295 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
4297 /* Add the new type to the chain of variants of TYPE. */
4298 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
4299 TYPE_NEXT_VARIANT (m) = t;
4300 TYPE_MAIN_VARIANT (t) = m;
4302 return t;
4305 /* Return true if the from tree in both tree maps are equal. */
4308 tree_map_base_eq (const void *va, const void *vb)
4310 const struct tree_map_base *const a = (const struct tree_map_base *) va,
4311 *const b = (const struct tree_map_base *) vb;
4312 return (a->from == b->from);
4315 /* Hash a from tree in a tree_map. */
4317 unsigned int
4318 tree_map_base_hash (const void *item)
4320 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
4323 /* Return true if this tree map structure is marked for garbage collection
4324 purposes. We simply return true if the from tree is marked, so that this
4325 structure goes away when the from tree goes away. */
4328 tree_map_base_marked_p (const void *p)
4330 return ggc_marked_p (((const struct tree_map_base *) p)->from);
4333 unsigned int
4334 tree_map_hash (const void *item)
4336 return (((const struct tree_map *) item)->hash);
4339 /* Return the initialization priority for DECL. */
4341 priority_type
4342 decl_init_priority_lookup (tree decl)
4344 struct tree_priority_map *h;
4345 struct tree_map_base in;
4347 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
4348 in.from = decl;
4349 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
4350 return h ? h->init : DEFAULT_INIT_PRIORITY;
4353 /* Return the finalization priority for DECL. */
4355 priority_type
4356 decl_fini_priority_lookup (tree decl)
4358 struct tree_priority_map *h;
4359 struct tree_map_base in;
4361 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4362 in.from = decl;
4363 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
4364 return h ? h->fini : DEFAULT_INIT_PRIORITY;
4367 /* Return the initialization and finalization priority information for
4368 DECL. If there is no previous priority information, a freshly
4369 allocated structure is returned. */
4371 static struct tree_priority_map *
4372 decl_priority_info (tree decl)
4374 struct tree_priority_map in;
4375 struct tree_priority_map *h;
4376 void **loc;
4378 in.base.from = decl;
4379 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
4380 h = (struct tree_priority_map *) *loc;
4381 if (!h)
4383 h = GGC_CNEW (struct tree_priority_map);
4384 *loc = h;
4385 h->base.from = decl;
4386 h->init = DEFAULT_INIT_PRIORITY;
4387 h->fini = DEFAULT_INIT_PRIORITY;
4390 return h;
4393 /* Set the initialization priority for DECL to PRIORITY. */
4395 void
4396 decl_init_priority_insert (tree decl, priority_type priority)
4398 struct tree_priority_map *h;
4400 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
4401 h = decl_priority_info (decl);
4402 h->init = priority;
4405 /* Set the finalization priority for DECL to PRIORITY. */
4407 void
4408 decl_fini_priority_insert (tree decl, priority_type priority)
4410 struct tree_priority_map *h;
4412 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4413 h = decl_priority_info (decl);
4414 h->fini = priority;
4417 /* Look up a restrict qualified base decl for FROM. */
4419 tree
4420 decl_restrict_base_lookup (tree from)
4422 struct tree_map *h;
4423 struct tree_map in;
4425 in.base.from = from;
4426 h = (struct tree_map *) htab_find_with_hash (restrict_base_for_decl, &in,
4427 htab_hash_pointer (from));
4428 return h ? h->to : NULL_TREE;
4431 /* Record the restrict qualified base TO for FROM. */
4433 void
4434 decl_restrict_base_insert (tree from, tree to)
4436 struct tree_map *h;
4437 void **loc;
4439 h = GGC_NEW (struct tree_map);
4440 h->hash = htab_hash_pointer (from);
4441 h->base.from = from;
4442 h->to = to;
4443 loc = htab_find_slot_with_hash (restrict_base_for_decl, h, h->hash, INSERT);
4444 *(struct tree_map **) loc = h;
4447 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
4449 static void
4450 print_debug_expr_statistics (void)
4452 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
4453 (long) htab_size (debug_expr_for_decl),
4454 (long) htab_elements (debug_expr_for_decl),
4455 htab_collisions (debug_expr_for_decl));
4458 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
4460 static void
4461 print_value_expr_statistics (void)
4463 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
4464 (long) htab_size (value_expr_for_decl),
4465 (long) htab_elements (value_expr_for_decl),
4466 htab_collisions (value_expr_for_decl));
4469 /* Print out statistics for the RESTRICT_BASE_FOR_DECL hash table, but
4470 don't print anything if the table is empty. */
4472 static void
4473 print_restrict_base_statistics (void)
4475 if (htab_elements (restrict_base_for_decl) != 0)
4476 fprintf (stderr,
4477 "RESTRICT_BASE hash: size %ld, %ld elements, %f collisions\n",
4478 (long) htab_size (restrict_base_for_decl),
4479 (long) htab_elements (restrict_base_for_decl),
4480 htab_collisions (restrict_base_for_decl));
4483 /* Lookup a debug expression for FROM, and return it if we find one. */
4485 tree
4486 decl_debug_expr_lookup (tree from)
4488 struct tree_map *h, in;
4489 in.base.from = from;
4491 h = (struct tree_map *) htab_find_with_hash (debug_expr_for_decl, &in,
4492 htab_hash_pointer (from));
4493 if (h)
4494 return h->to;
4495 return NULL_TREE;
4498 /* Insert a mapping FROM->TO in the debug expression hashtable. */
4500 void
4501 decl_debug_expr_insert (tree from, tree to)
4503 struct tree_map *h;
4504 void **loc;
4506 h = GGC_NEW (struct tree_map);
4507 h->hash = htab_hash_pointer (from);
4508 h->base.from = from;
4509 h->to = to;
4510 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, h->hash, INSERT);
4511 *(struct tree_map **) loc = h;
4514 /* Lookup a value expression for FROM, and return it if we find one. */
4516 tree
4517 decl_value_expr_lookup (tree from)
4519 struct tree_map *h, in;
4520 in.base.from = from;
4522 h = (struct tree_map *) htab_find_with_hash (value_expr_for_decl, &in,
4523 htab_hash_pointer (from));
4524 if (h)
4525 return h->to;
4526 return NULL_TREE;
4529 /* Insert a mapping FROM->TO in the value expression hashtable. */
4531 void
4532 decl_value_expr_insert (tree from, tree to)
4534 struct tree_map *h;
4535 void **loc;
4537 h = GGC_NEW (struct tree_map);
4538 h->hash = htab_hash_pointer (from);
4539 h->base.from = from;
4540 h->to = to;
4541 loc = htab_find_slot_with_hash (value_expr_for_decl, h, h->hash, INSERT);
4542 *(struct tree_map **) loc = h;
4545 /* Hashing of types so that we don't make duplicates.
4546 The entry point is `type_hash_canon'. */
4548 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
4549 with types in the TREE_VALUE slots), by adding the hash codes
4550 of the individual types. */
4552 static unsigned int
4553 type_hash_list (const_tree list, hashval_t hashcode)
4555 const_tree tail;
4557 for (tail = list; tail; tail = TREE_CHAIN (tail))
4558 if (TREE_VALUE (tail) != error_mark_node)
4559 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
4560 hashcode);
4562 return hashcode;
4565 /* These are the Hashtable callback functions. */
4567 /* Returns true iff the types are equivalent. */
4569 static int
4570 type_hash_eq (const void *va, const void *vb)
4572 const struct type_hash *const a = (const struct type_hash *) va,
4573 *const b = (const struct type_hash *) vb;
4575 /* First test the things that are the same for all types. */
4576 if (a->hash != b->hash
4577 || TREE_CODE (a->type) != TREE_CODE (b->type)
4578 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
4579 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
4580 TYPE_ATTRIBUTES (b->type))
4581 || TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
4582 || TYPE_MODE (a->type) != TYPE_MODE (b->type)
4583 || (TREE_CODE (a->type) != COMPLEX_TYPE
4584 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
4585 return 0;
4587 switch (TREE_CODE (a->type))
4589 case VOID_TYPE:
4590 case COMPLEX_TYPE:
4591 case POINTER_TYPE:
4592 case REFERENCE_TYPE:
4593 return 1;
4595 case VECTOR_TYPE:
4596 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
4598 case ENUMERAL_TYPE:
4599 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
4600 && !(TYPE_VALUES (a->type)
4601 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
4602 && TYPE_VALUES (b->type)
4603 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
4604 && type_list_equal (TYPE_VALUES (a->type),
4605 TYPE_VALUES (b->type))))
4606 return 0;
4608 /* ... fall through ... */
4610 case INTEGER_TYPE:
4611 case REAL_TYPE:
4612 case BOOLEAN_TYPE:
4613 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
4614 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
4615 TYPE_MAX_VALUE (b->type)))
4616 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
4617 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
4618 TYPE_MIN_VALUE (b->type))));
4620 case FIXED_POINT_TYPE:
4621 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
4623 case OFFSET_TYPE:
4624 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
4626 case METHOD_TYPE:
4627 return (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
4628 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
4629 || (TYPE_ARG_TYPES (a->type)
4630 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
4631 && TYPE_ARG_TYPES (b->type)
4632 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
4633 && type_list_equal (TYPE_ARG_TYPES (a->type),
4634 TYPE_ARG_TYPES (b->type)))));
4636 case ARRAY_TYPE:
4637 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
4639 case RECORD_TYPE:
4640 case UNION_TYPE:
4641 case QUAL_UNION_TYPE:
4642 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
4643 || (TYPE_FIELDS (a->type)
4644 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
4645 && TYPE_FIELDS (b->type)
4646 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
4647 && type_list_equal (TYPE_FIELDS (a->type),
4648 TYPE_FIELDS (b->type))));
4650 case FUNCTION_TYPE:
4651 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
4652 || (TYPE_ARG_TYPES (a->type)
4653 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
4654 && TYPE_ARG_TYPES (b->type)
4655 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
4656 && type_list_equal (TYPE_ARG_TYPES (a->type),
4657 TYPE_ARG_TYPES (b->type))))
4658 break;
4659 return 0;
4661 default:
4662 return 0;
4665 if (lang_hooks.types.type_hash_eq != NULL)
4666 return lang_hooks.types.type_hash_eq (a->type, b->type);
4668 return 1;
4671 /* Return the cached hash value. */
4673 static hashval_t
4674 type_hash_hash (const void *item)
4676 return ((const struct type_hash *) item)->hash;
4679 /* Look in the type hash table for a type isomorphic to TYPE.
4680 If one is found, return it. Otherwise return 0. */
4682 tree
4683 type_hash_lookup (hashval_t hashcode, tree type)
4685 struct type_hash *h, in;
4687 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
4688 must call that routine before comparing TYPE_ALIGNs. */
4689 layout_type (type);
4691 in.hash = hashcode;
4692 in.type = type;
4694 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
4695 hashcode);
4696 if (h)
4697 return h->type;
4698 return NULL_TREE;
4701 /* Add an entry to the type-hash-table
4702 for a type TYPE whose hash code is HASHCODE. */
4704 void
4705 type_hash_add (hashval_t hashcode, tree type)
4707 struct type_hash *h;
4708 void **loc;
4710 h = GGC_NEW (struct type_hash);
4711 h->hash = hashcode;
4712 h->type = type;
4713 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
4714 *loc = (void *)h;
4717 /* Given TYPE, and HASHCODE its hash code, return the canonical
4718 object for an identical type if one already exists.
4719 Otherwise, return TYPE, and record it as the canonical object.
4721 To use this function, first create a type of the sort you want.
4722 Then compute its hash code from the fields of the type that
4723 make it different from other similar types.
4724 Then call this function and use the value. */
4726 tree
4727 type_hash_canon (unsigned int hashcode, tree type)
4729 tree t1;
4731 /* The hash table only contains main variants, so ensure that's what we're
4732 being passed. */
4733 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
4735 if (!lang_hooks.types.hash_types)
4736 return type;
4738 /* See if the type is in the hash table already. If so, return it.
4739 Otherwise, add the type. */
4740 t1 = type_hash_lookup (hashcode, type);
4741 if (t1 != 0)
4743 #ifdef GATHER_STATISTICS
4744 tree_node_counts[(int) t_kind]--;
4745 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
4746 #endif
4747 return t1;
4749 else
4751 type_hash_add (hashcode, type);
4752 return type;
4756 /* See if the data pointed to by the type hash table is marked. We consider
4757 it marked if the type is marked or if a debug type number or symbol
4758 table entry has been made for the type. This reduces the amount of
4759 debugging output and eliminates that dependency of the debug output on
4760 the number of garbage collections. */
4762 static int
4763 type_hash_marked_p (const void *p)
4765 const_tree const type = ((const struct type_hash *) p)->type;
4767 return ggc_marked_p (type) || TYPE_SYMTAB_POINTER (type);
4770 static void
4771 print_type_hash_statistics (void)
4773 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
4774 (long) htab_size (type_hash_table),
4775 (long) htab_elements (type_hash_table),
4776 htab_collisions (type_hash_table));
4779 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
4780 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
4781 by adding the hash codes of the individual attributes. */
4783 static unsigned int
4784 attribute_hash_list (const_tree list, hashval_t hashcode)
4786 const_tree tail;
4788 for (tail = list; tail; tail = TREE_CHAIN (tail))
4789 /* ??? Do we want to add in TREE_VALUE too? */
4790 hashcode = iterative_hash_object
4791 (IDENTIFIER_HASH_VALUE (TREE_PURPOSE (tail)), hashcode);
4792 return hashcode;
4795 /* Given two lists of attributes, return true if list l2 is
4796 equivalent to l1. */
4799 attribute_list_equal (const_tree l1, const_tree l2)
4801 return attribute_list_contained (l1, l2)
4802 && attribute_list_contained (l2, l1);
4805 /* Given two lists of attributes, return true if list L2 is
4806 completely contained within L1. */
4807 /* ??? This would be faster if attribute names were stored in a canonicalized
4808 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
4809 must be used to show these elements are equivalent (which they are). */
4810 /* ??? It's not clear that attributes with arguments will always be handled
4811 correctly. */
4814 attribute_list_contained (const_tree l1, const_tree l2)
4816 const_tree t1, t2;
4818 /* First check the obvious, maybe the lists are identical. */
4819 if (l1 == l2)
4820 return 1;
4822 /* Maybe the lists are similar. */
4823 for (t1 = l1, t2 = l2;
4824 t1 != 0 && t2 != 0
4825 && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
4826 && TREE_VALUE (t1) == TREE_VALUE (t2);
4827 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
4829 /* Maybe the lists are equal. */
4830 if (t1 == 0 && t2 == 0)
4831 return 1;
4833 for (; t2 != 0; t2 = TREE_CHAIN (t2))
4835 const_tree attr;
4836 /* This CONST_CAST is okay because lookup_attribute does not
4837 modify its argument and the return value is assigned to a
4838 const_tree. */
4839 for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
4840 CONST_CAST_TREE(l1));
4841 attr != NULL_TREE;
4842 attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
4843 TREE_CHAIN (attr)))
4845 if (TREE_VALUE (t2) != NULL
4846 && TREE_CODE (TREE_VALUE (t2)) == TREE_LIST
4847 && TREE_VALUE (attr) != NULL
4848 && TREE_CODE (TREE_VALUE (attr)) == TREE_LIST)
4850 if (simple_cst_list_equal (TREE_VALUE (t2),
4851 TREE_VALUE (attr)) == 1)
4852 break;
4854 else if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) == 1)
4855 break;
4858 if (attr == 0)
4859 return 0;
4862 return 1;
4865 /* Given two lists of types
4866 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
4867 return 1 if the lists contain the same types in the same order.
4868 Also, the TREE_PURPOSEs must match. */
4871 type_list_equal (const_tree l1, const_tree l2)
4873 const_tree t1, t2;
4875 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
4876 if (TREE_VALUE (t1) != TREE_VALUE (t2)
4877 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
4878 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
4879 && (TREE_TYPE (TREE_PURPOSE (t1))
4880 == TREE_TYPE (TREE_PURPOSE (t2))))))
4881 return 0;
4883 return t1 == t2;
4886 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
4887 given by TYPE. If the argument list accepts variable arguments,
4888 then this function counts only the ordinary arguments. */
4891 type_num_arguments (const_tree type)
4893 int i = 0;
4894 tree t;
4896 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
4897 /* If the function does not take a variable number of arguments,
4898 the last element in the list will have type `void'. */
4899 if (VOID_TYPE_P (TREE_VALUE (t)))
4900 break;
4901 else
4902 ++i;
4904 return i;
4907 /* Nonzero if integer constants T1 and T2
4908 represent the same constant value. */
4911 tree_int_cst_equal (const_tree t1, const_tree t2)
4913 if (t1 == t2)
4914 return 1;
4916 if (t1 == 0 || t2 == 0)
4917 return 0;
4919 if (TREE_CODE (t1) == INTEGER_CST
4920 && TREE_CODE (t2) == INTEGER_CST
4921 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
4922 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
4923 return 1;
4925 return 0;
4928 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
4929 The precise way of comparison depends on their data type. */
4932 tree_int_cst_lt (const_tree t1, const_tree t2)
4934 if (t1 == t2)
4935 return 0;
4937 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
4939 int t1_sgn = tree_int_cst_sgn (t1);
4940 int t2_sgn = tree_int_cst_sgn (t2);
4942 if (t1_sgn < t2_sgn)
4943 return 1;
4944 else if (t1_sgn > t2_sgn)
4945 return 0;
4946 /* Otherwise, both are non-negative, so we compare them as
4947 unsigned just in case one of them would overflow a signed
4948 type. */
4950 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
4951 return INT_CST_LT (t1, t2);
4953 return INT_CST_LT_UNSIGNED (t1, t2);
4956 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
4959 tree_int_cst_compare (const_tree t1, const_tree t2)
4961 if (tree_int_cst_lt (t1, t2))
4962 return -1;
4963 else if (tree_int_cst_lt (t2, t1))
4964 return 1;
4965 else
4966 return 0;
4969 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
4970 the host. If POS is zero, the value can be represented in a single
4971 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
4972 be represented in a single unsigned HOST_WIDE_INT. */
4975 host_integerp (const_tree t, int pos)
4977 return (TREE_CODE (t) == INTEGER_CST
4978 && ((TREE_INT_CST_HIGH (t) == 0
4979 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
4980 || (! pos && TREE_INT_CST_HIGH (t) == -1
4981 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
4982 && (!TYPE_UNSIGNED (TREE_TYPE (t))
4983 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
4984 && TYPE_IS_SIZETYPE (TREE_TYPE (t)))))
4985 || (pos && TREE_INT_CST_HIGH (t) == 0)));
4988 /* Return the HOST_WIDE_INT least significant bits of T if it is an
4989 INTEGER_CST and there is no overflow. POS is nonzero if the result must
4990 be non-negative. We must be able to satisfy the above conditions. */
4992 HOST_WIDE_INT
4993 tree_low_cst (const_tree t, int pos)
4995 gcc_assert (host_integerp (t, pos));
4996 return TREE_INT_CST_LOW (t);
4999 /* Return the most significant bit of the integer constant T. */
5002 tree_int_cst_msb (const_tree t)
5004 int prec;
5005 HOST_WIDE_INT h;
5006 unsigned HOST_WIDE_INT l;
5008 /* Note that using TYPE_PRECISION here is wrong. We care about the
5009 actual bits, not the (arbitrary) range of the type. */
5010 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
5011 rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
5012 2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
5013 return (l & 1) == 1;
5016 /* Return an indication of the sign of the integer constant T.
5017 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
5018 Note that -1 will never be returned if T's type is unsigned. */
5021 tree_int_cst_sgn (const_tree t)
5023 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
5024 return 0;
5025 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
5026 return 1;
5027 else if (TREE_INT_CST_HIGH (t) < 0)
5028 return -1;
5029 else
5030 return 1;
5033 /* Return the minimum number of bits needed to represent VALUE in a
5034 signed or unsigned type, UNSIGNEDP says which. */
5036 unsigned int
5037 tree_int_cst_min_precision (tree value, bool unsignedp)
5039 int log;
5041 /* If the value is negative, compute its negative minus 1. The latter
5042 adjustment is because the absolute value of the largest negative value
5043 is one larger than the largest positive value. This is equivalent to
5044 a bit-wise negation, so use that operation instead. */
5046 if (tree_int_cst_sgn (value) < 0)
5047 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
5049 /* Return the number of bits needed, taking into account the fact
5050 that we need one more bit for a signed than unsigned type. */
5052 if (integer_zerop (value))
5053 log = 0;
5054 else
5055 log = tree_floor_log2 (value);
5057 return log + 1 + !unsignedp;
5060 /* Compare two constructor-element-type constants. Return 1 if the lists
5061 are known to be equal; otherwise return 0. */
5064 simple_cst_list_equal (const_tree l1, const_tree l2)
5066 while (l1 != NULL_TREE && l2 != NULL_TREE)
5068 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
5069 return 0;
5071 l1 = TREE_CHAIN (l1);
5072 l2 = TREE_CHAIN (l2);
5075 return l1 == l2;
5078 /* Return truthvalue of whether T1 is the same tree structure as T2.
5079 Return 1 if they are the same.
5080 Return 0 if they are understandably different.
5081 Return -1 if either contains tree structure not understood by
5082 this function. */
5085 simple_cst_equal (const_tree t1, const_tree t2)
5087 enum tree_code code1, code2;
5088 int cmp;
5089 int i;
5091 if (t1 == t2)
5092 return 1;
5093 if (t1 == 0 || t2 == 0)
5094 return 0;
5096 code1 = TREE_CODE (t1);
5097 code2 = TREE_CODE (t2);
5099 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
5101 if (CONVERT_EXPR_CODE_P (code2)
5102 || code2 == NON_LVALUE_EXPR)
5103 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5104 else
5105 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
5108 else if (CONVERT_EXPR_CODE_P (code2)
5109 || code2 == NON_LVALUE_EXPR)
5110 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
5112 if (code1 != code2)
5113 return 0;
5115 switch (code1)
5117 case INTEGER_CST:
5118 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
5119 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
5121 case REAL_CST:
5122 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
5124 case FIXED_CST:
5125 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
5127 case STRING_CST:
5128 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
5129 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
5130 TREE_STRING_LENGTH (t1)));
5132 case CONSTRUCTOR:
5134 unsigned HOST_WIDE_INT idx;
5135 VEC(constructor_elt, gc) *v1 = CONSTRUCTOR_ELTS (t1);
5136 VEC(constructor_elt, gc) *v2 = CONSTRUCTOR_ELTS (t2);
5138 if (VEC_length (constructor_elt, v1) != VEC_length (constructor_elt, v2))
5139 return false;
5141 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
5142 /* ??? Should we handle also fields here? */
5143 if (!simple_cst_equal (VEC_index (constructor_elt, v1, idx)->value,
5144 VEC_index (constructor_elt, v2, idx)->value))
5145 return false;
5146 return true;
5149 case SAVE_EXPR:
5150 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5152 case CALL_EXPR:
5153 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
5154 if (cmp <= 0)
5155 return cmp;
5156 if (call_expr_nargs (t1) != call_expr_nargs (t2))
5157 return 0;
5159 const_tree arg1, arg2;
5160 const_call_expr_arg_iterator iter1, iter2;
5161 for (arg1 = first_const_call_expr_arg (t1, &iter1),
5162 arg2 = first_const_call_expr_arg (t2, &iter2);
5163 arg1 && arg2;
5164 arg1 = next_const_call_expr_arg (&iter1),
5165 arg2 = next_const_call_expr_arg (&iter2))
5167 cmp = simple_cst_equal (arg1, arg2);
5168 if (cmp <= 0)
5169 return cmp;
5171 return arg1 == arg2;
5174 case TARGET_EXPR:
5175 /* Special case: if either target is an unallocated VAR_DECL,
5176 it means that it's going to be unified with whatever the
5177 TARGET_EXPR is really supposed to initialize, so treat it
5178 as being equivalent to anything. */
5179 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
5180 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
5181 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
5182 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
5183 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
5184 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
5185 cmp = 1;
5186 else
5187 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5189 if (cmp <= 0)
5190 return cmp;
5192 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
5194 case WITH_CLEANUP_EXPR:
5195 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5196 if (cmp <= 0)
5197 return cmp;
5199 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
5201 case COMPONENT_REF:
5202 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
5203 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5205 return 0;
5207 case VAR_DECL:
5208 case PARM_DECL:
5209 case CONST_DECL:
5210 case FUNCTION_DECL:
5211 return 0;
5213 default:
5214 break;
5217 /* This general rule works for most tree codes. All exceptions should be
5218 handled above. If this is a language-specific tree code, we can't
5219 trust what might be in the operand, so say we don't know
5220 the situation. */
5221 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
5222 return -1;
5224 switch (TREE_CODE_CLASS (code1))
5226 case tcc_unary:
5227 case tcc_binary:
5228 case tcc_comparison:
5229 case tcc_expression:
5230 case tcc_reference:
5231 case tcc_statement:
5232 cmp = 1;
5233 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
5235 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
5236 if (cmp <= 0)
5237 return cmp;
5240 return cmp;
5242 default:
5243 return -1;
5247 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
5248 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
5249 than U, respectively. */
5252 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
5254 if (tree_int_cst_sgn (t) < 0)
5255 return -1;
5256 else if (TREE_INT_CST_HIGH (t) != 0)
5257 return 1;
5258 else if (TREE_INT_CST_LOW (t) == u)
5259 return 0;
5260 else if (TREE_INT_CST_LOW (t) < u)
5261 return -1;
5262 else
5263 return 1;
5266 /* Return true if CODE represents an associative tree code. Otherwise
5267 return false. */
5268 bool
5269 associative_tree_code (enum tree_code code)
5271 switch (code)
5273 case BIT_IOR_EXPR:
5274 case BIT_AND_EXPR:
5275 case BIT_XOR_EXPR:
5276 case PLUS_EXPR:
5277 case MULT_EXPR:
5278 case MIN_EXPR:
5279 case MAX_EXPR:
5280 return true;
5282 default:
5283 break;
5285 return false;
5288 /* Return true if CODE represents a commutative tree code. Otherwise
5289 return false. */
5290 bool
5291 commutative_tree_code (enum tree_code code)
5293 switch (code)
5295 case PLUS_EXPR:
5296 case MULT_EXPR:
5297 case MIN_EXPR:
5298 case MAX_EXPR:
5299 case BIT_IOR_EXPR:
5300 case BIT_XOR_EXPR:
5301 case BIT_AND_EXPR:
5302 case NE_EXPR:
5303 case EQ_EXPR:
5304 case UNORDERED_EXPR:
5305 case ORDERED_EXPR:
5306 case UNEQ_EXPR:
5307 case LTGT_EXPR:
5308 case TRUTH_AND_EXPR:
5309 case TRUTH_XOR_EXPR:
5310 case TRUTH_OR_EXPR:
5311 return true;
5313 default:
5314 break;
5316 return false;
5319 /* Generate a hash value for an expression. This can be used iteratively
5320 by passing a previous result as the VAL argument.
5322 This function is intended to produce the same hash for expressions which
5323 would compare equal using operand_equal_p. */
5325 hashval_t
5326 iterative_hash_expr (const_tree t, hashval_t val)
5328 int i;
5329 enum tree_code code;
5330 char tclass;
5332 if (t == NULL_TREE)
5333 return iterative_hash_pointer (t, val);
5335 code = TREE_CODE (t);
5337 switch (code)
5339 /* Alas, constants aren't shared, so we can't rely on pointer
5340 identity. */
5341 case INTEGER_CST:
5342 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
5343 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
5344 case REAL_CST:
5346 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
5348 return iterative_hash_hashval_t (val2, val);
5350 case FIXED_CST:
5352 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
5354 return iterative_hash_hashval_t (val2, val);
5356 case STRING_CST:
5357 return iterative_hash (TREE_STRING_POINTER (t),
5358 TREE_STRING_LENGTH (t), val);
5359 case COMPLEX_CST:
5360 val = iterative_hash_expr (TREE_REALPART (t), val);
5361 return iterative_hash_expr (TREE_IMAGPART (t), val);
5362 case VECTOR_CST:
5363 return iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
5365 case SSA_NAME:
5366 /* we can just compare by pointer. */
5367 return iterative_hash_pointer (t, val);
5369 case TREE_LIST:
5370 /* A list of expressions, for a CALL_EXPR or as the elements of a
5371 VECTOR_CST. */
5372 for (; t; t = TREE_CHAIN (t))
5373 val = iterative_hash_expr (TREE_VALUE (t), val);
5374 return val;
5375 case CONSTRUCTOR:
5377 unsigned HOST_WIDE_INT idx;
5378 tree field, value;
5379 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
5381 val = iterative_hash_expr (field, val);
5382 val = iterative_hash_expr (value, val);
5384 return val;
5386 case FUNCTION_DECL:
5387 /* When referring to a built-in FUNCTION_DECL, use the
5388 __builtin__ form. Otherwise nodes that compare equal
5389 according to operand_equal_p might get different
5390 hash codes. */
5391 if (DECL_BUILT_IN (t))
5393 val = iterative_hash_pointer (built_in_decls[DECL_FUNCTION_CODE (t)],
5394 val);
5395 return val;
5397 /* else FALL THROUGH */
5398 default:
5399 tclass = TREE_CODE_CLASS (code);
5401 if (tclass == tcc_declaration)
5403 /* DECL's have a unique ID */
5404 val = iterative_hash_host_wide_int (DECL_UID (t), val);
5406 else
5408 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
5410 val = iterative_hash_object (code, val);
5412 /* Don't hash the type, that can lead to having nodes which
5413 compare equal according to operand_equal_p, but which
5414 have different hash codes. */
5415 if (CONVERT_EXPR_CODE_P (code)
5416 || code == NON_LVALUE_EXPR)
5418 /* Make sure to include signness in the hash computation. */
5419 val += TYPE_UNSIGNED (TREE_TYPE (t));
5420 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
5423 else if (commutative_tree_code (code))
5425 /* It's a commutative expression. We want to hash it the same
5426 however it appears. We do this by first hashing both operands
5427 and then rehashing based on the order of their independent
5428 hashes. */
5429 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
5430 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
5431 hashval_t t;
5433 if (one > two)
5434 t = one, one = two, two = t;
5436 val = iterative_hash_hashval_t (one, val);
5437 val = iterative_hash_hashval_t (two, val);
5439 else
5440 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
5441 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
5443 return val;
5444 break;
5448 /* Generate a hash value for a pair of expressions. This can be used
5449 iteratively by passing a previous result as the VAL argument.
5451 The same hash value is always returned for a given pair of expressions,
5452 regardless of the order in which they are presented. This is useful in
5453 hashing the operands of commutative functions. */
5455 hashval_t
5456 iterative_hash_exprs_commutative (const_tree t1,
5457 const_tree t2, hashval_t val)
5459 hashval_t one = iterative_hash_expr (t1, 0);
5460 hashval_t two = iterative_hash_expr (t2, 0);
5461 hashval_t t;
5463 if (one > two)
5464 t = one, one = two, two = t;
5465 val = iterative_hash_hashval_t (one, val);
5466 val = iterative_hash_hashval_t (two, val);
5468 return val;
5471 /* Constructors for pointer, array and function types.
5472 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
5473 constructed by language-dependent code, not here.) */
5475 /* Construct, lay out and return the type of pointers to TO_TYPE with
5476 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
5477 reference all of memory. If such a type has already been
5478 constructed, reuse it. */
5480 tree
5481 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
5482 bool can_alias_all)
5484 tree t;
5486 if (to_type == error_mark_node)
5487 return error_mark_node;
5489 /* If the pointed-to type has the may_alias attribute set, force
5490 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
5491 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
5492 can_alias_all = true;
5494 /* In some cases, languages will have things that aren't a POINTER_TYPE
5495 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
5496 In that case, return that type without regard to the rest of our
5497 operands.
5499 ??? This is a kludge, but consistent with the way this function has
5500 always operated and there doesn't seem to be a good way to avoid this
5501 at the moment. */
5502 if (TYPE_POINTER_TO (to_type) != 0
5503 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
5504 return TYPE_POINTER_TO (to_type);
5506 /* First, if we already have a type for pointers to TO_TYPE and it's
5507 the proper mode, use it. */
5508 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
5509 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
5510 return t;
5512 t = make_node (POINTER_TYPE);
5514 TREE_TYPE (t) = to_type;
5515 SET_TYPE_MODE (t, mode);
5516 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
5517 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
5518 TYPE_POINTER_TO (to_type) = t;
5520 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
5521 SET_TYPE_STRUCTURAL_EQUALITY (t);
5522 else if (TYPE_CANONICAL (to_type) != to_type)
5523 TYPE_CANONICAL (t)
5524 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
5525 mode, can_alias_all);
5527 /* Lay out the type. This function has many callers that are concerned
5528 with expression-construction, and this simplifies them all. */
5529 layout_type (t);
5531 return t;
5534 /* By default build pointers in ptr_mode. */
5536 tree
5537 build_pointer_type (tree to_type)
5539 return build_pointer_type_for_mode (to_type, ptr_mode, false);
5542 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
5544 tree
5545 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
5546 bool can_alias_all)
5548 tree t;
5550 if (to_type == error_mark_node)
5551 return error_mark_node;
5553 /* If the pointed-to type has the may_alias attribute set, force
5554 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
5555 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
5556 can_alias_all = true;
5558 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
5559 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
5560 In that case, return that type without regard to the rest of our
5561 operands.
5563 ??? This is a kludge, but consistent with the way this function has
5564 always operated and there doesn't seem to be a good way to avoid this
5565 at the moment. */
5566 if (TYPE_REFERENCE_TO (to_type) != 0
5567 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
5568 return TYPE_REFERENCE_TO (to_type);
5570 /* First, if we already have a type for pointers to TO_TYPE and it's
5571 the proper mode, use it. */
5572 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
5573 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
5574 return t;
5576 t = make_node (REFERENCE_TYPE);
5578 TREE_TYPE (t) = to_type;
5579 SET_TYPE_MODE (t, mode);
5580 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
5581 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
5582 TYPE_REFERENCE_TO (to_type) = t;
5584 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
5585 SET_TYPE_STRUCTURAL_EQUALITY (t);
5586 else if (TYPE_CANONICAL (to_type) != to_type)
5587 TYPE_CANONICAL (t)
5588 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
5589 mode, can_alias_all);
5591 layout_type (t);
5593 return t;
5597 /* Build the node for the type of references-to-TO_TYPE by default
5598 in ptr_mode. */
5600 tree
5601 build_reference_type (tree to_type)
5603 return build_reference_type_for_mode (to_type, ptr_mode, false);
5606 /* Build a type that is compatible with t but has no cv quals anywhere
5607 in its type, thus
5609 const char *const *const * -> char ***. */
5611 tree
5612 build_type_no_quals (tree t)
5614 switch (TREE_CODE (t))
5616 case POINTER_TYPE:
5617 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
5618 TYPE_MODE (t),
5619 TYPE_REF_CAN_ALIAS_ALL (t));
5620 case REFERENCE_TYPE:
5621 return
5622 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
5623 TYPE_MODE (t),
5624 TYPE_REF_CAN_ALIAS_ALL (t));
5625 default:
5626 return TYPE_MAIN_VARIANT (t);
5630 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
5631 MAXVAL should be the maximum value in the domain
5632 (one less than the length of the array).
5634 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
5635 We don't enforce this limit, that is up to caller (e.g. language front end).
5636 The limit exists because the result is a signed type and we don't handle
5637 sizes that use more than one HOST_WIDE_INT. */
5639 tree
5640 build_index_type (tree maxval)
5642 tree itype = make_node (INTEGER_TYPE);
5644 TREE_TYPE (itype) = sizetype;
5645 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
5646 TYPE_MIN_VALUE (itype) = size_zero_node;
5647 TYPE_MAX_VALUE (itype) = fold_convert (sizetype, maxval);
5648 SET_TYPE_MODE (itype, TYPE_MODE (sizetype));
5649 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
5650 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
5651 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
5652 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
5654 if (host_integerp (maxval, 1))
5655 return type_hash_canon (tree_low_cst (maxval, 1), itype);
5656 else
5658 /* Since we cannot hash this type, we need to compare it using
5659 structural equality checks. */
5660 SET_TYPE_STRUCTURAL_EQUALITY (itype);
5661 return itype;
5665 /* Builds a signed or unsigned integer type of precision PRECISION.
5666 Used for C bitfields whose precision does not match that of
5667 built-in target types. */
5668 tree
5669 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
5670 int unsignedp)
5672 tree itype = make_node (INTEGER_TYPE);
5674 TYPE_PRECISION (itype) = precision;
5676 if (unsignedp)
5677 fixup_unsigned_type (itype);
5678 else
5679 fixup_signed_type (itype);
5681 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
5682 return type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
5684 return itype;
5687 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
5688 ENUMERAL_TYPE or BOOLEAN_TYPE), with low bound LOWVAL and
5689 high bound HIGHVAL. If TYPE is NULL, sizetype is used. */
5691 tree
5692 build_range_type (tree type, tree lowval, tree highval)
5694 tree itype = make_node (INTEGER_TYPE);
5696 TREE_TYPE (itype) = type;
5697 if (type == NULL_TREE)
5698 type = sizetype;
5700 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
5701 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
5703 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
5704 SET_TYPE_MODE (itype, TYPE_MODE (type));
5705 TYPE_SIZE (itype) = TYPE_SIZE (type);
5706 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
5707 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
5708 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
5710 if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
5711 return type_hash_canon (tree_low_cst (highval, 0)
5712 - tree_low_cst (lowval, 0),
5713 itype);
5714 else
5715 return itype;
5718 /* Just like build_index_type, but takes lowval and highval instead
5719 of just highval (maxval). */
5721 tree
5722 build_index_2_type (tree lowval, tree highval)
5724 return build_range_type (sizetype, lowval, highval);
5727 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
5728 and number of elements specified by the range of values of INDEX_TYPE.
5729 If such a type has already been constructed, reuse it. */
5731 tree
5732 build_array_type (tree elt_type, tree index_type)
5734 tree t;
5735 hashval_t hashcode = 0;
5737 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
5739 error ("arrays of functions are not meaningful");
5740 elt_type = integer_type_node;
5743 t = make_node (ARRAY_TYPE);
5744 TREE_TYPE (t) = elt_type;
5745 TYPE_DOMAIN (t) = index_type;
5747 if (index_type == 0)
5749 tree save = t;
5750 hashcode = iterative_hash_object (TYPE_HASH (elt_type), hashcode);
5751 t = type_hash_canon (hashcode, t);
5752 if (save == t)
5753 layout_type (t);
5755 if (TYPE_CANONICAL (t) == t)
5757 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type))
5758 SET_TYPE_STRUCTURAL_EQUALITY (t);
5759 else if (TYPE_CANONICAL (elt_type) != elt_type)
5760 TYPE_CANONICAL (t)
5761 = build_array_type (TYPE_CANONICAL (elt_type), index_type);
5764 return t;
5767 hashcode = iterative_hash_object (TYPE_HASH (elt_type), hashcode);
5768 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
5769 t = type_hash_canon (hashcode, t);
5771 if (!COMPLETE_TYPE_P (t))
5772 layout_type (t);
5774 if (TYPE_CANONICAL (t) == t)
5776 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
5777 || TYPE_STRUCTURAL_EQUALITY_P (index_type))
5778 SET_TYPE_STRUCTURAL_EQUALITY (t);
5779 else if (TYPE_CANONICAL (elt_type) != elt_type
5780 || TYPE_CANONICAL (index_type) != index_type)
5781 TYPE_CANONICAL (t)
5782 = build_array_type (TYPE_CANONICAL (elt_type),
5783 TYPE_CANONICAL (index_type));
5786 return t;
5789 /* Recursively examines the array elements of TYPE, until a non-array
5790 element type is found. */
5792 tree
5793 strip_array_types (tree type)
5795 while (TREE_CODE (type) == ARRAY_TYPE)
5796 type = TREE_TYPE (type);
5798 return type;
5801 /* Computes the canonical argument types from the argument type list
5802 ARGTYPES.
5804 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
5805 on entry to this function, or if any of the ARGTYPES are
5806 structural.
5808 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
5809 true on entry to this function, or if any of the ARGTYPES are
5810 non-canonical.
5812 Returns a canonical argument list, which may be ARGTYPES when the
5813 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
5814 true) or would not differ from ARGTYPES. */
5816 static tree
5817 maybe_canonicalize_argtypes(tree argtypes,
5818 bool *any_structural_p,
5819 bool *any_noncanonical_p)
5821 tree arg;
5822 bool any_noncanonical_argtypes_p = false;
5824 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
5826 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
5827 /* Fail gracefully by stating that the type is structural. */
5828 *any_structural_p = true;
5829 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
5830 *any_structural_p = true;
5831 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
5832 || TREE_PURPOSE (arg))
5833 /* If the argument has a default argument, we consider it
5834 non-canonical even though the type itself is canonical.
5835 That way, different variants of function and method types
5836 with default arguments will all point to the variant with
5837 no defaults as their canonical type. */
5838 any_noncanonical_argtypes_p = true;
5841 if (*any_structural_p)
5842 return argtypes;
5844 if (any_noncanonical_argtypes_p)
5846 /* Build the canonical list of argument types. */
5847 tree canon_argtypes = NULL_TREE;
5848 bool is_void = false;
5850 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
5852 if (arg == void_list_node)
5853 is_void = true;
5854 else
5855 canon_argtypes = tree_cons (NULL_TREE,
5856 TYPE_CANONICAL (TREE_VALUE (arg)),
5857 canon_argtypes);
5860 canon_argtypes = nreverse (canon_argtypes);
5861 if (is_void)
5862 canon_argtypes = chainon (canon_argtypes, void_list_node);
5864 /* There is a non-canonical type. */
5865 *any_noncanonical_p = true;
5866 return canon_argtypes;
5869 /* The canonical argument types are the same as ARGTYPES. */
5870 return argtypes;
5873 /* Construct, lay out and return
5874 the type of functions returning type VALUE_TYPE
5875 given arguments of types ARG_TYPES.
5876 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
5877 are data type nodes for the arguments of the function.
5878 If such a type has already been constructed, reuse it. */
5880 tree
5881 build_function_type (tree value_type, tree arg_types)
5883 tree t;
5884 hashval_t hashcode = 0;
5885 bool any_structural_p, any_noncanonical_p;
5886 tree canon_argtypes;
5888 if (TREE_CODE (value_type) == FUNCTION_TYPE)
5890 error ("function return type cannot be function");
5891 value_type = integer_type_node;
5894 /* Make a node of the sort we want. */
5895 t = make_node (FUNCTION_TYPE);
5896 TREE_TYPE (t) = value_type;
5897 TYPE_ARG_TYPES (t) = arg_types;
5899 /* If we already have such a type, use the old one. */
5900 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
5901 hashcode = type_hash_list (arg_types, hashcode);
5902 t = type_hash_canon (hashcode, t);
5904 /* Set up the canonical type. */
5905 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
5906 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
5907 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
5908 &any_structural_p,
5909 &any_noncanonical_p);
5910 if (any_structural_p)
5911 SET_TYPE_STRUCTURAL_EQUALITY (t);
5912 else if (any_noncanonical_p)
5913 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
5914 canon_argtypes);
5916 if (!COMPLETE_TYPE_P (t))
5917 layout_type (t);
5918 return t;
5921 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP. */
5923 tree
5924 build_function_type_skip_args (tree orig_type, bitmap args_to_skip)
5926 tree new_type = NULL;
5927 tree args, new_args = NULL, t;
5928 tree new_reversed;
5929 int i = 0;
5931 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
5932 args = TREE_CHAIN (args), i++)
5933 if (!bitmap_bit_p (args_to_skip, i))
5934 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
5936 new_reversed = nreverse (new_args);
5937 if (args)
5939 if (new_reversed)
5940 TREE_CHAIN (new_args) = void_list_node;
5941 else
5942 new_reversed = void_list_node;
5944 gcc_assert (new_reversed);
5946 /* Use copy_node to preserve as much as possible from original type
5947 (debug info, attribute lists etc.)
5948 Exception is METHOD_TYPEs must have THIS argument.
5949 When we are asked to remove it, we need to build new FUNCTION_TYPE
5950 instead. */
5951 if (TREE_CODE (orig_type) != METHOD_TYPE
5952 || !bitmap_bit_p (args_to_skip, 0))
5954 new_type = copy_node (orig_type);
5955 TYPE_ARG_TYPES (new_type) = new_reversed;
5957 else
5959 new_type
5960 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
5961 new_reversed));
5962 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
5965 /* This is a new type, not a copy of an old type. Need to reassociate
5966 variants. We can handle everything except the main variant lazily. */
5967 t = TYPE_MAIN_VARIANT (orig_type);
5968 if (orig_type != t)
5970 TYPE_MAIN_VARIANT (new_type) = t;
5971 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
5972 TYPE_NEXT_VARIANT (t) = new_type;
5974 else
5976 TYPE_MAIN_VARIANT (new_type) = new_type;
5977 TYPE_NEXT_VARIANT (new_type) = NULL;
5979 return new_type;
5982 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP.
5984 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
5985 linked by TREE_CHAIN directly. It is caller responsibility to eliminate
5986 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
5988 tree
5989 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip)
5991 tree new_decl = copy_node (orig_decl);
5992 tree new_type;
5994 new_type = TREE_TYPE (orig_decl);
5995 if (prototype_p (new_type))
5996 new_type = build_function_type_skip_args (new_type, args_to_skip);
5997 TREE_TYPE (new_decl) = new_type;
5999 /* For declarations setting DECL_VINDEX (i.e. methods)
6000 we expect first argument to be THIS pointer. */
6001 if (bitmap_bit_p (args_to_skip, 0))
6002 DECL_VINDEX (new_decl) = NULL_TREE;
6003 return new_decl;
6006 /* Build a function type. The RETURN_TYPE is the type returned by the
6007 function. If VAARGS is set, no void_type_node is appended to the
6008 the list. ARGP muse be alway be terminated be a NULL_TREE. */
6010 static tree
6011 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
6013 tree t, args, last;
6015 t = va_arg (argp, tree);
6016 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
6017 args = tree_cons (NULL_TREE, t, args);
6019 if (vaargs)
6021 last = args;
6022 if (args != NULL_TREE)
6023 args = nreverse (args);
6024 gcc_assert (args != NULL_TREE && last != void_list_node);
6026 else if (args == NULL_TREE)
6027 args = void_list_node;
6028 else
6030 last = args;
6031 args = nreverse (args);
6032 TREE_CHAIN (last) = void_list_node;
6034 args = build_function_type (return_type, args);
6036 return args;
6039 /* Build a function type. The RETURN_TYPE is the type returned by the
6040 function. If additional arguments are provided, they are
6041 additional argument types. The list of argument types must always
6042 be terminated by NULL_TREE. */
6044 tree
6045 build_function_type_list (tree return_type, ...)
6047 tree args;
6048 va_list p;
6050 va_start (p, return_type);
6051 args = build_function_type_list_1 (false, return_type, p);
6052 va_end (p);
6053 return args;
6056 /* Build a variable argument function type. The RETURN_TYPE is the
6057 type returned by the function. If additional arguments are provided,
6058 they are additional argument types. The list of argument types must
6059 always be terminated by NULL_TREE. */
6061 tree
6062 build_varargs_function_type_list (tree return_type, ...)
6064 tree args;
6065 va_list p;
6067 va_start (p, return_type);
6068 args = build_function_type_list_1 (true, return_type, p);
6069 va_end (p);
6071 return args;
6074 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
6075 and ARGTYPES (a TREE_LIST) are the return type and arguments types
6076 for the method. An implicit additional parameter (of type
6077 pointer-to-BASETYPE) is added to the ARGTYPES. */
6079 tree
6080 build_method_type_directly (tree basetype,
6081 tree rettype,
6082 tree argtypes)
6084 tree t;
6085 tree ptype;
6086 int hashcode = 0;
6087 bool any_structural_p, any_noncanonical_p;
6088 tree canon_argtypes;
6090 /* Make a node of the sort we want. */
6091 t = make_node (METHOD_TYPE);
6093 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
6094 TREE_TYPE (t) = rettype;
6095 ptype = build_pointer_type (basetype);
6097 /* The actual arglist for this function includes a "hidden" argument
6098 which is "this". Put it into the list of argument types. */
6099 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
6100 TYPE_ARG_TYPES (t) = argtypes;
6102 /* If we already have such a type, use the old one. */
6103 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
6104 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
6105 hashcode = type_hash_list (argtypes, hashcode);
6106 t = type_hash_canon (hashcode, t);
6108 /* Set up the canonical type. */
6109 any_structural_p
6110 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
6111 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
6112 any_noncanonical_p
6113 = (TYPE_CANONICAL (basetype) != basetype
6114 || TYPE_CANONICAL (rettype) != rettype);
6115 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
6116 &any_structural_p,
6117 &any_noncanonical_p);
6118 if (any_structural_p)
6119 SET_TYPE_STRUCTURAL_EQUALITY (t);
6120 else if (any_noncanonical_p)
6121 TYPE_CANONICAL (t)
6122 = build_method_type_directly (TYPE_CANONICAL (basetype),
6123 TYPE_CANONICAL (rettype),
6124 canon_argtypes);
6125 if (!COMPLETE_TYPE_P (t))
6126 layout_type (t);
6128 return t;
6131 /* Construct, lay out and return the type of methods belonging to class
6132 BASETYPE and whose arguments and values are described by TYPE.
6133 If that type exists already, reuse it.
6134 TYPE must be a FUNCTION_TYPE node. */
6136 tree
6137 build_method_type (tree basetype, tree type)
6139 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
6141 return build_method_type_directly (basetype,
6142 TREE_TYPE (type),
6143 TYPE_ARG_TYPES (type));
6146 /* Construct, lay out and return the type of offsets to a value
6147 of type TYPE, within an object of type BASETYPE.
6148 If a suitable offset type exists already, reuse it. */
6150 tree
6151 build_offset_type (tree basetype, tree type)
6153 tree t;
6154 hashval_t hashcode = 0;
6156 /* Make a node of the sort we want. */
6157 t = make_node (OFFSET_TYPE);
6159 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
6160 TREE_TYPE (t) = type;
6162 /* If we already have such a type, use the old one. */
6163 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
6164 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
6165 t = type_hash_canon (hashcode, t);
6167 if (!COMPLETE_TYPE_P (t))
6168 layout_type (t);
6170 if (TYPE_CANONICAL (t) == t)
6172 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
6173 || TYPE_STRUCTURAL_EQUALITY_P (type))
6174 SET_TYPE_STRUCTURAL_EQUALITY (t);
6175 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
6176 || TYPE_CANONICAL (type) != type)
6177 TYPE_CANONICAL (t)
6178 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
6179 TYPE_CANONICAL (type));
6182 return t;
6185 /* Create a complex type whose components are COMPONENT_TYPE. */
6187 tree
6188 build_complex_type (tree component_type)
6190 tree t;
6191 hashval_t hashcode;
6193 gcc_assert (INTEGRAL_TYPE_P (component_type)
6194 || SCALAR_FLOAT_TYPE_P (component_type)
6195 || FIXED_POINT_TYPE_P (component_type));
6197 /* Make a node of the sort we want. */
6198 t = make_node (COMPLEX_TYPE);
6200 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
6202 /* If we already have such a type, use the old one. */
6203 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
6204 t = type_hash_canon (hashcode, t);
6206 if (!COMPLETE_TYPE_P (t))
6207 layout_type (t);
6209 if (TYPE_CANONICAL (t) == t)
6211 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
6212 SET_TYPE_STRUCTURAL_EQUALITY (t);
6213 else if (TYPE_CANONICAL (component_type) != component_type)
6214 TYPE_CANONICAL (t)
6215 = build_complex_type (TYPE_CANONICAL (component_type));
6218 /* We need to create a name, since complex is a fundamental type. */
6219 if (! TYPE_NAME (t))
6221 const char *name;
6222 if (component_type == char_type_node)
6223 name = "complex char";
6224 else if (component_type == signed_char_type_node)
6225 name = "complex signed char";
6226 else if (component_type == unsigned_char_type_node)
6227 name = "complex unsigned char";
6228 else if (component_type == short_integer_type_node)
6229 name = "complex short int";
6230 else if (component_type == short_unsigned_type_node)
6231 name = "complex short unsigned int";
6232 else if (component_type == integer_type_node)
6233 name = "complex int";
6234 else if (component_type == unsigned_type_node)
6235 name = "complex unsigned int";
6236 else if (component_type == long_integer_type_node)
6237 name = "complex long int";
6238 else if (component_type == long_unsigned_type_node)
6239 name = "complex long unsigned int";
6240 else if (component_type == long_long_integer_type_node)
6241 name = "complex long long int";
6242 else if (component_type == long_long_unsigned_type_node)
6243 name = "complex long long unsigned int";
6244 else
6245 name = 0;
6247 if (name != 0)
6248 TYPE_NAME (t) = build_decl (TYPE_DECL, get_identifier (name), t);
6251 return build_qualified_type (t, TYPE_QUALS (component_type));
6254 /* If TYPE is a real or complex floating-point type and the target
6255 does not directly support arithmetic on TYPE then return the wider
6256 type to be used for arithmetic on TYPE. Otherwise, return
6257 NULL_TREE. */
6259 tree
6260 excess_precision_type (tree type)
6262 if (flag_excess_precision != EXCESS_PRECISION_FAST)
6264 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
6265 switch (TREE_CODE (type))
6267 case REAL_TYPE:
6268 switch (flt_eval_method)
6270 case 1:
6271 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
6272 return double_type_node;
6273 break;
6274 case 2:
6275 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
6276 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
6277 return long_double_type_node;
6278 break;
6279 default:
6280 gcc_unreachable ();
6282 break;
6283 case COMPLEX_TYPE:
6284 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
6285 return NULL_TREE;
6286 switch (flt_eval_method)
6288 case 1:
6289 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
6290 return complex_double_type_node;
6291 break;
6292 case 2:
6293 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
6294 || (TYPE_MODE (TREE_TYPE (type))
6295 == TYPE_MODE (double_type_node)))
6296 return complex_long_double_type_node;
6297 break;
6298 default:
6299 gcc_unreachable ();
6301 break;
6302 default:
6303 break;
6306 return NULL_TREE;
6309 /* Return OP, stripped of any conversions to wider types as much as is safe.
6310 Converting the value back to OP's type makes a value equivalent to OP.
6312 If FOR_TYPE is nonzero, we return a value which, if converted to
6313 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
6315 OP must have integer, real or enumeral type. Pointers are not allowed!
6317 There are some cases where the obvious value we could return
6318 would regenerate to OP if converted to OP's type,
6319 but would not extend like OP to wider types.
6320 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
6321 For example, if OP is (unsigned short)(signed char)-1,
6322 we avoid returning (signed char)-1 if FOR_TYPE is int,
6323 even though extending that to an unsigned short would regenerate OP,
6324 since the result of extending (signed char)-1 to (int)
6325 is different from (int) OP. */
6327 tree
6328 get_unwidened (tree op, tree for_type)
6330 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
6331 tree type = TREE_TYPE (op);
6332 unsigned final_prec
6333 = TYPE_PRECISION (for_type != 0 ? for_type : type);
6334 int uns
6335 = (for_type != 0 && for_type != type
6336 && final_prec > TYPE_PRECISION (type)
6337 && TYPE_UNSIGNED (type));
6338 tree win = op;
6340 while (CONVERT_EXPR_P (op))
6342 int bitschange;
6344 /* TYPE_PRECISION on vector types has different meaning
6345 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
6346 so avoid them here. */
6347 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
6348 break;
6350 bitschange = TYPE_PRECISION (TREE_TYPE (op))
6351 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
6353 /* Truncations are many-one so cannot be removed.
6354 Unless we are later going to truncate down even farther. */
6355 if (bitschange < 0
6356 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
6357 break;
6359 /* See what's inside this conversion. If we decide to strip it,
6360 we will set WIN. */
6361 op = TREE_OPERAND (op, 0);
6363 /* If we have not stripped any zero-extensions (uns is 0),
6364 we can strip any kind of extension.
6365 If we have previously stripped a zero-extension,
6366 only zero-extensions can safely be stripped.
6367 Any extension can be stripped if the bits it would produce
6368 are all going to be discarded later by truncating to FOR_TYPE. */
6370 if (bitschange > 0)
6372 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
6373 win = op;
6374 /* TYPE_UNSIGNED says whether this is a zero-extension.
6375 Let's avoid computing it if it does not affect WIN
6376 and if UNS will not be needed again. */
6377 if ((uns
6378 || CONVERT_EXPR_P (op))
6379 && TYPE_UNSIGNED (TREE_TYPE (op)))
6381 uns = 1;
6382 win = op;
6387 return win;
6390 /* Return OP or a simpler expression for a narrower value
6391 which can be sign-extended or zero-extended to give back OP.
6392 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
6393 or 0 if the value should be sign-extended. */
6395 tree
6396 get_narrower (tree op, int *unsignedp_ptr)
6398 int uns = 0;
6399 int first = 1;
6400 tree win = op;
6401 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
6403 while (TREE_CODE (op) == NOP_EXPR)
6405 int bitschange
6406 = (TYPE_PRECISION (TREE_TYPE (op))
6407 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
6409 /* Truncations are many-one so cannot be removed. */
6410 if (bitschange < 0)
6411 break;
6413 /* See what's inside this conversion. If we decide to strip it,
6414 we will set WIN. */
6416 if (bitschange > 0)
6418 op = TREE_OPERAND (op, 0);
6419 /* An extension: the outermost one can be stripped,
6420 but remember whether it is zero or sign extension. */
6421 if (first)
6422 uns = TYPE_UNSIGNED (TREE_TYPE (op));
6423 /* Otherwise, if a sign extension has been stripped,
6424 only sign extensions can now be stripped;
6425 if a zero extension has been stripped, only zero-extensions. */
6426 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
6427 break;
6428 first = 0;
6430 else /* bitschange == 0 */
6432 /* A change in nominal type can always be stripped, but we must
6433 preserve the unsignedness. */
6434 if (first)
6435 uns = TYPE_UNSIGNED (TREE_TYPE (op));
6436 first = 0;
6437 op = TREE_OPERAND (op, 0);
6438 /* Keep trying to narrow, but don't assign op to win if it
6439 would turn an integral type into something else. */
6440 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
6441 continue;
6444 win = op;
6447 if (TREE_CODE (op) == COMPONENT_REF
6448 /* Since type_for_size always gives an integer type. */
6449 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
6450 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
6451 /* Ensure field is laid out already. */
6452 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
6453 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
6455 unsigned HOST_WIDE_INT innerprec
6456 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
6457 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
6458 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
6459 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
6461 /* We can get this structure field in a narrower type that fits it,
6462 but the resulting extension to its nominal type (a fullword type)
6463 must satisfy the same conditions as for other extensions.
6465 Do this only for fields that are aligned (not bit-fields),
6466 because when bit-field insns will be used there is no
6467 advantage in doing this. */
6469 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
6470 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
6471 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
6472 && type != 0)
6474 if (first)
6475 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
6476 win = fold_convert (type, op);
6480 *unsignedp_ptr = uns;
6481 return win;
6484 /* Nonzero if integer constant C has a value that is permissible
6485 for type TYPE (an INTEGER_TYPE). */
6488 int_fits_type_p (const_tree c, const_tree type)
6490 tree type_low_bound, type_high_bound;
6491 bool ok_for_low_bound, ok_for_high_bound, unsc;
6492 double_int dc, dd;
6494 dc = tree_to_double_int (c);
6495 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
6497 if (TREE_CODE (TREE_TYPE (c)) == INTEGER_TYPE
6498 && TYPE_IS_SIZETYPE (TREE_TYPE (c))
6499 && unsc)
6500 /* So c is an unsigned integer whose type is sizetype and type is not.
6501 sizetype'd integers are sign extended even though they are
6502 unsigned. If the integer value fits in the lower end word of c,
6503 and if the higher end word has all its bits set to 1, that
6504 means the higher end bits are set to 1 only for sign extension.
6505 So let's convert c into an equivalent zero extended unsigned
6506 integer. */
6507 dc = double_int_zext (dc, TYPE_PRECISION (TREE_TYPE (c)));
6509 retry:
6510 type_low_bound = TYPE_MIN_VALUE (type);
6511 type_high_bound = TYPE_MAX_VALUE (type);
6513 /* If at least one bound of the type is a constant integer, we can check
6514 ourselves and maybe make a decision. If no such decision is possible, but
6515 this type is a subtype, try checking against that. Otherwise, use
6516 fit_double_type, which checks against the precision.
6518 Compute the status for each possibly constant bound, and return if we see
6519 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
6520 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
6521 for "constant known to fit". */
6523 /* Check if c >= type_low_bound. */
6524 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
6526 dd = tree_to_double_int (type_low_bound);
6527 if (TREE_CODE (type) == INTEGER_TYPE
6528 && TYPE_IS_SIZETYPE (type)
6529 && TYPE_UNSIGNED (type))
6530 dd = double_int_zext (dd, TYPE_PRECISION (type));
6531 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
6533 int c_neg = (!unsc && double_int_negative_p (dc));
6534 int t_neg = (unsc && double_int_negative_p (dd));
6536 if (c_neg && !t_neg)
6537 return 0;
6538 if ((c_neg || !t_neg) && double_int_ucmp (dc, dd) < 0)
6539 return 0;
6541 else if (double_int_cmp (dc, dd, unsc) < 0)
6542 return 0;
6543 ok_for_low_bound = true;
6545 else
6546 ok_for_low_bound = false;
6548 /* Check if c <= type_high_bound. */
6549 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
6551 dd = tree_to_double_int (type_high_bound);
6552 if (TREE_CODE (type) == INTEGER_TYPE
6553 && TYPE_IS_SIZETYPE (type)
6554 && TYPE_UNSIGNED (type))
6555 dd = double_int_zext (dd, TYPE_PRECISION (type));
6556 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
6558 int c_neg = (!unsc && double_int_negative_p (dc));
6559 int t_neg = (unsc && double_int_negative_p (dd));
6561 if (t_neg && !c_neg)
6562 return 0;
6563 if ((t_neg || !c_neg) && double_int_ucmp (dc, dd) > 0)
6564 return 0;
6566 else if (double_int_cmp (dc, dd, unsc) > 0)
6567 return 0;
6568 ok_for_high_bound = true;
6570 else
6571 ok_for_high_bound = false;
6573 /* If the constant fits both bounds, the result is known. */
6574 if (ok_for_low_bound && ok_for_high_bound)
6575 return 1;
6577 /* Perform some generic filtering which may allow making a decision
6578 even if the bounds are not constant. First, negative integers
6579 never fit in unsigned types, */
6580 if (TYPE_UNSIGNED (type) && !unsc && double_int_negative_p (dc))
6581 return 0;
6583 /* Second, narrower types always fit in wider ones. */
6584 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
6585 return 1;
6587 /* Third, unsigned integers with top bit set never fit signed types. */
6588 if (! TYPE_UNSIGNED (type) && unsc)
6590 int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
6591 if (prec < HOST_BITS_PER_WIDE_INT)
6593 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
6594 return 0;
6596 else if (((((unsigned HOST_WIDE_INT) 1)
6597 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
6598 return 0;
6601 /* If we haven't been able to decide at this point, there nothing more we
6602 can check ourselves here. Look at the base type if we have one and it
6603 has the same precision. */
6604 if (TREE_CODE (type) == INTEGER_TYPE
6605 && TREE_TYPE (type) != 0
6606 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
6608 type = TREE_TYPE (type);
6609 goto retry;
6612 /* Or to fit_double_type, if nothing else. */
6613 return !fit_double_type (dc.low, dc.high, &dc.low, &dc.high, type);
6616 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
6617 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
6618 represented (assuming two's-complement arithmetic) within the bit
6619 precision of the type are returned instead. */
6621 void
6622 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
6624 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
6625 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
6626 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
6627 TYPE_UNSIGNED (type));
6628 else
6630 if (TYPE_UNSIGNED (type))
6631 mpz_set_ui (min, 0);
6632 else
6634 double_int mn;
6635 mn = double_int_mask (TYPE_PRECISION (type) - 1);
6636 mn = double_int_sext (double_int_add (mn, double_int_one),
6637 TYPE_PRECISION (type));
6638 mpz_set_double_int (min, mn, false);
6642 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
6643 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
6644 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
6645 TYPE_UNSIGNED (type));
6646 else
6648 if (TYPE_UNSIGNED (type))
6649 mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type)),
6650 true);
6651 else
6652 mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type) - 1),
6653 true);
6657 /* Return true if VAR is an automatic variable defined in function FN. */
6659 bool
6660 auto_var_in_fn_p (const_tree var, const_tree fn)
6662 return (DECL_P (var) && DECL_CONTEXT (var) == fn
6663 && (((TREE_CODE (var) == VAR_DECL || TREE_CODE (var) == PARM_DECL)
6664 && ! TREE_STATIC (var))
6665 || TREE_CODE (var) == LABEL_DECL
6666 || TREE_CODE (var) == RESULT_DECL));
6669 /* Subprogram of following function. Called by walk_tree.
6671 Return *TP if it is an automatic variable or parameter of the
6672 function passed in as DATA. */
6674 static tree
6675 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
6677 tree fn = (tree) data;
6679 if (TYPE_P (*tp))
6680 *walk_subtrees = 0;
6682 else if (DECL_P (*tp)
6683 && auto_var_in_fn_p (*tp, fn))
6684 return *tp;
6686 return NULL_TREE;
6689 /* Returns true if T is, contains, or refers to a type with variable
6690 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
6691 arguments, but not the return type. If FN is nonzero, only return
6692 true if a modifier of the type or position of FN is a variable or
6693 parameter inside FN.
6695 This concept is more general than that of C99 'variably modified types':
6696 in C99, a struct type is never variably modified because a VLA may not
6697 appear as a structure member. However, in GNU C code like:
6699 struct S { int i[f()]; };
6701 is valid, and other languages may define similar constructs. */
6703 bool
6704 variably_modified_type_p (tree type, tree fn)
6706 tree t;
6708 /* Test if T is either variable (if FN is zero) or an expression containing
6709 a variable in FN. */
6710 #define RETURN_TRUE_IF_VAR(T) \
6711 do { tree _t = (T); \
6712 if (_t && _t != error_mark_node && TREE_CODE (_t) != INTEGER_CST \
6713 && (!fn || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
6714 return true; } while (0)
6716 if (type == error_mark_node)
6717 return false;
6719 /* If TYPE itself has variable size, it is variably modified. */
6720 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
6721 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
6723 switch (TREE_CODE (type))
6725 case POINTER_TYPE:
6726 case REFERENCE_TYPE:
6727 case VECTOR_TYPE:
6728 if (variably_modified_type_p (TREE_TYPE (type), fn))
6729 return true;
6730 break;
6732 case FUNCTION_TYPE:
6733 case METHOD_TYPE:
6734 /* If TYPE is a function type, it is variably modified if the
6735 return type is variably modified. */
6736 if (variably_modified_type_p (TREE_TYPE (type), fn))
6737 return true;
6738 break;
6740 case INTEGER_TYPE:
6741 case REAL_TYPE:
6742 case FIXED_POINT_TYPE:
6743 case ENUMERAL_TYPE:
6744 case BOOLEAN_TYPE:
6745 /* Scalar types are variably modified if their end points
6746 aren't constant. */
6747 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
6748 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
6749 break;
6751 case RECORD_TYPE:
6752 case UNION_TYPE:
6753 case QUAL_UNION_TYPE:
6754 /* We can't see if any of the fields are variably-modified by the
6755 definition we normally use, since that would produce infinite
6756 recursion via pointers. */
6757 /* This is variably modified if some field's type is. */
6758 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
6759 if (TREE_CODE (t) == FIELD_DECL)
6761 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
6762 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
6763 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
6765 if (TREE_CODE (type) == QUAL_UNION_TYPE)
6766 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
6768 break;
6770 case ARRAY_TYPE:
6771 /* Do not call ourselves to avoid infinite recursion. This is
6772 variably modified if the element type is. */
6773 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
6774 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
6775 break;
6777 default:
6778 break;
6781 /* The current language may have other cases to check, but in general,
6782 all other types are not variably modified. */
6783 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
6785 #undef RETURN_TRUE_IF_VAR
6788 /* Given a DECL or TYPE, return the scope in which it was declared, or
6789 NULL_TREE if there is no containing scope. */
6791 tree
6792 get_containing_scope (const_tree t)
6794 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
6797 /* Return the innermost context enclosing DECL that is
6798 a FUNCTION_DECL, or zero if none. */
6800 tree
6801 decl_function_context (const_tree decl)
6803 tree context;
6805 if (TREE_CODE (decl) == ERROR_MARK)
6806 return 0;
6808 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
6809 where we look up the function at runtime. Such functions always take
6810 a first argument of type 'pointer to real context'.
6812 C++ should really be fixed to use DECL_CONTEXT for the real context,
6813 and use something else for the "virtual context". */
6814 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
6815 context
6816 = TYPE_MAIN_VARIANT
6817 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6818 else
6819 context = DECL_CONTEXT (decl);
6821 while (context && TREE_CODE (context) != FUNCTION_DECL)
6823 if (TREE_CODE (context) == BLOCK)
6824 context = BLOCK_SUPERCONTEXT (context);
6825 else
6826 context = get_containing_scope (context);
6829 return context;
6832 /* Return the innermost context enclosing DECL that is
6833 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
6834 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
6836 tree
6837 decl_type_context (const_tree decl)
6839 tree context = DECL_CONTEXT (decl);
6841 while (context)
6842 switch (TREE_CODE (context))
6844 case NAMESPACE_DECL:
6845 case TRANSLATION_UNIT_DECL:
6846 return NULL_TREE;
6848 case RECORD_TYPE:
6849 case UNION_TYPE:
6850 case QUAL_UNION_TYPE:
6851 return context;
6853 case TYPE_DECL:
6854 case FUNCTION_DECL:
6855 context = DECL_CONTEXT (context);
6856 break;
6858 case BLOCK:
6859 context = BLOCK_SUPERCONTEXT (context);
6860 break;
6862 default:
6863 gcc_unreachable ();
6866 return NULL_TREE;
6869 /* CALL is a CALL_EXPR. Return the declaration for the function
6870 called, or NULL_TREE if the called function cannot be
6871 determined. */
6873 tree
6874 get_callee_fndecl (const_tree call)
6876 tree addr;
6878 if (call == error_mark_node)
6879 return error_mark_node;
6881 /* It's invalid to call this function with anything but a
6882 CALL_EXPR. */
6883 gcc_assert (TREE_CODE (call) == CALL_EXPR);
6885 /* The first operand to the CALL is the address of the function
6886 called. */
6887 addr = CALL_EXPR_FN (call);
6889 STRIP_NOPS (addr);
6891 /* If this is a readonly function pointer, extract its initial value. */
6892 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
6893 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
6894 && DECL_INITIAL (addr))
6895 addr = DECL_INITIAL (addr);
6897 /* If the address is just `&f' for some function `f', then we know
6898 that `f' is being called. */
6899 if (TREE_CODE (addr) == ADDR_EXPR
6900 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
6901 return TREE_OPERAND (addr, 0);
6903 /* We couldn't figure out what was being called. */
6904 return NULL_TREE;
6907 /* Print debugging information about tree nodes generated during the compile,
6908 and any language-specific information. */
6910 void
6911 dump_tree_statistics (void)
6913 #ifdef GATHER_STATISTICS
6914 int i;
6915 int total_nodes, total_bytes;
6916 #endif
6918 fprintf (stderr, "\n??? tree nodes created\n\n");
6919 #ifdef GATHER_STATISTICS
6920 fprintf (stderr, "Kind Nodes Bytes\n");
6921 fprintf (stderr, "---------------------------------------\n");
6922 total_nodes = total_bytes = 0;
6923 for (i = 0; i < (int) all_kinds; i++)
6925 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
6926 tree_node_counts[i], tree_node_sizes[i]);
6927 total_nodes += tree_node_counts[i];
6928 total_bytes += tree_node_sizes[i];
6930 fprintf (stderr, "---------------------------------------\n");
6931 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
6932 fprintf (stderr, "---------------------------------------\n");
6933 ssanames_print_statistics ();
6934 phinodes_print_statistics ();
6935 #else
6936 fprintf (stderr, "(No per-node statistics)\n");
6937 #endif
6938 print_type_hash_statistics ();
6939 print_debug_expr_statistics ();
6940 print_value_expr_statistics ();
6941 print_restrict_base_statistics ();
6942 lang_hooks.print_statistics ();
6945 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
6947 /* Generate a crc32 of a string. */
6949 unsigned
6950 crc32_string (unsigned chksum, const char *string)
6954 unsigned value = *string << 24;
6955 unsigned ix;
6957 for (ix = 8; ix--; value <<= 1)
6959 unsigned feedback;
6961 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
6962 chksum <<= 1;
6963 chksum ^= feedback;
6966 while (*string++);
6967 return chksum;
6970 /* P is a string that will be used in a symbol. Mask out any characters
6971 that are not valid in that context. */
6973 void
6974 clean_symbol_name (char *p)
6976 for (; *p; p++)
6977 if (! (ISALNUM (*p)
6978 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
6979 || *p == '$'
6980 #endif
6981 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
6982 || *p == '.'
6983 #endif
6985 *p = '_';
6988 /* Generate a name for a special-purpose function function.
6989 The generated name may need to be unique across the whole link.
6990 TYPE is some string to identify the purpose of this function to the
6991 linker or collect2; it must start with an uppercase letter,
6992 one of:
6993 I - for constructors
6994 D - for destructors
6995 N - for C++ anonymous namespaces
6996 F - for DWARF unwind frame information. */
6998 tree
6999 get_file_function_name (const char *type)
7001 char *buf;
7002 const char *p;
7003 char *q;
7005 /* If we already have a name we know to be unique, just use that. */
7006 if (first_global_object_name)
7007 p = q = ASTRDUP (first_global_object_name);
7008 /* If the target is handling the constructors/destructors, they
7009 will be local to this file and the name is only necessary for
7010 debugging purposes. */
7011 else if ((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
7013 const char *file = main_input_filename;
7014 if (! file)
7015 file = input_filename;
7016 /* Just use the file's basename, because the full pathname
7017 might be quite long. */
7018 p = strrchr (file, '/');
7019 if (p)
7020 p++;
7021 else
7022 p = file;
7023 p = q = ASTRDUP (p);
7025 else
7027 /* Otherwise, the name must be unique across the entire link.
7028 We don't have anything that we know to be unique to this translation
7029 unit, so use what we do have and throw in some randomness. */
7030 unsigned len;
7031 const char *name = weak_global_object_name;
7032 const char *file = main_input_filename;
7034 if (! name)
7035 name = "";
7036 if (! file)
7037 file = input_filename;
7039 len = strlen (file);
7040 q = (char *) alloca (9 * 2 + len + 1);
7041 memcpy (q, file, len + 1);
7043 sprintf (q + len, "_%08X_%08X", crc32_string (0, name),
7044 crc32_string (0, get_random_seed (false)));
7046 p = q;
7049 clean_symbol_name (q);
7050 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
7051 + strlen (type));
7053 /* Set up the name of the file-level functions we may need.
7054 Use a global object (which is already required to be unique over
7055 the program) rather than the file name (which imposes extra
7056 constraints). */
7057 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
7059 return get_identifier (buf);
7062 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
7064 /* Complain that the tree code of NODE does not match the expected 0
7065 terminated list of trailing codes. The trailing code list can be
7066 empty, for a more vague error message. FILE, LINE, and FUNCTION
7067 are of the caller. */
7069 void
7070 tree_check_failed (const_tree node, const char *file,
7071 int line, const char *function, ...)
7073 va_list args;
7074 const char *buffer;
7075 unsigned length = 0;
7076 int code;
7078 va_start (args, function);
7079 while ((code = va_arg (args, int)))
7080 length += 4 + strlen (tree_code_name[code]);
7081 va_end (args);
7082 if (length)
7084 char *tmp;
7085 va_start (args, function);
7086 length += strlen ("expected ");
7087 buffer = tmp = (char *) alloca (length);
7088 length = 0;
7089 while ((code = va_arg (args, int)))
7091 const char *prefix = length ? " or " : "expected ";
7093 strcpy (tmp + length, prefix);
7094 length += strlen (prefix);
7095 strcpy (tmp + length, tree_code_name[code]);
7096 length += strlen (tree_code_name[code]);
7098 va_end (args);
7100 else
7101 buffer = "unexpected node";
7103 internal_error ("tree check: %s, have %s in %s, at %s:%d",
7104 buffer, tree_code_name[TREE_CODE (node)],
7105 function, trim_filename (file), line);
7108 /* Complain that the tree code of NODE does match the expected 0
7109 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
7110 the caller. */
7112 void
7113 tree_not_check_failed (const_tree node, const char *file,
7114 int line, const char *function, ...)
7116 va_list args;
7117 char *buffer;
7118 unsigned length = 0;
7119 int code;
7121 va_start (args, function);
7122 while ((code = va_arg (args, int)))
7123 length += 4 + strlen (tree_code_name[code]);
7124 va_end (args);
7125 va_start (args, function);
7126 buffer = (char *) alloca (length);
7127 length = 0;
7128 while ((code = va_arg (args, int)))
7130 if (length)
7132 strcpy (buffer + length, " or ");
7133 length += 4;
7135 strcpy (buffer + length, tree_code_name[code]);
7136 length += strlen (tree_code_name[code]);
7138 va_end (args);
7140 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
7141 buffer, tree_code_name[TREE_CODE (node)],
7142 function, trim_filename (file), line);
7145 /* Similar to tree_check_failed, except that we check for a class of tree
7146 code, given in CL. */
7148 void
7149 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
7150 const char *file, int line, const char *function)
7152 internal_error
7153 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
7154 TREE_CODE_CLASS_STRING (cl),
7155 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
7156 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
7159 /* Similar to tree_check_failed, except that instead of specifying a
7160 dozen codes, use the knowledge that they're all sequential. */
7162 void
7163 tree_range_check_failed (const_tree node, const char *file, int line,
7164 const char *function, enum tree_code c1,
7165 enum tree_code c2)
7167 char *buffer;
7168 unsigned length = 0;
7169 enum tree_code c;
7171 for (c = c1; c <= c2; ++c)
7172 length += 4 + strlen (tree_code_name[c]);
7174 length += strlen ("expected ");
7175 buffer = (char *) alloca (length);
7176 length = 0;
7178 for (c = c1; c <= c2; ++c)
7180 const char *prefix = length ? " or " : "expected ";
7182 strcpy (buffer + length, prefix);
7183 length += strlen (prefix);
7184 strcpy (buffer + length, tree_code_name[c]);
7185 length += strlen (tree_code_name[c]);
7188 internal_error ("tree check: %s, have %s in %s, at %s:%d",
7189 buffer, tree_code_name[TREE_CODE (node)],
7190 function, trim_filename (file), line);
7194 /* Similar to tree_check_failed, except that we check that a tree does
7195 not have the specified code, given in CL. */
7197 void
7198 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
7199 const char *file, int line, const char *function)
7201 internal_error
7202 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
7203 TREE_CODE_CLASS_STRING (cl),
7204 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
7205 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
7209 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
7211 void
7212 omp_clause_check_failed (const_tree node, const char *file, int line,
7213 const char *function, enum omp_clause_code code)
7215 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
7216 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
7217 function, trim_filename (file), line);
7221 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
7223 void
7224 omp_clause_range_check_failed (const_tree node, const char *file, int line,
7225 const char *function, enum omp_clause_code c1,
7226 enum omp_clause_code c2)
7228 char *buffer;
7229 unsigned length = 0;
7230 enum omp_clause_code c;
7232 for (c = c1; c <= c2; ++c)
7233 length += 4 + strlen (omp_clause_code_name[c]);
7235 length += strlen ("expected ");
7236 buffer = (char *) alloca (length);
7237 length = 0;
7239 for (c = c1; c <= c2; ++c)
7241 const char *prefix = length ? " or " : "expected ";
7243 strcpy (buffer + length, prefix);
7244 length += strlen (prefix);
7245 strcpy (buffer + length, omp_clause_code_name[c]);
7246 length += strlen (omp_clause_code_name[c]);
7249 internal_error ("tree check: %s, have %s in %s, at %s:%d",
7250 buffer, omp_clause_code_name[TREE_CODE (node)],
7251 function, trim_filename (file), line);
7255 #undef DEFTREESTRUCT
7256 #define DEFTREESTRUCT(VAL, NAME) NAME,
7258 static const char *ts_enum_names[] = {
7259 #include "treestruct.def"
7261 #undef DEFTREESTRUCT
7263 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
7265 /* Similar to tree_class_check_failed, except that we check for
7266 whether CODE contains the tree structure identified by EN. */
7268 void
7269 tree_contains_struct_check_failed (const_tree node,
7270 const enum tree_node_structure_enum en,
7271 const char *file, int line,
7272 const char *function)
7274 internal_error
7275 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
7276 TS_ENUM_NAME(en),
7277 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
7281 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
7282 (dynamically sized) vector. */
7284 void
7285 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
7286 const char *function)
7288 internal_error
7289 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
7290 idx + 1, len, function, trim_filename (file), line);
7293 /* Similar to above, except that the check is for the bounds of the operand
7294 vector of an expression node EXP. */
7296 void
7297 tree_operand_check_failed (int idx, const_tree exp, const char *file,
7298 int line, const char *function)
7300 int code = TREE_CODE (exp);
7301 internal_error
7302 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
7303 idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
7304 function, trim_filename (file), line);
7307 /* Similar to above, except that the check is for the number of
7308 operands of an OMP_CLAUSE node. */
7310 void
7311 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
7312 int line, const char *function)
7314 internal_error
7315 ("tree check: accessed operand %d of omp_clause %s with %d operands "
7316 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
7317 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
7318 trim_filename (file), line);
7320 #endif /* ENABLE_TREE_CHECKING */
7322 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
7323 and mapped to the machine mode MODE. Initialize its fields and build
7324 the information necessary for debugging output. */
7326 static tree
7327 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
7329 tree t;
7330 hashval_t hashcode = 0;
7332 /* Build a main variant, based on the main variant of the inner type, then
7333 use it to build the variant we return. */
7334 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
7335 && TYPE_MAIN_VARIANT (innertype) != innertype)
7336 return build_type_attribute_qual_variant (
7337 make_vector_type (TYPE_MAIN_VARIANT (innertype), nunits, mode),
7338 TYPE_ATTRIBUTES (innertype),
7339 TYPE_QUALS (innertype));
7341 t = make_node (VECTOR_TYPE);
7342 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
7343 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
7344 SET_TYPE_MODE (t, mode);
7345 TYPE_READONLY (t) = TYPE_READONLY (innertype);
7346 TYPE_VOLATILE (t) = TYPE_VOLATILE (innertype);
7348 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
7349 SET_TYPE_STRUCTURAL_EQUALITY (t);
7350 else if (TYPE_CANONICAL (innertype) != innertype
7351 || mode != VOIDmode)
7352 TYPE_CANONICAL (t)
7353 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
7355 layout_type (t);
7358 tree index = build_int_cst (NULL_TREE, nunits - 1);
7359 tree array = build_array_type (innertype, build_index_type (index));
7360 tree rt = make_node (RECORD_TYPE);
7362 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
7363 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
7364 layout_type (rt);
7365 TYPE_DEBUG_REPRESENTATION_TYPE (t) = rt;
7366 /* In dwarfout.c, type lookup uses TYPE_UID numbers. We want to output
7367 the representation type, and we want to find that die when looking up
7368 the vector type. This is most easily achieved by making the TYPE_UID
7369 numbers equal. */
7370 TYPE_UID (rt) = TYPE_UID (t);
7373 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
7374 hashcode = iterative_hash_host_wide_int (mode, hashcode);
7375 hashcode = iterative_hash_object (TYPE_HASH (innertype), hashcode);
7376 return type_hash_canon (hashcode, t);
7379 static tree
7380 make_or_reuse_type (unsigned size, int unsignedp)
7382 if (size == INT_TYPE_SIZE)
7383 return unsignedp ? unsigned_type_node : integer_type_node;
7384 if (size == CHAR_TYPE_SIZE)
7385 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
7386 if (size == SHORT_TYPE_SIZE)
7387 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
7388 if (size == LONG_TYPE_SIZE)
7389 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
7390 if (size == LONG_LONG_TYPE_SIZE)
7391 return (unsignedp ? long_long_unsigned_type_node
7392 : long_long_integer_type_node);
7394 if (unsignedp)
7395 return make_unsigned_type (size);
7396 else
7397 return make_signed_type (size);
7400 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
7402 static tree
7403 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
7405 if (satp)
7407 if (size == SHORT_FRACT_TYPE_SIZE)
7408 return unsignedp ? sat_unsigned_short_fract_type_node
7409 : sat_short_fract_type_node;
7410 if (size == FRACT_TYPE_SIZE)
7411 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
7412 if (size == LONG_FRACT_TYPE_SIZE)
7413 return unsignedp ? sat_unsigned_long_fract_type_node
7414 : sat_long_fract_type_node;
7415 if (size == LONG_LONG_FRACT_TYPE_SIZE)
7416 return unsignedp ? sat_unsigned_long_long_fract_type_node
7417 : sat_long_long_fract_type_node;
7419 else
7421 if (size == SHORT_FRACT_TYPE_SIZE)
7422 return unsignedp ? unsigned_short_fract_type_node
7423 : short_fract_type_node;
7424 if (size == FRACT_TYPE_SIZE)
7425 return unsignedp ? unsigned_fract_type_node : fract_type_node;
7426 if (size == LONG_FRACT_TYPE_SIZE)
7427 return unsignedp ? unsigned_long_fract_type_node
7428 : long_fract_type_node;
7429 if (size == LONG_LONG_FRACT_TYPE_SIZE)
7430 return unsignedp ? unsigned_long_long_fract_type_node
7431 : long_long_fract_type_node;
7434 return make_fract_type (size, unsignedp, satp);
7437 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
7439 static tree
7440 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
7442 if (satp)
7444 if (size == SHORT_ACCUM_TYPE_SIZE)
7445 return unsignedp ? sat_unsigned_short_accum_type_node
7446 : sat_short_accum_type_node;
7447 if (size == ACCUM_TYPE_SIZE)
7448 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
7449 if (size == LONG_ACCUM_TYPE_SIZE)
7450 return unsignedp ? sat_unsigned_long_accum_type_node
7451 : sat_long_accum_type_node;
7452 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
7453 return unsignedp ? sat_unsigned_long_long_accum_type_node
7454 : sat_long_long_accum_type_node;
7456 else
7458 if (size == SHORT_ACCUM_TYPE_SIZE)
7459 return unsignedp ? unsigned_short_accum_type_node
7460 : short_accum_type_node;
7461 if (size == ACCUM_TYPE_SIZE)
7462 return unsignedp ? unsigned_accum_type_node : accum_type_node;
7463 if (size == LONG_ACCUM_TYPE_SIZE)
7464 return unsignedp ? unsigned_long_accum_type_node
7465 : long_accum_type_node;
7466 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
7467 return unsignedp ? unsigned_long_long_accum_type_node
7468 : long_long_accum_type_node;
7471 return make_accum_type (size, unsignedp, satp);
7474 /* Create nodes for all integer types (and error_mark_node) using the sizes
7475 of C datatypes. The caller should call set_sizetype soon after calling
7476 this function to select one of the types as sizetype. */
7478 void
7479 build_common_tree_nodes (bool signed_char, bool signed_sizetype)
7481 error_mark_node = make_node (ERROR_MARK);
7482 TREE_TYPE (error_mark_node) = error_mark_node;
7484 initialize_sizetypes (signed_sizetype);
7486 /* Define both `signed char' and `unsigned char'. */
7487 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
7488 TYPE_STRING_FLAG (signed_char_type_node) = 1;
7489 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
7490 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
7492 /* Define `char', which is like either `signed char' or `unsigned char'
7493 but not the same as either. */
7494 char_type_node
7495 = (signed_char
7496 ? make_signed_type (CHAR_TYPE_SIZE)
7497 : make_unsigned_type (CHAR_TYPE_SIZE));
7498 TYPE_STRING_FLAG (char_type_node) = 1;
7500 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
7501 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
7502 integer_type_node = make_signed_type (INT_TYPE_SIZE);
7503 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
7504 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
7505 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
7506 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
7507 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
7509 /* Define a boolean type. This type only represents boolean values but
7510 may be larger than char depending on the value of BOOL_TYPE_SIZE.
7511 Front ends which want to override this size (i.e. Java) can redefine
7512 boolean_type_node before calling build_common_tree_nodes_2. */
7513 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
7514 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
7515 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
7516 TYPE_PRECISION (boolean_type_node) = 1;
7518 /* Fill in the rest of the sized types. Reuse existing type nodes
7519 when possible. */
7520 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
7521 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
7522 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
7523 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
7524 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
7526 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
7527 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
7528 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
7529 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
7530 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
7532 access_public_node = get_identifier ("public");
7533 access_protected_node = get_identifier ("protected");
7534 access_private_node = get_identifier ("private");
7537 /* Call this function after calling build_common_tree_nodes and set_sizetype.
7538 It will create several other common tree nodes. */
7540 void
7541 build_common_tree_nodes_2 (int short_double)
7543 /* Define these next since types below may used them. */
7544 integer_zero_node = build_int_cst (NULL_TREE, 0);
7545 integer_one_node = build_int_cst (NULL_TREE, 1);
7546 integer_minus_one_node = build_int_cst (NULL_TREE, -1);
7548 size_zero_node = size_int (0);
7549 size_one_node = size_int (1);
7550 bitsize_zero_node = bitsize_int (0);
7551 bitsize_one_node = bitsize_int (1);
7552 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
7554 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
7555 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
7557 void_type_node = make_node (VOID_TYPE);
7558 layout_type (void_type_node);
7560 /* We are not going to have real types in C with less than byte alignment,
7561 so we might as well not have any types that claim to have it. */
7562 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
7563 TYPE_USER_ALIGN (void_type_node) = 0;
7565 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
7566 layout_type (TREE_TYPE (null_pointer_node));
7568 ptr_type_node = build_pointer_type (void_type_node);
7569 const_ptr_type_node
7570 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
7571 fileptr_type_node = ptr_type_node;
7573 float_type_node = make_node (REAL_TYPE);
7574 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
7575 layout_type (float_type_node);
7577 double_type_node = make_node (REAL_TYPE);
7578 if (short_double)
7579 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
7580 else
7581 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
7582 layout_type (double_type_node);
7584 long_double_type_node = make_node (REAL_TYPE);
7585 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
7586 layout_type (long_double_type_node);
7588 float_ptr_type_node = build_pointer_type (float_type_node);
7589 double_ptr_type_node = build_pointer_type (double_type_node);
7590 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
7591 integer_ptr_type_node = build_pointer_type (integer_type_node);
7593 /* Fixed size integer types. */
7594 uint32_type_node = build_nonstandard_integer_type (32, true);
7595 uint64_type_node = build_nonstandard_integer_type (64, true);
7597 /* Decimal float types. */
7598 dfloat32_type_node = make_node (REAL_TYPE);
7599 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
7600 layout_type (dfloat32_type_node);
7601 SET_TYPE_MODE (dfloat32_type_node, SDmode);
7602 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
7604 dfloat64_type_node = make_node (REAL_TYPE);
7605 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
7606 layout_type (dfloat64_type_node);
7607 SET_TYPE_MODE (dfloat64_type_node, DDmode);
7608 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
7610 dfloat128_type_node = make_node (REAL_TYPE);
7611 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
7612 layout_type (dfloat128_type_node);
7613 SET_TYPE_MODE (dfloat128_type_node, TDmode);
7614 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
7616 complex_integer_type_node = build_complex_type (integer_type_node);
7617 complex_float_type_node = build_complex_type (float_type_node);
7618 complex_double_type_node = build_complex_type (double_type_node);
7619 complex_long_double_type_node = build_complex_type (long_double_type_node);
7621 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
7622 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
7623 sat_ ## KIND ## _type_node = \
7624 make_sat_signed_ ## KIND ## _type (SIZE); \
7625 sat_unsigned_ ## KIND ## _type_node = \
7626 make_sat_unsigned_ ## KIND ## _type (SIZE); \
7627 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
7628 unsigned_ ## KIND ## _type_node = \
7629 make_unsigned_ ## KIND ## _type (SIZE);
7631 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
7632 sat_ ## WIDTH ## KIND ## _type_node = \
7633 make_sat_signed_ ## KIND ## _type (SIZE); \
7634 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
7635 make_sat_unsigned_ ## KIND ## _type (SIZE); \
7636 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
7637 unsigned_ ## WIDTH ## KIND ## _type_node = \
7638 make_unsigned_ ## KIND ## _type (SIZE);
7640 /* Make fixed-point type nodes based on four different widths. */
7641 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
7642 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
7643 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
7644 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
7645 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
7647 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
7648 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
7649 NAME ## _type_node = \
7650 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
7651 u ## NAME ## _type_node = \
7652 make_or_reuse_unsigned_ ## KIND ## _type \
7653 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
7654 sat_ ## NAME ## _type_node = \
7655 make_or_reuse_sat_signed_ ## KIND ## _type \
7656 (GET_MODE_BITSIZE (MODE ## mode)); \
7657 sat_u ## NAME ## _type_node = \
7658 make_or_reuse_sat_unsigned_ ## KIND ## _type \
7659 (GET_MODE_BITSIZE (U ## MODE ## mode));
7661 /* Fixed-point type and mode nodes. */
7662 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
7663 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
7664 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
7665 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
7666 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
7667 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
7668 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
7669 MAKE_FIXED_MODE_NODE (accum, ha, HA)
7670 MAKE_FIXED_MODE_NODE (accum, sa, SA)
7671 MAKE_FIXED_MODE_NODE (accum, da, DA)
7672 MAKE_FIXED_MODE_NODE (accum, ta, TA)
7675 tree t = targetm.build_builtin_va_list ();
7677 /* Many back-ends define record types without setting TYPE_NAME.
7678 If we copied the record type here, we'd keep the original
7679 record type without a name. This breaks name mangling. So,
7680 don't copy record types and let c_common_nodes_and_builtins()
7681 declare the type to be __builtin_va_list. */
7682 if (TREE_CODE (t) != RECORD_TYPE)
7683 t = build_variant_type_copy (t);
7685 va_list_type_node = t;
7689 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
7691 static void
7692 local_define_builtin (const char *name, tree type, enum built_in_function code,
7693 const char *library_name, int ecf_flags)
7695 tree decl;
7697 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
7698 library_name, NULL_TREE);
7699 if (ecf_flags & ECF_CONST)
7700 TREE_READONLY (decl) = 1;
7701 if (ecf_flags & ECF_PURE)
7702 DECL_PURE_P (decl) = 1;
7703 if (ecf_flags & ECF_LOOPING_CONST_OR_PURE)
7704 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
7705 if (ecf_flags & ECF_NORETURN)
7706 TREE_THIS_VOLATILE (decl) = 1;
7707 if (ecf_flags & ECF_NOTHROW)
7708 TREE_NOTHROW (decl) = 1;
7709 if (ecf_flags & ECF_MALLOC)
7710 DECL_IS_MALLOC (decl) = 1;
7712 built_in_decls[code] = decl;
7713 implicit_built_in_decls[code] = decl;
7716 /* Call this function after instantiating all builtins that the language
7717 front end cares about. This will build the rest of the builtins that
7718 are relied upon by the tree optimizers and the middle-end. */
7720 void
7721 build_common_builtin_nodes (void)
7723 tree tmp, ftype;
7725 if (built_in_decls[BUILT_IN_MEMCPY] == NULL
7726 || built_in_decls[BUILT_IN_MEMMOVE] == NULL)
7728 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7729 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
7730 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7731 ftype = build_function_type (ptr_type_node, tmp);
7733 if (built_in_decls[BUILT_IN_MEMCPY] == NULL)
7734 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
7735 "memcpy", ECF_NOTHROW);
7736 if (built_in_decls[BUILT_IN_MEMMOVE] == NULL)
7737 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
7738 "memmove", ECF_NOTHROW);
7741 if (built_in_decls[BUILT_IN_MEMCMP] == NULL)
7743 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7744 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
7745 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
7746 ftype = build_function_type (integer_type_node, tmp);
7747 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
7748 "memcmp", ECF_PURE | ECF_NOTHROW);
7751 if (built_in_decls[BUILT_IN_MEMSET] == NULL)
7753 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7754 tmp = tree_cons (NULL_TREE, integer_type_node, tmp);
7755 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7756 ftype = build_function_type (ptr_type_node, tmp);
7757 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
7758 "memset", ECF_NOTHROW);
7761 if (built_in_decls[BUILT_IN_ALLOCA] == NULL)
7763 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7764 ftype = build_function_type (ptr_type_node, tmp);
7765 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
7766 "alloca", ECF_NOTHROW | ECF_MALLOC);
7769 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7770 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7771 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7772 ftype = build_function_type (void_type_node, tmp);
7773 local_define_builtin ("__builtin_init_trampoline", ftype,
7774 BUILT_IN_INIT_TRAMPOLINE,
7775 "__builtin_init_trampoline", ECF_NOTHROW);
7777 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7778 ftype = build_function_type (ptr_type_node, tmp);
7779 local_define_builtin ("__builtin_adjust_trampoline", ftype,
7780 BUILT_IN_ADJUST_TRAMPOLINE,
7781 "__builtin_adjust_trampoline",
7782 ECF_CONST | ECF_NOTHROW);
7784 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7785 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7786 ftype = build_function_type (void_type_node, tmp);
7787 local_define_builtin ("__builtin_nonlocal_goto", ftype,
7788 BUILT_IN_NONLOCAL_GOTO,
7789 "__builtin_nonlocal_goto",
7790 ECF_NORETURN | ECF_NOTHROW);
7792 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7793 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7794 ftype = build_function_type (void_type_node, tmp);
7795 local_define_builtin ("__builtin_setjmp_setup", ftype,
7796 BUILT_IN_SETJMP_SETUP,
7797 "__builtin_setjmp_setup", ECF_NOTHROW);
7799 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7800 ftype = build_function_type (ptr_type_node, tmp);
7801 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
7802 BUILT_IN_SETJMP_DISPATCHER,
7803 "__builtin_setjmp_dispatcher",
7804 ECF_PURE | ECF_NOTHROW);
7806 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7807 ftype = build_function_type (void_type_node, tmp);
7808 local_define_builtin ("__builtin_setjmp_receiver", ftype,
7809 BUILT_IN_SETJMP_RECEIVER,
7810 "__builtin_setjmp_receiver", ECF_NOTHROW);
7812 ftype = build_function_type (ptr_type_node, void_list_node);
7813 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
7814 "__builtin_stack_save", ECF_NOTHROW);
7816 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7817 ftype = build_function_type (void_type_node, tmp);
7818 local_define_builtin ("__builtin_stack_restore", ftype,
7819 BUILT_IN_STACK_RESTORE,
7820 "__builtin_stack_restore", ECF_NOTHROW);
7822 ftype = build_function_type (void_type_node, void_list_node);
7823 local_define_builtin ("__builtin_profile_func_enter", ftype,
7824 BUILT_IN_PROFILE_FUNC_ENTER, "profile_func_enter", 0);
7825 local_define_builtin ("__builtin_profile_func_exit", ftype,
7826 BUILT_IN_PROFILE_FUNC_EXIT, "profile_func_exit", 0);
7828 /* Complex multiplication and division. These are handled as builtins
7829 rather than optabs because emit_library_call_value doesn't support
7830 complex. Further, we can do slightly better with folding these
7831 beasties if the real and complex parts of the arguments are separate. */
7833 enum machine_mode mode;
7835 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
7837 char mode_name_buf[4], *q;
7838 const char *p;
7839 enum built_in_function mcode, dcode;
7840 tree type, inner_type;
7842 type = lang_hooks.types.type_for_mode (mode, 0);
7843 if (type == NULL)
7844 continue;
7845 inner_type = TREE_TYPE (type);
7847 tmp = tree_cons (NULL_TREE, inner_type, void_list_node);
7848 tmp = tree_cons (NULL_TREE, inner_type, tmp);
7849 tmp = tree_cons (NULL_TREE, inner_type, tmp);
7850 tmp = tree_cons (NULL_TREE, inner_type, tmp);
7851 ftype = build_function_type (type, tmp);
7853 mcode = ((enum built_in_function)
7854 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
7855 dcode = ((enum built_in_function)
7856 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
7858 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
7859 *q = TOLOWER (*p);
7860 *q = '\0';
7862 built_in_names[mcode] = concat ("__mul", mode_name_buf, "3", NULL);
7863 local_define_builtin (built_in_names[mcode], ftype, mcode,
7864 built_in_names[mcode], ECF_CONST | ECF_NOTHROW);
7866 built_in_names[dcode] = concat ("__div", mode_name_buf, "3", NULL);
7867 local_define_builtin (built_in_names[dcode], ftype, dcode,
7868 built_in_names[dcode], ECF_CONST | ECF_NOTHROW);
7873 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
7874 better way.
7876 If we requested a pointer to a vector, build up the pointers that
7877 we stripped off while looking for the inner type. Similarly for
7878 return values from functions.
7880 The argument TYPE is the top of the chain, and BOTTOM is the
7881 new type which we will point to. */
7883 tree
7884 reconstruct_complex_type (tree type, tree bottom)
7886 tree inner, outer;
7888 if (TREE_CODE (type) == POINTER_TYPE)
7890 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7891 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
7892 TYPE_REF_CAN_ALIAS_ALL (type));
7894 else if (TREE_CODE (type) == REFERENCE_TYPE)
7896 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7897 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
7898 TYPE_REF_CAN_ALIAS_ALL (type));
7900 else if (TREE_CODE (type) == ARRAY_TYPE)
7902 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7903 outer = build_array_type (inner, TYPE_DOMAIN (type));
7905 else if (TREE_CODE (type) == FUNCTION_TYPE)
7907 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7908 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
7910 else if (TREE_CODE (type) == METHOD_TYPE)
7912 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7913 /* The build_method_type_directly() routine prepends 'this' to argument list,
7914 so we must compensate by getting rid of it. */
7915 outer
7916 = build_method_type_directly
7917 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
7918 inner,
7919 TREE_CHAIN (TYPE_ARG_TYPES (type)));
7921 else if (TREE_CODE (type) == OFFSET_TYPE)
7923 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7924 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
7926 else
7927 return bottom;
7929 return build_qualified_type (outer, TYPE_QUALS (type));
7932 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
7933 the inner type. */
7934 tree
7935 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
7937 int nunits;
7939 switch (GET_MODE_CLASS (mode))
7941 case MODE_VECTOR_INT:
7942 case MODE_VECTOR_FLOAT:
7943 case MODE_VECTOR_FRACT:
7944 case MODE_VECTOR_UFRACT:
7945 case MODE_VECTOR_ACCUM:
7946 case MODE_VECTOR_UACCUM:
7947 nunits = GET_MODE_NUNITS (mode);
7948 break;
7950 case MODE_INT:
7951 /* Check that there are no leftover bits. */
7952 gcc_assert (GET_MODE_BITSIZE (mode)
7953 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
7955 nunits = GET_MODE_BITSIZE (mode)
7956 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
7957 break;
7959 default:
7960 gcc_unreachable ();
7963 return make_vector_type (innertype, nunits, mode);
7966 /* Similarly, but takes the inner type and number of units, which must be
7967 a power of two. */
7969 tree
7970 build_vector_type (tree innertype, int nunits)
7972 return make_vector_type (innertype, nunits, VOIDmode);
7975 /* Similarly, but takes the inner type and number of units, which must be
7976 a power of two. */
7978 tree
7979 build_opaque_vector_type (tree innertype, int nunits)
7981 tree t;
7982 innertype = build_distinct_type_copy (innertype);
7983 t = make_vector_type (innertype, nunits, VOIDmode);
7984 TYPE_VECTOR_OPAQUE (t) = true;
7985 return t;
7989 /* Build RESX_EXPR with given REGION_NUMBER. */
7990 tree
7991 build_resx (int region_number)
7993 tree t;
7994 t = build1 (RESX_EXPR, void_type_node,
7995 build_int_cst (NULL_TREE, region_number));
7996 return t;
7999 /* Given an initializer INIT, return TRUE if INIT is zero or some
8000 aggregate of zeros. Otherwise return FALSE. */
8001 bool
8002 initializer_zerop (const_tree init)
8004 tree elt;
8006 STRIP_NOPS (init);
8008 switch (TREE_CODE (init))
8010 case INTEGER_CST:
8011 return integer_zerop (init);
8013 case REAL_CST:
8014 /* ??? Note that this is not correct for C4X float formats. There,
8015 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
8016 negative exponent. */
8017 return real_zerop (init)
8018 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
8020 case FIXED_CST:
8021 return fixed_zerop (init);
8023 case COMPLEX_CST:
8024 return integer_zerop (init)
8025 || (real_zerop (init)
8026 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
8027 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
8029 case VECTOR_CST:
8030 for (elt = TREE_VECTOR_CST_ELTS (init); elt; elt = TREE_CHAIN (elt))
8031 if (!initializer_zerop (TREE_VALUE (elt)))
8032 return false;
8033 return true;
8035 case CONSTRUCTOR:
8037 unsigned HOST_WIDE_INT idx;
8039 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
8040 if (!initializer_zerop (elt))
8041 return false;
8042 return true;
8045 default:
8046 return false;
8050 /* Build an empty statement. */
8052 tree
8053 build_empty_stmt (void)
8055 return build1 (NOP_EXPR, void_type_node, size_zero_node);
8059 /* Build an OpenMP clause with code CODE. */
8061 tree
8062 build_omp_clause (enum omp_clause_code code)
8064 tree t;
8065 int size, length;
8067 length = omp_clause_num_ops[code];
8068 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
8070 t = GGC_NEWVAR (union tree_node, size);
8071 memset (t, 0, size);
8072 TREE_SET_CODE (t, OMP_CLAUSE);
8073 OMP_CLAUSE_SET_CODE (t, code);
8075 #ifdef GATHER_STATISTICS
8076 tree_node_counts[(int) omp_clause_kind]++;
8077 tree_node_sizes[(int) omp_clause_kind] += size;
8078 #endif
8080 return t;
8083 /* Set various status flags when building a CALL_EXPR object T. */
8085 static void
8086 process_call_operands (tree t)
8088 bool side_effects;
8090 side_effects = TREE_SIDE_EFFECTS (t);
8091 if (!side_effects)
8093 int i, n;
8094 n = TREE_OPERAND_LENGTH (t);
8095 for (i = 1; i < n; i++)
8097 tree op = TREE_OPERAND (t, i);
8098 if (op && TREE_SIDE_EFFECTS (op))
8100 side_effects = 1;
8101 break;
8105 if (!side_effects)
8107 int i;
8109 /* Calls have side-effects, except those to const or
8110 pure functions. */
8111 i = call_expr_flags (t);
8112 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
8113 side_effects = 1;
8115 TREE_SIDE_EFFECTS (t) = side_effects;
8118 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
8119 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
8120 Except for the CODE and operand count field, other storage for the
8121 object is initialized to zeros. */
8123 tree
8124 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
8126 tree t;
8127 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
8129 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
8130 gcc_assert (len >= 1);
8132 #ifdef GATHER_STATISTICS
8133 tree_node_counts[(int) e_kind]++;
8134 tree_node_sizes[(int) e_kind] += length;
8135 #endif
8137 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
8139 memset (t, 0, length);
8141 TREE_SET_CODE (t, code);
8143 /* Can't use TREE_OPERAND to store the length because if checking is
8144 enabled, it will try to check the length before we store it. :-P */
8145 t->exp.operands[0] = build_int_cst (sizetype, len);
8147 return t;
8151 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE
8152 and FN and a null static chain slot. ARGLIST is a TREE_LIST of the
8153 arguments. */
8155 tree
8156 build_call_list (tree return_type, tree fn, tree arglist)
8158 tree t;
8159 int i;
8161 t = build_vl_exp (CALL_EXPR, list_length (arglist) + 3);
8162 TREE_TYPE (t) = return_type;
8163 CALL_EXPR_FN (t) = fn;
8164 CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
8165 for (i = 0; arglist; arglist = TREE_CHAIN (arglist), i++)
8166 CALL_EXPR_ARG (t, i) = TREE_VALUE (arglist);
8167 process_call_operands (t);
8168 return t;
8171 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
8172 FN and a null static chain slot. NARGS is the number of call arguments
8173 which are specified as "..." arguments. */
8175 tree
8176 build_call_nary (tree return_type, tree fn, int nargs, ...)
8178 tree ret;
8179 va_list args;
8180 va_start (args, nargs);
8181 ret = build_call_valist (return_type, fn, nargs, args);
8182 va_end (args);
8183 return ret;
8186 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
8187 FN and a null static chain slot. NARGS is the number of call arguments
8188 which are specified as a va_list ARGS. */
8190 tree
8191 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
8193 tree t;
8194 int i;
8196 t = build_vl_exp (CALL_EXPR, nargs + 3);
8197 TREE_TYPE (t) = return_type;
8198 CALL_EXPR_FN (t) = fn;
8199 CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
8200 for (i = 0; i < nargs; i++)
8201 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
8202 process_call_operands (t);
8203 return t;
8206 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
8207 FN and a null static chain slot. NARGS is the number of call arguments
8208 which are specified as a tree array ARGS. */
8210 tree
8211 build_call_array (tree return_type, tree fn, int nargs, tree *args)
8213 tree t;
8214 int i;
8216 t = build_vl_exp (CALL_EXPR, nargs + 3);
8217 TREE_TYPE (t) = return_type;
8218 CALL_EXPR_FN (t) = fn;
8219 CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
8220 for (i = 0; i < nargs; i++)
8221 CALL_EXPR_ARG (t, i) = args[i];
8222 process_call_operands (t);
8223 return t;
8227 /* Returns true if it is possible to prove that the index of
8228 an array access REF (an ARRAY_REF expression) falls into the
8229 array bounds. */
8231 bool
8232 in_array_bounds_p (tree ref)
8234 tree idx = TREE_OPERAND (ref, 1);
8235 tree min, max;
8237 if (TREE_CODE (idx) != INTEGER_CST)
8238 return false;
8240 min = array_ref_low_bound (ref);
8241 max = array_ref_up_bound (ref);
8242 if (!min
8243 || !max
8244 || TREE_CODE (min) != INTEGER_CST
8245 || TREE_CODE (max) != INTEGER_CST)
8246 return false;
8248 if (tree_int_cst_lt (idx, min)
8249 || tree_int_cst_lt (max, idx))
8250 return false;
8252 return true;
8255 /* Returns true if it is possible to prove that the range of
8256 an array access REF (an ARRAY_RANGE_REF expression) falls
8257 into the array bounds. */
8259 bool
8260 range_in_array_bounds_p (tree ref)
8262 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
8263 tree range_min, range_max, min, max;
8265 range_min = TYPE_MIN_VALUE (domain_type);
8266 range_max = TYPE_MAX_VALUE (domain_type);
8267 if (!range_min
8268 || !range_max
8269 || TREE_CODE (range_min) != INTEGER_CST
8270 || TREE_CODE (range_max) != INTEGER_CST)
8271 return false;
8273 min = array_ref_low_bound (ref);
8274 max = array_ref_up_bound (ref);
8275 if (!min
8276 || !max
8277 || TREE_CODE (min) != INTEGER_CST
8278 || TREE_CODE (max) != INTEGER_CST)
8279 return false;
8281 if (tree_int_cst_lt (range_min, min)
8282 || tree_int_cst_lt (max, range_max))
8283 return false;
8285 return true;
8288 /* Return true if T (assumed to be a DECL) must be assigned a memory
8289 location. */
8291 bool
8292 needs_to_live_in_memory (const_tree t)
8294 if (TREE_CODE (t) == SSA_NAME)
8295 t = SSA_NAME_VAR (t);
8297 return (TREE_ADDRESSABLE (t)
8298 || is_global_var (t)
8299 || (TREE_CODE (t) == RESULT_DECL
8300 && aggregate_value_p (t, current_function_decl)));
8303 /* There are situations in which a language considers record types
8304 compatible which have different field lists. Decide if two fields
8305 are compatible. It is assumed that the parent records are compatible. */
8307 bool
8308 fields_compatible_p (const_tree f1, const_tree f2)
8310 if (!operand_equal_p (DECL_FIELD_BIT_OFFSET (f1),
8311 DECL_FIELD_BIT_OFFSET (f2), OEP_ONLY_CONST))
8312 return false;
8314 if (!operand_equal_p (DECL_FIELD_OFFSET (f1),
8315 DECL_FIELD_OFFSET (f2), OEP_ONLY_CONST))
8316 return false;
8318 if (!types_compatible_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8319 return false;
8321 return true;
8324 /* Locate within RECORD a field that is compatible with ORIG_FIELD. */
8326 tree
8327 find_compatible_field (tree record, tree orig_field)
8329 tree f;
8331 for (f = TYPE_FIELDS (record); f ; f = TREE_CHAIN (f))
8332 if (TREE_CODE (f) == FIELD_DECL
8333 && fields_compatible_p (f, orig_field))
8334 return f;
8336 /* ??? Why isn't this on the main fields list? */
8337 f = TYPE_VFIELD (record);
8338 if (f && TREE_CODE (f) == FIELD_DECL
8339 && fields_compatible_p (f, orig_field))
8340 return f;
8342 /* ??? We should abort here, but Java appears to do Bad Things
8343 with inherited fields. */
8344 return orig_field;
8347 /* Return value of a constant X and sign-extend it. */
8349 HOST_WIDE_INT
8350 int_cst_value (const_tree x)
8352 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
8353 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
8355 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
8356 gcc_assert (TREE_INT_CST_HIGH (x) == 0
8357 || TREE_INT_CST_HIGH (x) == -1);
8359 if (bits < HOST_BITS_PER_WIDE_INT)
8361 bool negative = ((val >> (bits - 1)) & 1) != 0;
8362 if (negative)
8363 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
8364 else
8365 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
8368 return val;
8371 /* If TYPE is an integral type, return an equivalent type which is
8372 unsigned iff UNSIGNEDP is true. If TYPE is not an integral type,
8373 return TYPE itself. */
8375 tree
8376 signed_or_unsigned_type_for (int unsignedp, tree type)
8378 tree t = type;
8379 if (POINTER_TYPE_P (type))
8380 t = size_type_node;
8382 if (!INTEGRAL_TYPE_P (t) || TYPE_UNSIGNED (t) == unsignedp)
8383 return t;
8385 return lang_hooks.types.type_for_size (TYPE_PRECISION (t), unsignedp);
8388 /* Returns unsigned variant of TYPE. */
8390 tree
8391 unsigned_type_for (tree type)
8393 return signed_or_unsigned_type_for (1, type);
8396 /* Returns signed variant of TYPE. */
8398 tree
8399 signed_type_for (tree type)
8401 return signed_or_unsigned_type_for (0, type);
8404 /* Returns the largest value obtainable by casting something in INNER type to
8405 OUTER type. */
8407 tree
8408 upper_bound_in_type (tree outer, tree inner)
8410 unsigned HOST_WIDE_INT lo, hi;
8411 unsigned int det = 0;
8412 unsigned oprec = TYPE_PRECISION (outer);
8413 unsigned iprec = TYPE_PRECISION (inner);
8414 unsigned prec;
8416 /* Compute a unique number for every combination. */
8417 det |= (oprec > iprec) ? 4 : 0;
8418 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
8419 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
8421 /* Determine the exponent to use. */
8422 switch (det)
8424 case 0:
8425 case 1:
8426 /* oprec <= iprec, outer: signed, inner: don't care. */
8427 prec = oprec - 1;
8428 break;
8429 case 2:
8430 case 3:
8431 /* oprec <= iprec, outer: unsigned, inner: don't care. */
8432 prec = oprec;
8433 break;
8434 case 4:
8435 /* oprec > iprec, outer: signed, inner: signed. */
8436 prec = iprec - 1;
8437 break;
8438 case 5:
8439 /* oprec > iprec, outer: signed, inner: unsigned. */
8440 prec = iprec;
8441 break;
8442 case 6:
8443 /* oprec > iprec, outer: unsigned, inner: signed. */
8444 prec = oprec;
8445 break;
8446 case 7:
8447 /* oprec > iprec, outer: unsigned, inner: unsigned. */
8448 prec = iprec;
8449 break;
8450 default:
8451 gcc_unreachable ();
8454 /* Compute 2^^prec - 1. */
8455 if (prec <= HOST_BITS_PER_WIDE_INT)
8457 hi = 0;
8458 lo = ((~(unsigned HOST_WIDE_INT) 0)
8459 >> (HOST_BITS_PER_WIDE_INT - prec));
8461 else
8463 hi = ((~(unsigned HOST_WIDE_INT) 0)
8464 >> (2 * HOST_BITS_PER_WIDE_INT - prec));
8465 lo = ~(unsigned HOST_WIDE_INT) 0;
8468 return build_int_cst_wide (outer, lo, hi);
8471 /* Returns the smallest value obtainable by casting something in INNER type to
8472 OUTER type. */
8474 tree
8475 lower_bound_in_type (tree outer, tree inner)
8477 unsigned HOST_WIDE_INT lo, hi;
8478 unsigned oprec = TYPE_PRECISION (outer);
8479 unsigned iprec = TYPE_PRECISION (inner);
8481 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
8482 and obtain 0. */
8483 if (TYPE_UNSIGNED (outer)
8484 /* If we are widening something of an unsigned type, OUTER type
8485 contains all values of INNER type. In particular, both INNER
8486 and OUTER types have zero in common. */
8487 || (oprec > iprec && TYPE_UNSIGNED (inner)))
8488 lo = hi = 0;
8489 else
8491 /* If we are widening a signed type to another signed type, we
8492 want to obtain -2^^(iprec-1). If we are keeping the
8493 precision or narrowing to a signed type, we want to obtain
8494 -2^(oprec-1). */
8495 unsigned prec = oprec > iprec ? iprec : oprec;
8497 if (prec <= HOST_BITS_PER_WIDE_INT)
8499 hi = ~(unsigned HOST_WIDE_INT) 0;
8500 lo = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
8502 else
8504 hi = ((~(unsigned HOST_WIDE_INT) 0)
8505 << (prec - HOST_BITS_PER_WIDE_INT - 1));
8506 lo = 0;
8510 return build_int_cst_wide (outer, lo, hi);
8513 /* Return nonzero if two operands that are suitable for PHI nodes are
8514 necessarily equal. Specifically, both ARG0 and ARG1 must be either
8515 SSA_NAME or invariant. Note that this is strictly an optimization.
8516 That is, callers of this function can directly call operand_equal_p
8517 and get the same result, only slower. */
8520 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
8522 if (arg0 == arg1)
8523 return 1;
8524 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
8525 return 0;
8526 return operand_equal_p (arg0, arg1, 0);
8529 /* Returns number of zeros at the end of binary representation of X.
8531 ??? Use ffs if available? */
8533 tree
8534 num_ending_zeros (const_tree x)
8536 unsigned HOST_WIDE_INT fr, nfr;
8537 unsigned num, abits;
8538 tree type = TREE_TYPE (x);
8540 if (TREE_INT_CST_LOW (x) == 0)
8542 num = HOST_BITS_PER_WIDE_INT;
8543 fr = TREE_INT_CST_HIGH (x);
8545 else
8547 num = 0;
8548 fr = TREE_INT_CST_LOW (x);
8551 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
8553 nfr = fr >> abits;
8554 if (nfr << abits == fr)
8556 num += abits;
8557 fr = nfr;
8561 if (num > TYPE_PRECISION (type))
8562 num = TYPE_PRECISION (type);
8564 return build_int_cst_type (type, num);
8568 #define WALK_SUBTREE(NODE) \
8569 do \
8571 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
8572 if (result) \
8573 return result; \
8575 while (0)
8577 /* This is a subroutine of walk_tree that walks field of TYPE that are to
8578 be walked whenever a type is seen in the tree. Rest of operands and return
8579 value are as for walk_tree. */
8581 static tree
8582 walk_type_fields (tree type, walk_tree_fn func, void *data,
8583 struct pointer_set_t *pset, walk_tree_lh lh)
8585 tree result = NULL_TREE;
8587 switch (TREE_CODE (type))
8589 case POINTER_TYPE:
8590 case REFERENCE_TYPE:
8591 /* We have to worry about mutually recursive pointers. These can't
8592 be written in C. They can in Ada. It's pathological, but
8593 there's an ACATS test (c38102a) that checks it. Deal with this
8594 by checking if we're pointing to another pointer, that one
8595 points to another pointer, that one does too, and we have no htab.
8596 If so, get a hash table. We check three levels deep to avoid
8597 the cost of the hash table if we don't need one. */
8598 if (POINTER_TYPE_P (TREE_TYPE (type))
8599 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
8600 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
8601 && !pset)
8603 result = walk_tree_without_duplicates (&TREE_TYPE (type),
8604 func, data);
8605 if (result)
8606 return result;
8608 break;
8611 /* ... fall through ... */
8613 case COMPLEX_TYPE:
8614 WALK_SUBTREE (TREE_TYPE (type));
8615 break;
8617 case METHOD_TYPE:
8618 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
8620 /* Fall through. */
8622 case FUNCTION_TYPE:
8623 WALK_SUBTREE (TREE_TYPE (type));
8625 tree arg;
8627 /* We never want to walk into default arguments. */
8628 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
8629 WALK_SUBTREE (TREE_VALUE (arg));
8631 break;
8633 case ARRAY_TYPE:
8634 /* Don't follow this nodes's type if a pointer for fear that
8635 we'll have infinite recursion. If we have a PSET, then we
8636 need not fear. */
8637 if (pset
8638 || (!POINTER_TYPE_P (TREE_TYPE (type))
8639 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
8640 WALK_SUBTREE (TREE_TYPE (type));
8641 WALK_SUBTREE (TYPE_DOMAIN (type));
8642 break;
8644 case OFFSET_TYPE:
8645 WALK_SUBTREE (TREE_TYPE (type));
8646 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
8647 break;
8649 default:
8650 break;
8653 return NULL_TREE;
8656 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
8657 called with the DATA and the address of each sub-tree. If FUNC returns a
8658 non-NULL value, the traversal is stopped, and the value returned by FUNC
8659 is returned. If PSET is non-NULL it is used to record the nodes visited,
8660 and to avoid visiting a node more than once. */
8662 tree
8663 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
8664 struct pointer_set_t *pset, walk_tree_lh lh)
8666 enum tree_code code;
8667 int walk_subtrees;
8668 tree result;
8670 #define WALK_SUBTREE_TAIL(NODE) \
8671 do \
8673 tp = & (NODE); \
8674 goto tail_recurse; \
8676 while (0)
8678 tail_recurse:
8679 /* Skip empty subtrees. */
8680 if (!*tp)
8681 return NULL_TREE;
8683 /* Don't walk the same tree twice, if the user has requested
8684 that we avoid doing so. */
8685 if (pset && pointer_set_insert (pset, *tp))
8686 return NULL_TREE;
8688 /* Call the function. */
8689 walk_subtrees = 1;
8690 result = (*func) (tp, &walk_subtrees, data);
8692 /* If we found something, return it. */
8693 if (result)
8694 return result;
8696 code = TREE_CODE (*tp);
8698 /* Even if we didn't, FUNC may have decided that there was nothing
8699 interesting below this point in the tree. */
8700 if (!walk_subtrees)
8702 /* But we still need to check our siblings. */
8703 if (code == TREE_LIST)
8704 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
8705 else if (code == OMP_CLAUSE)
8706 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8707 else
8708 return NULL_TREE;
8711 if (lh)
8713 result = (*lh) (tp, &walk_subtrees, func, data, pset);
8714 if (result || !walk_subtrees)
8715 return result;
8718 switch (code)
8720 case ERROR_MARK:
8721 case IDENTIFIER_NODE:
8722 case INTEGER_CST:
8723 case REAL_CST:
8724 case FIXED_CST:
8725 case VECTOR_CST:
8726 case STRING_CST:
8727 case BLOCK:
8728 case PLACEHOLDER_EXPR:
8729 case SSA_NAME:
8730 case FIELD_DECL:
8731 case RESULT_DECL:
8732 /* None of these have subtrees other than those already walked
8733 above. */
8734 break;
8736 case TREE_LIST:
8737 WALK_SUBTREE (TREE_VALUE (*tp));
8738 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
8739 break;
8741 case TREE_VEC:
8743 int len = TREE_VEC_LENGTH (*tp);
8745 if (len == 0)
8746 break;
8748 /* Walk all elements but the first. */
8749 while (--len)
8750 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
8752 /* Now walk the first one as a tail call. */
8753 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
8756 case COMPLEX_CST:
8757 WALK_SUBTREE (TREE_REALPART (*tp));
8758 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
8760 case CONSTRUCTOR:
8762 unsigned HOST_WIDE_INT idx;
8763 constructor_elt *ce;
8765 for (idx = 0;
8766 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (*tp), idx, ce);
8767 idx++)
8768 WALK_SUBTREE (ce->value);
8770 break;
8772 case SAVE_EXPR:
8773 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
8775 case BIND_EXPR:
8777 tree decl;
8778 for (decl = BIND_EXPR_VARS (*tp); decl; decl = TREE_CHAIN (decl))
8780 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
8781 into declarations that are just mentioned, rather than
8782 declared; they don't really belong to this part of the tree.
8783 And, we can see cycles: the initializer for a declaration
8784 can refer to the declaration itself. */
8785 WALK_SUBTREE (DECL_INITIAL (decl));
8786 WALK_SUBTREE (DECL_SIZE (decl));
8787 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
8789 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
8792 case STATEMENT_LIST:
8794 tree_stmt_iterator i;
8795 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
8796 WALK_SUBTREE (*tsi_stmt_ptr (i));
8798 break;
8800 case OMP_CLAUSE:
8801 switch (OMP_CLAUSE_CODE (*tp))
8803 case OMP_CLAUSE_PRIVATE:
8804 case OMP_CLAUSE_SHARED:
8805 case OMP_CLAUSE_FIRSTPRIVATE:
8806 case OMP_CLAUSE_COPYIN:
8807 case OMP_CLAUSE_COPYPRIVATE:
8808 case OMP_CLAUSE_IF:
8809 case OMP_CLAUSE_NUM_THREADS:
8810 case OMP_CLAUSE_SCHEDULE:
8811 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
8812 /* FALLTHRU */
8814 case OMP_CLAUSE_NOWAIT:
8815 case OMP_CLAUSE_ORDERED:
8816 case OMP_CLAUSE_DEFAULT:
8817 case OMP_CLAUSE_UNTIED:
8818 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8820 case OMP_CLAUSE_LASTPRIVATE:
8821 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
8822 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
8823 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8825 case OMP_CLAUSE_COLLAPSE:
8827 int i;
8828 for (i = 0; i < 3; i++)
8829 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
8830 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8833 case OMP_CLAUSE_REDUCTION:
8835 int i;
8836 for (i = 0; i < 4; i++)
8837 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
8838 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8841 default:
8842 gcc_unreachable ();
8844 break;
8846 case TARGET_EXPR:
8848 int i, len;
8850 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
8851 But, we only want to walk once. */
8852 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
8853 for (i = 0; i < len; ++i)
8854 WALK_SUBTREE (TREE_OPERAND (*tp, i));
8855 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
8858 case CHANGE_DYNAMIC_TYPE_EXPR:
8859 WALK_SUBTREE (CHANGE_DYNAMIC_TYPE_NEW_TYPE (*tp));
8860 WALK_SUBTREE_TAIL (CHANGE_DYNAMIC_TYPE_LOCATION (*tp));
8862 case DECL_EXPR:
8863 /* If this is a TYPE_DECL, walk into the fields of the type that it's
8864 defining. We only want to walk into these fields of a type in this
8865 case and not in the general case of a mere reference to the type.
8867 The criterion is as follows: if the field can be an expression, it
8868 must be walked only here. This should be in keeping with the fields
8869 that are directly gimplified in gimplify_type_sizes in order for the
8870 mark/copy-if-shared/unmark machinery of the gimplifier to work with
8871 variable-sized types.
8873 Note that DECLs get walked as part of processing the BIND_EXPR. */
8874 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
8876 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
8877 if (TREE_CODE (*type_p) == ERROR_MARK)
8878 return NULL_TREE;
8880 /* Call the function for the type. See if it returns anything or
8881 doesn't want us to continue. If we are to continue, walk both
8882 the normal fields and those for the declaration case. */
8883 result = (*func) (type_p, &walk_subtrees, data);
8884 if (result || !walk_subtrees)
8885 return result;
8887 result = walk_type_fields (*type_p, func, data, pset, lh);
8888 if (result)
8889 return result;
8891 /* If this is a record type, also walk the fields. */
8892 if (TREE_CODE (*type_p) == RECORD_TYPE
8893 || TREE_CODE (*type_p) == UNION_TYPE
8894 || TREE_CODE (*type_p) == QUAL_UNION_TYPE)
8896 tree field;
8898 for (field = TYPE_FIELDS (*type_p); field;
8899 field = TREE_CHAIN (field))
8901 /* We'd like to look at the type of the field, but we can
8902 easily get infinite recursion. So assume it's pointed
8903 to elsewhere in the tree. Also, ignore things that
8904 aren't fields. */
8905 if (TREE_CODE (field) != FIELD_DECL)
8906 continue;
8908 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
8909 WALK_SUBTREE (DECL_SIZE (field));
8910 WALK_SUBTREE (DECL_SIZE_UNIT (field));
8911 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
8912 WALK_SUBTREE (DECL_QUALIFIER (field));
8916 /* Same for scalar types. */
8917 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
8918 || TREE_CODE (*type_p) == ENUMERAL_TYPE
8919 || TREE_CODE (*type_p) == INTEGER_TYPE
8920 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
8921 || TREE_CODE (*type_p) == REAL_TYPE)
8923 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
8924 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
8927 WALK_SUBTREE (TYPE_SIZE (*type_p));
8928 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
8930 /* FALLTHRU */
8932 default:
8933 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
8935 int i, len;
8937 /* Walk over all the sub-trees of this operand. */
8938 len = TREE_OPERAND_LENGTH (*tp);
8940 /* Go through the subtrees. We need to do this in forward order so
8941 that the scope of a FOR_EXPR is handled properly. */
8942 if (len)
8944 for (i = 0; i < len - 1; ++i)
8945 WALK_SUBTREE (TREE_OPERAND (*tp, i));
8946 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
8949 /* If this is a type, walk the needed fields in the type. */
8950 else if (TYPE_P (*tp))
8951 return walk_type_fields (*tp, func, data, pset, lh);
8952 break;
8955 /* We didn't find what we were looking for. */
8956 return NULL_TREE;
8958 #undef WALK_SUBTREE_TAIL
8960 #undef WALK_SUBTREE
8962 /* Like walk_tree, but does not walk duplicate nodes more than once. */
8964 tree
8965 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
8966 walk_tree_lh lh)
8968 tree result;
8969 struct pointer_set_t *pset;
8971 pset = pointer_set_create ();
8972 result = walk_tree_1 (tp, func, data, pset, lh);
8973 pointer_set_destroy (pset);
8974 return result;
8978 tree *
8979 tree_block (tree t)
8981 char const c = TREE_CODE_CLASS (TREE_CODE (t));
8983 if (IS_EXPR_CODE_CLASS (c))
8984 return &t->exp.block;
8985 gcc_unreachable ();
8986 return NULL;
8989 /* Build and return a TREE_LIST of arguments in the CALL_EXPR exp.
8990 FIXME: don't use this function. It exists for compatibility with
8991 the old representation of CALL_EXPRs where a list was used to hold the
8992 arguments. Places that currently extract the arglist from a CALL_EXPR
8993 ought to be rewritten to use the CALL_EXPR itself. */
8994 tree
8995 call_expr_arglist (tree exp)
8997 tree arglist = NULL_TREE;
8998 int i;
8999 for (i = call_expr_nargs (exp) - 1; i >= 0; i--)
9000 arglist = tree_cons (NULL_TREE, CALL_EXPR_ARG (exp, i), arglist);
9001 return arglist;
9005 /* Create a nameless artificial label and put it in the current function
9006 context. Returns the newly created label. */
9008 tree
9009 create_artificial_label (void)
9011 tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
9013 DECL_ARTIFICIAL (lab) = 1;
9014 DECL_IGNORED_P (lab) = 1;
9015 DECL_CONTEXT (lab) = current_function_decl;
9016 return lab;
9019 /* Given a tree, try to return a useful variable name that we can use
9020 to prefix a temporary that is being assigned the value of the tree.
9021 I.E. given <temp> = &A, return A. */
9023 const char *
9024 get_name (tree t)
9026 tree stripped_decl;
9028 stripped_decl = t;
9029 STRIP_NOPS (stripped_decl);
9030 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
9031 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
9032 else
9034 switch (TREE_CODE (stripped_decl))
9036 case ADDR_EXPR:
9037 return get_name (TREE_OPERAND (stripped_decl, 0));
9038 default:
9039 return NULL;
9044 /* Return true if TYPE has a variable argument list. */
9046 bool
9047 stdarg_p (tree fntype)
9049 function_args_iterator args_iter;
9050 tree n = NULL_TREE, t;
9052 if (!fntype)
9053 return false;
9055 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
9057 n = t;
9060 return n != NULL_TREE && n != void_type_node;
9063 /* Return true if TYPE has a prototype. */
9065 bool
9066 prototype_p (tree fntype)
9068 tree t;
9070 gcc_assert (fntype != NULL_TREE);
9072 t = TYPE_ARG_TYPES (fntype);
9073 return (t != NULL_TREE);
9076 /* If BLOCK is inlined from an __attribute__((__artificial__))
9077 routine, return pointer to location from where it has been
9078 called. */
9079 location_t *
9080 block_nonartificial_location (tree block)
9082 location_t *ret = NULL;
9084 while (block && TREE_CODE (block) == BLOCK
9085 && BLOCK_ABSTRACT_ORIGIN (block))
9087 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
9089 while (TREE_CODE (ao) == BLOCK
9090 && BLOCK_ABSTRACT_ORIGIN (ao)
9091 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
9092 ao = BLOCK_ABSTRACT_ORIGIN (ao);
9094 if (TREE_CODE (ao) == FUNCTION_DECL)
9096 /* If AO is an artificial inline, point RET to the
9097 call site locus at which it has been inlined and continue
9098 the loop, in case AO's caller is also an artificial
9099 inline. */
9100 if (DECL_DECLARED_INLINE_P (ao)
9101 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
9102 ret = &BLOCK_SOURCE_LOCATION (block);
9103 else
9104 break;
9106 else if (TREE_CODE (ao) != BLOCK)
9107 break;
9109 block = BLOCK_SUPERCONTEXT (block);
9111 return ret;
9115 /* If EXP is inlined from an __attribute__((__artificial__))
9116 function, return the location of the original call expression. */
9118 location_t
9119 tree_nonartificial_location (tree exp)
9121 tree block = TREE_BLOCK (exp);
9123 while (block
9124 && TREE_CODE (block) == BLOCK
9125 && BLOCK_ABSTRACT_ORIGIN (block))
9127 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
9131 if (TREE_CODE (ao) == FUNCTION_DECL
9132 && DECL_DECLARED_INLINE_P (ao)
9133 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
9134 return BLOCK_SOURCE_LOCATION (block);
9135 else if (TREE_CODE (ao) == BLOCK
9136 && BLOCK_SUPERCONTEXT (ao) != ao)
9137 ao = BLOCK_SUPERCONTEXT (ao);
9138 else
9139 break;
9141 while (ao);
9143 block = BLOCK_SUPERCONTEXT (block);
9146 return EXPR_LOCATION (exp);
9150 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
9151 nodes. */
9153 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
9155 static hashval_t
9156 cl_option_hash_hash (const void *x)
9158 const_tree const t = (const_tree) x;
9159 const char *p;
9160 size_t i;
9161 size_t len = 0;
9162 hashval_t hash = 0;
9164 if (TREE_CODE (t) == OPTIMIZATION_NODE)
9166 p = (const char *)TREE_OPTIMIZATION (t);
9167 len = sizeof (struct cl_optimization);
9170 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
9172 p = (const char *)TREE_TARGET_OPTION (t);
9173 len = sizeof (struct cl_target_option);
9176 else
9177 gcc_unreachable ();
9179 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
9180 something else. */
9181 for (i = 0; i < len; i++)
9182 if (p[i])
9183 hash = (hash << 4) ^ ((i << 2) | p[i]);
9185 return hash;
9188 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
9189 TARGET_OPTION tree node) is the same as that given by *Y, which is the
9190 same. */
9192 static int
9193 cl_option_hash_eq (const void *x, const void *y)
9195 const_tree const xt = (const_tree) x;
9196 const_tree const yt = (const_tree) y;
9197 const char *xp;
9198 const char *yp;
9199 size_t len;
9201 if (TREE_CODE (xt) != TREE_CODE (yt))
9202 return 0;
9204 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
9206 xp = (const char *)TREE_OPTIMIZATION (xt);
9207 yp = (const char *)TREE_OPTIMIZATION (yt);
9208 len = sizeof (struct cl_optimization);
9211 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
9213 xp = (const char *)TREE_TARGET_OPTION (xt);
9214 yp = (const char *)TREE_TARGET_OPTION (yt);
9215 len = sizeof (struct cl_target_option);
9218 else
9219 gcc_unreachable ();
9221 return (memcmp (xp, yp, len) == 0);
9224 /* Build an OPTIMIZATION_NODE based on the current options. */
9226 tree
9227 build_optimization_node (void)
9229 tree t;
9230 void **slot;
9232 /* Use the cache of optimization nodes. */
9234 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node));
9236 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
9237 t = (tree) *slot;
9238 if (!t)
9240 /* Insert this one into the hash table. */
9241 t = cl_optimization_node;
9242 *slot = t;
9244 /* Make a new node for next time round. */
9245 cl_optimization_node = make_node (OPTIMIZATION_NODE);
9248 return t;
9251 /* Build a TARGET_OPTION_NODE based on the current options. */
9253 tree
9254 build_target_option_node (void)
9256 tree t;
9257 void **slot;
9259 /* Use the cache of optimization nodes. */
9261 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node));
9263 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
9264 t = (tree) *slot;
9265 if (!t)
9267 /* Insert this one into the hash table. */
9268 t = cl_target_option_node;
9269 *slot = t;
9271 /* Make a new node for next time round. */
9272 cl_target_option_node = make_node (TARGET_OPTION_NODE);
9275 return t;
9278 /* Determine the "ultimate origin" of a block. The block may be an inlined
9279 instance of an inlined instance of a block which is local to an inline
9280 function, so we have to trace all of the way back through the origin chain
9281 to find out what sort of node actually served as the original seed for the
9282 given block. */
9284 tree
9285 block_ultimate_origin (const_tree block)
9287 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
9289 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
9290 nodes in the function to point to themselves; ignore that if
9291 we're trying to output the abstract instance of this function. */
9292 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
9293 return NULL_TREE;
9295 if (immediate_origin == NULL_TREE)
9296 return NULL_TREE;
9297 else
9299 tree ret_val;
9300 tree lookahead = immediate_origin;
9304 ret_val = lookahead;
9305 lookahead = (TREE_CODE (ret_val) == BLOCK
9306 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
9308 while (lookahead != NULL && lookahead != ret_val);
9310 /* The block's abstract origin chain may not be the *ultimate* origin of
9311 the block. It could lead to a DECL that has an abstract origin set.
9312 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
9313 will give us if it has one). Note that DECL's abstract origins are
9314 supposed to be the most distant ancestor (or so decl_ultimate_origin
9315 claims), so we don't need to loop following the DECL origins. */
9316 if (DECL_P (ret_val))
9317 return DECL_ORIGIN (ret_val);
9319 return ret_val;
9323 /* Return true if T1 and T2 are equivalent lists. */
9325 bool
9326 list_equal_p (const_tree t1, const_tree t2)
9328 for (; t1 && t2; t1 = TREE_CHAIN (t1) , t2 = TREE_CHAIN (t2))
9329 if (TREE_VALUE (t1) != TREE_VALUE (t2))
9330 return false;
9331 return !t1 && !t2;
9335 #include "gt-tree.h"