* tree.h (STRIP_NOPS, STRIP_SIGN_NOPS,
[official-gcc.git] / gcc / tree.c
blobf48512fceeb8c3d60e8fc90be8ce1e0b3d187696
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 /* Build a chain of TREE_LIST nodes from a vector. */
1832 tree
1833 build_tree_list_vec_stat (const VEC(tree,gc) *vec MEM_STAT_DECL)
1835 tree ret = NULL_TREE;
1836 tree *pp = &ret;
1837 unsigned int i;
1838 tree t;
1839 for (i = 0; VEC_iterate (tree, vec, i, t); ++i)
1841 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
1842 pp = &TREE_CHAIN (*pp);
1844 return ret;
1847 /* Return a newly created TREE_LIST node whose
1848 purpose and value fields are PURPOSE and VALUE
1849 and whose TREE_CHAIN is CHAIN. */
1851 tree
1852 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
1854 tree node;
1856 node = (tree) ggc_alloc_zone_pass_stat (sizeof (struct tree_list), &tree_zone);
1858 memset (node, 0, sizeof (struct tree_common));
1860 #ifdef GATHER_STATISTICS
1861 tree_node_counts[(int) x_kind]++;
1862 tree_node_sizes[(int) x_kind] += sizeof (struct tree_list);
1863 #endif
1865 TREE_SET_CODE (node, TREE_LIST);
1866 TREE_CHAIN (node) = chain;
1867 TREE_PURPOSE (node) = purpose;
1868 TREE_VALUE (node) = value;
1869 return node;
1872 /* Return the elements of a CONSTRUCTOR as a TREE_LIST. */
1874 tree
1875 ctor_to_list (tree ctor)
1877 tree list = NULL_TREE;
1878 tree *p = &list;
1879 unsigned ix;
1880 tree purpose, val;
1882 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), ix, purpose, val)
1884 *p = build_tree_list (purpose, val);
1885 p = &TREE_CHAIN (*p);
1888 return list;
1891 /* Return the values of the elements of a CONSTRUCTOR as a vector of
1892 trees. */
1894 VEC(tree,gc) *
1895 ctor_to_vec (tree ctor)
1897 VEC(tree, gc) *vec = VEC_alloc (tree, gc, CONSTRUCTOR_NELTS (ctor));
1898 unsigned int ix;
1899 tree val;
1901 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
1902 VEC_quick_push (tree, vec, val);
1904 return vec;
1907 /* Return the size nominally occupied by an object of type TYPE
1908 when it resides in memory. The value is measured in units of bytes,
1909 and its data type is that normally used for type sizes
1910 (which is the first type created by make_signed_type or
1911 make_unsigned_type). */
1913 tree
1914 size_in_bytes (const_tree type)
1916 tree t;
1918 if (type == error_mark_node)
1919 return integer_zero_node;
1921 type = TYPE_MAIN_VARIANT (type);
1922 t = TYPE_SIZE_UNIT (type);
1924 if (t == 0)
1926 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
1927 return size_zero_node;
1930 return t;
1933 /* Return the size of TYPE (in bytes) as a wide integer
1934 or return -1 if the size can vary or is larger than an integer. */
1936 HOST_WIDE_INT
1937 int_size_in_bytes (const_tree type)
1939 tree t;
1941 if (type == error_mark_node)
1942 return 0;
1944 type = TYPE_MAIN_VARIANT (type);
1945 t = TYPE_SIZE_UNIT (type);
1946 if (t == 0
1947 || TREE_CODE (t) != INTEGER_CST
1948 || TREE_INT_CST_HIGH (t) != 0
1949 /* If the result would appear negative, it's too big to represent. */
1950 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
1951 return -1;
1953 return TREE_INT_CST_LOW (t);
1956 /* Return the maximum size of TYPE (in bytes) as a wide integer
1957 or return -1 if the size can vary or is larger than an integer. */
1959 HOST_WIDE_INT
1960 max_int_size_in_bytes (const_tree type)
1962 HOST_WIDE_INT size = -1;
1963 tree size_tree;
1965 /* If this is an array type, check for a possible MAX_SIZE attached. */
1967 if (TREE_CODE (type) == ARRAY_TYPE)
1969 size_tree = TYPE_ARRAY_MAX_SIZE (type);
1971 if (size_tree && host_integerp (size_tree, 1))
1972 size = tree_low_cst (size_tree, 1);
1975 /* If we still haven't been able to get a size, see if the language
1976 can compute a maximum size. */
1978 if (size == -1)
1980 size_tree = lang_hooks.types.max_size (type);
1982 if (size_tree && host_integerp (size_tree, 1))
1983 size = tree_low_cst (size_tree, 1);
1986 return size;
1989 /* Return the bit position of FIELD, in bits from the start of the record.
1990 This is a tree of type bitsizetype. */
1992 tree
1993 bit_position (const_tree field)
1995 return bit_from_pos (DECL_FIELD_OFFSET (field),
1996 DECL_FIELD_BIT_OFFSET (field));
1999 /* Likewise, but return as an integer. It must be representable in
2000 that way (since it could be a signed value, we don't have the
2001 option of returning -1 like int_size_in_byte can. */
2003 HOST_WIDE_INT
2004 int_bit_position (const_tree field)
2006 return tree_low_cst (bit_position (field), 0);
2009 /* Return the byte position of FIELD, in bytes from the start of the record.
2010 This is a tree of type sizetype. */
2012 tree
2013 byte_position (const_tree field)
2015 return byte_from_pos (DECL_FIELD_OFFSET (field),
2016 DECL_FIELD_BIT_OFFSET (field));
2019 /* Likewise, but return as an integer. It must be representable in
2020 that way (since it could be a signed value, we don't have the
2021 option of returning -1 like int_size_in_byte can. */
2023 HOST_WIDE_INT
2024 int_byte_position (const_tree field)
2026 return tree_low_cst (byte_position (field), 0);
2029 /* Return the strictest alignment, in bits, that T is known to have. */
2031 unsigned int
2032 expr_align (const_tree t)
2034 unsigned int align0, align1;
2036 switch (TREE_CODE (t))
2038 CASE_CONVERT: case NON_LVALUE_EXPR:
2039 /* If we have conversions, we know that the alignment of the
2040 object must meet each of the alignments of the types. */
2041 align0 = expr_align (TREE_OPERAND (t, 0));
2042 align1 = TYPE_ALIGN (TREE_TYPE (t));
2043 return MAX (align0, align1);
2045 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2046 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2047 case CLEANUP_POINT_EXPR:
2048 /* These don't change the alignment of an object. */
2049 return expr_align (TREE_OPERAND (t, 0));
2051 case COND_EXPR:
2052 /* The best we can do is say that the alignment is the least aligned
2053 of the two arms. */
2054 align0 = expr_align (TREE_OPERAND (t, 1));
2055 align1 = expr_align (TREE_OPERAND (t, 2));
2056 return MIN (align0, align1);
2058 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2059 meaningfully, it's always 1. */
2060 case LABEL_DECL: case CONST_DECL:
2061 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2062 case FUNCTION_DECL:
2063 gcc_assert (DECL_ALIGN (t) != 0);
2064 return DECL_ALIGN (t);
2066 default:
2067 break;
2070 /* Otherwise take the alignment from that of the type. */
2071 return TYPE_ALIGN (TREE_TYPE (t));
2074 /* Return, as a tree node, the number of elements for TYPE (which is an
2075 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2077 tree
2078 array_type_nelts (const_tree type)
2080 tree index_type, min, max;
2082 /* If they did it with unspecified bounds, then we should have already
2083 given an error about it before we got here. */
2084 if (! TYPE_DOMAIN (type))
2085 return error_mark_node;
2087 index_type = TYPE_DOMAIN (type);
2088 min = TYPE_MIN_VALUE (index_type);
2089 max = TYPE_MAX_VALUE (index_type);
2091 return (integer_zerop (min)
2092 ? max
2093 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2096 /* If arg is static -- a reference to an object in static storage -- then
2097 return the object. This is not the same as the C meaning of `static'.
2098 If arg isn't static, return NULL. */
2100 tree
2101 staticp (tree arg)
2103 switch (TREE_CODE (arg))
2105 case FUNCTION_DECL:
2106 /* Nested functions are static, even though taking their address will
2107 involve a trampoline as we unnest the nested function and create
2108 the trampoline on the tree level. */
2109 return arg;
2111 case VAR_DECL:
2112 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2113 && ! DECL_THREAD_LOCAL_P (arg)
2114 && ! DECL_DLLIMPORT_P (arg)
2115 ? arg : NULL);
2117 case CONST_DECL:
2118 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2119 ? arg : NULL);
2121 case CONSTRUCTOR:
2122 return TREE_STATIC (arg) ? arg : NULL;
2124 case LABEL_DECL:
2125 case STRING_CST:
2126 return arg;
2128 case COMPONENT_REF:
2129 /* If the thing being referenced is not a field, then it is
2130 something language specific. */
2131 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2133 /* If we are referencing a bitfield, we can't evaluate an
2134 ADDR_EXPR at compile time and so it isn't a constant. */
2135 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2136 return NULL;
2138 return staticp (TREE_OPERAND (arg, 0));
2140 case BIT_FIELD_REF:
2141 return NULL;
2143 case MISALIGNED_INDIRECT_REF:
2144 case ALIGN_INDIRECT_REF:
2145 case INDIRECT_REF:
2146 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2148 case ARRAY_REF:
2149 case ARRAY_RANGE_REF:
2150 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2151 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2152 return staticp (TREE_OPERAND (arg, 0));
2153 else
2154 return NULL;
2156 case COMPOUND_LITERAL_EXPR:
2157 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2159 default:
2160 return NULL;
2167 /* Return whether OP is a DECL whose address is function-invariant. */
2169 bool
2170 decl_address_invariant_p (const_tree op)
2172 /* The conditions below are slightly less strict than the one in
2173 staticp. */
2175 switch (TREE_CODE (op))
2177 case PARM_DECL:
2178 case RESULT_DECL:
2179 case LABEL_DECL:
2180 case FUNCTION_DECL:
2181 return true;
2183 case VAR_DECL:
2184 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2185 && !DECL_DLLIMPORT_P (op))
2186 || DECL_THREAD_LOCAL_P (op)
2187 || DECL_CONTEXT (op) == current_function_decl
2188 || decl_function_context (op) == current_function_decl)
2189 return true;
2190 break;
2192 case CONST_DECL:
2193 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2194 || decl_function_context (op) == current_function_decl)
2195 return true;
2196 break;
2198 default:
2199 break;
2202 return false;
2205 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2207 bool
2208 decl_address_ip_invariant_p (const_tree op)
2210 /* The conditions below are slightly less strict than the one in
2211 staticp. */
2213 switch (TREE_CODE (op))
2215 case LABEL_DECL:
2216 case FUNCTION_DECL:
2217 case STRING_CST:
2218 return true;
2220 case VAR_DECL:
2221 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2222 && !DECL_DLLIMPORT_P (op))
2223 || DECL_THREAD_LOCAL_P (op))
2224 return true;
2225 break;
2227 case CONST_DECL:
2228 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2229 return true;
2230 break;
2232 default:
2233 break;
2236 return false;
2240 /* Return true if T is function-invariant (internal function, does
2241 not handle arithmetic; that's handled in skip_simple_arithmetic and
2242 tree_invariant_p). */
2244 static bool tree_invariant_p (tree t);
2246 static bool
2247 tree_invariant_p_1 (tree t)
2249 tree op;
2251 if (TREE_CONSTANT (t)
2252 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2253 return true;
2255 switch (TREE_CODE (t))
2257 case SAVE_EXPR:
2258 return true;
2260 case ADDR_EXPR:
2261 op = TREE_OPERAND (t, 0);
2262 while (handled_component_p (op))
2264 switch (TREE_CODE (op))
2266 case ARRAY_REF:
2267 case ARRAY_RANGE_REF:
2268 if (!tree_invariant_p (TREE_OPERAND (op, 1))
2269 || TREE_OPERAND (op, 2) != NULL_TREE
2270 || TREE_OPERAND (op, 3) != NULL_TREE)
2271 return false;
2272 break;
2274 case COMPONENT_REF:
2275 if (TREE_OPERAND (op, 2) != NULL_TREE)
2276 return false;
2277 break;
2279 default:;
2281 op = TREE_OPERAND (op, 0);
2284 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2286 default:
2287 break;
2290 return false;
2293 /* Return true if T is function-invariant. */
2295 static bool
2296 tree_invariant_p (tree t)
2298 tree inner = skip_simple_arithmetic (t);
2299 return tree_invariant_p_1 (inner);
2302 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2303 Do this to any expression which may be used in more than one place,
2304 but must be evaluated only once.
2306 Normally, expand_expr would reevaluate the expression each time.
2307 Calling save_expr produces something that is evaluated and recorded
2308 the first time expand_expr is called on it. Subsequent calls to
2309 expand_expr just reuse the recorded value.
2311 The call to expand_expr that generates code that actually computes
2312 the value is the first call *at compile time*. Subsequent calls
2313 *at compile time* generate code to use the saved value.
2314 This produces correct result provided that *at run time* control
2315 always flows through the insns made by the first expand_expr
2316 before reaching the other places where the save_expr was evaluated.
2317 You, the caller of save_expr, must make sure this is so.
2319 Constants, and certain read-only nodes, are returned with no
2320 SAVE_EXPR because that is safe. Expressions containing placeholders
2321 are not touched; see tree.def for an explanation of what these
2322 are used for. */
2324 tree
2325 save_expr (tree expr)
2327 tree t = fold (expr);
2328 tree inner;
2330 /* If the tree evaluates to a constant, then we don't want to hide that
2331 fact (i.e. this allows further folding, and direct checks for constants).
2332 However, a read-only object that has side effects cannot be bypassed.
2333 Since it is no problem to reevaluate literals, we just return the
2334 literal node. */
2335 inner = skip_simple_arithmetic (t);
2336 if (TREE_CODE (inner) == ERROR_MARK)
2337 return inner;
2339 if (tree_invariant_p_1 (inner))
2340 return t;
2342 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2343 it means that the size or offset of some field of an object depends on
2344 the value within another field.
2346 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2347 and some variable since it would then need to be both evaluated once and
2348 evaluated more than once. Front-ends must assure this case cannot
2349 happen by surrounding any such subexpressions in their own SAVE_EXPR
2350 and forcing evaluation at the proper time. */
2351 if (contains_placeholder_p (inner))
2352 return t;
2354 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
2355 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
2357 /* This expression might be placed ahead of a jump to ensure that the
2358 value was computed on both sides of the jump. So make sure it isn't
2359 eliminated as dead. */
2360 TREE_SIDE_EFFECTS (t) = 1;
2361 return t;
2364 /* Look inside EXPR and into any simple arithmetic operations. Return
2365 the innermost non-arithmetic node. */
2367 tree
2368 skip_simple_arithmetic (tree expr)
2370 tree inner;
2372 /* We don't care about whether this can be used as an lvalue in this
2373 context. */
2374 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
2375 expr = TREE_OPERAND (expr, 0);
2377 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
2378 a constant, it will be more efficient to not make another SAVE_EXPR since
2379 it will allow better simplification and GCSE will be able to merge the
2380 computations if they actually occur. */
2381 inner = expr;
2382 while (1)
2384 if (UNARY_CLASS_P (inner))
2385 inner = TREE_OPERAND (inner, 0);
2386 else if (BINARY_CLASS_P (inner))
2388 if (tree_invariant_p (TREE_OPERAND (inner, 1)))
2389 inner = TREE_OPERAND (inner, 0);
2390 else if (tree_invariant_p (TREE_OPERAND (inner, 0)))
2391 inner = TREE_OPERAND (inner, 1);
2392 else
2393 break;
2395 else
2396 break;
2399 return inner;
2402 /* Return which tree structure is used by T. */
2404 enum tree_node_structure_enum
2405 tree_node_structure (const_tree t)
2407 const enum tree_code code = TREE_CODE (t);
2409 switch (TREE_CODE_CLASS (code))
2411 case tcc_declaration:
2413 switch (code)
2415 case FIELD_DECL:
2416 return TS_FIELD_DECL;
2417 case PARM_DECL:
2418 return TS_PARM_DECL;
2419 case VAR_DECL:
2420 return TS_VAR_DECL;
2421 case LABEL_DECL:
2422 return TS_LABEL_DECL;
2423 case RESULT_DECL:
2424 return TS_RESULT_DECL;
2425 case CONST_DECL:
2426 return TS_CONST_DECL;
2427 case TYPE_DECL:
2428 return TS_TYPE_DECL;
2429 case FUNCTION_DECL:
2430 return TS_FUNCTION_DECL;
2431 default:
2432 return TS_DECL_NON_COMMON;
2435 case tcc_type:
2436 return TS_TYPE;
2437 case tcc_reference:
2438 case tcc_comparison:
2439 case tcc_unary:
2440 case tcc_binary:
2441 case tcc_expression:
2442 case tcc_statement:
2443 case tcc_vl_exp:
2444 return TS_EXP;
2445 default: /* tcc_constant and tcc_exceptional */
2446 break;
2448 switch (code)
2450 /* tcc_constant cases. */
2451 case INTEGER_CST: return TS_INT_CST;
2452 case REAL_CST: return TS_REAL_CST;
2453 case FIXED_CST: return TS_FIXED_CST;
2454 case COMPLEX_CST: return TS_COMPLEX;
2455 case VECTOR_CST: return TS_VECTOR;
2456 case STRING_CST: return TS_STRING;
2457 /* tcc_exceptional cases. */
2458 case ERROR_MARK: return TS_COMMON;
2459 case IDENTIFIER_NODE: return TS_IDENTIFIER;
2460 case TREE_LIST: return TS_LIST;
2461 case TREE_VEC: return TS_VEC;
2462 case SSA_NAME: return TS_SSA_NAME;
2463 case PLACEHOLDER_EXPR: return TS_COMMON;
2464 case STATEMENT_LIST: return TS_STATEMENT_LIST;
2465 case BLOCK: return TS_BLOCK;
2466 case CONSTRUCTOR: return TS_CONSTRUCTOR;
2467 case TREE_BINFO: return TS_BINFO;
2468 case OMP_CLAUSE: return TS_OMP_CLAUSE;
2469 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
2470 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
2472 default:
2473 gcc_unreachable ();
2477 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
2478 or offset that depends on a field within a record. */
2480 bool
2481 contains_placeholder_p (const_tree exp)
2483 enum tree_code code;
2485 if (!exp)
2486 return 0;
2488 code = TREE_CODE (exp);
2489 if (code == PLACEHOLDER_EXPR)
2490 return 1;
2492 switch (TREE_CODE_CLASS (code))
2494 case tcc_reference:
2495 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
2496 position computations since they will be converted into a
2497 WITH_RECORD_EXPR involving the reference, which will assume
2498 here will be valid. */
2499 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2501 case tcc_exceptional:
2502 if (code == TREE_LIST)
2503 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
2504 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
2505 break;
2507 case tcc_unary:
2508 case tcc_binary:
2509 case tcc_comparison:
2510 case tcc_expression:
2511 switch (code)
2513 case COMPOUND_EXPR:
2514 /* Ignoring the first operand isn't quite right, but works best. */
2515 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
2517 case COND_EXPR:
2518 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2519 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
2520 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
2522 case SAVE_EXPR:
2523 /* The save_expr function never wraps anything containing
2524 a PLACEHOLDER_EXPR. */
2525 return 0;
2527 default:
2528 break;
2531 switch (TREE_CODE_LENGTH (code))
2533 case 1:
2534 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2535 case 2:
2536 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2537 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
2538 default:
2539 return 0;
2542 case tcc_vl_exp:
2543 switch (code)
2545 case CALL_EXPR:
2547 const_tree arg;
2548 const_call_expr_arg_iterator iter;
2549 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
2550 if (CONTAINS_PLACEHOLDER_P (arg))
2551 return 1;
2552 return 0;
2554 default:
2555 return 0;
2558 default:
2559 return 0;
2561 return 0;
2564 /* Return true if any part of the computation of TYPE involves a
2565 PLACEHOLDER_EXPR. This includes size, bounds, qualifiers
2566 (for QUAL_UNION_TYPE) and field positions. */
2568 static bool
2569 type_contains_placeholder_1 (const_tree type)
2571 /* If the size contains a placeholder or the parent type (component type in
2572 the case of arrays) type involves a placeholder, this type does. */
2573 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
2574 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
2575 || (TREE_TYPE (type) != 0
2576 && type_contains_placeholder_p (TREE_TYPE (type))))
2577 return true;
2579 /* Now do type-specific checks. Note that the last part of the check above
2580 greatly limits what we have to do below. */
2581 switch (TREE_CODE (type))
2583 case VOID_TYPE:
2584 case COMPLEX_TYPE:
2585 case ENUMERAL_TYPE:
2586 case BOOLEAN_TYPE:
2587 case POINTER_TYPE:
2588 case OFFSET_TYPE:
2589 case REFERENCE_TYPE:
2590 case METHOD_TYPE:
2591 case FUNCTION_TYPE:
2592 case VECTOR_TYPE:
2593 return false;
2595 case INTEGER_TYPE:
2596 case REAL_TYPE:
2597 case FIXED_POINT_TYPE:
2598 /* Here we just check the bounds. */
2599 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
2600 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
2602 case ARRAY_TYPE:
2603 /* We're already checked the component type (TREE_TYPE), so just check
2604 the index type. */
2605 return type_contains_placeholder_p (TYPE_DOMAIN (type));
2607 case RECORD_TYPE:
2608 case UNION_TYPE:
2609 case QUAL_UNION_TYPE:
2611 tree field;
2613 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2614 if (TREE_CODE (field) == FIELD_DECL
2615 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
2616 || (TREE_CODE (type) == QUAL_UNION_TYPE
2617 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
2618 || type_contains_placeholder_p (TREE_TYPE (field))))
2619 return true;
2621 return false;
2624 default:
2625 gcc_unreachable ();
2629 bool
2630 type_contains_placeholder_p (tree type)
2632 bool result;
2634 /* If the contains_placeholder_bits field has been initialized,
2635 then we know the answer. */
2636 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
2637 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
2639 /* Indicate that we've seen this type node, and the answer is false.
2640 This is what we want to return if we run into recursion via fields. */
2641 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
2643 /* Compute the real value. */
2644 result = type_contains_placeholder_1 (type);
2646 /* Store the real value. */
2647 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
2649 return result;
2652 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
2653 return a tree with all occurrences of references to F in a
2654 PLACEHOLDER_EXPR replaced by R. Note that we assume here that EXP
2655 contains only arithmetic expressions or a CALL_EXPR with a
2656 PLACEHOLDER_EXPR occurring only in its arglist. */
2658 tree
2659 substitute_in_expr (tree exp, tree f, tree r)
2661 enum tree_code code = TREE_CODE (exp);
2662 tree op0, op1, op2, op3;
2663 tree new_tree, inner;
2665 /* We handle TREE_LIST and COMPONENT_REF separately. */
2666 if (code == TREE_LIST)
2668 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
2669 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
2670 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2671 return exp;
2673 return tree_cons (TREE_PURPOSE (exp), op1, op0);
2675 else if (code == COMPONENT_REF)
2677 /* If this expression is getting a value from a PLACEHOLDER_EXPR
2678 and it is the right field, replace it with R. */
2679 for (inner = TREE_OPERAND (exp, 0);
2680 REFERENCE_CLASS_P (inner);
2681 inner = TREE_OPERAND (inner, 0))
2683 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2684 && TREE_OPERAND (exp, 1) == f)
2685 return r;
2687 /* If this expression hasn't been completed let, leave it alone. */
2688 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && TREE_TYPE (inner) == 0)
2689 return exp;
2691 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2692 if (op0 == TREE_OPERAND (exp, 0))
2693 return exp;
2695 new_tree = fold_build3 (COMPONENT_REF, TREE_TYPE (exp),
2696 op0, TREE_OPERAND (exp, 1), NULL_TREE);
2698 else
2699 switch (TREE_CODE_CLASS (code))
2701 case tcc_constant:
2702 case tcc_declaration:
2703 return exp;
2705 case tcc_exceptional:
2706 case tcc_unary:
2707 case tcc_binary:
2708 case tcc_comparison:
2709 case tcc_expression:
2710 case tcc_reference:
2711 switch (TREE_CODE_LENGTH (code))
2713 case 0:
2714 return exp;
2716 case 1:
2717 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2718 if (op0 == TREE_OPERAND (exp, 0))
2719 return exp;
2721 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
2722 break;
2724 case 2:
2725 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2726 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2728 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2729 return exp;
2731 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
2732 break;
2734 case 3:
2735 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2736 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2737 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
2739 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2740 && op2 == TREE_OPERAND (exp, 2))
2741 return exp;
2743 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
2744 break;
2746 case 4:
2747 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2748 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2749 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
2750 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
2752 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2753 && op2 == TREE_OPERAND (exp, 2)
2754 && op3 == TREE_OPERAND (exp, 3))
2755 return exp;
2757 new_tree = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
2758 break;
2760 default:
2761 gcc_unreachable ();
2763 break;
2765 case tcc_vl_exp:
2767 tree copy = NULL_TREE;
2768 int i;
2770 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
2772 tree op = TREE_OPERAND (exp, i);
2773 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
2774 if (new_op != op)
2776 if (!copy)
2777 copy = copy_node (exp);
2778 TREE_OPERAND (copy, i) = new_op;
2782 if (copy)
2783 new_tree = fold (copy);
2784 else
2785 return exp;
2787 break;
2789 default:
2790 gcc_unreachable ();
2793 TREE_READONLY (new_tree) = TREE_READONLY (exp);
2794 return new_tree;
2797 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
2798 for it within OBJ, a tree that is an object or a chain of references. */
2800 tree
2801 substitute_placeholder_in_expr (tree exp, tree obj)
2803 enum tree_code code = TREE_CODE (exp);
2804 tree op0, op1, op2, op3;
2806 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
2807 in the chain of OBJ. */
2808 if (code == PLACEHOLDER_EXPR)
2810 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
2811 tree elt;
2813 for (elt = obj; elt != 0;
2814 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
2815 || TREE_CODE (elt) == COND_EXPR)
2816 ? TREE_OPERAND (elt, 1)
2817 : (REFERENCE_CLASS_P (elt)
2818 || UNARY_CLASS_P (elt)
2819 || BINARY_CLASS_P (elt)
2820 || VL_EXP_CLASS_P (elt)
2821 || EXPRESSION_CLASS_P (elt))
2822 ? TREE_OPERAND (elt, 0) : 0))
2823 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
2824 return elt;
2826 for (elt = obj; elt != 0;
2827 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
2828 || TREE_CODE (elt) == COND_EXPR)
2829 ? TREE_OPERAND (elt, 1)
2830 : (REFERENCE_CLASS_P (elt)
2831 || UNARY_CLASS_P (elt)
2832 || BINARY_CLASS_P (elt)
2833 || VL_EXP_CLASS_P (elt)
2834 || EXPRESSION_CLASS_P (elt))
2835 ? TREE_OPERAND (elt, 0) : 0))
2836 if (POINTER_TYPE_P (TREE_TYPE (elt))
2837 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
2838 == need_type))
2839 return fold_build1 (INDIRECT_REF, need_type, elt);
2841 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
2842 survives until RTL generation, there will be an error. */
2843 return exp;
2846 /* TREE_LIST is special because we need to look at TREE_VALUE
2847 and TREE_CHAIN, not TREE_OPERANDS. */
2848 else if (code == TREE_LIST)
2850 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
2851 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
2852 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2853 return exp;
2855 return tree_cons (TREE_PURPOSE (exp), op1, op0);
2857 else
2858 switch (TREE_CODE_CLASS (code))
2860 case tcc_constant:
2861 case tcc_declaration:
2862 return exp;
2864 case tcc_exceptional:
2865 case tcc_unary:
2866 case tcc_binary:
2867 case tcc_comparison:
2868 case tcc_expression:
2869 case tcc_reference:
2870 case tcc_statement:
2871 switch (TREE_CODE_LENGTH (code))
2873 case 0:
2874 return exp;
2876 case 1:
2877 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2878 if (op0 == TREE_OPERAND (exp, 0))
2879 return exp;
2880 else
2881 return fold_build1 (code, TREE_TYPE (exp), op0);
2883 case 2:
2884 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2885 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2887 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2888 return exp;
2889 else
2890 return fold_build2 (code, TREE_TYPE (exp), op0, op1);
2892 case 3:
2893 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2894 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2895 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
2897 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2898 && op2 == TREE_OPERAND (exp, 2))
2899 return exp;
2900 else
2901 return fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
2903 case 4:
2904 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2905 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2906 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
2907 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
2909 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2910 && op2 == TREE_OPERAND (exp, 2)
2911 && op3 == TREE_OPERAND (exp, 3))
2912 return exp;
2913 else
2914 return fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
2916 default:
2917 gcc_unreachable ();
2919 break;
2921 case tcc_vl_exp:
2923 tree copy = NULL_TREE;
2924 int i;
2926 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
2928 tree op = TREE_OPERAND (exp, i);
2929 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
2930 if (new_op != op)
2932 if (!copy)
2933 copy = copy_node (exp);
2934 TREE_OPERAND (copy, i) = new_op;
2938 if (copy)
2939 return fold (copy);
2940 else
2941 return exp;
2944 default:
2945 gcc_unreachable ();
2949 /* Stabilize a reference so that we can use it any number of times
2950 without causing its operands to be evaluated more than once.
2951 Returns the stabilized reference. This works by means of save_expr,
2952 so see the caveats in the comments about save_expr.
2954 Also allows conversion expressions whose operands are references.
2955 Any other kind of expression is returned unchanged. */
2957 tree
2958 stabilize_reference (tree ref)
2960 tree result;
2961 enum tree_code code = TREE_CODE (ref);
2963 switch (code)
2965 case VAR_DECL:
2966 case PARM_DECL:
2967 case RESULT_DECL:
2968 /* No action is needed in this case. */
2969 return ref;
2971 CASE_CONVERT:
2972 case FLOAT_EXPR:
2973 case FIX_TRUNC_EXPR:
2974 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2975 break;
2977 case INDIRECT_REF:
2978 result = build_nt (INDIRECT_REF,
2979 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2980 break;
2982 case COMPONENT_REF:
2983 result = build_nt (COMPONENT_REF,
2984 stabilize_reference (TREE_OPERAND (ref, 0)),
2985 TREE_OPERAND (ref, 1), NULL_TREE);
2986 break;
2988 case BIT_FIELD_REF:
2989 result = build_nt (BIT_FIELD_REF,
2990 stabilize_reference (TREE_OPERAND (ref, 0)),
2991 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2992 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2993 break;
2995 case ARRAY_REF:
2996 result = build_nt (ARRAY_REF,
2997 stabilize_reference (TREE_OPERAND (ref, 0)),
2998 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2999 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3000 break;
3002 case ARRAY_RANGE_REF:
3003 result = build_nt (ARRAY_RANGE_REF,
3004 stabilize_reference (TREE_OPERAND (ref, 0)),
3005 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3006 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3007 break;
3009 case COMPOUND_EXPR:
3010 /* We cannot wrap the first expression in a SAVE_EXPR, as then
3011 it wouldn't be ignored. This matters when dealing with
3012 volatiles. */
3013 return stabilize_reference_1 (ref);
3015 /* If arg isn't a kind of lvalue we recognize, make no change.
3016 Caller should recognize the error for an invalid lvalue. */
3017 default:
3018 return ref;
3020 case ERROR_MARK:
3021 return error_mark_node;
3024 TREE_TYPE (result) = TREE_TYPE (ref);
3025 TREE_READONLY (result) = TREE_READONLY (ref);
3026 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
3027 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3029 return result;
3032 /* Subroutine of stabilize_reference; this is called for subtrees of
3033 references. Any expression with side-effects must be put in a SAVE_EXPR
3034 to ensure that it is only evaluated once.
3036 We don't put SAVE_EXPR nodes around everything, because assigning very
3037 simple expressions to temporaries causes us to miss good opportunities
3038 for optimizations. Among other things, the opportunity to fold in the
3039 addition of a constant into an addressing mode often gets lost, e.g.
3040 "y[i+1] += x;". In general, we take the approach that we should not make
3041 an assignment unless we are forced into it - i.e., that any non-side effect
3042 operator should be allowed, and that cse should take care of coalescing
3043 multiple utterances of the same expression should that prove fruitful. */
3045 tree
3046 stabilize_reference_1 (tree e)
3048 tree result;
3049 enum tree_code code = TREE_CODE (e);
3051 /* We cannot ignore const expressions because it might be a reference
3052 to a const array but whose index contains side-effects. But we can
3053 ignore things that are actual constant or that already have been
3054 handled by this function. */
3056 if (tree_invariant_p (e))
3057 return e;
3059 switch (TREE_CODE_CLASS (code))
3061 case tcc_exceptional:
3062 case tcc_type:
3063 case tcc_declaration:
3064 case tcc_comparison:
3065 case tcc_statement:
3066 case tcc_expression:
3067 case tcc_reference:
3068 case tcc_vl_exp:
3069 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3070 so that it will only be evaluated once. */
3071 /* The reference (r) and comparison (<) classes could be handled as
3072 below, but it is generally faster to only evaluate them once. */
3073 if (TREE_SIDE_EFFECTS (e))
3074 return save_expr (e);
3075 return e;
3077 case tcc_constant:
3078 /* Constants need no processing. In fact, we should never reach
3079 here. */
3080 return e;
3082 case tcc_binary:
3083 /* Division is slow and tends to be compiled with jumps,
3084 especially the division by powers of 2 that is often
3085 found inside of an array reference. So do it just once. */
3086 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3087 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3088 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3089 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3090 return save_expr (e);
3091 /* Recursively stabilize each operand. */
3092 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3093 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3094 break;
3096 case tcc_unary:
3097 /* Recursively stabilize each operand. */
3098 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3099 break;
3101 default:
3102 gcc_unreachable ();
3105 TREE_TYPE (result) = TREE_TYPE (e);
3106 TREE_READONLY (result) = TREE_READONLY (e);
3107 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3108 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3110 return result;
3113 /* Low-level constructors for expressions. */
3115 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
3116 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
3118 void
3119 recompute_tree_invariant_for_addr_expr (tree t)
3121 tree node;
3122 bool tc = true, se = false;
3124 /* We started out assuming this address is both invariant and constant, but
3125 does not have side effects. Now go down any handled components and see if
3126 any of them involve offsets that are either non-constant or non-invariant.
3127 Also check for side-effects.
3129 ??? Note that this code makes no attempt to deal with the case where
3130 taking the address of something causes a copy due to misalignment. */
3132 #define UPDATE_FLAGS(NODE) \
3133 do { tree _node = (NODE); \
3134 if (_node && !TREE_CONSTANT (_node)) tc = false; \
3135 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3137 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3138 node = TREE_OPERAND (node, 0))
3140 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3141 array reference (probably made temporarily by the G++ front end),
3142 so ignore all the operands. */
3143 if ((TREE_CODE (node) == ARRAY_REF
3144 || TREE_CODE (node) == ARRAY_RANGE_REF)
3145 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
3147 UPDATE_FLAGS (TREE_OPERAND (node, 1));
3148 if (TREE_OPERAND (node, 2))
3149 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3150 if (TREE_OPERAND (node, 3))
3151 UPDATE_FLAGS (TREE_OPERAND (node, 3));
3153 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
3154 FIELD_DECL, apparently. The G++ front end can put something else
3155 there, at least temporarily. */
3156 else if (TREE_CODE (node) == COMPONENT_REF
3157 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
3159 if (TREE_OPERAND (node, 2))
3160 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3162 else if (TREE_CODE (node) == BIT_FIELD_REF)
3163 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3166 node = lang_hooks.expr_to_decl (node, &tc, &se);
3168 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
3169 the address, since &(*a)->b is a form of addition. If it's a constant, the
3170 address is constant too. If it's a decl, its address is constant if the
3171 decl is static. Everything else is not constant and, furthermore,
3172 taking the address of a volatile variable is not volatile. */
3173 if (TREE_CODE (node) == INDIRECT_REF)
3174 UPDATE_FLAGS (TREE_OPERAND (node, 0));
3175 else if (CONSTANT_CLASS_P (node))
3177 else if (DECL_P (node))
3178 tc &= (staticp (node) != NULL_TREE);
3179 else
3181 tc = false;
3182 se |= TREE_SIDE_EFFECTS (node);
3186 TREE_CONSTANT (t) = tc;
3187 TREE_SIDE_EFFECTS (t) = se;
3188 #undef UPDATE_FLAGS
3191 /* Build an expression of code CODE, data type TYPE, and operands as
3192 specified. Expressions and reference nodes can be created this way.
3193 Constants, decls, types and misc nodes cannot be.
3195 We define 5 non-variadic functions, from 0 to 4 arguments. This is
3196 enough for all extant tree codes. */
3198 tree
3199 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
3201 tree t;
3203 gcc_assert (TREE_CODE_LENGTH (code) == 0);
3205 t = make_node_stat (code PASS_MEM_STAT);
3206 TREE_TYPE (t) = tt;
3208 return t;
3211 tree
3212 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
3214 int length = sizeof (struct tree_exp);
3215 #ifdef GATHER_STATISTICS
3216 tree_node_kind kind;
3217 #endif
3218 tree t;
3220 #ifdef GATHER_STATISTICS
3221 switch (TREE_CODE_CLASS (code))
3223 case tcc_statement: /* an expression with side effects */
3224 kind = s_kind;
3225 break;
3226 case tcc_reference: /* a reference */
3227 kind = r_kind;
3228 break;
3229 default:
3230 kind = e_kind;
3231 break;
3234 tree_node_counts[(int) kind]++;
3235 tree_node_sizes[(int) kind] += length;
3236 #endif
3238 gcc_assert (TREE_CODE_LENGTH (code) == 1);
3240 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
3242 memset (t, 0, sizeof (struct tree_common));
3244 TREE_SET_CODE (t, code);
3246 TREE_TYPE (t) = type;
3247 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
3248 TREE_OPERAND (t, 0) = node;
3249 TREE_BLOCK (t) = NULL_TREE;
3250 if (node && !TYPE_P (node))
3252 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
3253 TREE_READONLY (t) = TREE_READONLY (node);
3256 if (TREE_CODE_CLASS (code) == tcc_statement)
3257 TREE_SIDE_EFFECTS (t) = 1;
3258 else switch (code)
3260 case VA_ARG_EXPR:
3261 /* All of these have side-effects, no matter what their
3262 operands are. */
3263 TREE_SIDE_EFFECTS (t) = 1;
3264 TREE_READONLY (t) = 0;
3265 break;
3267 case MISALIGNED_INDIRECT_REF:
3268 case ALIGN_INDIRECT_REF:
3269 case INDIRECT_REF:
3270 /* Whether a dereference is readonly has nothing to do with whether
3271 its operand is readonly. */
3272 TREE_READONLY (t) = 0;
3273 break;
3275 case ADDR_EXPR:
3276 if (node)
3277 recompute_tree_invariant_for_addr_expr (t);
3278 break;
3280 default:
3281 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
3282 && node && !TYPE_P (node)
3283 && TREE_CONSTANT (node))
3284 TREE_CONSTANT (t) = 1;
3285 if (TREE_CODE_CLASS (code) == tcc_reference
3286 && node && TREE_THIS_VOLATILE (node))
3287 TREE_THIS_VOLATILE (t) = 1;
3288 break;
3291 return t;
3294 #define PROCESS_ARG(N) \
3295 do { \
3296 TREE_OPERAND (t, N) = arg##N; \
3297 if (arg##N &&!TYPE_P (arg##N)) \
3299 if (TREE_SIDE_EFFECTS (arg##N)) \
3300 side_effects = 1; \
3301 if (!TREE_READONLY (arg##N)) \
3302 read_only = 0; \
3303 if (!TREE_CONSTANT (arg##N)) \
3304 constant = 0; \
3306 } while (0)
3308 tree
3309 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
3311 bool constant, read_only, side_effects;
3312 tree t;
3314 gcc_assert (TREE_CODE_LENGTH (code) == 2);
3316 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
3317 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
3318 /* When sizetype precision doesn't match that of pointers
3319 we need to be able to build explicit extensions or truncations
3320 of the offset argument. */
3321 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
3322 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
3323 && TREE_CODE (arg1) == INTEGER_CST);
3325 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
3326 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
3327 && INTEGRAL_TYPE_P (TREE_TYPE (arg1))
3328 && useless_type_conversion_p (sizetype, TREE_TYPE (arg1)));
3330 t = make_node_stat (code PASS_MEM_STAT);
3331 TREE_TYPE (t) = tt;
3333 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
3334 result based on those same flags for the arguments. But if the
3335 arguments aren't really even `tree' expressions, we shouldn't be trying
3336 to do this. */
3338 /* Expressions without side effects may be constant if their
3339 arguments are as well. */
3340 constant = (TREE_CODE_CLASS (code) == tcc_comparison
3341 || TREE_CODE_CLASS (code) == tcc_binary);
3342 read_only = 1;
3343 side_effects = TREE_SIDE_EFFECTS (t);
3345 PROCESS_ARG(0);
3346 PROCESS_ARG(1);
3348 TREE_READONLY (t) = read_only;
3349 TREE_CONSTANT (t) = constant;
3350 TREE_SIDE_EFFECTS (t) = side_effects;
3351 TREE_THIS_VOLATILE (t)
3352 = (TREE_CODE_CLASS (code) == tcc_reference
3353 && arg0 && TREE_THIS_VOLATILE (arg0));
3355 return t;
3359 tree
3360 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3361 tree arg2 MEM_STAT_DECL)
3363 bool constant, read_only, side_effects;
3364 tree t;
3366 gcc_assert (TREE_CODE_LENGTH (code) == 3);
3367 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3369 t = make_node_stat (code PASS_MEM_STAT);
3370 TREE_TYPE (t) = tt;
3372 /* As a special exception, if COND_EXPR has NULL branches, we
3373 assume that it is a gimple statement and always consider
3374 it to have side effects. */
3375 if (code == COND_EXPR
3376 && tt == void_type_node
3377 && arg1 == NULL_TREE
3378 && arg2 == NULL_TREE)
3379 side_effects = true;
3380 else
3381 side_effects = TREE_SIDE_EFFECTS (t);
3383 PROCESS_ARG(0);
3384 PROCESS_ARG(1);
3385 PROCESS_ARG(2);
3387 TREE_SIDE_EFFECTS (t) = side_effects;
3388 TREE_THIS_VOLATILE (t)
3389 = (TREE_CODE_CLASS (code) == tcc_reference
3390 && arg0 && TREE_THIS_VOLATILE (arg0));
3392 return t;
3395 tree
3396 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3397 tree arg2, tree arg3 MEM_STAT_DECL)
3399 bool constant, read_only, side_effects;
3400 tree t;
3402 gcc_assert (TREE_CODE_LENGTH (code) == 4);
3404 t = make_node_stat (code PASS_MEM_STAT);
3405 TREE_TYPE (t) = tt;
3407 side_effects = TREE_SIDE_EFFECTS (t);
3409 PROCESS_ARG(0);
3410 PROCESS_ARG(1);
3411 PROCESS_ARG(2);
3412 PROCESS_ARG(3);
3414 TREE_SIDE_EFFECTS (t) = side_effects;
3415 TREE_THIS_VOLATILE (t)
3416 = (TREE_CODE_CLASS (code) == tcc_reference
3417 && arg0 && TREE_THIS_VOLATILE (arg0));
3419 return t;
3422 tree
3423 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3424 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
3426 bool constant, read_only, side_effects;
3427 tree t;
3429 gcc_assert (TREE_CODE_LENGTH (code) == 5);
3431 t = make_node_stat (code PASS_MEM_STAT);
3432 TREE_TYPE (t) = tt;
3434 side_effects = TREE_SIDE_EFFECTS (t);
3436 PROCESS_ARG(0);
3437 PROCESS_ARG(1);
3438 PROCESS_ARG(2);
3439 PROCESS_ARG(3);
3440 PROCESS_ARG(4);
3442 TREE_SIDE_EFFECTS (t) = side_effects;
3443 TREE_THIS_VOLATILE (t)
3444 = (TREE_CODE_CLASS (code) == tcc_reference
3445 && arg0 && TREE_THIS_VOLATILE (arg0));
3447 return t;
3450 tree
3451 build6_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3452 tree arg2, tree arg3, tree arg4, tree arg5 MEM_STAT_DECL)
3454 bool constant, read_only, side_effects;
3455 tree t;
3457 gcc_assert (code == TARGET_MEM_REF);
3459 t = make_node_stat (code PASS_MEM_STAT);
3460 TREE_TYPE (t) = tt;
3462 side_effects = TREE_SIDE_EFFECTS (t);
3464 PROCESS_ARG(0);
3465 PROCESS_ARG(1);
3466 PROCESS_ARG(2);
3467 PROCESS_ARG(3);
3468 PROCESS_ARG(4);
3469 PROCESS_ARG(5);
3471 TREE_SIDE_EFFECTS (t) = side_effects;
3472 TREE_THIS_VOLATILE (t) = 0;
3474 return t;
3477 /* Similar except don't specify the TREE_TYPE
3478 and leave the TREE_SIDE_EFFECTS as 0.
3479 It is permissible for arguments to be null,
3480 or even garbage if their values do not matter. */
3482 tree
3483 build_nt (enum tree_code code, ...)
3485 tree t;
3486 int length;
3487 int i;
3488 va_list p;
3490 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3492 va_start (p, code);
3494 t = make_node (code);
3495 length = TREE_CODE_LENGTH (code);
3497 for (i = 0; i < length; i++)
3498 TREE_OPERAND (t, i) = va_arg (p, tree);
3500 va_end (p);
3501 return t;
3504 /* Similar to build_nt, but for creating a CALL_EXPR object with
3505 ARGLIST passed as a list. */
3507 tree
3508 build_nt_call_list (tree fn, tree arglist)
3510 tree t;
3511 int i;
3513 t = build_vl_exp (CALL_EXPR, list_length (arglist) + 3);
3514 CALL_EXPR_FN (t) = fn;
3515 CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
3516 for (i = 0; arglist; arglist = TREE_CHAIN (arglist), i++)
3517 CALL_EXPR_ARG (t, i) = TREE_VALUE (arglist);
3518 return t;
3521 /* Similar to build_nt, but for creating a CALL_EXPR object with a
3522 tree VEC. */
3524 tree
3525 build_nt_call_vec (tree fn, VEC(tree,gc) *args)
3527 tree ret, t;
3528 unsigned int ix;
3530 ret = build_vl_exp (CALL_EXPR, VEC_length (tree, args) + 3);
3531 CALL_EXPR_FN (ret) = fn;
3532 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
3533 for (ix = 0; VEC_iterate (tree, args, ix, t); ++ix)
3534 CALL_EXPR_ARG (ret, ix) = t;
3535 return ret;
3538 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
3539 We do NOT enter this node in any sort of symbol table.
3541 LOC is the location of the decl.
3543 layout_decl is used to set up the decl's storage layout.
3544 Other slots are initialized to 0 or null pointers. */
3546 tree
3547 build_decl_stat (location_t loc, enum tree_code code, tree name,
3548 tree type MEM_STAT_DECL)
3550 tree t;
3552 t = make_node_stat (code PASS_MEM_STAT);
3553 DECL_SOURCE_LOCATION (t) = loc;
3555 /* if (type == error_mark_node)
3556 type = integer_type_node; */
3557 /* That is not done, deliberately, so that having error_mark_node
3558 as the type can suppress useless errors in the use of this variable. */
3560 DECL_NAME (t) = name;
3561 TREE_TYPE (t) = type;
3563 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
3564 layout_decl (t, 0);
3566 return t;
3569 /* Builds and returns function declaration with NAME and TYPE. */
3571 tree
3572 build_fn_decl (const char *name, tree type)
3574 tree id = get_identifier (name);
3575 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
3577 DECL_EXTERNAL (decl) = 1;
3578 TREE_PUBLIC (decl) = 1;
3579 DECL_ARTIFICIAL (decl) = 1;
3580 TREE_NOTHROW (decl) = 1;
3582 return decl;
3586 /* BLOCK nodes are used to represent the structure of binding contours
3587 and declarations, once those contours have been exited and their contents
3588 compiled. This information is used for outputting debugging info. */
3590 tree
3591 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
3593 tree block = make_node (BLOCK);
3595 BLOCK_VARS (block) = vars;
3596 BLOCK_SUBBLOCKS (block) = subblocks;
3597 BLOCK_SUPERCONTEXT (block) = supercontext;
3598 BLOCK_CHAIN (block) = chain;
3599 return block;
3602 expanded_location
3603 expand_location (source_location loc)
3605 expanded_location xloc;
3606 if (loc == 0)
3608 xloc.file = NULL;
3609 xloc.line = 0;
3610 xloc.column = 0;
3611 xloc.sysp = 0;
3613 else
3615 const struct line_map *map = linemap_lookup (line_table, loc);
3616 xloc.file = map->to_file;
3617 xloc.line = SOURCE_LINE (map, loc);
3618 xloc.column = SOURCE_COLUMN (map, loc);
3619 xloc.sysp = map->sysp != 0;
3621 return xloc;
3625 /* Source location accessor functions. */
3628 void
3629 set_expr_locus (tree node, source_location *loc)
3631 if (loc == NULL)
3632 EXPR_CHECK (node)->exp.locus = UNKNOWN_LOCATION;
3633 else
3634 EXPR_CHECK (node)->exp.locus = *loc;
3637 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
3639 LOC is the location to use in tree T. */
3641 void
3642 protected_set_expr_location (tree t, location_t loc)
3644 if (t && CAN_HAVE_LOCATION_P (t))
3645 SET_EXPR_LOCATION (t, loc);
3648 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
3649 is ATTRIBUTE. */
3651 tree
3652 build_decl_attribute_variant (tree ddecl, tree attribute)
3654 DECL_ATTRIBUTES (ddecl) = attribute;
3655 return ddecl;
3658 /* Borrowed from hashtab.c iterative_hash implementation. */
3659 #define mix(a,b,c) \
3661 a -= b; a -= c; a ^= (c>>13); \
3662 b -= c; b -= a; b ^= (a<< 8); \
3663 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
3664 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
3665 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
3666 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
3667 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
3668 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
3669 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
3673 /* Produce good hash value combining VAL and VAL2. */
3674 hashval_t
3675 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
3677 /* the golden ratio; an arbitrary value. */
3678 hashval_t a = 0x9e3779b9;
3680 mix (a, val, val2);
3681 return val2;
3684 /* Produce good hash value combining VAL and VAL2. */
3685 static inline hashval_t
3686 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
3688 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
3689 return iterative_hash_hashval_t (val, val2);
3690 else
3692 hashval_t a = (hashval_t) val;
3693 /* Avoid warnings about shifting of more than the width of the type on
3694 hosts that won't execute this path. */
3695 int zero = 0;
3696 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
3697 mix (a, b, val2);
3698 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
3700 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
3701 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
3702 mix (a, b, val2);
3704 return val2;
3708 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
3709 is ATTRIBUTE and its qualifiers are QUALS.
3711 Record such modified types already made so we don't make duplicates. */
3713 static tree
3714 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
3716 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
3718 hashval_t hashcode = 0;
3719 tree ntype;
3720 enum tree_code code = TREE_CODE (ttype);
3722 /* Building a distinct copy of a tagged type is inappropriate; it
3723 causes breakage in code that expects there to be a one-to-one
3724 relationship between a struct and its fields.
3725 build_duplicate_type is another solution (as used in
3726 handle_transparent_union_attribute), but that doesn't play well
3727 with the stronger C++ type identity model. */
3728 if (TREE_CODE (ttype) == RECORD_TYPE
3729 || TREE_CODE (ttype) == UNION_TYPE
3730 || TREE_CODE (ttype) == QUAL_UNION_TYPE
3731 || TREE_CODE (ttype) == ENUMERAL_TYPE)
3733 warning (OPT_Wattributes,
3734 "ignoring attributes applied to %qT after definition",
3735 TYPE_MAIN_VARIANT (ttype));
3736 return build_qualified_type (ttype, quals);
3739 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
3740 ntype = build_distinct_type_copy (ttype);
3742 TYPE_ATTRIBUTES (ntype) = attribute;
3744 hashcode = iterative_hash_object (code, hashcode);
3745 if (TREE_TYPE (ntype))
3746 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
3747 hashcode);
3748 hashcode = attribute_hash_list (attribute, hashcode);
3750 switch (TREE_CODE (ntype))
3752 case FUNCTION_TYPE:
3753 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
3754 break;
3755 case ARRAY_TYPE:
3756 if (TYPE_DOMAIN (ntype))
3757 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
3758 hashcode);
3759 break;
3760 case INTEGER_TYPE:
3761 hashcode = iterative_hash_object
3762 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
3763 hashcode = iterative_hash_object
3764 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
3765 break;
3766 case REAL_TYPE:
3767 case FIXED_POINT_TYPE:
3769 unsigned int precision = TYPE_PRECISION (ntype);
3770 hashcode = iterative_hash_object (precision, hashcode);
3772 break;
3773 default:
3774 break;
3777 ntype = type_hash_canon (hashcode, ntype);
3779 /* If the target-dependent attributes make NTYPE different from
3780 its canonical type, we will need to use structural equality
3781 checks for this type. */
3782 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
3783 || !targetm.comp_type_attributes (ntype, ttype))
3784 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
3785 else if (TYPE_CANONICAL (ntype) == ntype)
3786 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
3788 ttype = build_qualified_type (ntype, quals);
3790 else if (TYPE_QUALS (ttype) != quals)
3791 ttype = build_qualified_type (ttype, quals);
3793 return ttype;
3797 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
3798 is ATTRIBUTE.
3800 Record such modified types already made so we don't make duplicates. */
3802 tree
3803 build_type_attribute_variant (tree ttype, tree attribute)
3805 return build_type_attribute_qual_variant (ttype, attribute,
3806 TYPE_QUALS (ttype));
3809 /* Return nonzero if IDENT is a valid name for attribute ATTR,
3810 or zero if not.
3812 We try both `text' and `__text__', ATTR may be either one. */
3813 /* ??? It might be a reasonable simplification to require ATTR to be only
3814 `text'. One might then also require attribute lists to be stored in
3815 their canonicalized form. */
3817 static int
3818 is_attribute_with_length_p (const char *attr, int attr_len, const_tree ident)
3820 int ident_len;
3821 const char *p;
3823 if (TREE_CODE (ident) != IDENTIFIER_NODE)
3824 return 0;
3826 p = IDENTIFIER_POINTER (ident);
3827 ident_len = IDENTIFIER_LENGTH (ident);
3829 if (ident_len == attr_len
3830 && strcmp (attr, p) == 0)
3831 return 1;
3833 /* If ATTR is `__text__', IDENT must be `text'; and vice versa. */
3834 if (attr[0] == '_')
3836 gcc_assert (attr[1] == '_');
3837 gcc_assert (attr[attr_len - 2] == '_');
3838 gcc_assert (attr[attr_len - 1] == '_');
3839 if (ident_len == attr_len - 4
3840 && strncmp (attr + 2, p, attr_len - 4) == 0)
3841 return 1;
3843 else
3845 if (ident_len == attr_len + 4
3846 && p[0] == '_' && p[1] == '_'
3847 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
3848 && strncmp (attr, p + 2, attr_len) == 0)
3849 return 1;
3852 return 0;
3855 /* Return nonzero if IDENT is a valid name for attribute ATTR,
3856 or zero if not.
3858 We try both `text' and `__text__', ATTR may be either one. */
3861 is_attribute_p (const char *attr, const_tree ident)
3863 return is_attribute_with_length_p (attr, strlen (attr), ident);
3866 /* Given an attribute name and a list of attributes, return a pointer to the
3867 attribute's list element if the attribute is part of the list, or NULL_TREE
3868 if not found. If the attribute appears more than once, this only
3869 returns the first occurrence; the TREE_CHAIN of the return value should
3870 be passed back in if further occurrences are wanted. */
3872 tree
3873 lookup_attribute (const char *attr_name, tree list)
3875 tree l;
3876 size_t attr_len = strlen (attr_name);
3878 for (l = list; l; l = TREE_CHAIN (l))
3880 gcc_assert (TREE_CODE (TREE_PURPOSE (l)) == IDENTIFIER_NODE);
3881 if (is_attribute_with_length_p (attr_name, attr_len, TREE_PURPOSE (l)))
3882 return l;
3884 return NULL_TREE;
3887 /* Remove any instances of attribute ATTR_NAME in LIST and return the
3888 modified list. */
3890 tree
3891 remove_attribute (const char *attr_name, tree list)
3893 tree *p;
3894 size_t attr_len = strlen (attr_name);
3896 for (p = &list; *p; )
3898 tree l = *p;
3899 gcc_assert (TREE_CODE (TREE_PURPOSE (l)) == IDENTIFIER_NODE);
3900 if (is_attribute_with_length_p (attr_name, attr_len, TREE_PURPOSE (l)))
3901 *p = TREE_CHAIN (l);
3902 else
3903 p = &TREE_CHAIN (l);
3906 return list;
3909 /* Return an attribute list that is the union of a1 and a2. */
3911 tree
3912 merge_attributes (tree a1, tree a2)
3914 tree attributes;
3916 /* Either one unset? Take the set one. */
3918 if ((attributes = a1) == 0)
3919 attributes = a2;
3921 /* One that completely contains the other? Take it. */
3923 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
3925 if (attribute_list_contained (a2, a1))
3926 attributes = a2;
3927 else
3929 /* Pick the longest list, and hang on the other list. */
3931 if (list_length (a1) < list_length (a2))
3932 attributes = a2, a2 = a1;
3934 for (; a2 != 0; a2 = TREE_CHAIN (a2))
3936 tree a;
3937 for (a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
3938 attributes);
3939 a != NULL_TREE;
3940 a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
3941 TREE_CHAIN (a)))
3943 if (TREE_VALUE (a) != NULL
3944 && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
3945 && TREE_VALUE (a2) != NULL
3946 && TREE_CODE (TREE_VALUE (a2)) == TREE_LIST)
3948 if (simple_cst_list_equal (TREE_VALUE (a),
3949 TREE_VALUE (a2)) == 1)
3950 break;
3952 else if (simple_cst_equal (TREE_VALUE (a),
3953 TREE_VALUE (a2)) == 1)
3954 break;
3956 if (a == NULL_TREE)
3958 a1 = copy_node (a2);
3959 TREE_CHAIN (a1) = attributes;
3960 attributes = a1;
3965 return attributes;
3968 /* Given types T1 and T2, merge their attributes and return
3969 the result. */
3971 tree
3972 merge_type_attributes (tree t1, tree t2)
3974 return merge_attributes (TYPE_ATTRIBUTES (t1),
3975 TYPE_ATTRIBUTES (t2));
3978 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
3979 the result. */
3981 tree
3982 merge_decl_attributes (tree olddecl, tree newdecl)
3984 return merge_attributes (DECL_ATTRIBUTES (olddecl),
3985 DECL_ATTRIBUTES (newdecl));
3988 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
3990 /* Specialization of merge_decl_attributes for various Windows targets.
3992 This handles the following situation:
3994 __declspec (dllimport) int foo;
3995 int foo;
3997 The second instance of `foo' nullifies the dllimport. */
3999 tree
4000 merge_dllimport_decl_attributes (tree old, tree new_tree)
4002 tree a;
4003 int delete_dllimport_p = 1;
4005 /* What we need to do here is remove from `old' dllimport if it doesn't
4006 appear in `new'. dllimport behaves like extern: if a declaration is
4007 marked dllimport and a definition appears later, then the object
4008 is not dllimport'd. We also remove a `new' dllimport if the old list
4009 contains dllexport: dllexport always overrides dllimport, regardless
4010 of the order of declaration. */
4011 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
4012 delete_dllimport_p = 0;
4013 else if (DECL_DLLIMPORT_P (new_tree)
4014 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
4016 DECL_DLLIMPORT_P (new_tree) = 0;
4017 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
4018 "dllimport ignored", new_tree);
4020 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
4022 /* Warn about overriding a symbol that has already been used, e.g.:
4023 extern int __attribute__ ((dllimport)) foo;
4024 int* bar () {return &foo;}
4025 int foo;
4027 if (TREE_USED (old))
4029 warning (0, "%q+D redeclared without dllimport attribute "
4030 "after being referenced with dll linkage", new_tree);
4031 /* If we have used a variable's address with dllimport linkage,
4032 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
4033 decl may already have had TREE_CONSTANT computed.
4034 We still remove the attribute so that assembler code refers
4035 to '&foo rather than '_imp__foo'. */
4036 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
4037 DECL_DLLIMPORT_P (new_tree) = 1;
4040 /* Let an inline definition silently override the external reference,
4041 but otherwise warn about attribute inconsistency. */
4042 else if (TREE_CODE (new_tree) == VAR_DECL
4043 || !DECL_DECLARED_INLINE_P (new_tree))
4044 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
4045 "previous dllimport ignored", new_tree);
4047 else
4048 delete_dllimport_p = 0;
4050 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
4052 if (delete_dllimport_p)
4054 tree prev, t;
4055 const size_t attr_len = strlen ("dllimport");
4057 /* Scan the list for dllimport and delete it. */
4058 for (prev = NULL_TREE, t = a; t; prev = t, t = TREE_CHAIN (t))
4060 if (is_attribute_with_length_p ("dllimport", attr_len,
4061 TREE_PURPOSE (t)))
4063 if (prev == NULL_TREE)
4064 a = TREE_CHAIN (a);
4065 else
4066 TREE_CHAIN (prev) = TREE_CHAIN (t);
4067 break;
4072 return a;
4075 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
4076 struct attribute_spec.handler. */
4078 tree
4079 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
4080 bool *no_add_attrs)
4082 tree node = *pnode;
4083 bool is_dllimport;
4085 /* These attributes may apply to structure and union types being created,
4086 but otherwise should pass to the declaration involved. */
4087 if (!DECL_P (node))
4089 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
4090 | (int) ATTR_FLAG_ARRAY_NEXT))
4092 *no_add_attrs = true;
4093 return tree_cons (name, args, NULL_TREE);
4095 if (TREE_CODE (node) == RECORD_TYPE
4096 || TREE_CODE (node) == UNION_TYPE)
4098 node = TYPE_NAME (node);
4099 if (!node)
4100 return NULL_TREE;
4102 else
4104 warning (OPT_Wattributes, "%qE attribute ignored",
4105 name);
4106 *no_add_attrs = true;
4107 return NULL_TREE;
4111 if (TREE_CODE (node) != FUNCTION_DECL
4112 && TREE_CODE (node) != VAR_DECL
4113 && TREE_CODE (node) != TYPE_DECL)
4115 *no_add_attrs = true;
4116 warning (OPT_Wattributes, "%qE attribute ignored",
4117 name);
4118 return NULL_TREE;
4121 if (TREE_CODE (node) == TYPE_DECL
4122 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
4123 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
4125 *no_add_attrs = true;
4126 warning (OPT_Wattributes, "%qE attribute ignored",
4127 name);
4128 return NULL_TREE;
4131 is_dllimport = is_attribute_p ("dllimport", name);
4133 /* Report error on dllimport ambiguities seen now before they cause
4134 any damage. */
4135 if (is_dllimport)
4137 /* Honor any target-specific overrides. */
4138 if (!targetm.valid_dllimport_attribute_p (node))
4139 *no_add_attrs = true;
4141 else if (TREE_CODE (node) == FUNCTION_DECL
4142 && DECL_DECLARED_INLINE_P (node))
4144 warning (OPT_Wattributes, "inline function %q+D declared as "
4145 " dllimport: attribute ignored", node);
4146 *no_add_attrs = true;
4148 /* Like MS, treat definition of dllimported variables and
4149 non-inlined functions on declaration as syntax errors. */
4150 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
4152 error ("function %q+D definition is marked dllimport", node);
4153 *no_add_attrs = true;
4156 else if (TREE_CODE (node) == VAR_DECL)
4158 if (DECL_INITIAL (node))
4160 error ("variable %q+D definition is marked dllimport",
4161 node);
4162 *no_add_attrs = true;
4165 /* `extern' needn't be specified with dllimport.
4166 Specify `extern' now and hope for the best. Sigh. */
4167 DECL_EXTERNAL (node) = 1;
4168 /* Also, implicitly give dllimport'd variables declared within
4169 a function global scope, unless declared static. */
4170 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
4171 TREE_PUBLIC (node) = 1;
4174 if (*no_add_attrs == false)
4175 DECL_DLLIMPORT_P (node) = 1;
4177 else if (TREE_CODE (node) == FUNCTION_DECL
4178 && DECL_DECLARED_INLINE_P (node))
4179 /* An exported function, even if inline, must be emitted. */
4180 DECL_EXTERNAL (node) = 0;
4182 /* Report error if symbol is not accessible at global scope. */
4183 if (!TREE_PUBLIC (node)
4184 && (TREE_CODE (node) == VAR_DECL
4185 || TREE_CODE (node) == FUNCTION_DECL))
4187 error ("external linkage required for symbol %q+D because of "
4188 "%qE attribute", node, name);
4189 *no_add_attrs = true;
4192 /* A dllexport'd entity must have default visibility so that other
4193 program units (shared libraries or the main executable) can see
4194 it. A dllimport'd entity must have default visibility so that
4195 the linker knows that undefined references within this program
4196 unit can be resolved by the dynamic linker. */
4197 if (!*no_add_attrs)
4199 if (DECL_VISIBILITY_SPECIFIED (node)
4200 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
4201 error ("%qE implies default visibility, but %qD has already "
4202 "been declared with a different visibility",
4203 name, node);
4204 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
4205 DECL_VISIBILITY_SPECIFIED (node) = 1;
4208 return NULL_TREE;
4211 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
4213 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
4214 of the various TYPE_QUAL values. */
4216 static void
4217 set_type_quals (tree type, int type_quals)
4219 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
4220 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
4221 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
4224 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
4226 bool
4227 check_qualified_type (const_tree cand, const_tree base, int type_quals)
4229 return (TYPE_QUALS (cand) == type_quals
4230 && TYPE_NAME (cand) == TYPE_NAME (base)
4231 /* Apparently this is needed for Objective-C. */
4232 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
4233 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
4234 TYPE_ATTRIBUTES (base)));
4237 /* Return a version of the TYPE, qualified as indicated by the
4238 TYPE_QUALS, if one exists. If no qualified version exists yet,
4239 return NULL_TREE. */
4241 tree
4242 get_qualified_type (tree type, int type_quals)
4244 tree t;
4246 if (TYPE_QUALS (type) == type_quals)
4247 return type;
4249 /* Search the chain of variants to see if there is already one there just
4250 like the one we need to have. If so, use that existing one. We must
4251 preserve the TYPE_NAME, since there is code that depends on this. */
4252 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
4253 if (check_qualified_type (t, type, type_quals))
4254 return t;
4256 return NULL_TREE;
4259 /* Like get_qualified_type, but creates the type if it does not
4260 exist. This function never returns NULL_TREE. */
4262 tree
4263 build_qualified_type (tree type, int type_quals)
4265 tree t;
4267 /* See if we already have the appropriate qualified variant. */
4268 t = get_qualified_type (type, type_quals);
4270 /* If not, build it. */
4271 if (!t)
4273 t = build_variant_type_copy (type);
4274 set_type_quals (t, type_quals);
4276 if (TYPE_STRUCTURAL_EQUALITY_P (type))
4277 /* Propagate structural equality. */
4278 SET_TYPE_STRUCTURAL_EQUALITY (t);
4279 else if (TYPE_CANONICAL (type) != type)
4280 /* Build the underlying canonical type, since it is different
4281 from TYPE. */
4282 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
4283 type_quals);
4284 else
4285 /* T is its own canonical type. */
4286 TYPE_CANONICAL (t) = t;
4290 return t;
4293 /* Create a new distinct copy of TYPE. The new type is made its own
4294 MAIN_VARIANT. If TYPE requires structural equality checks, the
4295 resulting type requires structural equality checks; otherwise, its
4296 TYPE_CANONICAL points to itself. */
4298 tree
4299 build_distinct_type_copy (tree type)
4301 tree t = copy_node (type);
4303 TYPE_POINTER_TO (t) = 0;
4304 TYPE_REFERENCE_TO (t) = 0;
4306 /* Set the canonical type either to a new equivalence class, or
4307 propagate the need for structural equality checks. */
4308 if (TYPE_STRUCTURAL_EQUALITY_P (type))
4309 SET_TYPE_STRUCTURAL_EQUALITY (t);
4310 else
4311 TYPE_CANONICAL (t) = t;
4313 /* Make it its own variant. */
4314 TYPE_MAIN_VARIANT (t) = t;
4315 TYPE_NEXT_VARIANT (t) = 0;
4317 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
4318 whose TREE_TYPE is not t. This can also happen in the Ada
4319 frontend when using subtypes. */
4321 return t;
4324 /* Create a new variant of TYPE, equivalent but distinct. This is so
4325 the caller can modify it. TYPE_CANONICAL for the return type will
4326 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
4327 are considered equal by the language itself (or that both types
4328 require structural equality checks). */
4330 tree
4331 build_variant_type_copy (tree type)
4333 tree t, m = TYPE_MAIN_VARIANT (type);
4335 t = build_distinct_type_copy (type);
4337 /* Since we're building a variant, assume that it is a non-semantic
4338 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
4339 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
4341 /* Add the new type to the chain of variants of TYPE. */
4342 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
4343 TYPE_NEXT_VARIANT (m) = t;
4344 TYPE_MAIN_VARIANT (t) = m;
4346 return t;
4349 /* Return true if the from tree in both tree maps are equal. */
4352 tree_map_base_eq (const void *va, const void *vb)
4354 const struct tree_map_base *const a = (const struct tree_map_base *) va,
4355 *const b = (const struct tree_map_base *) vb;
4356 return (a->from == b->from);
4359 /* Hash a from tree in a tree_map. */
4361 unsigned int
4362 tree_map_base_hash (const void *item)
4364 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
4367 /* Return true if this tree map structure is marked for garbage collection
4368 purposes. We simply return true if the from tree is marked, so that this
4369 structure goes away when the from tree goes away. */
4372 tree_map_base_marked_p (const void *p)
4374 return ggc_marked_p (((const struct tree_map_base *) p)->from);
4377 unsigned int
4378 tree_map_hash (const void *item)
4380 return (((const struct tree_map *) item)->hash);
4383 /* Return the initialization priority for DECL. */
4385 priority_type
4386 decl_init_priority_lookup (tree decl)
4388 struct tree_priority_map *h;
4389 struct tree_map_base in;
4391 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
4392 in.from = decl;
4393 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
4394 return h ? h->init : DEFAULT_INIT_PRIORITY;
4397 /* Return the finalization priority for DECL. */
4399 priority_type
4400 decl_fini_priority_lookup (tree decl)
4402 struct tree_priority_map *h;
4403 struct tree_map_base in;
4405 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4406 in.from = decl;
4407 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
4408 return h ? h->fini : DEFAULT_INIT_PRIORITY;
4411 /* Return the initialization and finalization priority information for
4412 DECL. If there is no previous priority information, a freshly
4413 allocated structure is returned. */
4415 static struct tree_priority_map *
4416 decl_priority_info (tree decl)
4418 struct tree_priority_map in;
4419 struct tree_priority_map *h;
4420 void **loc;
4422 in.base.from = decl;
4423 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
4424 h = (struct tree_priority_map *) *loc;
4425 if (!h)
4427 h = GGC_CNEW (struct tree_priority_map);
4428 *loc = h;
4429 h->base.from = decl;
4430 h->init = DEFAULT_INIT_PRIORITY;
4431 h->fini = DEFAULT_INIT_PRIORITY;
4434 return h;
4437 /* Set the initialization priority for DECL to PRIORITY. */
4439 void
4440 decl_init_priority_insert (tree decl, priority_type priority)
4442 struct tree_priority_map *h;
4444 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
4445 h = decl_priority_info (decl);
4446 h->init = priority;
4449 /* Set the finalization priority for DECL to PRIORITY. */
4451 void
4452 decl_fini_priority_insert (tree decl, priority_type priority)
4454 struct tree_priority_map *h;
4456 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4457 h = decl_priority_info (decl);
4458 h->fini = priority;
4461 /* Look up a restrict qualified base decl for FROM. */
4463 tree
4464 decl_restrict_base_lookup (tree from)
4466 struct tree_map *h;
4467 struct tree_map in;
4469 in.base.from = from;
4470 h = (struct tree_map *) htab_find_with_hash (restrict_base_for_decl, &in,
4471 htab_hash_pointer (from));
4472 return h ? h->to : NULL_TREE;
4475 /* Record the restrict qualified base TO for FROM. */
4477 void
4478 decl_restrict_base_insert (tree from, tree to)
4480 struct tree_map *h;
4481 void **loc;
4483 h = GGC_NEW (struct tree_map);
4484 h->hash = htab_hash_pointer (from);
4485 h->base.from = from;
4486 h->to = to;
4487 loc = htab_find_slot_with_hash (restrict_base_for_decl, h, h->hash, INSERT);
4488 *(struct tree_map **) loc = h;
4491 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
4493 static void
4494 print_debug_expr_statistics (void)
4496 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
4497 (long) htab_size (debug_expr_for_decl),
4498 (long) htab_elements (debug_expr_for_decl),
4499 htab_collisions (debug_expr_for_decl));
4502 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
4504 static void
4505 print_value_expr_statistics (void)
4507 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
4508 (long) htab_size (value_expr_for_decl),
4509 (long) htab_elements (value_expr_for_decl),
4510 htab_collisions (value_expr_for_decl));
4513 /* Print out statistics for the RESTRICT_BASE_FOR_DECL hash table, but
4514 don't print anything if the table is empty. */
4516 static void
4517 print_restrict_base_statistics (void)
4519 if (htab_elements (restrict_base_for_decl) != 0)
4520 fprintf (stderr,
4521 "RESTRICT_BASE hash: size %ld, %ld elements, %f collisions\n",
4522 (long) htab_size (restrict_base_for_decl),
4523 (long) htab_elements (restrict_base_for_decl),
4524 htab_collisions (restrict_base_for_decl));
4527 /* Lookup a debug expression for FROM, and return it if we find one. */
4529 tree
4530 decl_debug_expr_lookup (tree from)
4532 struct tree_map *h, in;
4533 in.base.from = from;
4535 h = (struct tree_map *) htab_find_with_hash (debug_expr_for_decl, &in,
4536 htab_hash_pointer (from));
4537 if (h)
4538 return h->to;
4539 return NULL_TREE;
4542 /* Insert a mapping FROM->TO in the debug expression hashtable. */
4544 void
4545 decl_debug_expr_insert (tree from, tree to)
4547 struct tree_map *h;
4548 void **loc;
4550 h = GGC_NEW (struct tree_map);
4551 h->hash = htab_hash_pointer (from);
4552 h->base.from = from;
4553 h->to = to;
4554 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, h->hash, INSERT);
4555 *(struct tree_map **) loc = h;
4558 /* Lookup a value expression for FROM, and return it if we find one. */
4560 tree
4561 decl_value_expr_lookup (tree from)
4563 struct tree_map *h, in;
4564 in.base.from = from;
4566 h = (struct tree_map *) htab_find_with_hash (value_expr_for_decl, &in,
4567 htab_hash_pointer (from));
4568 if (h)
4569 return h->to;
4570 return NULL_TREE;
4573 /* Insert a mapping FROM->TO in the value expression hashtable. */
4575 void
4576 decl_value_expr_insert (tree from, tree to)
4578 struct tree_map *h;
4579 void **loc;
4581 h = GGC_NEW (struct tree_map);
4582 h->hash = htab_hash_pointer (from);
4583 h->base.from = from;
4584 h->to = to;
4585 loc = htab_find_slot_with_hash (value_expr_for_decl, h, h->hash, INSERT);
4586 *(struct tree_map **) loc = h;
4589 /* Hashing of types so that we don't make duplicates.
4590 The entry point is `type_hash_canon'. */
4592 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
4593 with types in the TREE_VALUE slots), by adding the hash codes
4594 of the individual types. */
4596 static unsigned int
4597 type_hash_list (const_tree list, hashval_t hashcode)
4599 const_tree tail;
4601 for (tail = list; tail; tail = TREE_CHAIN (tail))
4602 if (TREE_VALUE (tail) != error_mark_node)
4603 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
4604 hashcode);
4606 return hashcode;
4609 /* These are the Hashtable callback functions. */
4611 /* Returns true iff the types are equivalent. */
4613 static int
4614 type_hash_eq (const void *va, const void *vb)
4616 const struct type_hash *const a = (const struct type_hash *) va,
4617 *const b = (const struct type_hash *) vb;
4619 /* First test the things that are the same for all types. */
4620 if (a->hash != b->hash
4621 || TREE_CODE (a->type) != TREE_CODE (b->type)
4622 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
4623 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
4624 TYPE_ATTRIBUTES (b->type))
4625 || TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
4626 || TYPE_MODE (a->type) != TYPE_MODE (b->type)
4627 || (TREE_CODE (a->type) != COMPLEX_TYPE
4628 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
4629 return 0;
4631 switch (TREE_CODE (a->type))
4633 case VOID_TYPE:
4634 case COMPLEX_TYPE:
4635 case POINTER_TYPE:
4636 case REFERENCE_TYPE:
4637 return 1;
4639 case VECTOR_TYPE:
4640 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
4642 case ENUMERAL_TYPE:
4643 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
4644 && !(TYPE_VALUES (a->type)
4645 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
4646 && TYPE_VALUES (b->type)
4647 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
4648 && type_list_equal (TYPE_VALUES (a->type),
4649 TYPE_VALUES (b->type))))
4650 return 0;
4652 /* ... fall through ... */
4654 case INTEGER_TYPE:
4655 case REAL_TYPE:
4656 case BOOLEAN_TYPE:
4657 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
4658 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
4659 TYPE_MAX_VALUE (b->type)))
4660 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
4661 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
4662 TYPE_MIN_VALUE (b->type))));
4664 case FIXED_POINT_TYPE:
4665 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
4667 case OFFSET_TYPE:
4668 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
4670 case METHOD_TYPE:
4671 return (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
4672 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
4673 || (TYPE_ARG_TYPES (a->type)
4674 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
4675 && TYPE_ARG_TYPES (b->type)
4676 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
4677 && type_list_equal (TYPE_ARG_TYPES (a->type),
4678 TYPE_ARG_TYPES (b->type)))));
4680 case ARRAY_TYPE:
4681 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
4683 case RECORD_TYPE:
4684 case UNION_TYPE:
4685 case QUAL_UNION_TYPE:
4686 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
4687 || (TYPE_FIELDS (a->type)
4688 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
4689 && TYPE_FIELDS (b->type)
4690 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
4691 && type_list_equal (TYPE_FIELDS (a->type),
4692 TYPE_FIELDS (b->type))));
4694 case FUNCTION_TYPE:
4695 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
4696 || (TYPE_ARG_TYPES (a->type)
4697 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
4698 && TYPE_ARG_TYPES (b->type)
4699 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
4700 && type_list_equal (TYPE_ARG_TYPES (a->type),
4701 TYPE_ARG_TYPES (b->type))))
4702 break;
4703 return 0;
4705 default:
4706 return 0;
4709 if (lang_hooks.types.type_hash_eq != NULL)
4710 return lang_hooks.types.type_hash_eq (a->type, b->type);
4712 return 1;
4715 /* Return the cached hash value. */
4717 static hashval_t
4718 type_hash_hash (const void *item)
4720 return ((const struct type_hash *) item)->hash;
4723 /* Look in the type hash table for a type isomorphic to TYPE.
4724 If one is found, return it. Otherwise return 0. */
4726 tree
4727 type_hash_lookup (hashval_t hashcode, tree type)
4729 struct type_hash *h, in;
4731 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
4732 must call that routine before comparing TYPE_ALIGNs. */
4733 layout_type (type);
4735 in.hash = hashcode;
4736 in.type = type;
4738 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
4739 hashcode);
4740 if (h)
4741 return h->type;
4742 return NULL_TREE;
4745 /* Add an entry to the type-hash-table
4746 for a type TYPE whose hash code is HASHCODE. */
4748 void
4749 type_hash_add (hashval_t hashcode, tree type)
4751 struct type_hash *h;
4752 void **loc;
4754 h = GGC_NEW (struct type_hash);
4755 h->hash = hashcode;
4756 h->type = type;
4757 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
4758 *loc = (void *)h;
4761 /* Given TYPE, and HASHCODE its hash code, return the canonical
4762 object for an identical type if one already exists.
4763 Otherwise, return TYPE, and record it as the canonical object.
4765 To use this function, first create a type of the sort you want.
4766 Then compute its hash code from the fields of the type that
4767 make it different from other similar types.
4768 Then call this function and use the value. */
4770 tree
4771 type_hash_canon (unsigned int hashcode, tree type)
4773 tree t1;
4775 /* The hash table only contains main variants, so ensure that's what we're
4776 being passed. */
4777 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
4779 if (!lang_hooks.types.hash_types)
4780 return type;
4782 /* See if the type is in the hash table already. If so, return it.
4783 Otherwise, add the type. */
4784 t1 = type_hash_lookup (hashcode, type);
4785 if (t1 != 0)
4787 #ifdef GATHER_STATISTICS
4788 tree_node_counts[(int) t_kind]--;
4789 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
4790 #endif
4791 return t1;
4793 else
4795 type_hash_add (hashcode, type);
4796 return type;
4800 /* See if the data pointed to by the type hash table is marked. We consider
4801 it marked if the type is marked or if a debug type number or symbol
4802 table entry has been made for the type. This reduces the amount of
4803 debugging output and eliminates that dependency of the debug output on
4804 the number of garbage collections. */
4806 static int
4807 type_hash_marked_p (const void *p)
4809 const_tree const type = ((const struct type_hash *) p)->type;
4811 return ggc_marked_p (type) || TYPE_SYMTAB_POINTER (type);
4814 static void
4815 print_type_hash_statistics (void)
4817 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
4818 (long) htab_size (type_hash_table),
4819 (long) htab_elements (type_hash_table),
4820 htab_collisions (type_hash_table));
4823 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
4824 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
4825 by adding the hash codes of the individual attributes. */
4827 static unsigned int
4828 attribute_hash_list (const_tree list, hashval_t hashcode)
4830 const_tree tail;
4832 for (tail = list; tail; tail = TREE_CHAIN (tail))
4833 /* ??? Do we want to add in TREE_VALUE too? */
4834 hashcode = iterative_hash_object
4835 (IDENTIFIER_HASH_VALUE (TREE_PURPOSE (tail)), hashcode);
4836 return hashcode;
4839 /* Given two lists of attributes, return true if list l2 is
4840 equivalent to l1. */
4843 attribute_list_equal (const_tree l1, const_tree l2)
4845 return attribute_list_contained (l1, l2)
4846 && attribute_list_contained (l2, l1);
4849 /* Given two lists of attributes, return true if list L2 is
4850 completely contained within L1. */
4851 /* ??? This would be faster if attribute names were stored in a canonicalized
4852 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
4853 must be used to show these elements are equivalent (which they are). */
4854 /* ??? It's not clear that attributes with arguments will always be handled
4855 correctly. */
4858 attribute_list_contained (const_tree l1, const_tree l2)
4860 const_tree t1, t2;
4862 /* First check the obvious, maybe the lists are identical. */
4863 if (l1 == l2)
4864 return 1;
4866 /* Maybe the lists are similar. */
4867 for (t1 = l1, t2 = l2;
4868 t1 != 0 && t2 != 0
4869 && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
4870 && TREE_VALUE (t1) == TREE_VALUE (t2);
4871 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
4873 /* Maybe the lists are equal. */
4874 if (t1 == 0 && t2 == 0)
4875 return 1;
4877 for (; t2 != 0; t2 = TREE_CHAIN (t2))
4879 const_tree attr;
4880 /* This CONST_CAST is okay because lookup_attribute does not
4881 modify its argument and the return value is assigned to a
4882 const_tree. */
4883 for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
4884 CONST_CAST_TREE(l1));
4885 attr != NULL_TREE;
4886 attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
4887 TREE_CHAIN (attr)))
4889 if (TREE_VALUE (t2) != NULL
4890 && TREE_CODE (TREE_VALUE (t2)) == TREE_LIST
4891 && TREE_VALUE (attr) != NULL
4892 && TREE_CODE (TREE_VALUE (attr)) == TREE_LIST)
4894 if (simple_cst_list_equal (TREE_VALUE (t2),
4895 TREE_VALUE (attr)) == 1)
4896 break;
4898 else if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) == 1)
4899 break;
4902 if (attr == 0)
4903 return 0;
4906 return 1;
4909 /* Given two lists of types
4910 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
4911 return 1 if the lists contain the same types in the same order.
4912 Also, the TREE_PURPOSEs must match. */
4915 type_list_equal (const_tree l1, const_tree l2)
4917 const_tree t1, t2;
4919 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
4920 if (TREE_VALUE (t1) != TREE_VALUE (t2)
4921 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
4922 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
4923 && (TREE_TYPE (TREE_PURPOSE (t1))
4924 == TREE_TYPE (TREE_PURPOSE (t2))))))
4925 return 0;
4927 return t1 == t2;
4930 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
4931 given by TYPE. If the argument list accepts variable arguments,
4932 then this function counts only the ordinary arguments. */
4935 type_num_arguments (const_tree type)
4937 int i = 0;
4938 tree t;
4940 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
4941 /* If the function does not take a variable number of arguments,
4942 the last element in the list will have type `void'. */
4943 if (VOID_TYPE_P (TREE_VALUE (t)))
4944 break;
4945 else
4946 ++i;
4948 return i;
4951 /* Nonzero if integer constants T1 and T2
4952 represent the same constant value. */
4955 tree_int_cst_equal (const_tree t1, const_tree t2)
4957 if (t1 == t2)
4958 return 1;
4960 if (t1 == 0 || t2 == 0)
4961 return 0;
4963 if (TREE_CODE (t1) == INTEGER_CST
4964 && TREE_CODE (t2) == INTEGER_CST
4965 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
4966 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
4967 return 1;
4969 return 0;
4972 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
4973 The precise way of comparison depends on their data type. */
4976 tree_int_cst_lt (const_tree t1, const_tree t2)
4978 if (t1 == t2)
4979 return 0;
4981 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
4983 int t1_sgn = tree_int_cst_sgn (t1);
4984 int t2_sgn = tree_int_cst_sgn (t2);
4986 if (t1_sgn < t2_sgn)
4987 return 1;
4988 else if (t1_sgn > t2_sgn)
4989 return 0;
4990 /* Otherwise, both are non-negative, so we compare them as
4991 unsigned just in case one of them would overflow a signed
4992 type. */
4994 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
4995 return INT_CST_LT (t1, t2);
4997 return INT_CST_LT_UNSIGNED (t1, t2);
5000 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
5003 tree_int_cst_compare (const_tree t1, const_tree t2)
5005 if (tree_int_cst_lt (t1, t2))
5006 return -1;
5007 else if (tree_int_cst_lt (t2, t1))
5008 return 1;
5009 else
5010 return 0;
5013 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
5014 the host. If POS is zero, the value can be represented in a single
5015 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
5016 be represented in a single unsigned HOST_WIDE_INT. */
5019 host_integerp (const_tree t, int pos)
5021 return (TREE_CODE (t) == INTEGER_CST
5022 && ((TREE_INT_CST_HIGH (t) == 0
5023 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
5024 || (! pos && TREE_INT_CST_HIGH (t) == -1
5025 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
5026 && (!TYPE_UNSIGNED (TREE_TYPE (t))
5027 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5028 && TYPE_IS_SIZETYPE (TREE_TYPE (t)))))
5029 || (pos && TREE_INT_CST_HIGH (t) == 0)));
5032 /* Return the HOST_WIDE_INT least significant bits of T if it is an
5033 INTEGER_CST and there is no overflow. POS is nonzero if the result must
5034 be non-negative. We must be able to satisfy the above conditions. */
5036 HOST_WIDE_INT
5037 tree_low_cst (const_tree t, int pos)
5039 gcc_assert (host_integerp (t, pos));
5040 return TREE_INT_CST_LOW (t);
5043 /* Return the most significant bit of the integer constant T. */
5046 tree_int_cst_msb (const_tree t)
5048 int prec;
5049 HOST_WIDE_INT h;
5050 unsigned HOST_WIDE_INT l;
5052 /* Note that using TYPE_PRECISION here is wrong. We care about the
5053 actual bits, not the (arbitrary) range of the type. */
5054 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
5055 rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
5056 2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
5057 return (l & 1) == 1;
5060 /* Return an indication of the sign of the integer constant T.
5061 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
5062 Note that -1 will never be returned if T's type is unsigned. */
5065 tree_int_cst_sgn (const_tree t)
5067 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
5068 return 0;
5069 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
5070 return 1;
5071 else if (TREE_INT_CST_HIGH (t) < 0)
5072 return -1;
5073 else
5074 return 1;
5077 /* Return the minimum number of bits needed to represent VALUE in a
5078 signed or unsigned type, UNSIGNEDP says which. */
5080 unsigned int
5081 tree_int_cst_min_precision (tree value, bool unsignedp)
5083 int log;
5085 /* If the value is negative, compute its negative minus 1. The latter
5086 adjustment is because the absolute value of the largest negative value
5087 is one larger than the largest positive value. This is equivalent to
5088 a bit-wise negation, so use that operation instead. */
5090 if (tree_int_cst_sgn (value) < 0)
5091 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
5093 /* Return the number of bits needed, taking into account the fact
5094 that we need one more bit for a signed than unsigned type. */
5096 if (integer_zerop (value))
5097 log = 0;
5098 else
5099 log = tree_floor_log2 (value);
5101 return log + 1 + !unsignedp;
5104 /* Compare two constructor-element-type constants. Return 1 if the lists
5105 are known to be equal; otherwise return 0. */
5108 simple_cst_list_equal (const_tree l1, const_tree l2)
5110 while (l1 != NULL_TREE && l2 != NULL_TREE)
5112 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
5113 return 0;
5115 l1 = TREE_CHAIN (l1);
5116 l2 = TREE_CHAIN (l2);
5119 return l1 == l2;
5122 /* Return truthvalue of whether T1 is the same tree structure as T2.
5123 Return 1 if they are the same.
5124 Return 0 if they are understandably different.
5125 Return -1 if either contains tree structure not understood by
5126 this function. */
5129 simple_cst_equal (const_tree t1, const_tree t2)
5131 enum tree_code code1, code2;
5132 int cmp;
5133 int i;
5135 if (t1 == t2)
5136 return 1;
5137 if (t1 == 0 || t2 == 0)
5138 return 0;
5140 code1 = TREE_CODE (t1);
5141 code2 = TREE_CODE (t2);
5143 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
5145 if (CONVERT_EXPR_CODE_P (code2)
5146 || code2 == NON_LVALUE_EXPR)
5147 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5148 else
5149 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
5152 else if (CONVERT_EXPR_CODE_P (code2)
5153 || code2 == NON_LVALUE_EXPR)
5154 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
5156 if (code1 != code2)
5157 return 0;
5159 switch (code1)
5161 case INTEGER_CST:
5162 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
5163 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
5165 case REAL_CST:
5166 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
5168 case FIXED_CST:
5169 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
5171 case STRING_CST:
5172 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
5173 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
5174 TREE_STRING_LENGTH (t1)));
5176 case CONSTRUCTOR:
5178 unsigned HOST_WIDE_INT idx;
5179 VEC(constructor_elt, gc) *v1 = CONSTRUCTOR_ELTS (t1);
5180 VEC(constructor_elt, gc) *v2 = CONSTRUCTOR_ELTS (t2);
5182 if (VEC_length (constructor_elt, v1) != VEC_length (constructor_elt, v2))
5183 return false;
5185 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
5186 /* ??? Should we handle also fields here? */
5187 if (!simple_cst_equal (VEC_index (constructor_elt, v1, idx)->value,
5188 VEC_index (constructor_elt, v2, idx)->value))
5189 return false;
5190 return true;
5193 case SAVE_EXPR:
5194 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5196 case CALL_EXPR:
5197 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
5198 if (cmp <= 0)
5199 return cmp;
5200 if (call_expr_nargs (t1) != call_expr_nargs (t2))
5201 return 0;
5203 const_tree arg1, arg2;
5204 const_call_expr_arg_iterator iter1, iter2;
5205 for (arg1 = first_const_call_expr_arg (t1, &iter1),
5206 arg2 = first_const_call_expr_arg (t2, &iter2);
5207 arg1 && arg2;
5208 arg1 = next_const_call_expr_arg (&iter1),
5209 arg2 = next_const_call_expr_arg (&iter2))
5211 cmp = simple_cst_equal (arg1, arg2);
5212 if (cmp <= 0)
5213 return cmp;
5215 return arg1 == arg2;
5218 case TARGET_EXPR:
5219 /* Special case: if either target is an unallocated VAR_DECL,
5220 it means that it's going to be unified with whatever the
5221 TARGET_EXPR is really supposed to initialize, so treat it
5222 as being equivalent to anything. */
5223 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
5224 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
5225 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
5226 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
5227 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
5228 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
5229 cmp = 1;
5230 else
5231 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5233 if (cmp <= 0)
5234 return cmp;
5236 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
5238 case WITH_CLEANUP_EXPR:
5239 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5240 if (cmp <= 0)
5241 return cmp;
5243 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
5245 case COMPONENT_REF:
5246 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
5247 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5249 return 0;
5251 case VAR_DECL:
5252 case PARM_DECL:
5253 case CONST_DECL:
5254 case FUNCTION_DECL:
5255 return 0;
5257 default:
5258 break;
5261 /* This general rule works for most tree codes. All exceptions should be
5262 handled above. If this is a language-specific tree code, we can't
5263 trust what might be in the operand, so say we don't know
5264 the situation. */
5265 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
5266 return -1;
5268 switch (TREE_CODE_CLASS (code1))
5270 case tcc_unary:
5271 case tcc_binary:
5272 case tcc_comparison:
5273 case tcc_expression:
5274 case tcc_reference:
5275 case tcc_statement:
5276 cmp = 1;
5277 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
5279 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
5280 if (cmp <= 0)
5281 return cmp;
5284 return cmp;
5286 default:
5287 return -1;
5291 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
5292 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
5293 than U, respectively. */
5296 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
5298 if (tree_int_cst_sgn (t) < 0)
5299 return -1;
5300 else if (TREE_INT_CST_HIGH (t) != 0)
5301 return 1;
5302 else if (TREE_INT_CST_LOW (t) == u)
5303 return 0;
5304 else if (TREE_INT_CST_LOW (t) < u)
5305 return -1;
5306 else
5307 return 1;
5310 /* Return true if CODE represents an associative tree code. Otherwise
5311 return false. */
5312 bool
5313 associative_tree_code (enum tree_code code)
5315 switch (code)
5317 case BIT_IOR_EXPR:
5318 case BIT_AND_EXPR:
5319 case BIT_XOR_EXPR:
5320 case PLUS_EXPR:
5321 case MULT_EXPR:
5322 case MIN_EXPR:
5323 case MAX_EXPR:
5324 return true;
5326 default:
5327 break;
5329 return false;
5332 /* Return true if CODE represents a commutative tree code. Otherwise
5333 return false. */
5334 bool
5335 commutative_tree_code (enum tree_code code)
5337 switch (code)
5339 case PLUS_EXPR:
5340 case MULT_EXPR:
5341 case MIN_EXPR:
5342 case MAX_EXPR:
5343 case BIT_IOR_EXPR:
5344 case BIT_XOR_EXPR:
5345 case BIT_AND_EXPR:
5346 case NE_EXPR:
5347 case EQ_EXPR:
5348 case UNORDERED_EXPR:
5349 case ORDERED_EXPR:
5350 case UNEQ_EXPR:
5351 case LTGT_EXPR:
5352 case TRUTH_AND_EXPR:
5353 case TRUTH_XOR_EXPR:
5354 case TRUTH_OR_EXPR:
5355 return true;
5357 default:
5358 break;
5360 return false;
5363 /* Generate a hash value for an expression. This can be used iteratively
5364 by passing a previous result as the VAL argument.
5366 This function is intended to produce the same hash for expressions which
5367 would compare equal using operand_equal_p. */
5369 hashval_t
5370 iterative_hash_expr (const_tree t, hashval_t val)
5372 int i;
5373 enum tree_code code;
5374 char tclass;
5376 if (t == NULL_TREE)
5377 return iterative_hash_hashval_t (0, val);
5379 code = TREE_CODE (t);
5381 switch (code)
5383 /* Alas, constants aren't shared, so we can't rely on pointer
5384 identity. */
5385 case INTEGER_CST:
5386 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
5387 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
5388 case REAL_CST:
5390 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
5392 return iterative_hash_hashval_t (val2, val);
5394 case FIXED_CST:
5396 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
5398 return iterative_hash_hashval_t (val2, val);
5400 case STRING_CST:
5401 return iterative_hash (TREE_STRING_POINTER (t),
5402 TREE_STRING_LENGTH (t), val);
5403 case COMPLEX_CST:
5404 val = iterative_hash_expr (TREE_REALPART (t), val);
5405 return iterative_hash_expr (TREE_IMAGPART (t), val);
5406 case VECTOR_CST:
5407 return iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
5409 case SSA_NAME:
5410 /* we can just compare by pointer. */
5411 return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
5413 case TREE_LIST:
5414 /* A list of expressions, for a CALL_EXPR or as the elements of a
5415 VECTOR_CST. */
5416 for (; t; t = TREE_CHAIN (t))
5417 val = iterative_hash_expr (TREE_VALUE (t), val);
5418 return val;
5419 case CONSTRUCTOR:
5421 unsigned HOST_WIDE_INT idx;
5422 tree field, value;
5423 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
5425 val = iterative_hash_expr (field, val);
5426 val = iterative_hash_expr (value, val);
5428 return val;
5430 case FUNCTION_DECL:
5431 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
5432 Otherwise nodes that compare equal according to operand_equal_p might
5433 get different hash codes. However, don't do this for machine specific
5434 or front end builtins, since the function code is overloaded in those
5435 cases. */
5436 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
5437 && built_in_decls[DECL_FUNCTION_CODE (t)])
5439 t = built_in_decls[DECL_FUNCTION_CODE (t)];
5440 code = TREE_CODE (t);
5442 /* FALL THROUGH */
5443 default:
5444 tclass = TREE_CODE_CLASS (code);
5446 if (tclass == tcc_declaration)
5448 /* DECL's have a unique ID */
5449 val = iterative_hash_host_wide_int (DECL_UID (t), val);
5451 else
5453 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
5455 val = iterative_hash_object (code, val);
5457 /* Don't hash the type, that can lead to having nodes which
5458 compare equal according to operand_equal_p, but which
5459 have different hash codes. */
5460 if (CONVERT_EXPR_CODE_P (code)
5461 || code == NON_LVALUE_EXPR)
5463 /* Make sure to include signness in the hash computation. */
5464 val += TYPE_UNSIGNED (TREE_TYPE (t));
5465 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
5468 else if (commutative_tree_code (code))
5470 /* It's a commutative expression. We want to hash it the same
5471 however it appears. We do this by first hashing both operands
5472 and then rehashing based on the order of their independent
5473 hashes. */
5474 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
5475 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
5476 hashval_t t;
5478 if (one > two)
5479 t = one, one = two, two = t;
5481 val = iterative_hash_hashval_t (one, val);
5482 val = iterative_hash_hashval_t (two, val);
5484 else
5485 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
5486 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
5488 return val;
5489 break;
5493 /* Generate a hash value for a pair of expressions. This can be used
5494 iteratively by passing a previous result as the VAL argument.
5496 The same hash value is always returned for a given pair of expressions,
5497 regardless of the order in which they are presented. This is useful in
5498 hashing the operands of commutative functions. */
5500 hashval_t
5501 iterative_hash_exprs_commutative (const_tree t1,
5502 const_tree t2, hashval_t val)
5504 hashval_t one = iterative_hash_expr (t1, 0);
5505 hashval_t two = iterative_hash_expr (t2, 0);
5506 hashval_t t;
5508 if (one > two)
5509 t = one, one = two, two = t;
5510 val = iterative_hash_hashval_t (one, val);
5511 val = iterative_hash_hashval_t (two, val);
5513 return val;
5516 /* Constructors for pointer, array and function types.
5517 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
5518 constructed by language-dependent code, not here.) */
5520 /* Construct, lay out and return the type of pointers to TO_TYPE with
5521 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
5522 reference all of memory. If such a type has already been
5523 constructed, reuse it. */
5525 tree
5526 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
5527 bool can_alias_all)
5529 tree t;
5531 if (to_type == error_mark_node)
5532 return error_mark_node;
5534 /* If the pointed-to type has the may_alias attribute set, force
5535 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
5536 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
5537 can_alias_all = true;
5539 /* In some cases, languages will have things that aren't a POINTER_TYPE
5540 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
5541 In that case, return that type without regard to the rest of our
5542 operands.
5544 ??? This is a kludge, but consistent with the way this function has
5545 always operated and there doesn't seem to be a good way to avoid this
5546 at the moment. */
5547 if (TYPE_POINTER_TO (to_type) != 0
5548 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
5549 return TYPE_POINTER_TO (to_type);
5551 /* First, if we already have a type for pointers to TO_TYPE and it's
5552 the proper mode, use it. */
5553 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
5554 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
5555 return t;
5557 t = make_node (POINTER_TYPE);
5559 TREE_TYPE (t) = to_type;
5560 SET_TYPE_MODE (t, mode);
5561 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
5562 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
5563 TYPE_POINTER_TO (to_type) = t;
5565 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
5566 SET_TYPE_STRUCTURAL_EQUALITY (t);
5567 else if (TYPE_CANONICAL (to_type) != to_type)
5568 TYPE_CANONICAL (t)
5569 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
5570 mode, can_alias_all);
5572 /* Lay out the type. This function has many callers that are concerned
5573 with expression-construction, and this simplifies them all. */
5574 layout_type (t);
5576 return t;
5579 /* By default build pointers in ptr_mode. */
5581 tree
5582 build_pointer_type (tree to_type)
5584 return build_pointer_type_for_mode (to_type, ptr_mode, false);
5587 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
5589 tree
5590 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
5591 bool can_alias_all)
5593 tree t;
5595 if (to_type == error_mark_node)
5596 return error_mark_node;
5598 /* If the pointed-to type has the may_alias attribute set, force
5599 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
5600 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
5601 can_alias_all = true;
5603 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
5604 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
5605 In that case, return that type without regard to the rest of our
5606 operands.
5608 ??? This is a kludge, but consistent with the way this function has
5609 always operated and there doesn't seem to be a good way to avoid this
5610 at the moment. */
5611 if (TYPE_REFERENCE_TO (to_type) != 0
5612 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
5613 return TYPE_REFERENCE_TO (to_type);
5615 /* First, if we already have a type for pointers to TO_TYPE and it's
5616 the proper mode, use it. */
5617 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
5618 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
5619 return t;
5621 t = make_node (REFERENCE_TYPE);
5623 TREE_TYPE (t) = to_type;
5624 SET_TYPE_MODE (t, mode);
5625 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
5626 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
5627 TYPE_REFERENCE_TO (to_type) = t;
5629 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
5630 SET_TYPE_STRUCTURAL_EQUALITY (t);
5631 else if (TYPE_CANONICAL (to_type) != to_type)
5632 TYPE_CANONICAL (t)
5633 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
5634 mode, can_alias_all);
5636 layout_type (t);
5638 return t;
5642 /* Build the node for the type of references-to-TO_TYPE by default
5643 in ptr_mode. */
5645 tree
5646 build_reference_type (tree to_type)
5648 return build_reference_type_for_mode (to_type, ptr_mode, false);
5651 /* Build a type that is compatible with t but has no cv quals anywhere
5652 in its type, thus
5654 const char *const *const * -> char ***. */
5656 tree
5657 build_type_no_quals (tree t)
5659 switch (TREE_CODE (t))
5661 case POINTER_TYPE:
5662 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
5663 TYPE_MODE (t),
5664 TYPE_REF_CAN_ALIAS_ALL (t));
5665 case REFERENCE_TYPE:
5666 return
5667 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
5668 TYPE_MODE (t),
5669 TYPE_REF_CAN_ALIAS_ALL (t));
5670 default:
5671 return TYPE_MAIN_VARIANT (t);
5675 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
5676 MAXVAL should be the maximum value in the domain
5677 (one less than the length of the array).
5679 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
5680 We don't enforce this limit, that is up to caller (e.g. language front end).
5681 The limit exists because the result is a signed type and we don't handle
5682 sizes that use more than one HOST_WIDE_INT. */
5684 tree
5685 build_index_type (tree maxval)
5687 tree itype = make_node (INTEGER_TYPE);
5689 TREE_TYPE (itype) = sizetype;
5690 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
5691 TYPE_MIN_VALUE (itype) = size_zero_node;
5692 TYPE_MAX_VALUE (itype) = fold_convert (sizetype, maxval);
5693 SET_TYPE_MODE (itype, TYPE_MODE (sizetype));
5694 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
5695 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
5696 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
5697 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
5699 if (host_integerp (maxval, 1))
5700 return type_hash_canon (tree_low_cst (maxval, 1), itype);
5701 else
5703 /* Since we cannot hash this type, we need to compare it using
5704 structural equality checks. */
5705 SET_TYPE_STRUCTURAL_EQUALITY (itype);
5706 return itype;
5710 /* Builds a signed or unsigned integer type of precision PRECISION.
5711 Used for C bitfields whose precision does not match that of
5712 built-in target types. */
5713 tree
5714 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
5715 int unsignedp)
5717 tree itype = make_node (INTEGER_TYPE);
5719 TYPE_PRECISION (itype) = precision;
5721 if (unsignedp)
5722 fixup_unsigned_type (itype);
5723 else
5724 fixup_signed_type (itype);
5726 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
5727 return type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
5729 return itype;
5732 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
5733 ENUMERAL_TYPE or BOOLEAN_TYPE), with low bound LOWVAL and
5734 high bound HIGHVAL. If TYPE is NULL, sizetype is used. */
5736 tree
5737 build_range_type (tree type, tree lowval, tree highval)
5739 tree itype = make_node (INTEGER_TYPE);
5741 TREE_TYPE (itype) = type;
5742 if (type == NULL_TREE)
5743 type = sizetype;
5745 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
5746 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
5748 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
5749 SET_TYPE_MODE (itype, TYPE_MODE (type));
5750 TYPE_SIZE (itype) = TYPE_SIZE (type);
5751 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
5752 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
5753 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
5755 if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
5756 return type_hash_canon (tree_low_cst (highval, 0)
5757 - tree_low_cst (lowval, 0),
5758 itype);
5759 else
5760 return itype;
5763 /* Return true if the debug information for TYPE, a subtype, should be emitted
5764 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
5765 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
5766 debug info and doesn't reflect the source code. */
5768 bool
5769 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
5771 tree base_type = TREE_TYPE (type), low, high;
5773 /* Subrange types have a base type which is an integral type. */
5774 if (!INTEGRAL_TYPE_P (base_type))
5775 return false;
5777 /* Get the real bounds of the subtype. */
5778 if (lang_hooks.types.get_subrange_bounds)
5779 lang_hooks.types.get_subrange_bounds (type, &low, &high);
5780 else
5782 low = TYPE_MIN_VALUE (type);
5783 high = TYPE_MAX_VALUE (type);
5786 /* If the type and its base type have the same representation and the same
5787 name, then the type is not a subrange but a copy of the base type. */
5788 if ((TREE_CODE (base_type) == INTEGER_TYPE
5789 || TREE_CODE (base_type) == BOOLEAN_TYPE)
5790 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
5791 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
5792 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type)))
5794 tree type_name = TYPE_NAME (type);
5795 tree base_type_name = TYPE_NAME (base_type);
5797 if (type_name && TREE_CODE (type_name) == TYPE_DECL)
5798 type_name = DECL_NAME (type_name);
5800 if (base_type_name && TREE_CODE (base_type_name) == TYPE_DECL)
5801 base_type_name = DECL_NAME (base_type_name);
5803 if (type_name == base_type_name)
5804 return false;
5807 if (lowval)
5808 *lowval = low;
5809 if (highval)
5810 *highval = high;
5811 return true;
5814 /* Just like build_index_type, but takes lowval and highval instead
5815 of just highval (maxval). */
5817 tree
5818 build_index_2_type (tree lowval, tree highval)
5820 return build_range_type (sizetype, lowval, highval);
5823 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
5824 and number of elements specified by the range of values of INDEX_TYPE.
5825 If such a type has already been constructed, reuse it. */
5827 tree
5828 build_array_type (tree elt_type, tree index_type)
5830 tree t;
5831 hashval_t hashcode = 0;
5833 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
5835 error ("arrays of functions are not meaningful");
5836 elt_type = integer_type_node;
5839 t = make_node (ARRAY_TYPE);
5840 TREE_TYPE (t) = elt_type;
5841 TYPE_DOMAIN (t) = index_type;
5843 if (index_type == 0)
5845 tree save = t;
5846 hashcode = iterative_hash_object (TYPE_HASH (elt_type), hashcode);
5847 t = type_hash_canon (hashcode, t);
5848 if (save == t)
5849 layout_type (t);
5851 if (TYPE_CANONICAL (t) == t)
5853 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type))
5854 SET_TYPE_STRUCTURAL_EQUALITY (t);
5855 else if (TYPE_CANONICAL (elt_type) != elt_type)
5856 TYPE_CANONICAL (t)
5857 = build_array_type (TYPE_CANONICAL (elt_type), index_type);
5860 return t;
5863 hashcode = iterative_hash_object (TYPE_HASH (elt_type), hashcode);
5864 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
5865 t = type_hash_canon (hashcode, t);
5867 if (!COMPLETE_TYPE_P (t))
5868 layout_type (t);
5870 if (TYPE_CANONICAL (t) == t)
5872 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
5873 || TYPE_STRUCTURAL_EQUALITY_P (index_type))
5874 SET_TYPE_STRUCTURAL_EQUALITY (t);
5875 else if (TYPE_CANONICAL (elt_type) != elt_type
5876 || TYPE_CANONICAL (index_type) != index_type)
5877 TYPE_CANONICAL (t)
5878 = build_array_type (TYPE_CANONICAL (elt_type),
5879 TYPE_CANONICAL (index_type));
5882 return t;
5885 /* Recursively examines the array elements of TYPE, until a non-array
5886 element type is found. */
5888 tree
5889 strip_array_types (tree type)
5891 while (TREE_CODE (type) == ARRAY_TYPE)
5892 type = TREE_TYPE (type);
5894 return type;
5897 /* Computes the canonical argument types from the argument type list
5898 ARGTYPES.
5900 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
5901 on entry to this function, or if any of the ARGTYPES are
5902 structural.
5904 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
5905 true on entry to this function, or if any of the ARGTYPES are
5906 non-canonical.
5908 Returns a canonical argument list, which may be ARGTYPES when the
5909 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
5910 true) or would not differ from ARGTYPES. */
5912 static tree
5913 maybe_canonicalize_argtypes(tree argtypes,
5914 bool *any_structural_p,
5915 bool *any_noncanonical_p)
5917 tree arg;
5918 bool any_noncanonical_argtypes_p = false;
5920 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
5922 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
5923 /* Fail gracefully by stating that the type is structural. */
5924 *any_structural_p = true;
5925 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
5926 *any_structural_p = true;
5927 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
5928 || TREE_PURPOSE (arg))
5929 /* If the argument has a default argument, we consider it
5930 non-canonical even though the type itself is canonical.
5931 That way, different variants of function and method types
5932 with default arguments will all point to the variant with
5933 no defaults as their canonical type. */
5934 any_noncanonical_argtypes_p = true;
5937 if (*any_structural_p)
5938 return argtypes;
5940 if (any_noncanonical_argtypes_p)
5942 /* Build the canonical list of argument types. */
5943 tree canon_argtypes = NULL_TREE;
5944 bool is_void = false;
5946 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
5948 if (arg == void_list_node)
5949 is_void = true;
5950 else
5951 canon_argtypes = tree_cons (NULL_TREE,
5952 TYPE_CANONICAL (TREE_VALUE (arg)),
5953 canon_argtypes);
5956 canon_argtypes = nreverse (canon_argtypes);
5957 if (is_void)
5958 canon_argtypes = chainon (canon_argtypes, void_list_node);
5960 /* There is a non-canonical type. */
5961 *any_noncanonical_p = true;
5962 return canon_argtypes;
5965 /* The canonical argument types are the same as ARGTYPES. */
5966 return argtypes;
5969 /* Construct, lay out and return
5970 the type of functions returning type VALUE_TYPE
5971 given arguments of types ARG_TYPES.
5972 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
5973 are data type nodes for the arguments of the function.
5974 If such a type has already been constructed, reuse it. */
5976 tree
5977 build_function_type (tree value_type, tree arg_types)
5979 tree t;
5980 hashval_t hashcode = 0;
5981 bool any_structural_p, any_noncanonical_p;
5982 tree canon_argtypes;
5984 if (TREE_CODE (value_type) == FUNCTION_TYPE)
5986 error ("function return type cannot be function");
5987 value_type = integer_type_node;
5990 /* Make a node of the sort we want. */
5991 t = make_node (FUNCTION_TYPE);
5992 TREE_TYPE (t) = value_type;
5993 TYPE_ARG_TYPES (t) = arg_types;
5995 /* If we already have such a type, use the old one. */
5996 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
5997 hashcode = type_hash_list (arg_types, hashcode);
5998 t = type_hash_canon (hashcode, t);
6000 /* Set up the canonical type. */
6001 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
6002 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
6003 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
6004 &any_structural_p,
6005 &any_noncanonical_p);
6006 if (any_structural_p)
6007 SET_TYPE_STRUCTURAL_EQUALITY (t);
6008 else if (any_noncanonical_p)
6009 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
6010 canon_argtypes);
6012 if (!COMPLETE_TYPE_P (t))
6013 layout_type (t);
6014 return t;
6017 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP. */
6019 tree
6020 build_function_type_skip_args (tree orig_type, bitmap args_to_skip)
6022 tree new_type = NULL;
6023 tree args, new_args = NULL, t;
6024 tree new_reversed;
6025 int i = 0;
6027 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
6028 args = TREE_CHAIN (args), i++)
6029 if (!bitmap_bit_p (args_to_skip, i))
6030 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
6032 new_reversed = nreverse (new_args);
6033 if (args)
6035 if (new_reversed)
6036 TREE_CHAIN (new_args) = void_list_node;
6037 else
6038 new_reversed = void_list_node;
6040 gcc_assert (new_reversed);
6042 /* Use copy_node to preserve as much as possible from original type
6043 (debug info, attribute lists etc.)
6044 Exception is METHOD_TYPEs must have THIS argument.
6045 When we are asked to remove it, we need to build new FUNCTION_TYPE
6046 instead. */
6047 if (TREE_CODE (orig_type) != METHOD_TYPE
6048 || !bitmap_bit_p (args_to_skip, 0))
6050 new_type = copy_node (orig_type);
6051 TYPE_ARG_TYPES (new_type) = new_reversed;
6053 else
6055 new_type
6056 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
6057 new_reversed));
6058 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
6061 /* This is a new type, not a copy of an old type. Need to reassociate
6062 variants. We can handle everything except the main variant lazily. */
6063 t = TYPE_MAIN_VARIANT (orig_type);
6064 if (orig_type != t)
6066 TYPE_MAIN_VARIANT (new_type) = t;
6067 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
6068 TYPE_NEXT_VARIANT (t) = new_type;
6070 else
6072 TYPE_MAIN_VARIANT (new_type) = new_type;
6073 TYPE_NEXT_VARIANT (new_type) = NULL;
6075 return new_type;
6078 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP.
6080 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
6081 linked by TREE_CHAIN directly. It is caller responsibility to eliminate
6082 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
6084 tree
6085 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip)
6087 tree new_decl = copy_node (orig_decl);
6088 tree new_type;
6090 new_type = TREE_TYPE (orig_decl);
6091 if (prototype_p (new_type))
6092 new_type = build_function_type_skip_args (new_type, args_to_skip);
6093 TREE_TYPE (new_decl) = new_type;
6095 /* For declarations setting DECL_VINDEX (i.e. methods)
6096 we expect first argument to be THIS pointer. */
6097 if (bitmap_bit_p (args_to_skip, 0))
6098 DECL_VINDEX (new_decl) = NULL_TREE;
6099 return new_decl;
6102 /* Build a function type. The RETURN_TYPE is the type returned by the
6103 function. If VAARGS is set, no void_type_node is appended to the
6104 the list. ARGP muse be alway be terminated be a NULL_TREE. */
6106 static tree
6107 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
6109 tree t, args, last;
6111 t = va_arg (argp, tree);
6112 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
6113 args = tree_cons (NULL_TREE, t, args);
6115 if (vaargs)
6117 last = args;
6118 if (args != NULL_TREE)
6119 args = nreverse (args);
6120 gcc_assert (args != NULL_TREE && last != void_list_node);
6122 else if (args == NULL_TREE)
6123 args = void_list_node;
6124 else
6126 last = args;
6127 args = nreverse (args);
6128 TREE_CHAIN (last) = void_list_node;
6130 args = build_function_type (return_type, args);
6132 return args;
6135 /* Build a function type. The RETURN_TYPE is the type returned by the
6136 function. If additional arguments are provided, they are
6137 additional argument types. The list of argument types must always
6138 be terminated by NULL_TREE. */
6140 tree
6141 build_function_type_list (tree return_type, ...)
6143 tree args;
6144 va_list p;
6146 va_start (p, return_type);
6147 args = build_function_type_list_1 (false, return_type, p);
6148 va_end (p);
6149 return args;
6152 /* Build a variable argument function type. The RETURN_TYPE is the
6153 type returned by the function. If additional arguments are provided,
6154 they are additional argument types. The list of argument types must
6155 always be terminated by NULL_TREE. */
6157 tree
6158 build_varargs_function_type_list (tree return_type, ...)
6160 tree args;
6161 va_list p;
6163 va_start (p, return_type);
6164 args = build_function_type_list_1 (true, return_type, p);
6165 va_end (p);
6167 return args;
6170 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
6171 and ARGTYPES (a TREE_LIST) are the return type and arguments types
6172 for the method. An implicit additional parameter (of type
6173 pointer-to-BASETYPE) is added to the ARGTYPES. */
6175 tree
6176 build_method_type_directly (tree basetype,
6177 tree rettype,
6178 tree argtypes)
6180 tree t;
6181 tree ptype;
6182 int hashcode = 0;
6183 bool any_structural_p, any_noncanonical_p;
6184 tree canon_argtypes;
6186 /* Make a node of the sort we want. */
6187 t = make_node (METHOD_TYPE);
6189 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
6190 TREE_TYPE (t) = rettype;
6191 ptype = build_pointer_type (basetype);
6193 /* The actual arglist for this function includes a "hidden" argument
6194 which is "this". Put it into the list of argument types. */
6195 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
6196 TYPE_ARG_TYPES (t) = argtypes;
6198 /* If we already have such a type, use the old one. */
6199 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
6200 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
6201 hashcode = type_hash_list (argtypes, hashcode);
6202 t = type_hash_canon (hashcode, t);
6204 /* Set up the canonical type. */
6205 any_structural_p
6206 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
6207 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
6208 any_noncanonical_p
6209 = (TYPE_CANONICAL (basetype) != basetype
6210 || TYPE_CANONICAL (rettype) != rettype);
6211 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
6212 &any_structural_p,
6213 &any_noncanonical_p);
6214 if (any_structural_p)
6215 SET_TYPE_STRUCTURAL_EQUALITY (t);
6216 else if (any_noncanonical_p)
6217 TYPE_CANONICAL (t)
6218 = build_method_type_directly (TYPE_CANONICAL (basetype),
6219 TYPE_CANONICAL (rettype),
6220 canon_argtypes);
6221 if (!COMPLETE_TYPE_P (t))
6222 layout_type (t);
6224 return t;
6227 /* Construct, lay out and return the type of methods belonging to class
6228 BASETYPE and whose arguments and values are described by TYPE.
6229 If that type exists already, reuse it.
6230 TYPE must be a FUNCTION_TYPE node. */
6232 tree
6233 build_method_type (tree basetype, tree type)
6235 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
6237 return build_method_type_directly (basetype,
6238 TREE_TYPE (type),
6239 TYPE_ARG_TYPES (type));
6242 /* Construct, lay out and return the type of offsets to a value
6243 of type TYPE, within an object of type BASETYPE.
6244 If a suitable offset type exists already, reuse it. */
6246 tree
6247 build_offset_type (tree basetype, tree type)
6249 tree t;
6250 hashval_t hashcode = 0;
6252 /* Make a node of the sort we want. */
6253 t = make_node (OFFSET_TYPE);
6255 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
6256 TREE_TYPE (t) = type;
6258 /* If we already have such a type, use the old one. */
6259 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
6260 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
6261 t = type_hash_canon (hashcode, t);
6263 if (!COMPLETE_TYPE_P (t))
6264 layout_type (t);
6266 if (TYPE_CANONICAL (t) == t)
6268 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
6269 || TYPE_STRUCTURAL_EQUALITY_P (type))
6270 SET_TYPE_STRUCTURAL_EQUALITY (t);
6271 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
6272 || TYPE_CANONICAL (type) != type)
6273 TYPE_CANONICAL (t)
6274 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
6275 TYPE_CANONICAL (type));
6278 return t;
6281 /* Create a complex type whose components are COMPONENT_TYPE. */
6283 tree
6284 build_complex_type (tree component_type)
6286 tree t;
6287 hashval_t hashcode;
6289 gcc_assert (INTEGRAL_TYPE_P (component_type)
6290 || SCALAR_FLOAT_TYPE_P (component_type)
6291 || FIXED_POINT_TYPE_P (component_type));
6293 /* Make a node of the sort we want. */
6294 t = make_node (COMPLEX_TYPE);
6296 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
6298 /* If we already have such a type, use the old one. */
6299 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
6300 t = type_hash_canon (hashcode, t);
6302 if (!COMPLETE_TYPE_P (t))
6303 layout_type (t);
6305 if (TYPE_CANONICAL (t) == t)
6307 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
6308 SET_TYPE_STRUCTURAL_EQUALITY (t);
6309 else if (TYPE_CANONICAL (component_type) != component_type)
6310 TYPE_CANONICAL (t)
6311 = build_complex_type (TYPE_CANONICAL (component_type));
6314 /* We need to create a name, since complex is a fundamental type. */
6315 if (! TYPE_NAME (t))
6317 const char *name;
6318 if (component_type == char_type_node)
6319 name = "complex char";
6320 else if (component_type == signed_char_type_node)
6321 name = "complex signed char";
6322 else if (component_type == unsigned_char_type_node)
6323 name = "complex unsigned char";
6324 else if (component_type == short_integer_type_node)
6325 name = "complex short int";
6326 else if (component_type == short_unsigned_type_node)
6327 name = "complex short unsigned int";
6328 else if (component_type == integer_type_node)
6329 name = "complex int";
6330 else if (component_type == unsigned_type_node)
6331 name = "complex unsigned int";
6332 else if (component_type == long_integer_type_node)
6333 name = "complex long int";
6334 else if (component_type == long_unsigned_type_node)
6335 name = "complex long unsigned int";
6336 else if (component_type == long_long_integer_type_node)
6337 name = "complex long long int";
6338 else if (component_type == long_long_unsigned_type_node)
6339 name = "complex long long unsigned int";
6340 else
6341 name = 0;
6343 if (name != 0)
6344 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
6345 get_identifier (name), t);
6348 return build_qualified_type (t, TYPE_QUALS (component_type));
6351 /* If TYPE is a real or complex floating-point type and the target
6352 does not directly support arithmetic on TYPE then return the wider
6353 type to be used for arithmetic on TYPE. Otherwise, return
6354 NULL_TREE. */
6356 tree
6357 excess_precision_type (tree type)
6359 if (flag_excess_precision != EXCESS_PRECISION_FAST)
6361 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
6362 switch (TREE_CODE (type))
6364 case REAL_TYPE:
6365 switch (flt_eval_method)
6367 case 1:
6368 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
6369 return double_type_node;
6370 break;
6371 case 2:
6372 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
6373 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
6374 return long_double_type_node;
6375 break;
6376 default:
6377 gcc_unreachable ();
6379 break;
6380 case COMPLEX_TYPE:
6381 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
6382 return NULL_TREE;
6383 switch (flt_eval_method)
6385 case 1:
6386 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
6387 return complex_double_type_node;
6388 break;
6389 case 2:
6390 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
6391 || (TYPE_MODE (TREE_TYPE (type))
6392 == TYPE_MODE (double_type_node)))
6393 return complex_long_double_type_node;
6394 break;
6395 default:
6396 gcc_unreachable ();
6398 break;
6399 default:
6400 break;
6403 return NULL_TREE;
6406 /* Return OP, stripped of any conversions to wider types as much as is safe.
6407 Converting the value back to OP's type makes a value equivalent to OP.
6409 If FOR_TYPE is nonzero, we return a value which, if converted to
6410 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
6412 OP must have integer, real or enumeral type. Pointers are not allowed!
6414 There are some cases where the obvious value we could return
6415 would regenerate to OP if converted to OP's type,
6416 but would not extend like OP to wider types.
6417 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
6418 For example, if OP is (unsigned short)(signed char)-1,
6419 we avoid returning (signed char)-1 if FOR_TYPE is int,
6420 even though extending that to an unsigned short would regenerate OP,
6421 since the result of extending (signed char)-1 to (int)
6422 is different from (int) OP. */
6424 tree
6425 get_unwidened (tree op, tree for_type)
6427 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
6428 tree type = TREE_TYPE (op);
6429 unsigned final_prec
6430 = TYPE_PRECISION (for_type != 0 ? for_type : type);
6431 int uns
6432 = (for_type != 0 && for_type != type
6433 && final_prec > TYPE_PRECISION (type)
6434 && TYPE_UNSIGNED (type));
6435 tree win = op;
6437 while (CONVERT_EXPR_P (op))
6439 int bitschange;
6441 /* TYPE_PRECISION on vector types has different meaning
6442 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
6443 so avoid them here. */
6444 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
6445 break;
6447 bitschange = TYPE_PRECISION (TREE_TYPE (op))
6448 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
6450 /* Truncations are many-one so cannot be removed.
6451 Unless we are later going to truncate down even farther. */
6452 if (bitschange < 0
6453 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
6454 break;
6456 /* See what's inside this conversion. If we decide to strip it,
6457 we will set WIN. */
6458 op = TREE_OPERAND (op, 0);
6460 /* If we have not stripped any zero-extensions (uns is 0),
6461 we can strip any kind of extension.
6462 If we have previously stripped a zero-extension,
6463 only zero-extensions can safely be stripped.
6464 Any extension can be stripped if the bits it would produce
6465 are all going to be discarded later by truncating to FOR_TYPE. */
6467 if (bitschange > 0)
6469 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
6470 win = op;
6471 /* TYPE_UNSIGNED says whether this is a zero-extension.
6472 Let's avoid computing it if it does not affect WIN
6473 and if UNS will not be needed again. */
6474 if ((uns
6475 || CONVERT_EXPR_P (op))
6476 && TYPE_UNSIGNED (TREE_TYPE (op)))
6478 uns = 1;
6479 win = op;
6484 return win;
6487 /* Return OP or a simpler expression for a narrower value
6488 which can be sign-extended or zero-extended to give back OP.
6489 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
6490 or 0 if the value should be sign-extended. */
6492 tree
6493 get_narrower (tree op, int *unsignedp_ptr)
6495 int uns = 0;
6496 int first = 1;
6497 tree win = op;
6498 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
6500 while (TREE_CODE (op) == NOP_EXPR)
6502 int bitschange
6503 = (TYPE_PRECISION (TREE_TYPE (op))
6504 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
6506 /* Truncations are many-one so cannot be removed. */
6507 if (bitschange < 0)
6508 break;
6510 /* See what's inside this conversion. If we decide to strip it,
6511 we will set WIN. */
6513 if (bitschange > 0)
6515 op = TREE_OPERAND (op, 0);
6516 /* An extension: the outermost one can be stripped,
6517 but remember whether it is zero or sign extension. */
6518 if (first)
6519 uns = TYPE_UNSIGNED (TREE_TYPE (op));
6520 /* Otherwise, if a sign extension has been stripped,
6521 only sign extensions can now be stripped;
6522 if a zero extension has been stripped, only zero-extensions. */
6523 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
6524 break;
6525 first = 0;
6527 else /* bitschange == 0 */
6529 /* A change in nominal type can always be stripped, but we must
6530 preserve the unsignedness. */
6531 if (first)
6532 uns = TYPE_UNSIGNED (TREE_TYPE (op));
6533 first = 0;
6534 op = TREE_OPERAND (op, 0);
6535 /* Keep trying to narrow, but don't assign op to win if it
6536 would turn an integral type into something else. */
6537 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
6538 continue;
6541 win = op;
6544 if (TREE_CODE (op) == COMPONENT_REF
6545 /* Since type_for_size always gives an integer type. */
6546 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
6547 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
6548 /* Ensure field is laid out already. */
6549 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
6550 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
6552 unsigned HOST_WIDE_INT innerprec
6553 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
6554 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
6555 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
6556 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
6558 /* We can get this structure field in a narrower type that fits it,
6559 but the resulting extension to its nominal type (a fullword type)
6560 must satisfy the same conditions as for other extensions.
6562 Do this only for fields that are aligned (not bit-fields),
6563 because when bit-field insns will be used there is no
6564 advantage in doing this. */
6566 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
6567 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
6568 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
6569 && type != 0)
6571 if (first)
6572 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
6573 win = fold_convert (type, op);
6577 *unsignedp_ptr = uns;
6578 return win;
6581 /* Nonzero if integer constant C has a value that is permissible
6582 for type TYPE (an INTEGER_TYPE). */
6585 int_fits_type_p (const_tree c, const_tree type)
6587 tree type_low_bound, type_high_bound;
6588 bool ok_for_low_bound, ok_for_high_bound, unsc;
6589 double_int dc, dd;
6591 dc = tree_to_double_int (c);
6592 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
6594 if (TREE_CODE (TREE_TYPE (c)) == INTEGER_TYPE
6595 && TYPE_IS_SIZETYPE (TREE_TYPE (c))
6596 && unsc)
6597 /* So c is an unsigned integer whose type is sizetype and type is not.
6598 sizetype'd integers are sign extended even though they are
6599 unsigned. If the integer value fits in the lower end word of c,
6600 and if the higher end word has all its bits set to 1, that
6601 means the higher end bits are set to 1 only for sign extension.
6602 So let's convert c into an equivalent zero extended unsigned
6603 integer. */
6604 dc = double_int_zext (dc, TYPE_PRECISION (TREE_TYPE (c)));
6606 retry:
6607 type_low_bound = TYPE_MIN_VALUE (type);
6608 type_high_bound = TYPE_MAX_VALUE (type);
6610 /* If at least one bound of the type is a constant integer, we can check
6611 ourselves and maybe make a decision. If no such decision is possible, but
6612 this type is a subtype, try checking against that. Otherwise, use
6613 fit_double_type, which checks against the precision.
6615 Compute the status for each possibly constant bound, and return if we see
6616 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
6617 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
6618 for "constant known to fit". */
6620 /* Check if c >= type_low_bound. */
6621 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
6623 dd = tree_to_double_int (type_low_bound);
6624 if (TREE_CODE (type) == INTEGER_TYPE
6625 && TYPE_IS_SIZETYPE (type)
6626 && TYPE_UNSIGNED (type))
6627 dd = double_int_zext (dd, TYPE_PRECISION (type));
6628 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
6630 int c_neg = (!unsc && double_int_negative_p (dc));
6631 int t_neg = (unsc && double_int_negative_p (dd));
6633 if (c_neg && !t_neg)
6634 return 0;
6635 if ((c_neg || !t_neg) && double_int_ucmp (dc, dd) < 0)
6636 return 0;
6638 else if (double_int_cmp (dc, dd, unsc) < 0)
6639 return 0;
6640 ok_for_low_bound = true;
6642 else
6643 ok_for_low_bound = false;
6645 /* Check if c <= type_high_bound. */
6646 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
6648 dd = tree_to_double_int (type_high_bound);
6649 if (TREE_CODE (type) == INTEGER_TYPE
6650 && TYPE_IS_SIZETYPE (type)
6651 && TYPE_UNSIGNED (type))
6652 dd = double_int_zext (dd, TYPE_PRECISION (type));
6653 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
6655 int c_neg = (!unsc && double_int_negative_p (dc));
6656 int t_neg = (unsc && double_int_negative_p (dd));
6658 if (t_neg && !c_neg)
6659 return 0;
6660 if ((t_neg || !c_neg) && double_int_ucmp (dc, dd) > 0)
6661 return 0;
6663 else if (double_int_cmp (dc, dd, unsc) > 0)
6664 return 0;
6665 ok_for_high_bound = true;
6667 else
6668 ok_for_high_bound = false;
6670 /* If the constant fits both bounds, the result is known. */
6671 if (ok_for_low_bound && ok_for_high_bound)
6672 return 1;
6674 /* Perform some generic filtering which may allow making a decision
6675 even if the bounds are not constant. First, negative integers
6676 never fit in unsigned types, */
6677 if (TYPE_UNSIGNED (type) && !unsc && double_int_negative_p (dc))
6678 return 0;
6680 /* Second, narrower types always fit in wider ones. */
6681 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
6682 return 1;
6684 /* Third, unsigned integers with top bit set never fit signed types. */
6685 if (! TYPE_UNSIGNED (type) && unsc)
6687 int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
6688 if (prec < HOST_BITS_PER_WIDE_INT)
6690 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
6691 return 0;
6693 else if (((((unsigned HOST_WIDE_INT) 1)
6694 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
6695 return 0;
6698 /* If we haven't been able to decide at this point, there nothing more we
6699 can check ourselves here. Look at the base type if we have one and it
6700 has the same precision. */
6701 if (TREE_CODE (type) == INTEGER_TYPE
6702 && TREE_TYPE (type) != 0
6703 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
6705 type = TREE_TYPE (type);
6706 goto retry;
6709 /* Or to fit_double_type, if nothing else. */
6710 return !fit_double_type (dc.low, dc.high, &dc.low, &dc.high, type);
6713 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
6714 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
6715 represented (assuming two's-complement arithmetic) within the bit
6716 precision of the type are returned instead. */
6718 void
6719 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
6721 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
6722 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
6723 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
6724 TYPE_UNSIGNED (type));
6725 else
6727 if (TYPE_UNSIGNED (type))
6728 mpz_set_ui (min, 0);
6729 else
6731 double_int mn;
6732 mn = double_int_mask (TYPE_PRECISION (type) - 1);
6733 mn = double_int_sext (double_int_add (mn, double_int_one),
6734 TYPE_PRECISION (type));
6735 mpz_set_double_int (min, mn, false);
6739 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
6740 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
6741 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
6742 TYPE_UNSIGNED (type));
6743 else
6745 if (TYPE_UNSIGNED (type))
6746 mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type)),
6747 true);
6748 else
6749 mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type) - 1),
6750 true);
6754 /* Return true if VAR is an automatic variable defined in function FN. */
6756 bool
6757 auto_var_in_fn_p (const_tree var, const_tree fn)
6759 return (DECL_P (var) && DECL_CONTEXT (var) == fn
6760 && (((TREE_CODE (var) == VAR_DECL || TREE_CODE (var) == PARM_DECL)
6761 && ! TREE_STATIC (var))
6762 || TREE_CODE (var) == LABEL_DECL
6763 || TREE_CODE (var) == RESULT_DECL));
6766 /* Subprogram of following function. Called by walk_tree.
6768 Return *TP if it is an automatic variable or parameter of the
6769 function passed in as DATA. */
6771 static tree
6772 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
6774 tree fn = (tree) data;
6776 if (TYPE_P (*tp))
6777 *walk_subtrees = 0;
6779 else if (DECL_P (*tp)
6780 && auto_var_in_fn_p (*tp, fn))
6781 return *tp;
6783 return NULL_TREE;
6786 /* Returns true if T is, contains, or refers to a type with variable
6787 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
6788 arguments, but not the return type. If FN is nonzero, only return
6789 true if a modifier of the type or position of FN is a variable or
6790 parameter inside FN.
6792 This concept is more general than that of C99 'variably modified types':
6793 in C99, a struct type is never variably modified because a VLA may not
6794 appear as a structure member. However, in GNU C code like:
6796 struct S { int i[f()]; };
6798 is valid, and other languages may define similar constructs. */
6800 bool
6801 variably_modified_type_p (tree type, tree fn)
6803 tree t;
6805 /* Test if T is either variable (if FN is zero) or an expression containing
6806 a variable in FN. */
6807 #define RETURN_TRUE_IF_VAR(T) \
6808 do { tree _t = (T); \
6809 if (_t && _t != error_mark_node && TREE_CODE (_t) != INTEGER_CST \
6810 && (!fn || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
6811 return true; } while (0)
6813 if (type == error_mark_node)
6814 return false;
6816 /* If TYPE itself has variable size, it is variably modified. */
6817 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
6818 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
6820 switch (TREE_CODE (type))
6822 case POINTER_TYPE:
6823 case REFERENCE_TYPE:
6824 case VECTOR_TYPE:
6825 if (variably_modified_type_p (TREE_TYPE (type), fn))
6826 return true;
6827 break;
6829 case FUNCTION_TYPE:
6830 case METHOD_TYPE:
6831 /* If TYPE is a function type, it is variably modified if the
6832 return type is variably modified. */
6833 if (variably_modified_type_p (TREE_TYPE (type), fn))
6834 return true;
6835 break;
6837 case INTEGER_TYPE:
6838 case REAL_TYPE:
6839 case FIXED_POINT_TYPE:
6840 case ENUMERAL_TYPE:
6841 case BOOLEAN_TYPE:
6842 /* Scalar types are variably modified if their end points
6843 aren't constant. */
6844 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
6845 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
6846 break;
6848 case RECORD_TYPE:
6849 case UNION_TYPE:
6850 case QUAL_UNION_TYPE:
6851 /* We can't see if any of the fields are variably-modified by the
6852 definition we normally use, since that would produce infinite
6853 recursion via pointers. */
6854 /* This is variably modified if some field's type is. */
6855 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
6856 if (TREE_CODE (t) == FIELD_DECL)
6858 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
6859 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
6860 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
6862 if (TREE_CODE (type) == QUAL_UNION_TYPE)
6863 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
6865 break;
6867 case ARRAY_TYPE:
6868 /* Do not call ourselves to avoid infinite recursion. This is
6869 variably modified if the element type is. */
6870 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
6871 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
6872 break;
6874 default:
6875 break;
6878 /* The current language may have other cases to check, but in general,
6879 all other types are not variably modified. */
6880 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
6882 #undef RETURN_TRUE_IF_VAR
6885 /* Given a DECL or TYPE, return the scope in which it was declared, or
6886 NULL_TREE if there is no containing scope. */
6888 tree
6889 get_containing_scope (const_tree t)
6891 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
6894 /* Return the innermost context enclosing DECL that is
6895 a FUNCTION_DECL, or zero if none. */
6897 tree
6898 decl_function_context (const_tree decl)
6900 tree context;
6902 if (TREE_CODE (decl) == ERROR_MARK)
6903 return 0;
6905 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
6906 where we look up the function at runtime. Such functions always take
6907 a first argument of type 'pointer to real context'.
6909 C++ should really be fixed to use DECL_CONTEXT for the real context,
6910 and use something else for the "virtual context". */
6911 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
6912 context
6913 = TYPE_MAIN_VARIANT
6914 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6915 else
6916 context = DECL_CONTEXT (decl);
6918 while (context && TREE_CODE (context) != FUNCTION_DECL)
6920 if (TREE_CODE (context) == BLOCK)
6921 context = BLOCK_SUPERCONTEXT (context);
6922 else
6923 context = get_containing_scope (context);
6926 return context;
6929 /* Return the innermost context enclosing DECL that is
6930 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
6931 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
6933 tree
6934 decl_type_context (const_tree decl)
6936 tree context = DECL_CONTEXT (decl);
6938 while (context)
6939 switch (TREE_CODE (context))
6941 case NAMESPACE_DECL:
6942 case TRANSLATION_UNIT_DECL:
6943 return NULL_TREE;
6945 case RECORD_TYPE:
6946 case UNION_TYPE:
6947 case QUAL_UNION_TYPE:
6948 return context;
6950 case TYPE_DECL:
6951 case FUNCTION_DECL:
6952 context = DECL_CONTEXT (context);
6953 break;
6955 case BLOCK:
6956 context = BLOCK_SUPERCONTEXT (context);
6957 break;
6959 default:
6960 gcc_unreachable ();
6963 return NULL_TREE;
6966 /* CALL is a CALL_EXPR. Return the declaration for the function
6967 called, or NULL_TREE if the called function cannot be
6968 determined. */
6970 tree
6971 get_callee_fndecl (const_tree call)
6973 tree addr;
6975 if (call == error_mark_node)
6976 return error_mark_node;
6978 /* It's invalid to call this function with anything but a
6979 CALL_EXPR. */
6980 gcc_assert (TREE_CODE (call) == CALL_EXPR);
6982 /* The first operand to the CALL is the address of the function
6983 called. */
6984 addr = CALL_EXPR_FN (call);
6986 STRIP_NOPS (addr);
6988 /* If this is a readonly function pointer, extract its initial value. */
6989 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
6990 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
6991 && DECL_INITIAL (addr))
6992 addr = DECL_INITIAL (addr);
6994 /* If the address is just `&f' for some function `f', then we know
6995 that `f' is being called. */
6996 if (TREE_CODE (addr) == ADDR_EXPR
6997 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
6998 return TREE_OPERAND (addr, 0);
7000 /* We couldn't figure out what was being called. */
7001 return NULL_TREE;
7004 /* Print debugging information about tree nodes generated during the compile,
7005 and any language-specific information. */
7007 void
7008 dump_tree_statistics (void)
7010 #ifdef GATHER_STATISTICS
7011 int i;
7012 int total_nodes, total_bytes;
7013 #endif
7015 fprintf (stderr, "\n??? tree nodes created\n\n");
7016 #ifdef GATHER_STATISTICS
7017 fprintf (stderr, "Kind Nodes Bytes\n");
7018 fprintf (stderr, "---------------------------------------\n");
7019 total_nodes = total_bytes = 0;
7020 for (i = 0; i < (int) all_kinds; i++)
7022 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
7023 tree_node_counts[i], tree_node_sizes[i]);
7024 total_nodes += tree_node_counts[i];
7025 total_bytes += tree_node_sizes[i];
7027 fprintf (stderr, "---------------------------------------\n");
7028 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
7029 fprintf (stderr, "---------------------------------------\n");
7030 ssanames_print_statistics ();
7031 phinodes_print_statistics ();
7032 #else
7033 fprintf (stderr, "(No per-node statistics)\n");
7034 #endif
7035 print_type_hash_statistics ();
7036 print_debug_expr_statistics ();
7037 print_value_expr_statistics ();
7038 print_restrict_base_statistics ();
7039 lang_hooks.print_statistics ();
7042 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
7044 /* Generate a crc32 of a string. */
7046 unsigned
7047 crc32_string (unsigned chksum, const char *string)
7051 unsigned value = *string << 24;
7052 unsigned ix;
7054 for (ix = 8; ix--; value <<= 1)
7056 unsigned feedback;
7058 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
7059 chksum <<= 1;
7060 chksum ^= feedback;
7063 while (*string++);
7064 return chksum;
7067 /* P is a string that will be used in a symbol. Mask out any characters
7068 that are not valid in that context. */
7070 void
7071 clean_symbol_name (char *p)
7073 for (; *p; p++)
7074 if (! (ISALNUM (*p)
7075 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
7076 || *p == '$'
7077 #endif
7078 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
7079 || *p == '.'
7080 #endif
7082 *p = '_';
7085 /* Generate a name for a special-purpose function function.
7086 The generated name may need to be unique across the whole link.
7087 TYPE is some string to identify the purpose of this function to the
7088 linker or collect2; it must start with an uppercase letter,
7089 one of:
7090 I - for constructors
7091 D - for destructors
7092 N - for C++ anonymous namespaces
7093 F - for DWARF unwind frame information. */
7095 tree
7096 get_file_function_name (const char *type)
7098 char *buf;
7099 const char *p;
7100 char *q;
7102 /* If we already have a name we know to be unique, just use that. */
7103 if (first_global_object_name)
7104 p = q = ASTRDUP (first_global_object_name);
7105 /* If the target is handling the constructors/destructors, they
7106 will be local to this file and the name is only necessary for
7107 debugging purposes. */
7108 else if ((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
7110 const char *file = main_input_filename;
7111 if (! file)
7112 file = input_filename;
7113 /* Just use the file's basename, because the full pathname
7114 might be quite long. */
7115 p = strrchr (file, '/');
7116 if (p)
7117 p++;
7118 else
7119 p = file;
7120 p = q = ASTRDUP (p);
7122 else
7124 /* Otherwise, the name must be unique across the entire link.
7125 We don't have anything that we know to be unique to this translation
7126 unit, so use what we do have and throw in some randomness. */
7127 unsigned len;
7128 const char *name = weak_global_object_name;
7129 const char *file = main_input_filename;
7131 if (! name)
7132 name = "";
7133 if (! file)
7134 file = input_filename;
7136 len = strlen (file);
7137 q = (char *) alloca (9 * 2 + len + 1);
7138 memcpy (q, file, len + 1);
7140 sprintf (q + len, "_%08X_%08X", crc32_string (0, name),
7141 crc32_string (0, get_random_seed (false)));
7143 p = q;
7146 clean_symbol_name (q);
7147 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
7148 + strlen (type));
7150 /* Set up the name of the file-level functions we may need.
7151 Use a global object (which is already required to be unique over
7152 the program) rather than the file name (which imposes extra
7153 constraints). */
7154 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
7156 return get_identifier (buf);
7159 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
7161 /* Complain that the tree code of NODE does not match the expected 0
7162 terminated list of trailing codes. The trailing code list can be
7163 empty, for a more vague error message. FILE, LINE, and FUNCTION
7164 are of the caller. */
7166 void
7167 tree_check_failed (const_tree node, const char *file,
7168 int line, const char *function, ...)
7170 va_list args;
7171 const char *buffer;
7172 unsigned length = 0;
7173 int code;
7175 va_start (args, function);
7176 while ((code = va_arg (args, int)))
7177 length += 4 + strlen (tree_code_name[code]);
7178 va_end (args);
7179 if (length)
7181 char *tmp;
7182 va_start (args, function);
7183 length += strlen ("expected ");
7184 buffer = tmp = (char *) alloca (length);
7185 length = 0;
7186 while ((code = va_arg (args, int)))
7188 const char *prefix = length ? " or " : "expected ";
7190 strcpy (tmp + length, prefix);
7191 length += strlen (prefix);
7192 strcpy (tmp + length, tree_code_name[code]);
7193 length += strlen (tree_code_name[code]);
7195 va_end (args);
7197 else
7198 buffer = "unexpected node";
7200 internal_error ("tree check: %s, have %s in %s, at %s:%d",
7201 buffer, tree_code_name[TREE_CODE (node)],
7202 function, trim_filename (file), line);
7205 /* Complain that the tree code of NODE does match the expected 0
7206 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
7207 the caller. */
7209 void
7210 tree_not_check_failed (const_tree node, const char *file,
7211 int line, const char *function, ...)
7213 va_list args;
7214 char *buffer;
7215 unsigned length = 0;
7216 int code;
7218 va_start (args, function);
7219 while ((code = va_arg (args, int)))
7220 length += 4 + strlen (tree_code_name[code]);
7221 va_end (args);
7222 va_start (args, function);
7223 buffer = (char *) alloca (length);
7224 length = 0;
7225 while ((code = va_arg (args, int)))
7227 if (length)
7229 strcpy (buffer + length, " or ");
7230 length += 4;
7232 strcpy (buffer + length, tree_code_name[code]);
7233 length += strlen (tree_code_name[code]);
7235 va_end (args);
7237 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
7238 buffer, tree_code_name[TREE_CODE (node)],
7239 function, trim_filename (file), line);
7242 /* Similar to tree_check_failed, except that we check for a class of tree
7243 code, given in CL. */
7245 void
7246 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
7247 const char *file, int line, const char *function)
7249 internal_error
7250 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
7251 TREE_CODE_CLASS_STRING (cl),
7252 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
7253 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
7256 /* Similar to tree_check_failed, except that instead of specifying a
7257 dozen codes, use the knowledge that they're all sequential. */
7259 void
7260 tree_range_check_failed (const_tree node, const char *file, int line,
7261 const char *function, enum tree_code c1,
7262 enum tree_code c2)
7264 char *buffer;
7265 unsigned length = 0;
7266 unsigned int c;
7268 for (c = c1; c <= c2; ++c)
7269 length += 4 + strlen (tree_code_name[c]);
7271 length += strlen ("expected ");
7272 buffer = (char *) alloca (length);
7273 length = 0;
7275 for (c = c1; c <= c2; ++c)
7277 const char *prefix = length ? " or " : "expected ";
7279 strcpy (buffer + length, prefix);
7280 length += strlen (prefix);
7281 strcpy (buffer + length, tree_code_name[c]);
7282 length += strlen (tree_code_name[c]);
7285 internal_error ("tree check: %s, have %s in %s, at %s:%d",
7286 buffer, tree_code_name[TREE_CODE (node)],
7287 function, trim_filename (file), line);
7291 /* Similar to tree_check_failed, except that we check that a tree does
7292 not have the specified code, given in CL. */
7294 void
7295 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
7296 const char *file, int line, const char *function)
7298 internal_error
7299 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
7300 TREE_CODE_CLASS_STRING (cl),
7301 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
7302 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
7306 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
7308 void
7309 omp_clause_check_failed (const_tree node, const char *file, int line,
7310 const char *function, enum omp_clause_code code)
7312 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
7313 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
7314 function, trim_filename (file), line);
7318 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
7320 void
7321 omp_clause_range_check_failed (const_tree node, const char *file, int line,
7322 const char *function, enum omp_clause_code c1,
7323 enum omp_clause_code c2)
7325 char *buffer;
7326 unsigned length = 0;
7327 unsigned int c;
7329 for (c = c1; c <= c2; ++c)
7330 length += 4 + strlen (omp_clause_code_name[c]);
7332 length += strlen ("expected ");
7333 buffer = (char *) alloca (length);
7334 length = 0;
7336 for (c = c1; c <= c2; ++c)
7338 const char *prefix = length ? " or " : "expected ";
7340 strcpy (buffer + length, prefix);
7341 length += strlen (prefix);
7342 strcpy (buffer + length, omp_clause_code_name[c]);
7343 length += strlen (omp_clause_code_name[c]);
7346 internal_error ("tree check: %s, have %s in %s, at %s:%d",
7347 buffer, omp_clause_code_name[TREE_CODE (node)],
7348 function, trim_filename (file), line);
7352 #undef DEFTREESTRUCT
7353 #define DEFTREESTRUCT(VAL, NAME) NAME,
7355 static const char *ts_enum_names[] = {
7356 #include "treestruct.def"
7358 #undef DEFTREESTRUCT
7360 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
7362 /* Similar to tree_class_check_failed, except that we check for
7363 whether CODE contains the tree structure identified by EN. */
7365 void
7366 tree_contains_struct_check_failed (const_tree node,
7367 const enum tree_node_structure_enum en,
7368 const char *file, int line,
7369 const char *function)
7371 internal_error
7372 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
7373 TS_ENUM_NAME(en),
7374 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
7378 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
7379 (dynamically sized) vector. */
7381 void
7382 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
7383 const char *function)
7385 internal_error
7386 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
7387 idx + 1, len, function, trim_filename (file), line);
7390 /* Similar to above, except that the check is for the bounds of the operand
7391 vector of an expression node EXP. */
7393 void
7394 tree_operand_check_failed (int idx, const_tree exp, const char *file,
7395 int line, const char *function)
7397 int code = TREE_CODE (exp);
7398 internal_error
7399 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
7400 idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
7401 function, trim_filename (file), line);
7404 /* Similar to above, except that the check is for the number of
7405 operands of an OMP_CLAUSE node. */
7407 void
7408 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
7409 int line, const char *function)
7411 internal_error
7412 ("tree check: accessed operand %d of omp_clause %s with %d operands "
7413 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
7414 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
7415 trim_filename (file), line);
7417 #endif /* ENABLE_TREE_CHECKING */
7419 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
7420 and mapped to the machine mode MODE. Initialize its fields and build
7421 the information necessary for debugging output. */
7423 static tree
7424 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
7426 tree t;
7427 hashval_t hashcode = 0;
7429 /* Build a main variant, based on the main variant of the inner type, then
7430 use it to build the variant we return. */
7431 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
7432 && TYPE_MAIN_VARIANT (innertype) != innertype)
7433 return build_type_attribute_qual_variant (
7434 make_vector_type (TYPE_MAIN_VARIANT (innertype), nunits, mode),
7435 TYPE_ATTRIBUTES (innertype),
7436 TYPE_QUALS (innertype));
7438 t = make_node (VECTOR_TYPE);
7439 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
7440 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
7441 SET_TYPE_MODE (t, mode);
7442 TYPE_READONLY (t) = TYPE_READONLY (innertype);
7443 TYPE_VOLATILE (t) = TYPE_VOLATILE (innertype);
7445 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
7446 SET_TYPE_STRUCTURAL_EQUALITY (t);
7447 else if (TYPE_CANONICAL (innertype) != innertype
7448 || mode != VOIDmode)
7449 TYPE_CANONICAL (t)
7450 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
7452 layout_type (t);
7455 tree index = build_int_cst (NULL_TREE, nunits - 1);
7456 tree array = build_array_type (TYPE_MAIN_VARIANT (innertype),
7457 build_index_type (index));
7458 tree rt = make_node (RECORD_TYPE);
7460 TYPE_FIELDS (rt) = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
7461 get_identifier ("f"), array);
7462 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
7463 layout_type (rt);
7464 TYPE_DEBUG_REPRESENTATION_TYPE (t) = rt;
7465 /* In dwarfout.c, type lookup uses TYPE_UID numbers. We want to output
7466 the representation type, and we want to find that die when looking up
7467 the vector type. This is most easily achieved by making the TYPE_UID
7468 numbers equal. */
7469 TYPE_UID (rt) = TYPE_UID (t);
7472 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
7473 hashcode = iterative_hash_host_wide_int (mode, hashcode);
7474 hashcode = iterative_hash_object (TYPE_HASH (innertype), hashcode);
7475 return type_hash_canon (hashcode, t);
7478 static tree
7479 make_or_reuse_type (unsigned size, int unsignedp)
7481 if (size == INT_TYPE_SIZE)
7482 return unsignedp ? unsigned_type_node : integer_type_node;
7483 if (size == CHAR_TYPE_SIZE)
7484 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
7485 if (size == SHORT_TYPE_SIZE)
7486 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
7487 if (size == LONG_TYPE_SIZE)
7488 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
7489 if (size == LONG_LONG_TYPE_SIZE)
7490 return (unsignedp ? long_long_unsigned_type_node
7491 : long_long_integer_type_node);
7493 if (unsignedp)
7494 return make_unsigned_type (size);
7495 else
7496 return make_signed_type (size);
7499 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
7501 static tree
7502 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
7504 if (satp)
7506 if (size == SHORT_FRACT_TYPE_SIZE)
7507 return unsignedp ? sat_unsigned_short_fract_type_node
7508 : sat_short_fract_type_node;
7509 if (size == FRACT_TYPE_SIZE)
7510 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
7511 if (size == LONG_FRACT_TYPE_SIZE)
7512 return unsignedp ? sat_unsigned_long_fract_type_node
7513 : sat_long_fract_type_node;
7514 if (size == LONG_LONG_FRACT_TYPE_SIZE)
7515 return unsignedp ? sat_unsigned_long_long_fract_type_node
7516 : sat_long_long_fract_type_node;
7518 else
7520 if (size == SHORT_FRACT_TYPE_SIZE)
7521 return unsignedp ? unsigned_short_fract_type_node
7522 : short_fract_type_node;
7523 if (size == FRACT_TYPE_SIZE)
7524 return unsignedp ? unsigned_fract_type_node : fract_type_node;
7525 if (size == LONG_FRACT_TYPE_SIZE)
7526 return unsignedp ? unsigned_long_fract_type_node
7527 : long_fract_type_node;
7528 if (size == LONG_LONG_FRACT_TYPE_SIZE)
7529 return unsignedp ? unsigned_long_long_fract_type_node
7530 : long_long_fract_type_node;
7533 return make_fract_type (size, unsignedp, satp);
7536 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
7538 static tree
7539 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
7541 if (satp)
7543 if (size == SHORT_ACCUM_TYPE_SIZE)
7544 return unsignedp ? sat_unsigned_short_accum_type_node
7545 : sat_short_accum_type_node;
7546 if (size == ACCUM_TYPE_SIZE)
7547 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
7548 if (size == LONG_ACCUM_TYPE_SIZE)
7549 return unsignedp ? sat_unsigned_long_accum_type_node
7550 : sat_long_accum_type_node;
7551 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
7552 return unsignedp ? sat_unsigned_long_long_accum_type_node
7553 : sat_long_long_accum_type_node;
7555 else
7557 if (size == SHORT_ACCUM_TYPE_SIZE)
7558 return unsignedp ? unsigned_short_accum_type_node
7559 : short_accum_type_node;
7560 if (size == ACCUM_TYPE_SIZE)
7561 return unsignedp ? unsigned_accum_type_node : accum_type_node;
7562 if (size == LONG_ACCUM_TYPE_SIZE)
7563 return unsignedp ? unsigned_long_accum_type_node
7564 : long_accum_type_node;
7565 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
7566 return unsignedp ? unsigned_long_long_accum_type_node
7567 : long_long_accum_type_node;
7570 return make_accum_type (size, unsignedp, satp);
7573 /* Create nodes for all integer types (and error_mark_node) using the sizes
7574 of C datatypes. The caller should call set_sizetype soon after calling
7575 this function to select one of the types as sizetype. */
7577 void
7578 build_common_tree_nodes (bool signed_char, bool signed_sizetype)
7580 error_mark_node = make_node (ERROR_MARK);
7581 TREE_TYPE (error_mark_node) = error_mark_node;
7583 initialize_sizetypes (signed_sizetype);
7585 /* Define both `signed char' and `unsigned char'. */
7586 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
7587 TYPE_STRING_FLAG (signed_char_type_node) = 1;
7588 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
7589 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
7591 /* Define `char', which is like either `signed char' or `unsigned char'
7592 but not the same as either. */
7593 char_type_node
7594 = (signed_char
7595 ? make_signed_type (CHAR_TYPE_SIZE)
7596 : make_unsigned_type (CHAR_TYPE_SIZE));
7597 TYPE_STRING_FLAG (char_type_node) = 1;
7599 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
7600 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
7601 integer_type_node = make_signed_type (INT_TYPE_SIZE);
7602 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
7603 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
7604 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
7605 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
7606 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
7608 /* Define a boolean type. This type only represents boolean values but
7609 may be larger than char depending on the value of BOOL_TYPE_SIZE.
7610 Front ends which want to override this size (i.e. Java) can redefine
7611 boolean_type_node before calling build_common_tree_nodes_2. */
7612 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
7613 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
7614 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
7615 TYPE_PRECISION (boolean_type_node) = 1;
7617 /* Fill in the rest of the sized types. Reuse existing type nodes
7618 when possible. */
7619 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
7620 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
7621 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
7622 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
7623 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
7625 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
7626 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
7627 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
7628 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
7629 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
7631 access_public_node = get_identifier ("public");
7632 access_protected_node = get_identifier ("protected");
7633 access_private_node = get_identifier ("private");
7636 /* Call this function after calling build_common_tree_nodes and set_sizetype.
7637 It will create several other common tree nodes. */
7639 void
7640 build_common_tree_nodes_2 (int short_double)
7642 /* Define these next since types below may used them. */
7643 integer_zero_node = build_int_cst (NULL_TREE, 0);
7644 integer_one_node = build_int_cst (NULL_TREE, 1);
7645 integer_minus_one_node = build_int_cst (NULL_TREE, -1);
7647 size_zero_node = size_int (0);
7648 size_one_node = size_int (1);
7649 bitsize_zero_node = bitsize_int (0);
7650 bitsize_one_node = bitsize_int (1);
7651 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
7653 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
7654 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
7656 void_type_node = make_node (VOID_TYPE);
7657 layout_type (void_type_node);
7659 /* We are not going to have real types in C with less than byte alignment,
7660 so we might as well not have any types that claim to have it. */
7661 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
7662 TYPE_USER_ALIGN (void_type_node) = 0;
7664 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
7665 layout_type (TREE_TYPE (null_pointer_node));
7667 ptr_type_node = build_pointer_type (void_type_node);
7668 const_ptr_type_node
7669 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
7670 fileptr_type_node = ptr_type_node;
7672 float_type_node = make_node (REAL_TYPE);
7673 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
7674 layout_type (float_type_node);
7676 double_type_node = make_node (REAL_TYPE);
7677 if (short_double)
7678 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
7679 else
7680 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
7681 layout_type (double_type_node);
7683 long_double_type_node = make_node (REAL_TYPE);
7684 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
7685 layout_type (long_double_type_node);
7687 float_ptr_type_node = build_pointer_type (float_type_node);
7688 double_ptr_type_node = build_pointer_type (double_type_node);
7689 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
7690 integer_ptr_type_node = build_pointer_type (integer_type_node);
7692 /* Fixed size integer types. */
7693 uint32_type_node = build_nonstandard_integer_type (32, true);
7694 uint64_type_node = build_nonstandard_integer_type (64, true);
7696 /* Decimal float types. */
7697 dfloat32_type_node = make_node (REAL_TYPE);
7698 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
7699 layout_type (dfloat32_type_node);
7700 SET_TYPE_MODE (dfloat32_type_node, SDmode);
7701 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
7703 dfloat64_type_node = make_node (REAL_TYPE);
7704 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
7705 layout_type (dfloat64_type_node);
7706 SET_TYPE_MODE (dfloat64_type_node, DDmode);
7707 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
7709 dfloat128_type_node = make_node (REAL_TYPE);
7710 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
7711 layout_type (dfloat128_type_node);
7712 SET_TYPE_MODE (dfloat128_type_node, TDmode);
7713 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
7715 complex_integer_type_node = build_complex_type (integer_type_node);
7716 complex_float_type_node = build_complex_type (float_type_node);
7717 complex_double_type_node = build_complex_type (double_type_node);
7718 complex_long_double_type_node = build_complex_type (long_double_type_node);
7720 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
7721 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
7722 sat_ ## KIND ## _type_node = \
7723 make_sat_signed_ ## KIND ## _type (SIZE); \
7724 sat_unsigned_ ## KIND ## _type_node = \
7725 make_sat_unsigned_ ## KIND ## _type (SIZE); \
7726 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
7727 unsigned_ ## KIND ## _type_node = \
7728 make_unsigned_ ## KIND ## _type (SIZE);
7730 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
7731 sat_ ## WIDTH ## KIND ## _type_node = \
7732 make_sat_signed_ ## KIND ## _type (SIZE); \
7733 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
7734 make_sat_unsigned_ ## KIND ## _type (SIZE); \
7735 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
7736 unsigned_ ## WIDTH ## KIND ## _type_node = \
7737 make_unsigned_ ## KIND ## _type (SIZE);
7739 /* Make fixed-point type nodes based on four different widths. */
7740 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
7741 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
7742 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
7743 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
7744 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
7746 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
7747 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
7748 NAME ## _type_node = \
7749 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
7750 u ## NAME ## _type_node = \
7751 make_or_reuse_unsigned_ ## KIND ## _type \
7752 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
7753 sat_ ## NAME ## _type_node = \
7754 make_or_reuse_sat_signed_ ## KIND ## _type \
7755 (GET_MODE_BITSIZE (MODE ## mode)); \
7756 sat_u ## NAME ## _type_node = \
7757 make_or_reuse_sat_unsigned_ ## KIND ## _type \
7758 (GET_MODE_BITSIZE (U ## MODE ## mode));
7760 /* Fixed-point type and mode nodes. */
7761 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
7762 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
7763 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
7764 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
7765 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
7766 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
7767 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
7768 MAKE_FIXED_MODE_NODE (accum, ha, HA)
7769 MAKE_FIXED_MODE_NODE (accum, sa, SA)
7770 MAKE_FIXED_MODE_NODE (accum, da, DA)
7771 MAKE_FIXED_MODE_NODE (accum, ta, TA)
7774 tree t = targetm.build_builtin_va_list ();
7776 /* Many back-ends define record types without setting TYPE_NAME.
7777 If we copied the record type here, we'd keep the original
7778 record type without a name. This breaks name mangling. So,
7779 don't copy record types and let c_common_nodes_and_builtins()
7780 declare the type to be __builtin_va_list. */
7781 if (TREE_CODE (t) != RECORD_TYPE)
7782 t = build_variant_type_copy (t);
7784 va_list_type_node = t;
7788 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
7790 static void
7791 local_define_builtin (const char *name, tree type, enum built_in_function code,
7792 const char *library_name, int ecf_flags)
7794 tree decl;
7796 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
7797 library_name, NULL_TREE);
7798 if (ecf_flags & ECF_CONST)
7799 TREE_READONLY (decl) = 1;
7800 if (ecf_flags & ECF_PURE)
7801 DECL_PURE_P (decl) = 1;
7802 if (ecf_flags & ECF_LOOPING_CONST_OR_PURE)
7803 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
7804 if (ecf_flags & ECF_NORETURN)
7805 TREE_THIS_VOLATILE (decl) = 1;
7806 if (ecf_flags & ECF_NOTHROW)
7807 TREE_NOTHROW (decl) = 1;
7808 if (ecf_flags & ECF_MALLOC)
7809 DECL_IS_MALLOC (decl) = 1;
7811 built_in_decls[code] = decl;
7812 implicit_built_in_decls[code] = decl;
7815 /* Call this function after instantiating all builtins that the language
7816 front end cares about. This will build the rest of the builtins that
7817 are relied upon by the tree optimizers and the middle-end. */
7819 void
7820 build_common_builtin_nodes (void)
7822 tree tmp, ftype;
7824 if (built_in_decls[BUILT_IN_MEMCPY] == NULL
7825 || built_in_decls[BUILT_IN_MEMMOVE] == NULL)
7827 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7828 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
7829 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7830 ftype = build_function_type (ptr_type_node, tmp);
7832 if (built_in_decls[BUILT_IN_MEMCPY] == NULL)
7833 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
7834 "memcpy", ECF_NOTHROW);
7835 if (built_in_decls[BUILT_IN_MEMMOVE] == NULL)
7836 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
7837 "memmove", ECF_NOTHROW);
7840 if (built_in_decls[BUILT_IN_MEMCMP] == NULL)
7842 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7843 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
7844 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
7845 ftype = build_function_type (integer_type_node, tmp);
7846 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
7847 "memcmp", ECF_PURE | ECF_NOTHROW);
7850 if (built_in_decls[BUILT_IN_MEMSET] == NULL)
7852 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7853 tmp = tree_cons (NULL_TREE, integer_type_node, tmp);
7854 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7855 ftype = build_function_type (ptr_type_node, tmp);
7856 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
7857 "memset", ECF_NOTHROW);
7860 if (built_in_decls[BUILT_IN_ALLOCA] == NULL)
7862 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7863 ftype = build_function_type (ptr_type_node, tmp);
7864 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
7865 "alloca", ECF_NOTHROW | ECF_MALLOC);
7868 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7869 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7870 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7871 ftype = build_function_type (void_type_node, tmp);
7872 local_define_builtin ("__builtin_init_trampoline", ftype,
7873 BUILT_IN_INIT_TRAMPOLINE,
7874 "__builtin_init_trampoline", ECF_NOTHROW);
7876 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7877 ftype = build_function_type (ptr_type_node, tmp);
7878 local_define_builtin ("__builtin_adjust_trampoline", ftype,
7879 BUILT_IN_ADJUST_TRAMPOLINE,
7880 "__builtin_adjust_trampoline",
7881 ECF_CONST | ECF_NOTHROW);
7883 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7884 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7885 ftype = build_function_type (void_type_node, tmp);
7886 local_define_builtin ("__builtin_nonlocal_goto", ftype,
7887 BUILT_IN_NONLOCAL_GOTO,
7888 "__builtin_nonlocal_goto",
7889 ECF_NORETURN | ECF_NOTHROW);
7891 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7892 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7893 ftype = build_function_type (void_type_node, tmp);
7894 local_define_builtin ("__builtin_setjmp_setup", ftype,
7895 BUILT_IN_SETJMP_SETUP,
7896 "__builtin_setjmp_setup", ECF_NOTHROW);
7898 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7899 ftype = build_function_type (ptr_type_node, tmp);
7900 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
7901 BUILT_IN_SETJMP_DISPATCHER,
7902 "__builtin_setjmp_dispatcher",
7903 ECF_PURE | ECF_NOTHROW);
7905 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7906 ftype = build_function_type (void_type_node, tmp);
7907 local_define_builtin ("__builtin_setjmp_receiver", ftype,
7908 BUILT_IN_SETJMP_RECEIVER,
7909 "__builtin_setjmp_receiver", ECF_NOTHROW);
7911 ftype = build_function_type (ptr_type_node, void_list_node);
7912 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
7913 "__builtin_stack_save", ECF_NOTHROW);
7915 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7916 ftype = build_function_type (void_type_node, tmp);
7917 local_define_builtin ("__builtin_stack_restore", ftype,
7918 BUILT_IN_STACK_RESTORE,
7919 "__builtin_stack_restore", ECF_NOTHROW);
7921 ftype = build_function_type (void_type_node, void_list_node);
7922 local_define_builtin ("__builtin_profile_func_enter", ftype,
7923 BUILT_IN_PROFILE_FUNC_ENTER, "profile_func_enter", 0);
7924 local_define_builtin ("__builtin_profile_func_exit", ftype,
7925 BUILT_IN_PROFILE_FUNC_EXIT, "profile_func_exit", 0);
7927 /* Complex multiplication and division. These are handled as builtins
7928 rather than optabs because emit_library_call_value doesn't support
7929 complex. Further, we can do slightly better with folding these
7930 beasties if the real and complex parts of the arguments are separate. */
7932 int mode;
7934 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
7936 char mode_name_buf[4], *q;
7937 const char *p;
7938 enum built_in_function mcode, dcode;
7939 tree type, inner_type;
7941 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
7942 if (type == NULL)
7943 continue;
7944 inner_type = TREE_TYPE (type);
7946 tmp = tree_cons (NULL_TREE, inner_type, void_list_node);
7947 tmp = tree_cons (NULL_TREE, inner_type, tmp);
7948 tmp = tree_cons (NULL_TREE, inner_type, tmp);
7949 tmp = tree_cons (NULL_TREE, inner_type, tmp);
7950 ftype = build_function_type (type, tmp);
7952 mcode = ((enum built_in_function)
7953 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
7954 dcode = ((enum built_in_function)
7955 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
7957 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
7958 *q = TOLOWER (*p);
7959 *q = '\0';
7961 built_in_names[mcode] = concat ("__mul", mode_name_buf, "3", NULL);
7962 local_define_builtin (built_in_names[mcode], ftype, mcode,
7963 built_in_names[mcode], ECF_CONST | ECF_NOTHROW);
7965 built_in_names[dcode] = concat ("__div", mode_name_buf, "3", NULL);
7966 local_define_builtin (built_in_names[dcode], ftype, dcode,
7967 built_in_names[dcode], ECF_CONST | ECF_NOTHROW);
7972 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
7973 better way.
7975 If we requested a pointer to a vector, build up the pointers that
7976 we stripped off while looking for the inner type. Similarly for
7977 return values from functions.
7979 The argument TYPE is the top of the chain, and BOTTOM is the
7980 new type which we will point to. */
7982 tree
7983 reconstruct_complex_type (tree type, tree bottom)
7985 tree inner, outer;
7987 if (TREE_CODE (type) == POINTER_TYPE)
7989 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7990 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
7991 TYPE_REF_CAN_ALIAS_ALL (type));
7993 else if (TREE_CODE (type) == REFERENCE_TYPE)
7995 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7996 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
7997 TYPE_REF_CAN_ALIAS_ALL (type));
7999 else if (TREE_CODE (type) == ARRAY_TYPE)
8001 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
8002 outer = build_array_type (inner, TYPE_DOMAIN (type));
8004 else if (TREE_CODE (type) == FUNCTION_TYPE)
8006 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
8007 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
8009 else if (TREE_CODE (type) == METHOD_TYPE)
8011 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
8012 /* The build_method_type_directly() routine prepends 'this' to argument list,
8013 so we must compensate by getting rid of it. */
8014 outer
8015 = build_method_type_directly
8016 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
8017 inner,
8018 TREE_CHAIN (TYPE_ARG_TYPES (type)));
8020 else if (TREE_CODE (type) == OFFSET_TYPE)
8022 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
8023 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
8025 else
8026 return bottom;
8028 return build_qualified_type (outer, TYPE_QUALS (type));
8031 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
8032 the inner type. */
8033 tree
8034 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
8036 int nunits;
8038 switch (GET_MODE_CLASS (mode))
8040 case MODE_VECTOR_INT:
8041 case MODE_VECTOR_FLOAT:
8042 case MODE_VECTOR_FRACT:
8043 case MODE_VECTOR_UFRACT:
8044 case MODE_VECTOR_ACCUM:
8045 case MODE_VECTOR_UACCUM:
8046 nunits = GET_MODE_NUNITS (mode);
8047 break;
8049 case MODE_INT:
8050 /* Check that there are no leftover bits. */
8051 gcc_assert (GET_MODE_BITSIZE (mode)
8052 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
8054 nunits = GET_MODE_BITSIZE (mode)
8055 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
8056 break;
8058 default:
8059 gcc_unreachable ();
8062 return make_vector_type (innertype, nunits, mode);
8065 /* Similarly, but takes the inner type and number of units, which must be
8066 a power of two. */
8068 tree
8069 build_vector_type (tree innertype, int nunits)
8071 return make_vector_type (innertype, nunits, VOIDmode);
8074 /* Similarly, but takes the inner type and number of units, which must be
8075 a power of two. */
8077 tree
8078 build_opaque_vector_type (tree innertype, int nunits)
8080 tree t;
8081 innertype = build_distinct_type_copy (innertype);
8082 t = make_vector_type (innertype, nunits, VOIDmode);
8083 TYPE_VECTOR_OPAQUE (t) = true;
8084 return t;
8088 /* Build RESX_EXPR with given REGION_NUMBER. */
8089 tree
8090 build_resx (int region_number)
8092 tree t;
8093 t = build1 (RESX_EXPR, void_type_node,
8094 build_int_cst (NULL_TREE, region_number));
8095 return t;
8098 /* Given an initializer INIT, return TRUE if INIT is zero or some
8099 aggregate of zeros. Otherwise return FALSE. */
8100 bool
8101 initializer_zerop (const_tree init)
8103 tree elt;
8105 STRIP_NOPS (init);
8107 switch (TREE_CODE (init))
8109 case INTEGER_CST:
8110 return integer_zerop (init);
8112 case REAL_CST:
8113 /* ??? Note that this is not correct for C4X float formats. There,
8114 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
8115 negative exponent. */
8116 return real_zerop (init)
8117 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
8119 case FIXED_CST:
8120 return fixed_zerop (init);
8122 case COMPLEX_CST:
8123 return integer_zerop (init)
8124 || (real_zerop (init)
8125 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
8126 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
8128 case VECTOR_CST:
8129 for (elt = TREE_VECTOR_CST_ELTS (init); elt; elt = TREE_CHAIN (elt))
8130 if (!initializer_zerop (TREE_VALUE (elt)))
8131 return false;
8132 return true;
8134 case CONSTRUCTOR:
8136 unsigned HOST_WIDE_INT idx;
8138 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
8139 if (!initializer_zerop (elt))
8140 return false;
8141 return true;
8144 default:
8145 return false;
8149 /* Build an empty statement at location LOC. */
8151 tree
8152 build_empty_stmt (location_t loc)
8154 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
8155 SET_EXPR_LOCATION (t, loc);
8156 return t;
8160 /* Build an OpenMP clause with code CODE. LOC is the location of the
8161 clause. */
8163 tree
8164 build_omp_clause (location_t loc, enum omp_clause_code code)
8166 tree t;
8167 int size, length;
8169 length = omp_clause_num_ops[code];
8170 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
8172 t = GGC_NEWVAR (union tree_node, size);
8173 memset (t, 0, size);
8174 TREE_SET_CODE (t, OMP_CLAUSE);
8175 OMP_CLAUSE_SET_CODE (t, code);
8176 OMP_CLAUSE_LOCATION (t) = loc;
8178 #ifdef GATHER_STATISTICS
8179 tree_node_counts[(int) omp_clause_kind]++;
8180 tree_node_sizes[(int) omp_clause_kind] += size;
8181 #endif
8183 return t;
8186 /* Set various status flags when building a CALL_EXPR object T. */
8188 static void
8189 process_call_operands (tree t)
8191 bool side_effects;
8193 side_effects = TREE_SIDE_EFFECTS (t);
8194 if (!side_effects)
8196 int i, n;
8197 n = TREE_OPERAND_LENGTH (t);
8198 for (i = 1; i < n; i++)
8200 tree op = TREE_OPERAND (t, i);
8201 if (op && TREE_SIDE_EFFECTS (op))
8203 side_effects = 1;
8204 break;
8208 if (!side_effects)
8210 int i;
8212 /* Calls have side-effects, except those to const or
8213 pure functions. */
8214 i = call_expr_flags (t);
8215 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
8216 side_effects = 1;
8218 TREE_SIDE_EFFECTS (t) = side_effects;
8221 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
8222 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
8223 Except for the CODE and operand count field, other storage for the
8224 object is initialized to zeros. */
8226 tree
8227 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
8229 tree t;
8230 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
8232 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
8233 gcc_assert (len >= 1);
8235 #ifdef GATHER_STATISTICS
8236 tree_node_counts[(int) e_kind]++;
8237 tree_node_sizes[(int) e_kind] += length;
8238 #endif
8240 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
8242 memset (t, 0, length);
8244 TREE_SET_CODE (t, code);
8246 /* Can't use TREE_OPERAND to store the length because if checking is
8247 enabled, it will try to check the length before we store it. :-P */
8248 t->exp.operands[0] = build_int_cst (sizetype, len);
8250 return t;
8254 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE
8255 and FN and a null static chain slot. ARGLIST is a TREE_LIST of the
8256 arguments. */
8258 tree
8259 build_call_list (tree return_type, tree fn, tree arglist)
8261 tree t;
8262 int i;
8264 t = build_vl_exp (CALL_EXPR, list_length (arglist) + 3);
8265 TREE_TYPE (t) = return_type;
8266 CALL_EXPR_FN (t) = fn;
8267 CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
8268 for (i = 0; arglist; arglist = TREE_CHAIN (arglist), i++)
8269 CALL_EXPR_ARG (t, i) = TREE_VALUE (arglist);
8270 process_call_operands (t);
8271 return t;
8274 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
8275 FN and a null static chain slot. NARGS is the number of call arguments
8276 which are specified as "..." arguments. */
8278 tree
8279 build_call_nary (tree return_type, tree fn, int nargs, ...)
8281 tree ret;
8282 va_list args;
8283 va_start (args, nargs);
8284 ret = build_call_valist (return_type, fn, nargs, args);
8285 va_end (args);
8286 return ret;
8289 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
8290 FN and a null static chain slot. NARGS is the number of call arguments
8291 which are specified as a va_list ARGS. */
8293 tree
8294 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
8296 tree t;
8297 int i;
8299 t = build_vl_exp (CALL_EXPR, nargs + 3);
8300 TREE_TYPE (t) = return_type;
8301 CALL_EXPR_FN (t) = fn;
8302 CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
8303 for (i = 0; i < nargs; i++)
8304 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
8305 process_call_operands (t);
8306 return t;
8309 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
8310 FN and a null static chain slot. NARGS is the number of call arguments
8311 which are specified as a tree array ARGS. */
8313 tree
8314 build_call_array (tree return_type, tree fn, int nargs, const tree *args)
8316 tree t;
8317 int i;
8319 t = build_vl_exp (CALL_EXPR, nargs + 3);
8320 TREE_TYPE (t) = return_type;
8321 CALL_EXPR_FN (t) = fn;
8322 CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
8323 for (i = 0; i < nargs; i++)
8324 CALL_EXPR_ARG (t, i) = args[i];
8325 process_call_operands (t);
8326 return t;
8329 /* Like build_call_array, but takes a VEC. */
8331 tree
8332 build_call_vec (tree return_type, tree fn, VEC(tree,gc) *args)
8334 tree ret, t;
8335 unsigned int ix;
8337 ret = build_vl_exp (CALL_EXPR, VEC_length (tree, args) + 3);
8338 TREE_TYPE (ret) = return_type;
8339 CALL_EXPR_FN (ret) = fn;
8340 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
8341 for (ix = 0; VEC_iterate (tree, args, ix, t); ++ix)
8342 CALL_EXPR_ARG (ret, ix) = t;
8343 process_call_operands (ret);
8344 return ret;
8348 /* Returns true if it is possible to prove that the index of
8349 an array access REF (an ARRAY_REF expression) falls into the
8350 array bounds. */
8352 bool
8353 in_array_bounds_p (tree ref)
8355 tree idx = TREE_OPERAND (ref, 1);
8356 tree min, max;
8358 if (TREE_CODE (idx) != INTEGER_CST)
8359 return false;
8361 min = array_ref_low_bound (ref);
8362 max = array_ref_up_bound (ref);
8363 if (!min
8364 || !max
8365 || TREE_CODE (min) != INTEGER_CST
8366 || TREE_CODE (max) != INTEGER_CST)
8367 return false;
8369 if (tree_int_cst_lt (idx, min)
8370 || tree_int_cst_lt (max, idx))
8371 return false;
8373 return true;
8376 /* Returns true if it is possible to prove that the range of
8377 an array access REF (an ARRAY_RANGE_REF expression) falls
8378 into the array bounds. */
8380 bool
8381 range_in_array_bounds_p (tree ref)
8383 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
8384 tree range_min, range_max, min, max;
8386 range_min = TYPE_MIN_VALUE (domain_type);
8387 range_max = TYPE_MAX_VALUE (domain_type);
8388 if (!range_min
8389 || !range_max
8390 || TREE_CODE (range_min) != INTEGER_CST
8391 || TREE_CODE (range_max) != INTEGER_CST)
8392 return false;
8394 min = array_ref_low_bound (ref);
8395 max = array_ref_up_bound (ref);
8396 if (!min
8397 || !max
8398 || TREE_CODE (min) != INTEGER_CST
8399 || TREE_CODE (max) != INTEGER_CST)
8400 return false;
8402 if (tree_int_cst_lt (range_min, min)
8403 || tree_int_cst_lt (max, range_max))
8404 return false;
8406 return true;
8409 /* Return true if T (assumed to be a DECL) must be assigned a memory
8410 location. */
8412 bool
8413 needs_to_live_in_memory (const_tree t)
8415 if (TREE_CODE (t) == SSA_NAME)
8416 t = SSA_NAME_VAR (t);
8418 return (TREE_ADDRESSABLE (t)
8419 || is_global_var (t)
8420 || (TREE_CODE (t) == RESULT_DECL
8421 && aggregate_value_p (t, current_function_decl)));
8424 /* There are situations in which a language considers record types
8425 compatible which have different field lists. Decide if two fields
8426 are compatible. It is assumed that the parent records are compatible. */
8428 bool
8429 fields_compatible_p (const_tree f1, const_tree f2)
8431 if (!operand_equal_p (DECL_FIELD_BIT_OFFSET (f1),
8432 DECL_FIELD_BIT_OFFSET (f2), OEP_ONLY_CONST))
8433 return false;
8435 if (!operand_equal_p (DECL_FIELD_OFFSET (f1),
8436 DECL_FIELD_OFFSET (f2), OEP_ONLY_CONST))
8437 return false;
8439 if (!types_compatible_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8440 return false;
8442 return true;
8445 /* Locate within RECORD a field that is compatible with ORIG_FIELD. */
8447 tree
8448 find_compatible_field (tree record, tree orig_field)
8450 tree f;
8452 for (f = TYPE_FIELDS (record); f ; f = TREE_CHAIN (f))
8453 if (TREE_CODE (f) == FIELD_DECL
8454 && fields_compatible_p (f, orig_field))
8455 return f;
8457 /* ??? Why isn't this on the main fields list? */
8458 f = TYPE_VFIELD (record);
8459 if (f && TREE_CODE (f) == FIELD_DECL
8460 && fields_compatible_p (f, orig_field))
8461 return f;
8463 /* ??? We should abort here, but Java appears to do Bad Things
8464 with inherited fields. */
8465 return orig_field;
8468 /* Return value of a constant X and sign-extend it. */
8470 HOST_WIDE_INT
8471 int_cst_value (const_tree x)
8473 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
8474 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
8476 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
8477 gcc_assert (TREE_INT_CST_HIGH (x) == 0
8478 || TREE_INT_CST_HIGH (x) == -1);
8480 if (bits < HOST_BITS_PER_WIDE_INT)
8482 bool negative = ((val >> (bits - 1)) & 1) != 0;
8483 if (negative)
8484 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
8485 else
8486 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
8489 return val;
8492 /* Return value of a constant X and sign-extend it. */
8494 HOST_WIDEST_INT
8495 widest_int_cst_value (const_tree x)
8497 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
8498 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
8500 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
8501 gcc_assert (HOST_BITS_PER_WIDEST_INT >= 2 * HOST_BITS_PER_WIDE_INT);
8502 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
8503 << HOST_BITS_PER_WIDE_INT);
8504 #else
8505 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
8506 gcc_assert (TREE_INT_CST_HIGH (x) == 0
8507 || TREE_INT_CST_HIGH (x) == -1);
8508 #endif
8510 if (bits < HOST_BITS_PER_WIDEST_INT)
8512 bool negative = ((val >> (bits - 1)) & 1) != 0;
8513 if (negative)
8514 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
8515 else
8516 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
8519 return val;
8522 /* If TYPE is an integral type, return an equivalent type which is
8523 unsigned iff UNSIGNEDP is true. If TYPE is not an integral type,
8524 return TYPE itself. */
8526 tree
8527 signed_or_unsigned_type_for (int unsignedp, tree type)
8529 tree t = type;
8530 if (POINTER_TYPE_P (type))
8531 t = size_type_node;
8533 if (!INTEGRAL_TYPE_P (t) || TYPE_UNSIGNED (t) == unsignedp)
8534 return t;
8536 return lang_hooks.types.type_for_size (TYPE_PRECISION (t), unsignedp);
8539 /* Returns unsigned variant of TYPE. */
8541 tree
8542 unsigned_type_for (tree type)
8544 return signed_or_unsigned_type_for (1, type);
8547 /* Returns signed variant of TYPE. */
8549 tree
8550 signed_type_for (tree type)
8552 return signed_or_unsigned_type_for (0, type);
8555 /* Returns the largest value obtainable by casting something in INNER type to
8556 OUTER type. */
8558 tree
8559 upper_bound_in_type (tree outer, tree inner)
8561 unsigned HOST_WIDE_INT lo, hi;
8562 unsigned int det = 0;
8563 unsigned oprec = TYPE_PRECISION (outer);
8564 unsigned iprec = TYPE_PRECISION (inner);
8565 unsigned prec;
8567 /* Compute a unique number for every combination. */
8568 det |= (oprec > iprec) ? 4 : 0;
8569 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
8570 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
8572 /* Determine the exponent to use. */
8573 switch (det)
8575 case 0:
8576 case 1:
8577 /* oprec <= iprec, outer: signed, inner: don't care. */
8578 prec = oprec - 1;
8579 break;
8580 case 2:
8581 case 3:
8582 /* oprec <= iprec, outer: unsigned, inner: don't care. */
8583 prec = oprec;
8584 break;
8585 case 4:
8586 /* oprec > iprec, outer: signed, inner: signed. */
8587 prec = iprec - 1;
8588 break;
8589 case 5:
8590 /* oprec > iprec, outer: signed, inner: unsigned. */
8591 prec = iprec;
8592 break;
8593 case 6:
8594 /* oprec > iprec, outer: unsigned, inner: signed. */
8595 prec = oprec;
8596 break;
8597 case 7:
8598 /* oprec > iprec, outer: unsigned, inner: unsigned. */
8599 prec = iprec;
8600 break;
8601 default:
8602 gcc_unreachable ();
8605 /* Compute 2^^prec - 1. */
8606 if (prec <= HOST_BITS_PER_WIDE_INT)
8608 hi = 0;
8609 lo = ((~(unsigned HOST_WIDE_INT) 0)
8610 >> (HOST_BITS_PER_WIDE_INT - prec));
8612 else
8614 hi = ((~(unsigned HOST_WIDE_INT) 0)
8615 >> (2 * HOST_BITS_PER_WIDE_INT - prec));
8616 lo = ~(unsigned HOST_WIDE_INT) 0;
8619 return build_int_cst_wide (outer, lo, hi);
8622 /* Returns the smallest value obtainable by casting something in INNER type to
8623 OUTER type. */
8625 tree
8626 lower_bound_in_type (tree outer, tree inner)
8628 unsigned HOST_WIDE_INT lo, hi;
8629 unsigned oprec = TYPE_PRECISION (outer);
8630 unsigned iprec = TYPE_PRECISION (inner);
8632 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
8633 and obtain 0. */
8634 if (TYPE_UNSIGNED (outer)
8635 /* If we are widening something of an unsigned type, OUTER type
8636 contains all values of INNER type. In particular, both INNER
8637 and OUTER types have zero in common. */
8638 || (oprec > iprec && TYPE_UNSIGNED (inner)))
8639 lo = hi = 0;
8640 else
8642 /* If we are widening a signed type to another signed type, we
8643 want to obtain -2^^(iprec-1). If we are keeping the
8644 precision or narrowing to a signed type, we want to obtain
8645 -2^(oprec-1). */
8646 unsigned prec = oprec > iprec ? iprec : oprec;
8648 if (prec <= HOST_BITS_PER_WIDE_INT)
8650 hi = ~(unsigned HOST_WIDE_INT) 0;
8651 lo = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
8653 else
8655 hi = ((~(unsigned HOST_WIDE_INT) 0)
8656 << (prec - HOST_BITS_PER_WIDE_INT - 1));
8657 lo = 0;
8661 return build_int_cst_wide (outer, lo, hi);
8664 /* Return nonzero if two operands that are suitable for PHI nodes are
8665 necessarily equal. Specifically, both ARG0 and ARG1 must be either
8666 SSA_NAME or invariant. Note that this is strictly an optimization.
8667 That is, callers of this function can directly call operand_equal_p
8668 and get the same result, only slower. */
8671 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
8673 if (arg0 == arg1)
8674 return 1;
8675 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
8676 return 0;
8677 return operand_equal_p (arg0, arg1, 0);
8680 /* Returns number of zeros at the end of binary representation of X.
8682 ??? Use ffs if available? */
8684 tree
8685 num_ending_zeros (const_tree x)
8687 unsigned HOST_WIDE_INT fr, nfr;
8688 unsigned num, abits;
8689 tree type = TREE_TYPE (x);
8691 if (TREE_INT_CST_LOW (x) == 0)
8693 num = HOST_BITS_PER_WIDE_INT;
8694 fr = TREE_INT_CST_HIGH (x);
8696 else
8698 num = 0;
8699 fr = TREE_INT_CST_LOW (x);
8702 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
8704 nfr = fr >> abits;
8705 if (nfr << abits == fr)
8707 num += abits;
8708 fr = nfr;
8712 if (num > TYPE_PRECISION (type))
8713 num = TYPE_PRECISION (type);
8715 return build_int_cst_type (type, num);
8719 #define WALK_SUBTREE(NODE) \
8720 do \
8722 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
8723 if (result) \
8724 return result; \
8726 while (0)
8728 /* This is a subroutine of walk_tree that walks field of TYPE that are to
8729 be walked whenever a type is seen in the tree. Rest of operands and return
8730 value are as for walk_tree. */
8732 static tree
8733 walk_type_fields (tree type, walk_tree_fn func, void *data,
8734 struct pointer_set_t *pset, walk_tree_lh lh)
8736 tree result = NULL_TREE;
8738 switch (TREE_CODE (type))
8740 case POINTER_TYPE:
8741 case REFERENCE_TYPE:
8742 /* We have to worry about mutually recursive pointers. These can't
8743 be written in C. They can in Ada. It's pathological, but
8744 there's an ACATS test (c38102a) that checks it. Deal with this
8745 by checking if we're pointing to another pointer, that one
8746 points to another pointer, that one does too, and we have no htab.
8747 If so, get a hash table. We check three levels deep to avoid
8748 the cost of the hash table if we don't need one. */
8749 if (POINTER_TYPE_P (TREE_TYPE (type))
8750 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
8751 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
8752 && !pset)
8754 result = walk_tree_without_duplicates (&TREE_TYPE (type),
8755 func, data);
8756 if (result)
8757 return result;
8759 break;
8762 /* ... fall through ... */
8764 case COMPLEX_TYPE:
8765 WALK_SUBTREE (TREE_TYPE (type));
8766 break;
8768 case METHOD_TYPE:
8769 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
8771 /* Fall through. */
8773 case FUNCTION_TYPE:
8774 WALK_SUBTREE (TREE_TYPE (type));
8776 tree arg;
8778 /* We never want to walk into default arguments. */
8779 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
8780 WALK_SUBTREE (TREE_VALUE (arg));
8782 break;
8784 case ARRAY_TYPE:
8785 /* Don't follow this nodes's type if a pointer for fear that
8786 we'll have infinite recursion. If we have a PSET, then we
8787 need not fear. */
8788 if (pset
8789 || (!POINTER_TYPE_P (TREE_TYPE (type))
8790 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
8791 WALK_SUBTREE (TREE_TYPE (type));
8792 WALK_SUBTREE (TYPE_DOMAIN (type));
8793 break;
8795 case OFFSET_TYPE:
8796 WALK_SUBTREE (TREE_TYPE (type));
8797 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
8798 break;
8800 default:
8801 break;
8804 return NULL_TREE;
8807 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
8808 called with the DATA and the address of each sub-tree. If FUNC returns a
8809 non-NULL value, the traversal is stopped, and the value returned by FUNC
8810 is returned. If PSET is non-NULL it is used to record the nodes visited,
8811 and to avoid visiting a node more than once. */
8813 tree
8814 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
8815 struct pointer_set_t *pset, walk_tree_lh lh)
8817 enum tree_code code;
8818 int walk_subtrees;
8819 tree result;
8821 #define WALK_SUBTREE_TAIL(NODE) \
8822 do \
8824 tp = & (NODE); \
8825 goto tail_recurse; \
8827 while (0)
8829 tail_recurse:
8830 /* Skip empty subtrees. */
8831 if (!*tp)
8832 return NULL_TREE;
8834 /* Don't walk the same tree twice, if the user has requested
8835 that we avoid doing so. */
8836 if (pset && pointer_set_insert (pset, *tp))
8837 return NULL_TREE;
8839 /* Call the function. */
8840 walk_subtrees = 1;
8841 result = (*func) (tp, &walk_subtrees, data);
8843 /* If we found something, return it. */
8844 if (result)
8845 return result;
8847 code = TREE_CODE (*tp);
8849 /* Even if we didn't, FUNC may have decided that there was nothing
8850 interesting below this point in the tree. */
8851 if (!walk_subtrees)
8853 /* But we still need to check our siblings. */
8854 if (code == TREE_LIST)
8855 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
8856 else if (code == OMP_CLAUSE)
8857 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8858 else
8859 return NULL_TREE;
8862 if (lh)
8864 result = (*lh) (tp, &walk_subtrees, func, data, pset);
8865 if (result || !walk_subtrees)
8866 return result;
8869 switch (code)
8871 case ERROR_MARK:
8872 case IDENTIFIER_NODE:
8873 case INTEGER_CST:
8874 case REAL_CST:
8875 case FIXED_CST:
8876 case VECTOR_CST:
8877 case STRING_CST:
8878 case BLOCK:
8879 case PLACEHOLDER_EXPR:
8880 case SSA_NAME:
8881 case FIELD_DECL:
8882 case RESULT_DECL:
8883 /* None of these have subtrees other than those already walked
8884 above. */
8885 break;
8887 case TREE_LIST:
8888 WALK_SUBTREE (TREE_VALUE (*tp));
8889 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
8890 break;
8892 case TREE_VEC:
8894 int len = TREE_VEC_LENGTH (*tp);
8896 if (len == 0)
8897 break;
8899 /* Walk all elements but the first. */
8900 while (--len)
8901 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
8903 /* Now walk the first one as a tail call. */
8904 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
8907 case COMPLEX_CST:
8908 WALK_SUBTREE (TREE_REALPART (*tp));
8909 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
8911 case CONSTRUCTOR:
8913 unsigned HOST_WIDE_INT idx;
8914 constructor_elt *ce;
8916 for (idx = 0;
8917 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (*tp), idx, ce);
8918 idx++)
8919 WALK_SUBTREE (ce->value);
8921 break;
8923 case SAVE_EXPR:
8924 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
8926 case BIND_EXPR:
8928 tree decl;
8929 for (decl = BIND_EXPR_VARS (*tp); decl; decl = TREE_CHAIN (decl))
8931 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
8932 into declarations that are just mentioned, rather than
8933 declared; they don't really belong to this part of the tree.
8934 And, we can see cycles: the initializer for a declaration
8935 can refer to the declaration itself. */
8936 WALK_SUBTREE (DECL_INITIAL (decl));
8937 WALK_SUBTREE (DECL_SIZE (decl));
8938 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
8940 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
8943 case STATEMENT_LIST:
8945 tree_stmt_iterator i;
8946 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
8947 WALK_SUBTREE (*tsi_stmt_ptr (i));
8949 break;
8951 case OMP_CLAUSE:
8952 switch (OMP_CLAUSE_CODE (*tp))
8954 case OMP_CLAUSE_PRIVATE:
8955 case OMP_CLAUSE_SHARED:
8956 case OMP_CLAUSE_FIRSTPRIVATE:
8957 case OMP_CLAUSE_COPYIN:
8958 case OMP_CLAUSE_COPYPRIVATE:
8959 case OMP_CLAUSE_IF:
8960 case OMP_CLAUSE_NUM_THREADS:
8961 case OMP_CLAUSE_SCHEDULE:
8962 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
8963 /* FALLTHRU */
8965 case OMP_CLAUSE_NOWAIT:
8966 case OMP_CLAUSE_ORDERED:
8967 case OMP_CLAUSE_DEFAULT:
8968 case OMP_CLAUSE_UNTIED:
8969 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8971 case OMP_CLAUSE_LASTPRIVATE:
8972 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
8973 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
8974 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8976 case OMP_CLAUSE_COLLAPSE:
8978 int i;
8979 for (i = 0; i < 3; i++)
8980 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
8981 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8984 case OMP_CLAUSE_REDUCTION:
8986 int i;
8987 for (i = 0; i < 4; i++)
8988 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
8989 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8992 default:
8993 gcc_unreachable ();
8995 break;
8997 case TARGET_EXPR:
8999 int i, len;
9001 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
9002 But, we only want to walk once. */
9003 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
9004 for (i = 0; i < len; ++i)
9005 WALK_SUBTREE (TREE_OPERAND (*tp, i));
9006 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
9009 case DECL_EXPR:
9010 /* If this is a TYPE_DECL, walk into the fields of the type that it's
9011 defining. We only want to walk into these fields of a type in this
9012 case and not in the general case of a mere reference to the type.
9014 The criterion is as follows: if the field can be an expression, it
9015 must be walked only here. This should be in keeping with the fields
9016 that are directly gimplified in gimplify_type_sizes in order for the
9017 mark/copy-if-shared/unmark machinery of the gimplifier to work with
9018 variable-sized types.
9020 Note that DECLs get walked as part of processing the BIND_EXPR. */
9021 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
9023 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
9024 if (TREE_CODE (*type_p) == ERROR_MARK)
9025 return NULL_TREE;
9027 /* Call the function for the type. See if it returns anything or
9028 doesn't want us to continue. If we are to continue, walk both
9029 the normal fields and those for the declaration case. */
9030 result = (*func) (type_p, &walk_subtrees, data);
9031 if (result || !walk_subtrees)
9032 return result;
9034 result = walk_type_fields (*type_p, func, data, pset, lh);
9035 if (result)
9036 return result;
9038 /* If this is a record type, also walk the fields. */
9039 if (TREE_CODE (*type_p) == RECORD_TYPE
9040 || TREE_CODE (*type_p) == UNION_TYPE
9041 || TREE_CODE (*type_p) == QUAL_UNION_TYPE)
9043 tree field;
9045 for (field = TYPE_FIELDS (*type_p); field;
9046 field = TREE_CHAIN (field))
9048 /* We'd like to look at the type of the field, but we can
9049 easily get infinite recursion. So assume it's pointed
9050 to elsewhere in the tree. Also, ignore things that
9051 aren't fields. */
9052 if (TREE_CODE (field) != FIELD_DECL)
9053 continue;
9055 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
9056 WALK_SUBTREE (DECL_SIZE (field));
9057 WALK_SUBTREE (DECL_SIZE_UNIT (field));
9058 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
9059 WALK_SUBTREE (DECL_QUALIFIER (field));
9063 /* Same for scalar types. */
9064 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
9065 || TREE_CODE (*type_p) == ENUMERAL_TYPE
9066 || TREE_CODE (*type_p) == INTEGER_TYPE
9067 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
9068 || TREE_CODE (*type_p) == REAL_TYPE)
9070 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
9071 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
9074 WALK_SUBTREE (TYPE_SIZE (*type_p));
9075 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
9077 /* FALLTHRU */
9079 default:
9080 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
9082 int i, len;
9084 /* Walk over all the sub-trees of this operand. */
9085 len = TREE_OPERAND_LENGTH (*tp);
9087 /* Go through the subtrees. We need to do this in forward order so
9088 that the scope of a FOR_EXPR is handled properly. */
9089 if (len)
9091 for (i = 0; i < len - 1; ++i)
9092 WALK_SUBTREE (TREE_OPERAND (*tp, i));
9093 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
9096 /* If this is a type, walk the needed fields in the type. */
9097 else if (TYPE_P (*tp))
9098 return walk_type_fields (*tp, func, data, pset, lh);
9099 break;
9102 /* We didn't find what we were looking for. */
9103 return NULL_TREE;
9105 #undef WALK_SUBTREE_TAIL
9107 #undef WALK_SUBTREE
9109 /* Like walk_tree, but does not walk duplicate nodes more than once. */
9111 tree
9112 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
9113 walk_tree_lh lh)
9115 tree result;
9116 struct pointer_set_t *pset;
9118 pset = pointer_set_create ();
9119 result = walk_tree_1 (tp, func, data, pset, lh);
9120 pointer_set_destroy (pset);
9121 return result;
9125 tree *
9126 tree_block (tree t)
9128 char const c = TREE_CODE_CLASS (TREE_CODE (t));
9130 if (IS_EXPR_CODE_CLASS (c))
9131 return &t->exp.block;
9132 gcc_unreachable ();
9133 return NULL;
9136 /* Build and return a TREE_LIST of arguments in the CALL_EXPR exp.
9137 FIXME: don't use this function. It exists for compatibility with
9138 the old representation of CALL_EXPRs where a list was used to hold the
9139 arguments. Places that currently extract the arglist from a CALL_EXPR
9140 ought to be rewritten to use the CALL_EXPR itself. */
9141 tree
9142 call_expr_arglist (tree exp)
9144 tree arglist = NULL_TREE;
9145 int i;
9146 for (i = call_expr_nargs (exp) - 1; i >= 0; i--)
9147 arglist = tree_cons (NULL_TREE, CALL_EXPR_ARG (exp, i), arglist);
9148 return arglist;
9152 /* Create a nameless artificial label and put it in the current
9153 function context. The label has a location of LOC. Returns the
9154 newly created label. */
9156 tree
9157 create_artificial_label (location_t loc)
9159 tree lab = build_decl (loc,
9160 LABEL_DECL, NULL_TREE, void_type_node);
9162 DECL_ARTIFICIAL (lab) = 1;
9163 DECL_IGNORED_P (lab) = 1;
9164 DECL_CONTEXT (lab) = current_function_decl;
9165 return lab;
9168 /* Given a tree, try to return a useful variable name that we can use
9169 to prefix a temporary that is being assigned the value of the tree.
9170 I.E. given <temp> = &A, return A. */
9172 const char *
9173 get_name (tree t)
9175 tree stripped_decl;
9177 stripped_decl = t;
9178 STRIP_NOPS (stripped_decl);
9179 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
9180 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
9181 else
9183 switch (TREE_CODE (stripped_decl))
9185 case ADDR_EXPR:
9186 return get_name (TREE_OPERAND (stripped_decl, 0));
9187 default:
9188 return NULL;
9193 /* Return true if TYPE has a variable argument list. */
9195 bool
9196 stdarg_p (tree fntype)
9198 function_args_iterator args_iter;
9199 tree n = NULL_TREE, t;
9201 if (!fntype)
9202 return false;
9204 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
9206 n = t;
9209 return n != NULL_TREE && n != void_type_node;
9212 /* Return true if TYPE has a prototype. */
9214 bool
9215 prototype_p (tree fntype)
9217 tree t;
9219 gcc_assert (fntype != NULL_TREE);
9221 t = TYPE_ARG_TYPES (fntype);
9222 return (t != NULL_TREE);
9225 /* If BLOCK is inlined from an __attribute__((__artificial__))
9226 routine, return pointer to location from where it has been
9227 called. */
9228 location_t *
9229 block_nonartificial_location (tree block)
9231 location_t *ret = NULL;
9233 while (block && TREE_CODE (block) == BLOCK
9234 && BLOCK_ABSTRACT_ORIGIN (block))
9236 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
9238 while (TREE_CODE (ao) == BLOCK
9239 && BLOCK_ABSTRACT_ORIGIN (ao)
9240 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
9241 ao = BLOCK_ABSTRACT_ORIGIN (ao);
9243 if (TREE_CODE (ao) == FUNCTION_DECL)
9245 /* If AO is an artificial inline, point RET to the
9246 call site locus at which it has been inlined and continue
9247 the loop, in case AO's caller is also an artificial
9248 inline. */
9249 if (DECL_DECLARED_INLINE_P (ao)
9250 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
9251 ret = &BLOCK_SOURCE_LOCATION (block);
9252 else
9253 break;
9255 else if (TREE_CODE (ao) != BLOCK)
9256 break;
9258 block = BLOCK_SUPERCONTEXT (block);
9260 return ret;
9264 /* If EXP is inlined from an __attribute__((__artificial__))
9265 function, return the location of the original call expression. */
9267 location_t
9268 tree_nonartificial_location (tree exp)
9270 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
9272 if (loc)
9273 return *loc;
9274 else
9275 return EXPR_LOCATION (exp);
9279 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
9280 nodes. */
9282 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
9284 static hashval_t
9285 cl_option_hash_hash (const void *x)
9287 const_tree const t = (const_tree) x;
9288 const char *p;
9289 size_t i;
9290 size_t len = 0;
9291 hashval_t hash = 0;
9293 if (TREE_CODE (t) == OPTIMIZATION_NODE)
9295 p = (const char *)TREE_OPTIMIZATION (t);
9296 len = sizeof (struct cl_optimization);
9299 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
9301 p = (const char *)TREE_TARGET_OPTION (t);
9302 len = sizeof (struct cl_target_option);
9305 else
9306 gcc_unreachable ();
9308 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
9309 something else. */
9310 for (i = 0; i < len; i++)
9311 if (p[i])
9312 hash = (hash << 4) ^ ((i << 2) | p[i]);
9314 return hash;
9317 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
9318 TARGET_OPTION tree node) is the same as that given by *Y, which is the
9319 same. */
9321 static int
9322 cl_option_hash_eq (const void *x, const void *y)
9324 const_tree const xt = (const_tree) x;
9325 const_tree const yt = (const_tree) y;
9326 const char *xp;
9327 const char *yp;
9328 size_t len;
9330 if (TREE_CODE (xt) != TREE_CODE (yt))
9331 return 0;
9333 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
9335 xp = (const char *)TREE_OPTIMIZATION (xt);
9336 yp = (const char *)TREE_OPTIMIZATION (yt);
9337 len = sizeof (struct cl_optimization);
9340 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
9342 xp = (const char *)TREE_TARGET_OPTION (xt);
9343 yp = (const char *)TREE_TARGET_OPTION (yt);
9344 len = sizeof (struct cl_target_option);
9347 else
9348 gcc_unreachable ();
9350 return (memcmp (xp, yp, len) == 0);
9353 /* Build an OPTIMIZATION_NODE based on the current options. */
9355 tree
9356 build_optimization_node (void)
9358 tree t;
9359 void **slot;
9361 /* Use the cache of optimization nodes. */
9363 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node));
9365 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
9366 t = (tree) *slot;
9367 if (!t)
9369 /* Insert this one into the hash table. */
9370 t = cl_optimization_node;
9371 *slot = t;
9373 /* Make a new node for next time round. */
9374 cl_optimization_node = make_node (OPTIMIZATION_NODE);
9377 return t;
9380 /* Build a TARGET_OPTION_NODE based on the current options. */
9382 tree
9383 build_target_option_node (void)
9385 tree t;
9386 void **slot;
9388 /* Use the cache of optimization nodes. */
9390 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node));
9392 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
9393 t = (tree) *slot;
9394 if (!t)
9396 /* Insert this one into the hash table. */
9397 t = cl_target_option_node;
9398 *slot = t;
9400 /* Make a new node for next time round. */
9401 cl_target_option_node = make_node (TARGET_OPTION_NODE);
9404 return t;
9407 /* Determine the "ultimate origin" of a block. The block may be an inlined
9408 instance of an inlined instance of a block which is local to an inline
9409 function, so we have to trace all of the way back through the origin chain
9410 to find out what sort of node actually served as the original seed for the
9411 given block. */
9413 tree
9414 block_ultimate_origin (const_tree block)
9416 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
9418 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
9419 nodes in the function to point to themselves; ignore that if
9420 we're trying to output the abstract instance of this function. */
9421 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
9422 return NULL_TREE;
9424 if (immediate_origin == NULL_TREE)
9425 return NULL_TREE;
9426 else
9428 tree ret_val;
9429 tree lookahead = immediate_origin;
9433 ret_val = lookahead;
9434 lookahead = (TREE_CODE (ret_val) == BLOCK
9435 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
9437 while (lookahead != NULL && lookahead != ret_val);
9439 /* The block's abstract origin chain may not be the *ultimate* origin of
9440 the block. It could lead to a DECL that has an abstract origin set.
9441 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
9442 will give us if it has one). Note that DECL's abstract origins are
9443 supposed to be the most distant ancestor (or so decl_ultimate_origin
9444 claims), so we don't need to loop following the DECL origins. */
9445 if (DECL_P (ret_val))
9446 return DECL_ORIGIN (ret_val);
9448 return ret_val;
9452 /* Return true if T1 and T2 are equivalent lists. */
9454 bool
9455 list_equal_p (const_tree t1, const_tree t2)
9457 for (; t1 && t2; t1 = TREE_CHAIN (t1) , t2 = TREE_CHAIN (t2))
9458 if (TREE_VALUE (t1) != TREE_VALUE (t2))
9459 return false;
9460 return !t1 && !t2;
9463 /* Return true iff conversion in EXP generates no instruction. Mark
9464 it inline so that we fully inline into the stripping functions even
9465 though we have two uses of this function. */
9467 static inline bool
9468 tree_nop_conversion (const_tree exp)
9470 tree outer_type, inner_type;
9472 if (!CONVERT_EXPR_P (exp)
9473 && TREE_CODE (exp) != NON_LVALUE_EXPR)
9474 return false;
9475 if (TREE_OPERAND (exp, 0) == error_mark_node)
9476 return false;
9478 outer_type = TREE_TYPE (exp);
9479 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
9481 /* Use precision rather then machine mode when we can, which gives
9482 the correct answer even for submode (bit-field) types. */
9483 if ((INTEGRAL_TYPE_P (outer_type)
9484 || POINTER_TYPE_P (outer_type)
9485 || TREE_CODE (outer_type) == OFFSET_TYPE)
9486 && (INTEGRAL_TYPE_P (inner_type)
9487 || POINTER_TYPE_P (inner_type)
9488 || TREE_CODE (inner_type) == OFFSET_TYPE))
9489 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
9491 /* Otherwise fall back on comparing machine modes (e.g. for
9492 aggregate types, floats). */
9493 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
9496 /* Return true iff conversion in EXP generates no instruction. Don't
9497 consider conversions changing the signedness. */
9499 static bool
9500 tree_sign_nop_conversion (const_tree exp)
9502 tree outer_type, inner_type;
9504 if (!tree_nop_conversion (exp))
9505 return false;
9507 outer_type = TREE_TYPE (exp);
9508 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
9510 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
9511 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
9514 /* Strip conversions from EXP according to tree_nop_conversion and
9515 return the resulting expression. */
9517 tree
9518 tree_strip_nop_conversions (tree exp)
9520 while (tree_nop_conversion (exp))
9521 exp = TREE_OPERAND (exp, 0);
9522 return exp;
9525 /* Strip conversions from EXP according to tree_sign_nop_conversion
9526 and return the resulting expression. */
9528 tree
9529 tree_strip_sign_nop_conversions (tree exp)
9531 while (tree_sign_nop_conversion (exp))
9532 exp = TREE_OPERAND (exp, 0);
9533 return exp;
9537 #include "gt-tree.h"