Merge trunk version 195164 into gupc branch.
[official-gcc.git] / gcc / tree.c
blobf9741e6b70b4e76348e7810c3abe1b2106786deb
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains the low level primitives for operating on tree nodes,
21 including allocation, list operations, interning of identifiers,
22 construction of data type nodes and statement nodes,
23 and construction of type conversion nodes. It also contains
24 tables index by tree code that describe how to take apart
25 nodes of that code.
27 It is intended to be language-independent, but occasionally
28 calls language-dependent routines defined (for C) in typecheck.c. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "flags.h"
35 #include "tree.h"
36 #include "tm_p.h"
37 #include "function.h"
38 #include "obstack.h"
39 #include "toplev.h" /* get_random_seed */
40 #include "ggc.h"
41 #include "hashtab.h"
42 #include "filenames.h"
43 #include "output.h"
44 #include "target.h"
45 #include "common/common-target.h"
46 #include "langhooks.h"
47 #include "tree-inline.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 "tree-pass.h"
54 #include "langhooks-def.h"
55 #include "diagnostic.h"
56 #include "tree-diagnostic.h"
57 #include "tree-pretty-print.h"
58 #include "cgraph.h"
59 #include "except.h"
60 #include "debug.h"
61 #include "intl.h"
63 /* Tree code classes. */
65 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
66 #define END_OF_BASE_TREE_CODES tcc_exceptional,
68 const enum tree_code_class tree_code_type[] = {
69 #include "all-tree.def"
72 #undef DEFTREECODE
73 #undef END_OF_BASE_TREE_CODES
75 /* Table indexed by tree code giving number of expression
76 operands beyond the fixed part of the node structure.
77 Not used for types or decls. */
79 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
80 #define END_OF_BASE_TREE_CODES 0,
82 const unsigned char tree_code_length[] = {
83 #include "all-tree.def"
86 #undef DEFTREECODE
87 #undef END_OF_BASE_TREE_CODES
89 /* Names of tree components.
90 Used for printing out the tree and error messages. */
91 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
92 #define END_OF_BASE_TREE_CODES "@dummy",
94 const char *const tree_code_name[] = {
95 #include "all-tree.def"
98 #undef DEFTREECODE
99 #undef END_OF_BASE_TREE_CODES
101 /* Each tree code class has an associated string representation.
102 These must correspond to the tree_code_class entries. */
104 const char *const tree_code_class_strings[] =
106 "exceptional",
107 "constant",
108 "type",
109 "declaration",
110 "reference",
111 "comparison",
112 "unary",
113 "binary",
114 "statement",
115 "vl_exp",
116 "expression"
119 /* obstack.[ch] explicitly declined to prototype this. */
120 extern int _obstack_allocated_p (struct obstack *h, void *obj);
122 /* Statistics-gathering stuff. */
124 static int tree_code_counts[MAX_TREE_CODES];
125 int tree_node_counts[(int) all_kinds];
126 int tree_node_sizes[(int) all_kinds];
128 /* Keep in sync with tree.h:enum tree_node_kind. */
129 static const char * const tree_node_kind_names[] = {
130 "decls",
131 "types",
132 "blocks",
133 "stmts",
134 "refs",
135 "exprs",
136 "constants",
137 "identifiers",
138 "vecs",
139 "binfos",
140 "ssa names",
141 "constructors",
142 "random kinds",
143 "lang_decl kinds",
144 "lang_type kinds",
145 "omp clauses",
148 /* Unique id for next decl created. */
149 static GTY(()) int next_decl_uid;
150 /* Unique id for next type created. */
151 static GTY(()) int next_type_uid = 1;
152 /* Unique id for next debug decl created. Use negative numbers,
153 to catch erroneous uses. */
154 static GTY(()) int next_debug_decl_uid;
156 /* Since we cannot rehash a type after it is in the table, we have to
157 keep the hash code. */
159 struct GTY(()) type_hash {
160 unsigned long hash;
161 tree type;
164 /* Initial size of the hash table (rounded to next prime). */
165 #define TYPE_HASH_INITIAL_SIZE 1000
167 /* Now here is the hash table. When recording a type, it is added to
168 the slot whose index is the hash code. Note that the hash table is
169 used for several kinds of types (function types, array types and
170 array index range types, for now). While all these live in the
171 same table, they are completely independent, and the hash code is
172 computed differently for each of these. */
174 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
175 htab_t type_hash_table;
177 /* Hash table and temporary node for larger integer const values. */
178 static GTY (()) tree int_cst_node;
179 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
180 htab_t int_cst_hash_table;
182 /* Hash table for optimization flags and target option flags. Use the same
183 hash table for both sets of options. Nodes for building the current
184 optimization and target option nodes. The assumption is most of the time
185 the options created will already be in the hash table, so we avoid
186 allocating and freeing up a node repeatably. */
187 static GTY (()) tree cl_optimization_node;
188 static GTY (()) tree cl_target_option_node;
189 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
190 htab_t cl_option_hash_table;
192 /* General tree->tree mapping structure for use in hash tables. */
195 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
196 htab_t debug_expr_for_decl;
198 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
199 htab_t value_expr_for_decl;
201 static GTY ((if_marked ("tree_vec_map_marked_p"), param_is (struct tree_vec_map)))
202 htab_t debug_args_for_decl;
204 static GTY ((if_marked ("tree_priority_map_marked_p"),
205 param_is (struct tree_priority_map)))
206 htab_t init_priority_for_decl;
208 static void set_type_quals (tree, int, tree);
209 static int type_hash_eq (const void *, const void *);
210 static hashval_t type_hash_hash (const void *);
211 static hashval_t int_cst_hash_hash (const void *);
212 static int int_cst_hash_eq (const void *, const void *);
213 static hashval_t cl_option_hash_hash (const void *);
214 static int cl_option_hash_eq (const void *, const void *);
215 static void print_type_hash_statistics (void);
216 static void print_debug_expr_statistics (void);
217 static void print_value_expr_statistics (void);
218 static int type_hash_marked_p (const void *);
219 static unsigned int type_hash_list (const_tree, hashval_t);
220 static unsigned int attribute_hash_list (const_tree, hashval_t);
222 tree global_trees[TI_MAX];
223 tree integer_types[itk_none];
225 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
227 /* Number of operands for each OpenMP clause. */
228 unsigned const char omp_clause_num_ops[] =
230 0, /* OMP_CLAUSE_ERROR */
231 1, /* OMP_CLAUSE_PRIVATE */
232 1, /* OMP_CLAUSE_SHARED */
233 1, /* OMP_CLAUSE_FIRSTPRIVATE */
234 2, /* OMP_CLAUSE_LASTPRIVATE */
235 4, /* OMP_CLAUSE_REDUCTION */
236 1, /* OMP_CLAUSE_COPYIN */
237 1, /* OMP_CLAUSE_COPYPRIVATE */
238 1, /* OMP_CLAUSE_IF */
239 1, /* OMP_CLAUSE_NUM_THREADS */
240 1, /* OMP_CLAUSE_SCHEDULE */
241 0, /* OMP_CLAUSE_NOWAIT */
242 0, /* OMP_CLAUSE_ORDERED */
243 0, /* OMP_CLAUSE_DEFAULT */
244 3, /* OMP_CLAUSE_COLLAPSE */
245 0, /* OMP_CLAUSE_UNTIED */
246 1, /* OMP_CLAUSE_FINAL */
247 0 /* OMP_CLAUSE_MERGEABLE */
250 const char * const omp_clause_code_name[] =
252 "error_clause",
253 "private",
254 "shared",
255 "firstprivate",
256 "lastprivate",
257 "reduction",
258 "copyin",
259 "copyprivate",
260 "if",
261 "num_threads",
262 "schedule",
263 "nowait",
264 "ordered",
265 "default",
266 "collapse",
267 "untied",
268 "final",
269 "mergeable"
273 /* Return the tree node structure used by tree code CODE. */
275 static inline enum tree_node_structure_enum
276 tree_node_structure_for_code (enum tree_code code)
278 switch (TREE_CODE_CLASS (code))
280 case tcc_declaration:
282 switch (code)
284 case FIELD_DECL:
285 return TS_FIELD_DECL;
286 case PARM_DECL:
287 return TS_PARM_DECL;
288 case VAR_DECL:
289 return TS_VAR_DECL;
290 case LABEL_DECL:
291 return TS_LABEL_DECL;
292 case RESULT_DECL:
293 return TS_RESULT_DECL;
294 case DEBUG_EXPR_DECL:
295 return TS_DECL_WRTL;
296 case CONST_DECL:
297 return TS_CONST_DECL;
298 case TYPE_DECL:
299 return TS_TYPE_DECL;
300 case FUNCTION_DECL:
301 return TS_FUNCTION_DECL;
302 case TRANSLATION_UNIT_DECL:
303 return TS_TRANSLATION_UNIT_DECL;
304 default:
305 return TS_DECL_NON_COMMON;
308 case tcc_type:
309 return TS_TYPE_NON_COMMON;
310 case tcc_reference:
311 case tcc_comparison:
312 case tcc_unary:
313 case tcc_binary:
314 case tcc_expression:
315 case tcc_statement:
316 case tcc_vl_exp:
317 return TS_EXP;
318 default: /* tcc_constant and tcc_exceptional */
319 break;
321 switch (code)
323 /* tcc_constant cases. */
324 case INTEGER_CST: return TS_INT_CST;
325 case REAL_CST: return TS_REAL_CST;
326 case FIXED_CST: return TS_FIXED_CST;
327 case COMPLEX_CST: return TS_COMPLEX;
328 case VECTOR_CST: return TS_VECTOR;
329 case STRING_CST: return TS_STRING;
330 /* tcc_exceptional cases. */
331 case ERROR_MARK: return TS_COMMON;
332 case IDENTIFIER_NODE: return TS_IDENTIFIER;
333 case TREE_LIST: return TS_LIST;
334 case TREE_VEC: return TS_VEC;
335 case SSA_NAME: return TS_SSA_NAME;
336 case PLACEHOLDER_EXPR: return TS_COMMON;
337 case STATEMENT_LIST: return TS_STATEMENT_LIST;
338 case BLOCK: return TS_BLOCK;
339 case CONSTRUCTOR: return TS_CONSTRUCTOR;
340 case TREE_BINFO: return TS_BINFO;
341 case OMP_CLAUSE: return TS_OMP_CLAUSE;
342 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
343 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
345 default:
346 gcc_unreachable ();
351 /* Initialize tree_contains_struct to describe the hierarchy of tree
352 nodes. */
354 static void
355 initialize_tree_contains_struct (void)
357 unsigned i;
359 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
361 enum tree_code code;
362 enum tree_node_structure_enum ts_code;
364 code = (enum tree_code) i;
365 ts_code = tree_node_structure_for_code (code);
367 /* Mark the TS structure itself. */
368 tree_contains_struct[code][ts_code] = 1;
370 /* Mark all the structures that TS is derived from. */
371 switch (ts_code)
373 case TS_TYPED:
374 case TS_BLOCK:
375 MARK_TS_BASE (code);
376 break;
378 case TS_COMMON:
379 case TS_INT_CST:
380 case TS_REAL_CST:
381 case TS_FIXED_CST:
382 case TS_VECTOR:
383 case TS_STRING:
384 case TS_COMPLEX:
385 case TS_SSA_NAME:
386 case TS_CONSTRUCTOR:
387 case TS_EXP:
388 case TS_STATEMENT_LIST:
389 MARK_TS_TYPED (code);
390 break;
392 case TS_IDENTIFIER:
393 case TS_DECL_MINIMAL:
394 case TS_TYPE_COMMON:
395 case TS_LIST:
396 case TS_VEC:
397 case TS_BINFO:
398 case TS_OMP_CLAUSE:
399 case TS_OPTIMIZATION:
400 case TS_TARGET_OPTION:
401 MARK_TS_COMMON (code);
402 break;
404 case TS_TYPE_WITH_LANG_SPECIFIC:
405 MARK_TS_TYPE_COMMON (code);
406 break;
408 case TS_TYPE_NON_COMMON:
409 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
410 break;
412 case TS_DECL_COMMON:
413 MARK_TS_DECL_MINIMAL (code);
414 break;
416 case TS_DECL_WRTL:
417 case TS_CONST_DECL:
418 MARK_TS_DECL_COMMON (code);
419 break;
421 case TS_DECL_NON_COMMON:
422 MARK_TS_DECL_WITH_VIS (code);
423 break;
425 case TS_DECL_WITH_VIS:
426 case TS_PARM_DECL:
427 case TS_LABEL_DECL:
428 case TS_RESULT_DECL:
429 MARK_TS_DECL_WRTL (code);
430 break;
432 case TS_FIELD_DECL:
433 MARK_TS_DECL_COMMON (code);
434 break;
436 case TS_VAR_DECL:
437 MARK_TS_DECL_WITH_VIS (code);
438 break;
440 case TS_TYPE_DECL:
441 case TS_FUNCTION_DECL:
442 MARK_TS_DECL_NON_COMMON (code);
443 break;
445 case TS_TRANSLATION_UNIT_DECL:
446 MARK_TS_DECL_COMMON (code);
447 break;
449 default:
450 gcc_unreachable ();
454 /* Basic consistency checks for attributes used in fold. */
455 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
456 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
457 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
458 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
459 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
460 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
461 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
462 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
463 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
464 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
465 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
466 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
467 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
468 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
469 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
470 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
471 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
472 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
473 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
474 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
475 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
476 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
477 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
478 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
479 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
480 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
481 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
482 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
483 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
484 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
485 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
486 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
487 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
488 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
489 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
490 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
491 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
492 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
496 /* Init tree.c. */
498 void
499 init_ttree (void)
501 /* Initialize the hash table of types. */
502 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
503 type_hash_eq, 0);
505 debug_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
506 tree_decl_map_eq, 0);
508 value_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
509 tree_decl_map_eq, 0);
510 init_priority_for_decl = htab_create_ggc (512, tree_priority_map_hash,
511 tree_priority_map_eq, 0);
513 int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
514 int_cst_hash_eq, NULL);
516 int_cst_node = make_node (INTEGER_CST);
518 cl_option_hash_table = htab_create_ggc (64, cl_option_hash_hash,
519 cl_option_hash_eq, NULL);
521 cl_optimization_node = make_node (OPTIMIZATION_NODE);
522 cl_target_option_node = make_node (TARGET_OPTION_NODE);
524 /* Initialize the tree_contains_struct array. */
525 initialize_tree_contains_struct ();
526 lang_hooks.init_ts ();
530 /* The name of the object as the assembler will see it (but before any
531 translations made by ASM_OUTPUT_LABELREF). Often this is the same
532 as DECL_NAME. It is an IDENTIFIER_NODE. */
533 tree
534 decl_assembler_name (tree decl)
536 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
537 lang_hooks.set_decl_assembler_name (decl);
538 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
541 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
543 bool
544 decl_assembler_name_equal (tree decl, const_tree asmname)
546 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
547 const char *decl_str;
548 const char *asmname_str;
549 bool test = false;
551 if (decl_asmname == asmname)
552 return true;
554 decl_str = IDENTIFIER_POINTER (decl_asmname);
555 asmname_str = IDENTIFIER_POINTER (asmname);
558 /* If the target assembler name was set by the user, things are trickier.
559 We have a leading '*' to begin with. After that, it's arguable what
560 is the correct thing to do with -fleading-underscore. Arguably, we've
561 historically been doing the wrong thing in assemble_alias by always
562 printing the leading underscore. Since we're not changing that, make
563 sure user_label_prefix follows the '*' before matching. */
564 if (decl_str[0] == '*')
566 size_t ulp_len = strlen (user_label_prefix);
568 decl_str ++;
570 if (ulp_len == 0)
571 test = true;
572 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
573 decl_str += ulp_len, test=true;
574 else
575 decl_str --;
577 if (asmname_str[0] == '*')
579 size_t ulp_len = strlen (user_label_prefix);
581 asmname_str ++;
583 if (ulp_len == 0)
584 test = true;
585 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
586 asmname_str += ulp_len, test=true;
587 else
588 asmname_str --;
591 if (!test)
592 return false;
593 return strcmp (decl_str, asmname_str) == 0;
596 /* Hash asmnames ignoring the user specified marks. */
598 hashval_t
599 decl_assembler_name_hash (const_tree asmname)
601 if (IDENTIFIER_POINTER (asmname)[0] == '*')
603 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
604 size_t ulp_len = strlen (user_label_prefix);
606 if (ulp_len == 0)
608 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
609 decl_str += ulp_len;
611 return htab_hash_string (decl_str);
614 return htab_hash_string (IDENTIFIER_POINTER (asmname));
617 /* Compute the number of bytes occupied by a tree with code CODE.
618 This function cannot be used for nodes that have variable sizes,
619 including TREE_VEC, STRING_CST, and CALL_EXPR. */
620 size_t
621 tree_code_size (enum tree_code code)
623 switch (TREE_CODE_CLASS (code))
625 case tcc_declaration: /* A decl node */
627 switch (code)
629 case FIELD_DECL:
630 return sizeof (struct tree_field_decl);
631 case PARM_DECL:
632 return sizeof (struct tree_parm_decl);
633 case VAR_DECL:
634 return sizeof (struct tree_var_decl);
635 case LABEL_DECL:
636 return sizeof (struct tree_label_decl);
637 case RESULT_DECL:
638 return sizeof (struct tree_result_decl);
639 case CONST_DECL:
640 return sizeof (struct tree_const_decl);
641 case TYPE_DECL:
642 return sizeof (struct tree_type_decl);
643 case FUNCTION_DECL:
644 return sizeof (struct tree_function_decl);
645 case DEBUG_EXPR_DECL:
646 return sizeof (struct tree_decl_with_rtl);
647 default:
648 return sizeof (struct tree_decl_non_common);
652 case tcc_type: /* a type node */
653 return sizeof (struct tree_type_non_common);
655 case tcc_reference: /* a reference */
656 case tcc_expression: /* an expression */
657 case tcc_statement: /* an expression with side effects */
658 case tcc_comparison: /* a comparison expression */
659 case tcc_unary: /* a unary arithmetic expression */
660 case tcc_binary: /* a binary arithmetic expression */
661 return (sizeof (struct tree_exp)
662 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
664 case tcc_constant: /* a constant */
665 switch (code)
667 case INTEGER_CST: return sizeof (struct tree_int_cst);
668 case REAL_CST: return sizeof (struct tree_real_cst);
669 case FIXED_CST: return sizeof (struct tree_fixed_cst);
670 case COMPLEX_CST: return sizeof (struct tree_complex);
671 case VECTOR_CST: return sizeof (struct tree_vector);
672 case STRING_CST: gcc_unreachable ();
673 default:
674 return lang_hooks.tree_size (code);
677 case tcc_exceptional: /* something random, like an identifier. */
678 switch (code)
680 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
681 case TREE_LIST: return sizeof (struct tree_list);
683 case ERROR_MARK:
684 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
686 case TREE_VEC:
687 case OMP_CLAUSE: gcc_unreachable ();
689 case SSA_NAME: return sizeof (struct tree_ssa_name);
691 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
692 case BLOCK: return sizeof (struct tree_block);
693 case CONSTRUCTOR: return sizeof (struct tree_constructor);
694 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
695 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
697 default:
698 return lang_hooks.tree_size (code);
701 default:
702 gcc_unreachable ();
706 /* Compute the number of bytes occupied by NODE. This routine only
707 looks at TREE_CODE, except for those nodes that have variable sizes. */
708 size_t
709 tree_size (const_tree node)
711 const enum tree_code code = TREE_CODE (node);
712 switch (code)
714 case TREE_BINFO:
715 return (offsetof (struct tree_binfo, base_binfos)
716 + vec<tree, va_gc>
717 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
719 case TREE_VEC:
720 return (sizeof (struct tree_vec)
721 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
723 case VECTOR_CST:
724 return (sizeof (struct tree_vector)
725 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
727 case STRING_CST:
728 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
730 case OMP_CLAUSE:
731 return (sizeof (struct tree_omp_clause)
732 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
733 * sizeof (tree));
735 default:
736 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
737 return (sizeof (struct tree_exp)
738 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
739 else
740 return tree_code_size (code);
744 /* Record interesting allocation statistics for a tree node with CODE
745 and LENGTH. */
747 static void
748 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
749 size_t length ATTRIBUTE_UNUSED)
751 enum tree_code_class type = TREE_CODE_CLASS (code);
752 tree_node_kind kind;
754 if (!GATHER_STATISTICS)
755 return;
757 switch (type)
759 case tcc_declaration: /* A decl node */
760 kind = d_kind;
761 break;
763 case tcc_type: /* a type node */
764 kind = t_kind;
765 break;
767 case tcc_statement: /* an expression with side effects */
768 kind = s_kind;
769 break;
771 case tcc_reference: /* a reference */
772 kind = r_kind;
773 break;
775 case tcc_expression: /* an expression */
776 case tcc_comparison: /* a comparison expression */
777 case tcc_unary: /* a unary arithmetic expression */
778 case tcc_binary: /* a binary arithmetic expression */
779 kind = e_kind;
780 break;
782 case tcc_constant: /* a constant */
783 kind = c_kind;
784 break;
786 case tcc_exceptional: /* something random, like an identifier. */
787 switch (code)
789 case IDENTIFIER_NODE:
790 kind = id_kind;
791 break;
793 case TREE_VEC:
794 kind = vec_kind;
795 break;
797 case TREE_BINFO:
798 kind = binfo_kind;
799 break;
801 case SSA_NAME:
802 kind = ssa_name_kind;
803 break;
805 case BLOCK:
806 kind = b_kind;
807 break;
809 case CONSTRUCTOR:
810 kind = constr_kind;
811 break;
813 case OMP_CLAUSE:
814 kind = omp_clause_kind;
815 break;
817 default:
818 kind = x_kind;
819 break;
821 break;
823 case tcc_vl_exp:
824 kind = e_kind;
825 break;
827 default:
828 gcc_unreachable ();
831 tree_code_counts[(int) code]++;
832 tree_node_counts[(int) kind]++;
833 tree_node_sizes[(int) kind] += length;
836 /* Allocate and return a new UID from the DECL_UID namespace. */
839 allocate_decl_uid (void)
841 return next_decl_uid++;
844 /* Return a newly allocated node of code CODE. For decl and type
845 nodes, some other fields are initialized. The rest of the node is
846 initialized to zero. This function cannot be used for TREE_VEC or
847 OMP_CLAUSE nodes, which is enforced by asserts in tree_code_size.
849 Achoo! I got a code in the node. */
851 tree
852 make_node_stat (enum tree_code code MEM_STAT_DECL)
854 tree t;
855 enum tree_code_class type = TREE_CODE_CLASS (code);
856 size_t length = tree_code_size (code);
858 record_node_allocation_statistics (code, length);
860 t = ggc_alloc_zone_cleared_tree_node_stat (
861 (code == IDENTIFIER_NODE) ? &tree_id_zone : &tree_zone,
862 length PASS_MEM_STAT);
863 TREE_SET_CODE (t, code);
865 switch (type)
867 case tcc_statement:
868 TREE_SIDE_EFFECTS (t) = 1;
869 break;
871 case tcc_declaration:
872 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
874 if (code == FUNCTION_DECL)
876 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
877 DECL_MODE (t) = FUNCTION_MODE;
879 else
880 DECL_ALIGN (t) = 1;
882 DECL_SOURCE_LOCATION (t) = input_location;
883 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
884 DECL_UID (t) = --next_debug_decl_uid;
885 else
887 DECL_UID (t) = allocate_decl_uid ();
888 SET_DECL_PT_UID (t, -1);
890 if (TREE_CODE (t) == LABEL_DECL)
891 LABEL_DECL_UID (t) = -1;
893 break;
895 case tcc_type:
896 TYPE_UID (t) = next_type_uid++;
897 TYPE_ALIGN (t) = BITS_PER_UNIT;
898 TYPE_USER_ALIGN (t) = 0;
899 TYPE_MAIN_VARIANT (t) = t;
900 TYPE_CANONICAL (t) = t;
902 /* Default to no attributes for type, but let target change that. */
903 TYPE_ATTRIBUTES (t) = NULL_TREE;
904 targetm.set_default_type_attributes (t);
906 /* We have not yet computed the alias set for this type. */
907 TYPE_ALIAS_SET (t) = -1;
908 break;
910 case tcc_constant:
911 TREE_CONSTANT (t) = 1;
912 break;
914 case tcc_expression:
915 switch (code)
917 case INIT_EXPR:
918 case MODIFY_EXPR:
919 case VA_ARG_EXPR:
920 case PREDECREMENT_EXPR:
921 case PREINCREMENT_EXPR:
922 case POSTDECREMENT_EXPR:
923 case POSTINCREMENT_EXPR:
924 /* All of these have side-effects, no matter what their
925 operands are. */
926 TREE_SIDE_EFFECTS (t) = 1;
927 break;
929 default:
930 break;
932 break;
934 default:
935 /* Other classes need no special treatment. */
936 break;
939 return t;
942 /* Return a new node with the same contents as NODE except that its
943 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
945 tree
946 copy_node_stat (tree node MEM_STAT_DECL)
948 tree t;
949 enum tree_code code = TREE_CODE (node);
950 size_t length;
952 gcc_assert (code != STATEMENT_LIST);
954 length = tree_size (node);
955 record_node_allocation_statistics (code, length);
956 t = ggc_alloc_zone_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
957 memcpy (t, node, length);
959 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
960 TREE_CHAIN (t) = 0;
961 TREE_ASM_WRITTEN (t) = 0;
962 TREE_VISITED (t) = 0;
964 if (TREE_CODE_CLASS (code) == tcc_declaration)
966 if (code == DEBUG_EXPR_DECL)
967 DECL_UID (t) = --next_debug_decl_uid;
968 else
970 DECL_UID (t) = allocate_decl_uid ();
971 if (DECL_PT_UID_SET_P (node))
972 SET_DECL_PT_UID (t, DECL_PT_UID (node));
974 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
975 && DECL_HAS_VALUE_EXPR_P (node))
977 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
978 DECL_HAS_VALUE_EXPR_P (t) = 1;
980 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
982 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
983 DECL_HAS_INIT_PRIORITY_P (t) = 1;
985 if (TREE_CODE (node) == FUNCTION_DECL)
986 DECL_STRUCT_FUNCTION (t) = NULL;
988 else if (TREE_CODE_CLASS (code) == tcc_type)
990 TYPE_UID (t) = next_type_uid++;
991 /* The following is so that the debug code for
992 the copy is different from the original type.
993 The two statements usually duplicate each other
994 (because they clear fields of the same union),
995 but the optimizer should catch that. */
996 TYPE_SYMTAB_POINTER (t) = 0;
997 TYPE_SYMTAB_ADDRESS (t) = 0;
999 /* Do not copy the values cache. */
1000 if (TYPE_CACHED_VALUES_P(t))
1002 TYPE_CACHED_VALUES_P (t) = 0;
1003 TYPE_CACHED_VALUES (t) = NULL_TREE;
1006 if (TYPE_HAS_BLOCK_FACTOR (node))
1007 SET_TYPE_BLOCK_FACTOR (t, TYPE_BLOCK_FACTOR (node));
1010 return t;
1013 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1014 For example, this can copy a list made of TREE_LIST nodes. */
1016 tree
1017 copy_list (tree list)
1019 tree head;
1020 tree prev, next;
1022 if (list == 0)
1023 return 0;
1025 head = prev = copy_node (list);
1026 next = TREE_CHAIN (list);
1027 while (next)
1029 TREE_CHAIN (prev) = copy_node (next);
1030 prev = TREE_CHAIN (prev);
1031 next = TREE_CHAIN (next);
1033 return head;
1037 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1039 tree
1040 build_int_cst (tree type, HOST_WIDE_INT low)
1042 /* Support legacy code. */
1043 if (!type)
1044 type = integer_type_node;
1046 return double_int_to_tree (type, double_int::from_shwi (low));
1049 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1051 tree
1052 build_int_cst_type (tree type, HOST_WIDE_INT low)
1054 gcc_assert (type);
1056 return double_int_to_tree (type, double_int::from_shwi (low));
1059 /* Constructs tree in type TYPE from with value given by CST. Signedness
1060 of CST is assumed to be the same as the signedness of TYPE. */
1062 tree
1063 double_int_to_tree (tree type, double_int cst)
1065 bool sign_extended_type = !TYPE_UNSIGNED (type);
1067 cst = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1069 return build_int_cst_wide (type, cst.low, cst.high);
1072 /* Returns true if CST fits into range of TYPE. Signedness of CST is assumed
1073 to be the same as the signedness of TYPE. */
1075 bool
1076 double_int_fits_to_tree_p (const_tree type, double_int cst)
1078 bool sign_extended_type = !TYPE_UNSIGNED (type);
1080 double_int ext
1081 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1083 return cst == ext;
1086 /* We force the double_int CST to the range of the type TYPE by sign or
1087 zero extending it. OVERFLOWABLE indicates if we are interested in
1088 overflow of the value, when >0 we are only interested in signed
1089 overflow, for <0 we are interested in any overflow. OVERFLOWED
1090 indicates whether overflow has already occurred. CONST_OVERFLOWED
1091 indicates whether constant overflow has already occurred. We force
1092 T's value to be within range of T's type (by setting to 0 or 1 all
1093 the bits outside the type's range). We set TREE_OVERFLOWED if,
1094 OVERFLOWED is nonzero,
1095 or OVERFLOWABLE is >0 and signed overflow occurs
1096 or OVERFLOWABLE is <0 and any overflow occurs
1097 We return a new tree node for the extended double_int. The node
1098 is shared if no overflow flags are set. */
1101 tree
1102 force_fit_type_double (tree type, double_int cst, int overflowable,
1103 bool overflowed)
1105 bool sign_extended_type = !TYPE_UNSIGNED (type);
1107 /* If we need to set overflow flags, return a new unshared node. */
1108 if (overflowed || !double_int_fits_to_tree_p(type, cst))
1110 if (overflowed
1111 || overflowable < 0
1112 || (overflowable > 0 && sign_extended_type))
1114 tree t = make_node (INTEGER_CST);
1115 TREE_INT_CST (t)
1116 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1117 TREE_TYPE (t) = type;
1118 TREE_OVERFLOW (t) = 1;
1119 return t;
1123 /* Else build a shared node. */
1124 return double_int_to_tree (type, cst);
1127 /* These are the hash table functions for the hash table of INTEGER_CST
1128 nodes of a sizetype. */
1130 /* Return the hash code code X, an INTEGER_CST. */
1132 static hashval_t
1133 int_cst_hash_hash (const void *x)
1135 const_tree const t = (const_tree) x;
1137 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1138 ^ htab_hash_pointer (TREE_TYPE (t)));
1141 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1142 is the same as that given by *Y, which is the same. */
1144 static int
1145 int_cst_hash_eq (const void *x, const void *y)
1147 const_tree const xt = (const_tree) x;
1148 const_tree const yt = (const_tree) y;
1150 return (TREE_TYPE (xt) == TREE_TYPE (yt)
1151 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1152 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
1155 /* Create an INT_CST node of TYPE and value HI:LOW.
1156 The returned node is always shared. For small integers we use a
1157 per-type vector cache, for larger ones we use a single hash table. */
1159 tree
1160 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
1162 tree t;
1163 int ix = -1;
1164 int limit = 0;
1166 gcc_assert (type);
1168 switch (TREE_CODE (type))
1170 case NULLPTR_TYPE:
1171 gcc_assert (hi == 0 && low == 0);
1172 /* Fallthru. */
1174 case POINTER_TYPE:
1175 case REFERENCE_TYPE:
1176 /* Cache NULL pointer. */
1177 if (!hi && !low)
1179 limit = 1;
1180 ix = 0;
1182 break;
1184 case BOOLEAN_TYPE:
1185 /* Cache false or true. */
1186 limit = 2;
1187 if (!hi && low < 2)
1188 ix = low;
1189 break;
1191 case INTEGER_TYPE:
1192 case OFFSET_TYPE:
1193 if (TYPE_UNSIGNED (type))
1195 /* Cache 0..N */
1196 limit = INTEGER_SHARE_LIMIT;
1197 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1198 ix = low;
1200 else
1202 /* Cache -1..N */
1203 limit = INTEGER_SHARE_LIMIT + 1;
1204 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1205 ix = low + 1;
1206 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1207 ix = 0;
1209 break;
1211 case ENUMERAL_TYPE:
1212 break;
1214 default:
1215 gcc_unreachable ();
1218 if (ix >= 0)
1220 /* Look for it in the type's vector of small shared ints. */
1221 if (!TYPE_CACHED_VALUES_P (type))
1223 TYPE_CACHED_VALUES_P (type) = 1;
1224 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1227 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1228 if (t)
1230 /* Make sure no one is clobbering the shared constant. */
1231 gcc_assert (TREE_TYPE (t) == type);
1232 gcc_assert (TREE_INT_CST_LOW (t) == low);
1233 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
1235 else
1237 /* Create a new shared int. */
1238 t = make_node (INTEGER_CST);
1240 TREE_INT_CST_LOW (t) = low;
1241 TREE_INT_CST_HIGH (t) = hi;
1242 TREE_TYPE (t) = type;
1244 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1247 else
1249 /* Use the cache of larger shared ints. */
1250 void **slot;
1252 TREE_INT_CST_LOW (int_cst_node) = low;
1253 TREE_INT_CST_HIGH (int_cst_node) = hi;
1254 TREE_TYPE (int_cst_node) = type;
1256 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
1257 t = (tree) *slot;
1258 if (!t)
1260 /* Insert this one into the hash table. */
1261 t = int_cst_node;
1262 *slot = t;
1263 /* Make a new node for next time round. */
1264 int_cst_node = make_node (INTEGER_CST);
1268 return t;
1271 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1272 and the rest are zeros. */
1274 tree
1275 build_low_bits_mask (tree type, unsigned bits)
1277 double_int mask;
1279 gcc_assert (bits <= TYPE_PRECISION (type));
1281 if (bits == TYPE_PRECISION (type)
1282 && !TYPE_UNSIGNED (type))
1283 /* Sign extended all-ones mask. */
1284 mask = double_int_minus_one;
1285 else
1286 mask = double_int::mask (bits);
1288 return build_int_cst_wide (type, mask.low, mask.high);
1291 /* Checks that X is integer constant that can be expressed in (unsigned)
1292 HOST_WIDE_INT without loss of precision. */
1294 bool
1295 cst_and_fits_in_hwi (const_tree x)
1297 if (TREE_CODE (x) != INTEGER_CST)
1298 return false;
1300 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1301 return false;
1303 return (TREE_INT_CST_HIGH (x) == 0
1304 || TREE_INT_CST_HIGH (x) == -1);
1307 /* Build a newly constructed TREE_VEC node of length LEN. */
1309 tree
1310 make_vector_stat (unsigned len MEM_STAT_DECL)
1312 tree t;
1313 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1315 record_node_allocation_statistics (VECTOR_CST, length);
1317 t = ggc_alloc_zone_cleared_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
1319 TREE_SET_CODE (t, VECTOR_CST);
1320 TREE_CONSTANT (t) = 1;
1322 return t;
1325 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1326 are in a list pointed to by VALS. */
1328 tree
1329 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1331 int over = 0;
1332 unsigned cnt = 0;
1333 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1334 TREE_TYPE (v) = type;
1336 /* Iterate through elements and check for overflow. */
1337 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1339 tree value = vals[cnt];
1341 VECTOR_CST_ELT (v, cnt) = value;
1343 /* Don't crash if we get an address constant. */
1344 if (!CONSTANT_CLASS_P (value))
1345 continue;
1347 over |= TREE_OVERFLOW (value);
1350 TREE_OVERFLOW (v) = over;
1351 return v;
1354 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1355 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1357 tree
1358 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1360 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1361 unsigned HOST_WIDE_INT idx;
1362 tree value;
1364 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1365 vec[idx] = value;
1366 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1367 vec[idx] = build_zero_cst (TREE_TYPE (type));
1369 return build_vector (type, vec);
1372 /* Build a vector of type VECTYPE where all the elements are SCs. */
1373 tree
1374 build_vector_from_val (tree vectype, tree sc)
1376 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1378 if (sc == error_mark_node)
1379 return sc;
1381 /* Verify that the vector type is suitable for SC. Note that there
1382 is some inconsistency in the type-system with respect to restrict
1383 qualifications of pointers. Vector types always have a main-variant
1384 element type and the qualification is applied to the vector-type.
1385 So TREE_TYPE (vector-type) does not return a properly qualified
1386 vector element-type. */
1387 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1388 TREE_TYPE (vectype)));
1390 if (CONSTANT_CLASS_P (sc))
1392 tree *v = XALLOCAVEC (tree, nunits);
1393 for (i = 0; i < nunits; ++i)
1394 v[i] = sc;
1395 return build_vector (vectype, v);
1397 else
1399 vec<constructor_elt, va_gc> *v;
1400 vec_alloc (v, nunits);
1401 for (i = 0; i < nunits; ++i)
1402 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1403 return build_constructor (vectype, v);
1407 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1408 are in the vec pointed to by VALS. */
1409 tree
1410 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1412 tree c = make_node (CONSTRUCTOR);
1413 unsigned int i;
1414 constructor_elt *elt;
1415 bool constant_p = true;
1416 bool side_effects_p = false;
1418 TREE_TYPE (c) = type;
1419 CONSTRUCTOR_ELTS (c) = vals;
1421 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1423 /* Mostly ctors will have elts that don't have side-effects, so
1424 the usual case is to scan all the elements. Hence a single
1425 loop for both const and side effects, rather than one loop
1426 each (with early outs). */
1427 if (!TREE_CONSTANT (elt->value))
1428 constant_p = false;
1429 if (TREE_SIDE_EFFECTS (elt->value))
1430 side_effects_p = true;
1433 TREE_SIDE_EFFECTS (c) = side_effects_p;
1434 TREE_CONSTANT (c) = constant_p;
1436 return c;
1439 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1440 INDEX and VALUE. */
1441 tree
1442 build_constructor_single (tree type, tree index, tree value)
1444 vec<constructor_elt, va_gc> *v;
1445 constructor_elt elt = {index, value};
1447 vec_alloc (v, 1);
1448 v->quick_push (elt);
1450 return build_constructor (type, v);
1454 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1455 are in a list pointed to by VALS. */
1456 tree
1457 build_constructor_from_list (tree type, tree vals)
1459 tree t;
1460 vec<constructor_elt, va_gc> *v = NULL;
1462 if (vals)
1464 vec_alloc (v, list_length (vals));
1465 for (t = vals; t; t = TREE_CHAIN (t))
1466 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1469 return build_constructor (type, v);
1472 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1474 tree
1475 build_fixed (tree type, FIXED_VALUE_TYPE f)
1477 tree v;
1478 FIXED_VALUE_TYPE *fp;
1480 v = make_node (FIXED_CST);
1481 fp = ggc_alloc_fixed_value ();
1482 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1484 TREE_TYPE (v) = type;
1485 TREE_FIXED_CST_PTR (v) = fp;
1486 return v;
1489 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1491 tree
1492 build_real (tree type, REAL_VALUE_TYPE d)
1494 tree v;
1495 REAL_VALUE_TYPE *dp;
1496 int overflow = 0;
1498 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1499 Consider doing it via real_convert now. */
1501 v = make_node (REAL_CST);
1502 dp = ggc_alloc_real_value ();
1503 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1505 TREE_TYPE (v) = type;
1506 TREE_REAL_CST_PTR (v) = dp;
1507 TREE_OVERFLOW (v) = overflow;
1508 return v;
1511 /* Return a new REAL_CST node whose type is TYPE
1512 and whose value is the integer value of the INTEGER_CST node I. */
1514 REAL_VALUE_TYPE
1515 real_value_from_int_cst (const_tree type, const_tree i)
1517 REAL_VALUE_TYPE d;
1519 /* Clear all bits of the real value type so that we can later do
1520 bitwise comparisons to see if two values are the same. */
1521 memset (&d, 0, sizeof d);
1523 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1524 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1525 TYPE_UNSIGNED (TREE_TYPE (i)));
1526 return d;
1529 /* Given a tree representing an integer constant I, return a tree
1530 representing the same value as a floating-point constant of type TYPE. */
1532 tree
1533 build_real_from_int_cst (tree type, const_tree i)
1535 tree v;
1536 int overflow = TREE_OVERFLOW (i);
1538 v = build_real (type, real_value_from_int_cst (type, i));
1540 TREE_OVERFLOW (v) |= overflow;
1541 return v;
1544 /* Return a newly constructed STRING_CST node whose value is
1545 the LEN characters at STR.
1546 Note that for a C string literal, LEN should include the trailing NUL.
1547 The TREE_TYPE is not initialized. */
1549 tree
1550 build_string (int len, const char *str)
1552 tree s;
1553 size_t length;
1555 /* Do not waste bytes provided by padding of struct tree_string. */
1556 length = len + offsetof (struct tree_string, str) + 1;
1558 record_node_allocation_statistics (STRING_CST, length);
1560 s = ggc_alloc_tree_node (length);
1562 memset (s, 0, sizeof (struct tree_typed));
1563 TREE_SET_CODE (s, STRING_CST);
1564 TREE_CONSTANT (s) = 1;
1565 TREE_STRING_LENGTH (s) = len;
1566 memcpy (s->string.str, str, len);
1567 s->string.str[len] = '\0';
1569 return s;
1572 /* Return a newly constructed COMPLEX_CST node whose value is
1573 specified by the real and imaginary parts REAL and IMAG.
1574 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1575 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1577 tree
1578 build_complex (tree type, tree real, tree imag)
1580 tree t = make_node (COMPLEX_CST);
1582 TREE_REALPART (t) = real;
1583 TREE_IMAGPART (t) = imag;
1584 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1585 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1586 return t;
1589 /* Return a constant of arithmetic type TYPE which is the
1590 multiplicative identity of the set TYPE. */
1592 tree
1593 build_one_cst (tree type)
1595 switch (TREE_CODE (type))
1597 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1598 case POINTER_TYPE: case REFERENCE_TYPE:
1599 case OFFSET_TYPE:
1600 return build_int_cst (type, 1);
1602 case REAL_TYPE:
1603 return build_real (type, dconst1);
1605 case FIXED_POINT_TYPE:
1606 /* We can only generate 1 for accum types. */
1607 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1608 return build_fixed (type, FCONST1(TYPE_MODE (type)));
1610 case VECTOR_TYPE:
1612 tree scalar = build_one_cst (TREE_TYPE (type));
1614 return build_vector_from_val (type, scalar);
1617 case COMPLEX_TYPE:
1618 return build_complex (type,
1619 build_one_cst (TREE_TYPE (type)),
1620 build_zero_cst (TREE_TYPE (type)));
1622 default:
1623 gcc_unreachable ();
1627 /* Build 0 constant of type TYPE. This is used by constructor folding
1628 and thus the constant should be represented in memory by
1629 zero(es). */
1631 tree
1632 build_zero_cst (tree type)
1634 switch (TREE_CODE (type))
1636 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1637 case POINTER_TYPE: case REFERENCE_TYPE:
1638 case OFFSET_TYPE: case NULLPTR_TYPE:
1639 return build_int_cst (type, 0);
1641 case REAL_TYPE:
1642 return build_real (type, dconst0);
1644 case FIXED_POINT_TYPE:
1645 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
1647 case VECTOR_TYPE:
1649 tree scalar = build_zero_cst (TREE_TYPE (type));
1651 return build_vector_from_val (type, scalar);
1654 case COMPLEX_TYPE:
1656 tree zero = build_zero_cst (TREE_TYPE (type));
1658 return build_complex (type, zero, zero);
1661 default:
1662 if (!AGGREGATE_TYPE_P (type))
1663 return fold_convert (type, integer_zero_node);
1664 return build_constructor (type, NULL);
1669 /* Build a BINFO with LEN language slots. */
1671 tree
1672 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1674 tree t;
1675 size_t length = (offsetof (struct tree_binfo, base_binfos)
1676 + vec<tree, va_gc>::embedded_size (base_binfos));
1678 record_node_allocation_statistics (TREE_BINFO, length);
1680 t = ggc_alloc_zone_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
1682 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1684 TREE_SET_CODE (t, TREE_BINFO);
1686 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
1688 return t;
1691 /* Create a CASE_LABEL_EXPR tree node and return it. */
1693 tree
1694 build_case_label (tree low_value, tree high_value, tree label_decl)
1696 tree t = make_node (CASE_LABEL_EXPR);
1698 TREE_TYPE (t) = void_type_node;
1699 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
1701 CASE_LOW (t) = low_value;
1702 CASE_HIGH (t) = high_value;
1703 CASE_LABEL (t) = label_decl;
1704 CASE_CHAIN (t) = NULL_TREE;
1706 return t;
1709 /* Build a newly constructed TREE_VEC node of length LEN. */
1711 tree
1712 make_tree_vec_stat (int len MEM_STAT_DECL)
1714 tree t;
1715 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1717 record_node_allocation_statistics (TREE_VEC, length);
1719 t = ggc_alloc_zone_cleared_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
1721 TREE_SET_CODE (t, TREE_VEC);
1722 TREE_VEC_LENGTH (t) = len;
1724 return t;
1727 /* Return 1 if EXPR is the integer constant zero or a complex constant
1728 of zero. */
1731 integer_zerop (const_tree expr)
1733 STRIP_NOPS (expr);
1735 switch (TREE_CODE (expr))
1737 case INTEGER_CST:
1738 return (TREE_INT_CST_LOW (expr) == 0
1739 && TREE_INT_CST_HIGH (expr) == 0);
1740 case COMPLEX_CST:
1741 return (integer_zerop (TREE_REALPART (expr))
1742 && integer_zerop (TREE_IMAGPART (expr)));
1743 case VECTOR_CST:
1745 unsigned i;
1746 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1747 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
1748 return false;
1749 return true;
1751 default:
1752 return false;
1756 /* Return 1 if EXPR is the integer constant one or the corresponding
1757 complex constant. */
1760 integer_onep (const_tree expr)
1762 STRIP_NOPS (expr);
1764 switch (TREE_CODE (expr))
1766 case INTEGER_CST:
1767 return (TREE_INT_CST_LOW (expr) == 1
1768 && TREE_INT_CST_HIGH (expr) == 0);
1769 case COMPLEX_CST:
1770 return (integer_onep (TREE_REALPART (expr))
1771 && integer_zerop (TREE_IMAGPART (expr)));
1772 case VECTOR_CST:
1774 unsigned i;
1775 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1776 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
1777 return false;
1778 return true;
1780 default:
1781 return false;
1785 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1786 it contains. Likewise for the corresponding complex constant. */
1789 integer_all_onesp (const_tree expr)
1791 int prec;
1792 int uns;
1794 STRIP_NOPS (expr);
1796 if (TREE_CODE (expr) == COMPLEX_CST
1797 && integer_all_onesp (TREE_REALPART (expr))
1798 && integer_zerop (TREE_IMAGPART (expr)))
1799 return 1;
1801 else if (TREE_CODE (expr) == VECTOR_CST)
1803 unsigned i;
1804 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1805 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
1806 return 0;
1807 return 1;
1810 else if (TREE_CODE (expr) != INTEGER_CST)
1811 return 0;
1813 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1814 if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1815 && TREE_INT_CST_HIGH (expr) == -1)
1816 return 1;
1817 if (!uns)
1818 return 0;
1820 prec = TYPE_PRECISION (TREE_TYPE (expr));
1821 if (prec >= HOST_BITS_PER_WIDE_INT)
1823 HOST_WIDE_INT high_value;
1824 int shift_amount;
1826 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1828 /* Can not handle precisions greater than twice the host int size. */
1829 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
1830 if (shift_amount == HOST_BITS_PER_WIDE_INT)
1831 /* Shifting by the host word size is undefined according to the ANSI
1832 standard, so we must handle this as a special case. */
1833 high_value = -1;
1834 else
1835 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1837 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1838 && TREE_INT_CST_HIGH (expr) == high_value);
1840 else
1841 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1844 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1845 one bit on). */
1848 integer_pow2p (const_tree expr)
1850 int prec;
1851 unsigned HOST_WIDE_INT high, low;
1853 STRIP_NOPS (expr);
1855 if (TREE_CODE (expr) == COMPLEX_CST
1856 && integer_pow2p (TREE_REALPART (expr))
1857 && integer_zerop (TREE_IMAGPART (expr)))
1858 return 1;
1860 if (TREE_CODE (expr) != INTEGER_CST)
1861 return 0;
1863 prec = TYPE_PRECISION (TREE_TYPE (expr));
1864 high = TREE_INT_CST_HIGH (expr);
1865 low = TREE_INT_CST_LOW (expr);
1867 /* First clear all bits that are beyond the type's precision in case
1868 we've been sign extended. */
1870 if (prec == HOST_BITS_PER_DOUBLE_INT)
1872 else if (prec > HOST_BITS_PER_WIDE_INT)
1873 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1874 else
1876 high = 0;
1877 if (prec < HOST_BITS_PER_WIDE_INT)
1878 low &= ~((HOST_WIDE_INT) (-1) << prec);
1881 if (high == 0 && low == 0)
1882 return 0;
1884 return ((high == 0 && (low & (low - 1)) == 0)
1885 || (low == 0 && (high & (high - 1)) == 0));
1888 /* Return 1 if EXPR is an integer constant other than zero or a
1889 complex constant other than zero. */
1892 integer_nonzerop (const_tree expr)
1894 STRIP_NOPS (expr);
1896 return ((TREE_CODE (expr) == INTEGER_CST
1897 && (TREE_INT_CST_LOW (expr) != 0
1898 || TREE_INT_CST_HIGH (expr) != 0))
1899 || (TREE_CODE (expr) == COMPLEX_CST
1900 && (integer_nonzerop (TREE_REALPART (expr))
1901 || integer_nonzerop (TREE_IMAGPART (expr)))));
1904 /* Return 1 if EXPR is the fixed-point constant zero. */
1907 fixed_zerop (const_tree expr)
1909 return (TREE_CODE (expr) == FIXED_CST
1910 && TREE_FIXED_CST (expr).data.is_zero ());
1913 /* Return the power of two represented by a tree node known to be a
1914 power of two. */
1917 tree_log2 (const_tree expr)
1919 int prec;
1920 HOST_WIDE_INT high, low;
1922 STRIP_NOPS (expr);
1924 if (TREE_CODE (expr) == COMPLEX_CST)
1925 return tree_log2 (TREE_REALPART (expr));
1927 prec = TYPE_PRECISION (TREE_TYPE (expr));
1928 high = TREE_INT_CST_HIGH (expr);
1929 low = TREE_INT_CST_LOW (expr);
1931 /* First clear all bits that are beyond the type's precision in case
1932 we've been sign extended. */
1934 if (prec == HOST_BITS_PER_DOUBLE_INT)
1936 else if (prec > HOST_BITS_PER_WIDE_INT)
1937 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1938 else
1940 high = 0;
1941 if (prec < HOST_BITS_PER_WIDE_INT)
1942 low &= ~((HOST_WIDE_INT) (-1) << prec);
1945 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
1946 : exact_log2 (low));
1949 /* Similar, but return the largest integer Y such that 2 ** Y is less
1950 than or equal to EXPR. */
1953 tree_floor_log2 (const_tree expr)
1955 int prec;
1956 HOST_WIDE_INT high, low;
1958 STRIP_NOPS (expr);
1960 if (TREE_CODE (expr) == COMPLEX_CST)
1961 return tree_log2 (TREE_REALPART (expr));
1963 prec = TYPE_PRECISION (TREE_TYPE (expr));
1964 high = TREE_INT_CST_HIGH (expr);
1965 low = TREE_INT_CST_LOW (expr);
1967 /* First clear all bits that are beyond the type's precision in case
1968 we've been sign extended. Ignore if type's precision hasn't been set
1969 since what we are doing is setting it. */
1971 if (prec == HOST_BITS_PER_DOUBLE_INT || prec == 0)
1973 else if (prec > HOST_BITS_PER_WIDE_INT)
1974 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1975 else
1977 high = 0;
1978 if (prec < HOST_BITS_PER_WIDE_INT)
1979 low &= ~((HOST_WIDE_INT) (-1) << prec);
1982 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
1983 : floor_log2 (low));
1986 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
1987 decimal float constants, so don't return 1 for them. */
1990 real_zerop (const_tree expr)
1992 STRIP_NOPS (expr);
1994 switch (TREE_CODE (expr))
1996 case REAL_CST:
1997 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
1998 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
1999 case COMPLEX_CST:
2000 return real_zerop (TREE_REALPART (expr))
2001 && real_zerop (TREE_IMAGPART (expr));
2002 case VECTOR_CST:
2004 unsigned i;
2005 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2006 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2007 return false;
2008 return true;
2010 default:
2011 return false;
2015 /* Return 1 if EXPR is the real constant one in real or complex form.
2016 Trailing zeroes matter for decimal float constants, so don't return
2017 1 for them. */
2020 real_onep (const_tree expr)
2022 STRIP_NOPS (expr);
2024 switch (TREE_CODE (expr))
2026 case REAL_CST:
2027 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2028 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2029 case COMPLEX_CST:
2030 return real_onep (TREE_REALPART (expr))
2031 && real_zerop (TREE_IMAGPART (expr));
2032 case VECTOR_CST:
2034 unsigned i;
2035 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2036 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2037 return false;
2038 return true;
2040 default:
2041 return false;
2045 /* Return 1 if EXPR is the real constant two. Trailing zeroes matter
2046 for decimal float constants, so don't return 1 for them. */
2049 real_twop (const_tree expr)
2051 STRIP_NOPS (expr);
2053 switch (TREE_CODE (expr))
2055 case REAL_CST:
2056 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2)
2057 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2058 case COMPLEX_CST:
2059 return real_twop (TREE_REALPART (expr))
2060 && real_zerop (TREE_IMAGPART (expr));
2061 case VECTOR_CST:
2063 unsigned i;
2064 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2065 if (!real_twop (VECTOR_CST_ELT (expr, i)))
2066 return false;
2067 return true;
2069 default:
2070 return false;
2074 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2075 matter for decimal float constants, so don't return 1 for them. */
2078 real_minus_onep (const_tree expr)
2080 STRIP_NOPS (expr);
2082 switch (TREE_CODE (expr))
2084 case REAL_CST:
2085 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2086 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2087 case COMPLEX_CST:
2088 return real_minus_onep (TREE_REALPART (expr))
2089 && real_zerop (TREE_IMAGPART (expr));
2090 case VECTOR_CST:
2092 unsigned i;
2093 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2094 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2095 return false;
2096 return true;
2098 default:
2099 return false;
2103 /* Nonzero if EXP is a constant or a cast of a constant. */
2106 really_constant_p (const_tree exp)
2108 /* This is not quite the same as STRIP_NOPS. It does more. */
2109 while (CONVERT_EXPR_P (exp)
2110 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2111 exp = TREE_OPERAND (exp, 0);
2112 return TREE_CONSTANT (exp);
2115 /* Return first list element whose TREE_VALUE is ELEM.
2116 Return 0 if ELEM is not in LIST. */
2118 tree
2119 value_member (tree elem, tree list)
2121 while (list)
2123 if (elem == TREE_VALUE (list))
2124 return list;
2125 list = TREE_CHAIN (list);
2127 return NULL_TREE;
2130 /* Return first list element whose TREE_PURPOSE is ELEM.
2131 Return 0 if ELEM is not in LIST. */
2133 tree
2134 purpose_member (const_tree elem, tree list)
2136 while (list)
2138 if (elem == TREE_PURPOSE (list))
2139 return list;
2140 list = TREE_CHAIN (list);
2142 return NULL_TREE;
2145 /* Return true if ELEM is in V. */
2147 bool
2148 vec_member (const_tree elem, vec<tree, va_gc> *v)
2150 unsigned ix;
2151 tree t;
2152 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2153 if (elem == t)
2154 return true;
2155 return false;
2158 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2159 NULL_TREE. */
2161 tree
2162 chain_index (int idx, tree chain)
2164 for (; chain && idx > 0; --idx)
2165 chain = TREE_CHAIN (chain);
2166 return chain;
2169 /* Return nonzero if ELEM is part of the chain CHAIN. */
2172 chain_member (const_tree elem, const_tree chain)
2174 while (chain)
2176 if (elem == chain)
2177 return 1;
2178 chain = DECL_CHAIN (chain);
2181 return 0;
2184 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2185 We expect a null pointer to mark the end of the chain.
2186 This is the Lisp primitive `length'. */
2189 list_length (const_tree t)
2191 const_tree p = t;
2192 #ifdef ENABLE_TREE_CHECKING
2193 const_tree q = t;
2194 #endif
2195 int len = 0;
2197 while (p)
2199 p = TREE_CHAIN (p);
2200 #ifdef ENABLE_TREE_CHECKING
2201 if (len % 2)
2202 q = TREE_CHAIN (q);
2203 gcc_assert (p != q);
2204 #endif
2205 len++;
2208 return len;
2211 /* Returns the number of FIELD_DECLs in TYPE. */
2214 fields_length (const_tree type)
2216 tree t = TYPE_FIELDS (type);
2217 int count = 0;
2219 for (; t; t = DECL_CHAIN (t))
2220 if (TREE_CODE (t) == FIELD_DECL)
2221 ++count;
2223 return count;
2226 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2227 UNION_TYPE TYPE, or NULL_TREE if none. */
2229 tree
2230 first_field (const_tree type)
2232 tree t = TYPE_FIELDS (type);
2233 while (t && TREE_CODE (t) != FIELD_DECL)
2234 t = TREE_CHAIN (t);
2235 return t;
2238 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2239 by modifying the last node in chain 1 to point to chain 2.
2240 This is the Lisp primitive `nconc'. */
2242 tree
2243 chainon (tree op1, tree op2)
2245 tree t1;
2247 if (!op1)
2248 return op2;
2249 if (!op2)
2250 return op1;
2252 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2253 continue;
2254 TREE_CHAIN (t1) = op2;
2256 #ifdef ENABLE_TREE_CHECKING
2258 tree t2;
2259 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2260 gcc_assert (t2 != t1);
2262 #endif
2264 return op1;
2267 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2269 tree
2270 tree_last (tree chain)
2272 tree next;
2273 if (chain)
2274 while ((next = TREE_CHAIN (chain)))
2275 chain = next;
2276 return chain;
2279 /* Reverse the order of elements in the chain T,
2280 and return the new head of the chain (old last element). */
2282 tree
2283 nreverse (tree t)
2285 tree prev = 0, decl, next;
2286 for (decl = t; decl; decl = next)
2288 /* We shouldn't be using this function to reverse BLOCK chains; we
2289 have blocks_nreverse for that. */
2290 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2291 next = TREE_CHAIN (decl);
2292 TREE_CHAIN (decl) = prev;
2293 prev = decl;
2295 return prev;
2298 /* Return a newly created TREE_LIST node whose
2299 purpose and value fields are PARM and VALUE. */
2301 tree
2302 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2304 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2305 TREE_PURPOSE (t) = parm;
2306 TREE_VALUE (t) = value;
2307 return t;
2310 /* Build a chain of TREE_LIST nodes from a vector. */
2312 tree
2313 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2315 tree ret = NULL_TREE;
2316 tree *pp = &ret;
2317 unsigned int i;
2318 tree t;
2319 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2321 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2322 pp = &TREE_CHAIN (*pp);
2324 return ret;
2327 /* Return a newly created TREE_LIST node whose
2328 purpose and value fields are PURPOSE and VALUE
2329 and whose TREE_CHAIN is CHAIN. */
2331 tree
2332 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2334 tree node;
2336 node = ggc_alloc_zone_tree_node_stat (&tree_zone, sizeof (struct tree_list)
2337 PASS_MEM_STAT);
2338 memset (node, 0, sizeof (struct tree_common));
2340 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2342 TREE_SET_CODE (node, TREE_LIST);
2343 TREE_CHAIN (node) = chain;
2344 TREE_PURPOSE (node) = purpose;
2345 TREE_VALUE (node) = value;
2346 return node;
2349 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2350 trees. */
2352 vec<tree, va_gc> *
2353 ctor_to_vec (tree ctor)
2355 vec<tree, va_gc> *vec;
2356 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2357 unsigned int ix;
2358 tree val;
2360 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2361 vec->quick_push (val);
2363 return vec;
2366 /* Return the size nominally occupied by an object of type TYPE
2367 when it resides in memory. The value is measured in units of bytes,
2368 and its data type is that normally used for type sizes
2369 (which is the first type created by make_signed_type or
2370 make_unsigned_type). */
2372 tree
2373 size_in_bytes (const_tree type)
2375 tree t;
2377 if (type == error_mark_node)
2378 return integer_zero_node;
2380 type = TYPE_MAIN_VARIANT (type);
2381 t = TYPE_SIZE_UNIT (type);
2383 if (t == 0)
2385 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2386 return size_zero_node;
2389 return t;
2392 /* Return the size of TYPE (in bytes) as a wide integer
2393 or return -1 if the size can vary or is larger than an integer. */
2395 HOST_WIDE_INT
2396 int_size_in_bytes (const_tree type)
2398 tree t;
2400 if (type == error_mark_node)
2401 return 0;
2403 type = TYPE_MAIN_VARIANT (type);
2404 t = TYPE_SIZE_UNIT (type);
2405 if (t == 0
2406 || TREE_CODE (t) != INTEGER_CST
2407 || TREE_INT_CST_HIGH (t) != 0
2408 /* If the result would appear negative, it's too big to represent. */
2409 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
2410 return -1;
2412 return TREE_INT_CST_LOW (t);
2415 /* Return the maximum size of TYPE (in bytes) as a wide integer
2416 or return -1 if the size can vary or is larger than an integer. */
2418 HOST_WIDE_INT
2419 max_int_size_in_bytes (const_tree type)
2421 HOST_WIDE_INT size = -1;
2422 tree size_tree;
2424 /* If this is an array type, check for a possible MAX_SIZE attached. */
2426 if (TREE_CODE (type) == ARRAY_TYPE)
2428 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2430 if (size_tree && host_integerp (size_tree, 1))
2431 size = tree_low_cst (size_tree, 1);
2434 /* If we still haven't been able to get a size, see if the language
2435 can compute a maximum size. */
2437 if (size == -1)
2439 size_tree = lang_hooks.types.max_size (type);
2441 if (size_tree && host_integerp (size_tree, 1))
2442 size = tree_low_cst (size_tree, 1);
2445 return size;
2448 /* Returns a tree for the size of EXP in bytes. */
2450 tree
2451 tree_expr_size (const_tree exp)
2453 if (DECL_P (exp)
2454 && DECL_SIZE_UNIT (exp) != 0)
2455 return DECL_SIZE_UNIT (exp);
2456 else
2457 return size_in_bytes (TREE_TYPE (exp));
2460 /* Return the bit position of FIELD, in bits from the start of the record.
2461 This is a tree of type bitsizetype. */
2463 tree
2464 bit_position (const_tree field)
2466 return bit_from_pos (DECL_FIELD_OFFSET (field),
2467 DECL_FIELD_BIT_OFFSET (field));
2470 /* Likewise, but return as an integer. It must be representable in
2471 that way (since it could be a signed value, we don't have the
2472 option of returning -1 like int_size_in_byte can. */
2474 HOST_WIDE_INT
2475 int_bit_position (const_tree field)
2477 return tree_low_cst (bit_position (field), 0);
2480 /* Return the byte position of FIELD, in bytes from the start of the record.
2481 This is a tree of type sizetype. */
2483 tree
2484 byte_position (const_tree field)
2486 return byte_from_pos (DECL_FIELD_OFFSET (field),
2487 DECL_FIELD_BIT_OFFSET (field));
2490 /* Likewise, but return as an integer. It must be representable in
2491 that way (since it could be a signed value, we don't have the
2492 option of returning -1 like int_size_in_byte can. */
2494 HOST_WIDE_INT
2495 int_byte_position (const_tree field)
2497 return tree_low_cst (byte_position (field), 0);
2500 /* Return the strictest alignment, in bits, that T is known to have. */
2502 unsigned int
2503 expr_align (const_tree t)
2505 unsigned int align0, align1;
2507 switch (TREE_CODE (t))
2509 CASE_CONVERT: case NON_LVALUE_EXPR:
2510 /* If we have conversions, we know that the alignment of the
2511 object must meet each of the alignments of the types. */
2512 align0 = expr_align (TREE_OPERAND (t, 0));
2513 align1 = TYPE_ALIGN (TREE_TYPE (t));
2514 return MAX (align0, align1);
2516 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2517 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2518 case CLEANUP_POINT_EXPR:
2519 /* These don't change the alignment of an object. */
2520 return expr_align (TREE_OPERAND (t, 0));
2522 case COND_EXPR:
2523 /* The best we can do is say that the alignment is the least aligned
2524 of the two arms. */
2525 align0 = expr_align (TREE_OPERAND (t, 1));
2526 align1 = expr_align (TREE_OPERAND (t, 2));
2527 return MIN (align0, align1);
2529 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2530 meaningfully, it's always 1. */
2531 case LABEL_DECL: case CONST_DECL:
2532 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2533 case FUNCTION_DECL:
2534 gcc_assert (DECL_ALIGN (t) != 0);
2535 return DECL_ALIGN (t);
2537 default:
2538 break;
2541 /* Otherwise take the alignment from that of the type. */
2542 return TYPE_ALIGN (TREE_TYPE (t));
2545 /* Return, as a tree node, the number of elements for TYPE (which is an
2546 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2548 tree
2549 array_type_nelts (const_tree type)
2551 tree index_type, min, max;
2553 /* If they did it with unspecified bounds, then we should have already
2554 given an error about it before we got here. */
2555 if (! TYPE_DOMAIN (type))
2556 return error_mark_node;
2558 index_type = TYPE_DOMAIN (type);
2559 min = TYPE_MIN_VALUE (index_type);
2560 max = TYPE_MAX_VALUE (index_type);
2562 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
2563 if (!max)
2564 return error_mark_node;
2566 return (integer_zerop (min)
2567 ? max
2568 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2571 /* If arg is static -- a reference to an object in static storage -- then
2572 return the object. This is not the same as the C meaning of `static'.
2573 If arg isn't static, return NULL. */
2575 tree
2576 staticp (tree arg)
2578 switch (TREE_CODE (arg))
2580 case FUNCTION_DECL:
2581 /* Nested functions are static, even though taking their address will
2582 involve a trampoline as we unnest the nested function and create
2583 the trampoline on the tree level. */
2584 return arg;
2586 case VAR_DECL:
2587 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2588 && ! DECL_THREAD_LOCAL_P (arg)
2589 && ! DECL_DLLIMPORT_P (arg)
2590 ? arg : NULL);
2592 case CONST_DECL:
2593 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2594 ? arg : NULL);
2596 case CONSTRUCTOR:
2597 return TREE_STATIC (arg) ? arg : NULL;
2599 case LABEL_DECL:
2600 case STRING_CST:
2601 return arg;
2603 case COMPONENT_REF:
2604 /* If the thing being referenced is not a field, then it is
2605 something language specific. */
2606 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2608 /* If we are referencing a bitfield, we can't evaluate an
2609 ADDR_EXPR at compile time and so it isn't a constant. */
2610 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2611 return NULL;
2613 return staticp (TREE_OPERAND (arg, 0));
2615 case BIT_FIELD_REF:
2616 return NULL;
2618 case INDIRECT_REF:
2619 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2621 case ARRAY_REF:
2622 case ARRAY_RANGE_REF:
2623 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2624 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2625 return staticp (TREE_OPERAND (arg, 0));
2626 else
2627 return NULL;
2629 case COMPOUND_LITERAL_EXPR:
2630 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2632 default:
2633 return NULL;
2640 /* Return whether OP is a DECL whose address is function-invariant. */
2642 bool
2643 decl_address_invariant_p (const_tree op)
2645 /* The conditions below are slightly less strict than the one in
2646 staticp. */
2648 switch (TREE_CODE (op))
2650 case PARM_DECL:
2651 case RESULT_DECL:
2652 case LABEL_DECL:
2653 case FUNCTION_DECL:
2654 return true;
2656 case VAR_DECL:
2657 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2658 || DECL_THREAD_LOCAL_P (op)
2659 || DECL_CONTEXT (op) == current_function_decl
2660 || decl_function_context (op) == current_function_decl)
2661 return true;
2662 break;
2664 case CONST_DECL:
2665 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2666 || decl_function_context (op) == current_function_decl)
2667 return true;
2668 break;
2670 default:
2671 break;
2674 return false;
2677 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2679 bool
2680 decl_address_ip_invariant_p (const_tree op)
2682 /* The conditions below are slightly less strict than the one in
2683 staticp. */
2685 switch (TREE_CODE (op))
2687 case LABEL_DECL:
2688 case FUNCTION_DECL:
2689 case STRING_CST:
2690 return true;
2692 case VAR_DECL:
2693 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2694 && !DECL_DLLIMPORT_P (op))
2695 || DECL_THREAD_LOCAL_P (op))
2696 return true;
2697 break;
2699 case CONST_DECL:
2700 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2701 return true;
2702 break;
2704 default:
2705 break;
2708 return false;
2712 /* Return true if T is function-invariant (internal function, does
2713 not handle arithmetic; that's handled in skip_simple_arithmetic and
2714 tree_invariant_p). */
2716 static bool tree_invariant_p (tree t);
2718 static bool
2719 tree_invariant_p_1 (tree t)
2721 tree op;
2723 if (TREE_CONSTANT (t)
2724 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2725 return true;
2727 switch (TREE_CODE (t))
2729 case SAVE_EXPR:
2730 return true;
2732 case ADDR_EXPR:
2733 op = TREE_OPERAND (t, 0);
2734 while (handled_component_p (op))
2736 switch (TREE_CODE (op))
2738 case ARRAY_REF:
2739 case ARRAY_RANGE_REF:
2740 if (!tree_invariant_p (TREE_OPERAND (op, 1))
2741 || TREE_OPERAND (op, 2) != NULL_TREE
2742 || TREE_OPERAND (op, 3) != NULL_TREE)
2743 return false;
2744 break;
2746 case COMPONENT_REF:
2747 if (TREE_OPERAND (op, 2) != NULL_TREE)
2748 return false;
2749 break;
2751 default:;
2753 op = TREE_OPERAND (op, 0);
2756 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2758 default:
2759 break;
2762 return false;
2765 /* Return true if T is function-invariant. */
2767 static bool
2768 tree_invariant_p (tree t)
2770 tree inner = skip_simple_arithmetic (t);
2771 return tree_invariant_p_1 (inner);
2774 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2775 Do this to any expression which may be used in more than one place,
2776 but must be evaluated only once.
2778 Normally, expand_expr would reevaluate the expression each time.
2779 Calling save_expr produces something that is evaluated and recorded
2780 the first time expand_expr is called on it. Subsequent calls to
2781 expand_expr just reuse the recorded value.
2783 The call to expand_expr that generates code that actually computes
2784 the value is the first call *at compile time*. Subsequent calls
2785 *at compile time* generate code to use the saved value.
2786 This produces correct result provided that *at run time* control
2787 always flows through the insns made by the first expand_expr
2788 before reaching the other places where the save_expr was evaluated.
2789 You, the caller of save_expr, must make sure this is so.
2791 Constants, and certain read-only nodes, are returned with no
2792 SAVE_EXPR because that is safe. Expressions containing placeholders
2793 are not touched; see tree.def for an explanation of what these
2794 are used for. */
2796 tree
2797 save_expr (tree expr)
2799 tree t = fold (expr);
2800 tree inner;
2802 /* If the tree evaluates to a constant, then we don't want to hide that
2803 fact (i.e. this allows further folding, and direct checks for constants).
2804 However, a read-only object that has side effects cannot be bypassed.
2805 Since it is no problem to reevaluate literals, we just return the
2806 literal node. */
2807 inner = skip_simple_arithmetic (t);
2808 if (TREE_CODE (inner) == ERROR_MARK)
2809 return inner;
2811 if (tree_invariant_p_1 (inner))
2812 return t;
2814 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2815 it means that the size or offset of some field of an object depends on
2816 the value within another field.
2818 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2819 and some variable since it would then need to be both evaluated once and
2820 evaluated more than once. Front-ends must assure this case cannot
2821 happen by surrounding any such subexpressions in their own SAVE_EXPR
2822 and forcing evaluation at the proper time. */
2823 if (contains_placeholder_p (inner))
2824 return t;
2826 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
2827 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
2829 /* This expression might be placed ahead of a jump to ensure that the
2830 value was computed on both sides of the jump. So make sure it isn't
2831 eliminated as dead. */
2832 TREE_SIDE_EFFECTS (t) = 1;
2833 return t;
2836 /* Look inside EXPR and into any simple arithmetic operations. Return
2837 the innermost non-arithmetic node. */
2839 tree
2840 skip_simple_arithmetic (tree expr)
2842 tree inner;
2844 /* We don't care about whether this can be used as an lvalue in this
2845 context. */
2846 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
2847 expr = TREE_OPERAND (expr, 0);
2849 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
2850 a constant, it will be more efficient to not make another SAVE_EXPR since
2851 it will allow better simplification and GCSE will be able to merge the
2852 computations if they actually occur. */
2853 inner = expr;
2854 while (1)
2856 if (UNARY_CLASS_P (inner))
2857 inner = TREE_OPERAND (inner, 0);
2858 else if (BINARY_CLASS_P (inner))
2860 if (tree_invariant_p (TREE_OPERAND (inner, 1)))
2861 inner = TREE_OPERAND (inner, 0);
2862 else if (tree_invariant_p (TREE_OPERAND (inner, 0)))
2863 inner = TREE_OPERAND (inner, 1);
2864 else
2865 break;
2867 else
2868 break;
2871 return inner;
2875 /* Return which tree structure is used by T. */
2877 enum tree_node_structure_enum
2878 tree_node_structure (const_tree t)
2880 const enum tree_code code = TREE_CODE (t);
2881 return tree_node_structure_for_code (code);
2884 /* Set various status flags when building a CALL_EXPR object T. */
2886 static void
2887 process_call_operands (tree t)
2889 bool side_effects = TREE_SIDE_EFFECTS (t);
2890 bool read_only = false;
2891 int i = call_expr_flags (t);
2893 /* Calls have side-effects, except those to const or pure functions. */
2894 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
2895 side_effects = true;
2896 /* Propagate TREE_READONLY of arguments for const functions. */
2897 if (i & ECF_CONST)
2898 read_only = true;
2900 if (!side_effects || read_only)
2901 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
2903 tree op = TREE_OPERAND (t, i);
2904 if (op && TREE_SIDE_EFFECTS (op))
2905 side_effects = true;
2906 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
2907 read_only = false;
2910 TREE_SIDE_EFFECTS (t) = side_effects;
2911 TREE_READONLY (t) = read_only;
2914 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
2915 size or offset that depends on a field within a record. */
2917 bool
2918 contains_placeholder_p (const_tree exp)
2920 enum tree_code code;
2922 if (!exp)
2923 return 0;
2925 code = TREE_CODE (exp);
2926 if (code == PLACEHOLDER_EXPR)
2927 return 1;
2929 switch (TREE_CODE_CLASS (code))
2931 case tcc_reference:
2932 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
2933 position computations since they will be converted into a
2934 WITH_RECORD_EXPR involving the reference, which will assume
2935 here will be valid. */
2936 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2938 case tcc_exceptional:
2939 if (code == TREE_LIST)
2940 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
2941 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
2942 break;
2944 case tcc_unary:
2945 case tcc_binary:
2946 case tcc_comparison:
2947 case tcc_expression:
2948 switch (code)
2950 case COMPOUND_EXPR:
2951 /* Ignoring the first operand isn't quite right, but works best. */
2952 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
2954 case COND_EXPR:
2955 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2956 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
2957 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
2959 case SAVE_EXPR:
2960 /* The save_expr function never wraps anything containing
2961 a PLACEHOLDER_EXPR. */
2962 return 0;
2964 default:
2965 break;
2968 switch (TREE_CODE_LENGTH (code))
2970 case 1:
2971 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2972 case 2:
2973 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2974 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
2975 default:
2976 return 0;
2979 case tcc_vl_exp:
2980 switch (code)
2982 case CALL_EXPR:
2984 const_tree arg;
2985 const_call_expr_arg_iterator iter;
2986 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
2987 if (CONTAINS_PLACEHOLDER_P (arg))
2988 return 1;
2989 return 0;
2991 default:
2992 return 0;
2995 default:
2996 return 0;
2998 return 0;
3001 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3002 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3003 field positions. */
3005 static bool
3006 type_contains_placeholder_1 (const_tree type)
3008 /* If the size contains a placeholder or the parent type (component type in
3009 the case of arrays) type involves a placeholder, this type does. */
3010 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3011 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3012 || (!POINTER_TYPE_P (type)
3013 && TREE_TYPE (type)
3014 && type_contains_placeholder_p (TREE_TYPE (type))))
3015 return true;
3017 /* Now do type-specific checks. Note that the last part of the check above
3018 greatly limits what we have to do below. */
3019 switch (TREE_CODE (type))
3021 case VOID_TYPE:
3022 case COMPLEX_TYPE:
3023 case ENUMERAL_TYPE:
3024 case BOOLEAN_TYPE:
3025 case POINTER_TYPE:
3026 case OFFSET_TYPE:
3027 case REFERENCE_TYPE:
3028 case METHOD_TYPE:
3029 case FUNCTION_TYPE:
3030 case VECTOR_TYPE:
3031 case NULLPTR_TYPE:
3032 return false;
3034 case INTEGER_TYPE:
3035 case REAL_TYPE:
3036 case FIXED_POINT_TYPE:
3037 /* Here we just check the bounds. */
3038 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3039 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3041 case ARRAY_TYPE:
3042 /* We have already checked the component type above, so just check the
3043 domain type. */
3044 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3046 case RECORD_TYPE:
3047 case UNION_TYPE:
3048 case QUAL_UNION_TYPE:
3050 tree field;
3052 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3053 if (TREE_CODE (field) == FIELD_DECL
3054 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3055 || (TREE_CODE (type) == QUAL_UNION_TYPE
3056 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3057 || type_contains_placeholder_p (TREE_TYPE (field))))
3058 return true;
3060 return false;
3063 default:
3064 gcc_unreachable ();
3068 /* Wrapper around above function used to cache its result. */
3070 bool
3071 type_contains_placeholder_p (tree type)
3073 bool result;
3075 /* If the contains_placeholder_bits field has been initialized,
3076 then we know the answer. */
3077 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3078 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3080 /* Indicate that we've seen this type node, and the answer is false.
3081 This is what we want to return if we run into recursion via fields. */
3082 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3084 /* Compute the real value. */
3085 result = type_contains_placeholder_1 (type);
3087 /* Store the real value. */
3088 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3090 return result;
3093 /* Push tree EXP onto vector QUEUE if it is not already present. */
3095 static void
3096 push_without_duplicates (tree exp, vec<tree> *queue)
3098 unsigned int i;
3099 tree iter;
3101 FOR_EACH_VEC_ELT (*queue, i, iter)
3102 if (simple_cst_equal (iter, exp) == 1)
3103 break;
3105 if (!iter)
3106 queue->safe_push (exp);
3109 /* Given a tree EXP, find all occurrences of references to fields
3110 in a PLACEHOLDER_EXPR and place them in vector REFS without
3111 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3112 we assume here that EXP contains only arithmetic expressions
3113 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3114 argument list. */
3116 void
3117 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3119 enum tree_code code = TREE_CODE (exp);
3120 tree inner;
3121 int i;
3123 /* We handle TREE_LIST and COMPONENT_REF separately. */
3124 if (code == TREE_LIST)
3126 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3127 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3129 else if (code == COMPONENT_REF)
3131 for (inner = TREE_OPERAND (exp, 0);
3132 REFERENCE_CLASS_P (inner);
3133 inner = TREE_OPERAND (inner, 0))
3136 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3137 push_without_duplicates (exp, refs);
3138 else
3139 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3141 else
3142 switch (TREE_CODE_CLASS (code))
3144 case tcc_constant:
3145 break;
3147 case tcc_declaration:
3148 /* Variables allocated to static storage can stay. */
3149 if (!TREE_STATIC (exp))
3150 push_without_duplicates (exp, refs);
3151 break;
3153 case tcc_expression:
3154 /* This is the pattern built in ada/make_aligning_type. */
3155 if (code == ADDR_EXPR
3156 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3158 push_without_duplicates (exp, refs);
3159 break;
3162 /* Fall through... */
3164 case tcc_exceptional:
3165 case tcc_unary:
3166 case tcc_binary:
3167 case tcc_comparison:
3168 case tcc_reference:
3169 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3170 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3171 break;
3173 case tcc_vl_exp:
3174 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3175 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3176 break;
3178 default:
3179 gcc_unreachable ();
3183 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3184 return a tree with all occurrences of references to F in a
3185 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3186 CONST_DECLs. Note that we assume here that EXP contains only
3187 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3188 occurring only in their argument list. */
3190 tree
3191 substitute_in_expr (tree exp, tree f, tree r)
3193 enum tree_code code = TREE_CODE (exp);
3194 tree op0, op1, op2, op3;
3195 tree new_tree;
3197 /* We handle TREE_LIST and COMPONENT_REF separately. */
3198 if (code == TREE_LIST)
3200 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3201 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3202 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3203 return exp;
3205 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3207 else if (code == COMPONENT_REF)
3209 tree inner;
3211 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3212 and it is the right field, replace it with R. */
3213 for (inner = TREE_OPERAND (exp, 0);
3214 REFERENCE_CLASS_P (inner);
3215 inner = TREE_OPERAND (inner, 0))
3218 /* The field. */
3219 op1 = TREE_OPERAND (exp, 1);
3221 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3222 return r;
3224 /* If this expression hasn't been completed let, leave it alone. */
3225 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3226 return exp;
3228 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3229 if (op0 == TREE_OPERAND (exp, 0))
3230 return exp;
3232 new_tree
3233 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3235 else
3236 switch (TREE_CODE_CLASS (code))
3238 case tcc_constant:
3239 return exp;
3241 case tcc_declaration:
3242 if (exp == f)
3243 return r;
3244 else
3245 return exp;
3247 case tcc_expression:
3248 if (exp == f)
3249 return r;
3251 /* Fall through... */
3253 case tcc_exceptional:
3254 case tcc_unary:
3255 case tcc_binary:
3256 case tcc_comparison:
3257 case tcc_reference:
3258 switch (TREE_CODE_LENGTH (code))
3260 case 0:
3261 return exp;
3263 case 1:
3264 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3265 if (op0 == TREE_OPERAND (exp, 0))
3266 return exp;
3268 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3269 break;
3271 case 2:
3272 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3273 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3275 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3276 return exp;
3278 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3279 break;
3281 case 3:
3282 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3283 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3284 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3286 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3287 && op2 == TREE_OPERAND (exp, 2))
3288 return exp;
3290 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3291 break;
3293 case 4:
3294 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3295 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3296 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3297 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3299 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3300 && op2 == TREE_OPERAND (exp, 2)
3301 && op3 == TREE_OPERAND (exp, 3))
3302 return exp;
3304 new_tree
3305 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3306 break;
3308 default:
3309 gcc_unreachable ();
3311 break;
3313 case tcc_vl_exp:
3315 int i;
3317 new_tree = NULL_TREE;
3319 /* If we are trying to replace F with a constant, inline back
3320 functions which do nothing else than computing a value from
3321 the arguments they are passed. This makes it possible to
3322 fold partially or entirely the replacement expression. */
3323 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3325 tree t = maybe_inline_call_in_expr (exp);
3326 if (t)
3327 return SUBSTITUTE_IN_EXPR (t, f, r);
3330 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3332 tree op = TREE_OPERAND (exp, i);
3333 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3334 if (new_op != op)
3336 if (!new_tree)
3337 new_tree = copy_node (exp);
3338 TREE_OPERAND (new_tree, i) = new_op;
3342 if (new_tree)
3344 new_tree = fold (new_tree);
3345 if (TREE_CODE (new_tree) == CALL_EXPR)
3346 process_call_operands (new_tree);
3348 else
3349 return exp;
3351 break;
3353 default:
3354 gcc_unreachable ();
3357 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3359 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3360 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3362 return new_tree;
3365 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3366 for it within OBJ, a tree that is an object or a chain of references. */
3368 tree
3369 substitute_placeholder_in_expr (tree exp, tree obj)
3371 enum tree_code code = TREE_CODE (exp);
3372 tree op0, op1, op2, op3;
3373 tree new_tree;
3375 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3376 in the chain of OBJ. */
3377 if (code == PLACEHOLDER_EXPR)
3379 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3380 tree elt;
3382 for (elt = obj; elt != 0;
3383 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3384 || TREE_CODE (elt) == COND_EXPR)
3385 ? TREE_OPERAND (elt, 1)
3386 : (REFERENCE_CLASS_P (elt)
3387 || UNARY_CLASS_P (elt)
3388 || BINARY_CLASS_P (elt)
3389 || VL_EXP_CLASS_P (elt)
3390 || EXPRESSION_CLASS_P (elt))
3391 ? TREE_OPERAND (elt, 0) : 0))
3392 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3393 return elt;
3395 for (elt = obj; elt != 0;
3396 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3397 || TREE_CODE (elt) == COND_EXPR)
3398 ? TREE_OPERAND (elt, 1)
3399 : (REFERENCE_CLASS_P (elt)
3400 || UNARY_CLASS_P (elt)
3401 || BINARY_CLASS_P (elt)
3402 || VL_EXP_CLASS_P (elt)
3403 || EXPRESSION_CLASS_P (elt))
3404 ? TREE_OPERAND (elt, 0) : 0))
3405 if (POINTER_TYPE_P (TREE_TYPE (elt))
3406 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3407 == need_type))
3408 return fold_build1 (INDIRECT_REF, need_type, elt);
3410 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3411 survives until RTL generation, there will be an error. */
3412 return exp;
3415 /* TREE_LIST is special because we need to look at TREE_VALUE
3416 and TREE_CHAIN, not TREE_OPERANDS. */
3417 else if (code == TREE_LIST)
3419 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3420 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3421 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3422 return exp;
3424 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3426 else
3427 switch (TREE_CODE_CLASS (code))
3429 case tcc_constant:
3430 case tcc_declaration:
3431 return exp;
3433 case tcc_exceptional:
3434 case tcc_unary:
3435 case tcc_binary:
3436 case tcc_comparison:
3437 case tcc_expression:
3438 case tcc_reference:
3439 case tcc_statement:
3440 switch (TREE_CODE_LENGTH (code))
3442 case 0:
3443 return exp;
3445 case 1:
3446 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3447 if (op0 == TREE_OPERAND (exp, 0))
3448 return exp;
3450 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3451 break;
3453 case 2:
3454 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3455 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3457 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3458 return exp;
3460 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3461 break;
3463 case 3:
3464 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3465 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3466 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3468 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3469 && op2 == TREE_OPERAND (exp, 2))
3470 return exp;
3472 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3473 break;
3475 case 4:
3476 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3477 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3478 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3479 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3481 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3482 && op2 == TREE_OPERAND (exp, 2)
3483 && op3 == TREE_OPERAND (exp, 3))
3484 return exp;
3486 new_tree
3487 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3488 break;
3490 default:
3491 gcc_unreachable ();
3493 break;
3495 case tcc_vl_exp:
3497 int i;
3499 new_tree = NULL_TREE;
3501 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3503 tree op = TREE_OPERAND (exp, i);
3504 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3505 if (new_op != op)
3507 if (!new_tree)
3508 new_tree = copy_node (exp);
3509 TREE_OPERAND (new_tree, i) = new_op;
3513 if (new_tree)
3515 new_tree = fold (new_tree);
3516 if (TREE_CODE (new_tree) == CALL_EXPR)
3517 process_call_operands (new_tree);
3519 else
3520 return exp;
3522 break;
3524 default:
3525 gcc_unreachable ();
3528 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3530 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3531 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3533 return new_tree;
3536 /* Stabilize a reference so that we can use it any number of times
3537 without causing its operands to be evaluated more than once.
3538 Returns the stabilized reference. This works by means of save_expr,
3539 so see the caveats in the comments about save_expr.
3541 Also allows conversion expressions whose operands are references.
3542 Any other kind of expression is returned unchanged. */
3544 tree
3545 stabilize_reference (tree ref)
3547 tree result;
3548 enum tree_code code = TREE_CODE (ref);
3550 switch (code)
3552 case VAR_DECL:
3553 case PARM_DECL:
3554 case RESULT_DECL:
3555 /* No action is needed in this case. */
3556 return ref;
3558 CASE_CONVERT:
3559 case FLOAT_EXPR:
3560 case FIX_TRUNC_EXPR:
3561 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
3562 break;
3564 case INDIRECT_REF:
3565 result = build_nt (INDIRECT_REF,
3566 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
3567 break;
3569 case COMPONENT_REF:
3570 result = build_nt (COMPONENT_REF,
3571 stabilize_reference (TREE_OPERAND (ref, 0)),
3572 TREE_OPERAND (ref, 1), NULL_TREE);
3573 break;
3575 case BIT_FIELD_REF:
3576 result = build_nt (BIT_FIELD_REF,
3577 stabilize_reference (TREE_OPERAND (ref, 0)),
3578 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
3579 break;
3581 case ARRAY_REF:
3582 result = build_nt (ARRAY_REF,
3583 stabilize_reference (TREE_OPERAND (ref, 0)),
3584 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3585 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3586 break;
3588 case ARRAY_RANGE_REF:
3589 result = build_nt (ARRAY_RANGE_REF,
3590 stabilize_reference (TREE_OPERAND (ref, 0)),
3591 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3592 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3593 break;
3595 case COMPOUND_EXPR:
3596 /* We cannot wrap the first expression in a SAVE_EXPR, as then
3597 it wouldn't be ignored. This matters when dealing with
3598 volatiles. */
3599 return stabilize_reference_1 (ref);
3601 /* If arg isn't a kind of lvalue we recognize, make no change.
3602 Caller should recognize the error for an invalid lvalue. */
3603 default:
3604 return ref;
3606 case ERROR_MARK:
3607 return error_mark_node;
3610 TREE_TYPE (result) = TREE_TYPE (ref);
3611 TREE_READONLY (result) = TREE_READONLY (ref);
3612 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
3613 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3614 TREE_SHARED (result) = TREE_SHARED (ref);
3615 TREE_STRICT (result) = TREE_STRICT (ref);
3616 TREE_RELAXED (result) = TREE_RELAXED (ref);
3618 return result;
3621 /* Subroutine of stabilize_reference; this is called for subtrees of
3622 references. Any expression with side-effects must be put in a SAVE_EXPR
3623 to ensure that it is only evaluated once.
3625 We don't put SAVE_EXPR nodes around everything, because assigning very
3626 simple expressions to temporaries causes us to miss good opportunities
3627 for optimizations. Among other things, the opportunity to fold in the
3628 addition of a constant into an addressing mode often gets lost, e.g.
3629 "y[i+1] += x;". In general, we take the approach that we should not make
3630 an assignment unless we are forced into it - i.e., that any non-side effect
3631 operator should be allowed, and that cse should take care of coalescing
3632 multiple utterances of the same expression should that prove fruitful. */
3634 tree
3635 stabilize_reference_1 (tree e)
3637 tree result;
3638 enum tree_code code = TREE_CODE (e);
3640 /* We cannot ignore const expressions because it might be a reference
3641 to a const array but whose index contains side-effects. But we can
3642 ignore things that are actual constant or that already have been
3643 handled by this function. */
3645 if (tree_invariant_p (e))
3646 return e;
3648 switch (TREE_CODE_CLASS (code))
3650 case tcc_exceptional:
3651 case tcc_type:
3652 case tcc_declaration:
3653 case tcc_comparison:
3654 case tcc_statement:
3655 case tcc_expression:
3656 case tcc_reference:
3657 case tcc_vl_exp:
3658 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3659 so that it will only be evaluated once. */
3660 /* The reference (r) and comparison (<) classes could be handled as
3661 below, but it is generally faster to only evaluate them once. */
3662 if (TREE_SIDE_EFFECTS (e))
3663 return save_expr (e);
3664 return e;
3666 case tcc_constant:
3667 /* Constants need no processing. In fact, we should never reach
3668 here. */
3669 return e;
3671 case tcc_binary:
3672 /* Division is slow and tends to be compiled with jumps,
3673 especially the division by powers of 2 that is often
3674 found inside of an array reference. So do it just once. */
3675 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3676 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3677 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3678 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3679 return save_expr (e);
3680 /* Recursively stabilize each operand. */
3681 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3682 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3683 break;
3685 case tcc_unary:
3686 /* Recursively stabilize each operand. */
3687 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3688 break;
3690 default:
3691 gcc_unreachable ();
3694 TREE_TYPE (result) = TREE_TYPE (e);
3695 TREE_READONLY (result) = TREE_READONLY (e);
3696 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3697 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3698 TREE_SHARED (result) = TREE_SHARED (e);
3699 TREE_STRICT (result) = TREE_STRICT (e);
3700 TREE_RELAXED (result) = TREE_RELAXED (e);
3702 return result;
3705 /* Low-level constructors for expressions. */
3707 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
3708 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
3710 void
3711 recompute_tree_invariant_for_addr_expr (tree t)
3713 tree node;
3714 bool tc = true, se = false;
3716 /* We started out assuming this address is both invariant and constant, but
3717 does not have side effects. Now go down any handled components and see if
3718 any of them involve offsets that are either non-constant or non-invariant.
3719 Also check for side-effects.
3721 ??? Note that this code makes no attempt to deal with the case where
3722 taking the address of something causes a copy due to misalignment. */
3724 #define UPDATE_FLAGS(NODE) \
3725 do { tree _node = (NODE); \
3726 if (_node && !TREE_CONSTANT (_node)) tc = false; \
3727 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3729 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3730 node = TREE_OPERAND (node, 0))
3732 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3733 array reference (probably made temporarily by the G++ front end),
3734 so ignore all the operands. */
3735 if ((TREE_CODE (node) == ARRAY_REF
3736 || TREE_CODE (node) == ARRAY_RANGE_REF)
3737 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
3739 UPDATE_FLAGS (TREE_OPERAND (node, 1));
3740 if (TREE_OPERAND (node, 2))
3741 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3742 if (TREE_OPERAND (node, 3))
3743 UPDATE_FLAGS (TREE_OPERAND (node, 3));
3745 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
3746 FIELD_DECL, apparently. The G++ front end can put something else
3747 there, at least temporarily. */
3748 else if (TREE_CODE (node) == COMPONENT_REF
3749 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
3751 if (TREE_OPERAND (node, 2))
3752 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3756 node = lang_hooks.expr_to_decl (node, &tc, &se);
3758 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
3759 the address, since &(*a)->b is a form of addition. If it's a constant, the
3760 address is constant too. If it's a decl, its address is constant if the
3761 decl is static. Everything else is not constant and, furthermore,
3762 taking the address of a volatile variable is not volatile. */
3763 if (TREE_CODE (node) == INDIRECT_REF
3764 || TREE_CODE (node) == MEM_REF)
3765 UPDATE_FLAGS (TREE_OPERAND (node, 0));
3766 else if (CONSTANT_CLASS_P (node))
3768 else if (DECL_P (node))
3769 tc &= (staticp (node) != NULL_TREE);
3770 else
3772 tc = false;
3773 se |= TREE_SIDE_EFFECTS (node);
3777 TREE_CONSTANT (t) = tc;
3778 TREE_SIDE_EFFECTS (t) = se;
3779 #undef UPDATE_FLAGS
3782 /* Build an expression of code CODE, data type TYPE, and operands as
3783 specified. Expressions and reference nodes can be created this way.
3784 Constants, decls, types and misc nodes cannot be.
3786 We define 5 non-variadic functions, from 0 to 4 arguments. This is
3787 enough for all extant tree codes. */
3789 tree
3790 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
3792 tree t;
3794 gcc_assert (TREE_CODE_LENGTH (code) == 0);
3796 t = make_node_stat (code PASS_MEM_STAT);
3797 TREE_TYPE (t) = tt;
3799 return t;
3802 tree
3803 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
3805 int length = sizeof (struct tree_exp);
3806 tree t;
3808 record_node_allocation_statistics (code, length);
3810 gcc_assert (TREE_CODE_LENGTH (code) == 1);
3812 t = ggc_alloc_zone_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
3814 memset (t, 0, sizeof (struct tree_common));
3816 TREE_SET_CODE (t, code);
3818 TREE_TYPE (t) = type;
3819 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
3820 TREE_OPERAND (t, 0) = node;
3821 if (node && !TYPE_P (node))
3823 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
3824 TREE_READONLY (t) = TREE_READONLY (node);
3827 if (TREE_CODE_CLASS (code) == tcc_statement)
3828 TREE_SIDE_EFFECTS (t) = 1;
3829 else switch (code)
3831 case VA_ARG_EXPR:
3832 /* All of these have side-effects, no matter what their
3833 operands are. */
3834 TREE_SIDE_EFFECTS (t) = 1;
3835 TREE_READONLY (t) = 0;
3836 break;
3838 case INDIRECT_REF:
3839 /* Whether a dereference is readonly has nothing to do with whether
3840 its operand is readonly. */
3841 TREE_READONLY (t) = 0;
3842 TREE_SHARED (t) = upc_shared_type_p (type);
3843 break;
3845 case ADDR_EXPR:
3846 if (node)
3847 recompute_tree_invariant_for_addr_expr (t);
3848 break;
3850 default:
3851 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
3852 && node && !TYPE_P (node)
3853 && TREE_CONSTANT (node))
3854 TREE_CONSTANT (t) = 1;
3855 if (TREE_CODE_CLASS (code) == tcc_reference
3856 && node && TREE_THIS_VOLATILE (node))
3857 TREE_THIS_VOLATILE (t) = 1;
3858 /* Drop the UPC "shared" type qualifier for
3859 expressions involving UPC shared objects. */
3860 if (TREE_CODE_CLASS (code) == tcc_unary
3861 && node && !TYPE_P (node)
3862 && upc_shared_type_p (type))
3863 TREE_TYPE (t) = build_upc_unshared_type (type);
3864 break;
3867 return t;
3870 #define PROCESS_ARG(N) \
3871 do { \
3872 TREE_OPERAND (t, N) = arg##N; \
3873 if (arg##N &&!TYPE_P (arg##N)) \
3875 if (TREE_SIDE_EFFECTS (arg##N)) \
3876 side_effects = 1; \
3877 if (!TREE_READONLY (arg##N) \
3878 && !CONSTANT_CLASS_P (arg##N)) \
3879 (void) (read_only = 0); \
3880 if (!TREE_CONSTANT (arg##N)) \
3881 (void) (constant = 0); \
3883 } while (0)
3885 tree
3886 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
3888 bool constant, read_only, side_effects;
3889 tree t;
3891 gcc_assert (TREE_CODE_LENGTH (code) == 2);
3893 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
3894 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
3895 /* When sizetype precision doesn't match that of pointers
3896 we need to be able to build explicit extensions or truncations
3897 of the offset argument. */
3898 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
3899 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
3900 && TREE_CODE (arg1) == INTEGER_CST);
3902 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
3903 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
3904 && ptrofftype_p (TREE_TYPE (arg1)));
3906 t = make_node_stat (code PASS_MEM_STAT);
3908 /* Remove UPC shared type qualifiers from the result type. */
3909 if (upc_shared_type_p (tt))
3910 tt = build_upc_unshared_type (tt);
3911 TREE_TYPE (t) = tt;
3913 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
3914 result based on those same flags for the arguments. But if the
3915 arguments aren't really even `tree' expressions, we shouldn't be trying
3916 to do this. */
3918 /* Expressions without side effects may be constant if their
3919 arguments are as well. */
3920 constant = (TREE_CODE_CLASS (code) == tcc_comparison
3921 || TREE_CODE_CLASS (code) == tcc_binary);
3922 read_only = 1;
3923 side_effects = TREE_SIDE_EFFECTS (t);
3925 PROCESS_ARG(0);
3926 PROCESS_ARG(1);
3928 TREE_READONLY (t) = read_only;
3929 TREE_CONSTANT (t) = constant;
3930 TREE_SIDE_EFFECTS (t) = side_effects;
3931 TREE_THIS_VOLATILE (t)
3932 = (TREE_CODE_CLASS (code) == tcc_reference
3933 && arg0 && TREE_THIS_VOLATILE (arg0));
3935 return t;
3939 tree
3940 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3941 tree arg2 MEM_STAT_DECL)
3943 bool constant, read_only, side_effects;
3944 tree t;
3946 gcc_assert (TREE_CODE_LENGTH (code) == 3);
3947 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3949 t = make_node_stat (code PASS_MEM_STAT);
3950 TREE_TYPE (t) = tt;
3952 read_only = 1;
3954 /* As a special exception, if COND_EXPR has NULL branches, we
3955 assume that it is a gimple statement and always consider
3956 it to have side effects. */
3957 if (code == COND_EXPR
3958 && tt == void_type_node
3959 && arg1 == NULL_TREE
3960 && arg2 == NULL_TREE)
3961 side_effects = true;
3962 else
3963 side_effects = TREE_SIDE_EFFECTS (t);
3965 PROCESS_ARG(0);
3966 PROCESS_ARG(1);
3967 PROCESS_ARG(2);
3969 if (code == COND_EXPR)
3970 TREE_READONLY (t) = read_only;
3972 TREE_SIDE_EFFECTS (t) = side_effects;
3973 TREE_THIS_VOLATILE (t)
3974 = (TREE_CODE_CLASS (code) == tcc_reference
3975 && arg0 && TREE_THIS_VOLATILE (arg0));
3976 TREE_SHARED (t)
3977 = (TREE_CODE_CLASS (code) == tcc_reference
3978 && arg0 && TREE_SHARED (arg0));
3979 if (TREE_SHARED (t))
3981 TREE_STRICT (t) = TREE_STRICT (arg0);
3982 TREE_RELAXED (t) = TREE_RELAXED (arg0);
3985 return t;
3988 tree
3989 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3990 tree arg2, tree arg3 MEM_STAT_DECL)
3992 bool constant, read_only, side_effects;
3993 tree t;
3995 gcc_assert (TREE_CODE_LENGTH (code) == 4);
3997 t = make_node_stat (code PASS_MEM_STAT);
3998 TREE_TYPE (t) = tt;
4000 side_effects = TREE_SIDE_EFFECTS (t);
4002 PROCESS_ARG(0);
4003 PROCESS_ARG(1);
4004 PROCESS_ARG(2);
4005 PROCESS_ARG(3);
4007 TREE_SIDE_EFFECTS (t) = side_effects;
4008 TREE_THIS_VOLATILE (t)
4009 = (TREE_CODE_CLASS (code) == tcc_reference
4010 && arg0 && TREE_THIS_VOLATILE (arg0));
4012 return t;
4015 tree
4016 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4017 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4019 bool constant, read_only, side_effects;
4020 tree t;
4022 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4024 t = make_node_stat (code PASS_MEM_STAT);
4025 TREE_TYPE (t) = tt;
4027 side_effects = TREE_SIDE_EFFECTS (t);
4029 PROCESS_ARG(0);
4030 PROCESS_ARG(1);
4031 PROCESS_ARG(2);
4032 PROCESS_ARG(3);
4033 PROCESS_ARG(4);
4035 TREE_SIDE_EFFECTS (t) = side_effects;
4036 TREE_THIS_VOLATILE (t)
4037 = (TREE_CODE_CLASS (code) == tcc_reference
4038 && arg0 && TREE_THIS_VOLATILE (arg0));
4040 return t;
4043 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4044 on the pointer PTR. */
4046 tree
4047 build_simple_mem_ref_loc (location_t loc, tree ptr)
4049 HOST_WIDE_INT offset = 0;
4050 tree ptype = TREE_TYPE (ptr);
4051 tree tem;
4052 /* For convenience allow addresses that collapse to a simple base
4053 and offset. */
4054 if (TREE_CODE (ptr) == ADDR_EXPR
4055 && (handled_component_p (TREE_OPERAND (ptr, 0))
4056 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4058 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4059 gcc_assert (ptr);
4060 ptr = build_fold_addr_expr (ptr);
4061 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4063 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4064 ptr, build_int_cst (ptype, offset));
4065 SET_EXPR_LOCATION (tem, loc);
4066 return tem;
4069 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4071 double_int
4072 mem_ref_offset (const_tree t)
4074 tree toff = TREE_OPERAND (t, 1);
4075 return tree_to_double_int (toff).sext (TYPE_PRECISION (TREE_TYPE (toff)));
4078 /* Return the pointer-type relevant for TBAA purposes from the
4079 gimple memory reference tree T. This is the type to be used for
4080 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T. */
4082 tree
4083 reference_alias_ptr_type (const_tree t)
4085 const_tree base = t;
4086 while (handled_component_p (base))
4087 base = TREE_OPERAND (base, 0);
4088 if (TREE_CODE (base) == MEM_REF)
4089 return TREE_TYPE (TREE_OPERAND (base, 1));
4090 else if (TREE_CODE (base) == TARGET_MEM_REF)
4091 return TREE_TYPE (TMR_OFFSET (base));
4092 else
4093 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (base)));
4096 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4097 offsetted by OFFSET units. */
4099 tree
4100 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4102 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4103 build_fold_addr_expr (base),
4104 build_int_cst (ptr_type_node, offset));
4105 tree addr = build1 (ADDR_EXPR, type, ref);
4106 recompute_tree_invariant_for_addr_expr (addr);
4107 return addr;
4110 /* Similar except don't specify the TREE_TYPE
4111 and leave the TREE_SIDE_EFFECTS as 0.
4112 It is permissible for arguments to be null,
4113 or even garbage if their values do not matter. */
4115 tree
4116 build_nt (enum tree_code code, ...)
4118 tree t;
4119 int length;
4120 int i;
4121 va_list p;
4123 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4125 va_start (p, code);
4127 t = make_node (code);
4128 length = TREE_CODE_LENGTH (code);
4130 for (i = 0; i < length; i++)
4131 TREE_OPERAND (t, i) = va_arg (p, tree);
4133 va_end (p);
4134 return t;
4137 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4138 tree vec. */
4140 tree
4141 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4143 tree ret, t;
4144 unsigned int ix;
4146 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4147 CALL_EXPR_FN (ret) = fn;
4148 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4149 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4150 CALL_EXPR_ARG (ret, ix) = t;
4151 return ret;
4154 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4155 We do NOT enter this node in any sort of symbol table.
4157 LOC is the location of the decl.
4159 layout_decl is used to set up the decl's storage layout.
4160 Other slots are initialized to 0 or null pointers. */
4162 tree
4163 build_decl_stat (location_t loc, enum tree_code code, tree name,
4164 tree type MEM_STAT_DECL)
4166 tree t;
4168 t = make_node_stat (code PASS_MEM_STAT);
4169 DECL_SOURCE_LOCATION (t) = loc;
4171 /* if (type == error_mark_node)
4172 type = integer_type_node; */
4173 /* That is not done, deliberately, so that having error_mark_node
4174 as the type can suppress useless errors in the use of this variable. */
4176 DECL_NAME (t) = name;
4177 TREE_TYPE (t) = type;
4179 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4180 layout_decl (t, 0);
4182 return t;
4185 /* Builds and returns function declaration with NAME and TYPE. */
4187 tree
4188 build_fn_decl (const char *name, tree type)
4190 tree id = get_identifier (name);
4191 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4193 DECL_EXTERNAL (decl) = 1;
4194 TREE_PUBLIC (decl) = 1;
4195 DECL_ARTIFICIAL (decl) = 1;
4196 TREE_NOTHROW (decl) = 1;
4198 return decl;
4201 vec<tree, va_gc> *all_translation_units;
4203 /* Builds a new translation-unit decl with name NAME, queues it in the
4204 global list of translation-unit decls and returns it. */
4206 tree
4207 build_translation_unit_decl (tree name)
4209 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4210 name, NULL_TREE);
4211 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4212 vec_safe_push (all_translation_units, tu);
4213 return tu;
4217 /* BLOCK nodes are used to represent the structure of binding contours
4218 and declarations, once those contours have been exited and their contents
4219 compiled. This information is used for outputting debugging info. */
4221 tree
4222 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4224 tree block = make_node (BLOCK);
4226 BLOCK_VARS (block) = vars;
4227 BLOCK_SUBBLOCKS (block) = subblocks;
4228 BLOCK_SUPERCONTEXT (block) = supercontext;
4229 BLOCK_CHAIN (block) = chain;
4230 return block;
4234 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4236 LOC is the location to use in tree T. */
4238 void
4239 protected_set_expr_location (tree t, location_t loc)
4241 if (t && CAN_HAVE_LOCATION_P (t))
4242 SET_EXPR_LOCATION (t, loc);
4245 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4246 is ATTRIBUTE. */
4248 tree
4249 build_decl_attribute_variant (tree ddecl, tree attribute)
4251 DECL_ATTRIBUTES (ddecl) = attribute;
4252 return ddecl;
4255 /* Borrowed from hashtab.c iterative_hash implementation. */
4256 #define mix(a,b,c) \
4258 a -= b; a -= c; a ^= (c>>13); \
4259 b -= c; b -= a; b ^= (a<< 8); \
4260 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
4261 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
4262 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
4263 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
4264 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
4265 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
4266 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
4270 /* Produce good hash value combining VAL and VAL2. */
4271 hashval_t
4272 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
4274 /* the golden ratio; an arbitrary value. */
4275 hashval_t a = 0x9e3779b9;
4277 mix (a, val, val2);
4278 return val2;
4281 /* Produce good hash value combining VAL and VAL2. */
4282 hashval_t
4283 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
4285 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
4286 return iterative_hash_hashval_t (val, val2);
4287 else
4289 hashval_t a = (hashval_t) val;
4290 /* Avoid warnings about shifting of more than the width of the type on
4291 hosts that won't execute this path. */
4292 int zero = 0;
4293 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
4294 mix (a, b, val2);
4295 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
4297 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
4298 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
4299 mix (a, b, val2);
4301 return val2;
4305 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4306 is ATTRIBUTE and its qualifiers are QUALS.
4308 Record such modified types already made so we don't make duplicates. */
4310 tree
4311 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4313 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4315 hashval_t hashcode = 0;
4316 tree ntype;
4317 enum tree_code code = TREE_CODE (ttype);
4319 /* Building a distinct copy of a tagged type is inappropriate; it
4320 causes breakage in code that expects there to be a one-to-one
4321 relationship between a struct and its fields.
4322 build_duplicate_type is another solution (as used in
4323 handle_transparent_union_attribute), but that doesn't play well
4324 with the stronger C++ type identity model. */
4325 if (TREE_CODE (ttype) == RECORD_TYPE
4326 || TREE_CODE (ttype) == UNION_TYPE
4327 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4328 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4330 warning (OPT_Wattributes,
4331 "ignoring attributes applied to %qT after definition",
4332 TYPE_MAIN_VARIANT (ttype));
4333 return build_qualified_type (ttype, quals);
4336 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4337 ntype = build_distinct_type_copy (ttype);
4339 TYPE_ATTRIBUTES (ntype) = attribute;
4341 hashcode = iterative_hash_object (code, hashcode);
4342 if (TREE_TYPE (ntype))
4343 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
4344 hashcode);
4345 hashcode = attribute_hash_list (attribute, hashcode);
4347 switch (TREE_CODE (ntype))
4349 case FUNCTION_TYPE:
4350 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
4351 break;
4352 case ARRAY_TYPE:
4353 if (TYPE_DOMAIN (ntype))
4354 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
4355 hashcode);
4356 break;
4357 case INTEGER_TYPE:
4358 hashcode = iterative_hash_object
4359 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
4360 hashcode = iterative_hash_object
4361 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
4362 break;
4363 case REAL_TYPE:
4364 case FIXED_POINT_TYPE:
4366 unsigned int precision = TYPE_PRECISION (ntype);
4367 hashcode = iterative_hash_object (precision, hashcode);
4369 break;
4370 default:
4371 break;
4374 ntype = type_hash_canon (hashcode, ntype);
4376 /* If the target-dependent attributes make NTYPE different from
4377 its canonical type, we will need to use structural equality
4378 checks for this type. */
4379 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4380 || !comp_type_attributes (ntype, ttype))
4381 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4382 else if (TYPE_CANONICAL (ntype) == ntype)
4383 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4385 ttype = build_qualified_type (ntype, quals);
4387 else if (TYPE_QUALS (ttype) != quals)
4388 ttype = build_qualified_type (ttype, quals);
4390 return ttype;
4393 /* Compare two attributes for their value identity. Return true if the
4394 attribute values are known to be equal; otherwise return false.
4397 static bool
4398 attribute_value_equal (const_tree attr1, const_tree attr2)
4400 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4401 return true;
4403 if (TREE_VALUE (attr1) != NULL_TREE
4404 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4405 && TREE_VALUE (attr2) != NULL
4406 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4407 return (simple_cst_list_equal (TREE_VALUE (attr1),
4408 TREE_VALUE (attr2)) == 1);
4410 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4413 /* Return 0 if the attributes for two types are incompatible, 1 if they
4414 are compatible, and 2 if they are nearly compatible (which causes a
4415 warning to be generated). */
4417 comp_type_attributes (const_tree type1, const_tree type2)
4419 const_tree a1 = TYPE_ATTRIBUTES (type1);
4420 const_tree a2 = TYPE_ATTRIBUTES (type2);
4421 const_tree a;
4423 if (a1 == a2)
4424 return 1;
4425 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4427 const struct attribute_spec *as;
4428 const_tree attr;
4430 as = lookup_attribute_spec (get_attribute_name (a));
4431 if (!as || as->affects_type_identity == false)
4432 continue;
4434 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4435 if (!attr || !attribute_value_equal (a, attr))
4436 break;
4438 if (!a)
4440 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4442 const struct attribute_spec *as;
4444 as = lookup_attribute_spec (get_attribute_name (a));
4445 if (!as || as->affects_type_identity == false)
4446 continue;
4448 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4449 break;
4450 /* We don't need to compare trees again, as we did this
4451 already in first loop. */
4453 /* All types - affecting identity - are equal, so
4454 there is no need to call target hook for comparison. */
4455 if (!a)
4456 return 1;
4458 /* As some type combinations - like default calling-convention - might
4459 be compatible, we have to call the target hook to get the final result. */
4460 return targetm.comp_type_attributes (type1, type2);
4463 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4464 is ATTRIBUTE.
4466 Record such modified types already made so we don't make duplicates. */
4468 tree
4469 build_type_attribute_variant (tree ttype, tree attribute)
4471 return build_type_attribute_qual_variant (ttype, attribute,
4472 TYPE_QUALS (ttype));
4476 /* Reset the expression *EXPR_P, a size or position.
4478 ??? We could reset all non-constant sizes or positions. But it's cheap
4479 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4481 We need to reset self-referential sizes or positions because they cannot
4482 be gimplified and thus can contain a CALL_EXPR after the gimplification
4483 is finished, which will run afoul of LTO streaming. And they need to be
4484 reset to something essentially dummy but not constant, so as to preserve
4485 the properties of the object they are attached to. */
4487 static inline void
4488 free_lang_data_in_one_sizepos (tree *expr_p)
4490 tree expr = *expr_p;
4491 if (CONTAINS_PLACEHOLDER_P (expr))
4492 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4496 /* Reset all the fields in a binfo node BINFO. We only keep
4497 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
4499 static void
4500 free_lang_data_in_binfo (tree binfo)
4502 unsigned i;
4503 tree t;
4505 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4507 BINFO_VIRTUALS (binfo) = NULL_TREE;
4508 BINFO_BASE_ACCESSES (binfo) = NULL;
4509 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4510 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4512 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
4513 free_lang_data_in_binfo (t);
4517 /* Reset all language specific information still present in TYPE. */
4519 static void
4520 free_lang_data_in_type (tree type)
4522 gcc_assert (TYPE_P (type));
4524 /* Give the FE a chance to remove its own data first. */
4525 lang_hooks.free_lang_data (type);
4527 TREE_LANG_FLAG_0 (type) = 0;
4528 TREE_LANG_FLAG_1 (type) = 0;
4529 TREE_LANG_FLAG_2 (type) = 0;
4530 TREE_LANG_FLAG_3 (type) = 0;
4531 TREE_LANG_FLAG_4 (type) = 0;
4532 TREE_LANG_FLAG_5 (type) = 0;
4533 TREE_LANG_FLAG_6 (type) = 0;
4535 if (TREE_CODE (type) == FUNCTION_TYPE)
4537 /* Remove the const and volatile qualifiers from arguments. The
4538 C++ front end removes them, but the C front end does not,
4539 leading to false ODR violation errors when merging two
4540 instances of the same function signature compiled by
4541 different front ends. */
4542 tree p;
4544 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4546 tree arg_type = TREE_VALUE (p);
4548 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
4550 int quals = TYPE_QUALS (arg_type)
4551 & ~TYPE_QUAL_CONST
4552 & ~TYPE_QUAL_VOLATILE;
4553 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
4554 free_lang_data_in_type (TREE_VALUE (p));
4559 /* Remove members that are not actually FIELD_DECLs from the field
4560 list of an aggregate. These occur in C++. */
4561 if (RECORD_OR_UNION_TYPE_P (type))
4563 tree prev, member;
4565 /* Note that TYPE_FIELDS can be shared across distinct
4566 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
4567 to be removed, we cannot set its TREE_CHAIN to NULL.
4568 Otherwise, we would not be able to find all the other fields
4569 in the other instances of this TREE_TYPE.
4571 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
4572 prev = NULL_TREE;
4573 member = TYPE_FIELDS (type);
4574 while (member)
4576 if (TREE_CODE (member) == FIELD_DECL
4577 || TREE_CODE (member) == TYPE_DECL)
4579 if (prev)
4580 TREE_CHAIN (prev) = member;
4581 else
4582 TYPE_FIELDS (type) = member;
4583 prev = member;
4586 member = TREE_CHAIN (member);
4589 if (prev)
4590 TREE_CHAIN (prev) = NULL_TREE;
4591 else
4592 TYPE_FIELDS (type) = NULL_TREE;
4594 TYPE_METHODS (type) = NULL_TREE;
4595 if (TYPE_BINFO (type))
4596 free_lang_data_in_binfo (TYPE_BINFO (type));
4598 else
4600 /* For non-aggregate types, clear out the language slot (which
4601 overloads TYPE_BINFO). */
4602 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
4604 if (INTEGRAL_TYPE_P (type)
4605 || SCALAR_FLOAT_TYPE_P (type)
4606 || FIXED_POINT_TYPE_P (type))
4608 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
4609 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
4613 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
4614 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
4616 if (TYPE_CONTEXT (type)
4617 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
4619 tree ctx = TYPE_CONTEXT (type);
4622 ctx = BLOCK_SUPERCONTEXT (ctx);
4624 while (ctx && TREE_CODE (ctx) == BLOCK);
4625 TYPE_CONTEXT (type) = ctx;
4630 /* Return true if DECL may need an assembler name to be set. */
4632 static inline bool
4633 need_assembler_name_p (tree decl)
4635 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
4636 if (TREE_CODE (decl) != FUNCTION_DECL
4637 && TREE_CODE (decl) != VAR_DECL)
4638 return false;
4640 /* If DECL already has its assembler name set, it does not need a
4641 new one. */
4642 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
4643 || DECL_ASSEMBLER_NAME_SET_P (decl))
4644 return false;
4646 /* Abstract decls do not need an assembler name. */
4647 if (DECL_ABSTRACT (decl))
4648 return false;
4650 /* For VAR_DECLs, only static, public and external symbols need an
4651 assembler name. */
4652 if (TREE_CODE (decl) == VAR_DECL
4653 && !TREE_STATIC (decl)
4654 && !TREE_PUBLIC (decl)
4655 && !DECL_EXTERNAL (decl))
4656 return false;
4658 if (TREE_CODE (decl) == FUNCTION_DECL)
4660 /* Do not set assembler name on builtins. Allow RTL expansion to
4661 decide whether to expand inline or via a regular call. */
4662 if (DECL_BUILT_IN (decl)
4663 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
4664 return false;
4666 /* Functions represented in the callgraph need an assembler name. */
4667 if (cgraph_get_node (decl) != NULL)
4668 return true;
4670 /* Unused and not public functions don't need an assembler name. */
4671 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
4672 return false;
4675 return true;
4679 /* Reset all language specific information still present in symbol
4680 DECL. */
4682 static void
4683 free_lang_data_in_decl (tree decl)
4685 gcc_assert (DECL_P (decl));
4687 /* Give the FE a chance to remove its own data first. */
4688 lang_hooks.free_lang_data (decl);
4690 TREE_LANG_FLAG_0 (decl) = 0;
4691 TREE_LANG_FLAG_1 (decl) = 0;
4692 TREE_LANG_FLAG_2 (decl) = 0;
4693 TREE_LANG_FLAG_3 (decl) = 0;
4694 TREE_LANG_FLAG_4 (decl) = 0;
4695 TREE_LANG_FLAG_5 (decl) = 0;
4696 TREE_LANG_FLAG_6 (decl) = 0;
4698 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
4699 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
4700 if (TREE_CODE (decl) == FIELD_DECL)
4702 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
4703 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
4704 DECL_QUALIFIER (decl) = NULL_TREE;
4707 if (TREE_CODE (decl) == FUNCTION_DECL)
4709 if (gimple_has_body_p (decl))
4711 tree t;
4713 /* If DECL has a gimple body, then the context for its
4714 arguments must be DECL. Otherwise, it doesn't really
4715 matter, as we will not be emitting any code for DECL. In
4716 general, there may be other instances of DECL created by
4717 the front end and since PARM_DECLs are generally shared,
4718 their DECL_CONTEXT changes as the replicas of DECL are
4719 created. The only time where DECL_CONTEXT is important
4720 is for the FUNCTION_DECLs that have a gimple body (since
4721 the PARM_DECL will be used in the function's body). */
4722 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
4723 DECL_CONTEXT (t) = decl;
4726 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
4727 At this point, it is not needed anymore. */
4728 DECL_SAVED_TREE (decl) = NULL_TREE;
4730 /* Clear the abstract origin if it refers to a method. Otherwise
4731 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
4732 origin will not be output correctly. */
4733 if (DECL_ABSTRACT_ORIGIN (decl)
4734 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
4735 && RECORD_OR_UNION_TYPE_P
4736 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
4737 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
4739 /* Sometimes the C++ frontend doesn't manage to transform a temporary
4740 DECL_VINDEX referring to itself into a vtable slot number as it
4741 should. Happens with functions that are copied and then forgotten
4742 about. Just clear it, it won't matter anymore. */
4743 if (DECL_VINDEX (decl) && !host_integerp (DECL_VINDEX (decl), 0))
4744 DECL_VINDEX (decl) = NULL_TREE;
4746 else if (TREE_CODE (decl) == VAR_DECL)
4748 if ((DECL_EXTERNAL (decl)
4749 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
4750 || (decl_function_context (decl) && !TREE_STATIC (decl)))
4751 DECL_INITIAL (decl) = NULL_TREE;
4753 else if (TREE_CODE (decl) == TYPE_DECL
4754 || TREE_CODE (decl) == FIELD_DECL)
4755 DECL_INITIAL (decl) = NULL_TREE;
4756 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
4757 && DECL_INITIAL (decl)
4758 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
4760 /* Strip builtins from the translation-unit BLOCK. We still have targets
4761 without builtin_decl_explicit support and also builtins are shared
4762 nodes and thus we can't use TREE_CHAIN in multiple lists. */
4763 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
4764 while (*nextp)
4766 tree var = *nextp;
4767 if (TREE_CODE (var) == FUNCTION_DECL
4768 && DECL_BUILT_IN (var))
4769 *nextp = TREE_CHAIN (var);
4770 else
4771 nextp = &TREE_CHAIN (var);
4777 /* Data used when collecting DECLs and TYPEs for language data removal. */
4779 struct free_lang_data_d
4781 /* Worklist to avoid excessive recursion. */
4782 vec<tree> worklist;
4784 /* Set of traversed objects. Used to avoid duplicate visits. */
4785 struct pointer_set_t *pset;
4787 /* Array of symbols to process with free_lang_data_in_decl. */
4788 vec<tree> decls;
4790 /* Array of types to process with free_lang_data_in_type. */
4791 vec<tree> types;
4795 /* Save all language fields needed to generate proper debug information
4796 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
4798 static void
4799 save_debug_info_for_decl (tree t)
4801 /*struct saved_debug_info_d *sdi;*/
4803 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
4805 /* FIXME. Partial implementation for saving debug info removed. */
4809 /* Save all language fields needed to generate proper debug information
4810 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
4812 static void
4813 save_debug_info_for_type (tree t)
4815 /*struct saved_debug_info_d *sdi;*/
4817 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
4819 /* FIXME. Partial implementation for saving debug info removed. */
4823 /* Add type or decl T to one of the list of tree nodes that need their
4824 language data removed. The lists are held inside FLD. */
4826 static void
4827 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
4829 if (DECL_P (t))
4831 fld->decls.safe_push (t);
4832 if (debug_info_level > DINFO_LEVEL_TERSE)
4833 save_debug_info_for_decl (t);
4835 else if (TYPE_P (t))
4837 fld->types.safe_push (t);
4838 if (debug_info_level > DINFO_LEVEL_TERSE)
4839 save_debug_info_for_type (t);
4841 else
4842 gcc_unreachable ();
4845 /* Push tree node T into FLD->WORKLIST. */
4847 static inline void
4848 fld_worklist_push (tree t, struct free_lang_data_d *fld)
4850 if (t && !is_lang_specific (t) && !pointer_set_contains (fld->pset, t))
4851 fld->worklist.safe_push ((t));
4855 /* Operand callback helper for free_lang_data_in_node. *TP is the
4856 subtree operand being considered. */
4858 static tree
4859 find_decls_types_r (tree *tp, int *ws, void *data)
4861 tree t = *tp;
4862 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
4864 if (TREE_CODE (t) == TREE_LIST)
4865 return NULL_TREE;
4867 /* Language specific nodes will be removed, so there is no need
4868 to gather anything under them. */
4869 if (is_lang_specific (t))
4871 *ws = 0;
4872 return NULL_TREE;
4875 if (DECL_P (t))
4877 /* Note that walk_tree does not traverse every possible field in
4878 decls, so we have to do our own traversals here. */
4879 add_tree_to_fld_list (t, fld);
4881 fld_worklist_push (DECL_NAME (t), fld);
4882 fld_worklist_push (DECL_CONTEXT (t), fld);
4883 fld_worklist_push (DECL_SIZE (t), fld);
4884 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
4886 /* We are going to remove everything under DECL_INITIAL for
4887 TYPE_DECLs. No point walking them. */
4888 if (TREE_CODE (t) != TYPE_DECL)
4889 fld_worklist_push (DECL_INITIAL (t), fld);
4891 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
4892 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
4894 if (TREE_CODE (t) == FUNCTION_DECL)
4896 fld_worklist_push (DECL_ARGUMENTS (t), fld);
4897 fld_worklist_push (DECL_RESULT (t), fld);
4899 else if (TREE_CODE (t) == TYPE_DECL)
4901 fld_worklist_push (DECL_ARGUMENT_FLD (t), fld);
4902 fld_worklist_push (DECL_VINDEX (t), fld);
4903 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
4905 else if (TREE_CODE (t) == FIELD_DECL)
4907 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
4908 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
4909 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
4910 fld_worklist_push (DECL_FCONTEXT (t), fld);
4912 else if (TREE_CODE (t) == VAR_DECL)
4914 fld_worklist_push (DECL_SECTION_NAME (t), fld);
4915 fld_worklist_push (DECL_COMDAT_GROUP (t), fld);
4918 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
4919 && DECL_HAS_VALUE_EXPR_P (t))
4920 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
4922 if (TREE_CODE (t) != FIELD_DECL
4923 && TREE_CODE (t) != TYPE_DECL)
4924 fld_worklist_push (TREE_CHAIN (t), fld);
4925 *ws = 0;
4927 else if (TYPE_P (t))
4929 /* Note that walk_tree does not traverse every possible field in
4930 types, so we have to do our own traversals here. */
4931 add_tree_to_fld_list (t, fld);
4933 if (!RECORD_OR_UNION_TYPE_P (t))
4934 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
4935 fld_worklist_push (TYPE_SIZE (t), fld);
4936 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
4937 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
4938 fld_worklist_push (TYPE_POINTER_TO (t), fld);
4939 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
4940 fld_worklist_push (TYPE_NAME (t), fld);
4941 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
4942 them and thus do not and want not to reach unused pointer types
4943 this way. */
4944 if (!POINTER_TYPE_P (t))
4945 fld_worklist_push (TYPE_MINVAL (t), fld);
4946 if (!RECORD_OR_UNION_TYPE_P (t))
4947 fld_worklist_push (TYPE_MAXVAL (t), fld);
4948 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
4949 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
4950 do not and want not to reach unused variants this way. */
4951 if (TYPE_CONTEXT (t))
4953 tree ctx = TYPE_CONTEXT (t);
4954 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
4955 So push that instead. */
4956 while (ctx && TREE_CODE (ctx) == BLOCK)
4957 ctx = BLOCK_SUPERCONTEXT (ctx);
4958 fld_worklist_push (ctx, fld);
4960 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
4961 and want not to reach unused types this way. */
4963 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
4965 unsigned i;
4966 tree tem;
4967 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
4968 fld_worklist_push (TREE_TYPE (tem), fld);
4969 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
4970 if (tem
4971 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
4972 && TREE_CODE (tem) == TREE_LIST)
4975 fld_worklist_push (TREE_VALUE (tem), fld);
4976 tem = TREE_CHAIN (tem);
4978 while (tem);
4980 if (RECORD_OR_UNION_TYPE_P (t))
4982 tree tem;
4983 /* Push all TYPE_FIELDS - there can be interleaving interesting
4984 and non-interesting things. */
4985 tem = TYPE_FIELDS (t);
4986 while (tem)
4988 if (TREE_CODE (tem) == FIELD_DECL
4989 || TREE_CODE (tem) == TYPE_DECL)
4990 fld_worklist_push (tem, fld);
4991 tem = TREE_CHAIN (tem);
4995 fld_worklist_push (TYPE_STUB_DECL (t), fld);
4996 *ws = 0;
4998 else if (TREE_CODE (t) == BLOCK)
5000 tree tem;
5001 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5002 fld_worklist_push (tem, fld);
5003 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5004 fld_worklist_push (tem, fld);
5005 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5008 if (TREE_CODE (t) != IDENTIFIER_NODE
5009 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5010 fld_worklist_push (TREE_TYPE (t), fld);
5012 return NULL_TREE;
5016 /* Find decls and types in T. */
5018 static void
5019 find_decls_types (tree t, struct free_lang_data_d *fld)
5021 while (1)
5023 if (!pointer_set_contains (fld->pset, t))
5024 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5025 if (fld->worklist.is_empty ())
5026 break;
5027 t = fld->worklist.pop ();
5031 /* Translate all the types in LIST with the corresponding runtime
5032 types. */
5034 static tree
5035 get_eh_types_for_runtime (tree list)
5037 tree head, prev;
5039 if (list == NULL_TREE)
5040 return NULL_TREE;
5042 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5043 prev = head;
5044 list = TREE_CHAIN (list);
5045 while (list)
5047 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5048 TREE_CHAIN (prev) = n;
5049 prev = TREE_CHAIN (prev);
5050 list = TREE_CHAIN (list);
5053 return head;
5057 /* Find decls and types referenced in EH region R and store them in
5058 FLD->DECLS and FLD->TYPES. */
5060 static void
5061 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5063 switch (r->type)
5065 case ERT_CLEANUP:
5066 break;
5068 case ERT_TRY:
5070 eh_catch c;
5072 /* The types referenced in each catch must first be changed to the
5073 EH types used at runtime. This removes references to FE types
5074 in the region. */
5075 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5077 c->type_list = get_eh_types_for_runtime (c->type_list);
5078 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5081 break;
5083 case ERT_ALLOWED_EXCEPTIONS:
5084 r->u.allowed.type_list
5085 = get_eh_types_for_runtime (r->u.allowed.type_list);
5086 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5087 break;
5089 case ERT_MUST_NOT_THROW:
5090 walk_tree (&r->u.must_not_throw.failure_decl,
5091 find_decls_types_r, fld, fld->pset);
5092 break;
5097 /* Find decls and types referenced in cgraph node N and store them in
5098 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5099 look for *every* kind of DECL and TYPE node reachable from N,
5100 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5101 NAMESPACE_DECLs, etc). */
5103 static void
5104 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5106 basic_block bb;
5107 struct function *fn;
5108 unsigned ix;
5109 tree t;
5111 find_decls_types (n->symbol.decl, fld);
5113 if (!gimple_has_body_p (n->symbol.decl))
5114 return;
5116 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5118 fn = DECL_STRUCT_FUNCTION (n->symbol.decl);
5120 /* Traverse locals. */
5121 FOR_EACH_LOCAL_DECL (fn, ix, t)
5122 find_decls_types (t, fld);
5124 /* Traverse EH regions in FN. */
5126 eh_region r;
5127 FOR_ALL_EH_REGION_FN (r, fn)
5128 find_decls_types_in_eh_region (r, fld);
5131 /* Traverse every statement in FN. */
5132 FOR_EACH_BB_FN (bb, fn)
5134 gimple_stmt_iterator si;
5135 unsigned i;
5137 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5139 gimple phi = gsi_stmt (si);
5141 for (i = 0; i < gimple_phi_num_args (phi); i++)
5143 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5144 find_decls_types (*arg_p, fld);
5148 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5150 gimple stmt = gsi_stmt (si);
5152 if (is_gimple_call (stmt))
5153 find_decls_types (gimple_call_fntype (stmt), fld);
5155 for (i = 0; i < gimple_num_ops (stmt); i++)
5157 tree arg = gimple_op (stmt, i);
5158 find_decls_types (arg, fld);
5165 /* Find decls and types referenced in varpool node N and store them in
5166 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5167 look for *every* kind of DECL and TYPE node reachable from N,
5168 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5169 NAMESPACE_DECLs, etc). */
5171 static void
5172 find_decls_types_in_var (struct varpool_node *v, struct free_lang_data_d *fld)
5174 find_decls_types (v->symbol.decl, fld);
5177 /* If T needs an assembler name, have one created for it. */
5179 void
5180 assign_assembler_name_if_neeeded (tree t)
5182 if (need_assembler_name_p (t))
5184 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5185 diagnostics that use input_location to show locus
5186 information. The problem here is that, at this point,
5187 input_location is generally anchored to the end of the file
5188 (since the parser is long gone), so we don't have a good
5189 position to pin it to.
5191 To alleviate this problem, this uses the location of T's
5192 declaration. Examples of this are
5193 testsuite/g++.dg/template/cond2.C and
5194 testsuite/g++.dg/template/pr35240.C. */
5195 location_t saved_location = input_location;
5196 input_location = DECL_SOURCE_LOCATION (t);
5198 decl_assembler_name (t);
5200 input_location = saved_location;
5205 /* Free language specific information for every operand and expression
5206 in every node of the call graph. This process operates in three stages:
5208 1- Every callgraph node and varpool node is traversed looking for
5209 decls and types embedded in them. This is a more exhaustive
5210 search than that done by find_referenced_vars, because it will
5211 also collect individual fields, decls embedded in types, etc.
5213 2- All the decls found are sent to free_lang_data_in_decl.
5215 3- All the types found are sent to free_lang_data_in_type.
5217 The ordering between decls and types is important because
5218 free_lang_data_in_decl sets assembler names, which includes
5219 mangling. So types cannot be freed up until assembler names have
5220 been set up. */
5222 static void
5223 free_lang_data_in_cgraph (void)
5225 struct cgraph_node *n;
5226 struct varpool_node *v;
5227 struct free_lang_data_d fld;
5228 tree t;
5229 unsigned i;
5230 alias_pair *p;
5232 /* Initialize sets and arrays to store referenced decls and types. */
5233 fld.pset = pointer_set_create ();
5234 fld.worklist.create (0);
5235 fld.decls.create (100);
5236 fld.types.create (100);
5238 /* Find decls and types in the body of every function in the callgraph. */
5239 FOR_EACH_FUNCTION (n)
5240 find_decls_types_in_node (n, &fld);
5242 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5243 find_decls_types (p->decl, &fld);
5245 /* Find decls and types in every varpool symbol. */
5246 FOR_EACH_VARIABLE (v)
5247 find_decls_types_in_var (v, &fld);
5249 /* Set the assembler name on every decl found. We need to do this
5250 now because free_lang_data_in_decl will invalidate data needed
5251 for mangling. This breaks mangling on interdependent decls. */
5252 FOR_EACH_VEC_ELT (fld.decls, i, t)
5253 assign_assembler_name_if_neeeded (t);
5255 /* Traverse every decl found freeing its language data. */
5256 FOR_EACH_VEC_ELT (fld.decls, i, t)
5257 free_lang_data_in_decl (t);
5259 /* Traverse every type found freeing its language data. */
5260 FOR_EACH_VEC_ELT (fld.types, i, t)
5261 free_lang_data_in_type (t);
5263 pointer_set_destroy (fld.pset);
5264 fld.worklist.release ();
5265 fld.decls.release ();
5266 fld.types.release ();
5270 /* Free resources that are used by FE but are not needed once they are done. */
5272 static unsigned
5273 free_lang_data (void)
5275 unsigned i;
5277 /* If we are the LTO frontend we have freed lang-specific data already. */
5278 if (in_lto_p
5279 || !flag_generate_lto)
5280 return 0;
5282 /* Allocate and assign alias sets to the standard integer types
5283 while the slots are still in the way the frontends generated them. */
5284 for (i = 0; i < itk_none; ++i)
5285 if (integer_types[i])
5286 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5288 /* Traverse the IL resetting language specific information for
5289 operands, expressions, etc. */
5290 free_lang_data_in_cgraph ();
5292 /* Create gimple variants for common types. */
5293 ptrdiff_type_node = integer_type_node;
5294 fileptr_type_node = ptr_type_node;
5296 /* Reset some langhooks. Do not reset types_compatible_p, it may
5297 still be used indirectly via the get_alias_set langhook. */
5298 lang_hooks.dwarf_name = lhd_dwarf_name;
5299 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5300 /* We do not want the default decl_assembler_name implementation,
5301 rather if we have fixed everything we want a wrapper around it
5302 asserting that all non-local symbols already got their assembler
5303 name and only produce assembler names for local symbols. Or rather
5304 make sure we never call decl_assembler_name on local symbols and
5305 devise a separate, middle-end private scheme for it. */
5307 /* Reset diagnostic machinery. */
5308 tree_diagnostics_defaults (global_dc);
5310 return 0;
5314 struct simple_ipa_opt_pass pass_ipa_free_lang_data =
5317 SIMPLE_IPA_PASS,
5318 "*free_lang_data", /* name */
5319 OPTGROUP_NONE, /* optinfo_flags */
5320 NULL, /* gate */
5321 free_lang_data, /* execute */
5322 NULL, /* sub */
5323 NULL, /* next */
5324 0, /* static_pass_number */
5325 TV_IPA_FREE_LANG_DATA, /* tv_id */
5326 0, /* properties_required */
5327 0, /* properties_provided */
5328 0, /* properties_destroyed */
5329 0, /* todo_flags_start */
5330 TODO_ggc_collect /* todo_flags_finish */
5334 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5335 ATTR_NAME. Also used internally by remove_attribute(). */
5336 bool
5337 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5339 size_t ident_len = IDENTIFIER_LENGTH (ident);
5341 if (ident_len == attr_len)
5343 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5344 return true;
5346 else if (ident_len == attr_len + 4)
5348 /* There is the possibility that ATTR is 'text' and IDENT is
5349 '__text__'. */
5350 const char *p = IDENTIFIER_POINTER (ident);
5351 if (p[0] == '_' && p[1] == '_'
5352 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5353 && strncmp (attr_name, p + 2, attr_len) == 0)
5354 return true;
5357 return false;
5360 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5361 of ATTR_NAME, and LIST is not NULL_TREE. */
5362 tree
5363 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5365 while (list)
5367 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5369 if (ident_len == attr_len)
5371 if (!strcmp (attr_name,
5372 IDENTIFIER_POINTER (get_attribute_name (list))))
5373 break;
5375 /* TODO: If we made sure that attributes were stored in the
5376 canonical form without '__...__' (ie, as in 'text' as opposed
5377 to '__text__') then we could avoid the following case. */
5378 else if (ident_len == attr_len + 4)
5380 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5381 if (p[0] == '_' && p[1] == '_'
5382 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5383 && strncmp (attr_name, p + 2, attr_len) == 0)
5384 break;
5386 list = TREE_CHAIN (list);
5389 return list;
5392 /* A variant of lookup_attribute() that can be used with an identifier
5393 as the first argument, and where the identifier can be either
5394 'text' or '__text__'.
5396 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5397 return a pointer to the attribute's list element if the attribute
5398 is part of the list, or NULL_TREE if not found. If the attribute
5399 appears more than once, this only returns the first occurrence; the
5400 TREE_CHAIN of the return value should be passed back in if further
5401 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5402 can be in the form 'text' or '__text__'. */
5403 static tree
5404 lookup_ident_attribute (tree attr_identifier, tree list)
5406 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5408 while (list)
5410 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5411 == IDENTIFIER_NODE);
5413 /* Identifiers can be compared directly for equality. */
5414 if (attr_identifier == get_attribute_name (list))
5415 break;
5417 /* If they are not equal, they may still be one in the form
5418 'text' while the other one is in the form '__text__'. TODO:
5419 If we were storing attributes in normalized 'text' form, then
5420 this could all go away and we could take full advantage of
5421 the fact that we're comparing identifiers. :-) */
5423 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5424 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5426 if (ident_len == attr_len + 4)
5428 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5429 const char *q = IDENTIFIER_POINTER (attr_identifier);
5430 if (p[0] == '_' && p[1] == '_'
5431 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5432 && strncmp (q, p + 2, attr_len) == 0)
5433 break;
5435 else if (ident_len + 4 == attr_len)
5437 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5438 const char *q = IDENTIFIER_POINTER (attr_identifier);
5439 if (q[0] == '_' && q[1] == '_'
5440 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5441 && strncmp (q + 2, p, ident_len) == 0)
5442 break;
5445 list = TREE_CHAIN (list);
5448 return list;
5451 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5452 modified list. */
5454 tree
5455 remove_attribute (const char *attr_name, tree list)
5457 tree *p;
5458 size_t attr_len = strlen (attr_name);
5460 gcc_checking_assert (attr_name[0] != '_');
5462 for (p = &list; *p; )
5464 tree l = *p;
5465 /* TODO: If we were storing attributes in normalized form, here
5466 we could use a simple strcmp(). */
5467 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
5468 *p = TREE_CHAIN (l);
5469 else
5470 p = &TREE_CHAIN (l);
5473 return list;
5476 /* Return an attribute list that is the union of a1 and a2. */
5478 tree
5479 merge_attributes (tree a1, tree a2)
5481 tree attributes;
5483 /* Either one unset? Take the set one. */
5485 if ((attributes = a1) == 0)
5486 attributes = a2;
5488 /* One that completely contains the other? Take it. */
5490 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
5492 if (attribute_list_contained (a2, a1))
5493 attributes = a2;
5494 else
5496 /* Pick the longest list, and hang on the other list. */
5498 if (list_length (a1) < list_length (a2))
5499 attributes = a2, a2 = a1;
5501 for (; a2 != 0; a2 = TREE_CHAIN (a2))
5503 tree a;
5504 for (a = lookup_ident_attribute (get_attribute_name (a2),
5505 attributes);
5506 a != NULL_TREE && !attribute_value_equal (a, a2);
5507 a = lookup_ident_attribute (get_attribute_name (a2),
5508 TREE_CHAIN (a)))
5510 if (a == NULL_TREE)
5512 a1 = copy_node (a2);
5513 TREE_CHAIN (a1) = attributes;
5514 attributes = a1;
5519 return attributes;
5522 /* Given types T1 and T2, merge their attributes and return
5523 the result. */
5525 tree
5526 merge_type_attributes (tree t1, tree t2)
5528 return merge_attributes (TYPE_ATTRIBUTES (t1),
5529 TYPE_ATTRIBUTES (t2));
5532 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
5533 the result. */
5535 tree
5536 merge_decl_attributes (tree olddecl, tree newdecl)
5538 return merge_attributes (DECL_ATTRIBUTES (olddecl),
5539 DECL_ATTRIBUTES (newdecl));
5542 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5544 /* Specialization of merge_decl_attributes for various Windows targets.
5546 This handles the following situation:
5548 __declspec (dllimport) int foo;
5549 int foo;
5551 The second instance of `foo' nullifies the dllimport. */
5553 tree
5554 merge_dllimport_decl_attributes (tree old, tree new_tree)
5556 tree a;
5557 int delete_dllimport_p = 1;
5559 /* What we need to do here is remove from `old' dllimport if it doesn't
5560 appear in `new'. dllimport behaves like extern: if a declaration is
5561 marked dllimport and a definition appears later, then the object
5562 is not dllimport'd. We also remove a `new' dllimport if the old list
5563 contains dllexport: dllexport always overrides dllimport, regardless
5564 of the order of declaration. */
5565 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
5566 delete_dllimport_p = 0;
5567 else if (DECL_DLLIMPORT_P (new_tree)
5568 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
5570 DECL_DLLIMPORT_P (new_tree) = 0;
5571 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
5572 "dllimport ignored", new_tree);
5574 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
5576 /* Warn about overriding a symbol that has already been used, e.g.:
5577 extern int __attribute__ ((dllimport)) foo;
5578 int* bar () {return &foo;}
5579 int foo;
5581 if (TREE_USED (old))
5583 warning (0, "%q+D redeclared without dllimport attribute "
5584 "after being referenced with dll linkage", new_tree);
5585 /* If we have used a variable's address with dllimport linkage,
5586 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
5587 decl may already have had TREE_CONSTANT computed.
5588 We still remove the attribute so that assembler code refers
5589 to '&foo rather than '_imp__foo'. */
5590 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
5591 DECL_DLLIMPORT_P (new_tree) = 1;
5594 /* Let an inline definition silently override the external reference,
5595 but otherwise warn about attribute inconsistency. */
5596 else if (TREE_CODE (new_tree) == VAR_DECL
5597 || !DECL_DECLARED_INLINE_P (new_tree))
5598 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
5599 "previous dllimport ignored", new_tree);
5601 else
5602 delete_dllimport_p = 0;
5604 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
5606 if (delete_dllimport_p)
5607 a = remove_attribute ("dllimport", a);
5609 return a;
5612 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
5613 struct attribute_spec.handler. */
5615 tree
5616 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
5617 bool *no_add_attrs)
5619 tree node = *pnode;
5620 bool is_dllimport;
5622 /* These attributes may apply to structure and union types being created,
5623 but otherwise should pass to the declaration involved. */
5624 if (!DECL_P (node))
5626 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
5627 | (int) ATTR_FLAG_ARRAY_NEXT))
5629 *no_add_attrs = true;
5630 return tree_cons (name, args, NULL_TREE);
5632 if (TREE_CODE (node) == RECORD_TYPE
5633 || TREE_CODE (node) == UNION_TYPE)
5635 node = TYPE_NAME (node);
5636 if (!node)
5637 return NULL_TREE;
5639 else
5641 warning (OPT_Wattributes, "%qE attribute ignored",
5642 name);
5643 *no_add_attrs = true;
5644 return NULL_TREE;
5648 if (TREE_CODE (node) != FUNCTION_DECL
5649 && TREE_CODE (node) != VAR_DECL
5650 && TREE_CODE (node) != TYPE_DECL)
5652 *no_add_attrs = true;
5653 warning (OPT_Wattributes, "%qE attribute ignored",
5654 name);
5655 return NULL_TREE;
5658 if (TREE_CODE (node) == TYPE_DECL
5659 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
5660 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
5662 *no_add_attrs = true;
5663 warning (OPT_Wattributes, "%qE attribute ignored",
5664 name);
5665 return NULL_TREE;
5668 is_dllimport = is_attribute_p ("dllimport", name);
5670 /* Report error on dllimport ambiguities seen now before they cause
5671 any damage. */
5672 if (is_dllimport)
5674 /* Honor any target-specific overrides. */
5675 if (!targetm.valid_dllimport_attribute_p (node))
5676 *no_add_attrs = true;
5678 else if (TREE_CODE (node) == FUNCTION_DECL
5679 && DECL_DECLARED_INLINE_P (node))
5681 warning (OPT_Wattributes, "inline function %q+D declared as "
5682 " dllimport: attribute ignored", node);
5683 *no_add_attrs = true;
5685 /* Like MS, treat definition of dllimported variables and
5686 non-inlined functions on declaration as syntax errors. */
5687 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
5689 error ("function %q+D definition is marked dllimport", node);
5690 *no_add_attrs = true;
5693 else if (TREE_CODE (node) == VAR_DECL)
5695 if (DECL_INITIAL (node))
5697 error ("variable %q+D definition is marked dllimport",
5698 node);
5699 *no_add_attrs = true;
5702 /* `extern' needn't be specified with dllimport.
5703 Specify `extern' now and hope for the best. Sigh. */
5704 DECL_EXTERNAL (node) = 1;
5705 /* Also, implicitly give dllimport'd variables declared within
5706 a function global scope, unless declared static. */
5707 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
5708 TREE_PUBLIC (node) = 1;
5711 if (*no_add_attrs == false)
5712 DECL_DLLIMPORT_P (node) = 1;
5714 else if (TREE_CODE (node) == FUNCTION_DECL
5715 && DECL_DECLARED_INLINE_P (node)
5716 && flag_keep_inline_dllexport)
5717 /* An exported function, even if inline, must be emitted. */
5718 DECL_EXTERNAL (node) = 0;
5720 /* Report error if symbol is not accessible at global scope. */
5721 if (!TREE_PUBLIC (node)
5722 && (TREE_CODE (node) == VAR_DECL
5723 || TREE_CODE (node) == FUNCTION_DECL))
5725 error ("external linkage required for symbol %q+D because of "
5726 "%qE attribute", node, name);
5727 *no_add_attrs = true;
5730 /* A dllexport'd entity must have default visibility so that other
5731 program units (shared libraries or the main executable) can see
5732 it. A dllimport'd entity must have default visibility so that
5733 the linker knows that undefined references within this program
5734 unit can be resolved by the dynamic linker. */
5735 if (!*no_add_attrs)
5737 if (DECL_VISIBILITY_SPECIFIED (node)
5738 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
5739 error ("%qE implies default visibility, but %qD has already "
5740 "been declared with a different visibility",
5741 name, node);
5742 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
5743 DECL_VISIBILITY_SPECIFIED (node) = 1;
5746 return NULL_TREE;
5749 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
5751 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
5752 of the various TYPE_QUAL values. Also, set the UPC layout qualifier,
5753 which is either null or a reference to an integral constant. */
5755 static void
5756 set_type_quals (tree type, int type_quals, tree layout_qualifier)
5758 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
5759 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
5760 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
5761 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
5762 TYPE_SHARED (type) = (type_quals & TYPE_QUAL_SHARED) != 0;
5763 TYPE_STRICT (type) = (type_quals & TYPE_QUAL_STRICT) != 0;
5764 TYPE_RELAXED (type) = (type_quals & TYPE_QUAL_RELAXED) != 0;
5765 if (TYPE_SHARED (type))
5766 SET_TYPE_BLOCK_FACTOR (type, layout_qualifier);
5769 /* Returns true iff CAND is equivalent to BASE with
5770 TYPE_QUALS and LAYOUT_QUALIFIER. */
5772 bool
5773 check_qualified_type (tree cand, tree base,
5774 int type_quals, tree layout_qualifier)
5776 return (TYPE_QUALS (cand) == type_quals
5777 && (TYPE_BLOCK_FACTOR (cand) == layout_qualifier
5778 || tree_int_cst_equal (TYPE_BLOCK_FACTOR (cand),
5779 layout_qualifier))
5780 && TYPE_NAME (cand) == TYPE_NAME (base)
5781 /* Apparently this is needed for Objective-C. */
5782 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5783 /* Check alignment. */
5784 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
5785 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5786 TYPE_ATTRIBUTES (base)));
5789 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
5791 static bool
5792 check_aligned_type (tree cand, tree base, unsigned int align)
5794 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
5795 && (TYPE_BLOCK_FACTOR (cand) == TYPE_BLOCK_FACTOR (base)
5796 || tree_int_cst_equal (TYPE_BLOCK_FACTOR (cand),
5797 TYPE_BLOCK_FACTOR (base)))
5798 && TYPE_NAME (cand) == TYPE_NAME (base)
5799 /* Apparently this is needed for Objective-C. */
5800 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5801 /* Check alignment. */
5802 && TYPE_ALIGN (cand) == align
5803 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5804 TYPE_ATTRIBUTES (base)));
5807 /* Return a version of the TYPE, qualified as indicated by the
5808 TYPE_QUALS, if one exists. If no qualified version exists yet,
5809 return NULL_TREE. */
5811 tree
5812 get_qualified_type_1 (tree type, int type_quals, tree layout_qualifier)
5814 tree t;
5816 if (TYPE_QUALS (type) == type_quals)
5817 return type;
5819 /* Search the chain of variants to see if there is already one there just
5820 like the one we need to have. If so, use that existing one. We must
5821 preserve the TYPE_NAME, since there is code that depends on this. */
5822 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5823 if (check_qualified_type (t, type, type_quals, layout_qualifier))
5824 return t;
5826 return NULL_TREE;
5829 /* Like get_qualified_type_1, but creates the type if it does not
5830 exist. This function never returns NULL_TREE. */
5832 tree
5833 build_qualified_type_1 (tree type, int type_quals, tree layout_qualifier)
5835 tree t;
5837 /* See if we already have the appropriate qualified variant. */
5838 t = get_qualified_type_1 (type, type_quals, layout_qualifier);
5840 /* If not, build it. */
5841 if (!t)
5843 t = build_variant_type_copy (type);
5844 set_type_quals (t, type_quals, layout_qualifier);
5846 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5847 /* Propagate structural equality. */
5848 SET_TYPE_STRUCTURAL_EQUALITY (t);
5849 else if (TYPE_CANONICAL (type) != type)
5850 /* Build the underlying canonical type, since it is different
5851 from TYPE. */
5852 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
5853 type_quals);
5854 else
5855 /* T is its own canonical type. */
5856 TYPE_CANONICAL (t) = t;
5860 return t;
5863 /* Return a variant of TYPE, where all UPC qualifiers
5864 have been removed. */
5866 tree
5867 build_upc_unshared_type (tree type)
5869 tree u_type = type;
5870 if (TREE_CODE (type) == ARRAY_TYPE)
5872 const tree elem_type = TREE_TYPE(type);
5873 const tree u_elem_type = build_upc_unshared_type (elem_type);
5874 if (u_elem_type != elem_type)
5876 for (u_type = TYPE_MAIN_VARIANT (type);
5877 u_type && TREE_TYPE(u_type) != u_elem_type;
5878 u_type = TYPE_NEXT_VARIANT (u_type)) /* loop */;
5879 if (!u_type)
5881 u_type = build_variant_type_copy (type);
5882 TREE_TYPE (u_type) = u_elem_type;
5886 else
5888 const int quals = TYPE_QUALS (type);
5889 const int u_quals = quals & ~(TYPE_QUAL_SHARED
5890 | TYPE_QUAL_RELAXED
5891 | TYPE_QUAL_STRICT);
5892 u_type = build_qualified_type (type, u_quals);
5894 return u_type;
5897 /* Create a variant of type T with alignment ALIGN. */
5899 tree
5900 build_aligned_type (tree type, unsigned int align)
5902 tree t;
5904 if (TYPE_PACKED (type)
5905 || TYPE_ALIGN (type) == align)
5906 return type;
5908 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5909 if (check_aligned_type (t, type, align))
5910 return t;
5912 t = build_variant_type_copy (type);
5913 TYPE_ALIGN (t) = align;
5915 return t;
5918 /* Create a new distinct copy of TYPE. The new type is made its own
5919 MAIN_VARIANT. If TYPE requires structural equality checks, the
5920 resulting type requires structural equality checks; otherwise, its
5921 TYPE_CANONICAL points to itself. */
5923 tree
5924 build_distinct_type_copy (tree type)
5926 tree t = copy_node (type);
5928 TYPE_POINTER_TO (t) = 0;
5929 TYPE_REFERENCE_TO (t) = 0;
5931 /* Set the canonical type either to a new equivalence class, or
5932 propagate the need for structural equality checks. */
5933 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5934 SET_TYPE_STRUCTURAL_EQUALITY (t);
5935 else
5936 TYPE_CANONICAL (t) = t;
5938 /* Make it its own variant. */
5939 TYPE_MAIN_VARIANT (t) = t;
5940 TYPE_NEXT_VARIANT (t) = 0;
5942 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
5943 whose TREE_TYPE is not t. This can also happen in the Ada
5944 frontend when using subtypes. */
5946 return t;
5949 /* Create a new variant of TYPE, equivalent but distinct. This is so
5950 the caller can modify it. TYPE_CANONICAL for the return type will
5951 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
5952 are considered equal by the language itself (or that both types
5953 require structural equality checks). */
5955 tree
5956 build_variant_type_copy (tree type)
5958 tree t, m = TYPE_MAIN_VARIANT (type);
5960 t = build_distinct_type_copy (type);
5962 /* Since we're building a variant, assume that it is a non-semantic
5963 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
5964 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
5966 /* Add the new type to the chain of variants of TYPE. */
5967 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
5968 TYPE_NEXT_VARIANT (m) = t;
5969 TYPE_MAIN_VARIANT (t) = m;
5971 return t;
5974 /* Return true if the from tree in both tree maps are equal. */
5977 tree_map_base_eq (const void *va, const void *vb)
5979 const struct tree_map_base *const a = (const struct tree_map_base *) va,
5980 *const b = (const struct tree_map_base *) vb;
5981 return (a->from == b->from);
5984 /* Hash a from tree in a tree_base_map. */
5986 unsigned int
5987 tree_map_base_hash (const void *item)
5989 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
5992 /* Return true if this tree map structure is marked for garbage collection
5993 purposes. We simply return true if the from tree is marked, so that this
5994 structure goes away when the from tree goes away. */
5997 tree_map_base_marked_p (const void *p)
5999 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6002 /* Hash a from tree in a tree_map. */
6004 unsigned int
6005 tree_map_hash (const void *item)
6007 return (((const struct tree_map *) item)->hash);
6010 /* Hash a from tree in a tree_decl_map. */
6012 unsigned int
6013 tree_decl_map_hash (const void *item)
6015 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6018 /* Return the initialization priority for DECL. */
6020 priority_type
6021 decl_init_priority_lookup (tree decl)
6023 struct tree_priority_map *h;
6024 struct tree_map_base in;
6026 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6027 in.from = decl;
6028 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6029 return h ? h->init : DEFAULT_INIT_PRIORITY;
6032 /* Return the finalization priority for DECL. */
6034 priority_type
6035 decl_fini_priority_lookup (tree decl)
6037 struct tree_priority_map *h;
6038 struct tree_map_base in;
6040 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6041 in.from = decl;
6042 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6043 return h ? h->fini : DEFAULT_INIT_PRIORITY;
6046 /* Return the initialization and finalization priority information for
6047 DECL. If there is no previous priority information, a freshly
6048 allocated structure is returned. */
6050 static struct tree_priority_map *
6051 decl_priority_info (tree decl)
6053 struct tree_priority_map in;
6054 struct tree_priority_map *h;
6055 void **loc;
6057 in.base.from = decl;
6058 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
6059 h = (struct tree_priority_map *) *loc;
6060 if (!h)
6062 h = ggc_alloc_cleared_tree_priority_map ();
6063 *loc = h;
6064 h->base.from = decl;
6065 h->init = DEFAULT_INIT_PRIORITY;
6066 h->fini = DEFAULT_INIT_PRIORITY;
6069 return h;
6072 /* Set the initialization priority for DECL to PRIORITY. */
6074 void
6075 decl_init_priority_insert (tree decl, priority_type priority)
6077 struct tree_priority_map *h;
6079 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6080 if (priority == DEFAULT_INIT_PRIORITY)
6081 return;
6082 h = decl_priority_info (decl);
6083 h->init = priority;
6086 /* Set the finalization priority for DECL to PRIORITY. */
6088 void
6089 decl_fini_priority_insert (tree decl, priority_type priority)
6091 struct tree_priority_map *h;
6093 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6094 if (priority == DEFAULT_INIT_PRIORITY)
6095 return;
6096 h = decl_priority_info (decl);
6097 h->fini = priority;
6100 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6102 static void
6103 print_debug_expr_statistics (void)
6105 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6106 (long) htab_size (debug_expr_for_decl),
6107 (long) htab_elements (debug_expr_for_decl),
6108 htab_collisions (debug_expr_for_decl));
6111 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6113 static void
6114 print_value_expr_statistics (void)
6116 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6117 (long) htab_size (value_expr_for_decl),
6118 (long) htab_elements (value_expr_for_decl),
6119 htab_collisions (value_expr_for_decl));
6122 /* Lookup a debug expression for FROM, and return it if we find one. */
6124 tree
6125 decl_debug_expr_lookup (tree from)
6127 struct tree_decl_map *h, in;
6128 in.base.from = from;
6130 h = (struct tree_decl_map *)
6131 htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
6132 if (h)
6133 return h->to;
6134 return NULL_TREE;
6137 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6139 void
6140 decl_debug_expr_insert (tree from, tree to)
6142 struct tree_decl_map *h;
6143 void **loc;
6145 h = ggc_alloc_tree_decl_map ();
6146 h->base.from = from;
6147 h->to = to;
6148 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
6149 INSERT);
6150 *(struct tree_decl_map **) loc = h;
6153 /* Lookup a value expression for FROM, and return it if we find one. */
6155 tree
6156 decl_value_expr_lookup (tree from)
6158 struct tree_decl_map *h, in;
6159 in.base.from = from;
6161 h = (struct tree_decl_map *)
6162 htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
6163 if (h)
6164 return h->to;
6165 return NULL_TREE;
6168 /* Insert a mapping FROM->TO in the value expression hashtable. */
6170 void
6171 decl_value_expr_insert (tree from, tree to)
6173 struct tree_decl_map *h;
6174 void **loc;
6176 h = ggc_alloc_tree_decl_map ();
6177 h->base.from = from;
6178 h->to = to;
6179 loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6180 INSERT);
6181 *(struct tree_decl_map **) loc = h;
6184 /* Lookup a vector of debug arguments for FROM, and return it if we
6185 find one. */
6187 vec<tree, va_gc> **
6188 decl_debug_args_lookup (tree from)
6190 struct tree_vec_map *h, in;
6192 if (!DECL_HAS_DEBUG_ARGS_P (from))
6193 return NULL;
6194 gcc_checking_assert (debug_args_for_decl != NULL);
6195 in.base.from = from;
6196 h = (struct tree_vec_map *)
6197 htab_find_with_hash (debug_args_for_decl, &in, DECL_UID (from));
6198 if (h)
6199 return &h->to;
6200 return NULL;
6203 /* Insert a mapping FROM->empty vector of debug arguments in the value
6204 expression hashtable. */
6206 vec<tree, va_gc> **
6207 decl_debug_args_insert (tree from)
6209 struct tree_vec_map *h;
6210 void **loc;
6212 if (DECL_HAS_DEBUG_ARGS_P (from))
6213 return decl_debug_args_lookup (from);
6214 if (debug_args_for_decl == NULL)
6215 debug_args_for_decl = htab_create_ggc (64, tree_vec_map_hash,
6216 tree_vec_map_eq, 0);
6217 h = ggc_alloc_tree_vec_map ();
6218 h->base.from = from;
6219 h->to = NULL;
6220 loc = htab_find_slot_with_hash (debug_args_for_decl, h, DECL_UID (from),
6221 INSERT);
6222 *(struct tree_vec_map **) loc = h;
6223 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6224 return &h->to;
6227 /* Hashing of types so that we don't make duplicates.
6228 The entry point is `type_hash_canon'. */
6230 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6231 with types in the TREE_VALUE slots), by adding the hash codes
6232 of the individual types. */
6234 static unsigned int
6235 type_hash_list (const_tree list, hashval_t hashcode)
6237 const_tree tail;
6239 for (tail = list; tail; tail = TREE_CHAIN (tail))
6240 if (TREE_VALUE (tail) != error_mark_node)
6241 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
6242 hashcode);
6244 return hashcode;
6247 /* These are the Hashtable callback functions. */
6249 /* Returns true iff the types are equivalent. */
6251 static int
6252 type_hash_eq (const void *va, const void *vb)
6254 const struct type_hash *const a = (const struct type_hash *) va,
6255 *const b = (const struct type_hash *) vb;
6257 /* First test the things that are the same for all types. */
6258 if (a->hash != b->hash
6259 || TREE_CODE (a->type) != TREE_CODE (b->type)
6260 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6261 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6262 TYPE_ATTRIBUTES (b->type))
6263 || (TREE_CODE (a->type) != COMPLEX_TYPE
6264 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6265 return 0;
6267 /* Be careful about comparing arrays before and after the element type
6268 has been completed; don't compare TYPE_ALIGN unless both types are
6269 complete. */
6270 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6271 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6272 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6273 return 0;
6275 switch (TREE_CODE (a->type))
6277 case VOID_TYPE:
6278 case COMPLEX_TYPE:
6279 case POINTER_TYPE:
6280 case REFERENCE_TYPE:
6281 case NULLPTR_TYPE:
6282 return 1;
6284 case VECTOR_TYPE:
6285 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6287 case ENUMERAL_TYPE:
6288 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6289 && !(TYPE_VALUES (a->type)
6290 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6291 && TYPE_VALUES (b->type)
6292 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6293 && type_list_equal (TYPE_VALUES (a->type),
6294 TYPE_VALUES (b->type))))
6295 return 0;
6297 /* ... fall through ... */
6299 case INTEGER_TYPE:
6300 case REAL_TYPE:
6301 case BOOLEAN_TYPE:
6302 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6303 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6304 TYPE_MAX_VALUE (b->type)))
6305 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6306 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6307 TYPE_MIN_VALUE (b->type))));
6309 case FIXED_POINT_TYPE:
6310 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6312 case OFFSET_TYPE:
6313 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6315 case METHOD_TYPE:
6316 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6317 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6318 || (TYPE_ARG_TYPES (a->type)
6319 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6320 && TYPE_ARG_TYPES (b->type)
6321 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6322 && type_list_equal (TYPE_ARG_TYPES (a->type),
6323 TYPE_ARG_TYPES (b->type)))))
6324 break;
6325 return 0;
6326 case ARRAY_TYPE:
6327 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6329 case RECORD_TYPE:
6330 case UNION_TYPE:
6331 case QUAL_UNION_TYPE:
6332 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6333 || (TYPE_FIELDS (a->type)
6334 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6335 && TYPE_FIELDS (b->type)
6336 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6337 && type_list_equal (TYPE_FIELDS (a->type),
6338 TYPE_FIELDS (b->type))));
6340 case FUNCTION_TYPE:
6341 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6342 || (TYPE_ARG_TYPES (a->type)
6343 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6344 && TYPE_ARG_TYPES (b->type)
6345 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6346 && type_list_equal (TYPE_ARG_TYPES (a->type),
6347 TYPE_ARG_TYPES (b->type))))
6348 break;
6349 return 0;
6351 default:
6352 return 0;
6355 if (lang_hooks.types.type_hash_eq != NULL)
6356 return lang_hooks.types.type_hash_eq (a->type, b->type);
6358 return 1;
6361 /* Return the cached hash value. */
6363 static hashval_t
6364 type_hash_hash (const void *item)
6366 return ((const struct type_hash *) item)->hash;
6369 /* Look in the type hash table for a type isomorphic to TYPE.
6370 If one is found, return it. Otherwise return 0. */
6372 tree
6373 type_hash_lookup (hashval_t hashcode, tree type)
6375 struct type_hash *h, in;
6377 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6378 must call that routine before comparing TYPE_ALIGNs. */
6379 layout_type (type);
6381 in.hash = hashcode;
6382 in.type = type;
6384 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
6385 hashcode);
6386 if (h)
6387 return h->type;
6388 return NULL_TREE;
6391 /* Add an entry to the type-hash-table
6392 for a type TYPE whose hash code is HASHCODE. */
6394 void
6395 type_hash_add (hashval_t hashcode, tree type)
6397 struct type_hash *h;
6398 void **loc;
6400 h = ggc_alloc_type_hash ();
6401 h->hash = hashcode;
6402 h->type = type;
6403 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
6404 *loc = (void *)h;
6407 /* Given TYPE, and HASHCODE its hash code, return the canonical
6408 object for an identical type if one already exists.
6409 Otherwise, return TYPE, and record it as the canonical object.
6411 To use this function, first create a type of the sort you want.
6412 Then compute its hash code from the fields of the type that
6413 make it different from other similar types.
6414 Then call this function and use the value. */
6416 tree
6417 type_hash_canon (unsigned int hashcode, tree type)
6419 tree t1;
6421 /* The hash table only contains main variants, so ensure that's what we're
6422 being passed. */
6423 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6425 /* See if the type is in the hash table already. If so, return it.
6426 Otherwise, add the type. */
6427 t1 = type_hash_lookup (hashcode, type);
6428 if (t1 != 0)
6430 if (GATHER_STATISTICS)
6432 tree_code_counts[(int) TREE_CODE (type)]--;
6433 tree_node_counts[(int) t_kind]--;
6434 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6436 return t1;
6438 else
6440 type_hash_add (hashcode, type);
6441 return type;
6445 /* See if the data pointed to by the type hash table is marked. We consider
6446 it marked if the type is marked or if a debug type number or symbol
6447 table entry has been made for the type. */
6449 static int
6450 type_hash_marked_p (const void *p)
6452 const_tree const type = ((const struct type_hash *) p)->type;
6454 return ggc_marked_p (type);
6457 static void
6458 print_type_hash_statistics (void)
6460 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6461 (long) htab_size (type_hash_table),
6462 (long) htab_elements (type_hash_table),
6463 htab_collisions (type_hash_table));
6466 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6467 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6468 by adding the hash codes of the individual attributes. */
6470 static unsigned int
6471 attribute_hash_list (const_tree list, hashval_t hashcode)
6473 const_tree tail;
6475 for (tail = list; tail; tail = TREE_CHAIN (tail))
6476 /* ??? Do we want to add in TREE_VALUE too? */
6477 hashcode = iterative_hash_object
6478 (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)), hashcode);
6479 return hashcode;
6482 /* Given two lists of attributes, return true if list l2 is
6483 equivalent to l1. */
6486 attribute_list_equal (const_tree l1, const_tree l2)
6488 if (l1 == l2)
6489 return 1;
6491 return attribute_list_contained (l1, l2)
6492 && attribute_list_contained (l2, l1);
6495 /* Given two lists of attributes, return true if list L2 is
6496 completely contained within L1. */
6497 /* ??? This would be faster if attribute names were stored in a canonicalized
6498 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6499 must be used to show these elements are equivalent (which they are). */
6500 /* ??? It's not clear that attributes with arguments will always be handled
6501 correctly. */
6504 attribute_list_contained (const_tree l1, const_tree l2)
6506 const_tree t1, t2;
6508 /* First check the obvious, maybe the lists are identical. */
6509 if (l1 == l2)
6510 return 1;
6512 /* Maybe the lists are similar. */
6513 for (t1 = l1, t2 = l2;
6514 t1 != 0 && t2 != 0
6515 && get_attribute_name (t1) == get_attribute_name (t2)
6516 && TREE_VALUE (t1) == TREE_VALUE (t2);
6517 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6520 /* Maybe the lists are equal. */
6521 if (t1 == 0 && t2 == 0)
6522 return 1;
6524 for (; t2 != 0; t2 = TREE_CHAIN (t2))
6526 const_tree attr;
6527 /* This CONST_CAST is okay because lookup_attribute does not
6528 modify its argument and the return value is assigned to a
6529 const_tree. */
6530 for (attr = lookup_ident_attribute (get_attribute_name (t2), CONST_CAST_TREE(l1));
6531 attr != NULL_TREE && !attribute_value_equal (t2, attr);
6532 attr = lookup_ident_attribute (get_attribute_name (t2), TREE_CHAIN (attr)))
6535 if (attr == NULL_TREE)
6536 return 0;
6539 return 1;
6542 /* Given two lists of types
6543 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6544 return 1 if the lists contain the same types in the same order.
6545 Also, the TREE_PURPOSEs must match. */
6548 type_list_equal (const_tree l1, const_tree l2)
6550 const_tree t1, t2;
6552 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6553 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6554 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6555 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6556 && (TREE_TYPE (TREE_PURPOSE (t1))
6557 == TREE_TYPE (TREE_PURPOSE (t2))))))
6558 return 0;
6560 return t1 == t2;
6563 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6564 given by TYPE. If the argument list accepts variable arguments,
6565 then this function counts only the ordinary arguments. */
6568 type_num_arguments (const_tree type)
6570 int i = 0;
6571 tree t;
6573 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6574 /* If the function does not take a variable number of arguments,
6575 the last element in the list will have type `void'. */
6576 if (VOID_TYPE_P (TREE_VALUE (t)))
6577 break;
6578 else
6579 ++i;
6581 return i;
6584 /* Nonzero if integer constants T1 and T2
6585 represent the same constant value. */
6588 tree_int_cst_equal (const_tree t1, const_tree t2)
6590 if (t1 == t2)
6591 return 1;
6593 if (t1 == 0 || t2 == 0)
6594 return 0;
6596 if (TREE_CODE (t1) == INTEGER_CST
6597 && TREE_CODE (t2) == INTEGER_CST
6598 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6599 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
6600 return 1;
6602 return 0;
6605 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
6606 The precise way of comparison depends on their data type. */
6609 tree_int_cst_lt (const_tree t1, const_tree t2)
6611 if (t1 == t2)
6612 return 0;
6614 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
6616 int t1_sgn = tree_int_cst_sgn (t1);
6617 int t2_sgn = tree_int_cst_sgn (t2);
6619 if (t1_sgn < t2_sgn)
6620 return 1;
6621 else if (t1_sgn > t2_sgn)
6622 return 0;
6623 /* Otherwise, both are non-negative, so we compare them as
6624 unsigned just in case one of them would overflow a signed
6625 type. */
6627 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
6628 return INT_CST_LT (t1, t2);
6630 return INT_CST_LT_UNSIGNED (t1, t2);
6633 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
6636 tree_int_cst_compare (const_tree t1, const_tree t2)
6638 if (tree_int_cst_lt (t1, t2))
6639 return -1;
6640 else if (tree_int_cst_lt (t2, t1))
6641 return 1;
6642 else
6643 return 0;
6646 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
6647 the host. If POS is zero, the value can be represented in a single
6648 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
6649 be represented in a single unsigned HOST_WIDE_INT. */
6652 host_integerp (const_tree t, int pos)
6654 if (t == NULL_TREE)
6655 return 0;
6657 return (TREE_CODE (t) == INTEGER_CST
6658 && ((TREE_INT_CST_HIGH (t) == 0
6659 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
6660 || (! pos && TREE_INT_CST_HIGH (t) == -1
6661 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
6662 && !TYPE_UNSIGNED (TREE_TYPE (t)))
6663 || (pos && TREE_INT_CST_HIGH (t) == 0)));
6666 /* Return the HOST_WIDE_INT least significant bits of T if it is an
6667 INTEGER_CST and there is no overflow. POS is nonzero if the result must
6668 be non-negative. We must be able to satisfy the above conditions. */
6670 HOST_WIDE_INT
6671 tree_low_cst (const_tree t, int pos)
6673 gcc_assert (host_integerp (t, pos));
6674 return TREE_INT_CST_LOW (t);
6677 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
6678 kind INTEGER_CST. This makes sure to properly sign-extend the
6679 constant. */
6681 HOST_WIDE_INT
6682 size_low_cst (const_tree t)
6684 double_int d = tree_to_double_int (t);
6685 return d.sext (TYPE_PRECISION (TREE_TYPE (t))).low;
6688 /* Return the most significant (sign) bit of T. */
6691 tree_int_cst_sign_bit (const_tree t)
6693 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
6694 unsigned HOST_WIDE_INT w;
6696 if (bitno < HOST_BITS_PER_WIDE_INT)
6697 w = TREE_INT_CST_LOW (t);
6698 else
6700 w = TREE_INT_CST_HIGH (t);
6701 bitno -= HOST_BITS_PER_WIDE_INT;
6704 return (w >> bitno) & 1;
6707 /* Return an indication of the sign of the integer constant T.
6708 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6709 Note that -1 will never be returned if T's type is unsigned. */
6712 tree_int_cst_sgn (const_tree t)
6714 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
6715 return 0;
6716 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6717 return 1;
6718 else if (TREE_INT_CST_HIGH (t) < 0)
6719 return -1;
6720 else
6721 return 1;
6724 /* Return the minimum number of bits needed to represent VALUE in a
6725 signed or unsigned type, UNSIGNEDP says which. */
6727 unsigned int
6728 tree_int_cst_min_precision (tree value, bool unsignedp)
6730 int log;
6732 /* If the value is negative, compute its negative minus 1. The latter
6733 adjustment is because the absolute value of the largest negative value
6734 is one larger than the largest positive value. This is equivalent to
6735 a bit-wise negation, so use that operation instead. */
6737 if (tree_int_cst_sgn (value) < 0)
6738 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6740 /* Return the number of bits needed, taking into account the fact
6741 that we need one more bit for a signed than unsigned type. */
6743 if (integer_zerop (value))
6744 log = 0;
6745 else
6746 log = tree_floor_log2 (value);
6748 return log + 1 + !unsignedp;
6751 /* Compare two constructor-element-type constants. Return 1 if the lists
6752 are known to be equal; otherwise return 0. */
6755 simple_cst_list_equal (const_tree l1, const_tree l2)
6757 while (l1 != NULL_TREE && l2 != NULL_TREE)
6759 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
6760 return 0;
6762 l1 = TREE_CHAIN (l1);
6763 l2 = TREE_CHAIN (l2);
6766 return l1 == l2;
6769 /* Return truthvalue of whether T1 is the same tree structure as T2.
6770 Return 1 if they are the same.
6771 Return 0 if they are understandably different.
6772 Return -1 if either contains tree structure not understood by
6773 this function. */
6776 simple_cst_equal (const_tree t1, const_tree t2)
6778 enum tree_code code1, code2;
6779 int cmp;
6780 int i;
6782 if (t1 == t2)
6783 return 1;
6784 if (t1 == 0 || t2 == 0)
6785 return 0;
6787 code1 = TREE_CODE (t1);
6788 code2 = TREE_CODE (t2);
6790 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6792 if (CONVERT_EXPR_CODE_P (code2)
6793 || code2 == NON_LVALUE_EXPR)
6794 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6795 else
6796 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6799 else if (CONVERT_EXPR_CODE_P (code2)
6800 || code2 == NON_LVALUE_EXPR)
6801 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6803 if (code1 != code2)
6804 return 0;
6806 switch (code1)
6808 case INTEGER_CST:
6809 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6810 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
6812 case REAL_CST:
6813 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
6815 case FIXED_CST:
6816 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6818 case STRING_CST:
6819 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6820 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6821 TREE_STRING_LENGTH (t1)));
6823 case CONSTRUCTOR:
6825 unsigned HOST_WIDE_INT idx;
6826 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
6827 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
6829 if (vec_safe_length (v1) != vec_safe_length (v2))
6830 return false;
6832 for (idx = 0; idx < vec_safe_length (v1); ++idx)
6833 /* ??? Should we handle also fields here? */
6834 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
6835 return false;
6836 return true;
6839 case SAVE_EXPR:
6840 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6842 case CALL_EXPR:
6843 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6844 if (cmp <= 0)
6845 return cmp;
6846 if (call_expr_nargs (t1) != call_expr_nargs (t2))
6847 return 0;
6849 const_tree arg1, arg2;
6850 const_call_expr_arg_iterator iter1, iter2;
6851 for (arg1 = first_const_call_expr_arg (t1, &iter1),
6852 arg2 = first_const_call_expr_arg (t2, &iter2);
6853 arg1 && arg2;
6854 arg1 = next_const_call_expr_arg (&iter1),
6855 arg2 = next_const_call_expr_arg (&iter2))
6857 cmp = simple_cst_equal (arg1, arg2);
6858 if (cmp <= 0)
6859 return cmp;
6861 return arg1 == arg2;
6864 case TARGET_EXPR:
6865 /* Special case: if either target is an unallocated VAR_DECL,
6866 it means that it's going to be unified with whatever the
6867 TARGET_EXPR is really supposed to initialize, so treat it
6868 as being equivalent to anything. */
6869 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
6870 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
6871 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
6872 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
6873 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
6874 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
6875 cmp = 1;
6876 else
6877 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6879 if (cmp <= 0)
6880 return cmp;
6882 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
6884 case WITH_CLEANUP_EXPR:
6885 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6886 if (cmp <= 0)
6887 return cmp;
6889 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
6891 case COMPONENT_REF:
6892 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
6893 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6895 return 0;
6897 case VAR_DECL:
6898 case PARM_DECL:
6899 case CONST_DECL:
6900 case FUNCTION_DECL:
6901 return 0;
6903 default:
6904 break;
6907 /* This general rule works for most tree codes. All exceptions should be
6908 handled above. If this is a language-specific tree code, we can't
6909 trust what might be in the operand, so say we don't know
6910 the situation. */
6911 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
6912 return -1;
6914 switch (TREE_CODE_CLASS (code1))
6916 case tcc_unary:
6917 case tcc_binary:
6918 case tcc_comparison:
6919 case tcc_expression:
6920 case tcc_reference:
6921 case tcc_statement:
6922 cmp = 1;
6923 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
6925 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
6926 if (cmp <= 0)
6927 return cmp;
6930 return cmp;
6932 default:
6933 return -1;
6937 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
6938 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
6939 than U, respectively. */
6942 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
6944 if (tree_int_cst_sgn (t) < 0)
6945 return -1;
6946 else if (TREE_INT_CST_HIGH (t) != 0)
6947 return 1;
6948 else if (TREE_INT_CST_LOW (t) == u)
6949 return 0;
6950 else if (TREE_INT_CST_LOW (t) < u)
6951 return -1;
6952 else
6953 return 1;
6956 /* Return true if SIZE represents a constant size that is in bounds of
6957 what the middle-end and the backend accepts (covering not more than
6958 half of the address-space). */
6960 bool
6961 valid_constant_size_p (const_tree size)
6963 if (! host_integerp (size, 1)
6964 || TREE_OVERFLOW (size)
6965 || tree_int_cst_sign_bit (size) != 0)
6966 return false;
6967 return true;
6970 /* Return true if CODE represents an associative tree code. Otherwise
6971 return false. */
6972 bool
6973 associative_tree_code (enum tree_code code)
6975 switch (code)
6977 case BIT_IOR_EXPR:
6978 case BIT_AND_EXPR:
6979 case BIT_XOR_EXPR:
6980 case PLUS_EXPR:
6981 case MULT_EXPR:
6982 case MIN_EXPR:
6983 case MAX_EXPR:
6984 return true;
6986 default:
6987 break;
6989 return false;
6992 /* Return true if CODE represents a commutative tree code. Otherwise
6993 return false. */
6994 bool
6995 commutative_tree_code (enum tree_code code)
6997 switch (code)
6999 case PLUS_EXPR:
7000 case MULT_EXPR:
7001 case MULT_HIGHPART_EXPR:
7002 case MIN_EXPR:
7003 case MAX_EXPR:
7004 case BIT_IOR_EXPR:
7005 case BIT_XOR_EXPR:
7006 case BIT_AND_EXPR:
7007 case NE_EXPR:
7008 case EQ_EXPR:
7009 case UNORDERED_EXPR:
7010 case ORDERED_EXPR:
7011 case UNEQ_EXPR:
7012 case LTGT_EXPR:
7013 case TRUTH_AND_EXPR:
7014 case TRUTH_XOR_EXPR:
7015 case TRUTH_OR_EXPR:
7016 case WIDEN_MULT_EXPR:
7017 case VEC_WIDEN_MULT_HI_EXPR:
7018 case VEC_WIDEN_MULT_LO_EXPR:
7019 case VEC_WIDEN_MULT_EVEN_EXPR:
7020 case VEC_WIDEN_MULT_ODD_EXPR:
7021 return true;
7023 default:
7024 break;
7026 return false;
7029 /* Return true if CODE represents a ternary tree code for which the
7030 first two operands are commutative. Otherwise return false. */
7031 bool
7032 commutative_ternary_tree_code (enum tree_code code)
7034 switch (code)
7036 case WIDEN_MULT_PLUS_EXPR:
7037 case WIDEN_MULT_MINUS_EXPR:
7038 return true;
7040 default:
7041 break;
7043 return false;
7046 /* Generate a hash value for an expression. This can be used iteratively
7047 by passing a previous result as the VAL argument.
7049 This function is intended to produce the same hash for expressions which
7050 would compare equal using operand_equal_p. */
7052 hashval_t
7053 iterative_hash_expr (const_tree t, hashval_t val)
7055 int i;
7056 enum tree_code code;
7057 char tclass;
7059 if (t == NULL_TREE)
7060 return iterative_hash_hashval_t (0, val);
7062 code = TREE_CODE (t);
7064 switch (code)
7066 /* Alas, constants aren't shared, so we can't rely on pointer
7067 identity. */
7068 case INTEGER_CST:
7069 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
7070 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
7071 case REAL_CST:
7073 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7075 return iterative_hash_hashval_t (val2, val);
7077 case FIXED_CST:
7079 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7081 return iterative_hash_hashval_t (val2, val);
7083 case STRING_CST:
7084 return iterative_hash (TREE_STRING_POINTER (t),
7085 TREE_STRING_LENGTH (t), val);
7086 case COMPLEX_CST:
7087 val = iterative_hash_expr (TREE_REALPART (t), val);
7088 return iterative_hash_expr (TREE_IMAGPART (t), val);
7089 case VECTOR_CST:
7091 unsigned i;
7092 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7093 val = iterative_hash_expr (VECTOR_CST_ELT (t, i), val);
7094 return val;
7096 case SSA_NAME:
7097 /* We can just compare by pointer. */
7098 return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
7099 case PLACEHOLDER_EXPR:
7100 /* The node itself doesn't matter. */
7101 return val;
7102 case TREE_LIST:
7103 /* A list of expressions, for a CALL_EXPR or as the elements of a
7104 VECTOR_CST. */
7105 for (; t; t = TREE_CHAIN (t))
7106 val = iterative_hash_expr (TREE_VALUE (t), val);
7107 return val;
7108 case CONSTRUCTOR:
7110 unsigned HOST_WIDE_INT idx;
7111 tree field, value;
7112 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7114 val = iterative_hash_expr (field, val);
7115 val = iterative_hash_expr (value, val);
7117 return val;
7119 case MEM_REF:
7121 /* The type of the second operand is relevant, except for
7122 its top-level qualifiers. */
7123 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (t, 1)));
7125 val = iterative_hash_object (TYPE_HASH (type), val);
7127 /* We could use the standard hash computation from this point
7128 on. */
7129 val = iterative_hash_object (code, val);
7130 val = iterative_hash_expr (TREE_OPERAND (t, 1), val);
7131 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7132 return val;
7134 case FUNCTION_DECL:
7135 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7136 Otherwise nodes that compare equal according to operand_equal_p might
7137 get different hash codes. However, don't do this for machine specific
7138 or front end builtins, since the function code is overloaded in those
7139 cases. */
7140 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7141 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7143 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7144 code = TREE_CODE (t);
7146 /* FALL THROUGH */
7147 default:
7148 tclass = TREE_CODE_CLASS (code);
7150 if (tclass == tcc_declaration)
7152 /* DECL's have a unique ID */
7153 val = iterative_hash_host_wide_int (DECL_UID (t), val);
7155 else
7157 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7159 val = iterative_hash_object (code, val);
7161 /* Don't hash the type, that can lead to having nodes which
7162 compare equal according to operand_equal_p, but which
7163 have different hash codes. */
7164 if (CONVERT_EXPR_CODE_P (code)
7165 || code == NON_LVALUE_EXPR)
7167 /* Make sure to include signness in the hash computation. */
7168 val += TYPE_UNSIGNED (TREE_TYPE (t));
7169 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7172 else if (commutative_tree_code (code))
7174 /* It's a commutative expression. We want to hash it the same
7175 however it appears. We do this by first hashing both operands
7176 and then rehashing based on the order of their independent
7177 hashes. */
7178 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
7179 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
7180 hashval_t t;
7182 if (one > two)
7183 t = one, one = two, two = t;
7185 val = iterative_hash_hashval_t (one, val);
7186 val = iterative_hash_hashval_t (two, val);
7188 else
7189 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7190 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
7192 return val;
7196 /* Generate a hash value for a pair of expressions. This can be used
7197 iteratively by passing a previous result as the VAL argument.
7199 The same hash value is always returned for a given pair of expressions,
7200 regardless of the order in which they are presented. This is useful in
7201 hashing the operands of commutative functions. */
7203 hashval_t
7204 iterative_hash_exprs_commutative (const_tree t1,
7205 const_tree t2, hashval_t val)
7207 hashval_t one = iterative_hash_expr (t1, 0);
7208 hashval_t two = iterative_hash_expr (t2, 0);
7209 hashval_t t;
7211 if (one > two)
7212 t = one, one = two, two = t;
7213 val = iterative_hash_hashval_t (one, val);
7214 val = iterative_hash_hashval_t (two, val);
7216 return val;
7219 /* Constructors for pointer, array and function types.
7220 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7221 constructed by language-dependent code, not here.) */
7223 /* Construct, lay out and return the type of pointers to TO_TYPE with
7224 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7225 reference all of memory. If such a type has already been
7226 constructed, reuse it. */
7228 tree
7229 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
7230 bool can_alias_all)
7232 tree t;
7234 if (to_type == error_mark_node)
7235 return error_mark_node;
7237 /* If the pointed-to type has the may_alias attribute set, force
7238 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7239 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7240 can_alias_all = true;
7242 /* In some cases, languages will have things that aren't a POINTER_TYPE
7243 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7244 In that case, return that type without regard to the rest of our
7245 operands.
7247 ??? This is a kludge, but consistent with the way this function has
7248 always operated and there doesn't seem to be a good way to avoid this
7249 at the moment. */
7250 if (TYPE_POINTER_TO (to_type) != 0
7251 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7252 return TYPE_POINTER_TO (to_type);
7254 /* First, if we already have a type for pointers to TO_TYPE and it's
7255 the proper mode, use it. */
7256 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7257 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7258 return t;
7260 t = make_node (POINTER_TYPE);
7262 TREE_TYPE (t) = to_type;
7263 SET_TYPE_MODE (t, mode);
7264 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7265 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7266 TYPE_POINTER_TO (to_type) = t;
7268 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7269 SET_TYPE_STRUCTURAL_EQUALITY (t);
7270 else if (TYPE_CANONICAL (to_type) != to_type)
7271 TYPE_CANONICAL (t)
7272 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7273 mode, can_alias_all);
7275 /* Lay out the type. This function has many callers that are concerned
7276 with expression-construction, and this simplifies them all. */
7277 layout_type (t);
7279 return t;
7282 /* By default build pointers in ptr_mode. */
7284 tree
7285 build_pointer_type (tree to_type)
7287 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7288 : TYPE_ADDR_SPACE (to_type);
7289 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7290 if (upc_shared_type_p (to_type))
7292 tree upc_pts_type;
7293 pointer_mode = TYPE_MODE (upc_pts_rep_type_node);
7294 upc_pts_type = build_pointer_type_for_mode (to_type, pointer_mode,
7295 false);
7296 TYPE_USER_ALIGN (upc_pts_type) = TYPE_USER_ALIGN (upc_pts_rep_type_node);
7297 TYPE_ALIGN (upc_pts_type) = TYPE_ALIGN (upc_pts_rep_type_node);
7298 return upc_pts_type;
7300 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7303 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7305 tree
7306 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7307 bool can_alias_all)
7309 tree t;
7311 if (to_type == error_mark_node)
7312 return error_mark_node;
7314 /* If the pointed-to type has the may_alias attribute set, force
7315 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7316 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7317 can_alias_all = true;
7319 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7320 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7321 In that case, return that type without regard to the rest of our
7322 operands.
7324 ??? This is a kludge, but consistent with the way this function has
7325 always operated and there doesn't seem to be a good way to avoid this
7326 at the moment. */
7327 if (TYPE_REFERENCE_TO (to_type) != 0
7328 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7329 return TYPE_REFERENCE_TO (to_type);
7331 /* First, if we already have a type for pointers to TO_TYPE and it's
7332 the proper mode, use it. */
7333 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7334 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7335 return t;
7337 t = make_node (REFERENCE_TYPE);
7339 TREE_TYPE (t) = to_type;
7340 SET_TYPE_MODE (t, mode);
7341 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7342 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7343 TYPE_REFERENCE_TO (to_type) = t;
7345 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7346 SET_TYPE_STRUCTURAL_EQUALITY (t);
7347 else if (TYPE_CANONICAL (to_type) != to_type)
7348 TYPE_CANONICAL (t)
7349 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7350 mode, can_alias_all);
7352 layout_type (t);
7354 return t;
7358 /* Build the node for the type of references-to-TO_TYPE by default
7359 in ptr_mode. */
7361 tree
7362 build_reference_type (tree to_type)
7364 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7365 : TYPE_ADDR_SPACE (to_type);
7366 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7367 return build_reference_type_for_mode (to_type, pointer_mode, false);
7370 /* Build a type that is compatible with t but has no cv quals anywhere
7371 in its type, thus
7373 const char *const *const * -> char ***. */
7375 tree
7376 build_type_no_quals (tree t)
7378 switch (TREE_CODE (t))
7380 case POINTER_TYPE:
7381 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7382 TYPE_MODE (t),
7383 TYPE_REF_CAN_ALIAS_ALL (t));
7384 case REFERENCE_TYPE:
7385 return
7386 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7387 TYPE_MODE (t),
7388 TYPE_REF_CAN_ALIAS_ALL (t));
7389 default:
7390 return TYPE_MAIN_VARIANT (t);
7394 #define MAX_INT_CACHED_PREC \
7395 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7396 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7398 /* Builds a signed or unsigned integer type of precision PRECISION.
7399 Used for C bitfields whose precision does not match that of
7400 built-in target types. */
7401 tree
7402 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7403 int unsignedp)
7405 tree itype, ret;
7407 if (unsignedp)
7408 unsignedp = MAX_INT_CACHED_PREC + 1;
7410 if (precision <= MAX_INT_CACHED_PREC)
7412 itype = nonstandard_integer_type_cache[precision + unsignedp];
7413 if (itype)
7414 return itype;
7417 itype = make_node (INTEGER_TYPE);
7418 TYPE_PRECISION (itype) = precision;
7420 if (unsignedp)
7421 fixup_unsigned_type (itype);
7422 else
7423 fixup_signed_type (itype);
7425 ret = itype;
7426 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
7427 ret = type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
7428 if (precision <= MAX_INT_CACHED_PREC)
7429 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7431 return ret;
7434 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7435 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7436 is true, reuse such a type that has already been constructed. */
7438 static tree
7439 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7441 tree itype = make_node (INTEGER_TYPE);
7442 hashval_t hashcode = 0;
7444 TREE_TYPE (itype) = type;
7446 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7447 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7449 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7450 SET_TYPE_MODE (itype, TYPE_MODE (type));
7451 TYPE_SIZE (itype) = TYPE_SIZE (type);
7452 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7453 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7454 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7456 if (!shared)
7457 return itype;
7459 if ((TYPE_MIN_VALUE (itype)
7460 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7461 || (TYPE_MAX_VALUE (itype)
7462 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7464 /* Since we cannot reliably merge this type, we need to compare it using
7465 structural equality checks. */
7466 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7467 return itype;
7470 hashcode = iterative_hash_expr (TYPE_MIN_VALUE (itype), hashcode);
7471 hashcode = iterative_hash_expr (TYPE_MAX_VALUE (itype), hashcode);
7472 hashcode = iterative_hash_hashval_t (TYPE_HASH (type), hashcode);
7473 itype = type_hash_canon (hashcode, itype);
7475 return itype;
7478 /* Wrapper around build_range_type_1 with SHARED set to true. */
7480 tree
7481 build_range_type (tree type, tree lowval, tree highval)
7483 return build_range_type_1 (type, lowval, highval, true);
7486 /* Wrapper around build_range_type_1 with SHARED set to false. */
7488 tree
7489 build_nonshared_range_type (tree type, tree lowval, tree highval)
7491 return build_range_type_1 (type, lowval, highval, false);
7494 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7495 MAXVAL should be the maximum value in the domain
7496 (one less than the length of the array).
7498 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7499 We don't enforce this limit, that is up to caller (e.g. language front end).
7500 The limit exists because the result is a signed type and we don't handle
7501 sizes that use more than one HOST_WIDE_INT. */
7503 tree
7504 build_index_type (tree maxval)
7506 return build_range_type (sizetype, size_zero_node, maxval);
7509 /* Return true if the debug information for TYPE, a subtype, should be emitted
7510 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7511 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7512 debug info and doesn't reflect the source code. */
7514 bool
7515 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7517 tree base_type = TREE_TYPE (type), low, high;
7519 /* Subrange types have a base type which is an integral type. */
7520 if (!INTEGRAL_TYPE_P (base_type))
7521 return false;
7523 /* Get the real bounds of the subtype. */
7524 if (lang_hooks.types.get_subrange_bounds)
7525 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7526 else
7528 low = TYPE_MIN_VALUE (type);
7529 high = TYPE_MAX_VALUE (type);
7532 /* If the type and its base type have the same representation and the same
7533 name, then the type is not a subrange but a copy of the base type. */
7534 if ((TREE_CODE (base_type) == INTEGER_TYPE
7535 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7536 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7537 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7538 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type)))
7540 tree type_name = TYPE_NAME (type);
7541 tree base_type_name = TYPE_NAME (base_type);
7543 if (type_name && TREE_CODE (type_name) == TYPE_DECL)
7544 type_name = DECL_NAME (type_name);
7546 if (base_type_name && TREE_CODE (base_type_name) == TYPE_DECL)
7547 base_type_name = DECL_NAME (base_type_name);
7549 if (type_name == base_type_name)
7550 return false;
7553 if (lowval)
7554 *lowval = low;
7555 if (highval)
7556 *highval = high;
7557 return true;
7560 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7561 and number of elements specified by the range of values of INDEX_TYPE.
7562 If SHARED is true, reuse such a type that has already been constructed. */
7564 static tree
7565 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7567 tree t;
7569 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7571 error ("arrays of functions are not meaningful");
7572 elt_type = integer_type_node;
7575 t = make_node (ARRAY_TYPE);
7576 TREE_TYPE (t) = elt_type;
7577 TYPE_DOMAIN (t) = index_type;
7578 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7579 layout_type (t);
7581 /* If the element type is incomplete at this point we get marked for
7582 structural equality. Do not record these types in the canonical
7583 type hashtable. */
7584 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7585 return t;
7587 if (shared)
7589 hashval_t hashcode = iterative_hash_object (TYPE_HASH (elt_type), 0);
7590 if (index_type)
7591 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
7592 t = type_hash_canon (hashcode, t);
7595 if (TYPE_CANONICAL (t) == t)
7597 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7598 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7599 SET_TYPE_STRUCTURAL_EQUALITY (t);
7600 else if (TYPE_CANONICAL (elt_type) != elt_type
7601 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7602 TYPE_CANONICAL (t)
7603 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7604 index_type
7605 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7606 shared);
7609 return t;
7612 /* Wrapper around build_array_type_1 with SHARED set to true. */
7614 tree
7615 build_array_type (tree elt_type, tree index_type)
7617 return build_array_type_1 (elt_type, index_type, true);
7620 /* Wrapper around build_array_type_1 with SHARED set to false. */
7622 tree
7623 build_nonshared_array_type (tree elt_type, tree index_type)
7625 return build_array_type_1 (elt_type, index_type, false);
7628 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7629 sizetype. */
7631 tree
7632 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7634 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7637 /* Recursively examines the array elements of TYPE, until a non-array
7638 element type is found. */
7640 tree
7641 strip_array_types (tree type)
7643 while (TREE_CODE (type) == ARRAY_TYPE)
7644 type = TREE_TYPE (type);
7646 return type;
7649 /* Computes the canonical argument types from the argument type list
7650 ARGTYPES.
7652 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7653 on entry to this function, or if any of the ARGTYPES are
7654 structural.
7656 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7657 true on entry to this function, or if any of the ARGTYPES are
7658 non-canonical.
7660 Returns a canonical argument list, which may be ARGTYPES when the
7661 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7662 true) or would not differ from ARGTYPES. */
7664 static tree
7665 maybe_canonicalize_argtypes(tree argtypes,
7666 bool *any_structural_p,
7667 bool *any_noncanonical_p)
7669 tree arg;
7670 bool any_noncanonical_argtypes_p = false;
7672 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7674 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7675 /* Fail gracefully by stating that the type is structural. */
7676 *any_structural_p = true;
7677 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7678 *any_structural_p = true;
7679 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7680 || TREE_PURPOSE (arg))
7681 /* If the argument has a default argument, we consider it
7682 non-canonical even though the type itself is canonical.
7683 That way, different variants of function and method types
7684 with default arguments will all point to the variant with
7685 no defaults as their canonical type. */
7686 any_noncanonical_argtypes_p = true;
7689 if (*any_structural_p)
7690 return argtypes;
7692 if (any_noncanonical_argtypes_p)
7694 /* Build the canonical list of argument types. */
7695 tree canon_argtypes = NULL_TREE;
7696 bool is_void = false;
7698 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7700 if (arg == void_list_node)
7701 is_void = true;
7702 else
7703 canon_argtypes = tree_cons (NULL_TREE,
7704 TYPE_CANONICAL (TREE_VALUE (arg)),
7705 canon_argtypes);
7708 canon_argtypes = nreverse (canon_argtypes);
7709 if (is_void)
7710 canon_argtypes = chainon (canon_argtypes, void_list_node);
7712 /* There is a non-canonical type. */
7713 *any_noncanonical_p = true;
7714 return canon_argtypes;
7717 /* The canonical argument types are the same as ARGTYPES. */
7718 return argtypes;
7721 /* Construct, lay out and return
7722 the type of functions returning type VALUE_TYPE
7723 given arguments of types ARG_TYPES.
7724 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7725 are data type nodes for the arguments of the function.
7726 If such a type has already been constructed, reuse it. */
7728 tree
7729 build_function_type (tree value_type, tree arg_types)
7731 tree t;
7732 hashval_t hashcode = 0;
7733 bool any_structural_p, any_noncanonical_p;
7734 tree canon_argtypes;
7736 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7738 error ("function return type cannot be function");
7739 value_type = integer_type_node;
7742 /* Make a node of the sort we want. */
7743 t = make_node (FUNCTION_TYPE);
7744 TREE_TYPE (t) = value_type;
7745 TYPE_ARG_TYPES (t) = arg_types;
7747 /* If we already have such a type, use the old one. */
7748 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
7749 hashcode = type_hash_list (arg_types, hashcode);
7750 t = type_hash_canon (hashcode, t);
7752 /* Set up the canonical type. */
7753 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7754 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7755 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7756 &any_structural_p,
7757 &any_noncanonical_p);
7758 if (any_structural_p)
7759 SET_TYPE_STRUCTURAL_EQUALITY (t);
7760 else if (any_noncanonical_p)
7761 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7762 canon_argtypes);
7764 if (!COMPLETE_TYPE_P (t))
7765 layout_type (t);
7766 return t;
7769 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
7770 return value if SKIP_RETURN is true. */
7772 static tree
7773 build_function_type_skip_args (tree orig_type, bitmap args_to_skip,
7774 bool skip_return)
7776 tree new_type = NULL;
7777 tree args, new_args = NULL, t;
7778 tree new_reversed;
7779 int i = 0;
7781 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
7782 args = TREE_CHAIN (args), i++)
7783 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
7784 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
7786 new_reversed = nreverse (new_args);
7787 if (args)
7789 if (new_reversed)
7790 TREE_CHAIN (new_args) = void_list_node;
7791 else
7792 new_reversed = void_list_node;
7795 /* Use copy_node to preserve as much as possible from original type
7796 (debug info, attribute lists etc.)
7797 Exception is METHOD_TYPEs must have THIS argument.
7798 When we are asked to remove it, we need to build new FUNCTION_TYPE
7799 instead. */
7800 if (TREE_CODE (orig_type) != METHOD_TYPE
7801 || !args_to_skip
7802 || !bitmap_bit_p (args_to_skip, 0))
7804 new_type = build_distinct_type_copy (orig_type);
7805 TYPE_ARG_TYPES (new_type) = new_reversed;
7807 else
7809 new_type
7810 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
7811 new_reversed));
7812 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
7815 if (skip_return)
7816 TREE_TYPE (new_type) = void_type_node;
7818 /* This is a new type, not a copy of an old type. Need to reassociate
7819 variants. We can handle everything except the main variant lazily. */
7820 t = TYPE_MAIN_VARIANT (orig_type);
7821 if (t != orig_type)
7823 t = build_function_type_skip_args (t, args_to_skip, skip_return);
7824 TYPE_MAIN_VARIANT (new_type) = t;
7825 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
7826 TYPE_NEXT_VARIANT (t) = new_type;
7828 else
7830 TYPE_MAIN_VARIANT (new_type) = new_type;
7831 TYPE_NEXT_VARIANT (new_type) = NULL;
7834 return new_type;
7837 /* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
7838 return value if SKIP_RETURN is true.
7840 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
7841 linked by TREE_CHAIN directly. The caller is responsible for eliminating
7842 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
7844 tree
7845 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip,
7846 bool skip_return)
7848 tree new_decl = copy_node (orig_decl);
7849 tree new_type;
7851 new_type = TREE_TYPE (orig_decl);
7852 if (prototype_p (new_type)
7853 || (skip_return && !VOID_TYPE_P (TREE_TYPE (new_type))))
7854 new_type
7855 = build_function_type_skip_args (new_type, args_to_skip, skip_return);
7856 TREE_TYPE (new_decl) = new_type;
7858 /* For declarations setting DECL_VINDEX (i.e. methods)
7859 we expect first argument to be THIS pointer. */
7860 if (args_to_skip && bitmap_bit_p (args_to_skip, 0))
7861 DECL_VINDEX (new_decl) = NULL_TREE;
7863 /* When signature changes, we need to clear builtin info. */
7864 if (DECL_BUILT_IN (new_decl)
7865 && args_to_skip
7866 && !bitmap_empty_p (args_to_skip))
7868 DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
7869 DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
7871 return new_decl;
7874 /* Build a function type. The RETURN_TYPE is the type returned by the
7875 function. If VAARGS is set, no void_type_node is appended to the
7876 the list. ARGP must be always be terminated be a NULL_TREE. */
7878 static tree
7879 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
7881 tree t, args, last;
7883 t = va_arg (argp, tree);
7884 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
7885 args = tree_cons (NULL_TREE, t, args);
7887 if (vaargs)
7889 last = args;
7890 if (args != NULL_TREE)
7891 args = nreverse (args);
7892 gcc_assert (last != void_list_node);
7894 else if (args == NULL_TREE)
7895 args = void_list_node;
7896 else
7898 last = args;
7899 args = nreverse (args);
7900 TREE_CHAIN (last) = void_list_node;
7902 args = build_function_type (return_type, args);
7904 return args;
7907 /* Build a function type. The RETURN_TYPE is the type returned by the
7908 function. If additional arguments are provided, they are
7909 additional argument types. The list of argument types must always
7910 be terminated by NULL_TREE. */
7912 tree
7913 build_function_type_list (tree return_type, ...)
7915 tree args;
7916 va_list p;
7918 va_start (p, return_type);
7919 args = build_function_type_list_1 (false, return_type, p);
7920 va_end (p);
7921 return args;
7924 /* Build a variable argument function type. The RETURN_TYPE is the
7925 type returned by the function. If additional arguments are provided,
7926 they are additional argument types. The list of argument types must
7927 always be terminated by NULL_TREE. */
7929 tree
7930 build_varargs_function_type_list (tree return_type, ...)
7932 tree args;
7933 va_list p;
7935 va_start (p, return_type);
7936 args = build_function_type_list_1 (true, return_type, p);
7937 va_end (p);
7939 return args;
7942 /* Build a function type. RETURN_TYPE is the type returned by the
7943 function; VAARGS indicates whether the function takes varargs. The
7944 function takes N named arguments, the types of which are provided in
7945 ARG_TYPES. */
7947 static tree
7948 build_function_type_array_1 (bool vaargs, tree return_type, int n,
7949 tree *arg_types)
7951 int i;
7952 tree t = vaargs ? NULL_TREE : void_list_node;
7954 for (i = n - 1; i >= 0; i--)
7955 t = tree_cons (NULL_TREE, arg_types[i], t);
7957 return build_function_type (return_type, t);
7960 /* Build a function type. RETURN_TYPE is the type returned by the
7961 function. The function takes N named arguments, the types of which
7962 are provided in ARG_TYPES. */
7964 tree
7965 build_function_type_array (tree return_type, int n, tree *arg_types)
7967 return build_function_type_array_1 (false, return_type, n, arg_types);
7970 /* Build a variable argument function type. RETURN_TYPE is the type
7971 returned by the function. The function takes N named arguments, the
7972 types of which are provided in ARG_TYPES. */
7974 tree
7975 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
7977 return build_function_type_array_1 (true, return_type, n, arg_types);
7980 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
7981 and ARGTYPES (a TREE_LIST) are the return type and arguments types
7982 for the method. An implicit additional parameter (of type
7983 pointer-to-BASETYPE) is added to the ARGTYPES. */
7985 tree
7986 build_method_type_directly (tree basetype,
7987 tree rettype,
7988 tree argtypes)
7990 tree t;
7991 tree ptype;
7992 int hashcode = 0;
7993 bool any_structural_p, any_noncanonical_p;
7994 tree canon_argtypes;
7996 /* Make a node of the sort we want. */
7997 t = make_node (METHOD_TYPE);
7999 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8000 TREE_TYPE (t) = rettype;
8001 ptype = build_pointer_type (basetype);
8003 /* The actual arglist for this function includes a "hidden" argument
8004 which is "this". Put it into the list of argument types. */
8005 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8006 TYPE_ARG_TYPES (t) = argtypes;
8008 /* If we already have such a type, use the old one. */
8009 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8010 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
8011 hashcode = type_hash_list (argtypes, hashcode);
8012 t = type_hash_canon (hashcode, t);
8014 /* Set up the canonical type. */
8015 any_structural_p
8016 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8017 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8018 any_noncanonical_p
8019 = (TYPE_CANONICAL (basetype) != basetype
8020 || TYPE_CANONICAL (rettype) != rettype);
8021 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8022 &any_structural_p,
8023 &any_noncanonical_p);
8024 if (any_structural_p)
8025 SET_TYPE_STRUCTURAL_EQUALITY (t);
8026 else if (any_noncanonical_p)
8027 TYPE_CANONICAL (t)
8028 = build_method_type_directly (TYPE_CANONICAL (basetype),
8029 TYPE_CANONICAL (rettype),
8030 canon_argtypes);
8031 if (!COMPLETE_TYPE_P (t))
8032 layout_type (t);
8034 return t;
8037 /* Construct, lay out and return the type of methods belonging to class
8038 BASETYPE and whose arguments and values are described by TYPE.
8039 If that type exists already, reuse it.
8040 TYPE must be a FUNCTION_TYPE node. */
8042 tree
8043 build_method_type (tree basetype, tree type)
8045 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8047 return build_method_type_directly (basetype,
8048 TREE_TYPE (type),
8049 TYPE_ARG_TYPES (type));
8052 /* Construct, lay out and return the type of offsets to a value
8053 of type TYPE, within an object of type BASETYPE.
8054 If a suitable offset type exists already, reuse it. */
8056 tree
8057 build_offset_type (tree basetype, tree type)
8059 tree t;
8060 hashval_t hashcode = 0;
8062 /* Make a node of the sort we want. */
8063 t = make_node (OFFSET_TYPE);
8065 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8066 TREE_TYPE (t) = type;
8068 /* If we already have such a type, use the old one. */
8069 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8070 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
8071 t = type_hash_canon (hashcode, t);
8073 if (!COMPLETE_TYPE_P (t))
8074 layout_type (t);
8076 if (TYPE_CANONICAL (t) == t)
8078 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8079 || TYPE_STRUCTURAL_EQUALITY_P (type))
8080 SET_TYPE_STRUCTURAL_EQUALITY (t);
8081 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8082 || TYPE_CANONICAL (type) != type)
8083 TYPE_CANONICAL (t)
8084 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8085 TYPE_CANONICAL (type));
8088 return t;
8091 /* Create a complex type whose components are COMPONENT_TYPE. */
8093 tree
8094 build_complex_type (tree component_type)
8096 tree t;
8097 hashval_t hashcode;
8099 gcc_assert (INTEGRAL_TYPE_P (component_type)
8100 || SCALAR_FLOAT_TYPE_P (component_type)
8101 || FIXED_POINT_TYPE_P (component_type));
8103 /* Make a node of the sort we want. */
8104 t = make_node (COMPLEX_TYPE);
8106 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8108 /* If we already have such a type, use the old one. */
8109 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
8110 t = type_hash_canon (hashcode, t);
8112 if (!COMPLETE_TYPE_P (t))
8113 layout_type (t);
8115 if (TYPE_CANONICAL (t) == t)
8117 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8118 SET_TYPE_STRUCTURAL_EQUALITY (t);
8119 else if (TYPE_CANONICAL (component_type) != component_type)
8120 TYPE_CANONICAL (t)
8121 = build_complex_type (TYPE_CANONICAL (component_type));
8124 /* We need to create a name, since complex is a fundamental type. */
8125 if (! TYPE_NAME (t))
8127 const char *name;
8128 if (component_type == char_type_node)
8129 name = "complex char";
8130 else if (component_type == signed_char_type_node)
8131 name = "complex signed char";
8132 else if (component_type == unsigned_char_type_node)
8133 name = "complex unsigned char";
8134 else if (component_type == short_integer_type_node)
8135 name = "complex short int";
8136 else if (component_type == short_unsigned_type_node)
8137 name = "complex short unsigned int";
8138 else if (component_type == integer_type_node)
8139 name = "complex int";
8140 else if (component_type == unsigned_type_node)
8141 name = "complex unsigned int";
8142 else if (component_type == long_integer_type_node)
8143 name = "complex long int";
8144 else if (component_type == long_unsigned_type_node)
8145 name = "complex long unsigned int";
8146 else if (component_type == long_long_integer_type_node)
8147 name = "complex long long int";
8148 else if (component_type == long_long_unsigned_type_node)
8149 name = "complex long long unsigned int";
8150 else
8151 name = 0;
8153 if (name != 0)
8154 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8155 get_identifier (name), t);
8158 return build_qualified_type (t, TYPE_QUALS (component_type));
8161 /* If TYPE is a real or complex floating-point type and the target
8162 does not directly support arithmetic on TYPE then return the wider
8163 type to be used for arithmetic on TYPE. Otherwise, return
8164 NULL_TREE. */
8166 tree
8167 excess_precision_type (tree type)
8169 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8171 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8172 switch (TREE_CODE (type))
8174 case REAL_TYPE:
8175 switch (flt_eval_method)
8177 case 1:
8178 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8179 return double_type_node;
8180 break;
8181 case 2:
8182 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8183 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8184 return long_double_type_node;
8185 break;
8186 default:
8187 gcc_unreachable ();
8189 break;
8190 case COMPLEX_TYPE:
8191 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8192 return NULL_TREE;
8193 switch (flt_eval_method)
8195 case 1:
8196 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8197 return complex_double_type_node;
8198 break;
8199 case 2:
8200 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8201 || (TYPE_MODE (TREE_TYPE (type))
8202 == TYPE_MODE (double_type_node)))
8203 return complex_long_double_type_node;
8204 break;
8205 default:
8206 gcc_unreachable ();
8208 break;
8209 default:
8210 break;
8213 return NULL_TREE;
8216 /* Return OP, stripped of any conversions to wider types as much as is safe.
8217 Converting the value back to OP's type makes a value equivalent to OP.
8219 If FOR_TYPE is nonzero, we return a value which, if converted to
8220 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8222 OP must have integer, real or enumeral type. Pointers are not allowed!
8224 There are some cases where the obvious value we could return
8225 would regenerate to OP if converted to OP's type,
8226 but would not extend like OP to wider types.
8227 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8228 For example, if OP is (unsigned short)(signed char)-1,
8229 we avoid returning (signed char)-1 if FOR_TYPE is int,
8230 even though extending that to an unsigned short would regenerate OP,
8231 since the result of extending (signed char)-1 to (int)
8232 is different from (int) OP. */
8234 tree
8235 get_unwidened (tree op, tree for_type)
8237 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8238 tree type = TREE_TYPE (op);
8239 unsigned final_prec
8240 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8241 int uns
8242 = (for_type != 0 && for_type != type
8243 && final_prec > TYPE_PRECISION (type)
8244 && TYPE_UNSIGNED (type));
8245 tree win = op;
8247 while (CONVERT_EXPR_P (op))
8249 int bitschange;
8251 /* TYPE_PRECISION on vector types has different meaning
8252 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8253 so avoid them here. */
8254 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8255 break;
8257 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8258 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8260 /* Truncations are many-one so cannot be removed.
8261 Unless we are later going to truncate down even farther. */
8262 if (bitschange < 0
8263 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8264 break;
8266 /* See what's inside this conversion. If we decide to strip it,
8267 we will set WIN. */
8268 op = TREE_OPERAND (op, 0);
8270 /* If we have not stripped any zero-extensions (uns is 0),
8271 we can strip any kind of extension.
8272 If we have previously stripped a zero-extension,
8273 only zero-extensions can safely be stripped.
8274 Any extension can be stripped if the bits it would produce
8275 are all going to be discarded later by truncating to FOR_TYPE. */
8277 if (bitschange > 0)
8279 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8280 win = op;
8281 /* TYPE_UNSIGNED says whether this is a zero-extension.
8282 Let's avoid computing it if it does not affect WIN
8283 and if UNS will not be needed again. */
8284 if ((uns
8285 || CONVERT_EXPR_P (op))
8286 && TYPE_UNSIGNED (TREE_TYPE (op)))
8288 uns = 1;
8289 win = op;
8294 /* If we finally reach a constant see if it fits in for_type and
8295 in that case convert it. */
8296 if (for_type
8297 && TREE_CODE (win) == INTEGER_CST
8298 && TREE_TYPE (win) != for_type
8299 && int_fits_type_p (win, for_type))
8300 win = fold_convert (for_type, win);
8302 return win;
8305 /* Return OP or a simpler expression for a narrower value
8306 which can be sign-extended or zero-extended to give back OP.
8307 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8308 or 0 if the value should be sign-extended. */
8310 tree
8311 get_narrower (tree op, int *unsignedp_ptr)
8313 int uns = 0;
8314 int first = 1;
8315 tree win = op;
8316 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8318 while (TREE_CODE (op) == NOP_EXPR)
8320 int bitschange
8321 = (TYPE_PRECISION (TREE_TYPE (op))
8322 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8324 /* Truncations are many-one so cannot be removed. */
8325 if (bitschange < 0)
8326 break;
8328 /* See what's inside this conversion. If we decide to strip it,
8329 we will set WIN. */
8331 if (bitschange > 0)
8333 op = TREE_OPERAND (op, 0);
8334 /* An extension: the outermost one can be stripped,
8335 but remember whether it is zero or sign extension. */
8336 if (first)
8337 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8338 /* Otherwise, if a sign extension has been stripped,
8339 only sign extensions can now be stripped;
8340 if a zero extension has been stripped, only zero-extensions. */
8341 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8342 break;
8343 first = 0;
8345 else /* bitschange == 0 */
8347 /* A change in nominal type can always be stripped, but we must
8348 preserve the unsignedness. */
8349 if (first)
8350 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8351 first = 0;
8352 op = TREE_OPERAND (op, 0);
8353 /* Keep trying to narrow, but don't assign op to win if it
8354 would turn an integral type into something else. */
8355 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8356 continue;
8359 win = op;
8362 if (TREE_CODE (op) == COMPONENT_REF
8363 /* Since type_for_size always gives an integer type. */
8364 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8365 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8366 /* Ensure field is laid out already. */
8367 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8368 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
8370 unsigned HOST_WIDE_INT innerprec
8371 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
8372 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8373 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8374 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8376 /* We can get this structure field in a narrower type that fits it,
8377 but the resulting extension to its nominal type (a fullword type)
8378 must satisfy the same conditions as for other extensions.
8380 Do this only for fields that are aligned (not bit-fields),
8381 because when bit-field insns will be used there is no
8382 advantage in doing this. */
8384 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8385 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8386 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8387 && type != 0)
8389 if (first)
8390 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8391 win = fold_convert (type, op);
8395 *unsignedp_ptr = uns;
8396 return win;
8399 /* Returns true if integer constant C has a value that is permissible
8400 for type TYPE (an INTEGER_TYPE). */
8402 bool
8403 int_fits_type_p (const_tree c, const_tree type)
8405 tree type_low_bound, type_high_bound;
8406 bool ok_for_low_bound, ok_for_high_bound, unsc;
8407 double_int dc, dd;
8409 dc = tree_to_double_int (c);
8410 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
8412 retry:
8413 type_low_bound = TYPE_MIN_VALUE (type);
8414 type_high_bound = TYPE_MAX_VALUE (type);
8416 /* If at least one bound of the type is a constant integer, we can check
8417 ourselves and maybe make a decision. If no such decision is possible, but
8418 this type is a subtype, try checking against that. Otherwise, use
8419 double_int_fits_to_tree_p, which checks against the precision.
8421 Compute the status for each possibly constant bound, and return if we see
8422 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8423 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8424 for "constant known to fit". */
8426 /* Check if c >= type_low_bound. */
8427 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8429 dd = tree_to_double_int (type_low_bound);
8430 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
8432 int c_neg = (!unsc && dc.is_negative ());
8433 int t_neg = (unsc && dd.is_negative ());
8435 if (c_neg && !t_neg)
8436 return false;
8437 if ((c_neg || !t_neg) && dc.ult (dd))
8438 return false;
8440 else if (dc.cmp (dd, unsc) < 0)
8441 return false;
8442 ok_for_low_bound = true;
8444 else
8445 ok_for_low_bound = false;
8447 /* Check if c <= type_high_bound. */
8448 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8450 dd = tree_to_double_int (type_high_bound);
8451 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
8453 int c_neg = (!unsc && dc.is_negative ());
8454 int t_neg = (unsc && dd.is_negative ());
8456 if (t_neg && !c_neg)
8457 return false;
8458 if ((t_neg || !c_neg) && dc.ugt (dd))
8459 return false;
8461 else if (dc.cmp (dd, unsc) > 0)
8462 return false;
8463 ok_for_high_bound = true;
8465 else
8466 ok_for_high_bound = false;
8468 /* If the constant fits both bounds, the result is known. */
8469 if (ok_for_low_bound && ok_for_high_bound)
8470 return true;
8472 /* Perform some generic filtering which may allow making a decision
8473 even if the bounds are not constant. First, negative integers
8474 never fit in unsigned types, */
8475 if (TYPE_UNSIGNED (type) && !unsc && dc.is_negative ())
8476 return false;
8478 /* Second, narrower types always fit in wider ones. */
8479 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8480 return true;
8482 /* Third, unsigned integers with top bit set never fit signed types. */
8483 if (! TYPE_UNSIGNED (type) && unsc)
8485 int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
8486 if (prec < HOST_BITS_PER_WIDE_INT)
8488 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
8489 return false;
8491 else if (((((unsigned HOST_WIDE_INT) 1)
8492 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
8493 return false;
8496 /* If we haven't been able to decide at this point, there nothing more we
8497 can check ourselves here. Look at the base type if we have one and it
8498 has the same precision. */
8499 if (TREE_CODE (type) == INTEGER_TYPE
8500 && TREE_TYPE (type) != 0
8501 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8503 type = TREE_TYPE (type);
8504 goto retry;
8507 /* Or to double_int_fits_to_tree_p, if nothing else. */
8508 return double_int_fits_to_tree_p (type, dc);
8511 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8512 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8513 represented (assuming two's-complement arithmetic) within the bit
8514 precision of the type are returned instead. */
8516 void
8517 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8519 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8520 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8521 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
8522 TYPE_UNSIGNED (type));
8523 else
8525 if (TYPE_UNSIGNED (type))
8526 mpz_set_ui (min, 0);
8527 else
8529 double_int mn;
8530 mn = double_int::mask (TYPE_PRECISION (type) - 1);
8531 mn = (mn + double_int_one).sext (TYPE_PRECISION (type));
8532 mpz_set_double_int (min, mn, false);
8536 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8537 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8538 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
8539 TYPE_UNSIGNED (type));
8540 else
8542 if (TYPE_UNSIGNED (type))
8543 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)),
8544 true);
8545 else
8546 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type) - 1),
8547 true);
8551 /* Return true if VAR is an automatic variable defined in function FN. */
8553 bool
8554 auto_var_in_fn_p (const_tree var, const_tree fn)
8556 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8557 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8558 || TREE_CODE (var) == PARM_DECL)
8559 && ! TREE_STATIC (var))
8560 || TREE_CODE (var) == LABEL_DECL
8561 || TREE_CODE (var) == RESULT_DECL));
8564 /* Subprogram of following function. Called by walk_tree.
8566 Return *TP if it is an automatic variable or parameter of the
8567 function passed in as DATA. */
8569 static tree
8570 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8572 tree fn = (tree) data;
8574 if (TYPE_P (*tp))
8575 *walk_subtrees = 0;
8577 else if (DECL_P (*tp)
8578 && auto_var_in_fn_p (*tp, fn))
8579 return *tp;
8581 return NULL_TREE;
8584 /* Returns true if T is, contains, or refers to a type with variable
8585 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8586 arguments, but not the return type. If FN is nonzero, only return
8587 true if a modifier of the type or position of FN is a variable or
8588 parameter inside FN.
8590 This concept is more general than that of C99 'variably modified types':
8591 in C99, a struct type is never variably modified because a VLA may not
8592 appear as a structure member. However, in GNU C code like:
8594 struct S { int i[f()]; };
8596 is valid, and other languages may define similar constructs. */
8598 bool
8599 variably_modified_type_p (tree type, tree fn)
8601 tree t;
8603 /* Test if T is either variable (if FN is zero) or an expression containing
8604 a variable in FN. If TYPE isn't gimplified, return true also if
8605 gimplify_one_sizepos would gimplify the expression into a local
8606 variable. */
8607 #define RETURN_TRUE_IF_VAR(T) \
8608 do { tree _t = (T); \
8609 if (_t != NULL_TREE \
8610 && _t != error_mark_node \
8611 && TREE_CODE (_t) != INTEGER_CST \
8612 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8613 && (!fn \
8614 || (!TYPE_SIZES_GIMPLIFIED (type) \
8615 && !is_gimple_sizepos (_t)) \
8616 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8617 return true; } while (0)
8619 if (type == error_mark_node)
8620 return false;
8622 /* If TYPE itself has variable size, it is variably modified. */
8623 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8624 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8626 switch (TREE_CODE (type))
8628 case POINTER_TYPE:
8629 case REFERENCE_TYPE:
8630 case VECTOR_TYPE:
8631 if (variably_modified_type_p (TREE_TYPE (type), fn))
8632 return true;
8633 break;
8635 case FUNCTION_TYPE:
8636 case METHOD_TYPE:
8637 /* If TYPE is a function type, it is variably modified if the
8638 return type is variably modified. */
8639 if (variably_modified_type_p (TREE_TYPE (type), fn))
8640 return true;
8641 break;
8643 case INTEGER_TYPE:
8644 case REAL_TYPE:
8645 case FIXED_POINT_TYPE:
8646 case ENUMERAL_TYPE:
8647 case BOOLEAN_TYPE:
8648 /* Scalar types are variably modified if their end points
8649 aren't constant. */
8650 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8651 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8652 break;
8654 case RECORD_TYPE:
8655 case UNION_TYPE:
8656 case QUAL_UNION_TYPE:
8657 /* We can't see if any of the fields are variably-modified by the
8658 definition we normally use, since that would produce infinite
8659 recursion via pointers. */
8660 /* This is variably modified if some field's type is. */
8661 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8662 if (TREE_CODE (t) == FIELD_DECL)
8664 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8665 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8666 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8668 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8669 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8671 break;
8673 case ARRAY_TYPE:
8674 /* Do not call ourselves to avoid infinite recursion. This is
8675 variably modified if the element type is. */
8676 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8677 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8678 break;
8680 default:
8681 break;
8684 /* The current language may have other cases to check, but in general,
8685 all other types are not variably modified. */
8686 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8688 #undef RETURN_TRUE_IF_VAR
8691 /* Given a DECL or TYPE, return the scope in which it was declared, or
8692 NULL_TREE if there is no containing scope. */
8694 tree
8695 get_containing_scope (const_tree t)
8697 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8700 /* Return the innermost context enclosing DECL that is
8701 a FUNCTION_DECL, or zero if none. */
8703 tree
8704 decl_function_context (const_tree decl)
8706 tree context;
8708 if (TREE_CODE (decl) == ERROR_MARK)
8709 return 0;
8711 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8712 where we look up the function at runtime. Such functions always take
8713 a first argument of type 'pointer to real context'.
8715 C++ should really be fixed to use DECL_CONTEXT for the real context,
8716 and use something else for the "virtual context". */
8717 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8718 context
8719 = TYPE_MAIN_VARIANT
8720 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8721 else
8722 context = DECL_CONTEXT (decl);
8724 while (context && TREE_CODE (context) != FUNCTION_DECL)
8726 if (TREE_CODE (context) == BLOCK)
8727 context = BLOCK_SUPERCONTEXT (context);
8728 else
8729 context = get_containing_scope (context);
8732 return context;
8735 /* Return the innermost context enclosing DECL that is
8736 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8737 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8739 tree
8740 decl_type_context (const_tree decl)
8742 tree context = DECL_CONTEXT (decl);
8744 while (context)
8745 switch (TREE_CODE (context))
8747 case NAMESPACE_DECL:
8748 case TRANSLATION_UNIT_DECL:
8749 return NULL_TREE;
8751 case RECORD_TYPE:
8752 case UNION_TYPE:
8753 case QUAL_UNION_TYPE:
8754 return context;
8756 case TYPE_DECL:
8757 case FUNCTION_DECL:
8758 context = DECL_CONTEXT (context);
8759 break;
8761 case BLOCK:
8762 context = BLOCK_SUPERCONTEXT (context);
8763 break;
8765 default:
8766 gcc_unreachable ();
8769 return NULL_TREE;
8772 /* CALL is a CALL_EXPR. Return the declaration for the function
8773 called, or NULL_TREE if the called function cannot be
8774 determined. */
8776 tree
8777 get_callee_fndecl (const_tree call)
8779 tree addr;
8781 if (call == error_mark_node)
8782 return error_mark_node;
8784 /* It's invalid to call this function with anything but a
8785 CALL_EXPR. */
8786 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8788 /* The first operand to the CALL is the address of the function
8789 called. */
8790 addr = CALL_EXPR_FN (call);
8792 STRIP_NOPS (addr);
8794 /* If this is a readonly function pointer, extract its initial value. */
8795 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8796 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8797 && DECL_INITIAL (addr))
8798 addr = DECL_INITIAL (addr);
8800 /* If the address is just `&f' for some function `f', then we know
8801 that `f' is being called. */
8802 if (TREE_CODE (addr) == ADDR_EXPR
8803 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8804 return TREE_OPERAND (addr, 0);
8806 /* We couldn't figure out what was being called. */
8807 return NULL_TREE;
8810 /* Print debugging information about tree nodes generated during the compile,
8811 and any language-specific information. */
8813 void
8814 dump_tree_statistics (void)
8816 if (GATHER_STATISTICS)
8818 int i;
8819 int total_nodes, total_bytes;
8820 fprintf (stderr, "Kind Nodes Bytes\n");
8821 fprintf (stderr, "---------------------------------------\n");
8822 total_nodes = total_bytes = 0;
8823 for (i = 0; i < (int) all_kinds; i++)
8825 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8826 tree_node_counts[i], tree_node_sizes[i]);
8827 total_nodes += tree_node_counts[i];
8828 total_bytes += tree_node_sizes[i];
8830 fprintf (stderr, "---------------------------------------\n");
8831 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8832 fprintf (stderr, "---------------------------------------\n");
8833 fprintf (stderr, "Code Nodes\n");
8834 fprintf (stderr, "----------------------------\n");
8835 for (i = 0; i < (int) MAX_TREE_CODES; i++)
8836 fprintf (stderr, "%-20s %7d\n", tree_code_name[i], tree_code_counts[i]);
8837 fprintf (stderr, "----------------------------\n");
8838 ssanames_print_statistics ();
8839 phinodes_print_statistics ();
8841 else
8842 fprintf (stderr, "(No per-node statistics)\n");
8844 print_type_hash_statistics ();
8845 print_debug_expr_statistics ();
8846 print_value_expr_statistics ();
8847 lang_hooks.print_statistics ();
8850 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8852 /* Generate a crc32 of a byte. */
8854 static unsigned
8855 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
8857 unsigned ix;
8859 for (ix = bits; ix--; value <<= 1)
8861 unsigned feedback;
8863 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
8864 chksum <<= 1;
8865 chksum ^= feedback;
8867 return chksum;
8870 /* Generate a crc32 of a 32-bit unsigned. */
8872 unsigned
8873 crc32_unsigned (unsigned chksum, unsigned value)
8875 return crc32_unsigned_bits (chksum, value, 32);
8878 /* Generate a crc32 of a byte. */
8880 unsigned
8881 crc32_byte (unsigned chksum, char byte)
8883 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
8886 /* Generate a crc32 of a string. */
8888 unsigned
8889 crc32_string (unsigned chksum, const char *string)
8893 chksum = crc32_byte (chksum, *string);
8895 while (*string++);
8896 return chksum;
8899 /* P is a string that will be used in a symbol. Mask out any characters
8900 that are not valid in that context. */
8902 void
8903 clean_symbol_name (char *p)
8905 for (; *p; p++)
8906 if (! (ISALNUM (*p)
8907 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
8908 || *p == '$'
8909 #endif
8910 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
8911 || *p == '.'
8912 #endif
8914 *p = '_';
8917 /* Generate a name for a special-purpose function.
8918 The generated name may need to be unique across the whole link.
8919 Changes to this function may also require corresponding changes to
8920 xstrdup_mask_random.
8921 TYPE is some string to identify the purpose of this function to the
8922 linker or collect2; it must start with an uppercase letter,
8923 one of:
8924 I - for constructors
8925 D - for destructors
8926 N - for C++ anonymous namespaces
8927 F - for DWARF unwind frame information. */
8929 tree
8930 get_file_function_name (const char *type)
8932 char *buf;
8933 const char *p;
8934 char *q;
8936 /* If we already have a name we know to be unique, just use that. */
8937 if (first_global_object_name)
8938 p = q = ASTRDUP (first_global_object_name);
8939 /* If the target is handling the constructors/destructors, they
8940 will be local to this file and the name is only necessary for
8941 debugging purposes.
8942 We also assign sub_I and sub_D sufixes to constructors called from
8943 the global static constructors. These are always local. */
8944 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
8945 || (strncmp (type, "sub_", 4) == 0
8946 && (type[4] == 'I' || type[4] == 'D')))
8948 const char *file = main_input_filename;
8949 if (! file)
8950 file = input_filename;
8951 /* Just use the file's basename, because the full pathname
8952 might be quite long. */
8953 p = q = ASTRDUP (lbasename (file));
8955 else
8957 /* Otherwise, the name must be unique across the entire link.
8958 We don't have anything that we know to be unique to this translation
8959 unit, so use what we do have and throw in some randomness. */
8960 unsigned len;
8961 const char *name = weak_global_object_name;
8962 const char *file = main_input_filename;
8964 if (! name)
8965 name = "";
8966 if (! file)
8967 file = input_filename;
8969 len = strlen (file);
8970 q = (char *) alloca (9 + 17 + len + 1);
8971 memcpy (q, file, len + 1);
8973 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
8974 crc32_string (0, name), get_random_seed (false));
8976 p = q;
8979 clean_symbol_name (q);
8980 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
8981 + strlen (type));
8983 /* Set up the name of the file-level functions we may need.
8984 Use a global object (which is already required to be unique over
8985 the program) rather than the file name (which imposes extra
8986 constraints). */
8987 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
8989 return get_identifier (buf);
8992 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
8994 /* Complain that the tree code of NODE does not match the expected 0
8995 terminated list of trailing codes. The trailing code list can be
8996 empty, for a more vague error message. FILE, LINE, and FUNCTION
8997 are of the caller. */
8999 void
9000 tree_check_failed (const_tree node, const char *file,
9001 int line, const char *function, ...)
9003 va_list args;
9004 const char *buffer;
9005 unsigned length = 0;
9006 int code;
9008 va_start (args, function);
9009 while ((code = va_arg (args, int)))
9010 length += 4 + strlen (tree_code_name[code]);
9011 va_end (args);
9012 if (length)
9014 char *tmp;
9015 va_start (args, function);
9016 length += strlen ("expected ");
9017 buffer = tmp = (char *) alloca (length);
9018 length = 0;
9019 while ((code = va_arg (args, int)))
9021 const char *prefix = length ? " or " : "expected ";
9023 strcpy (tmp + length, prefix);
9024 length += strlen (prefix);
9025 strcpy (tmp + length, tree_code_name[code]);
9026 length += strlen (tree_code_name[code]);
9028 va_end (args);
9030 else
9031 buffer = "unexpected node";
9033 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9034 buffer, tree_code_name[TREE_CODE (node)],
9035 function, trim_filename (file), line);
9038 /* Complain that the tree code of NODE does match the expected 0
9039 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9040 the caller. */
9042 void
9043 tree_not_check_failed (const_tree node, const char *file,
9044 int line, const char *function, ...)
9046 va_list args;
9047 char *buffer;
9048 unsigned length = 0;
9049 int code;
9051 va_start (args, function);
9052 while ((code = va_arg (args, int)))
9053 length += 4 + strlen (tree_code_name[code]);
9054 va_end (args);
9055 va_start (args, function);
9056 buffer = (char *) alloca (length);
9057 length = 0;
9058 while ((code = va_arg (args, int)))
9060 if (length)
9062 strcpy (buffer + length, " or ");
9063 length += 4;
9065 strcpy (buffer + length, tree_code_name[code]);
9066 length += strlen (tree_code_name[code]);
9068 va_end (args);
9070 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9071 buffer, tree_code_name[TREE_CODE (node)],
9072 function, trim_filename (file), line);
9075 /* Similar to tree_check_failed, except that we check for a class of tree
9076 code, given in CL. */
9078 void
9079 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9080 const char *file, int line, const char *function)
9082 internal_error
9083 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9084 TREE_CODE_CLASS_STRING (cl),
9085 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9086 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9089 /* Similar to tree_check_failed, except that instead of specifying a
9090 dozen codes, use the knowledge that they're all sequential. */
9092 void
9093 tree_range_check_failed (const_tree node, const char *file, int line,
9094 const char *function, enum tree_code c1,
9095 enum tree_code c2)
9097 char *buffer;
9098 unsigned length = 0;
9099 unsigned int c;
9101 for (c = c1; c <= c2; ++c)
9102 length += 4 + strlen (tree_code_name[c]);
9104 length += strlen ("expected ");
9105 buffer = (char *) alloca (length);
9106 length = 0;
9108 for (c = c1; c <= c2; ++c)
9110 const char *prefix = length ? " or " : "expected ";
9112 strcpy (buffer + length, prefix);
9113 length += strlen (prefix);
9114 strcpy (buffer + length, tree_code_name[c]);
9115 length += strlen (tree_code_name[c]);
9118 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9119 buffer, tree_code_name[TREE_CODE (node)],
9120 function, trim_filename (file), line);
9124 /* Similar to tree_check_failed, except that we check that a tree does
9125 not have the specified code, given in CL. */
9127 void
9128 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9129 const char *file, int line, const char *function)
9131 internal_error
9132 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9133 TREE_CODE_CLASS_STRING (cl),
9134 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9135 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9139 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9141 void
9142 omp_clause_check_failed (const_tree node, const char *file, int line,
9143 const char *function, enum omp_clause_code code)
9145 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9146 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
9147 function, trim_filename (file), line);
9151 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9153 void
9154 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9155 const char *function, enum omp_clause_code c1,
9156 enum omp_clause_code c2)
9158 char *buffer;
9159 unsigned length = 0;
9160 unsigned int c;
9162 for (c = c1; c <= c2; ++c)
9163 length += 4 + strlen (omp_clause_code_name[c]);
9165 length += strlen ("expected ");
9166 buffer = (char *) alloca (length);
9167 length = 0;
9169 for (c = c1; c <= c2; ++c)
9171 const char *prefix = length ? " or " : "expected ";
9173 strcpy (buffer + length, prefix);
9174 length += strlen (prefix);
9175 strcpy (buffer + length, omp_clause_code_name[c]);
9176 length += strlen (omp_clause_code_name[c]);
9179 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9180 buffer, omp_clause_code_name[TREE_CODE (node)],
9181 function, trim_filename (file), line);
9185 #undef DEFTREESTRUCT
9186 #define DEFTREESTRUCT(VAL, NAME) NAME,
9188 static const char *ts_enum_names[] = {
9189 #include "treestruct.def"
9191 #undef DEFTREESTRUCT
9193 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9195 /* Similar to tree_class_check_failed, except that we check for
9196 whether CODE contains the tree structure identified by EN. */
9198 void
9199 tree_contains_struct_check_failed (const_tree node,
9200 const enum tree_node_structure_enum en,
9201 const char *file, int line,
9202 const char *function)
9204 internal_error
9205 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9206 TS_ENUM_NAME(en),
9207 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9211 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9212 (dynamically sized) vector. */
9214 void
9215 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9216 const char *function)
9218 internal_error
9219 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9220 idx + 1, len, function, trim_filename (file), line);
9223 /* Similar to above, except that the check is for the bounds of the operand
9224 vector of an expression node EXP. */
9226 void
9227 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9228 int line, const char *function)
9230 int code = TREE_CODE (exp);
9231 internal_error
9232 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9233 idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
9234 function, trim_filename (file), line);
9237 /* Similar to above, except that the check is for the number of
9238 operands of an OMP_CLAUSE node. */
9240 void
9241 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9242 int line, const char *function)
9244 internal_error
9245 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9246 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9247 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9248 trim_filename (file), line);
9250 #endif /* ENABLE_TREE_CHECKING */
9252 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9253 and mapped to the machine mode MODE. Initialize its fields and build
9254 the information necessary for debugging output. */
9256 static tree
9257 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9259 tree t;
9260 hashval_t hashcode = 0;
9262 t = make_node (VECTOR_TYPE);
9263 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9264 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9265 SET_TYPE_MODE (t, mode);
9267 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9268 SET_TYPE_STRUCTURAL_EQUALITY (t);
9269 else if (TYPE_CANONICAL (innertype) != innertype
9270 || mode != VOIDmode)
9271 TYPE_CANONICAL (t)
9272 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9274 layout_type (t);
9276 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9277 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9278 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9279 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9280 t = type_hash_canon (hashcode, t);
9282 /* We have built a main variant, based on the main variant of the
9283 inner type. Use it to build the variant we return. */
9284 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9285 && TREE_TYPE (t) != innertype)
9286 return build_type_attribute_qual_variant (t,
9287 TYPE_ATTRIBUTES (innertype),
9288 TYPE_QUALS (innertype));
9290 return t;
9293 static tree
9294 make_or_reuse_type (unsigned size, int unsignedp)
9296 if (size == INT_TYPE_SIZE)
9297 return unsignedp ? unsigned_type_node : integer_type_node;
9298 if (size == CHAR_TYPE_SIZE)
9299 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9300 if (size == SHORT_TYPE_SIZE)
9301 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9302 if (size == LONG_TYPE_SIZE)
9303 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9304 if (size == LONG_LONG_TYPE_SIZE)
9305 return (unsignedp ? long_long_unsigned_type_node
9306 : long_long_integer_type_node);
9307 if (size == 128 && int128_integer_type_node)
9308 return (unsignedp ? int128_unsigned_type_node
9309 : int128_integer_type_node);
9311 if (unsignedp)
9312 return make_unsigned_type (size);
9313 else
9314 return make_signed_type (size);
9317 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9319 static tree
9320 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9322 if (satp)
9324 if (size == SHORT_FRACT_TYPE_SIZE)
9325 return unsignedp ? sat_unsigned_short_fract_type_node
9326 : sat_short_fract_type_node;
9327 if (size == FRACT_TYPE_SIZE)
9328 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9329 if (size == LONG_FRACT_TYPE_SIZE)
9330 return unsignedp ? sat_unsigned_long_fract_type_node
9331 : sat_long_fract_type_node;
9332 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9333 return unsignedp ? sat_unsigned_long_long_fract_type_node
9334 : sat_long_long_fract_type_node;
9336 else
9338 if (size == SHORT_FRACT_TYPE_SIZE)
9339 return unsignedp ? unsigned_short_fract_type_node
9340 : short_fract_type_node;
9341 if (size == FRACT_TYPE_SIZE)
9342 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9343 if (size == LONG_FRACT_TYPE_SIZE)
9344 return unsignedp ? unsigned_long_fract_type_node
9345 : long_fract_type_node;
9346 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9347 return unsignedp ? unsigned_long_long_fract_type_node
9348 : long_long_fract_type_node;
9351 return make_fract_type (size, unsignedp, satp);
9354 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9356 static tree
9357 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9359 if (satp)
9361 if (size == SHORT_ACCUM_TYPE_SIZE)
9362 return unsignedp ? sat_unsigned_short_accum_type_node
9363 : sat_short_accum_type_node;
9364 if (size == ACCUM_TYPE_SIZE)
9365 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9366 if (size == LONG_ACCUM_TYPE_SIZE)
9367 return unsignedp ? sat_unsigned_long_accum_type_node
9368 : sat_long_accum_type_node;
9369 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9370 return unsignedp ? sat_unsigned_long_long_accum_type_node
9371 : sat_long_long_accum_type_node;
9373 else
9375 if (size == SHORT_ACCUM_TYPE_SIZE)
9376 return unsignedp ? unsigned_short_accum_type_node
9377 : short_accum_type_node;
9378 if (size == ACCUM_TYPE_SIZE)
9379 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9380 if (size == LONG_ACCUM_TYPE_SIZE)
9381 return unsignedp ? unsigned_long_accum_type_node
9382 : long_accum_type_node;
9383 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9384 return unsignedp ? unsigned_long_long_accum_type_node
9385 : long_long_accum_type_node;
9388 return make_accum_type (size, unsignedp, satp);
9391 /* Create nodes for all integer types (and error_mark_node) using the sizes
9392 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9393 SHORT_DOUBLE specifies whether double should be of the same precision
9394 as float. */
9396 void
9397 build_common_tree_nodes (bool signed_char, bool short_double)
9399 error_mark_node = make_node (ERROR_MARK);
9400 TREE_TYPE (error_mark_node) = error_mark_node;
9402 initialize_sizetypes ();
9404 /* Define both `signed char' and `unsigned char'. */
9405 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9406 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9407 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9408 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9410 /* Define `char', which is like either `signed char' or `unsigned char'
9411 but not the same as either. */
9412 char_type_node
9413 = (signed_char
9414 ? make_signed_type (CHAR_TYPE_SIZE)
9415 : make_unsigned_type (CHAR_TYPE_SIZE));
9416 TYPE_STRING_FLAG (char_type_node) = 1;
9418 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9419 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9420 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9421 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9422 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9423 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9424 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9425 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9426 #if HOST_BITS_PER_WIDE_INT >= 64
9427 /* TODO: This isn't correct, but as logic depends at the moment on
9428 host's instead of target's wide-integer.
9429 If there is a target not supporting TImode, but has an 128-bit
9430 integer-scalar register, this target check needs to be adjusted. */
9431 if (targetm.scalar_mode_supported_p (TImode))
9433 int128_integer_type_node = make_signed_type (128);
9434 int128_unsigned_type_node = make_unsigned_type (128);
9436 #endif
9438 /* Define a boolean type. This type only represents boolean values but
9439 may be larger than char depending on the value of BOOL_TYPE_SIZE.
9440 Front ends which want to override this size (i.e. Java) can redefine
9441 boolean_type_node before calling build_common_tree_nodes_2. */
9442 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9443 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9444 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9445 TYPE_PRECISION (boolean_type_node) = 1;
9447 /* Define what type to use for size_t. */
9448 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9449 size_type_node = unsigned_type_node;
9450 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9451 size_type_node = long_unsigned_type_node;
9452 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9453 size_type_node = long_long_unsigned_type_node;
9454 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9455 size_type_node = short_unsigned_type_node;
9456 else
9457 gcc_unreachable ();
9459 /* Fill in the rest of the sized types. Reuse existing type nodes
9460 when possible. */
9461 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9462 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9463 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9464 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9465 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9467 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9468 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9469 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9470 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9471 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9473 access_public_node = get_identifier ("public");
9474 access_protected_node = get_identifier ("protected");
9475 access_private_node = get_identifier ("private");
9477 /* Define these next since types below may used them. */
9478 integer_zero_node = build_int_cst (integer_type_node, 0);
9479 integer_one_node = build_int_cst (integer_type_node, 1);
9480 integer_three_node = build_int_cst (integer_type_node, 3);
9481 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9483 size_zero_node = size_int (0);
9484 size_one_node = size_int (1);
9485 bitsize_zero_node = bitsize_int (0);
9486 bitsize_one_node = bitsize_int (1);
9487 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9489 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9490 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9492 void_type_node = make_node (VOID_TYPE);
9493 layout_type (void_type_node);
9495 /* We are not going to have real types in C with less than byte alignment,
9496 so we might as well not have any types that claim to have it. */
9497 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9498 TYPE_USER_ALIGN (void_type_node) = 0;
9500 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9501 layout_type (TREE_TYPE (null_pointer_node));
9503 ptr_type_node = build_pointer_type (void_type_node);
9504 const_ptr_type_node
9505 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9506 fileptr_type_node = ptr_type_node;
9508 float_type_node = make_node (REAL_TYPE);
9509 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9510 layout_type (float_type_node);
9512 double_type_node = make_node (REAL_TYPE);
9513 if (short_double)
9514 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9515 else
9516 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9517 layout_type (double_type_node);
9519 long_double_type_node = make_node (REAL_TYPE);
9520 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9521 layout_type (long_double_type_node);
9523 float_ptr_type_node = build_pointer_type (float_type_node);
9524 double_ptr_type_node = build_pointer_type (double_type_node);
9525 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9526 integer_ptr_type_node = build_pointer_type (integer_type_node);
9528 /* Fixed size integer types. */
9529 uint16_type_node = build_nonstandard_integer_type (16, true);
9530 uint32_type_node = build_nonstandard_integer_type (32, true);
9531 uint64_type_node = build_nonstandard_integer_type (64, true);
9533 /* Decimal float types. */
9534 dfloat32_type_node = make_node (REAL_TYPE);
9535 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9536 layout_type (dfloat32_type_node);
9537 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9538 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9540 dfloat64_type_node = make_node (REAL_TYPE);
9541 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9542 layout_type (dfloat64_type_node);
9543 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9544 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9546 dfloat128_type_node = make_node (REAL_TYPE);
9547 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9548 layout_type (dfloat128_type_node);
9549 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9550 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9552 complex_integer_type_node = build_complex_type (integer_type_node);
9553 complex_float_type_node = build_complex_type (float_type_node);
9554 complex_double_type_node = build_complex_type (double_type_node);
9555 complex_long_double_type_node = build_complex_type (long_double_type_node);
9557 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9558 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9559 sat_ ## KIND ## _type_node = \
9560 make_sat_signed_ ## KIND ## _type (SIZE); \
9561 sat_unsigned_ ## KIND ## _type_node = \
9562 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9563 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9564 unsigned_ ## KIND ## _type_node = \
9565 make_unsigned_ ## KIND ## _type (SIZE);
9567 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9568 sat_ ## WIDTH ## KIND ## _type_node = \
9569 make_sat_signed_ ## KIND ## _type (SIZE); \
9570 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9571 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9572 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9573 unsigned_ ## WIDTH ## KIND ## _type_node = \
9574 make_unsigned_ ## KIND ## _type (SIZE);
9576 /* Make fixed-point type nodes based on four different widths. */
9577 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9578 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9579 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9580 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9581 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9583 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9584 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9585 NAME ## _type_node = \
9586 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9587 u ## NAME ## _type_node = \
9588 make_or_reuse_unsigned_ ## KIND ## _type \
9589 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9590 sat_ ## NAME ## _type_node = \
9591 make_or_reuse_sat_signed_ ## KIND ## _type \
9592 (GET_MODE_BITSIZE (MODE ## mode)); \
9593 sat_u ## NAME ## _type_node = \
9594 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9595 (GET_MODE_BITSIZE (U ## MODE ## mode));
9597 /* Fixed-point type and mode nodes. */
9598 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9599 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9600 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9601 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9602 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9603 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9604 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9605 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9606 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9607 MAKE_FIXED_MODE_NODE (accum, da, DA)
9608 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9611 tree t = targetm.build_builtin_va_list ();
9613 /* Many back-ends define record types without setting TYPE_NAME.
9614 If we copied the record type here, we'd keep the original
9615 record type without a name. This breaks name mangling. So,
9616 don't copy record types and let c_common_nodes_and_builtins()
9617 declare the type to be __builtin_va_list. */
9618 if (TREE_CODE (t) != RECORD_TYPE)
9619 t = build_variant_type_copy (t);
9621 va_list_type_node = t;
9625 /* Modify DECL for given flags. */
9626 void
9627 set_call_expr_flags (tree decl, int flags)
9629 if (flags & ECF_NOTHROW)
9630 TREE_NOTHROW (decl) = 1;
9631 if (flags & ECF_CONST)
9632 TREE_READONLY (decl) = 1;
9633 if (flags & ECF_PURE)
9634 DECL_PURE_P (decl) = 1;
9635 if (flags & ECF_LOOPING_CONST_OR_PURE)
9636 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9637 if (flags & ECF_NOVOPS)
9638 DECL_IS_NOVOPS (decl) = 1;
9639 if (flags & ECF_NORETURN)
9640 TREE_THIS_VOLATILE (decl) = 1;
9641 if (flags & ECF_MALLOC)
9642 DECL_IS_MALLOC (decl) = 1;
9643 if (flags & ECF_RETURNS_TWICE)
9644 DECL_IS_RETURNS_TWICE (decl) = 1;
9645 if (flags & ECF_LEAF)
9646 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9647 NULL, DECL_ATTRIBUTES (decl));
9648 if ((flags & ECF_TM_PURE) && flag_tm)
9649 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("transaction_pure"),
9650 NULL, DECL_ATTRIBUTES (decl));
9651 /* Looping const or pure is implied by noreturn.
9652 There is currently no way to declare looping const or looping pure alone. */
9653 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9654 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9658 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9660 static void
9661 local_define_builtin (const char *name, tree type, enum built_in_function code,
9662 const char *library_name, int ecf_flags)
9664 tree decl;
9666 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9667 library_name, NULL_TREE);
9668 set_call_expr_flags (decl, ecf_flags);
9670 set_builtin_decl (code, decl, true);
9673 /* Call this function after instantiating all builtins that the language
9674 front end cares about. This will build the rest of the builtins that
9675 are relied upon by the tree optimizers and the middle-end. */
9677 void
9678 build_common_builtin_nodes (void)
9680 tree tmp, ftype;
9681 int ecf_flags;
9683 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9685 ftype = build_function_type (void_type_node, void_list_node);
9686 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9687 "__builtin_unreachable",
9688 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9689 | ECF_CONST | ECF_LEAF);
9692 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9693 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9695 ftype = build_function_type_list (ptr_type_node,
9696 ptr_type_node, const_ptr_type_node,
9697 size_type_node, NULL_TREE);
9699 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9700 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9701 "memcpy", ECF_NOTHROW | ECF_LEAF);
9702 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9703 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9704 "memmove", ECF_NOTHROW | ECF_LEAF);
9707 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9709 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9710 const_ptr_type_node, size_type_node,
9711 NULL_TREE);
9712 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9713 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9716 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9718 ftype = build_function_type_list (ptr_type_node,
9719 ptr_type_node, integer_type_node,
9720 size_type_node, NULL_TREE);
9721 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9722 "memset", ECF_NOTHROW | ECF_LEAF);
9725 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9727 ftype = build_function_type_list (ptr_type_node,
9728 size_type_node, NULL_TREE);
9729 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9730 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9733 ftype = build_function_type_list (ptr_type_node, size_type_node,
9734 size_type_node, NULL_TREE);
9735 local_define_builtin ("__builtin_alloca_with_align", ftype,
9736 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
9737 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9739 /* If we're checking the stack, `alloca' can throw. */
9740 if (flag_stack_check)
9742 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
9743 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
9746 ftype = build_function_type_list (void_type_node,
9747 ptr_type_node, ptr_type_node,
9748 ptr_type_node, NULL_TREE);
9749 local_define_builtin ("__builtin_init_trampoline", ftype,
9750 BUILT_IN_INIT_TRAMPOLINE,
9751 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9752 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9753 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9754 "__builtin_init_heap_trampoline",
9755 ECF_NOTHROW | ECF_LEAF);
9757 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9758 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9759 BUILT_IN_ADJUST_TRAMPOLINE,
9760 "__builtin_adjust_trampoline",
9761 ECF_CONST | ECF_NOTHROW);
9763 ftype = build_function_type_list (void_type_node,
9764 ptr_type_node, ptr_type_node, NULL_TREE);
9765 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9766 BUILT_IN_NONLOCAL_GOTO,
9767 "__builtin_nonlocal_goto",
9768 ECF_NORETURN | ECF_NOTHROW);
9770 ftype = build_function_type_list (void_type_node,
9771 ptr_type_node, ptr_type_node, NULL_TREE);
9772 local_define_builtin ("__builtin_setjmp_setup", ftype,
9773 BUILT_IN_SETJMP_SETUP,
9774 "__builtin_setjmp_setup", ECF_NOTHROW);
9776 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9777 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
9778 BUILT_IN_SETJMP_DISPATCHER,
9779 "__builtin_setjmp_dispatcher",
9780 ECF_PURE | ECF_NOTHROW);
9782 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9783 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9784 BUILT_IN_SETJMP_RECEIVER,
9785 "__builtin_setjmp_receiver", ECF_NOTHROW);
9787 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9788 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9789 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9791 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9792 local_define_builtin ("__builtin_stack_restore", ftype,
9793 BUILT_IN_STACK_RESTORE,
9794 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9796 /* If there's a possibility that we might use the ARM EABI, build the
9797 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
9798 if (targetm.arm_eabi_unwinder)
9800 ftype = build_function_type_list (void_type_node, NULL_TREE);
9801 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9802 BUILT_IN_CXA_END_CLEANUP,
9803 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9806 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9807 local_define_builtin ("__builtin_unwind_resume", ftype,
9808 BUILT_IN_UNWIND_RESUME,
9809 ((targetm_common.except_unwind_info (&global_options)
9810 == UI_SJLJ)
9811 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9812 ECF_NORETURN);
9814 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
9816 ftype = build_function_type_list (ptr_type_node, integer_type_node,
9817 NULL_TREE);
9818 local_define_builtin ("__builtin_return_address", ftype,
9819 BUILT_IN_RETURN_ADDRESS,
9820 "__builtin_return_address",
9821 ECF_NOTHROW);
9824 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
9825 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9827 ftype = build_function_type_list (void_type_node, ptr_type_node,
9828 ptr_type_node, NULL_TREE);
9829 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
9830 local_define_builtin ("__cyg_profile_func_enter", ftype,
9831 BUILT_IN_PROFILE_FUNC_ENTER,
9832 "__cyg_profile_func_enter", 0);
9833 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9834 local_define_builtin ("__cyg_profile_func_exit", ftype,
9835 BUILT_IN_PROFILE_FUNC_EXIT,
9836 "__cyg_profile_func_exit", 0);
9839 /* The exception object and filter values from the runtime. The argument
9840 must be zero before exception lowering, i.e. from the front end. After
9841 exception lowering, it will be the region number for the exception
9842 landing pad. These functions are PURE instead of CONST to prevent
9843 them from being hoisted past the exception edge that will initialize
9844 its value in the landing pad. */
9845 ftype = build_function_type_list (ptr_type_node,
9846 integer_type_node, NULL_TREE);
9847 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
9848 /* Only use TM_PURE if we we have TM language support. */
9849 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
9850 ecf_flags |= ECF_TM_PURE;
9851 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
9852 "__builtin_eh_pointer", ecf_flags);
9854 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
9855 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
9856 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
9857 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9859 ftype = build_function_type_list (void_type_node,
9860 integer_type_node, integer_type_node,
9861 NULL_TREE);
9862 local_define_builtin ("__builtin_eh_copy_values", ftype,
9863 BUILT_IN_EH_COPY_VALUES,
9864 "__builtin_eh_copy_values", ECF_NOTHROW);
9866 /* Complex multiplication and division. These are handled as builtins
9867 rather than optabs because emit_library_call_value doesn't support
9868 complex. Further, we can do slightly better with folding these
9869 beasties if the real and complex parts of the arguments are separate. */
9871 int mode;
9873 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
9875 char mode_name_buf[4], *q;
9876 const char *p;
9877 enum built_in_function mcode, dcode;
9878 tree type, inner_type;
9879 const char *prefix = "__";
9881 if (targetm.libfunc_gnu_prefix)
9882 prefix = "__gnu_";
9884 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
9885 if (type == NULL)
9886 continue;
9887 inner_type = TREE_TYPE (type);
9889 ftype = build_function_type_list (type, inner_type, inner_type,
9890 inner_type, inner_type, NULL_TREE);
9892 mcode = ((enum built_in_function)
9893 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9894 dcode = ((enum built_in_function)
9895 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9897 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
9898 *q = TOLOWER (*p);
9899 *q = '\0';
9901 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
9902 NULL);
9903 local_define_builtin (built_in_names[mcode], ftype, mcode,
9904 built_in_names[mcode],
9905 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9907 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
9908 NULL);
9909 local_define_builtin (built_in_names[dcode], ftype, dcode,
9910 built_in_names[dcode],
9911 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9916 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
9917 better way.
9919 If we requested a pointer to a vector, build up the pointers that
9920 we stripped off while looking for the inner type. Similarly for
9921 return values from functions.
9923 The argument TYPE is the top of the chain, and BOTTOM is the
9924 new type which we will point to. */
9926 tree
9927 reconstruct_complex_type (tree type, tree bottom)
9929 tree inner, outer;
9931 if (TREE_CODE (type) == POINTER_TYPE)
9933 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9934 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
9935 TYPE_REF_CAN_ALIAS_ALL (type));
9937 else if (TREE_CODE (type) == REFERENCE_TYPE)
9939 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9940 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
9941 TYPE_REF_CAN_ALIAS_ALL (type));
9943 else if (TREE_CODE (type) == ARRAY_TYPE)
9945 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9946 outer = build_array_type (inner, TYPE_DOMAIN (type));
9948 else if (TREE_CODE (type) == FUNCTION_TYPE)
9950 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9951 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
9953 else if (TREE_CODE (type) == METHOD_TYPE)
9955 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9956 /* The build_method_type_directly() routine prepends 'this' to argument list,
9957 so we must compensate by getting rid of it. */
9958 outer
9959 = build_method_type_directly
9960 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
9961 inner,
9962 TREE_CHAIN (TYPE_ARG_TYPES (type)));
9964 else if (TREE_CODE (type) == OFFSET_TYPE)
9966 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9967 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
9969 else
9970 return bottom;
9972 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
9973 TYPE_QUALS (type));
9976 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
9977 the inner type. */
9978 tree
9979 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
9981 int nunits;
9983 switch (GET_MODE_CLASS (mode))
9985 case MODE_VECTOR_INT:
9986 case MODE_VECTOR_FLOAT:
9987 case MODE_VECTOR_FRACT:
9988 case MODE_VECTOR_UFRACT:
9989 case MODE_VECTOR_ACCUM:
9990 case MODE_VECTOR_UACCUM:
9991 nunits = GET_MODE_NUNITS (mode);
9992 break;
9994 case MODE_INT:
9995 /* Check that there are no leftover bits. */
9996 gcc_assert (GET_MODE_BITSIZE (mode)
9997 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
9999 nunits = GET_MODE_BITSIZE (mode)
10000 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10001 break;
10003 default:
10004 gcc_unreachable ();
10007 return make_vector_type (innertype, nunits, mode);
10010 /* Similarly, but takes the inner type and number of units, which must be
10011 a power of two. */
10013 tree
10014 build_vector_type (tree innertype, int nunits)
10016 return make_vector_type (innertype, nunits, VOIDmode);
10019 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10021 tree
10022 build_opaque_vector_type (tree innertype, int nunits)
10024 tree t = make_vector_type (innertype, nunits, VOIDmode);
10025 tree cand;
10026 /* We always build the non-opaque variant before the opaque one,
10027 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10028 cand = TYPE_NEXT_VARIANT (t);
10029 if (cand
10030 && TYPE_VECTOR_OPAQUE (cand)
10031 && check_qualified_type (cand, t, TYPE_QUALS (t), NULL_TREE))
10032 return cand;
10033 /* Othewise build a variant type and make sure to queue it after
10034 the non-opaque type. */
10035 cand = build_distinct_type_copy (t);
10036 TYPE_VECTOR_OPAQUE (cand) = true;
10037 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10038 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10039 TYPE_NEXT_VARIANT (t) = cand;
10040 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10041 return cand;
10045 /* Given an initializer INIT, return TRUE if INIT is zero or some
10046 aggregate of zeros. Otherwise return FALSE. */
10047 bool
10048 initializer_zerop (const_tree init)
10050 tree elt;
10052 STRIP_NOPS (init);
10054 switch (TREE_CODE (init))
10056 case INTEGER_CST:
10057 return integer_zerop (init);
10059 case REAL_CST:
10060 /* ??? Note that this is not correct for C4X float formats. There,
10061 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10062 negative exponent. */
10063 return real_zerop (init)
10064 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10066 case FIXED_CST:
10067 return fixed_zerop (init);
10069 case COMPLEX_CST:
10070 return integer_zerop (init)
10071 || (real_zerop (init)
10072 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10073 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10075 case VECTOR_CST:
10077 unsigned i;
10078 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10079 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10080 return false;
10081 return true;
10084 case CONSTRUCTOR:
10086 unsigned HOST_WIDE_INT idx;
10088 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10089 if (!initializer_zerop (elt))
10090 return false;
10091 return true;
10094 case STRING_CST:
10096 int i;
10098 /* We need to loop through all elements to handle cases like
10099 "\0" and "\0foobar". */
10100 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10101 if (TREE_STRING_POINTER (init)[i] != '\0')
10102 return false;
10104 return true;
10107 default:
10108 return false;
10112 /* Build an empty statement at location LOC. */
10114 tree
10115 build_empty_stmt (location_t loc)
10117 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10118 SET_EXPR_LOCATION (t, loc);
10119 return t;
10123 /* Build an OpenMP clause with code CODE. LOC is the location of the
10124 clause. */
10126 tree
10127 build_omp_clause (location_t loc, enum omp_clause_code code)
10129 tree t;
10130 int size, length;
10132 length = omp_clause_num_ops[code];
10133 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10135 record_node_allocation_statistics (OMP_CLAUSE, size);
10137 t = ggc_alloc_tree_node (size);
10138 memset (t, 0, size);
10139 TREE_SET_CODE (t, OMP_CLAUSE);
10140 OMP_CLAUSE_SET_CODE (t, code);
10141 OMP_CLAUSE_LOCATION (t) = loc;
10143 return t;
10146 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10147 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10148 Except for the CODE and operand count field, other storage for the
10149 object is initialized to zeros. */
10151 tree
10152 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10154 tree t;
10155 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10157 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10158 gcc_assert (len >= 1);
10160 record_node_allocation_statistics (code, length);
10162 t = ggc_alloc_zone_cleared_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
10164 TREE_SET_CODE (t, code);
10166 /* Can't use TREE_OPERAND to store the length because if checking is
10167 enabled, it will try to check the length before we store it. :-P */
10168 t->exp.operands[0] = build_int_cst (sizetype, len);
10170 return t;
10173 /* Helper function for build_call_* functions; build a CALL_EXPR with
10174 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10175 the argument slots. */
10177 static tree
10178 build_call_1 (tree return_type, tree fn, int nargs)
10180 tree t;
10182 t = build_vl_exp (CALL_EXPR, nargs + 3);
10183 TREE_TYPE (t) = return_type;
10184 CALL_EXPR_FN (t) = fn;
10185 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10187 return t;
10190 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10191 FN and a null static chain slot. NARGS is the number of call arguments
10192 which are specified as "..." arguments. */
10194 tree
10195 build_call_nary (tree return_type, tree fn, int nargs, ...)
10197 tree ret;
10198 va_list args;
10199 va_start (args, nargs);
10200 ret = build_call_valist (return_type, fn, nargs, args);
10201 va_end (args);
10202 return ret;
10205 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10206 FN and a null static chain slot. NARGS is the number of call arguments
10207 which are specified as a va_list ARGS. */
10209 tree
10210 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10212 tree t;
10213 int i;
10215 t = build_call_1 (return_type, fn, nargs);
10216 for (i = 0; i < nargs; i++)
10217 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10218 process_call_operands (t);
10219 return t;
10222 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10223 FN and a null static chain slot. NARGS is the number of call arguments
10224 which are specified as a tree array ARGS. */
10226 tree
10227 build_call_array_loc (location_t loc, tree return_type, tree fn,
10228 int nargs, const tree *args)
10230 tree t;
10231 int i;
10233 t = build_call_1 (return_type, fn, nargs);
10234 for (i = 0; i < nargs; i++)
10235 CALL_EXPR_ARG (t, i) = args[i];
10236 process_call_operands (t);
10237 SET_EXPR_LOCATION (t, loc);
10238 return t;
10241 /* Like build_call_array, but takes a vec. */
10243 tree
10244 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10246 tree ret, t;
10247 unsigned int ix;
10249 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10250 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10251 CALL_EXPR_ARG (ret, ix) = t;
10252 process_call_operands (ret);
10253 return ret;
10257 /* Returns true if it is possible to prove that the index of
10258 an array access REF (an ARRAY_REF expression) falls into the
10259 array bounds. */
10261 bool
10262 in_array_bounds_p (tree ref)
10264 tree idx = TREE_OPERAND (ref, 1);
10265 tree min, max;
10267 if (TREE_CODE (idx) != INTEGER_CST)
10268 return false;
10270 min = array_ref_low_bound (ref);
10271 max = array_ref_up_bound (ref);
10272 if (!min
10273 || !max
10274 || TREE_CODE (min) != INTEGER_CST
10275 || TREE_CODE (max) != INTEGER_CST)
10276 return false;
10278 if (tree_int_cst_lt (idx, min)
10279 || tree_int_cst_lt (max, idx))
10280 return false;
10282 return true;
10285 /* Returns true if it is possible to prove that the range of
10286 an array access REF (an ARRAY_RANGE_REF expression) falls
10287 into the array bounds. */
10289 bool
10290 range_in_array_bounds_p (tree ref)
10292 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
10293 tree range_min, range_max, min, max;
10295 range_min = TYPE_MIN_VALUE (domain_type);
10296 range_max = TYPE_MAX_VALUE (domain_type);
10297 if (!range_min
10298 || !range_max
10299 || TREE_CODE (range_min) != INTEGER_CST
10300 || TREE_CODE (range_max) != INTEGER_CST)
10301 return false;
10303 min = array_ref_low_bound (ref);
10304 max = array_ref_up_bound (ref);
10305 if (!min
10306 || !max
10307 || TREE_CODE (min) != INTEGER_CST
10308 || TREE_CODE (max) != INTEGER_CST)
10309 return false;
10311 if (tree_int_cst_lt (range_min, min)
10312 || tree_int_cst_lt (max, range_max))
10313 return false;
10315 return true;
10318 /* Return true if T (assumed to be a DECL) must be assigned a memory
10319 location. */
10321 bool
10322 needs_to_live_in_memory (const_tree t)
10324 return (TREE_ADDRESSABLE (t)
10325 || is_global_var (t)
10326 || (TREE_CODE (t) == RESULT_DECL
10327 && !DECL_BY_REFERENCE (t)
10328 && aggregate_value_p (t, current_function_decl)));
10331 /* Return value of a constant X and sign-extend it. */
10333 HOST_WIDE_INT
10334 int_cst_value (const_tree x)
10336 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10337 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10339 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10340 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10341 || TREE_INT_CST_HIGH (x) == -1);
10343 if (bits < HOST_BITS_PER_WIDE_INT)
10345 bool negative = ((val >> (bits - 1)) & 1) != 0;
10346 if (negative)
10347 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10348 else
10349 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10352 return val;
10355 /* Return value of a constant X and sign-extend it. */
10357 HOST_WIDEST_INT
10358 widest_int_cst_value (const_tree x)
10360 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10361 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10363 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10364 gcc_assert (HOST_BITS_PER_WIDEST_INT >= HOST_BITS_PER_DOUBLE_INT);
10365 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
10366 << HOST_BITS_PER_WIDE_INT);
10367 #else
10368 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10369 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10370 || TREE_INT_CST_HIGH (x) == -1);
10371 #endif
10373 if (bits < HOST_BITS_PER_WIDEST_INT)
10375 bool negative = ((val >> (bits - 1)) & 1) != 0;
10376 if (negative)
10377 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10378 else
10379 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10382 return val;
10385 /* If TYPE is an integral or pointer type, return an integer type with
10386 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10387 if TYPE is already an integer type of signedness UNSIGNEDP. */
10389 tree
10390 signed_or_unsigned_type_for (int unsignedp, tree type)
10392 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10393 return type;
10395 if (TREE_CODE (type) == VECTOR_TYPE)
10397 tree inner = TREE_TYPE (type);
10398 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10399 if (!inner2)
10400 return NULL_TREE;
10401 if (inner == inner2)
10402 return type;
10403 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10406 if (!INTEGRAL_TYPE_P (type)
10407 && !POINTER_TYPE_P (type))
10408 return NULL_TREE;
10410 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10413 /* If TYPE is an integral or pointer type, return an integer type with
10414 the same precision which is unsigned, or itself if TYPE is already an
10415 unsigned integer type. */
10417 tree
10418 unsigned_type_for (tree type)
10420 return signed_or_unsigned_type_for (1, type);
10423 /* If TYPE is an integral or pointer type, return an integer type with
10424 the same precision which is signed, or itself if TYPE is already a
10425 signed integer type. */
10427 tree
10428 signed_type_for (tree type)
10430 return signed_or_unsigned_type_for (0, type);
10433 /* If TYPE is a vector type, return a signed integer vector type with the
10434 same width and number of subparts. Otherwise return boolean_type_node. */
10436 tree
10437 truth_type_for (tree type)
10439 if (TREE_CODE (type) == VECTOR_TYPE)
10441 tree elem = lang_hooks.types.type_for_size
10442 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10443 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10445 else
10446 return boolean_type_node;
10449 /* Returns the largest value obtainable by casting something in INNER type to
10450 OUTER type. */
10452 tree
10453 upper_bound_in_type (tree outer, tree inner)
10455 double_int high;
10456 unsigned int det = 0;
10457 unsigned oprec = TYPE_PRECISION (outer);
10458 unsigned iprec = TYPE_PRECISION (inner);
10459 unsigned prec;
10461 /* Compute a unique number for every combination. */
10462 det |= (oprec > iprec) ? 4 : 0;
10463 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10464 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10466 /* Determine the exponent to use. */
10467 switch (det)
10469 case 0:
10470 case 1:
10471 /* oprec <= iprec, outer: signed, inner: don't care. */
10472 prec = oprec - 1;
10473 break;
10474 case 2:
10475 case 3:
10476 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10477 prec = oprec;
10478 break;
10479 case 4:
10480 /* oprec > iprec, outer: signed, inner: signed. */
10481 prec = iprec - 1;
10482 break;
10483 case 5:
10484 /* oprec > iprec, outer: signed, inner: unsigned. */
10485 prec = iprec;
10486 break;
10487 case 6:
10488 /* oprec > iprec, outer: unsigned, inner: signed. */
10489 prec = oprec;
10490 break;
10491 case 7:
10492 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10493 prec = iprec;
10494 break;
10495 default:
10496 gcc_unreachable ();
10499 /* Compute 2^^prec - 1. */
10500 if (prec <= HOST_BITS_PER_WIDE_INT)
10502 high.high = 0;
10503 high.low = ((~(unsigned HOST_WIDE_INT) 0)
10504 >> (HOST_BITS_PER_WIDE_INT - prec));
10506 else
10508 high.high = ((~(unsigned HOST_WIDE_INT) 0)
10509 >> (HOST_BITS_PER_DOUBLE_INT - prec));
10510 high.low = ~(unsigned HOST_WIDE_INT) 0;
10513 return double_int_to_tree (outer, high);
10516 /* Returns the smallest value obtainable by casting something in INNER type to
10517 OUTER type. */
10519 tree
10520 lower_bound_in_type (tree outer, tree inner)
10522 double_int low;
10523 unsigned oprec = TYPE_PRECISION (outer);
10524 unsigned iprec = TYPE_PRECISION (inner);
10526 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10527 and obtain 0. */
10528 if (TYPE_UNSIGNED (outer)
10529 /* If we are widening something of an unsigned type, OUTER type
10530 contains all values of INNER type. In particular, both INNER
10531 and OUTER types have zero in common. */
10532 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10533 low.low = low.high = 0;
10534 else
10536 /* If we are widening a signed type to another signed type, we
10537 want to obtain -2^^(iprec-1). If we are keeping the
10538 precision or narrowing to a signed type, we want to obtain
10539 -2^(oprec-1). */
10540 unsigned prec = oprec > iprec ? iprec : oprec;
10542 if (prec <= HOST_BITS_PER_WIDE_INT)
10544 low.high = ~(unsigned HOST_WIDE_INT) 0;
10545 low.low = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
10547 else
10549 low.high = ((~(unsigned HOST_WIDE_INT) 0)
10550 << (prec - HOST_BITS_PER_WIDE_INT - 1));
10551 low.low = 0;
10555 return double_int_to_tree (outer, low);
10558 /* Return nonzero if two operands that are suitable for PHI nodes are
10559 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10560 SSA_NAME or invariant. Note that this is strictly an optimization.
10561 That is, callers of this function can directly call operand_equal_p
10562 and get the same result, only slower. */
10565 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10567 if (arg0 == arg1)
10568 return 1;
10569 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10570 return 0;
10571 return operand_equal_p (arg0, arg1, 0);
10574 /* Returns number of zeros at the end of binary representation of X.
10576 ??? Use ffs if available? */
10578 tree
10579 num_ending_zeros (const_tree x)
10581 unsigned HOST_WIDE_INT fr, nfr;
10582 unsigned num, abits;
10583 tree type = TREE_TYPE (x);
10585 if (TREE_INT_CST_LOW (x) == 0)
10587 num = HOST_BITS_PER_WIDE_INT;
10588 fr = TREE_INT_CST_HIGH (x);
10590 else
10592 num = 0;
10593 fr = TREE_INT_CST_LOW (x);
10596 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
10598 nfr = fr >> abits;
10599 if (nfr << abits == fr)
10601 num += abits;
10602 fr = nfr;
10606 if (num > TYPE_PRECISION (type))
10607 num = TYPE_PRECISION (type);
10609 return build_int_cst_type (type, num);
10613 #define WALK_SUBTREE(NODE) \
10614 do \
10616 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10617 if (result) \
10618 return result; \
10620 while (0)
10622 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10623 be walked whenever a type is seen in the tree. Rest of operands and return
10624 value are as for walk_tree. */
10626 static tree
10627 walk_type_fields (tree type, walk_tree_fn func, void *data,
10628 struct pointer_set_t *pset, walk_tree_lh lh)
10630 tree result = NULL_TREE;
10632 switch (TREE_CODE (type))
10634 case POINTER_TYPE:
10635 case REFERENCE_TYPE:
10636 /* We have to worry about mutually recursive pointers. These can't
10637 be written in C. They can in Ada. It's pathological, but
10638 there's an ACATS test (c38102a) that checks it. Deal with this
10639 by checking if we're pointing to another pointer, that one
10640 points to another pointer, that one does too, and we have no htab.
10641 If so, get a hash table. We check three levels deep to avoid
10642 the cost of the hash table if we don't need one. */
10643 if (POINTER_TYPE_P (TREE_TYPE (type))
10644 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10645 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10646 && !pset)
10648 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10649 func, data);
10650 if (result)
10651 return result;
10653 break;
10656 /* ... fall through ... */
10658 case COMPLEX_TYPE:
10659 WALK_SUBTREE (TREE_TYPE (type));
10660 break;
10662 case METHOD_TYPE:
10663 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10665 /* Fall through. */
10667 case FUNCTION_TYPE:
10668 WALK_SUBTREE (TREE_TYPE (type));
10670 tree arg;
10672 /* We never want to walk into default arguments. */
10673 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10674 WALK_SUBTREE (TREE_VALUE (arg));
10676 break;
10678 case ARRAY_TYPE:
10679 /* Don't follow this nodes's type if a pointer for fear that
10680 we'll have infinite recursion. If we have a PSET, then we
10681 need not fear. */
10682 if (pset
10683 || (!POINTER_TYPE_P (TREE_TYPE (type))
10684 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10685 WALK_SUBTREE (TREE_TYPE (type));
10686 WALK_SUBTREE (TYPE_DOMAIN (type));
10687 break;
10689 case OFFSET_TYPE:
10690 WALK_SUBTREE (TREE_TYPE (type));
10691 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10692 break;
10694 default:
10695 break;
10698 return NULL_TREE;
10701 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10702 called with the DATA and the address of each sub-tree. If FUNC returns a
10703 non-NULL value, the traversal is stopped, and the value returned by FUNC
10704 is returned. If PSET is non-NULL it is used to record the nodes visited,
10705 and to avoid visiting a node more than once. */
10707 tree
10708 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10709 struct pointer_set_t *pset, walk_tree_lh lh)
10711 enum tree_code code;
10712 int walk_subtrees;
10713 tree result;
10715 #define WALK_SUBTREE_TAIL(NODE) \
10716 do \
10718 tp = & (NODE); \
10719 goto tail_recurse; \
10721 while (0)
10723 tail_recurse:
10724 /* Skip empty subtrees. */
10725 if (!*tp)
10726 return NULL_TREE;
10728 /* Don't walk the same tree twice, if the user has requested
10729 that we avoid doing so. */
10730 if (pset && pointer_set_insert (pset, *tp))
10731 return NULL_TREE;
10733 /* Call the function. */
10734 walk_subtrees = 1;
10735 result = (*func) (tp, &walk_subtrees, data);
10737 /* If we found something, return it. */
10738 if (result)
10739 return result;
10741 code = TREE_CODE (*tp);
10743 /* Even if we didn't, FUNC may have decided that there was nothing
10744 interesting below this point in the tree. */
10745 if (!walk_subtrees)
10747 /* But we still need to check our siblings. */
10748 if (code == TREE_LIST)
10749 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10750 else if (code == OMP_CLAUSE)
10751 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10752 else
10753 return NULL_TREE;
10756 if (lh)
10758 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10759 if (result || !walk_subtrees)
10760 return result;
10763 switch (code)
10765 case ERROR_MARK:
10766 case IDENTIFIER_NODE:
10767 case INTEGER_CST:
10768 case REAL_CST:
10769 case FIXED_CST:
10770 case VECTOR_CST:
10771 case STRING_CST:
10772 case BLOCK:
10773 case PLACEHOLDER_EXPR:
10774 case SSA_NAME:
10775 case FIELD_DECL:
10776 case RESULT_DECL:
10777 /* None of these have subtrees other than those already walked
10778 above. */
10779 break;
10781 case TREE_LIST:
10782 WALK_SUBTREE (TREE_VALUE (*tp));
10783 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10784 break;
10786 case TREE_VEC:
10788 int len = TREE_VEC_LENGTH (*tp);
10790 if (len == 0)
10791 break;
10793 /* Walk all elements but the first. */
10794 while (--len)
10795 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
10797 /* Now walk the first one as a tail call. */
10798 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
10801 case COMPLEX_CST:
10802 WALK_SUBTREE (TREE_REALPART (*tp));
10803 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
10805 case CONSTRUCTOR:
10807 unsigned HOST_WIDE_INT idx;
10808 constructor_elt *ce;
10810 for (idx = 0; vec_safe_iterate(CONSTRUCTOR_ELTS (*tp), idx, &ce); idx++)
10811 WALK_SUBTREE (ce->value);
10813 break;
10815 case SAVE_EXPR:
10816 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
10818 case BIND_EXPR:
10820 tree decl;
10821 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
10823 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
10824 into declarations that are just mentioned, rather than
10825 declared; they don't really belong to this part of the tree.
10826 And, we can see cycles: the initializer for a declaration
10827 can refer to the declaration itself. */
10828 WALK_SUBTREE (DECL_INITIAL (decl));
10829 WALK_SUBTREE (DECL_SIZE (decl));
10830 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
10832 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
10835 case STATEMENT_LIST:
10837 tree_stmt_iterator i;
10838 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
10839 WALK_SUBTREE (*tsi_stmt_ptr (i));
10841 break;
10843 case OMP_CLAUSE:
10844 switch (OMP_CLAUSE_CODE (*tp))
10846 case OMP_CLAUSE_PRIVATE:
10847 case OMP_CLAUSE_SHARED:
10848 case OMP_CLAUSE_FIRSTPRIVATE:
10849 case OMP_CLAUSE_COPYIN:
10850 case OMP_CLAUSE_COPYPRIVATE:
10851 case OMP_CLAUSE_FINAL:
10852 case OMP_CLAUSE_IF:
10853 case OMP_CLAUSE_NUM_THREADS:
10854 case OMP_CLAUSE_SCHEDULE:
10855 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
10856 /* FALLTHRU */
10858 case OMP_CLAUSE_NOWAIT:
10859 case OMP_CLAUSE_ORDERED:
10860 case OMP_CLAUSE_DEFAULT:
10861 case OMP_CLAUSE_UNTIED:
10862 case OMP_CLAUSE_MERGEABLE:
10863 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10865 case OMP_CLAUSE_LASTPRIVATE:
10866 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
10867 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
10868 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10870 case OMP_CLAUSE_COLLAPSE:
10872 int i;
10873 for (i = 0; i < 3; i++)
10874 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10875 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10878 case OMP_CLAUSE_REDUCTION:
10880 int i;
10881 for (i = 0; i < 4; i++)
10882 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10883 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10886 default:
10887 gcc_unreachable ();
10889 break;
10891 case TARGET_EXPR:
10893 int i, len;
10895 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
10896 But, we only want to walk once. */
10897 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
10898 for (i = 0; i < len; ++i)
10899 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10900 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
10903 case DECL_EXPR:
10904 /* If this is a TYPE_DECL, walk into the fields of the type that it's
10905 defining. We only want to walk into these fields of a type in this
10906 case and not in the general case of a mere reference to the type.
10908 The criterion is as follows: if the field can be an expression, it
10909 must be walked only here. This should be in keeping with the fields
10910 that are directly gimplified in gimplify_type_sizes in order for the
10911 mark/copy-if-shared/unmark machinery of the gimplifier to work with
10912 variable-sized types.
10914 Note that DECLs get walked as part of processing the BIND_EXPR. */
10915 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
10917 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
10918 if (TREE_CODE (*type_p) == ERROR_MARK)
10919 return NULL_TREE;
10921 /* Call the function for the type. See if it returns anything or
10922 doesn't want us to continue. If we are to continue, walk both
10923 the normal fields and those for the declaration case. */
10924 result = (*func) (type_p, &walk_subtrees, data);
10925 if (result || !walk_subtrees)
10926 return result;
10928 /* But do not walk a pointed-to type since it may itself need to
10929 be walked in the declaration case if it isn't anonymous. */
10930 if (!POINTER_TYPE_P (*type_p))
10932 result = walk_type_fields (*type_p, func, data, pset, lh);
10933 if (result)
10934 return result;
10937 /* If this is a record type, also walk the fields. */
10938 if (RECORD_OR_UNION_TYPE_P (*type_p))
10940 tree field;
10942 for (field = TYPE_FIELDS (*type_p); field;
10943 field = DECL_CHAIN (field))
10945 /* We'd like to look at the type of the field, but we can
10946 easily get infinite recursion. So assume it's pointed
10947 to elsewhere in the tree. Also, ignore things that
10948 aren't fields. */
10949 if (TREE_CODE (field) != FIELD_DECL)
10950 continue;
10952 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
10953 WALK_SUBTREE (DECL_SIZE (field));
10954 WALK_SUBTREE (DECL_SIZE_UNIT (field));
10955 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
10956 WALK_SUBTREE (DECL_QUALIFIER (field));
10960 /* Same for scalar types. */
10961 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
10962 || TREE_CODE (*type_p) == ENUMERAL_TYPE
10963 || TREE_CODE (*type_p) == INTEGER_TYPE
10964 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
10965 || TREE_CODE (*type_p) == REAL_TYPE)
10967 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
10968 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
10971 WALK_SUBTREE (TYPE_SIZE (*type_p));
10972 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
10974 /* FALLTHRU */
10976 default:
10977 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
10979 int i, len;
10981 /* Walk over all the sub-trees of this operand. */
10982 len = TREE_OPERAND_LENGTH (*tp);
10984 /* Go through the subtrees. We need to do this in forward order so
10985 that the scope of a FOR_EXPR is handled properly. */
10986 if (len)
10988 for (i = 0; i < len - 1; ++i)
10989 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10990 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
10993 /* If this is a type, walk the needed fields in the type. */
10994 else if (TYPE_P (*tp))
10995 return walk_type_fields (*tp, func, data, pset, lh);
10996 break;
10999 /* We didn't find what we were looking for. */
11000 return NULL_TREE;
11002 #undef WALK_SUBTREE_TAIL
11004 #undef WALK_SUBTREE
11006 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11008 tree
11009 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11010 walk_tree_lh lh)
11012 tree result;
11013 struct pointer_set_t *pset;
11015 pset = pointer_set_create ();
11016 result = walk_tree_1 (tp, func, data, pset, lh);
11017 pointer_set_destroy (pset);
11018 return result;
11022 tree
11023 tree_block (tree t)
11025 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11027 if (IS_EXPR_CODE_CLASS (c))
11028 return LOCATION_BLOCK (t->exp.locus);
11029 gcc_unreachable ();
11030 return NULL;
11033 void
11034 tree_set_block (tree t, tree b)
11036 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11038 if (IS_EXPR_CODE_CLASS (c))
11040 if (b)
11041 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11042 else
11043 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11045 else
11046 gcc_unreachable ();
11049 /* Create a nameless artificial label and put it in the current
11050 function context. The label has a location of LOC. Returns the
11051 newly created label. */
11053 tree
11054 create_artificial_label (location_t loc)
11056 tree lab = build_decl (loc,
11057 LABEL_DECL, NULL_TREE, void_type_node);
11059 DECL_ARTIFICIAL (lab) = 1;
11060 DECL_IGNORED_P (lab) = 1;
11061 DECL_CONTEXT (lab) = current_function_decl;
11062 return lab;
11065 /* Given a tree, try to return a useful variable name that we can use
11066 to prefix a temporary that is being assigned the value of the tree.
11067 I.E. given <temp> = &A, return A. */
11069 const char *
11070 get_name (tree t)
11072 tree stripped_decl;
11074 stripped_decl = t;
11075 STRIP_NOPS (stripped_decl);
11076 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11077 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11078 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11080 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11081 if (!name)
11082 return NULL;
11083 return IDENTIFIER_POINTER (name);
11085 else
11087 switch (TREE_CODE (stripped_decl))
11089 case ADDR_EXPR:
11090 return get_name (TREE_OPERAND (stripped_decl, 0));
11091 default:
11092 return NULL;
11097 /* Return true if TYPE has a variable argument list. */
11099 bool
11100 stdarg_p (const_tree fntype)
11102 function_args_iterator args_iter;
11103 tree n = NULL_TREE, t;
11105 if (!fntype)
11106 return false;
11108 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
11110 n = t;
11113 return n != NULL_TREE && n != void_type_node;
11116 /* Return true if TYPE has a prototype. */
11118 bool
11119 prototype_p (tree fntype)
11121 tree t;
11123 gcc_assert (fntype != NULL_TREE);
11125 t = TYPE_ARG_TYPES (fntype);
11126 return (t != NULL_TREE);
11129 /* If BLOCK is inlined from an __attribute__((__artificial__))
11130 routine, return pointer to location from where it has been
11131 called. */
11132 location_t *
11133 block_nonartificial_location (tree block)
11135 location_t *ret = NULL;
11137 while (block && TREE_CODE (block) == BLOCK
11138 && BLOCK_ABSTRACT_ORIGIN (block))
11140 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11142 while (TREE_CODE (ao) == BLOCK
11143 && BLOCK_ABSTRACT_ORIGIN (ao)
11144 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11145 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11147 if (TREE_CODE (ao) == FUNCTION_DECL)
11149 /* If AO is an artificial inline, point RET to the
11150 call site locus at which it has been inlined and continue
11151 the loop, in case AO's caller is also an artificial
11152 inline. */
11153 if (DECL_DECLARED_INLINE_P (ao)
11154 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11155 ret = &BLOCK_SOURCE_LOCATION (block);
11156 else
11157 break;
11159 else if (TREE_CODE (ao) != BLOCK)
11160 break;
11162 block = BLOCK_SUPERCONTEXT (block);
11164 return ret;
11168 /* If EXP is inlined from an __attribute__((__artificial__))
11169 function, return the location of the original call expression. */
11171 location_t
11172 tree_nonartificial_location (tree exp)
11174 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11176 if (loc)
11177 return *loc;
11178 else
11179 return EXPR_LOCATION (exp);
11183 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11184 nodes. */
11186 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11188 static hashval_t
11189 cl_option_hash_hash (const void *x)
11191 const_tree const t = (const_tree) x;
11192 const char *p;
11193 size_t i;
11194 size_t len = 0;
11195 hashval_t hash = 0;
11197 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11199 p = (const char *)TREE_OPTIMIZATION (t);
11200 len = sizeof (struct cl_optimization);
11203 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11205 p = (const char *)TREE_TARGET_OPTION (t);
11206 len = sizeof (struct cl_target_option);
11209 else
11210 gcc_unreachable ();
11212 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11213 something else. */
11214 for (i = 0; i < len; i++)
11215 if (p[i])
11216 hash = (hash << 4) ^ ((i << 2) | p[i]);
11218 return hash;
11221 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11222 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11223 same. */
11225 static int
11226 cl_option_hash_eq (const void *x, const void *y)
11228 const_tree const xt = (const_tree) x;
11229 const_tree const yt = (const_tree) y;
11230 const char *xp;
11231 const char *yp;
11232 size_t len;
11234 if (TREE_CODE (xt) != TREE_CODE (yt))
11235 return 0;
11237 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11239 xp = (const char *)TREE_OPTIMIZATION (xt);
11240 yp = (const char *)TREE_OPTIMIZATION (yt);
11241 len = sizeof (struct cl_optimization);
11244 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11246 xp = (const char *)TREE_TARGET_OPTION (xt);
11247 yp = (const char *)TREE_TARGET_OPTION (yt);
11248 len = sizeof (struct cl_target_option);
11251 else
11252 gcc_unreachable ();
11254 return (memcmp (xp, yp, len) == 0);
11257 /* Build an OPTIMIZATION_NODE based on the current options. */
11259 tree
11260 build_optimization_node (void)
11262 tree t;
11263 void **slot;
11265 /* Use the cache of optimization nodes. */
11267 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11268 &global_options);
11270 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11271 t = (tree) *slot;
11272 if (!t)
11274 /* Insert this one into the hash table. */
11275 t = cl_optimization_node;
11276 *slot = t;
11278 /* Make a new node for next time round. */
11279 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11282 return t;
11285 /* Build a TARGET_OPTION_NODE based on the current options. */
11287 tree
11288 build_target_option_node (void)
11290 tree t;
11291 void **slot;
11293 /* Use the cache of optimization nodes. */
11295 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11296 &global_options);
11298 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11299 t = (tree) *slot;
11300 if (!t)
11302 /* Insert this one into the hash table. */
11303 t = cl_target_option_node;
11304 *slot = t;
11306 /* Make a new node for next time round. */
11307 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11310 return t;
11313 /* Determine the "ultimate origin" of a block. The block may be an inlined
11314 instance of an inlined instance of a block which is local to an inline
11315 function, so we have to trace all of the way back through the origin chain
11316 to find out what sort of node actually served as the original seed for the
11317 given block. */
11319 tree
11320 block_ultimate_origin (const_tree block)
11322 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11324 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
11325 nodes in the function to point to themselves; ignore that if
11326 we're trying to output the abstract instance of this function. */
11327 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11328 return NULL_TREE;
11330 if (immediate_origin == NULL_TREE)
11331 return NULL_TREE;
11332 else
11334 tree ret_val;
11335 tree lookahead = immediate_origin;
11339 ret_val = lookahead;
11340 lookahead = (TREE_CODE (ret_val) == BLOCK
11341 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11343 while (lookahead != NULL && lookahead != ret_val);
11345 /* The block's abstract origin chain may not be the *ultimate* origin of
11346 the block. It could lead to a DECL that has an abstract origin set.
11347 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11348 will give us if it has one). Note that DECL's abstract origins are
11349 supposed to be the most distant ancestor (or so decl_ultimate_origin
11350 claims), so we don't need to loop following the DECL origins. */
11351 if (DECL_P (ret_val))
11352 return DECL_ORIGIN (ret_val);
11354 return ret_val;
11358 /* Return true if T1 and T2 are equivalent lists. */
11360 bool
11361 list_equal_p (const_tree t1, const_tree t2)
11363 for (; t1 && t2; t1 = TREE_CHAIN (t1) , t2 = TREE_CHAIN (t2))
11364 if (TREE_VALUE (t1) != TREE_VALUE (t2))
11365 return false;
11366 return !t1 && !t2;
11369 /* Return true iff conversion in EXP generates no instruction. Mark
11370 it inline so that we fully inline into the stripping functions even
11371 though we have two uses of this function. */
11373 static inline bool
11374 tree_nop_conversion (const_tree exp)
11376 tree outer_type, inner_type;
11377 int outer_is_pts_p, inner_is_pts_p;
11379 if (!CONVERT_EXPR_P (exp)
11380 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11381 return false;
11382 if (TREE_OPERAND (exp, 0) == error_mark_node)
11383 return false;
11385 outer_type = TREE_TYPE (exp);
11386 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11388 if (!inner_type)
11389 return false;
11391 outer_is_pts_p = (POINTER_TYPE_P (outer_type)
11392 && upc_shared_type_p (TREE_TYPE (outer_type)));
11393 inner_is_pts_p = (POINTER_TYPE_P (inner_type)
11394 && upc_shared_type_p (TREE_TYPE (inner_type)));
11396 /* UPC pointer-to-shared types have special
11397 equivalence rules that must be checked. */
11398 if (outer_is_pts_p && inner_is_pts_p
11399 && lang_hooks.types_compatible_p)
11400 return lang_hooks.types_compatible_p (outer_type, inner_type);
11402 /* UPC pointer-to-shared types are not interchangeable
11403 with integral types. */
11404 if (outer_is_pts_p || inner_is_pts_p)
11405 return false;
11407 /* Use precision rather then machine mode when we can, which gives
11408 the correct answer even for submode (bit-field) types. */
11409 if ((INTEGRAL_TYPE_P (outer_type)
11410 || POINTER_TYPE_P (outer_type)
11411 || TREE_CODE (outer_type) == OFFSET_TYPE)
11412 && (INTEGRAL_TYPE_P (inner_type)
11413 || POINTER_TYPE_P (inner_type)
11414 || TREE_CODE (inner_type) == OFFSET_TYPE))
11415 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11417 /* Otherwise fall back on comparing machine modes (e.g. for
11418 aggregate types, floats). */
11419 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11422 /* Return true iff conversion in EXP generates no instruction. Don't
11423 consider conversions changing the signedness. */
11425 static bool
11426 tree_sign_nop_conversion (const_tree exp)
11428 tree outer_type, inner_type;
11430 if (!tree_nop_conversion (exp))
11431 return false;
11433 outer_type = TREE_TYPE (exp);
11434 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11436 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11437 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11440 /* Strip conversions from EXP according to tree_nop_conversion and
11441 return the resulting expression. */
11443 tree
11444 tree_strip_nop_conversions (tree exp)
11446 while (tree_nop_conversion (exp))
11447 exp = TREE_OPERAND (exp, 0);
11448 return exp;
11451 /* Strip conversions from EXP according to tree_sign_nop_conversion
11452 and return the resulting expression. */
11454 tree
11455 tree_strip_sign_nop_conversions (tree exp)
11457 while (tree_sign_nop_conversion (exp))
11458 exp = TREE_OPERAND (exp, 0);
11459 return exp;
11462 /* Avoid any floating point extensions from EXP. */
11463 tree
11464 strip_float_extensions (tree exp)
11466 tree sub, expt, subt;
11468 /* For floating point constant look up the narrowest type that can hold
11469 it properly and handle it like (type)(narrowest_type)constant.
11470 This way we can optimize for instance a=a*2.0 where "a" is float
11471 but 2.0 is double constant. */
11472 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11474 REAL_VALUE_TYPE orig;
11475 tree type = NULL;
11477 orig = TREE_REAL_CST (exp);
11478 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11479 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11480 type = float_type_node;
11481 else if (TYPE_PRECISION (TREE_TYPE (exp))
11482 > TYPE_PRECISION (double_type_node)
11483 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11484 type = double_type_node;
11485 if (type)
11486 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11489 if (!CONVERT_EXPR_P (exp))
11490 return exp;
11492 sub = TREE_OPERAND (exp, 0);
11493 subt = TREE_TYPE (sub);
11494 expt = TREE_TYPE (exp);
11496 if (!FLOAT_TYPE_P (subt))
11497 return exp;
11499 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11500 return exp;
11502 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11503 return exp;
11505 return strip_float_extensions (sub);
11508 /* Strip out all handled components that produce invariant
11509 offsets. */
11511 const_tree
11512 strip_invariant_refs (const_tree op)
11514 while (handled_component_p (op))
11516 switch (TREE_CODE (op))
11518 case ARRAY_REF:
11519 case ARRAY_RANGE_REF:
11520 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11521 || TREE_OPERAND (op, 2) != NULL_TREE
11522 || TREE_OPERAND (op, 3) != NULL_TREE)
11523 return NULL;
11524 break;
11526 case COMPONENT_REF:
11527 if (TREE_OPERAND (op, 2) != NULL_TREE)
11528 return NULL;
11529 break;
11531 default:;
11533 op = TREE_OPERAND (op, 0);
11536 return op;
11539 static GTY(()) tree gcc_eh_personality_decl;
11541 /* Return the GCC personality function decl. */
11543 tree
11544 lhd_gcc_personality (void)
11546 if (!gcc_eh_personality_decl)
11547 gcc_eh_personality_decl = build_personality_function ("gcc");
11548 return gcc_eh_personality_decl;
11551 /* Try to find a base info of BINFO that would have its field decl at offset
11552 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11553 found, return, otherwise return NULL_TREE. */
11555 tree
11556 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11558 tree type = BINFO_TYPE (binfo);
11560 while (true)
11562 HOST_WIDE_INT pos, size;
11563 tree fld;
11564 int i;
11566 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (expected_type))
11567 return binfo;
11568 if (offset < 0)
11569 return NULL_TREE;
11571 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11573 if (TREE_CODE (fld) != FIELD_DECL)
11574 continue;
11576 pos = int_bit_position (fld);
11577 size = tree_low_cst (DECL_SIZE (fld), 1);
11578 if (pos <= offset && (pos + size) > offset)
11579 break;
11581 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11582 return NULL_TREE;
11584 if (!DECL_ARTIFICIAL (fld))
11586 binfo = TYPE_BINFO (TREE_TYPE (fld));
11587 if (!binfo)
11588 return NULL_TREE;
11590 /* Offset 0 indicates the primary base, whose vtable contents are
11591 represented in the binfo for the derived class. */
11592 else if (offset != 0)
11594 tree base_binfo, found_binfo = NULL_TREE;
11595 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11596 if (TREE_TYPE (base_binfo) == TREE_TYPE (fld))
11598 found_binfo = base_binfo;
11599 break;
11601 if (!found_binfo)
11602 return NULL_TREE;
11603 binfo = found_binfo;
11606 type = TREE_TYPE (fld);
11607 offset -= pos;
11611 /* Returns true if X is a typedef decl. */
11613 bool
11614 is_typedef_decl (tree x)
11616 return (x && TREE_CODE (x) == TYPE_DECL
11617 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
11620 /* Returns true iff TYPE is a type variant created for a typedef. */
11622 bool
11623 typedef_variant_p (tree type)
11625 return is_typedef_decl (TYPE_NAME (type));
11628 /* Warn about a use of an identifier which was marked deprecated. */
11629 void
11630 warn_deprecated_use (tree node, tree attr)
11632 const char *msg;
11634 if (node == 0 || !warn_deprecated_decl)
11635 return;
11637 if (!attr)
11639 if (DECL_P (node))
11640 attr = DECL_ATTRIBUTES (node);
11641 else if (TYPE_P (node))
11643 tree decl = TYPE_STUB_DECL (node);
11644 if (decl)
11645 attr = lookup_attribute ("deprecated",
11646 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
11650 if (attr)
11651 attr = lookup_attribute ("deprecated", attr);
11653 if (attr)
11654 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
11655 else
11656 msg = NULL;
11658 if (DECL_P (node))
11660 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
11661 if (msg)
11662 warning (OPT_Wdeprecated_declarations,
11663 "%qD is deprecated (declared at %s:%d): %s",
11664 node, xloc.file, xloc.line, msg);
11665 else
11666 warning (OPT_Wdeprecated_declarations,
11667 "%qD is deprecated (declared at %s:%d)",
11668 node, xloc.file, xloc.line);
11670 else if (TYPE_P (node))
11672 tree what = NULL_TREE;
11673 tree decl = TYPE_STUB_DECL (node);
11675 if (TYPE_NAME (node))
11677 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
11678 what = TYPE_NAME (node);
11679 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
11680 && DECL_NAME (TYPE_NAME (node)))
11681 what = DECL_NAME (TYPE_NAME (node));
11684 if (decl)
11686 expanded_location xloc
11687 = expand_location (DECL_SOURCE_LOCATION (decl));
11688 if (what)
11690 if (msg)
11691 warning (OPT_Wdeprecated_declarations,
11692 "%qE is deprecated (declared at %s:%d): %s",
11693 what, xloc.file, xloc.line, msg);
11694 else
11695 warning (OPT_Wdeprecated_declarations,
11696 "%qE is deprecated (declared at %s:%d)", what,
11697 xloc.file, xloc.line);
11699 else
11701 if (msg)
11702 warning (OPT_Wdeprecated_declarations,
11703 "type is deprecated (declared at %s:%d): %s",
11704 xloc.file, xloc.line, msg);
11705 else
11706 warning (OPT_Wdeprecated_declarations,
11707 "type is deprecated (declared at %s:%d)",
11708 xloc.file, xloc.line);
11711 else
11713 if (what)
11715 if (msg)
11716 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
11717 what, msg);
11718 else
11719 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
11721 else
11723 if (msg)
11724 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
11725 msg);
11726 else
11727 warning (OPT_Wdeprecated_declarations, "type is deprecated");
11733 #include "gt-tree.h"