Mark ChangeLog
[official-gcc.git] / gcc / tree.c
blob077eed8ac082c92464177ab0eaab3019d3a2feda
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);
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_cleared_tree_node_stat (length PASS_MEM_STAT);
861 TREE_SET_CODE (t, code);
863 switch (type)
865 case tcc_statement:
866 TREE_SIDE_EFFECTS (t) = 1;
867 break;
869 case tcc_declaration:
870 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
872 if (code == FUNCTION_DECL)
874 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
875 DECL_MODE (t) = FUNCTION_MODE;
877 else
878 DECL_ALIGN (t) = 1;
880 DECL_SOURCE_LOCATION (t) = input_location;
881 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
882 DECL_UID (t) = --next_debug_decl_uid;
883 else
885 DECL_UID (t) = allocate_decl_uid ();
886 SET_DECL_PT_UID (t, -1);
888 if (TREE_CODE (t) == LABEL_DECL)
889 LABEL_DECL_UID (t) = -1;
891 break;
893 case tcc_type:
894 TYPE_UID (t) = next_type_uid++;
895 TYPE_ALIGN (t) = BITS_PER_UNIT;
896 TYPE_USER_ALIGN (t) = 0;
897 TYPE_MAIN_VARIANT (t) = t;
898 TYPE_CANONICAL (t) = t;
900 /* Default to no attributes for type, but let target change that. */
901 TYPE_ATTRIBUTES (t) = NULL_TREE;
902 targetm.set_default_type_attributes (t);
904 /* We have not yet computed the alias set for this type. */
905 TYPE_ALIAS_SET (t) = -1;
906 break;
908 case tcc_constant:
909 TREE_CONSTANT (t) = 1;
910 break;
912 case tcc_expression:
913 switch (code)
915 case INIT_EXPR:
916 case MODIFY_EXPR:
917 case VA_ARG_EXPR:
918 case PREDECREMENT_EXPR:
919 case PREINCREMENT_EXPR:
920 case POSTDECREMENT_EXPR:
921 case POSTINCREMENT_EXPR:
922 /* All of these have side-effects, no matter what their
923 operands are. */
924 TREE_SIDE_EFFECTS (t) = 1;
925 break;
927 default:
928 break;
930 break;
932 default:
933 /* Other classes need no special treatment. */
934 break;
937 return t;
940 /* Return a new node with the same contents as NODE except that its
941 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
943 tree
944 copy_node_stat (tree node MEM_STAT_DECL)
946 tree t;
947 enum tree_code code = TREE_CODE (node);
948 size_t length;
950 gcc_assert (code != STATEMENT_LIST);
952 length = tree_size (node);
953 record_node_allocation_statistics (code, length);
954 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
955 memcpy (t, node, length);
957 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
958 TREE_CHAIN (t) = 0;
959 TREE_ASM_WRITTEN (t) = 0;
960 TREE_VISITED (t) = 0;
962 if (TREE_CODE_CLASS (code) == tcc_declaration)
964 if (code == DEBUG_EXPR_DECL)
965 DECL_UID (t) = --next_debug_decl_uid;
966 else
968 DECL_UID (t) = allocate_decl_uid ();
969 if (DECL_PT_UID_SET_P (node))
970 SET_DECL_PT_UID (t, DECL_PT_UID (node));
972 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
973 && DECL_HAS_VALUE_EXPR_P (node))
975 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
976 DECL_HAS_VALUE_EXPR_P (t) = 1;
978 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
980 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
981 DECL_HAS_INIT_PRIORITY_P (t) = 1;
983 if (TREE_CODE (node) == FUNCTION_DECL)
984 DECL_STRUCT_FUNCTION (t) = NULL;
986 else if (TREE_CODE_CLASS (code) == tcc_type)
988 TYPE_UID (t) = next_type_uid++;
989 /* The following is so that the debug code for
990 the copy is different from the original type.
991 The two statements usually duplicate each other
992 (because they clear fields of the same union),
993 but the optimizer should catch that. */
994 TYPE_SYMTAB_POINTER (t) = 0;
995 TYPE_SYMTAB_ADDRESS (t) = 0;
997 /* Do not copy the values cache. */
998 if (TYPE_CACHED_VALUES_P(t))
1000 TYPE_CACHED_VALUES_P (t) = 0;
1001 TYPE_CACHED_VALUES (t) = NULL_TREE;
1005 return t;
1008 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1009 For example, this can copy a list made of TREE_LIST nodes. */
1011 tree
1012 copy_list (tree list)
1014 tree head;
1015 tree prev, next;
1017 if (list == 0)
1018 return 0;
1020 head = prev = copy_node (list);
1021 next = TREE_CHAIN (list);
1022 while (next)
1024 TREE_CHAIN (prev) = copy_node (next);
1025 prev = TREE_CHAIN (prev);
1026 next = TREE_CHAIN (next);
1028 return head;
1032 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1034 tree
1035 build_int_cst (tree type, HOST_WIDE_INT low)
1037 /* Support legacy code. */
1038 if (!type)
1039 type = integer_type_node;
1041 return double_int_to_tree (type, double_int::from_shwi (low));
1044 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1046 tree
1047 build_int_cst_type (tree type, HOST_WIDE_INT low)
1049 gcc_assert (type);
1051 return double_int_to_tree (type, double_int::from_shwi (low));
1054 /* Constructs tree in type TYPE from with value given by CST. Signedness
1055 of CST is assumed to be the same as the signedness of TYPE. */
1057 tree
1058 double_int_to_tree (tree type, double_int cst)
1060 bool sign_extended_type = !TYPE_UNSIGNED (type);
1062 cst = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1064 return build_int_cst_wide (type, cst.low, cst.high);
1067 /* Returns true if CST fits into range of TYPE. Signedness of CST is assumed
1068 to be the same as the signedness of TYPE. */
1070 bool
1071 double_int_fits_to_tree_p (const_tree type, double_int cst)
1073 bool sign_extended_type = !TYPE_UNSIGNED (type);
1075 double_int ext
1076 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1078 return cst == ext;
1081 /* We force the double_int CST to the range of the type TYPE by sign or
1082 zero extending it. OVERFLOWABLE indicates if we are interested in
1083 overflow of the value, when >0 we are only interested in signed
1084 overflow, for <0 we are interested in any overflow. OVERFLOWED
1085 indicates whether overflow has already occurred. CONST_OVERFLOWED
1086 indicates whether constant overflow has already occurred. We force
1087 T's value to be within range of T's type (by setting to 0 or 1 all
1088 the bits outside the type's range). We set TREE_OVERFLOWED if,
1089 OVERFLOWED is nonzero,
1090 or OVERFLOWABLE is >0 and signed overflow occurs
1091 or OVERFLOWABLE is <0 and any overflow occurs
1092 We return a new tree node for the extended double_int. The node
1093 is shared if no overflow flags are set. */
1096 tree
1097 force_fit_type_double (tree type, double_int cst, int overflowable,
1098 bool overflowed)
1100 bool sign_extended_type = !TYPE_UNSIGNED (type);
1102 /* If we need to set overflow flags, return a new unshared node. */
1103 if (overflowed || !double_int_fits_to_tree_p(type, cst))
1105 if (overflowed
1106 || overflowable < 0
1107 || (overflowable > 0 && sign_extended_type))
1109 tree t = make_node (INTEGER_CST);
1110 TREE_INT_CST (t)
1111 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1112 TREE_TYPE (t) = type;
1113 TREE_OVERFLOW (t) = 1;
1114 return t;
1118 /* Else build a shared node. */
1119 return double_int_to_tree (type, cst);
1122 /* These are the hash table functions for the hash table of INTEGER_CST
1123 nodes of a sizetype. */
1125 /* Return the hash code code X, an INTEGER_CST. */
1127 static hashval_t
1128 int_cst_hash_hash (const void *x)
1130 const_tree const t = (const_tree) x;
1132 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1133 ^ TYPE_UID (TREE_TYPE (t)));
1136 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1137 is the same as that given by *Y, which is the same. */
1139 static int
1140 int_cst_hash_eq (const void *x, const void *y)
1142 const_tree const xt = (const_tree) x;
1143 const_tree const yt = (const_tree) y;
1145 return (TREE_TYPE (xt) == TREE_TYPE (yt)
1146 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1147 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
1150 /* Create an INT_CST node of TYPE and value HI:LOW.
1151 The returned node is always shared. For small integers we use a
1152 per-type vector cache, for larger ones we use a single hash table. */
1154 tree
1155 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
1157 tree t;
1158 int ix = -1;
1159 int limit = 0;
1161 gcc_assert (type);
1163 switch (TREE_CODE (type))
1165 case NULLPTR_TYPE:
1166 gcc_assert (hi == 0 && low == 0);
1167 /* Fallthru. */
1169 case POINTER_TYPE:
1170 case REFERENCE_TYPE:
1171 /* Cache NULL pointer. */
1172 if (!hi && !low)
1174 limit = 1;
1175 ix = 0;
1177 break;
1179 case BOOLEAN_TYPE:
1180 /* Cache false or true. */
1181 limit = 2;
1182 if (!hi && low < 2)
1183 ix = low;
1184 break;
1186 case INTEGER_TYPE:
1187 case OFFSET_TYPE:
1188 if (TYPE_UNSIGNED (type))
1190 /* Cache 0..N */
1191 limit = INTEGER_SHARE_LIMIT;
1192 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1193 ix = low;
1195 else
1197 /* Cache -1..N */
1198 limit = INTEGER_SHARE_LIMIT + 1;
1199 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1200 ix = low + 1;
1201 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1202 ix = 0;
1204 break;
1206 case ENUMERAL_TYPE:
1207 break;
1209 default:
1210 gcc_unreachable ();
1213 if (ix >= 0)
1215 /* Look for it in the type's vector of small shared ints. */
1216 if (!TYPE_CACHED_VALUES_P (type))
1218 TYPE_CACHED_VALUES_P (type) = 1;
1219 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1222 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1223 if (t)
1225 /* Make sure no one is clobbering the shared constant. */
1226 gcc_assert (TREE_TYPE (t) == type);
1227 gcc_assert (TREE_INT_CST_LOW (t) == low);
1228 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
1230 else
1232 /* Create a new shared int. */
1233 t = make_node (INTEGER_CST);
1235 TREE_INT_CST_LOW (t) = low;
1236 TREE_INT_CST_HIGH (t) = hi;
1237 TREE_TYPE (t) = type;
1239 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1242 else
1244 /* Use the cache of larger shared ints. */
1245 void **slot;
1247 TREE_INT_CST_LOW (int_cst_node) = low;
1248 TREE_INT_CST_HIGH (int_cst_node) = hi;
1249 TREE_TYPE (int_cst_node) = type;
1251 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
1252 t = (tree) *slot;
1253 if (!t)
1255 /* Insert this one into the hash table. */
1256 t = int_cst_node;
1257 *slot = t;
1258 /* Make a new node for next time round. */
1259 int_cst_node = make_node (INTEGER_CST);
1263 return t;
1266 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1267 and the rest are zeros. */
1269 tree
1270 build_low_bits_mask (tree type, unsigned bits)
1272 double_int mask;
1274 gcc_assert (bits <= TYPE_PRECISION (type));
1276 if (bits == TYPE_PRECISION (type)
1277 && !TYPE_UNSIGNED (type))
1278 /* Sign extended all-ones mask. */
1279 mask = double_int_minus_one;
1280 else
1281 mask = double_int::mask (bits);
1283 return build_int_cst_wide (type, mask.low, mask.high);
1286 /* Checks that X is integer constant that can be expressed in (unsigned)
1287 HOST_WIDE_INT without loss of precision. */
1289 bool
1290 cst_and_fits_in_hwi (const_tree x)
1292 if (TREE_CODE (x) != INTEGER_CST)
1293 return false;
1295 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1296 return false;
1298 return (TREE_INT_CST_HIGH (x) == 0
1299 || TREE_INT_CST_HIGH (x) == -1);
1302 /* Build a newly constructed TREE_VEC node of length LEN. */
1304 tree
1305 make_vector_stat (unsigned len MEM_STAT_DECL)
1307 tree t;
1308 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1310 record_node_allocation_statistics (VECTOR_CST, length);
1312 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1314 TREE_SET_CODE (t, VECTOR_CST);
1315 TREE_CONSTANT (t) = 1;
1317 return t;
1320 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1321 are in a list pointed to by VALS. */
1323 tree
1324 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1326 int over = 0;
1327 unsigned cnt = 0;
1328 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1329 TREE_TYPE (v) = type;
1331 /* Iterate through elements and check for overflow. */
1332 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1334 tree value = vals[cnt];
1336 VECTOR_CST_ELT (v, cnt) = value;
1338 /* Don't crash if we get an address constant. */
1339 if (!CONSTANT_CLASS_P (value))
1340 continue;
1342 over |= TREE_OVERFLOW (value);
1345 TREE_OVERFLOW (v) = over;
1346 return v;
1349 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1350 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1352 tree
1353 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1355 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1356 unsigned HOST_WIDE_INT idx;
1357 tree value;
1359 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1360 vec[idx] = value;
1361 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1362 vec[idx] = build_zero_cst (TREE_TYPE (type));
1364 return build_vector (type, vec);
1367 /* Build a vector of type VECTYPE where all the elements are SCs. */
1368 tree
1369 build_vector_from_val (tree vectype, tree sc)
1371 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1373 if (sc == error_mark_node)
1374 return sc;
1376 /* Verify that the vector type is suitable for SC. Note that there
1377 is some inconsistency in the type-system with respect to restrict
1378 qualifications of pointers. Vector types always have a main-variant
1379 element type and the qualification is applied to the vector-type.
1380 So TREE_TYPE (vector-type) does not return a properly qualified
1381 vector element-type. */
1382 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1383 TREE_TYPE (vectype)));
1385 if (CONSTANT_CLASS_P (sc))
1387 tree *v = XALLOCAVEC (tree, nunits);
1388 for (i = 0; i < nunits; ++i)
1389 v[i] = sc;
1390 return build_vector (vectype, v);
1392 else
1394 vec<constructor_elt, va_gc> *v;
1395 vec_alloc (v, nunits);
1396 for (i = 0; i < nunits; ++i)
1397 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1398 return build_constructor (vectype, v);
1402 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1403 are in the vec pointed to by VALS. */
1404 tree
1405 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1407 tree c = make_node (CONSTRUCTOR);
1408 unsigned int i;
1409 constructor_elt *elt;
1410 bool constant_p = true;
1411 bool side_effects_p = false;
1413 TREE_TYPE (c) = type;
1414 CONSTRUCTOR_ELTS (c) = vals;
1416 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1418 /* Mostly ctors will have elts that don't have side-effects, so
1419 the usual case is to scan all the elements. Hence a single
1420 loop for both const and side effects, rather than one loop
1421 each (with early outs). */
1422 if (!TREE_CONSTANT (elt->value))
1423 constant_p = false;
1424 if (TREE_SIDE_EFFECTS (elt->value))
1425 side_effects_p = true;
1428 TREE_SIDE_EFFECTS (c) = side_effects_p;
1429 TREE_CONSTANT (c) = constant_p;
1431 return c;
1434 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1435 INDEX and VALUE. */
1436 tree
1437 build_constructor_single (tree type, tree index, tree value)
1439 vec<constructor_elt, va_gc> *v;
1440 constructor_elt elt = {index, value};
1442 vec_alloc (v, 1);
1443 v->quick_push (elt);
1445 return build_constructor (type, v);
1449 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1450 are in a list pointed to by VALS. */
1451 tree
1452 build_constructor_from_list (tree type, tree vals)
1454 tree t;
1455 vec<constructor_elt, va_gc> *v = NULL;
1457 if (vals)
1459 vec_alloc (v, list_length (vals));
1460 for (t = vals; t; t = TREE_CHAIN (t))
1461 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1464 return build_constructor (type, v);
1467 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1469 tree
1470 build_fixed (tree type, FIXED_VALUE_TYPE f)
1472 tree v;
1473 FIXED_VALUE_TYPE *fp;
1475 v = make_node (FIXED_CST);
1476 fp = ggc_alloc_fixed_value ();
1477 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1479 TREE_TYPE (v) = type;
1480 TREE_FIXED_CST_PTR (v) = fp;
1481 return v;
1484 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1486 tree
1487 build_real (tree type, REAL_VALUE_TYPE d)
1489 tree v;
1490 REAL_VALUE_TYPE *dp;
1491 int overflow = 0;
1493 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1494 Consider doing it via real_convert now. */
1496 v = make_node (REAL_CST);
1497 dp = ggc_alloc_real_value ();
1498 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1500 TREE_TYPE (v) = type;
1501 TREE_REAL_CST_PTR (v) = dp;
1502 TREE_OVERFLOW (v) = overflow;
1503 return v;
1506 /* Return a new REAL_CST node whose type is TYPE
1507 and whose value is the integer value of the INTEGER_CST node I. */
1509 REAL_VALUE_TYPE
1510 real_value_from_int_cst (const_tree type, const_tree i)
1512 REAL_VALUE_TYPE d;
1514 /* Clear all bits of the real value type so that we can later do
1515 bitwise comparisons to see if two values are the same. */
1516 memset (&d, 0, sizeof d);
1518 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1519 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1520 TYPE_UNSIGNED (TREE_TYPE (i)));
1521 return d;
1524 /* Given a tree representing an integer constant I, return a tree
1525 representing the same value as a floating-point constant of type TYPE. */
1527 tree
1528 build_real_from_int_cst (tree type, const_tree i)
1530 tree v;
1531 int overflow = TREE_OVERFLOW (i);
1533 v = build_real (type, real_value_from_int_cst (type, i));
1535 TREE_OVERFLOW (v) |= overflow;
1536 return v;
1539 /* Return a newly constructed STRING_CST node whose value is
1540 the LEN characters at STR.
1541 Note that for a C string literal, LEN should include the trailing NUL.
1542 The TREE_TYPE is not initialized. */
1544 tree
1545 build_string (int len, const char *str)
1547 tree s;
1548 size_t length;
1550 /* Do not waste bytes provided by padding of struct tree_string. */
1551 length = len + offsetof (struct tree_string, str) + 1;
1553 record_node_allocation_statistics (STRING_CST, length);
1555 s = ggc_alloc_tree_node (length);
1557 memset (s, 0, sizeof (struct tree_typed));
1558 TREE_SET_CODE (s, STRING_CST);
1559 TREE_CONSTANT (s) = 1;
1560 TREE_STRING_LENGTH (s) = len;
1561 memcpy (s->string.str, str, len);
1562 s->string.str[len] = '\0';
1564 return s;
1567 /* Return a newly constructed COMPLEX_CST node whose value is
1568 specified by the real and imaginary parts REAL and IMAG.
1569 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1570 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1572 tree
1573 build_complex (tree type, tree real, tree imag)
1575 tree t = make_node (COMPLEX_CST);
1577 TREE_REALPART (t) = real;
1578 TREE_IMAGPART (t) = imag;
1579 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1580 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1581 return t;
1584 /* Return a constant of arithmetic type TYPE which is the
1585 multiplicative identity of the set TYPE. */
1587 tree
1588 build_one_cst (tree type)
1590 switch (TREE_CODE (type))
1592 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1593 case POINTER_TYPE: case REFERENCE_TYPE:
1594 case OFFSET_TYPE:
1595 return build_int_cst (type, 1);
1597 case REAL_TYPE:
1598 return build_real (type, dconst1);
1600 case FIXED_POINT_TYPE:
1601 /* We can only generate 1 for accum types. */
1602 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1603 return build_fixed (type, FCONST1(TYPE_MODE (type)));
1605 case VECTOR_TYPE:
1607 tree scalar = build_one_cst (TREE_TYPE (type));
1609 return build_vector_from_val (type, scalar);
1612 case COMPLEX_TYPE:
1613 return build_complex (type,
1614 build_one_cst (TREE_TYPE (type)),
1615 build_zero_cst (TREE_TYPE (type)));
1617 default:
1618 gcc_unreachable ();
1622 /* Return an integer of type TYPE containing all 1's in as much precision as
1623 it contains, or a complex or vector whose subparts are such integers. */
1625 tree
1626 build_all_ones_cst (tree type)
1628 if (TREE_CODE (type) == COMPLEX_TYPE)
1630 tree scalar = build_all_ones_cst (TREE_TYPE (type));
1631 return build_complex (type, scalar, scalar);
1633 else
1634 return build_minus_one_cst (type);
1637 /* Return a constant of arithmetic type TYPE which is the
1638 opposite of the multiplicative identity of the set TYPE. */
1640 tree
1641 build_minus_one_cst (tree type)
1643 switch (TREE_CODE (type))
1645 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1646 case POINTER_TYPE: case REFERENCE_TYPE:
1647 case OFFSET_TYPE:
1648 return build_int_cst (type, -1);
1650 case REAL_TYPE:
1651 return build_real (type, dconstm1);
1653 case FIXED_POINT_TYPE:
1654 /* We can only generate 1 for accum types. */
1655 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1656 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
1657 TYPE_MODE (type)));
1659 case VECTOR_TYPE:
1661 tree scalar = build_minus_one_cst (TREE_TYPE (type));
1663 return build_vector_from_val (type, scalar);
1666 case COMPLEX_TYPE:
1667 return build_complex (type,
1668 build_minus_one_cst (TREE_TYPE (type)),
1669 build_zero_cst (TREE_TYPE (type)));
1671 default:
1672 gcc_unreachable ();
1676 /* Build 0 constant of type TYPE. This is used by constructor folding
1677 and thus the constant should be represented in memory by
1678 zero(es). */
1680 tree
1681 build_zero_cst (tree type)
1683 switch (TREE_CODE (type))
1685 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1686 case POINTER_TYPE: case REFERENCE_TYPE:
1687 case OFFSET_TYPE: case NULLPTR_TYPE:
1688 return build_int_cst (type, 0);
1690 case REAL_TYPE:
1691 return build_real (type, dconst0);
1693 case FIXED_POINT_TYPE:
1694 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
1696 case VECTOR_TYPE:
1698 tree scalar = build_zero_cst (TREE_TYPE (type));
1700 return build_vector_from_val (type, scalar);
1703 case COMPLEX_TYPE:
1705 tree zero = build_zero_cst (TREE_TYPE (type));
1707 return build_complex (type, zero, zero);
1710 default:
1711 if (!AGGREGATE_TYPE_P (type))
1712 return fold_convert (type, integer_zero_node);
1713 return build_constructor (type, NULL);
1718 /* Build a BINFO with LEN language slots. */
1720 tree
1721 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1723 tree t;
1724 size_t length = (offsetof (struct tree_binfo, base_binfos)
1725 + vec<tree, va_gc>::embedded_size (base_binfos));
1727 record_node_allocation_statistics (TREE_BINFO, length);
1729 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1731 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1733 TREE_SET_CODE (t, TREE_BINFO);
1735 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
1737 return t;
1740 /* Create a CASE_LABEL_EXPR tree node and return it. */
1742 tree
1743 build_case_label (tree low_value, tree high_value, tree label_decl)
1745 tree t = make_node (CASE_LABEL_EXPR);
1747 TREE_TYPE (t) = void_type_node;
1748 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
1750 CASE_LOW (t) = low_value;
1751 CASE_HIGH (t) = high_value;
1752 CASE_LABEL (t) = label_decl;
1753 CASE_CHAIN (t) = NULL_TREE;
1755 return t;
1758 /* Build a newly constructed TREE_VEC node of length LEN. */
1760 tree
1761 make_tree_vec_stat (int len MEM_STAT_DECL)
1763 tree t;
1764 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1766 record_node_allocation_statistics (TREE_VEC, length);
1768 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1770 TREE_SET_CODE (t, TREE_VEC);
1771 TREE_VEC_LENGTH (t) = len;
1773 return t;
1776 /* Return 1 if EXPR is the integer constant zero or a complex constant
1777 of zero. */
1780 integer_zerop (const_tree expr)
1782 STRIP_NOPS (expr);
1784 switch (TREE_CODE (expr))
1786 case INTEGER_CST:
1787 return (TREE_INT_CST_LOW (expr) == 0
1788 && TREE_INT_CST_HIGH (expr) == 0);
1789 case COMPLEX_CST:
1790 return (integer_zerop (TREE_REALPART (expr))
1791 && integer_zerop (TREE_IMAGPART (expr)));
1792 case VECTOR_CST:
1794 unsigned i;
1795 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1796 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
1797 return false;
1798 return true;
1800 default:
1801 return false;
1805 /* Return 1 if EXPR is the integer constant one or the corresponding
1806 complex constant. */
1809 integer_onep (const_tree expr)
1811 STRIP_NOPS (expr);
1813 switch (TREE_CODE (expr))
1815 case INTEGER_CST:
1816 return (TREE_INT_CST_LOW (expr) == 1
1817 && TREE_INT_CST_HIGH (expr) == 0);
1818 case COMPLEX_CST:
1819 return (integer_onep (TREE_REALPART (expr))
1820 && integer_zerop (TREE_IMAGPART (expr)));
1821 case VECTOR_CST:
1823 unsigned i;
1824 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1825 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
1826 return false;
1827 return true;
1829 default:
1830 return false;
1834 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1835 it contains. Likewise for the corresponding complex constant. */
1838 integer_all_onesp (const_tree expr)
1840 int prec;
1841 int uns;
1843 STRIP_NOPS (expr);
1845 if (TREE_CODE (expr) == COMPLEX_CST
1846 && integer_all_onesp (TREE_REALPART (expr))
1847 && integer_zerop (TREE_IMAGPART (expr)))
1848 return 1;
1850 else if (TREE_CODE (expr) == VECTOR_CST)
1852 unsigned i;
1853 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1854 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
1855 return 0;
1856 return 1;
1859 else if (TREE_CODE (expr) != INTEGER_CST)
1860 return 0;
1862 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1863 if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1864 && TREE_INT_CST_HIGH (expr) == -1)
1865 return 1;
1866 if (!uns)
1867 return 0;
1869 prec = TYPE_PRECISION (TREE_TYPE (expr));
1870 if (prec >= HOST_BITS_PER_WIDE_INT)
1872 HOST_WIDE_INT high_value;
1873 int shift_amount;
1875 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1877 /* Can not handle precisions greater than twice the host int size. */
1878 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
1879 if (shift_amount == HOST_BITS_PER_WIDE_INT)
1880 /* Shifting by the host word size is undefined according to the ANSI
1881 standard, so we must handle this as a special case. */
1882 high_value = -1;
1883 else
1884 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1886 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1887 && TREE_INT_CST_HIGH (expr) == high_value);
1889 else
1890 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1893 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1894 one bit on). */
1897 integer_pow2p (const_tree expr)
1899 int prec;
1900 unsigned HOST_WIDE_INT high, low;
1902 STRIP_NOPS (expr);
1904 if (TREE_CODE (expr) == COMPLEX_CST
1905 && integer_pow2p (TREE_REALPART (expr))
1906 && integer_zerop (TREE_IMAGPART (expr)))
1907 return 1;
1909 if (TREE_CODE (expr) != INTEGER_CST)
1910 return 0;
1912 prec = TYPE_PRECISION (TREE_TYPE (expr));
1913 high = TREE_INT_CST_HIGH (expr);
1914 low = TREE_INT_CST_LOW (expr);
1916 /* First clear all bits that are beyond the type's precision in case
1917 we've been sign extended. */
1919 if (prec == HOST_BITS_PER_DOUBLE_INT)
1921 else if (prec > HOST_BITS_PER_WIDE_INT)
1922 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1923 else
1925 high = 0;
1926 if (prec < HOST_BITS_PER_WIDE_INT)
1927 low &= ~((HOST_WIDE_INT) (-1) << prec);
1930 if (high == 0 && low == 0)
1931 return 0;
1933 return ((high == 0 && (low & (low - 1)) == 0)
1934 || (low == 0 && (high & (high - 1)) == 0));
1937 /* Return 1 if EXPR is an integer constant other than zero or a
1938 complex constant other than zero. */
1941 integer_nonzerop (const_tree expr)
1943 STRIP_NOPS (expr);
1945 return ((TREE_CODE (expr) == INTEGER_CST
1946 && (TREE_INT_CST_LOW (expr) != 0
1947 || TREE_INT_CST_HIGH (expr) != 0))
1948 || (TREE_CODE (expr) == COMPLEX_CST
1949 && (integer_nonzerop (TREE_REALPART (expr))
1950 || integer_nonzerop (TREE_IMAGPART (expr)))));
1953 /* Return 1 if EXPR is the fixed-point constant zero. */
1956 fixed_zerop (const_tree expr)
1958 return (TREE_CODE (expr) == FIXED_CST
1959 && TREE_FIXED_CST (expr).data.is_zero ());
1962 /* Return the power of two represented by a tree node known to be a
1963 power of two. */
1966 tree_log2 (const_tree expr)
1968 int prec;
1969 HOST_WIDE_INT high, low;
1971 STRIP_NOPS (expr);
1973 if (TREE_CODE (expr) == COMPLEX_CST)
1974 return tree_log2 (TREE_REALPART (expr));
1976 prec = TYPE_PRECISION (TREE_TYPE (expr));
1977 high = TREE_INT_CST_HIGH (expr);
1978 low = TREE_INT_CST_LOW (expr);
1980 /* First clear all bits that are beyond the type's precision in case
1981 we've been sign extended. */
1983 if (prec == HOST_BITS_PER_DOUBLE_INT)
1985 else if (prec > HOST_BITS_PER_WIDE_INT)
1986 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1987 else
1989 high = 0;
1990 if (prec < HOST_BITS_PER_WIDE_INT)
1991 low &= ~((HOST_WIDE_INT) (-1) << prec);
1994 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
1995 : exact_log2 (low));
1998 /* Similar, but return the largest integer Y such that 2 ** Y is less
1999 than or equal to EXPR. */
2002 tree_floor_log2 (const_tree expr)
2004 int prec;
2005 HOST_WIDE_INT high, low;
2007 STRIP_NOPS (expr);
2009 if (TREE_CODE (expr) == COMPLEX_CST)
2010 return tree_log2 (TREE_REALPART (expr));
2012 prec = TYPE_PRECISION (TREE_TYPE (expr));
2013 high = TREE_INT_CST_HIGH (expr);
2014 low = TREE_INT_CST_LOW (expr);
2016 /* First clear all bits that are beyond the type's precision in case
2017 we've been sign extended. Ignore if type's precision hasn't been set
2018 since what we are doing is setting it. */
2020 if (prec == HOST_BITS_PER_DOUBLE_INT || prec == 0)
2022 else if (prec > HOST_BITS_PER_WIDE_INT)
2023 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
2024 else
2026 high = 0;
2027 if (prec < HOST_BITS_PER_WIDE_INT)
2028 low &= ~((HOST_WIDE_INT) (-1) << prec);
2031 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
2032 : floor_log2 (low));
2035 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2036 decimal float constants, so don't return 1 for them. */
2039 real_zerop (const_tree expr)
2041 STRIP_NOPS (expr);
2043 switch (TREE_CODE (expr))
2045 case REAL_CST:
2046 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
2047 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2048 case COMPLEX_CST:
2049 return real_zerop (TREE_REALPART (expr))
2050 && real_zerop (TREE_IMAGPART (expr));
2051 case VECTOR_CST:
2053 unsigned i;
2054 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2055 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2056 return false;
2057 return true;
2059 default:
2060 return false;
2064 /* Return 1 if EXPR is the real constant one in real or complex form.
2065 Trailing zeroes matter for decimal float constants, so don't return
2066 1 for them. */
2069 real_onep (const_tree expr)
2071 STRIP_NOPS (expr);
2073 switch (TREE_CODE (expr))
2075 case REAL_CST:
2076 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2077 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2078 case COMPLEX_CST:
2079 return real_onep (TREE_REALPART (expr))
2080 && real_zerop (TREE_IMAGPART (expr));
2081 case VECTOR_CST:
2083 unsigned i;
2084 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2085 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2086 return false;
2087 return true;
2089 default:
2090 return false;
2094 /* Return 1 if EXPR is the real constant two. Trailing zeroes matter
2095 for decimal float constants, so don't return 1 for them. */
2098 real_twop (const_tree expr)
2100 STRIP_NOPS (expr);
2102 switch (TREE_CODE (expr))
2104 case REAL_CST:
2105 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2)
2106 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2107 case COMPLEX_CST:
2108 return real_twop (TREE_REALPART (expr))
2109 && real_zerop (TREE_IMAGPART (expr));
2110 case VECTOR_CST:
2112 unsigned i;
2113 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2114 if (!real_twop (VECTOR_CST_ELT (expr, i)))
2115 return false;
2116 return true;
2118 default:
2119 return false;
2123 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2124 matter for decimal float constants, so don't return 1 for them. */
2127 real_minus_onep (const_tree expr)
2129 STRIP_NOPS (expr);
2131 switch (TREE_CODE (expr))
2133 case REAL_CST:
2134 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2135 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2136 case COMPLEX_CST:
2137 return real_minus_onep (TREE_REALPART (expr))
2138 && real_zerop (TREE_IMAGPART (expr));
2139 case VECTOR_CST:
2141 unsigned i;
2142 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2143 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2144 return false;
2145 return true;
2147 default:
2148 return false;
2152 /* Nonzero if EXP is a constant or a cast of a constant. */
2155 really_constant_p (const_tree exp)
2157 /* This is not quite the same as STRIP_NOPS. It does more. */
2158 while (CONVERT_EXPR_P (exp)
2159 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2160 exp = TREE_OPERAND (exp, 0);
2161 return TREE_CONSTANT (exp);
2164 /* Return first list element whose TREE_VALUE is ELEM.
2165 Return 0 if ELEM is not in LIST. */
2167 tree
2168 value_member (tree elem, tree list)
2170 while (list)
2172 if (elem == TREE_VALUE (list))
2173 return list;
2174 list = TREE_CHAIN (list);
2176 return NULL_TREE;
2179 /* Return first list element whose TREE_PURPOSE is ELEM.
2180 Return 0 if ELEM is not in LIST. */
2182 tree
2183 purpose_member (const_tree elem, tree list)
2185 while (list)
2187 if (elem == TREE_PURPOSE (list))
2188 return list;
2189 list = TREE_CHAIN (list);
2191 return NULL_TREE;
2194 /* Return true if ELEM is in V. */
2196 bool
2197 vec_member (const_tree elem, vec<tree, va_gc> *v)
2199 unsigned ix;
2200 tree t;
2201 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2202 if (elem == t)
2203 return true;
2204 return false;
2207 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2208 NULL_TREE. */
2210 tree
2211 chain_index (int idx, tree chain)
2213 for (; chain && idx > 0; --idx)
2214 chain = TREE_CHAIN (chain);
2215 return chain;
2218 /* Return nonzero if ELEM is part of the chain CHAIN. */
2221 chain_member (const_tree elem, const_tree chain)
2223 while (chain)
2225 if (elem == chain)
2226 return 1;
2227 chain = DECL_CHAIN (chain);
2230 return 0;
2233 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2234 We expect a null pointer to mark the end of the chain.
2235 This is the Lisp primitive `length'. */
2238 list_length (const_tree t)
2240 const_tree p = t;
2241 #ifdef ENABLE_TREE_CHECKING
2242 const_tree q = t;
2243 #endif
2244 int len = 0;
2246 while (p)
2248 p = TREE_CHAIN (p);
2249 #ifdef ENABLE_TREE_CHECKING
2250 if (len % 2)
2251 q = TREE_CHAIN (q);
2252 gcc_assert (p != q);
2253 #endif
2254 len++;
2257 return len;
2260 /* Returns the number of FIELD_DECLs in TYPE. */
2263 fields_length (const_tree type)
2265 tree t = TYPE_FIELDS (type);
2266 int count = 0;
2268 for (; t; t = DECL_CHAIN (t))
2269 if (TREE_CODE (t) == FIELD_DECL)
2270 ++count;
2272 return count;
2275 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2276 UNION_TYPE TYPE, or NULL_TREE if none. */
2278 tree
2279 first_field (const_tree type)
2281 tree t = TYPE_FIELDS (type);
2282 while (t && TREE_CODE (t) != FIELD_DECL)
2283 t = TREE_CHAIN (t);
2284 return t;
2287 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2288 by modifying the last node in chain 1 to point to chain 2.
2289 This is the Lisp primitive `nconc'. */
2291 tree
2292 chainon (tree op1, tree op2)
2294 tree t1;
2296 if (!op1)
2297 return op2;
2298 if (!op2)
2299 return op1;
2301 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2302 continue;
2303 TREE_CHAIN (t1) = op2;
2305 #ifdef ENABLE_TREE_CHECKING
2307 tree t2;
2308 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2309 gcc_assert (t2 != t1);
2311 #endif
2313 return op1;
2316 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2318 tree
2319 tree_last (tree chain)
2321 tree next;
2322 if (chain)
2323 while ((next = TREE_CHAIN (chain)))
2324 chain = next;
2325 return chain;
2328 /* Reverse the order of elements in the chain T,
2329 and return the new head of the chain (old last element). */
2331 tree
2332 nreverse (tree t)
2334 tree prev = 0, decl, next;
2335 for (decl = t; decl; decl = next)
2337 /* We shouldn't be using this function to reverse BLOCK chains; we
2338 have blocks_nreverse for that. */
2339 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2340 next = TREE_CHAIN (decl);
2341 TREE_CHAIN (decl) = prev;
2342 prev = decl;
2344 return prev;
2347 /* Return a newly created TREE_LIST node whose
2348 purpose and value fields are PARM and VALUE. */
2350 tree
2351 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2353 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2354 TREE_PURPOSE (t) = parm;
2355 TREE_VALUE (t) = value;
2356 return t;
2359 /* Build a chain of TREE_LIST nodes from a vector. */
2361 tree
2362 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2364 tree ret = NULL_TREE;
2365 tree *pp = &ret;
2366 unsigned int i;
2367 tree t;
2368 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2370 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2371 pp = &TREE_CHAIN (*pp);
2373 return ret;
2376 /* Return a newly created TREE_LIST node whose
2377 purpose and value fields are PURPOSE and VALUE
2378 and whose TREE_CHAIN is CHAIN. */
2380 tree
2381 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2383 tree node;
2385 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2386 memset (node, 0, sizeof (struct tree_common));
2388 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2390 TREE_SET_CODE (node, TREE_LIST);
2391 TREE_CHAIN (node) = chain;
2392 TREE_PURPOSE (node) = purpose;
2393 TREE_VALUE (node) = value;
2394 return node;
2397 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2398 trees. */
2400 vec<tree, va_gc> *
2401 ctor_to_vec (tree ctor)
2403 vec<tree, va_gc> *vec;
2404 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2405 unsigned int ix;
2406 tree val;
2408 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2409 vec->quick_push (val);
2411 return vec;
2414 /* Return the size nominally occupied by an object of type TYPE
2415 when it resides in memory. The value is measured in units of bytes,
2416 and its data type is that normally used for type sizes
2417 (which is the first type created by make_signed_type or
2418 make_unsigned_type). */
2420 tree
2421 size_in_bytes (const_tree type)
2423 tree t;
2425 if (type == error_mark_node)
2426 return integer_zero_node;
2428 type = TYPE_MAIN_VARIANT (type);
2429 t = TYPE_SIZE_UNIT (type);
2431 if (t == 0)
2433 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2434 return size_zero_node;
2437 return t;
2440 /* Return the size of TYPE (in bytes) as a wide integer
2441 or return -1 if the size can vary or is larger than an integer. */
2443 HOST_WIDE_INT
2444 int_size_in_bytes (const_tree type)
2446 tree t;
2448 if (type == error_mark_node)
2449 return 0;
2451 type = TYPE_MAIN_VARIANT (type);
2452 t = TYPE_SIZE_UNIT (type);
2453 if (t == 0
2454 || TREE_CODE (t) != INTEGER_CST
2455 || TREE_INT_CST_HIGH (t) != 0
2456 /* If the result would appear negative, it's too big to represent. */
2457 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
2458 return -1;
2460 return TREE_INT_CST_LOW (t);
2463 /* Return the maximum size of TYPE (in bytes) as a wide integer
2464 or return -1 if the size can vary or is larger than an integer. */
2466 HOST_WIDE_INT
2467 max_int_size_in_bytes (const_tree type)
2469 HOST_WIDE_INT size = -1;
2470 tree size_tree;
2472 /* If this is an array type, check for a possible MAX_SIZE attached. */
2474 if (TREE_CODE (type) == ARRAY_TYPE)
2476 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2478 if (size_tree && host_integerp (size_tree, 1))
2479 size = tree_low_cst (size_tree, 1);
2482 /* If we still haven't been able to get a size, see if the language
2483 can compute a maximum size. */
2485 if (size == -1)
2487 size_tree = lang_hooks.types.max_size (type);
2489 if (size_tree && host_integerp (size_tree, 1))
2490 size = tree_low_cst (size_tree, 1);
2493 return size;
2496 /* Returns a tree for the size of EXP in bytes. */
2498 tree
2499 tree_expr_size (const_tree exp)
2501 if (DECL_P (exp)
2502 && DECL_SIZE_UNIT (exp) != 0)
2503 return DECL_SIZE_UNIT (exp);
2504 else
2505 return size_in_bytes (TREE_TYPE (exp));
2508 /* Return the bit position of FIELD, in bits from the start of the record.
2509 This is a tree of type bitsizetype. */
2511 tree
2512 bit_position (const_tree field)
2514 return bit_from_pos (DECL_FIELD_OFFSET (field),
2515 DECL_FIELD_BIT_OFFSET (field));
2518 /* Likewise, but return as an integer. It must be representable in
2519 that way (since it could be a signed value, we don't have the
2520 option of returning -1 like int_size_in_byte can. */
2522 HOST_WIDE_INT
2523 int_bit_position (const_tree field)
2525 return tree_low_cst (bit_position (field), 0);
2528 /* Return the byte position of FIELD, in bytes from the start of the record.
2529 This is a tree of type sizetype. */
2531 tree
2532 byte_position (const_tree field)
2534 return byte_from_pos (DECL_FIELD_OFFSET (field),
2535 DECL_FIELD_BIT_OFFSET (field));
2538 /* Likewise, but return as an integer. It must be representable in
2539 that way (since it could be a signed value, we don't have the
2540 option of returning -1 like int_size_in_byte can. */
2542 HOST_WIDE_INT
2543 int_byte_position (const_tree field)
2545 return tree_low_cst (byte_position (field), 0);
2548 /* Return the strictest alignment, in bits, that T is known to have. */
2550 unsigned int
2551 expr_align (const_tree t)
2553 unsigned int align0, align1;
2555 switch (TREE_CODE (t))
2557 CASE_CONVERT: case NON_LVALUE_EXPR:
2558 /* If we have conversions, we know that the alignment of the
2559 object must meet each of the alignments of the types. */
2560 align0 = expr_align (TREE_OPERAND (t, 0));
2561 align1 = TYPE_ALIGN (TREE_TYPE (t));
2562 return MAX (align0, align1);
2564 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2565 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2566 case CLEANUP_POINT_EXPR:
2567 /* These don't change the alignment of an object. */
2568 return expr_align (TREE_OPERAND (t, 0));
2570 case COND_EXPR:
2571 /* The best we can do is say that the alignment is the least aligned
2572 of the two arms. */
2573 align0 = expr_align (TREE_OPERAND (t, 1));
2574 align1 = expr_align (TREE_OPERAND (t, 2));
2575 return MIN (align0, align1);
2577 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2578 meaningfully, it's always 1. */
2579 case LABEL_DECL: case CONST_DECL:
2580 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2581 case FUNCTION_DECL:
2582 gcc_assert (DECL_ALIGN (t) != 0);
2583 return DECL_ALIGN (t);
2585 default:
2586 break;
2589 /* Otherwise take the alignment from that of the type. */
2590 return TYPE_ALIGN (TREE_TYPE (t));
2593 /* Return, as a tree node, the number of elements for TYPE (which is an
2594 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2596 tree
2597 array_type_nelts (const_tree type)
2599 tree index_type, min, max;
2601 /* If they did it with unspecified bounds, then we should have already
2602 given an error about it before we got here. */
2603 if (! TYPE_DOMAIN (type))
2604 return error_mark_node;
2606 index_type = TYPE_DOMAIN (type);
2607 min = TYPE_MIN_VALUE (index_type);
2608 max = TYPE_MAX_VALUE (index_type);
2610 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
2611 if (!max)
2612 return error_mark_node;
2614 return (integer_zerop (min)
2615 ? max
2616 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2619 /* If arg is static -- a reference to an object in static storage -- then
2620 return the object. This is not the same as the C meaning of `static'.
2621 If arg isn't static, return NULL. */
2623 tree
2624 staticp (tree arg)
2626 switch (TREE_CODE (arg))
2628 case FUNCTION_DECL:
2629 /* Nested functions are static, even though taking their address will
2630 involve a trampoline as we unnest the nested function and create
2631 the trampoline on the tree level. */
2632 return arg;
2634 case VAR_DECL:
2635 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2636 && ! DECL_THREAD_LOCAL_P (arg)
2637 && ! DECL_DLLIMPORT_P (arg)
2638 ? arg : NULL);
2640 case CONST_DECL:
2641 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2642 ? arg : NULL);
2644 case CONSTRUCTOR:
2645 return TREE_STATIC (arg) ? arg : NULL;
2647 case LABEL_DECL:
2648 case STRING_CST:
2649 return arg;
2651 case COMPONENT_REF:
2652 /* If the thing being referenced is not a field, then it is
2653 something language specific. */
2654 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2656 /* If we are referencing a bitfield, we can't evaluate an
2657 ADDR_EXPR at compile time and so it isn't a constant. */
2658 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2659 return NULL;
2661 return staticp (TREE_OPERAND (arg, 0));
2663 case BIT_FIELD_REF:
2664 return NULL;
2666 case INDIRECT_REF:
2667 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2669 case ARRAY_REF:
2670 case ARRAY_RANGE_REF:
2671 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2672 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2673 return staticp (TREE_OPERAND (arg, 0));
2674 else
2675 return NULL;
2677 case COMPOUND_LITERAL_EXPR:
2678 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2680 default:
2681 return NULL;
2688 /* Return whether OP is a DECL whose address is function-invariant. */
2690 bool
2691 decl_address_invariant_p (const_tree op)
2693 /* The conditions below are slightly less strict than the one in
2694 staticp. */
2696 switch (TREE_CODE (op))
2698 case PARM_DECL:
2699 case RESULT_DECL:
2700 case LABEL_DECL:
2701 case FUNCTION_DECL:
2702 return true;
2704 case VAR_DECL:
2705 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2706 || DECL_THREAD_LOCAL_P (op)
2707 || DECL_CONTEXT (op) == current_function_decl
2708 || decl_function_context (op) == current_function_decl)
2709 return true;
2710 break;
2712 case CONST_DECL:
2713 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2714 || decl_function_context (op) == current_function_decl)
2715 return true;
2716 break;
2718 default:
2719 break;
2722 return false;
2725 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2727 bool
2728 decl_address_ip_invariant_p (const_tree op)
2730 /* The conditions below are slightly less strict than the one in
2731 staticp. */
2733 switch (TREE_CODE (op))
2735 case LABEL_DECL:
2736 case FUNCTION_DECL:
2737 case STRING_CST:
2738 return true;
2740 case VAR_DECL:
2741 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2742 && !DECL_DLLIMPORT_P (op))
2743 || DECL_THREAD_LOCAL_P (op))
2744 return true;
2745 break;
2747 case CONST_DECL:
2748 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2749 return true;
2750 break;
2752 default:
2753 break;
2756 return false;
2760 /* Return true if T is function-invariant (internal function, does
2761 not handle arithmetic; that's handled in skip_simple_arithmetic and
2762 tree_invariant_p). */
2764 static bool tree_invariant_p (tree t);
2766 static bool
2767 tree_invariant_p_1 (tree t)
2769 tree op;
2771 if (TREE_CONSTANT (t)
2772 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2773 return true;
2775 switch (TREE_CODE (t))
2777 case SAVE_EXPR:
2778 return true;
2780 case ADDR_EXPR:
2781 op = TREE_OPERAND (t, 0);
2782 while (handled_component_p (op))
2784 switch (TREE_CODE (op))
2786 case ARRAY_REF:
2787 case ARRAY_RANGE_REF:
2788 if (!tree_invariant_p (TREE_OPERAND (op, 1))
2789 || TREE_OPERAND (op, 2) != NULL_TREE
2790 || TREE_OPERAND (op, 3) != NULL_TREE)
2791 return false;
2792 break;
2794 case COMPONENT_REF:
2795 if (TREE_OPERAND (op, 2) != NULL_TREE)
2796 return false;
2797 break;
2799 default:;
2801 op = TREE_OPERAND (op, 0);
2804 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2806 default:
2807 break;
2810 return false;
2813 /* Return true if T is function-invariant. */
2815 static bool
2816 tree_invariant_p (tree t)
2818 tree inner = skip_simple_arithmetic (t);
2819 return tree_invariant_p_1 (inner);
2822 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2823 Do this to any expression which may be used in more than one place,
2824 but must be evaluated only once.
2826 Normally, expand_expr would reevaluate the expression each time.
2827 Calling save_expr produces something that is evaluated and recorded
2828 the first time expand_expr is called on it. Subsequent calls to
2829 expand_expr just reuse the recorded value.
2831 The call to expand_expr that generates code that actually computes
2832 the value is the first call *at compile time*. Subsequent calls
2833 *at compile time* generate code to use the saved value.
2834 This produces correct result provided that *at run time* control
2835 always flows through the insns made by the first expand_expr
2836 before reaching the other places where the save_expr was evaluated.
2837 You, the caller of save_expr, must make sure this is so.
2839 Constants, and certain read-only nodes, are returned with no
2840 SAVE_EXPR because that is safe. Expressions containing placeholders
2841 are not touched; see tree.def for an explanation of what these
2842 are used for. */
2844 tree
2845 save_expr (tree expr)
2847 tree t = fold (expr);
2848 tree inner;
2850 /* If the tree evaluates to a constant, then we don't want to hide that
2851 fact (i.e. this allows further folding, and direct checks for constants).
2852 However, a read-only object that has side effects cannot be bypassed.
2853 Since it is no problem to reevaluate literals, we just return the
2854 literal node. */
2855 inner = skip_simple_arithmetic (t);
2856 if (TREE_CODE (inner) == ERROR_MARK)
2857 return inner;
2859 if (tree_invariant_p_1 (inner))
2860 return t;
2862 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2863 it means that the size or offset of some field of an object depends on
2864 the value within another field.
2866 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2867 and some variable since it would then need to be both evaluated once and
2868 evaluated more than once. Front-ends must assure this case cannot
2869 happen by surrounding any such subexpressions in their own SAVE_EXPR
2870 and forcing evaluation at the proper time. */
2871 if (contains_placeholder_p (inner))
2872 return t;
2874 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
2875 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
2877 /* This expression might be placed ahead of a jump to ensure that the
2878 value was computed on both sides of the jump. So make sure it isn't
2879 eliminated as dead. */
2880 TREE_SIDE_EFFECTS (t) = 1;
2881 return t;
2884 /* Look inside EXPR and into any simple arithmetic operations. Return
2885 the innermost non-arithmetic node. */
2887 tree
2888 skip_simple_arithmetic (tree expr)
2890 tree inner;
2892 /* We don't care about whether this can be used as an lvalue in this
2893 context. */
2894 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
2895 expr = TREE_OPERAND (expr, 0);
2897 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
2898 a constant, it will be more efficient to not make another SAVE_EXPR since
2899 it will allow better simplification and GCSE will be able to merge the
2900 computations if they actually occur. */
2901 inner = expr;
2902 while (1)
2904 if (UNARY_CLASS_P (inner))
2905 inner = TREE_OPERAND (inner, 0);
2906 else if (BINARY_CLASS_P (inner))
2908 if (tree_invariant_p (TREE_OPERAND (inner, 1)))
2909 inner = TREE_OPERAND (inner, 0);
2910 else if (tree_invariant_p (TREE_OPERAND (inner, 0)))
2911 inner = TREE_OPERAND (inner, 1);
2912 else
2913 break;
2915 else
2916 break;
2919 return inner;
2923 /* Return which tree structure is used by T. */
2925 enum tree_node_structure_enum
2926 tree_node_structure (const_tree t)
2928 const enum tree_code code = TREE_CODE (t);
2929 return tree_node_structure_for_code (code);
2932 /* Set various status flags when building a CALL_EXPR object T. */
2934 static void
2935 process_call_operands (tree t)
2937 bool side_effects = TREE_SIDE_EFFECTS (t);
2938 bool read_only = false;
2939 int i = call_expr_flags (t);
2941 /* Calls have side-effects, except those to const or pure functions. */
2942 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
2943 side_effects = true;
2944 /* Propagate TREE_READONLY of arguments for const functions. */
2945 if (i & ECF_CONST)
2946 read_only = true;
2948 if (!side_effects || read_only)
2949 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
2951 tree op = TREE_OPERAND (t, i);
2952 if (op && TREE_SIDE_EFFECTS (op))
2953 side_effects = true;
2954 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
2955 read_only = false;
2958 TREE_SIDE_EFFECTS (t) = side_effects;
2959 TREE_READONLY (t) = read_only;
2962 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
2963 size or offset that depends on a field within a record. */
2965 bool
2966 contains_placeholder_p (const_tree exp)
2968 enum tree_code code;
2970 if (!exp)
2971 return 0;
2973 code = TREE_CODE (exp);
2974 if (code == PLACEHOLDER_EXPR)
2975 return 1;
2977 switch (TREE_CODE_CLASS (code))
2979 case tcc_reference:
2980 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
2981 position computations since they will be converted into a
2982 WITH_RECORD_EXPR involving the reference, which will assume
2983 here will be valid. */
2984 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2986 case tcc_exceptional:
2987 if (code == TREE_LIST)
2988 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
2989 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
2990 break;
2992 case tcc_unary:
2993 case tcc_binary:
2994 case tcc_comparison:
2995 case tcc_expression:
2996 switch (code)
2998 case COMPOUND_EXPR:
2999 /* Ignoring the first operand isn't quite right, but works best. */
3000 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3002 case COND_EXPR:
3003 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3004 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3005 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3007 case SAVE_EXPR:
3008 /* The save_expr function never wraps anything containing
3009 a PLACEHOLDER_EXPR. */
3010 return 0;
3012 default:
3013 break;
3016 switch (TREE_CODE_LENGTH (code))
3018 case 1:
3019 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3020 case 2:
3021 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3022 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3023 default:
3024 return 0;
3027 case tcc_vl_exp:
3028 switch (code)
3030 case CALL_EXPR:
3032 const_tree arg;
3033 const_call_expr_arg_iterator iter;
3034 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3035 if (CONTAINS_PLACEHOLDER_P (arg))
3036 return 1;
3037 return 0;
3039 default:
3040 return 0;
3043 default:
3044 return 0;
3046 return 0;
3049 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3050 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3051 field positions. */
3053 static bool
3054 type_contains_placeholder_1 (const_tree type)
3056 /* If the size contains a placeholder or the parent type (component type in
3057 the case of arrays) type involves a placeholder, this type does. */
3058 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3059 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3060 || (!POINTER_TYPE_P (type)
3061 && TREE_TYPE (type)
3062 && type_contains_placeholder_p (TREE_TYPE (type))))
3063 return true;
3065 /* Now do type-specific checks. Note that the last part of the check above
3066 greatly limits what we have to do below. */
3067 switch (TREE_CODE (type))
3069 case VOID_TYPE:
3070 case COMPLEX_TYPE:
3071 case ENUMERAL_TYPE:
3072 case BOOLEAN_TYPE:
3073 case POINTER_TYPE:
3074 case OFFSET_TYPE:
3075 case REFERENCE_TYPE:
3076 case METHOD_TYPE:
3077 case FUNCTION_TYPE:
3078 case VECTOR_TYPE:
3079 case NULLPTR_TYPE:
3080 return false;
3082 case INTEGER_TYPE:
3083 case REAL_TYPE:
3084 case FIXED_POINT_TYPE:
3085 /* Here we just check the bounds. */
3086 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3087 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3089 case ARRAY_TYPE:
3090 /* We have already checked the component type above, so just check the
3091 domain type. */
3092 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3094 case RECORD_TYPE:
3095 case UNION_TYPE:
3096 case QUAL_UNION_TYPE:
3098 tree field;
3100 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3101 if (TREE_CODE (field) == FIELD_DECL
3102 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3103 || (TREE_CODE (type) == QUAL_UNION_TYPE
3104 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3105 || type_contains_placeholder_p (TREE_TYPE (field))))
3106 return true;
3108 return false;
3111 default:
3112 gcc_unreachable ();
3116 /* Wrapper around above function used to cache its result. */
3118 bool
3119 type_contains_placeholder_p (tree type)
3121 bool result;
3123 /* If the contains_placeholder_bits field has been initialized,
3124 then we know the answer. */
3125 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3126 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3128 /* Indicate that we've seen this type node, and the answer is false.
3129 This is what we want to return if we run into recursion via fields. */
3130 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3132 /* Compute the real value. */
3133 result = type_contains_placeholder_1 (type);
3135 /* Store the real value. */
3136 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3138 return result;
3141 /* Push tree EXP onto vector QUEUE if it is not already present. */
3143 static void
3144 push_without_duplicates (tree exp, vec<tree> *queue)
3146 unsigned int i;
3147 tree iter;
3149 FOR_EACH_VEC_ELT (*queue, i, iter)
3150 if (simple_cst_equal (iter, exp) == 1)
3151 break;
3153 if (!iter)
3154 queue->safe_push (exp);
3157 /* Given a tree EXP, find all occurrences of references to fields
3158 in a PLACEHOLDER_EXPR and place them in vector REFS without
3159 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3160 we assume here that EXP contains only arithmetic expressions
3161 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3162 argument list. */
3164 void
3165 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3167 enum tree_code code = TREE_CODE (exp);
3168 tree inner;
3169 int i;
3171 /* We handle TREE_LIST and COMPONENT_REF separately. */
3172 if (code == TREE_LIST)
3174 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3175 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3177 else if (code == COMPONENT_REF)
3179 for (inner = TREE_OPERAND (exp, 0);
3180 REFERENCE_CLASS_P (inner);
3181 inner = TREE_OPERAND (inner, 0))
3184 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3185 push_without_duplicates (exp, refs);
3186 else
3187 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3189 else
3190 switch (TREE_CODE_CLASS (code))
3192 case tcc_constant:
3193 break;
3195 case tcc_declaration:
3196 /* Variables allocated to static storage can stay. */
3197 if (!TREE_STATIC (exp))
3198 push_without_duplicates (exp, refs);
3199 break;
3201 case tcc_expression:
3202 /* This is the pattern built in ada/make_aligning_type. */
3203 if (code == ADDR_EXPR
3204 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3206 push_without_duplicates (exp, refs);
3207 break;
3210 /* Fall through... */
3212 case tcc_exceptional:
3213 case tcc_unary:
3214 case tcc_binary:
3215 case tcc_comparison:
3216 case tcc_reference:
3217 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3218 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3219 break;
3221 case tcc_vl_exp:
3222 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3223 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3224 break;
3226 default:
3227 gcc_unreachable ();
3231 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3232 return a tree with all occurrences of references to F in a
3233 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3234 CONST_DECLs. Note that we assume here that EXP contains only
3235 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3236 occurring only in their argument list. */
3238 tree
3239 substitute_in_expr (tree exp, tree f, tree r)
3241 enum tree_code code = TREE_CODE (exp);
3242 tree op0, op1, op2, op3;
3243 tree new_tree;
3245 /* We handle TREE_LIST and COMPONENT_REF separately. */
3246 if (code == TREE_LIST)
3248 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3249 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3250 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3251 return exp;
3253 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3255 else if (code == COMPONENT_REF)
3257 tree inner;
3259 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3260 and it is the right field, replace it with R. */
3261 for (inner = TREE_OPERAND (exp, 0);
3262 REFERENCE_CLASS_P (inner);
3263 inner = TREE_OPERAND (inner, 0))
3266 /* The field. */
3267 op1 = TREE_OPERAND (exp, 1);
3269 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3270 return r;
3272 /* If this expression hasn't been completed let, leave it alone. */
3273 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3274 return exp;
3276 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3277 if (op0 == TREE_OPERAND (exp, 0))
3278 return exp;
3280 new_tree
3281 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3283 else
3284 switch (TREE_CODE_CLASS (code))
3286 case tcc_constant:
3287 return exp;
3289 case tcc_declaration:
3290 if (exp == f)
3291 return r;
3292 else
3293 return exp;
3295 case tcc_expression:
3296 if (exp == f)
3297 return r;
3299 /* Fall through... */
3301 case tcc_exceptional:
3302 case tcc_unary:
3303 case tcc_binary:
3304 case tcc_comparison:
3305 case tcc_reference:
3306 switch (TREE_CODE_LENGTH (code))
3308 case 0:
3309 return exp;
3311 case 1:
3312 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3313 if (op0 == TREE_OPERAND (exp, 0))
3314 return exp;
3316 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3317 break;
3319 case 2:
3320 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3321 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3323 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3324 return exp;
3326 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3327 break;
3329 case 3:
3330 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3331 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3332 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3334 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3335 && op2 == TREE_OPERAND (exp, 2))
3336 return exp;
3338 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3339 break;
3341 case 4:
3342 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3343 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3344 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3345 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3347 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3348 && op2 == TREE_OPERAND (exp, 2)
3349 && op3 == TREE_OPERAND (exp, 3))
3350 return exp;
3352 new_tree
3353 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3354 break;
3356 default:
3357 gcc_unreachable ();
3359 break;
3361 case tcc_vl_exp:
3363 int i;
3365 new_tree = NULL_TREE;
3367 /* If we are trying to replace F with a constant, inline back
3368 functions which do nothing else than computing a value from
3369 the arguments they are passed. This makes it possible to
3370 fold partially or entirely the replacement expression. */
3371 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3373 tree t = maybe_inline_call_in_expr (exp);
3374 if (t)
3375 return SUBSTITUTE_IN_EXPR (t, f, r);
3378 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3380 tree op = TREE_OPERAND (exp, i);
3381 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3382 if (new_op != op)
3384 if (!new_tree)
3385 new_tree = copy_node (exp);
3386 TREE_OPERAND (new_tree, i) = new_op;
3390 if (new_tree)
3392 new_tree = fold (new_tree);
3393 if (TREE_CODE (new_tree) == CALL_EXPR)
3394 process_call_operands (new_tree);
3396 else
3397 return exp;
3399 break;
3401 default:
3402 gcc_unreachable ();
3405 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3407 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3408 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3410 return new_tree;
3413 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3414 for it within OBJ, a tree that is an object or a chain of references. */
3416 tree
3417 substitute_placeholder_in_expr (tree exp, tree obj)
3419 enum tree_code code = TREE_CODE (exp);
3420 tree op0, op1, op2, op3;
3421 tree new_tree;
3423 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3424 in the chain of OBJ. */
3425 if (code == PLACEHOLDER_EXPR)
3427 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3428 tree elt;
3430 for (elt = obj; elt != 0;
3431 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3432 || TREE_CODE (elt) == COND_EXPR)
3433 ? TREE_OPERAND (elt, 1)
3434 : (REFERENCE_CLASS_P (elt)
3435 || UNARY_CLASS_P (elt)
3436 || BINARY_CLASS_P (elt)
3437 || VL_EXP_CLASS_P (elt)
3438 || EXPRESSION_CLASS_P (elt))
3439 ? TREE_OPERAND (elt, 0) : 0))
3440 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3441 return elt;
3443 for (elt = obj; elt != 0;
3444 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3445 || TREE_CODE (elt) == COND_EXPR)
3446 ? TREE_OPERAND (elt, 1)
3447 : (REFERENCE_CLASS_P (elt)
3448 || UNARY_CLASS_P (elt)
3449 || BINARY_CLASS_P (elt)
3450 || VL_EXP_CLASS_P (elt)
3451 || EXPRESSION_CLASS_P (elt))
3452 ? TREE_OPERAND (elt, 0) : 0))
3453 if (POINTER_TYPE_P (TREE_TYPE (elt))
3454 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3455 == need_type))
3456 return fold_build1 (INDIRECT_REF, need_type, elt);
3458 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3459 survives until RTL generation, there will be an error. */
3460 return exp;
3463 /* TREE_LIST is special because we need to look at TREE_VALUE
3464 and TREE_CHAIN, not TREE_OPERANDS. */
3465 else if (code == TREE_LIST)
3467 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3468 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3469 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3470 return exp;
3472 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3474 else
3475 switch (TREE_CODE_CLASS (code))
3477 case tcc_constant:
3478 case tcc_declaration:
3479 return exp;
3481 case tcc_exceptional:
3482 case tcc_unary:
3483 case tcc_binary:
3484 case tcc_comparison:
3485 case tcc_expression:
3486 case tcc_reference:
3487 case tcc_statement:
3488 switch (TREE_CODE_LENGTH (code))
3490 case 0:
3491 return exp;
3493 case 1:
3494 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3495 if (op0 == TREE_OPERAND (exp, 0))
3496 return exp;
3498 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3499 break;
3501 case 2:
3502 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3503 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3505 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3506 return exp;
3508 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3509 break;
3511 case 3:
3512 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3513 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3514 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3516 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3517 && op2 == TREE_OPERAND (exp, 2))
3518 return exp;
3520 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3521 break;
3523 case 4:
3524 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3525 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3526 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3527 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3529 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3530 && op2 == TREE_OPERAND (exp, 2)
3531 && op3 == TREE_OPERAND (exp, 3))
3532 return exp;
3534 new_tree
3535 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3536 break;
3538 default:
3539 gcc_unreachable ();
3541 break;
3543 case tcc_vl_exp:
3545 int i;
3547 new_tree = NULL_TREE;
3549 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3551 tree op = TREE_OPERAND (exp, i);
3552 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3553 if (new_op != op)
3555 if (!new_tree)
3556 new_tree = copy_node (exp);
3557 TREE_OPERAND (new_tree, i) = new_op;
3561 if (new_tree)
3563 new_tree = fold (new_tree);
3564 if (TREE_CODE (new_tree) == CALL_EXPR)
3565 process_call_operands (new_tree);
3567 else
3568 return exp;
3570 break;
3572 default:
3573 gcc_unreachable ();
3576 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3578 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3579 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3581 return new_tree;
3584 /* Stabilize a reference so that we can use it any number of times
3585 without causing its operands to be evaluated more than once.
3586 Returns the stabilized reference. This works by means of save_expr,
3587 so see the caveats in the comments about save_expr.
3589 Also allows conversion expressions whose operands are references.
3590 Any other kind of expression is returned unchanged. */
3592 tree
3593 stabilize_reference (tree ref)
3595 tree result;
3596 enum tree_code code = TREE_CODE (ref);
3598 switch (code)
3600 case VAR_DECL:
3601 case PARM_DECL:
3602 case RESULT_DECL:
3603 /* No action is needed in this case. */
3604 return ref;
3606 CASE_CONVERT:
3607 case FLOAT_EXPR:
3608 case FIX_TRUNC_EXPR:
3609 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
3610 break;
3612 case INDIRECT_REF:
3613 result = build_nt (INDIRECT_REF,
3614 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
3615 break;
3617 case COMPONENT_REF:
3618 result = build_nt (COMPONENT_REF,
3619 stabilize_reference (TREE_OPERAND (ref, 0)),
3620 TREE_OPERAND (ref, 1), NULL_TREE);
3621 break;
3623 case BIT_FIELD_REF:
3624 result = build_nt (BIT_FIELD_REF,
3625 stabilize_reference (TREE_OPERAND (ref, 0)),
3626 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
3627 break;
3629 case ARRAY_REF:
3630 result = build_nt (ARRAY_REF,
3631 stabilize_reference (TREE_OPERAND (ref, 0)),
3632 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3633 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3634 break;
3636 case ARRAY_RANGE_REF:
3637 result = build_nt (ARRAY_RANGE_REF,
3638 stabilize_reference (TREE_OPERAND (ref, 0)),
3639 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3640 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3641 break;
3643 case COMPOUND_EXPR:
3644 /* We cannot wrap the first expression in a SAVE_EXPR, as then
3645 it wouldn't be ignored. This matters when dealing with
3646 volatiles. */
3647 return stabilize_reference_1 (ref);
3649 /* If arg isn't a kind of lvalue we recognize, make no change.
3650 Caller should recognize the error for an invalid lvalue. */
3651 default:
3652 return ref;
3654 case ERROR_MARK:
3655 return error_mark_node;
3658 TREE_TYPE (result) = TREE_TYPE (ref);
3659 TREE_READONLY (result) = TREE_READONLY (ref);
3660 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
3661 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3663 return result;
3666 /* Subroutine of stabilize_reference; this is called for subtrees of
3667 references. Any expression with side-effects must be put in a SAVE_EXPR
3668 to ensure that it is only evaluated once.
3670 We don't put SAVE_EXPR nodes around everything, because assigning very
3671 simple expressions to temporaries causes us to miss good opportunities
3672 for optimizations. Among other things, the opportunity to fold in the
3673 addition of a constant into an addressing mode often gets lost, e.g.
3674 "y[i+1] += x;". In general, we take the approach that we should not make
3675 an assignment unless we are forced into it - i.e., that any non-side effect
3676 operator should be allowed, and that cse should take care of coalescing
3677 multiple utterances of the same expression should that prove fruitful. */
3679 tree
3680 stabilize_reference_1 (tree e)
3682 tree result;
3683 enum tree_code code = TREE_CODE (e);
3685 /* We cannot ignore const expressions because it might be a reference
3686 to a const array but whose index contains side-effects. But we can
3687 ignore things that are actual constant or that already have been
3688 handled by this function. */
3690 if (tree_invariant_p (e))
3691 return e;
3693 switch (TREE_CODE_CLASS (code))
3695 case tcc_exceptional:
3696 case tcc_type:
3697 case tcc_declaration:
3698 case tcc_comparison:
3699 case tcc_statement:
3700 case tcc_expression:
3701 case tcc_reference:
3702 case tcc_vl_exp:
3703 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3704 so that it will only be evaluated once. */
3705 /* The reference (r) and comparison (<) classes could be handled as
3706 below, but it is generally faster to only evaluate them once. */
3707 if (TREE_SIDE_EFFECTS (e))
3708 return save_expr (e);
3709 return e;
3711 case tcc_constant:
3712 /* Constants need no processing. In fact, we should never reach
3713 here. */
3714 return e;
3716 case tcc_binary:
3717 /* Division is slow and tends to be compiled with jumps,
3718 especially the division by powers of 2 that is often
3719 found inside of an array reference. So do it just once. */
3720 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3721 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3722 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3723 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3724 return save_expr (e);
3725 /* Recursively stabilize each operand. */
3726 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3727 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3728 break;
3730 case tcc_unary:
3731 /* Recursively stabilize each operand. */
3732 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3733 break;
3735 default:
3736 gcc_unreachable ();
3739 TREE_TYPE (result) = TREE_TYPE (e);
3740 TREE_READONLY (result) = TREE_READONLY (e);
3741 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3742 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3744 return result;
3747 /* Low-level constructors for expressions. */
3749 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
3750 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
3752 void
3753 recompute_tree_invariant_for_addr_expr (tree t)
3755 tree node;
3756 bool tc = true, se = false;
3758 /* We started out assuming this address is both invariant and constant, but
3759 does not have side effects. Now go down any handled components and see if
3760 any of them involve offsets that are either non-constant or non-invariant.
3761 Also check for side-effects.
3763 ??? Note that this code makes no attempt to deal with the case where
3764 taking the address of something causes a copy due to misalignment. */
3766 #define UPDATE_FLAGS(NODE) \
3767 do { tree _node = (NODE); \
3768 if (_node && !TREE_CONSTANT (_node)) tc = false; \
3769 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3771 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3772 node = TREE_OPERAND (node, 0))
3774 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3775 array reference (probably made temporarily by the G++ front end),
3776 so ignore all the operands. */
3777 if ((TREE_CODE (node) == ARRAY_REF
3778 || TREE_CODE (node) == ARRAY_RANGE_REF)
3779 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
3781 UPDATE_FLAGS (TREE_OPERAND (node, 1));
3782 if (TREE_OPERAND (node, 2))
3783 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3784 if (TREE_OPERAND (node, 3))
3785 UPDATE_FLAGS (TREE_OPERAND (node, 3));
3787 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
3788 FIELD_DECL, apparently. The G++ front end can put something else
3789 there, at least temporarily. */
3790 else if (TREE_CODE (node) == COMPONENT_REF
3791 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
3793 if (TREE_OPERAND (node, 2))
3794 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3798 node = lang_hooks.expr_to_decl (node, &tc, &se);
3800 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
3801 the address, since &(*a)->b is a form of addition. If it's a constant, the
3802 address is constant too. If it's a decl, its address is constant if the
3803 decl is static. Everything else is not constant and, furthermore,
3804 taking the address of a volatile variable is not volatile. */
3805 if (TREE_CODE (node) == INDIRECT_REF
3806 || TREE_CODE (node) == MEM_REF)
3807 UPDATE_FLAGS (TREE_OPERAND (node, 0));
3808 else if (CONSTANT_CLASS_P (node))
3810 else if (DECL_P (node))
3811 tc &= (staticp (node) != NULL_TREE);
3812 else
3814 tc = false;
3815 se |= TREE_SIDE_EFFECTS (node);
3819 TREE_CONSTANT (t) = tc;
3820 TREE_SIDE_EFFECTS (t) = se;
3821 #undef UPDATE_FLAGS
3824 /* Build an expression of code CODE, data type TYPE, and operands as
3825 specified. Expressions and reference nodes can be created this way.
3826 Constants, decls, types and misc nodes cannot be.
3828 We define 5 non-variadic functions, from 0 to 4 arguments. This is
3829 enough for all extant tree codes. */
3831 tree
3832 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
3834 tree t;
3836 gcc_assert (TREE_CODE_LENGTH (code) == 0);
3838 t = make_node_stat (code PASS_MEM_STAT);
3839 TREE_TYPE (t) = tt;
3841 return t;
3844 tree
3845 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
3847 int length = sizeof (struct tree_exp);
3848 tree t;
3850 record_node_allocation_statistics (code, length);
3852 gcc_assert (TREE_CODE_LENGTH (code) == 1);
3854 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
3856 memset (t, 0, sizeof (struct tree_common));
3858 TREE_SET_CODE (t, code);
3860 TREE_TYPE (t) = type;
3861 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
3862 TREE_OPERAND (t, 0) = node;
3863 if (node && !TYPE_P (node))
3865 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
3866 TREE_READONLY (t) = TREE_READONLY (node);
3869 if (TREE_CODE_CLASS (code) == tcc_statement)
3870 TREE_SIDE_EFFECTS (t) = 1;
3871 else switch (code)
3873 case VA_ARG_EXPR:
3874 /* All of these have side-effects, no matter what their
3875 operands are. */
3876 TREE_SIDE_EFFECTS (t) = 1;
3877 TREE_READONLY (t) = 0;
3878 break;
3880 case INDIRECT_REF:
3881 /* Whether a dereference is readonly has nothing to do with whether
3882 its operand is readonly. */
3883 TREE_READONLY (t) = 0;
3884 break;
3886 case ADDR_EXPR:
3887 if (node)
3888 recompute_tree_invariant_for_addr_expr (t);
3889 break;
3891 default:
3892 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
3893 && node && !TYPE_P (node)
3894 && TREE_CONSTANT (node))
3895 TREE_CONSTANT (t) = 1;
3896 if (TREE_CODE_CLASS (code) == tcc_reference
3897 && node && TREE_THIS_VOLATILE (node))
3898 TREE_THIS_VOLATILE (t) = 1;
3899 break;
3902 return t;
3905 #define PROCESS_ARG(N) \
3906 do { \
3907 TREE_OPERAND (t, N) = arg##N; \
3908 if (arg##N &&!TYPE_P (arg##N)) \
3910 if (TREE_SIDE_EFFECTS (arg##N)) \
3911 side_effects = 1; \
3912 if (!TREE_READONLY (arg##N) \
3913 && !CONSTANT_CLASS_P (arg##N)) \
3914 (void) (read_only = 0); \
3915 if (!TREE_CONSTANT (arg##N)) \
3916 (void) (constant = 0); \
3918 } while (0)
3920 tree
3921 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
3923 bool constant, read_only, side_effects;
3924 tree t;
3926 gcc_assert (TREE_CODE_LENGTH (code) == 2);
3928 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
3929 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
3930 /* When sizetype precision doesn't match that of pointers
3931 we need to be able to build explicit extensions or truncations
3932 of the offset argument. */
3933 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
3934 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
3935 && TREE_CODE (arg1) == INTEGER_CST);
3937 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
3938 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
3939 && ptrofftype_p (TREE_TYPE (arg1)));
3941 t = make_node_stat (code PASS_MEM_STAT);
3942 TREE_TYPE (t) = tt;
3944 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
3945 result based on those same flags for the arguments. But if the
3946 arguments aren't really even `tree' expressions, we shouldn't be trying
3947 to do this. */
3949 /* Expressions without side effects may be constant if their
3950 arguments are as well. */
3951 constant = (TREE_CODE_CLASS (code) == tcc_comparison
3952 || TREE_CODE_CLASS (code) == tcc_binary);
3953 read_only = 1;
3954 side_effects = TREE_SIDE_EFFECTS (t);
3956 PROCESS_ARG(0);
3957 PROCESS_ARG(1);
3959 TREE_READONLY (t) = read_only;
3960 TREE_CONSTANT (t) = constant;
3961 TREE_SIDE_EFFECTS (t) = side_effects;
3962 TREE_THIS_VOLATILE (t)
3963 = (TREE_CODE_CLASS (code) == tcc_reference
3964 && arg0 && TREE_THIS_VOLATILE (arg0));
3966 return t;
3970 tree
3971 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3972 tree arg2 MEM_STAT_DECL)
3974 bool constant, read_only, side_effects;
3975 tree t;
3977 gcc_assert (TREE_CODE_LENGTH (code) == 3);
3978 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3980 t = make_node_stat (code PASS_MEM_STAT);
3981 TREE_TYPE (t) = tt;
3983 read_only = 1;
3985 /* As a special exception, if COND_EXPR has NULL branches, we
3986 assume that it is a gimple statement and always consider
3987 it to have side effects. */
3988 if (code == COND_EXPR
3989 && tt == void_type_node
3990 && arg1 == NULL_TREE
3991 && arg2 == NULL_TREE)
3992 side_effects = true;
3993 else
3994 side_effects = TREE_SIDE_EFFECTS (t);
3996 PROCESS_ARG(0);
3997 PROCESS_ARG(1);
3998 PROCESS_ARG(2);
4000 if (code == COND_EXPR)
4001 TREE_READONLY (t) = read_only;
4003 TREE_SIDE_EFFECTS (t) = side_effects;
4004 TREE_THIS_VOLATILE (t)
4005 = (TREE_CODE_CLASS (code) == tcc_reference
4006 && arg0 && TREE_THIS_VOLATILE (arg0));
4008 return t;
4011 tree
4012 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4013 tree arg2, tree arg3 MEM_STAT_DECL)
4015 bool constant, read_only, side_effects;
4016 tree t;
4018 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4020 t = make_node_stat (code PASS_MEM_STAT);
4021 TREE_TYPE (t) = tt;
4023 side_effects = TREE_SIDE_EFFECTS (t);
4025 PROCESS_ARG(0);
4026 PROCESS_ARG(1);
4027 PROCESS_ARG(2);
4028 PROCESS_ARG(3);
4030 TREE_SIDE_EFFECTS (t) = side_effects;
4031 TREE_THIS_VOLATILE (t)
4032 = (TREE_CODE_CLASS (code) == tcc_reference
4033 && arg0 && TREE_THIS_VOLATILE (arg0));
4035 return t;
4038 tree
4039 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4040 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4042 bool constant, read_only, side_effects;
4043 tree t;
4045 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4047 t = make_node_stat (code PASS_MEM_STAT);
4048 TREE_TYPE (t) = tt;
4050 side_effects = TREE_SIDE_EFFECTS (t);
4052 PROCESS_ARG(0);
4053 PROCESS_ARG(1);
4054 PROCESS_ARG(2);
4055 PROCESS_ARG(3);
4056 PROCESS_ARG(4);
4058 TREE_SIDE_EFFECTS (t) = side_effects;
4059 TREE_THIS_VOLATILE (t)
4060 = (TREE_CODE_CLASS (code) == tcc_reference
4061 && arg0 && TREE_THIS_VOLATILE (arg0));
4063 return t;
4066 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4067 on the pointer PTR. */
4069 tree
4070 build_simple_mem_ref_loc (location_t loc, tree ptr)
4072 HOST_WIDE_INT offset = 0;
4073 tree ptype = TREE_TYPE (ptr);
4074 tree tem;
4075 /* For convenience allow addresses that collapse to a simple base
4076 and offset. */
4077 if (TREE_CODE (ptr) == ADDR_EXPR
4078 && (handled_component_p (TREE_OPERAND (ptr, 0))
4079 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4081 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4082 gcc_assert (ptr);
4083 ptr = build_fold_addr_expr (ptr);
4084 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4086 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4087 ptr, build_int_cst (ptype, offset));
4088 SET_EXPR_LOCATION (tem, loc);
4089 return tem;
4092 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4094 double_int
4095 mem_ref_offset (const_tree t)
4097 tree toff = TREE_OPERAND (t, 1);
4098 return tree_to_double_int (toff).sext (TYPE_PRECISION (TREE_TYPE (toff)));
4101 /* Return the pointer-type relevant for TBAA purposes from the
4102 gimple memory reference tree T. This is the type to be used for
4103 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T. */
4105 tree
4106 reference_alias_ptr_type (const_tree t)
4108 const_tree base = t;
4109 while (handled_component_p (base))
4110 base = TREE_OPERAND (base, 0);
4111 if (TREE_CODE (base) == MEM_REF)
4112 return TREE_TYPE (TREE_OPERAND (base, 1));
4113 else if (TREE_CODE (base) == TARGET_MEM_REF)
4114 return TREE_TYPE (TMR_OFFSET (base));
4115 else
4116 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (base)));
4119 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4120 offsetted by OFFSET units. */
4122 tree
4123 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4125 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4126 build_fold_addr_expr (base),
4127 build_int_cst (ptr_type_node, offset));
4128 tree addr = build1 (ADDR_EXPR, type, ref);
4129 recompute_tree_invariant_for_addr_expr (addr);
4130 return addr;
4133 /* Similar except don't specify the TREE_TYPE
4134 and leave the TREE_SIDE_EFFECTS as 0.
4135 It is permissible for arguments to be null,
4136 or even garbage if their values do not matter. */
4138 tree
4139 build_nt (enum tree_code code, ...)
4141 tree t;
4142 int length;
4143 int i;
4144 va_list p;
4146 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4148 va_start (p, code);
4150 t = make_node (code);
4151 length = TREE_CODE_LENGTH (code);
4153 for (i = 0; i < length; i++)
4154 TREE_OPERAND (t, i) = va_arg (p, tree);
4156 va_end (p);
4157 return t;
4160 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4161 tree vec. */
4163 tree
4164 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4166 tree ret, t;
4167 unsigned int ix;
4169 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4170 CALL_EXPR_FN (ret) = fn;
4171 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4172 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4173 CALL_EXPR_ARG (ret, ix) = t;
4174 return ret;
4177 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4178 We do NOT enter this node in any sort of symbol table.
4180 LOC is the location of the decl.
4182 layout_decl is used to set up the decl's storage layout.
4183 Other slots are initialized to 0 or null pointers. */
4185 tree
4186 build_decl_stat (location_t loc, enum tree_code code, tree name,
4187 tree type MEM_STAT_DECL)
4189 tree t;
4191 t = make_node_stat (code PASS_MEM_STAT);
4192 DECL_SOURCE_LOCATION (t) = loc;
4194 /* if (type == error_mark_node)
4195 type = integer_type_node; */
4196 /* That is not done, deliberately, so that having error_mark_node
4197 as the type can suppress useless errors in the use of this variable. */
4199 DECL_NAME (t) = name;
4200 TREE_TYPE (t) = type;
4202 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4203 layout_decl (t, 0);
4205 return t;
4208 /* Builds and returns function declaration with NAME and TYPE. */
4210 tree
4211 build_fn_decl (const char *name, tree type)
4213 tree id = get_identifier (name);
4214 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4216 DECL_EXTERNAL (decl) = 1;
4217 TREE_PUBLIC (decl) = 1;
4218 DECL_ARTIFICIAL (decl) = 1;
4219 TREE_NOTHROW (decl) = 1;
4221 return decl;
4224 vec<tree, va_gc> *all_translation_units;
4226 /* Builds a new translation-unit decl with name NAME, queues it in the
4227 global list of translation-unit decls and returns it. */
4229 tree
4230 build_translation_unit_decl (tree name)
4232 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4233 name, NULL_TREE);
4234 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4235 vec_safe_push (all_translation_units, tu);
4236 return tu;
4240 /* BLOCK nodes are used to represent the structure of binding contours
4241 and declarations, once those contours have been exited and their contents
4242 compiled. This information is used for outputting debugging info. */
4244 tree
4245 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4247 tree block = make_node (BLOCK);
4249 BLOCK_VARS (block) = vars;
4250 BLOCK_SUBBLOCKS (block) = subblocks;
4251 BLOCK_SUPERCONTEXT (block) = supercontext;
4252 BLOCK_CHAIN (block) = chain;
4253 return block;
4257 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4259 LOC is the location to use in tree T. */
4261 void
4262 protected_set_expr_location (tree t, location_t loc)
4264 if (t && CAN_HAVE_LOCATION_P (t))
4265 SET_EXPR_LOCATION (t, loc);
4268 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4269 is ATTRIBUTE. */
4271 tree
4272 build_decl_attribute_variant (tree ddecl, tree attribute)
4274 DECL_ATTRIBUTES (ddecl) = attribute;
4275 return ddecl;
4278 /* Borrowed from hashtab.c iterative_hash implementation. */
4279 #define mix(a,b,c) \
4281 a -= b; a -= c; a ^= (c>>13); \
4282 b -= c; b -= a; b ^= (a<< 8); \
4283 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
4284 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
4285 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
4286 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
4287 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
4288 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
4289 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
4293 /* Produce good hash value combining VAL and VAL2. */
4294 hashval_t
4295 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
4297 /* the golden ratio; an arbitrary value. */
4298 hashval_t a = 0x9e3779b9;
4300 mix (a, val, val2);
4301 return val2;
4304 /* Produce good hash value combining VAL and VAL2. */
4305 hashval_t
4306 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
4308 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
4309 return iterative_hash_hashval_t (val, val2);
4310 else
4312 hashval_t a = (hashval_t) val;
4313 /* Avoid warnings about shifting of more than the width of the type on
4314 hosts that won't execute this path. */
4315 int zero = 0;
4316 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
4317 mix (a, b, val2);
4318 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
4320 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
4321 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
4322 mix (a, b, val2);
4324 return val2;
4328 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4329 is ATTRIBUTE and its qualifiers are QUALS.
4331 Record such modified types already made so we don't make duplicates. */
4333 tree
4334 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4336 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4338 hashval_t hashcode = 0;
4339 tree ntype;
4340 enum tree_code code = TREE_CODE (ttype);
4342 /* Building a distinct copy of a tagged type is inappropriate; it
4343 causes breakage in code that expects there to be a one-to-one
4344 relationship between a struct and its fields.
4345 build_duplicate_type is another solution (as used in
4346 handle_transparent_union_attribute), but that doesn't play well
4347 with the stronger C++ type identity model. */
4348 if (TREE_CODE (ttype) == RECORD_TYPE
4349 || TREE_CODE (ttype) == UNION_TYPE
4350 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4351 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4353 warning (OPT_Wattributes,
4354 "ignoring attributes applied to %qT after definition",
4355 TYPE_MAIN_VARIANT (ttype));
4356 return build_qualified_type (ttype, quals);
4359 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4360 ntype = build_distinct_type_copy (ttype);
4362 TYPE_ATTRIBUTES (ntype) = attribute;
4364 hashcode = iterative_hash_object (code, hashcode);
4365 if (TREE_TYPE (ntype))
4366 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
4367 hashcode);
4368 hashcode = attribute_hash_list (attribute, hashcode);
4370 switch (TREE_CODE (ntype))
4372 case FUNCTION_TYPE:
4373 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
4374 break;
4375 case ARRAY_TYPE:
4376 if (TYPE_DOMAIN (ntype))
4377 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
4378 hashcode);
4379 break;
4380 case INTEGER_TYPE:
4381 hashcode = iterative_hash_object
4382 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
4383 hashcode = iterative_hash_object
4384 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
4385 break;
4386 case REAL_TYPE:
4387 case FIXED_POINT_TYPE:
4389 unsigned int precision = TYPE_PRECISION (ntype);
4390 hashcode = iterative_hash_object (precision, hashcode);
4392 break;
4393 default:
4394 break;
4397 ntype = type_hash_canon (hashcode, ntype);
4399 /* If the target-dependent attributes make NTYPE different from
4400 its canonical type, we will need to use structural equality
4401 checks for this type. */
4402 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4403 || !comp_type_attributes (ntype, ttype))
4404 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4405 else if (TYPE_CANONICAL (ntype) == ntype)
4406 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4408 ttype = build_qualified_type (ntype, quals);
4410 else if (TYPE_QUALS (ttype) != quals)
4411 ttype = build_qualified_type (ttype, quals);
4413 return ttype;
4416 /* Compare two attributes for their value identity. Return true if the
4417 attribute values are known to be equal; otherwise return false.
4420 static bool
4421 attribute_value_equal (const_tree attr1, const_tree attr2)
4423 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4424 return true;
4426 if (TREE_VALUE (attr1) != NULL_TREE
4427 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4428 && TREE_VALUE (attr2) != NULL
4429 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4430 return (simple_cst_list_equal (TREE_VALUE (attr1),
4431 TREE_VALUE (attr2)) == 1);
4433 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4436 /* Return 0 if the attributes for two types are incompatible, 1 if they
4437 are compatible, and 2 if they are nearly compatible (which causes a
4438 warning to be generated). */
4440 comp_type_attributes (const_tree type1, const_tree type2)
4442 const_tree a1 = TYPE_ATTRIBUTES (type1);
4443 const_tree a2 = TYPE_ATTRIBUTES (type2);
4444 const_tree a;
4446 if (a1 == a2)
4447 return 1;
4448 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4450 const struct attribute_spec *as;
4451 const_tree attr;
4453 as = lookup_attribute_spec (get_attribute_name (a));
4454 if (!as || as->affects_type_identity == false)
4455 continue;
4457 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4458 if (!attr || !attribute_value_equal (a, attr))
4459 break;
4461 if (!a)
4463 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4465 const struct attribute_spec *as;
4467 as = lookup_attribute_spec (get_attribute_name (a));
4468 if (!as || as->affects_type_identity == false)
4469 continue;
4471 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4472 break;
4473 /* We don't need to compare trees again, as we did this
4474 already in first loop. */
4476 /* All types - affecting identity - are equal, so
4477 there is no need to call target hook for comparison. */
4478 if (!a)
4479 return 1;
4481 /* As some type combinations - like default calling-convention - might
4482 be compatible, we have to call the target hook to get the final result. */
4483 return targetm.comp_type_attributes (type1, type2);
4486 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4487 is ATTRIBUTE.
4489 Record such modified types already made so we don't make duplicates. */
4491 tree
4492 build_type_attribute_variant (tree ttype, tree attribute)
4494 return build_type_attribute_qual_variant (ttype, attribute,
4495 TYPE_QUALS (ttype));
4499 /* Reset the expression *EXPR_P, a size or position.
4501 ??? We could reset all non-constant sizes or positions. But it's cheap
4502 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4504 We need to reset self-referential sizes or positions because they cannot
4505 be gimplified and thus can contain a CALL_EXPR after the gimplification
4506 is finished, which will run afoul of LTO streaming. And they need to be
4507 reset to something essentially dummy but not constant, so as to preserve
4508 the properties of the object they are attached to. */
4510 static inline void
4511 free_lang_data_in_one_sizepos (tree *expr_p)
4513 tree expr = *expr_p;
4514 if (CONTAINS_PLACEHOLDER_P (expr))
4515 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4519 /* Reset all the fields in a binfo node BINFO. We only keep
4520 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
4522 static void
4523 free_lang_data_in_binfo (tree binfo)
4525 unsigned i;
4526 tree t;
4528 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4530 BINFO_VIRTUALS (binfo) = NULL_TREE;
4531 BINFO_BASE_ACCESSES (binfo) = NULL;
4532 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4533 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4535 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
4536 free_lang_data_in_binfo (t);
4540 /* Reset all language specific information still present in TYPE. */
4542 static void
4543 free_lang_data_in_type (tree type)
4545 gcc_assert (TYPE_P (type));
4547 /* Give the FE a chance to remove its own data first. */
4548 lang_hooks.free_lang_data (type);
4550 TREE_LANG_FLAG_0 (type) = 0;
4551 TREE_LANG_FLAG_1 (type) = 0;
4552 TREE_LANG_FLAG_2 (type) = 0;
4553 TREE_LANG_FLAG_3 (type) = 0;
4554 TREE_LANG_FLAG_4 (type) = 0;
4555 TREE_LANG_FLAG_5 (type) = 0;
4556 TREE_LANG_FLAG_6 (type) = 0;
4558 if (TREE_CODE (type) == FUNCTION_TYPE)
4560 /* Remove the const and volatile qualifiers from arguments. The
4561 C++ front end removes them, but the C front end does not,
4562 leading to false ODR violation errors when merging two
4563 instances of the same function signature compiled by
4564 different front ends. */
4565 tree p;
4567 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4569 tree arg_type = TREE_VALUE (p);
4571 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
4573 int quals = TYPE_QUALS (arg_type)
4574 & ~TYPE_QUAL_CONST
4575 & ~TYPE_QUAL_VOLATILE;
4576 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
4577 free_lang_data_in_type (TREE_VALUE (p));
4582 /* Remove members that are not actually FIELD_DECLs from the field
4583 list of an aggregate. These occur in C++. */
4584 if (RECORD_OR_UNION_TYPE_P (type))
4586 tree prev, member;
4588 /* Note that TYPE_FIELDS can be shared across distinct
4589 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
4590 to be removed, we cannot set its TREE_CHAIN to NULL.
4591 Otherwise, we would not be able to find all the other fields
4592 in the other instances of this TREE_TYPE.
4594 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
4595 prev = NULL_TREE;
4596 member = TYPE_FIELDS (type);
4597 while (member)
4599 if (TREE_CODE (member) == FIELD_DECL
4600 || TREE_CODE (member) == TYPE_DECL)
4602 if (prev)
4603 TREE_CHAIN (prev) = member;
4604 else
4605 TYPE_FIELDS (type) = member;
4606 prev = member;
4609 member = TREE_CHAIN (member);
4612 if (prev)
4613 TREE_CHAIN (prev) = NULL_TREE;
4614 else
4615 TYPE_FIELDS (type) = NULL_TREE;
4617 TYPE_METHODS (type) = NULL_TREE;
4618 if (TYPE_BINFO (type))
4619 free_lang_data_in_binfo (TYPE_BINFO (type));
4621 else
4623 /* For non-aggregate types, clear out the language slot (which
4624 overloads TYPE_BINFO). */
4625 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
4627 if (INTEGRAL_TYPE_P (type)
4628 || SCALAR_FLOAT_TYPE_P (type)
4629 || FIXED_POINT_TYPE_P (type))
4631 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
4632 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
4636 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
4637 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
4639 if (TYPE_CONTEXT (type)
4640 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
4642 tree ctx = TYPE_CONTEXT (type);
4645 ctx = BLOCK_SUPERCONTEXT (ctx);
4647 while (ctx && TREE_CODE (ctx) == BLOCK);
4648 TYPE_CONTEXT (type) = ctx;
4653 /* Return true if DECL may need an assembler name to be set. */
4655 static inline bool
4656 need_assembler_name_p (tree decl)
4658 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
4659 if (TREE_CODE (decl) != FUNCTION_DECL
4660 && TREE_CODE (decl) != VAR_DECL)
4661 return false;
4663 /* If DECL already has its assembler name set, it does not need a
4664 new one. */
4665 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
4666 || DECL_ASSEMBLER_NAME_SET_P (decl))
4667 return false;
4669 /* Abstract decls do not need an assembler name. */
4670 if (DECL_ABSTRACT (decl))
4671 return false;
4673 /* For VAR_DECLs, only static, public and external symbols need an
4674 assembler name. */
4675 if (TREE_CODE (decl) == VAR_DECL
4676 && !TREE_STATIC (decl)
4677 && !TREE_PUBLIC (decl)
4678 && !DECL_EXTERNAL (decl))
4679 return false;
4681 if (TREE_CODE (decl) == FUNCTION_DECL)
4683 /* Do not set assembler name on builtins. Allow RTL expansion to
4684 decide whether to expand inline or via a regular call. */
4685 if (DECL_BUILT_IN (decl)
4686 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
4687 return false;
4689 /* Functions represented in the callgraph need an assembler name. */
4690 if (cgraph_get_node (decl) != NULL)
4691 return true;
4693 /* Unused and not public functions don't need an assembler name. */
4694 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
4695 return false;
4698 return true;
4702 /* Reset all language specific information still present in symbol
4703 DECL. */
4705 static void
4706 free_lang_data_in_decl (tree decl)
4708 gcc_assert (DECL_P (decl));
4710 /* Give the FE a chance to remove its own data first. */
4711 lang_hooks.free_lang_data (decl);
4713 TREE_LANG_FLAG_0 (decl) = 0;
4714 TREE_LANG_FLAG_1 (decl) = 0;
4715 TREE_LANG_FLAG_2 (decl) = 0;
4716 TREE_LANG_FLAG_3 (decl) = 0;
4717 TREE_LANG_FLAG_4 (decl) = 0;
4718 TREE_LANG_FLAG_5 (decl) = 0;
4719 TREE_LANG_FLAG_6 (decl) = 0;
4721 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
4722 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
4723 if (TREE_CODE (decl) == FIELD_DECL)
4725 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
4726 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
4727 DECL_QUALIFIER (decl) = NULL_TREE;
4730 if (TREE_CODE (decl) == FUNCTION_DECL)
4732 if (gimple_has_body_p (decl))
4734 tree t;
4736 /* If DECL has a gimple body, then the context for its
4737 arguments must be DECL. Otherwise, it doesn't really
4738 matter, as we will not be emitting any code for DECL. In
4739 general, there may be other instances of DECL created by
4740 the front end and since PARM_DECLs are generally shared,
4741 their DECL_CONTEXT changes as the replicas of DECL are
4742 created. The only time where DECL_CONTEXT is important
4743 is for the FUNCTION_DECLs that have a gimple body (since
4744 the PARM_DECL will be used in the function's body). */
4745 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
4746 DECL_CONTEXT (t) = decl;
4749 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
4750 At this point, it is not needed anymore. */
4751 DECL_SAVED_TREE (decl) = NULL_TREE;
4753 /* Clear the abstract origin if it refers to a method. Otherwise
4754 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
4755 origin will not be output correctly. */
4756 if (DECL_ABSTRACT_ORIGIN (decl)
4757 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
4758 && RECORD_OR_UNION_TYPE_P
4759 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
4760 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
4762 /* Sometimes the C++ frontend doesn't manage to transform a temporary
4763 DECL_VINDEX referring to itself into a vtable slot number as it
4764 should. Happens with functions that are copied and then forgotten
4765 about. Just clear it, it won't matter anymore. */
4766 if (DECL_VINDEX (decl) && !host_integerp (DECL_VINDEX (decl), 0))
4767 DECL_VINDEX (decl) = NULL_TREE;
4769 else if (TREE_CODE (decl) == VAR_DECL)
4771 if ((DECL_EXTERNAL (decl)
4772 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
4773 || (decl_function_context (decl) && !TREE_STATIC (decl)))
4774 DECL_INITIAL (decl) = NULL_TREE;
4776 else if (TREE_CODE (decl) == TYPE_DECL
4777 || TREE_CODE (decl) == FIELD_DECL)
4778 DECL_INITIAL (decl) = NULL_TREE;
4779 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
4780 && DECL_INITIAL (decl)
4781 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
4783 /* Strip builtins from the translation-unit BLOCK. We still have targets
4784 without builtin_decl_explicit support and also builtins are shared
4785 nodes and thus we can't use TREE_CHAIN in multiple lists. */
4786 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
4787 while (*nextp)
4789 tree var = *nextp;
4790 if (TREE_CODE (var) == FUNCTION_DECL
4791 && DECL_BUILT_IN (var))
4792 *nextp = TREE_CHAIN (var);
4793 else
4794 nextp = &TREE_CHAIN (var);
4800 /* Data used when collecting DECLs and TYPEs for language data removal. */
4802 struct free_lang_data_d
4804 /* Worklist to avoid excessive recursion. */
4805 vec<tree> worklist;
4807 /* Set of traversed objects. Used to avoid duplicate visits. */
4808 struct pointer_set_t *pset;
4810 /* Array of symbols to process with free_lang_data_in_decl. */
4811 vec<tree> decls;
4813 /* Array of types to process with free_lang_data_in_type. */
4814 vec<tree> types;
4818 /* Save all language fields needed to generate proper debug information
4819 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
4821 static void
4822 save_debug_info_for_decl (tree t)
4824 /*struct saved_debug_info_d *sdi;*/
4826 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
4828 /* FIXME. Partial implementation for saving debug info removed. */
4832 /* Save all language fields needed to generate proper debug information
4833 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
4835 static void
4836 save_debug_info_for_type (tree t)
4838 /*struct saved_debug_info_d *sdi;*/
4840 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
4842 /* FIXME. Partial implementation for saving debug info removed. */
4846 /* Add type or decl T to one of the list of tree nodes that need their
4847 language data removed. The lists are held inside FLD. */
4849 static void
4850 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
4852 if (DECL_P (t))
4854 fld->decls.safe_push (t);
4855 if (debug_info_level > DINFO_LEVEL_TERSE)
4856 save_debug_info_for_decl (t);
4858 else if (TYPE_P (t))
4860 fld->types.safe_push (t);
4861 if (debug_info_level > DINFO_LEVEL_TERSE)
4862 save_debug_info_for_type (t);
4864 else
4865 gcc_unreachable ();
4868 /* Push tree node T into FLD->WORKLIST. */
4870 static inline void
4871 fld_worklist_push (tree t, struct free_lang_data_d *fld)
4873 if (t && !is_lang_specific (t) && !pointer_set_contains (fld->pset, t))
4874 fld->worklist.safe_push ((t));
4878 /* Operand callback helper for free_lang_data_in_node. *TP is the
4879 subtree operand being considered. */
4881 static tree
4882 find_decls_types_r (tree *tp, int *ws, void *data)
4884 tree t = *tp;
4885 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
4887 if (TREE_CODE (t) == TREE_LIST)
4888 return NULL_TREE;
4890 /* Language specific nodes will be removed, so there is no need
4891 to gather anything under them. */
4892 if (is_lang_specific (t))
4894 *ws = 0;
4895 return NULL_TREE;
4898 if (DECL_P (t))
4900 /* Note that walk_tree does not traverse every possible field in
4901 decls, so we have to do our own traversals here. */
4902 add_tree_to_fld_list (t, fld);
4904 fld_worklist_push (DECL_NAME (t), fld);
4905 fld_worklist_push (DECL_CONTEXT (t), fld);
4906 fld_worklist_push (DECL_SIZE (t), fld);
4907 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
4909 /* We are going to remove everything under DECL_INITIAL for
4910 TYPE_DECLs. No point walking them. */
4911 if (TREE_CODE (t) != TYPE_DECL)
4912 fld_worklist_push (DECL_INITIAL (t), fld);
4914 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
4915 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
4917 if (TREE_CODE (t) == FUNCTION_DECL)
4919 fld_worklist_push (DECL_ARGUMENTS (t), fld);
4920 fld_worklist_push (DECL_RESULT (t), fld);
4922 else if (TREE_CODE (t) == TYPE_DECL)
4924 fld_worklist_push (DECL_ARGUMENT_FLD (t), fld);
4925 fld_worklist_push (DECL_VINDEX (t), fld);
4926 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
4928 else if (TREE_CODE (t) == FIELD_DECL)
4930 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
4931 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
4932 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
4933 fld_worklist_push (DECL_FCONTEXT (t), fld);
4935 else if (TREE_CODE (t) == VAR_DECL)
4937 fld_worklist_push (DECL_SECTION_NAME (t), fld);
4938 fld_worklist_push (DECL_COMDAT_GROUP (t), fld);
4941 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
4942 && DECL_HAS_VALUE_EXPR_P (t))
4943 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
4945 if (TREE_CODE (t) != FIELD_DECL
4946 && TREE_CODE (t) != TYPE_DECL)
4947 fld_worklist_push (TREE_CHAIN (t), fld);
4948 *ws = 0;
4950 else if (TYPE_P (t))
4952 /* Note that walk_tree does not traverse every possible field in
4953 types, so we have to do our own traversals here. */
4954 add_tree_to_fld_list (t, fld);
4956 if (!RECORD_OR_UNION_TYPE_P (t))
4957 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
4958 fld_worklist_push (TYPE_SIZE (t), fld);
4959 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
4960 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
4961 fld_worklist_push (TYPE_POINTER_TO (t), fld);
4962 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
4963 fld_worklist_push (TYPE_NAME (t), fld);
4964 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
4965 them and thus do not and want not to reach unused pointer types
4966 this way. */
4967 if (!POINTER_TYPE_P (t))
4968 fld_worklist_push (TYPE_MINVAL (t), fld);
4969 if (!RECORD_OR_UNION_TYPE_P (t))
4970 fld_worklist_push (TYPE_MAXVAL (t), fld);
4971 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
4972 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
4973 do not and want not to reach unused variants this way. */
4974 if (TYPE_CONTEXT (t))
4976 tree ctx = TYPE_CONTEXT (t);
4977 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
4978 So push that instead. */
4979 while (ctx && TREE_CODE (ctx) == BLOCK)
4980 ctx = BLOCK_SUPERCONTEXT (ctx);
4981 fld_worklist_push (ctx, fld);
4983 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
4984 and want not to reach unused types this way. */
4986 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
4988 unsigned i;
4989 tree tem;
4990 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
4991 fld_worklist_push (TREE_TYPE (tem), fld);
4992 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
4993 if (tem
4994 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
4995 && TREE_CODE (tem) == TREE_LIST)
4998 fld_worklist_push (TREE_VALUE (tem), fld);
4999 tem = TREE_CHAIN (tem);
5001 while (tem);
5003 if (RECORD_OR_UNION_TYPE_P (t))
5005 tree tem;
5006 /* Push all TYPE_FIELDS - there can be interleaving interesting
5007 and non-interesting things. */
5008 tem = TYPE_FIELDS (t);
5009 while (tem)
5011 if (TREE_CODE (tem) == FIELD_DECL
5012 || TREE_CODE (tem) == TYPE_DECL)
5013 fld_worklist_push (tem, fld);
5014 tem = TREE_CHAIN (tem);
5018 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5019 *ws = 0;
5021 else if (TREE_CODE (t) == BLOCK)
5023 tree tem;
5024 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5025 fld_worklist_push (tem, fld);
5026 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5027 fld_worklist_push (tem, fld);
5028 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5031 if (TREE_CODE (t) != IDENTIFIER_NODE
5032 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5033 fld_worklist_push (TREE_TYPE (t), fld);
5035 return NULL_TREE;
5039 /* Find decls and types in T. */
5041 static void
5042 find_decls_types (tree t, struct free_lang_data_d *fld)
5044 while (1)
5046 if (!pointer_set_contains (fld->pset, t))
5047 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5048 if (fld->worklist.is_empty ())
5049 break;
5050 t = fld->worklist.pop ();
5054 /* Translate all the types in LIST with the corresponding runtime
5055 types. */
5057 static tree
5058 get_eh_types_for_runtime (tree list)
5060 tree head, prev;
5062 if (list == NULL_TREE)
5063 return NULL_TREE;
5065 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5066 prev = head;
5067 list = TREE_CHAIN (list);
5068 while (list)
5070 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5071 TREE_CHAIN (prev) = n;
5072 prev = TREE_CHAIN (prev);
5073 list = TREE_CHAIN (list);
5076 return head;
5080 /* Find decls and types referenced in EH region R and store them in
5081 FLD->DECLS and FLD->TYPES. */
5083 static void
5084 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5086 switch (r->type)
5088 case ERT_CLEANUP:
5089 break;
5091 case ERT_TRY:
5093 eh_catch c;
5095 /* The types referenced in each catch must first be changed to the
5096 EH types used at runtime. This removes references to FE types
5097 in the region. */
5098 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5100 c->type_list = get_eh_types_for_runtime (c->type_list);
5101 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5104 break;
5106 case ERT_ALLOWED_EXCEPTIONS:
5107 r->u.allowed.type_list
5108 = get_eh_types_for_runtime (r->u.allowed.type_list);
5109 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5110 break;
5112 case ERT_MUST_NOT_THROW:
5113 walk_tree (&r->u.must_not_throw.failure_decl,
5114 find_decls_types_r, fld, fld->pset);
5115 break;
5120 /* Find decls and types referenced in cgraph node N and store them in
5121 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5122 look for *every* kind of DECL and TYPE node reachable from N,
5123 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5124 NAMESPACE_DECLs, etc). */
5126 static void
5127 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5129 basic_block bb;
5130 struct function *fn;
5131 unsigned ix;
5132 tree t;
5134 find_decls_types (n->symbol.decl, fld);
5136 if (!gimple_has_body_p (n->symbol.decl))
5137 return;
5139 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5141 fn = DECL_STRUCT_FUNCTION (n->symbol.decl);
5143 /* Traverse locals. */
5144 FOR_EACH_LOCAL_DECL (fn, ix, t)
5145 find_decls_types (t, fld);
5147 /* Traverse EH regions in FN. */
5149 eh_region r;
5150 FOR_ALL_EH_REGION_FN (r, fn)
5151 find_decls_types_in_eh_region (r, fld);
5154 /* Traverse every statement in FN. */
5155 FOR_EACH_BB_FN (bb, fn)
5157 gimple_stmt_iterator si;
5158 unsigned i;
5160 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5162 gimple phi = gsi_stmt (si);
5164 for (i = 0; i < gimple_phi_num_args (phi); i++)
5166 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5167 find_decls_types (*arg_p, fld);
5171 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5173 gimple stmt = gsi_stmt (si);
5175 if (is_gimple_call (stmt))
5176 find_decls_types (gimple_call_fntype (stmt), fld);
5178 for (i = 0; i < gimple_num_ops (stmt); i++)
5180 tree arg = gimple_op (stmt, i);
5181 find_decls_types (arg, fld);
5188 /* Find decls and types referenced in varpool node N and store them in
5189 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5190 look for *every* kind of DECL and TYPE node reachable from N,
5191 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5192 NAMESPACE_DECLs, etc). */
5194 static void
5195 find_decls_types_in_var (struct varpool_node *v, struct free_lang_data_d *fld)
5197 find_decls_types (v->symbol.decl, fld);
5200 /* If T needs an assembler name, have one created for it. */
5202 void
5203 assign_assembler_name_if_neeeded (tree t)
5205 if (need_assembler_name_p (t))
5207 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5208 diagnostics that use input_location to show locus
5209 information. The problem here is that, at this point,
5210 input_location is generally anchored to the end of the file
5211 (since the parser is long gone), so we don't have a good
5212 position to pin it to.
5214 To alleviate this problem, this uses the location of T's
5215 declaration. Examples of this are
5216 testsuite/g++.dg/template/cond2.C and
5217 testsuite/g++.dg/template/pr35240.C. */
5218 location_t saved_location = input_location;
5219 input_location = DECL_SOURCE_LOCATION (t);
5221 decl_assembler_name (t);
5223 input_location = saved_location;
5228 /* Free language specific information for every operand and expression
5229 in every node of the call graph. This process operates in three stages:
5231 1- Every callgraph node and varpool node is traversed looking for
5232 decls and types embedded in them. This is a more exhaustive
5233 search than that done by find_referenced_vars, because it will
5234 also collect individual fields, decls embedded in types, etc.
5236 2- All the decls found are sent to free_lang_data_in_decl.
5238 3- All the types found are sent to free_lang_data_in_type.
5240 The ordering between decls and types is important because
5241 free_lang_data_in_decl sets assembler names, which includes
5242 mangling. So types cannot be freed up until assembler names have
5243 been set up. */
5245 static void
5246 free_lang_data_in_cgraph (void)
5248 struct cgraph_node *n;
5249 struct varpool_node *v;
5250 struct free_lang_data_d fld;
5251 tree t;
5252 unsigned i;
5253 alias_pair *p;
5255 /* Initialize sets and arrays to store referenced decls and types. */
5256 fld.pset = pointer_set_create ();
5257 fld.worklist.create (0);
5258 fld.decls.create (100);
5259 fld.types.create (100);
5261 /* Find decls and types in the body of every function in the callgraph. */
5262 FOR_EACH_FUNCTION (n)
5263 find_decls_types_in_node (n, &fld);
5265 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5266 find_decls_types (p->decl, &fld);
5268 /* Find decls and types in every varpool symbol. */
5269 FOR_EACH_VARIABLE (v)
5270 find_decls_types_in_var (v, &fld);
5272 /* Set the assembler name on every decl found. We need to do this
5273 now because free_lang_data_in_decl will invalidate data needed
5274 for mangling. This breaks mangling on interdependent decls. */
5275 FOR_EACH_VEC_ELT (fld.decls, i, t)
5276 assign_assembler_name_if_neeeded (t);
5278 /* Traverse every decl found freeing its language data. */
5279 FOR_EACH_VEC_ELT (fld.decls, i, t)
5280 free_lang_data_in_decl (t);
5282 /* Traverse every type found freeing its language data. */
5283 FOR_EACH_VEC_ELT (fld.types, i, t)
5284 free_lang_data_in_type (t);
5286 pointer_set_destroy (fld.pset);
5287 fld.worklist.release ();
5288 fld.decls.release ();
5289 fld.types.release ();
5293 /* Free resources that are used by FE but are not needed once they are done. */
5295 static unsigned
5296 free_lang_data (void)
5298 unsigned i;
5300 /* If we are the LTO frontend we have freed lang-specific data already. */
5301 if (in_lto_p
5302 || !flag_generate_lto)
5303 return 0;
5305 /* Allocate and assign alias sets to the standard integer types
5306 while the slots are still in the way the frontends generated them. */
5307 for (i = 0; i < itk_none; ++i)
5308 if (integer_types[i])
5309 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5311 /* Traverse the IL resetting language specific information for
5312 operands, expressions, etc. */
5313 free_lang_data_in_cgraph ();
5315 /* Create gimple variants for common types. */
5316 ptrdiff_type_node = integer_type_node;
5317 fileptr_type_node = ptr_type_node;
5319 /* Reset some langhooks. Do not reset types_compatible_p, it may
5320 still be used indirectly via the get_alias_set langhook. */
5321 lang_hooks.dwarf_name = lhd_dwarf_name;
5322 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5323 /* We do not want the default decl_assembler_name implementation,
5324 rather if we have fixed everything we want a wrapper around it
5325 asserting that all non-local symbols already got their assembler
5326 name and only produce assembler names for local symbols. Or rather
5327 make sure we never call decl_assembler_name on local symbols and
5328 devise a separate, middle-end private scheme for it. */
5330 /* Reset diagnostic machinery. */
5331 tree_diagnostics_defaults (global_dc);
5333 return 0;
5337 struct simple_ipa_opt_pass pass_ipa_free_lang_data =
5340 SIMPLE_IPA_PASS,
5341 "*free_lang_data", /* name */
5342 OPTGROUP_NONE, /* optinfo_flags */
5343 NULL, /* gate */
5344 free_lang_data, /* execute */
5345 NULL, /* sub */
5346 NULL, /* next */
5347 0, /* static_pass_number */
5348 TV_IPA_FREE_LANG_DATA, /* tv_id */
5349 0, /* properties_required */
5350 0, /* properties_provided */
5351 0, /* properties_destroyed */
5352 0, /* todo_flags_start */
5353 TODO_ggc_collect /* todo_flags_finish */
5357 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5358 ATTR_NAME. Also used internally by remove_attribute(). */
5359 bool
5360 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5362 size_t ident_len = IDENTIFIER_LENGTH (ident);
5364 if (ident_len == attr_len)
5366 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5367 return true;
5369 else if (ident_len == attr_len + 4)
5371 /* There is the possibility that ATTR is 'text' and IDENT is
5372 '__text__'. */
5373 const char *p = IDENTIFIER_POINTER (ident);
5374 if (p[0] == '_' && p[1] == '_'
5375 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5376 && strncmp (attr_name, p + 2, attr_len) == 0)
5377 return true;
5380 return false;
5383 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5384 of ATTR_NAME, and LIST is not NULL_TREE. */
5385 tree
5386 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5388 while (list)
5390 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5392 if (ident_len == attr_len)
5394 if (!strcmp (attr_name,
5395 IDENTIFIER_POINTER (get_attribute_name (list))))
5396 break;
5398 /* TODO: If we made sure that attributes were stored in the
5399 canonical form without '__...__' (ie, as in 'text' as opposed
5400 to '__text__') then we could avoid the following case. */
5401 else if (ident_len == attr_len + 4)
5403 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5404 if (p[0] == '_' && p[1] == '_'
5405 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5406 && strncmp (attr_name, p + 2, attr_len) == 0)
5407 break;
5409 list = TREE_CHAIN (list);
5412 return list;
5415 /* A variant of lookup_attribute() that can be used with an identifier
5416 as the first argument, and where the identifier can be either
5417 'text' or '__text__'.
5419 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5420 return a pointer to the attribute's list element if the attribute
5421 is part of the list, or NULL_TREE if not found. If the attribute
5422 appears more than once, this only returns the first occurrence; the
5423 TREE_CHAIN of the return value should be passed back in if further
5424 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5425 can be in the form 'text' or '__text__'. */
5426 static tree
5427 lookup_ident_attribute (tree attr_identifier, tree list)
5429 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5431 while (list)
5433 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5434 == IDENTIFIER_NODE);
5436 /* Identifiers can be compared directly for equality. */
5437 if (attr_identifier == get_attribute_name (list))
5438 break;
5440 /* If they are not equal, they may still be one in the form
5441 'text' while the other one is in the form '__text__'. TODO:
5442 If we were storing attributes in normalized 'text' form, then
5443 this could all go away and we could take full advantage of
5444 the fact that we're comparing identifiers. :-) */
5446 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5447 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5449 if (ident_len == attr_len + 4)
5451 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5452 const char *q = IDENTIFIER_POINTER (attr_identifier);
5453 if (p[0] == '_' && p[1] == '_'
5454 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5455 && strncmp (q, p + 2, attr_len) == 0)
5456 break;
5458 else if (ident_len + 4 == attr_len)
5460 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5461 const char *q = IDENTIFIER_POINTER (attr_identifier);
5462 if (q[0] == '_' && q[1] == '_'
5463 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5464 && strncmp (q + 2, p, ident_len) == 0)
5465 break;
5468 list = TREE_CHAIN (list);
5471 return list;
5474 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5475 modified list. */
5477 tree
5478 remove_attribute (const char *attr_name, tree list)
5480 tree *p;
5481 size_t attr_len = strlen (attr_name);
5483 gcc_checking_assert (attr_name[0] != '_');
5485 for (p = &list; *p; )
5487 tree l = *p;
5488 /* TODO: If we were storing attributes in normalized form, here
5489 we could use a simple strcmp(). */
5490 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
5491 *p = TREE_CHAIN (l);
5492 else
5493 p = &TREE_CHAIN (l);
5496 return list;
5499 /* Return an attribute list that is the union of a1 and a2. */
5501 tree
5502 merge_attributes (tree a1, tree a2)
5504 tree attributes;
5506 /* Either one unset? Take the set one. */
5508 if ((attributes = a1) == 0)
5509 attributes = a2;
5511 /* One that completely contains the other? Take it. */
5513 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
5515 if (attribute_list_contained (a2, a1))
5516 attributes = a2;
5517 else
5519 /* Pick the longest list, and hang on the other list. */
5521 if (list_length (a1) < list_length (a2))
5522 attributes = a2, a2 = a1;
5524 for (; a2 != 0; a2 = TREE_CHAIN (a2))
5526 tree a;
5527 for (a = lookup_ident_attribute (get_attribute_name (a2),
5528 attributes);
5529 a != NULL_TREE && !attribute_value_equal (a, a2);
5530 a = lookup_ident_attribute (get_attribute_name (a2),
5531 TREE_CHAIN (a)))
5533 if (a == NULL_TREE)
5535 a1 = copy_node (a2);
5536 TREE_CHAIN (a1) = attributes;
5537 attributes = a1;
5542 return attributes;
5545 /* Given types T1 and T2, merge their attributes and return
5546 the result. */
5548 tree
5549 merge_type_attributes (tree t1, tree t2)
5551 return merge_attributes (TYPE_ATTRIBUTES (t1),
5552 TYPE_ATTRIBUTES (t2));
5555 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
5556 the result. */
5558 tree
5559 merge_decl_attributes (tree olddecl, tree newdecl)
5561 return merge_attributes (DECL_ATTRIBUTES (olddecl),
5562 DECL_ATTRIBUTES (newdecl));
5565 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5567 /* Specialization of merge_decl_attributes for various Windows targets.
5569 This handles the following situation:
5571 __declspec (dllimport) int foo;
5572 int foo;
5574 The second instance of `foo' nullifies the dllimport. */
5576 tree
5577 merge_dllimport_decl_attributes (tree old, tree new_tree)
5579 tree a;
5580 int delete_dllimport_p = 1;
5582 /* What we need to do here is remove from `old' dllimport if it doesn't
5583 appear in `new'. dllimport behaves like extern: if a declaration is
5584 marked dllimport and a definition appears later, then the object
5585 is not dllimport'd. We also remove a `new' dllimport if the old list
5586 contains dllexport: dllexport always overrides dllimport, regardless
5587 of the order of declaration. */
5588 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
5589 delete_dllimport_p = 0;
5590 else if (DECL_DLLIMPORT_P (new_tree)
5591 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
5593 DECL_DLLIMPORT_P (new_tree) = 0;
5594 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
5595 "dllimport ignored", new_tree);
5597 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
5599 /* Warn about overriding a symbol that has already been used, e.g.:
5600 extern int __attribute__ ((dllimport)) foo;
5601 int* bar () {return &foo;}
5602 int foo;
5604 if (TREE_USED (old))
5606 warning (0, "%q+D redeclared without dllimport attribute "
5607 "after being referenced with dll linkage", new_tree);
5608 /* If we have used a variable's address with dllimport linkage,
5609 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
5610 decl may already have had TREE_CONSTANT computed.
5611 We still remove the attribute so that assembler code refers
5612 to '&foo rather than '_imp__foo'. */
5613 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
5614 DECL_DLLIMPORT_P (new_tree) = 1;
5617 /* Let an inline definition silently override the external reference,
5618 but otherwise warn about attribute inconsistency. */
5619 else if (TREE_CODE (new_tree) == VAR_DECL
5620 || !DECL_DECLARED_INLINE_P (new_tree))
5621 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
5622 "previous dllimport ignored", new_tree);
5624 else
5625 delete_dllimport_p = 0;
5627 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
5629 if (delete_dllimport_p)
5630 a = remove_attribute ("dllimport", a);
5632 return a;
5635 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
5636 struct attribute_spec.handler. */
5638 tree
5639 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
5640 bool *no_add_attrs)
5642 tree node = *pnode;
5643 bool is_dllimport;
5645 /* These attributes may apply to structure and union types being created,
5646 but otherwise should pass to the declaration involved. */
5647 if (!DECL_P (node))
5649 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
5650 | (int) ATTR_FLAG_ARRAY_NEXT))
5652 *no_add_attrs = true;
5653 return tree_cons (name, args, NULL_TREE);
5655 if (TREE_CODE (node) == RECORD_TYPE
5656 || TREE_CODE (node) == UNION_TYPE)
5658 node = TYPE_NAME (node);
5659 if (!node)
5660 return NULL_TREE;
5662 else
5664 warning (OPT_Wattributes, "%qE attribute ignored",
5665 name);
5666 *no_add_attrs = true;
5667 return NULL_TREE;
5671 if (TREE_CODE (node) != FUNCTION_DECL
5672 && TREE_CODE (node) != VAR_DECL
5673 && TREE_CODE (node) != TYPE_DECL)
5675 *no_add_attrs = true;
5676 warning (OPT_Wattributes, "%qE attribute ignored",
5677 name);
5678 return NULL_TREE;
5681 if (TREE_CODE (node) == TYPE_DECL
5682 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
5683 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
5685 *no_add_attrs = true;
5686 warning (OPT_Wattributes, "%qE attribute ignored",
5687 name);
5688 return NULL_TREE;
5691 is_dllimport = is_attribute_p ("dllimport", name);
5693 /* Report error on dllimport ambiguities seen now before they cause
5694 any damage. */
5695 if (is_dllimport)
5697 /* Honor any target-specific overrides. */
5698 if (!targetm.valid_dllimport_attribute_p (node))
5699 *no_add_attrs = true;
5701 else if (TREE_CODE (node) == FUNCTION_DECL
5702 && DECL_DECLARED_INLINE_P (node))
5704 warning (OPT_Wattributes, "inline function %q+D declared as "
5705 " dllimport: attribute ignored", node);
5706 *no_add_attrs = true;
5708 /* Like MS, treat definition of dllimported variables and
5709 non-inlined functions on declaration as syntax errors. */
5710 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
5712 error ("function %q+D definition is marked dllimport", node);
5713 *no_add_attrs = true;
5716 else if (TREE_CODE (node) == VAR_DECL)
5718 if (DECL_INITIAL (node))
5720 error ("variable %q+D definition is marked dllimport",
5721 node);
5722 *no_add_attrs = true;
5725 /* `extern' needn't be specified with dllimport.
5726 Specify `extern' now and hope for the best. Sigh. */
5727 DECL_EXTERNAL (node) = 1;
5728 /* Also, implicitly give dllimport'd variables declared within
5729 a function global scope, unless declared static. */
5730 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
5731 TREE_PUBLIC (node) = 1;
5734 if (*no_add_attrs == false)
5735 DECL_DLLIMPORT_P (node) = 1;
5737 else if (TREE_CODE (node) == FUNCTION_DECL
5738 && DECL_DECLARED_INLINE_P (node)
5739 && flag_keep_inline_dllexport)
5740 /* An exported function, even if inline, must be emitted. */
5741 DECL_EXTERNAL (node) = 0;
5743 /* Report error if symbol is not accessible at global scope. */
5744 if (!TREE_PUBLIC (node)
5745 && (TREE_CODE (node) == VAR_DECL
5746 || TREE_CODE (node) == FUNCTION_DECL))
5748 error ("external linkage required for symbol %q+D because of "
5749 "%qE attribute", node, name);
5750 *no_add_attrs = true;
5753 /* A dllexport'd entity must have default visibility so that other
5754 program units (shared libraries or the main executable) can see
5755 it. A dllimport'd entity must have default visibility so that
5756 the linker knows that undefined references within this program
5757 unit can be resolved by the dynamic linker. */
5758 if (!*no_add_attrs)
5760 if (DECL_VISIBILITY_SPECIFIED (node)
5761 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
5762 error ("%qE implies default visibility, but %qD has already "
5763 "been declared with a different visibility",
5764 name, node);
5765 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
5766 DECL_VISIBILITY_SPECIFIED (node) = 1;
5769 return NULL_TREE;
5772 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
5774 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
5775 of the various TYPE_QUAL values. */
5777 static void
5778 set_type_quals (tree type, int type_quals)
5780 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
5781 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
5782 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
5783 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
5786 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
5788 bool
5789 check_qualified_type (const_tree cand, const_tree base, int type_quals)
5791 return (TYPE_QUALS (cand) == type_quals
5792 && TYPE_NAME (cand) == TYPE_NAME (base)
5793 /* Apparently this is needed for Objective-C. */
5794 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5795 /* Check alignment. */
5796 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
5797 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5798 TYPE_ATTRIBUTES (base)));
5801 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
5803 static bool
5804 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
5806 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
5807 && TYPE_NAME (cand) == TYPE_NAME (base)
5808 /* Apparently this is needed for Objective-C. */
5809 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5810 /* Check alignment. */
5811 && TYPE_ALIGN (cand) == align
5812 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5813 TYPE_ATTRIBUTES (base)));
5816 /* Return a version of the TYPE, qualified as indicated by the
5817 TYPE_QUALS, if one exists. If no qualified version exists yet,
5818 return NULL_TREE. */
5820 tree
5821 get_qualified_type (tree type, int type_quals)
5823 tree t;
5825 if (TYPE_QUALS (type) == type_quals)
5826 return type;
5828 /* Search the chain of variants to see if there is already one there just
5829 like the one we need to have. If so, use that existing one. We must
5830 preserve the TYPE_NAME, since there is code that depends on this. */
5831 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5832 if (check_qualified_type (t, type, type_quals))
5833 return t;
5835 return NULL_TREE;
5838 /* Like get_qualified_type, but creates the type if it does not
5839 exist. This function never returns NULL_TREE. */
5841 tree
5842 build_qualified_type (tree type, int type_quals)
5844 tree t;
5846 /* See if we already have the appropriate qualified variant. */
5847 t = get_qualified_type (type, type_quals);
5849 /* If not, build it. */
5850 if (!t)
5852 t = build_variant_type_copy (type);
5853 set_type_quals (t, type_quals);
5855 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5856 /* Propagate structural equality. */
5857 SET_TYPE_STRUCTURAL_EQUALITY (t);
5858 else if (TYPE_CANONICAL (type) != type)
5859 /* Build the underlying canonical type, since it is different
5860 from TYPE. */
5861 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
5862 type_quals);
5863 else
5864 /* T is its own canonical type. */
5865 TYPE_CANONICAL (t) = t;
5869 return t;
5872 /* Create a variant of type T with alignment ALIGN. */
5874 tree
5875 build_aligned_type (tree type, unsigned int align)
5877 tree t;
5879 if (TYPE_PACKED (type)
5880 || TYPE_ALIGN (type) == align)
5881 return type;
5883 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5884 if (check_aligned_type (t, type, align))
5885 return t;
5887 t = build_variant_type_copy (type);
5888 TYPE_ALIGN (t) = align;
5890 return t;
5893 /* Create a new distinct copy of TYPE. The new type is made its own
5894 MAIN_VARIANT. If TYPE requires structural equality checks, the
5895 resulting type requires structural equality checks; otherwise, its
5896 TYPE_CANONICAL points to itself. */
5898 tree
5899 build_distinct_type_copy (tree type)
5901 tree t = copy_node (type);
5903 TYPE_POINTER_TO (t) = 0;
5904 TYPE_REFERENCE_TO (t) = 0;
5906 /* Set the canonical type either to a new equivalence class, or
5907 propagate the need for structural equality checks. */
5908 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5909 SET_TYPE_STRUCTURAL_EQUALITY (t);
5910 else
5911 TYPE_CANONICAL (t) = t;
5913 /* Make it its own variant. */
5914 TYPE_MAIN_VARIANT (t) = t;
5915 TYPE_NEXT_VARIANT (t) = 0;
5917 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
5918 whose TREE_TYPE is not t. This can also happen in the Ada
5919 frontend when using subtypes. */
5921 return t;
5924 /* Create a new variant of TYPE, equivalent but distinct. This is so
5925 the caller can modify it. TYPE_CANONICAL for the return type will
5926 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
5927 are considered equal by the language itself (or that both types
5928 require structural equality checks). */
5930 tree
5931 build_variant_type_copy (tree type)
5933 tree t, m = TYPE_MAIN_VARIANT (type);
5935 t = build_distinct_type_copy (type);
5937 /* Since we're building a variant, assume that it is a non-semantic
5938 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
5939 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
5941 /* Add the new type to the chain of variants of TYPE. */
5942 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
5943 TYPE_NEXT_VARIANT (m) = t;
5944 TYPE_MAIN_VARIANT (t) = m;
5946 return t;
5949 /* Return true if the from tree in both tree maps are equal. */
5952 tree_map_base_eq (const void *va, const void *vb)
5954 const struct tree_map_base *const a = (const struct tree_map_base *) va,
5955 *const b = (const struct tree_map_base *) vb;
5956 return (a->from == b->from);
5959 /* Hash a from tree in a tree_base_map. */
5961 unsigned int
5962 tree_map_base_hash (const void *item)
5964 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
5967 /* Return true if this tree map structure is marked for garbage collection
5968 purposes. We simply return true if the from tree is marked, so that this
5969 structure goes away when the from tree goes away. */
5972 tree_map_base_marked_p (const void *p)
5974 return ggc_marked_p (((const struct tree_map_base *) p)->from);
5977 /* Hash a from tree in a tree_map. */
5979 unsigned int
5980 tree_map_hash (const void *item)
5982 return (((const struct tree_map *) item)->hash);
5985 /* Hash a from tree in a tree_decl_map. */
5987 unsigned int
5988 tree_decl_map_hash (const void *item)
5990 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
5993 /* Return the initialization priority for DECL. */
5995 priority_type
5996 decl_init_priority_lookup (tree decl)
5998 struct tree_priority_map *h;
5999 struct tree_map_base in;
6001 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6002 in.from = decl;
6003 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6004 return h ? h->init : DEFAULT_INIT_PRIORITY;
6007 /* Return the finalization priority for DECL. */
6009 priority_type
6010 decl_fini_priority_lookup (tree decl)
6012 struct tree_priority_map *h;
6013 struct tree_map_base in;
6015 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6016 in.from = decl;
6017 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6018 return h ? h->fini : DEFAULT_INIT_PRIORITY;
6021 /* Return the initialization and finalization priority information for
6022 DECL. If there is no previous priority information, a freshly
6023 allocated structure is returned. */
6025 static struct tree_priority_map *
6026 decl_priority_info (tree decl)
6028 struct tree_priority_map in;
6029 struct tree_priority_map *h;
6030 void **loc;
6032 in.base.from = decl;
6033 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
6034 h = (struct tree_priority_map *) *loc;
6035 if (!h)
6037 h = ggc_alloc_cleared_tree_priority_map ();
6038 *loc = h;
6039 h->base.from = decl;
6040 h->init = DEFAULT_INIT_PRIORITY;
6041 h->fini = DEFAULT_INIT_PRIORITY;
6044 return h;
6047 /* Set the initialization priority for DECL to PRIORITY. */
6049 void
6050 decl_init_priority_insert (tree decl, priority_type priority)
6052 struct tree_priority_map *h;
6054 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6055 if (priority == DEFAULT_INIT_PRIORITY)
6056 return;
6057 h = decl_priority_info (decl);
6058 h->init = priority;
6061 /* Set the finalization priority for DECL to PRIORITY. */
6063 void
6064 decl_fini_priority_insert (tree decl, priority_type priority)
6066 struct tree_priority_map *h;
6068 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6069 if (priority == DEFAULT_INIT_PRIORITY)
6070 return;
6071 h = decl_priority_info (decl);
6072 h->fini = priority;
6075 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6077 static void
6078 print_debug_expr_statistics (void)
6080 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6081 (long) htab_size (debug_expr_for_decl),
6082 (long) htab_elements (debug_expr_for_decl),
6083 htab_collisions (debug_expr_for_decl));
6086 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6088 static void
6089 print_value_expr_statistics (void)
6091 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6092 (long) htab_size (value_expr_for_decl),
6093 (long) htab_elements (value_expr_for_decl),
6094 htab_collisions (value_expr_for_decl));
6097 /* Lookup a debug expression for FROM, and return it if we find one. */
6099 tree
6100 decl_debug_expr_lookup (tree from)
6102 struct tree_decl_map *h, in;
6103 in.base.from = from;
6105 h = (struct tree_decl_map *)
6106 htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
6107 if (h)
6108 return h->to;
6109 return NULL_TREE;
6112 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6114 void
6115 decl_debug_expr_insert (tree from, tree to)
6117 struct tree_decl_map *h;
6118 void **loc;
6120 h = ggc_alloc_tree_decl_map ();
6121 h->base.from = from;
6122 h->to = to;
6123 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
6124 INSERT);
6125 *(struct tree_decl_map **) loc = h;
6128 /* Lookup a value expression for FROM, and return it if we find one. */
6130 tree
6131 decl_value_expr_lookup (tree from)
6133 struct tree_decl_map *h, in;
6134 in.base.from = from;
6136 h = (struct tree_decl_map *)
6137 htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
6138 if (h)
6139 return h->to;
6140 return NULL_TREE;
6143 /* Insert a mapping FROM->TO in the value expression hashtable. */
6145 void
6146 decl_value_expr_insert (tree from, tree to)
6148 struct tree_decl_map *h;
6149 void **loc;
6151 h = ggc_alloc_tree_decl_map ();
6152 h->base.from = from;
6153 h->to = to;
6154 loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6155 INSERT);
6156 *(struct tree_decl_map **) loc = h;
6159 /* Lookup a vector of debug arguments for FROM, and return it if we
6160 find one. */
6162 vec<tree, va_gc> **
6163 decl_debug_args_lookup (tree from)
6165 struct tree_vec_map *h, in;
6167 if (!DECL_HAS_DEBUG_ARGS_P (from))
6168 return NULL;
6169 gcc_checking_assert (debug_args_for_decl != NULL);
6170 in.base.from = from;
6171 h = (struct tree_vec_map *)
6172 htab_find_with_hash (debug_args_for_decl, &in, DECL_UID (from));
6173 if (h)
6174 return &h->to;
6175 return NULL;
6178 /* Insert a mapping FROM->empty vector of debug arguments in the value
6179 expression hashtable. */
6181 vec<tree, va_gc> **
6182 decl_debug_args_insert (tree from)
6184 struct tree_vec_map *h;
6185 void **loc;
6187 if (DECL_HAS_DEBUG_ARGS_P (from))
6188 return decl_debug_args_lookup (from);
6189 if (debug_args_for_decl == NULL)
6190 debug_args_for_decl = htab_create_ggc (64, tree_vec_map_hash,
6191 tree_vec_map_eq, 0);
6192 h = ggc_alloc_tree_vec_map ();
6193 h->base.from = from;
6194 h->to = NULL;
6195 loc = htab_find_slot_with_hash (debug_args_for_decl, h, DECL_UID (from),
6196 INSERT);
6197 *(struct tree_vec_map **) loc = h;
6198 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6199 return &h->to;
6202 /* Hashing of types so that we don't make duplicates.
6203 The entry point is `type_hash_canon'. */
6205 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6206 with types in the TREE_VALUE slots), by adding the hash codes
6207 of the individual types. */
6209 static unsigned int
6210 type_hash_list (const_tree list, hashval_t hashcode)
6212 const_tree tail;
6214 for (tail = list; tail; tail = TREE_CHAIN (tail))
6215 if (TREE_VALUE (tail) != error_mark_node)
6216 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
6217 hashcode);
6219 return hashcode;
6222 /* These are the Hashtable callback functions. */
6224 /* Returns true iff the types are equivalent. */
6226 static int
6227 type_hash_eq (const void *va, const void *vb)
6229 const struct type_hash *const a = (const struct type_hash *) va,
6230 *const b = (const struct type_hash *) vb;
6232 /* First test the things that are the same for all types. */
6233 if (a->hash != b->hash
6234 || TREE_CODE (a->type) != TREE_CODE (b->type)
6235 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6236 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6237 TYPE_ATTRIBUTES (b->type))
6238 || (TREE_CODE (a->type) != COMPLEX_TYPE
6239 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6240 return 0;
6242 /* Be careful about comparing arrays before and after the element type
6243 has been completed; don't compare TYPE_ALIGN unless both types are
6244 complete. */
6245 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6246 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6247 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6248 return 0;
6250 switch (TREE_CODE (a->type))
6252 case VOID_TYPE:
6253 case COMPLEX_TYPE:
6254 case POINTER_TYPE:
6255 case REFERENCE_TYPE:
6256 case NULLPTR_TYPE:
6257 return 1;
6259 case VECTOR_TYPE:
6260 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6262 case ENUMERAL_TYPE:
6263 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6264 && !(TYPE_VALUES (a->type)
6265 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6266 && TYPE_VALUES (b->type)
6267 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6268 && type_list_equal (TYPE_VALUES (a->type),
6269 TYPE_VALUES (b->type))))
6270 return 0;
6272 /* ... fall through ... */
6274 case INTEGER_TYPE:
6275 case REAL_TYPE:
6276 case BOOLEAN_TYPE:
6277 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6278 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6279 TYPE_MAX_VALUE (b->type)))
6280 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6281 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6282 TYPE_MIN_VALUE (b->type))));
6284 case FIXED_POINT_TYPE:
6285 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6287 case OFFSET_TYPE:
6288 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6290 case METHOD_TYPE:
6291 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6292 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6293 || (TYPE_ARG_TYPES (a->type)
6294 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6295 && TYPE_ARG_TYPES (b->type)
6296 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6297 && type_list_equal (TYPE_ARG_TYPES (a->type),
6298 TYPE_ARG_TYPES (b->type)))))
6299 break;
6300 return 0;
6301 case ARRAY_TYPE:
6302 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6304 case RECORD_TYPE:
6305 case UNION_TYPE:
6306 case QUAL_UNION_TYPE:
6307 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6308 || (TYPE_FIELDS (a->type)
6309 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6310 && TYPE_FIELDS (b->type)
6311 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6312 && type_list_equal (TYPE_FIELDS (a->type),
6313 TYPE_FIELDS (b->type))));
6315 case FUNCTION_TYPE:
6316 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6317 || (TYPE_ARG_TYPES (a->type)
6318 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6319 && TYPE_ARG_TYPES (b->type)
6320 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6321 && type_list_equal (TYPE_ARG_TYPES (a->type),
6322 TYPE_ARG_TYPES (b->type))))
6323 break;
6324 return 0;
6326 default:
6327 return 0;
6330 if (lang_hooks.types.type_hash_eq != NULL)
6331 return lang_hooks.types.type_hash_eq (a->type, b->type);
6333 return 1;
6336 /* Return the cached hash value. */
6338 static hashval_t
6339 type_hash_hash (const void *item)
6341 return ((const struct type_hash *) item)->hash;
6344 /* Look in the type hash table for a type isomorphic to TYPE.
6345 If one is found, return it. Otherwise return 0. */
6347 tree
6348 type_hash_lookup (hashval_t hashcode, tree type)
6350 struct type_hash *h, in;
6352 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6353 must call that routine before comparing TYPE_ALIGNs. */
6354 layout_type (type);
6356 in.hash = hashcode;
6357 in.type = type;
6359 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
6360 hashcode);
6361 if (h)
6362 return h->type;
6363 return NULL_TREE;
6366 /* Add an entry to the type-hash-table
6367 for a type TYPE whose hash code is HASHCODE. */
6369 void
6370 type_hash_add (hashval_t hashcode, tree type)
6372 struct type_hash *h;
6373 void **loc;
6375 h = ggc_alloc_type_hash ();
6376 h->hash = hashcode;
6377 h->type = type;
6378 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
6379 *loc = (void *)h;
6382 /* Given TYPE, and HASHCODE its hash code, return the canonical
6383 object for an identical type if one already exists.
6384 Otherwise, return TYPE, and record it as the canonical object.
6386 To use this function, first create a type of the sort you want.
6387 Then compute its hash code from the fields of the type that
6388 make it different from other similar types.
6389 Then call this function and use the value. */
6391 tree
6392 type_hash_canon (unsigned int hashcode, tree type)
6394 tree t1;
6396 /* The hash table only contains main variants, so ensure that's what we're
6397 being passed. */
6398 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6400 /* See if the type is in the hash table already. If so, return it.
6401 Otherwise, add the type. */
6402 t1 = type_hash_lookup (hashcode, type);
6403 if (t1 != 0)
6405 if (GATHER_STATISTICS)
6407 tree_code_counts[(int) TREE_CODE (type)]--;
6408 tree_node_counts[(int) t_kind]--;
6409 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6411 return t1;
6413 else
6415 type_hash_add (hashcode, type);
6416 return type;
6420 /* See if the data pointed to by the type hash table is marked. We consider
6421 it marked if the type is marked or if a debug type number or symbol
6422 table entry has been made for the type. */
6424 static int
6425 type_hash_marked_p (const void *p)
6427 const_tree const type = ((const struct type_hash *) p)->type;
6429 return ggc_marked_p (type);
6432 static void
6433 print_type_hash_statistics (void)
6435 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6436 (long) htab_size (type_hash_table),
6437 (long) htab_elements (type_hash_table),
6438 htab_collisions (type_hash_table));
6441 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6442 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6443 by adding the hash codes of the individual attributes. */
6445 static unsigned int
6446 attribute_hash_list (const_tree list, hashval_t hashcode)
6448 const_tree tail;
6450 for (tail = list; tail; tail = TREE_CHAIN (tail))
6451 /* ??? Do we want to add in TREE_VALUE too? */
6452 hashcode = iterative_hash_object
6453 (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)), hashcode);
6454 return hashcode;
6457 /* Given two lists of attributes, return true if list l2 is
6458 equivalent to l1. */
6461 attribute_list_equal (const_tree l1, const_tree l2)
6463 if (l1 == l2)
6464 return 1;
6466 return attribute_list_contained (l1, l2)
6467 && attribute_list_contained (l2, l1);
6470 /* Given two lists of attributes, return true if list L2 is
6471 completely contained within L1. */
6472 /* ??? This would be faster if attribute names were stored in a canonicalized
6473 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6474 must be used to show these elements are equivalent (which they are). */
6475 /* ??? It's not clear that attributes with arguments will always be handled
6476 correctly. */
6479 attribute_list_contained (const_tree l1, const_tree l2)
6481 const_tree t1, t2;
6483 /* First check the obvious, maybe the lists are identical. */
6484 if (l1 == l2)
6485 return 1;
6487 /* Maybe the lists are similar. */
6488 for (t1 = l1, t2 = l2;
6489 t1 != 0 && t2 != 0
6490 && get_attribute_name (t1) == get_attribute_name (t2)
6491 && TREE_VALUE (t1) == TREE_VALUE (t2);
6492 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6495 /* Maybe the lists are equal. */
6496 if (t1 == 0 && t2 == 0)
6497 return 1;
6499 for (; t2 != 0; t2 = TREE_CHAIN (t2))
6501 const_tree attr;
6502 /* This CONST_CAST is okay because lookup_attribute does not
6503 modify its argument and the return value is assigned to a
6504 const_tree. */
6505 for (attr = lookup_ident_attribute (get_attribute_name (t2), CONST_CAST_TREE(l1));
6506 attr != NULL_TREE && !attribute_value_equal (t2, attr);
6507 attr = lookup_ident_attribute (get_attribute_name (t2), TREE_CHAIN (attr)))
6510 if (attr == NULL_TREE)
6511 return 0;
6514 return 1;
6517 /* Given two lists of types
6518 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6519 return 1 if the lists contain the same types in the same order.
6520 Also, the TREE_PURPOSEs must match. */
6523 type_list_equal (const_tree l1, const_tree l2)
6525 const_tree t1, t2;
6527 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6528 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6529 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6530 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6531 && (TREE_TYPE (TREE_PURPOSE (t1))
6532 == TREE_TYPE (TREE_PURPOSE (t2))))))
6533 return 0;
6535 return t1 == t2;
6538 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6539 given by TYPE. If the argument list accepts variable arguments,
6540 then this function counts only the ordinary arguments. */
6543 type_num_arguments (const_tree type)
6545 int i = 0;
6546 tree t;
6548 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6549 /* If the function does not take a variable number of arguments,
6550 the last element in the list will have type `void'. */
6551 if (VOID_TYPE_P (TREE_VALUE (t)))
6552 break;
6553 else
6554 ++i;
6556 return i;
6559 /* Nonzero if integer constants T1 and T2
6560 represent the same constant value. */
6563 tree_int_cst_equal (const_tree t1, const_tree t2)
6565 if (t1 == t2)
6566 return 1;
6568 if (t1 == 0 || t2 == 0)
6569 return 0;
6571 if (TREE_CODE (t1) == INTEGER_CST
6572 && TREE_CODE (t2) == INTEGER_CST
6573 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6574 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
6575 return 1;
6577 return 0;
6580 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
6581 The precise way of comparison depends on their data type. */
6584 tree_int_cst_lt (const_tree t1, const_tree t2)
6586 if (t1 == t2)
6587 return 0;
6589 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
6591 int t1_sgn = tree_int_cst_sgn (t1);
6592 int t2_sgn = tree_int_cst_sgn (t2);
6594 if (t1_sgn < t2_sgn)
6595 return 1;
6596 else if (t1_sgn > t2_sgn)
6597 return 0;
6598 /* Otherwise, both are non-negative, so we compare them as
6599 unsigned just in case one of them would overflow a signed
6600 type. */
6602 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
6603 return INT_CST_LT (t1, t2);
6605 return INT_CST_LT_UNSIGNED (t1, t2);
6608 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
6611 tree_int_cst_compare (const_tree t1, const_tree t2)
6613 if (tree_int_cst_lt (t1, t2))
6614 return -1;
6615 else if (tree_int_cst_lt (t2, t1))
6616 return 1;
6617 else
6618 return 0;
6621 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
6622 the host. If POS is zero, the value can be represented in a single
6623 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
6624 be represented in a single unsigned HOST_WIDE_INT. */
6627 host_integerp (const_tree t, int pos)
6629 if (t == NULL_TREE)
6630 return 0;
6632 return (TREE_CODE (t) == INTEGER_CST
6633 && ((TREE_INT_CST_HIGH (t) == 0
6634 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
6635 || (! pos && TREE_INT_CST_HIGH (t) == -1
6636 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
6637 && !TYPE_UNSIGNED (TREE_TYPE (t)))
6638 || (pos && TREE_INT_CST_HIGH (t) == 0)));
6641 /* Return the HOST_WIDE_INT least significant bits of T if it is an
6642 INTEGER_CST and there is no overflow. POS is nonzero if the result must
6643 be non-negative. We must be able to satisfy the above conditions. */
6645 HOST_WIDE_INT
6646 tree_low_cst (const_tree t, int pos)
6648 gcc_assert (host_integerp (t, pos));
6649 return TREE_INT_CST_LOW (t);
6652 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
6653 kind INTEGER_CST. This makes sure to properly sign-extend the
6654 constant. */
6656 HOST_WIDE_INT
6657 size_low_cst (const_tree t)
6659 double_int d = tree_to_double_int (t);
6660 return d.sext (TYPE_PRECISION (TREE_TYPE (t))).low;
6663 /* Return the most significant (sign) bit of T. */
6666 tree_int_cst_sign_bit (const_tree t)
6668 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
6669 unsigned HOST_WIDE_INT w;
6671 if (bitno < HOST_BITS_PER_WIDE_INT)
6672 w = TREE_INT_CST_LOW (t);
6673 else
6675 w = TREE_INT_CST_HIGH (t);
6676 bitno -= HOST_BITS_PER_WIDE_INT;
6679 return (w >> bitno) & 1;
6682 /* Return an indication of the sign of the integer constant T.
6683 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6684 Note that -1 will never be returned if T's type is unsigned. */
6687 tree_int_cst_sgn (const_tree t)
6689 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
6690 return 0;
6691 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6692 return 1;
6693 else if (TREE_INT_CST_HIGH (t) < 0)
6694 return -1;
6695 else
6696 return 1;
6699 /* Return the minimum number of bits needed to represent VALUE in a
6700 signed or unsigned type, UNSIGNEDP says which. */
6702 unsigned int
6703 tree_int_cst_min_precision (tree value, bool unsignedp)
6705 int log;
6707 /* If the value is negative, compute its negative minus 1. The latter
6708 adjustment is because the absolute value of the largest negative value
6709 is one larger than the largest positive value. This is equivalent to
6710 a bit-wise negation, so use that operation instead. */
6712 if (tree_int_cst_sgn (value) < 0)
6713 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6715 /* Return the number of bits needed, taking into account the fact
6716 that we need one more bit for a signed than unsigned type. */
6718 if (integer_zerop (value))
6719 log = 0;
6720 else
6721 log = tree_floor_log2 (value);
6723 return log + 1 + !unsignedp;
6726 /* Compare two constructor-element-type constants. Return 1 if the lists
6727 are known to be equal; otherwise return 0. */
6730 simple_cst_list_equal (const_tree l1, const_tree l2)
6732 while (l1 != NULL_TREE && l2 != NULL_TREE)
6734 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
6735 return 0;
6737 l1 = TREE_CHAIN (l1);
6738 l2 = TREE_CHAIN (l2);
6741 return l1 == l2;
6744 /* Return truthvalue of whether T1 is the same tree structure as T2.
6745 Return 1 if they are the same.
6746 Return 0 if they are understandably different.
6747 Return -1 if either contains tree structure not understood by
6748 this function. */
6751 simple_cst_equal (const_tree t1, const_tree t2)
6753 enum tree_code code1, code2;
6754 int cmp;
6755 int i;
6757 if (t1 == t2)
6758 return 1;
6759 if (t1 == 0 || t2 == 0)
6760 return 0;
6762 code1 = TREE_CODE (t1);
6763 code2 = TREE_CODE (t2);
6765 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6767 if (CONVERT_EXPR_CODE_P (code2)
6768 || code2 == NON_LVALUE_EXPR)
6769 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6770 else
6771 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6774 else if (CONVERT_EXPR_CODE_P (code2)
6775 || code2 == NON_LVALUE_EXPR)
6776 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6778 if (code1 != code2)
6779 return 0;
6781 switch (code1)
6783 case INTEGER_CST:
6784 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6785 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
6787 case REAL_CST:
6788 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
6790 case FIXED_CST:
6791 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6793 case STRING_CST:
6794 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6795 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6796 TREE_STRING_LENGTH (t1)));
6798 case CONSTRUCTOR:
6800 unsigned HOST_WIDE_INT idx;
6801 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
6802 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
6804 if (vec_safe_length (v1) != vec_safe_length (v2))
6805 return false;
6807 for (idx = 0; idx < vec_safe_length (v1); ++idx)
6808 /* ??? Should we handle also fields here? */
6809 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
6810 return false;
6811 return true;
6814 case SAVE_EXPR:
6815 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6817 case CALL_EXPR:
6818 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6819 if (cmp <= 0)
6820 return cmp;
6821 if (call_expr_nargs (t1) != call_expr_nargs (t2))
6822 return 0;
6824 const_tree arg1, arg2;
6825 const_call_expr_arg_iterator iter1, iter2;
6826 for (arg1 = first_const_call_expr_arg (t1, &iter1),
6827 arg2 = first_const_call_expr_arg (t2, &iter2);
6828 arg1 && arg2;
6829 arg1 = next_const_call_expr_arg (&iter1),
6830 arg2 = next_const_call_expr_arg (&iter2))
6832 cmp = simple_cst_equal (arg1, arg2);
6833 if (cmp <= 0)
6834 return cmp;
6836 return arg1 == arg2;
6839 case TARGET_EXPR:
6840 /* Special case: if either target is an unallocated VAR_DECL,
6841 it means that it's going to be unified with whatever the
6842 TARGET_EXPR is really supposed to initialize, so treat it
6843 as being equivalent to anything. */
6844 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
6845 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
6846 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
6847 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
6848 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
6849 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
6850 cmp = 1;
6851 else
6852 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6854 if (cmp <= 0)
6855 return cmp;
6857 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
6859 case WITH_CLEANUP_EXPR:
6860 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6861 if (cmp <= 0)
6862 return cmp;
6864 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
6866 case COMPONENT_REF:
6867 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
6868 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6870 return 0;
6872 case VAR_DECL:
6873 case PARM_DECL:
6874 case CONST_DECL:
6875 case FUNCTION_DECL:
6876 return 0;
6878 default:
6879 break;
6882 /* This general rule works for most tree codes. All exceptions should be
6883 handled above. If this is a language-specific tree code, we can't
6884 trust what might be in the operand, so say we don't know
6885 the situation. */
6886 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
6887 return -1;
6889 switch (TREE_CODE_CLASS (code1))
6891 case tcc_unary:
6892 case tcc_binary:
6893 case tcc_comparison:
6894 case tcc_expression:
6895 case tcc_reference:
6896 case tcc_statement:
6897 cmp = 1;
6898 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
6900 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
6901 if (cmp <= 0)
6902 return cmp;
6905 return cmp;
6907 default:
6908 return -1;
6912 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
6913 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
6914 than U, respectively. */
6917 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
6919 if (tree_int_cst_sgn (t) < 0)
6920 return -1;
6921 else if (TREE_INT_CST_HIGH (t) != 0)
6922 return 1;
6923 else if (TREE_INT_CST_LOW (t) == u)
6924 return 0;
6925 else if (TREE_INT_CST_LOW (t) < u)
6926 return -1;
6927 else
6928 return 1;
6931 /* Return true if SIZE represents a constant size that is in bounds of
6932 what the middle-end and the backend accepts (covering not more than
6933 half of the address-space). */
6935 bool
6936 valid_constant_size_p (const_tree size)
6938 if (! host_integerp (size, 1)
6939 || TREE_OVERFLOW (size)
6940 || tree_int_cst_sign_bit (size) != 0)
6941 return false;
6942 return true;
6945 /* Return true if CODE represents an associative tree code. Otherwise
6946 return false. */
6947 bool
6948 associative_tree_code (enum tree_code code)
6950 switch (code)
6952 case BIT_IOR_EXPR:
6953 case BIT_AND_EXPR:
6954 case BIT_XOR_EXPR:
6955 case PLUS_EXPR:
6956 case MULT_EXPR:
6957 case MIN_EXPR:
6958 case MAX_EXPR:
6959 return true;
6961 default:
6962 break;
6964 return false;
6967 /* Return true if CODE represents a commutative tree code. Otherwise
6968 return false. */
6969 bool
6970 commutative_tree_code (enum tree_code code)
6972 switch (code)
6974 case PLUS_EXPR:
6975 case MULT_EXPR:
6976 case MULT_HIGHPART_EXPR:
6977 case MIN_EXPR:
6978 case MAX_EXPR:
6979 case BIT_IOR_EXPR:
6980 case BIT_XOR_EXPR:
6981 case BIT_AND_EXPR:
6982 case NE_EXPR:
6983 case EQ_EXPR:
6984 case UNORDERED_EXPR:
6985 case ORDERED_EXPR:
6986 case UNEQ_EXPR:
6987 case LTGT_EXPR:
6988 case TRUTH_AND_EXPR:
6989 case TRUTH_XOR_EXPR:
6990 case TRUTH_OR_EXPR:
6991 case WIDEN_MULT_EXPR:
6992 case VEC_WIDEN_MULT_HI_EXPR:
6993 case VEC_WIDEN_MULT_LO_EXPR:
6994 case VEC_WIDEN_MULT_EVEN_EXPR:
6995 case VEC_WIDEN_MULT_ODD_EXPR:
6996 return true;
6998 default:
6999 break;
7001 return false;
7004 /* Return true if CODE represents a ternary tree code for which the
7005 first two operands are commutative. Otherwise return false. */
7006 bool
7007 commutative_ternary_tree_code (enum tree_code code)
7009 switch (code)
7011 case WIDEN_MULT_PLUS_EXPR:
7012 case WIDEN_MULT_MINUS_EXPR:
7013 return true;
7015 default:
7016 break;
7018 return false;
7021 /* Generate a hash value for an expression. This can be used iteratively
7022 by passing a previous result as the VAL argument.
7024 This function is intended to produce the same hash for expressions which
7025 would compare equal using operand_equal_p. */
7027 hashval_t
7028 iterative_hash_expr (const_tree t, hashval_t val)
7030 int i;
7031 enum tree_code code;
7032 char tclass;
7034 if (t == NULL_TREE)
7035 return iterative_hash_hashval_t (0, val);
7037 code = TREE_CODE (t);
7039 switch (code)
7041 /* Alas, constants aren't shared, so we can't rely on pointer
7042 identity. */
7043 case INTEGER_CST:
7044 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
7045 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
7046 case REAL_CST:
7048 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7050 return iterative_hash_hashval_t (val2, val);
7052 case FIXED_CST:
7054 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7056 return iterative_hash_hashval_t (val2, val);
7058 case STRING_CST:
7059 return iterative_hash (TREE_STRING_POINTER (t),
7060 TREE_STRING_LENGTH (t), val);
7061 case COMPLEX_CST:
7062 val = iterative_hash_expr (TREE_REALPART (t), val);
7063 return iterative_hash_expr (TREE_IMAGPART (t), val);
7064 case VECTOR_CST:
7066 unsigned i;
7067 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7068 val = iterative_hash_expr (VECTOR_CST_ELT (t, i), val);
7069 return val;
7071 case SSA_NAME:
7072 /* We can just compare by pointer. */
7073 return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
7074 case PLACEHOLDER_EXPR:
7075 /* The node itself doesn't matter. */
7076 return val;
7077 case TREE_LIST:
7078 /* A list of expressions, for a CALL_EXPR or as the elements of a
7079 VECTOR_CST. */
7080 for (; t; t = TREE_CHAIN (t))
7081 val = iterative_hash_expr (TREE_VALUE (t), val);
7082 return val;
7083 case CONSTRUCTOR:
7085 unsigned HOST_WIDE_INT idx;
7086 tree field, value;
7087 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7089 val = iterative_hash_expr (field, val);
7090 val = iterative_hash_expr (value, val);
7092 return val;
7094 case MEM_REF:
7096 /* The type of the second operand is relevant, except for
7097 its top-level qualifiers. */
7098 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (t, 1)));
7100 val = iterative_hash_object (TYPE_HASH (type), val);
7102 /* We could use the standard hash computation from this point
7103 on. */
7104 val = iterative_hash_object (code, val);
7105 val = iterative_hash_expr (TREE_OPERAND (t, 1), val);
7106 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7107 return val;
7109 case FUNCTION_DECL:
7110 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7111 Otherwise nodes that compare equal according to operand_equal_p might
7112 get different hash codes. However, don't do this for machine specific
7113 or front end builtins, since the function code is overloaded in those
7114 cases. */
7115 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7116 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7118 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7119 code = TREE_CODE (t);
7121 /* FALL THROUGH */
7122 default:
7123 tclass = TREE_CODE_CLASS (code);
7125 if (tclass == tcc_declaration)
7127 /* DECL's have a unique ID */
7128 val = iterative_hash_host_wide_int (DECL_UID (t), val);
7130 else
7132 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7134 val = iterative_hash_object (code, val);
7136 /* Don't hash the type, that can lead to having nodes which
7137 compare equal according to operand_equal_p, but which
7138 have different hash codes. */
7139 if (CONVERT_EXPR_CODE_P (code)
7140 || code == NON_LVALUE_EXPR)
7142 /* Make sure to include signness in the hash computation. */
7143 val += TYPE_UNSIGNED (TREE_TYPE (t));
7144 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7147 else if (commutative_tree_code (code))
7149 /* It's a commutative expression. We want to hash it the same
7150 however it appears. We do this by first hashing both operands
7151 and then rehashing based on the order of their independent
7152 hashes. */
7153 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
7154 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
7155 hashval_t t;
7157 if (one > two)
7158 t = one, one = two, two = t;
7160 val = iterative_hash_hashval_t (one, val);
7161 val = iterative_hash_hashval_t (two, val);
7163 else
7164 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7165 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
7167 return val;
7171 /* Generate a hash value for a pair of expressions. This can be used
7172 iteratively by passing a previous result as the VAL argument.
7174 The same hash value is always returned for a given pair of expressions,
7175 regardless of the order in which they are presented. This is useful in
7176 hashing the operands of commutative functions. */
7178 hashval_t
7179 iterative_hash_exprs_commutative (const_tree t1,
7180 const_tree t2, hashval_t val)
7182 hashval_t one = iterative_hash_expr (t1, 0);
7183 hashval_t two = iterative_hash_expr (t2, 0);
7184 hashval_t t;
7186 if (one > two)
7187 t = one, one = two, two = t;
7188 val = iterative_hash_hashval_t (one, val);
7189 val = iterative_hash_hashval_t (two, val);
7191 return val;
7194 /* Constructors for pointer, array and function types.
7195 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7196 constructed by language-dependent code, not here.) */
7198 /* Construct, lay out and return the type of pointers to TO_TYPE with
7199 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7200 reference all of memory. If such a type has already been
7201 constructed, reuse it. */
7203 tree
7204 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
7205 bool can_alias_all)
7207 tree t;
7209 if (to_type == error_mark_node)
7210 return error_mark_node;
7212 /* If the pointed-to type has the may_alias attribute set, force
7213 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7214 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7215 can_alias_all = true;
7217 /* In some cases, languages will have things that aren't a POINTER_TYPE
7218 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7219 In that case, return that type without regard to the rest of our
7220 operands.
7222 ??? This is a kludge, but consistent with the way this function has
7223 always operated and there doesn't seem to be a good way to avoid this
7224 at the moment. */
7225 if (TYPE_POINTER_TO (to_type) != 0
7226 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7227 return TYPE_POINTER_TO (to_type);
7229 /* First, if we already have a type for pointers to TO_TYPE and it's
7230 the proper mode, use it. */
7231 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7232 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7233 return t;
7235 t = make_node (POINTER_TYPE);
7237 TREE_TYPE (t) = to_type;
7238 SET_TYPE_MODE (t, mode);
7239 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7240 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7241 TYPE_POINTER_TO (to_type) = t;
7243 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7244 SET_TYPE_STRUCTURAL_EQUALITY (t);
7245 else if (TYPE_CANONICAL (to_type) != to_type)
7246 TYPE_CANONICAL (t)
7247 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7248 mode, can_alias_all);
7250 /* Lay out the type. This function has many callers that are concerned
7251 with expression-construction, and this simplifies them all. */
7252 layout_type (t);
7254 return t;
7257 /* By default build pointers in ptr_mode. */
7259 tree
7260 build_pointer_type (tree to_type)
7262 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7263 : TYPE_ADDR_SPACE (to_type);
7264 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7265 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7268 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7270 tree
7271 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7272 bool can_alias_all)
7274 tree t;
7276 if (to_type == error_mark_node)
7277 return error_mark_node;
7279 /* If the pointed-to type has the may_alias attribute set, force
7280 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7281 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7282 can_alias_all = true;
7284 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7285 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7286 In that case, return that type without regard to the rest of our
7287 operands.
7289 ??? This is a kludge, but consistent with the way this function has
7290 always operated and there doesn't seem to be a good way to avoid this
7291 at the moment. */
7292 if (TYPE_REFERENCE_TO (to_type) != 0
7293 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7294 return TYPE_REFERENCE_TO (to_type);
7296 /* First, if we already have a type for pointers to TO_TYPE and it's
7297 the proper mode, use it. */
7298 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7299 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7300 return t;
7302 t = make_node (REFERENCE_TYPE);
7304 TREE_TYPE (t) = to_type;
7305 SET_TYPE_MODE (t, mode);
7306 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7307 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7308 TYPE_REFERENCE_TO (to_type) = t;
7310 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7311 SET_TYPE_STRUCTURAL_EQUALITY (t);
7312 else if (TYPE_CANONICAL (to_type) != to_type)
7313 TYPE_CANONICAL (t)
7314 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7315 mode, can_alias_all);
7317 layout_type (t);
7319 return t;
7323 /* Build the node for the type of references-to-TO_TYPE by default
7324 in ptr_mode. */
7326 tree
7327 build_reference_type (tree to_type)
7329 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7330 : TYPE_ADDR_SPACE (to_type);
7331 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7332 return build_reference_type_for_mode (to_type, pointer_mode, false);
7335 /* Build a type that is compatible with t but has no cv quals anywhere
7336 in its type, thus
7338 const char *const *const * -> char ***. */
7340 tree
7341 build_type_no_quals (tree t)
7343 switch (TREE_CODE (t))
7345 case POINTER_TYPE:
7346 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7347 TYPE_MODE (t),
7348 TYPE_REF_CAN_ALIAS_ALL (t));
7349 case REFERENCE_TYPE:
7350 return
7351 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7352 TYPE_MODE (t),
7353 TYPE_REF_CAN_ALIAS_ALL (t));
7354 default:
7355 return TYPE_MAIN_VARIANT (t);
7359 #define MAX_INT_CACHED_PREC \
7360 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7361 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7363 /* Builds a signed or unsigned integer type of precision PRECISION.
7364 Used for C bitfields whose precision does not match that of
7365 built-in target types. */
7366 tree
7367 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7368 int unsignedp)
7370 tree itype, ret;
7372 if (unsignedp)
7373 unsignedp = MAX_INT_CACHED_PREC + 1;
7375 if (precision <= MAX_INT_CACHED_PREC)
7377 itype = nonstandard_integer_type_cache[precision + unsignedp];
7378 if (itype)
7379 return itype;
7382 itype = make_node (INTEGER_TYPE);
7383 TYPE_PRECISION (itype) = precision;
7385 if (unsignedp)
7386 fixup_unsigned_type (itype);
7387 else
7388 fixup_signed_type (itype);
7390 ret = itype;
7391 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
7392 ret = type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
7393 if (precision <= MAX_INT_CACHED_PREC)
7394 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7396 return ret;
7399 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7400 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7401 is true, reuse such a type that has already been constructed. */
7403 static tree
7404 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7406 tree itype = make_node (INTEGER_TYPE);
7407 hashval_t hashcode = 0;
7409 TREE_TYPE (itype) = type;
7411 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7412 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7414 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7415 SET_TYPE_MODE (itype, TYPE_MODE (type));
7416 TYPE_SIZE (itype) = TYPE_SIZE (type);
7417 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7418 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7419 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7421 if (!shared)
7422 return itype;
7424 if ((TYPE_MIN_VALUE (itype)
7425 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7426 || (TYPE_MAX_VALUE (itype)
7427 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7429 /* Since we cannot reliably merge this type, we need to compare it using
7430 structural equality checks. */
7431 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7432 return itype;
7435 hashcode = iterative_hash_expr (TYPE_MIN_VALUE (itype), hashcode);
7436 hashcode = iterative_hash_expr (TYPE_MAX_VALUE (itype), hashcode);
7437 hashcode = iterative_hash_hashval_t (TYPE_HASH (type), hashcode);
7438 itype = type_hash_canon (hashcode, itype);
7440 return itype;
7443 /* Wrapper around build_range_type_1 with SHARED set to true. */
7445 tree
7446 build_range_type (tree type, tree lowval, tree highval)
7448 return build_range_type_1 (type, lowval, highval, true);
7451 /* Wrapper around build_range_type_1 with SHARED set to false. */
7453 tree
7454 build_nonshared_range_type (tree type, tree lowval, tree highval)
7456 return build_range_type_1 (type, lowval, highval, false);
7459 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7460 MAXVAL should be the maximum value in the domain
7461 (one less than the length of the array).
7463 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7464 We don't enforce this limit, that is up to caller (e.g. language front end).
7465 The limit exists because the result is a signed type and we don't handle
7466 sizes that use more than one HOST_WIDE_INT. */
7468 tree
7469 build_index_type (tree maxval)
7471 return build_range_type (sizetype, size_zero_node, maxval);
7474 /* Return true if the debug information for TYPE, a subtype, should be emitted
7475 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7476 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7477 debug info and doesn't reflect the source code. */
7479 bool
7480 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7482 tree base_type = TREE_TYPE (type), low, high;
7484 /* Subrange types have a base type which is an integral type. */
7485 if (!INTEGRAL_TYPE_P (base_type))
7486 return false;
7488 /* Get the real bounds of the subtype. */
7489 if (lang_hooks.types.get_subrange_bounds)
7490 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7491 else
7493 low = TYPE_MIN_VALUE (type);
7494 high = TYPE_MAX_VALUE (type);
7497 /* If the type and its base type have the same representation and the same
7498 name, then the type is not a subrange but a copy of the base type. */
7499 if ((TREE_CODE (base_type) == INTEGER_TYPE
7500 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7501 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7502 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7503 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type)))
7505 tree type_name = TYPE_NAME (type);
7506 tree base_type_name = TYPE_NAME (base_type);
7508 if (type_name && TREE_CODE (type_name) == TYPE_DECL)
7509 type_name = DECL_NAME (type_name);
7511 if (base_type_name && TREE_CODE (base_type_name) == TYPE_DECL)
7512 base_type_name = DECL_NAME (base_type_name);
7514 if (type_name == base_type_name)
7515 return false;
7518 if (lowval)
7519 *lowval = low;
7520 if (highval)
7521 *highval = high;
7522 return true;
7525 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7526 and number of elements specified by the range of values of INDEX_TYPE.
7527 If SHARED is true, reuse such a type that has already been constructed. */
7529 static tree
7530 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7532 tree t;
7534 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7536 error ("arrays of functions are not meaningful");
7537 elt_type = integer_type_node;
7540 t = make_node (ARRAY_TYPE);
7541 TREE_TYPE (t) = elt_type;
7542 TYPE_DOMAIN (t) = index_type;
7543 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7544 layout_type (t);
7546 /* If the element type is incomplete at this point we get marked for
7547 structural equality. Do not record these types in the canonical
7548 type hashtable. */
7549 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7550 return t;
7552 if (shared)
7554 hashval_t hashcode = iterative_hash_object (TYPE_HASH (elt_type), 0);
7555 if (index_type)
7556 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
7557 t = type_hash_canon (hashcode, t);
7560 if (TYPE_CANONICAL (t) == t)
7562 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7563 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7564 SET_TYPE_STRUCTURAL_EQUALITY (t);
7565 else if (TYPE_CANONICAL (elt_type) != elt_type
7566 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7567 TYPE_CANONICAL (t)
7568 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7569 index_type
7570 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7571 shared);
7574 return t;
7577 /* Wrapper around build_array_type_1 with SHARED set to true. */
7579 tree
7580 build_array_type (tree elt_type, tree index_type)
7582 return build_array_type_1 (elt_type, index_type, true);
7585 /* Wrapper around build_array_type_1 with SHARED set to false. */
7587 tree
7588 build_nonshared_array_type (tree elt_type, tree index_type)
7590 return build_array_type_1 (elt_type, index_type, false);
7593 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7594 sizetype. */
7596 tree
7597 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7599 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7602 /* Recursively examines the array elements of TYPE, until a non-array
7603 element type is found. */
7605 tree
7606 strip_array_types (tree type)
7608 while (TREE_CODE (type) == ARRAY_TYPE)
7609 type = TREE_TYPE (type);
7611 return type;
7614 /* Computes the canonical argument types from the argument type list
7615 ARGTYPES.
7617 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7618 on entry to this function, or if any of the ARGTYPES are
7619 structural.
7621 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7622 true on entry to this function, or if any of the ARGTYPES are
7623 non-canonical.
7625 Returns a canonical argument list, which may be ARGTYPES when the
7626 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7627 true) or would not differ from ARGTYPES. */
7629 static tree
7630 maybe_canonicalize_argtypes(tree argtypes,
7631 bool *any_structural_p,
7632 bool *any_noncanonical_p)
7634 tree arg;
7635 bool any_noncanonical_argtypes_p = false;
7637 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7639 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7640 /* Fail gracefully by stating that the type is structural. */
7641 *any_structural_p = true;
7642 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7643 *any_structural_p = true;
7644 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7645 || TREE_PURPOSE (arg))
7646 /* If the argument has a default argument, we consider it
7647 non-canonical even though the type itself is canonical.
7648 That way, different variants of function and method types
7649 with default arguments will all point to the variant with
7650 no defaults as their canonical type. */
7651 any_noncanonical_argtypes_p = true;
7654 if (*any_structural_p)
7655 return argtypes;
7657 if (any_noncanonical_argtypes_p)
7659 /* Build the canonical list of argument types. */
7660 tree canon_argtypes = NULL_TREE;
7661 bool is_void = false;
7663 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7665 if (arg == void_list_node)
7666 is_void = true;
7667 else
7668 canon_argtypes = tree_cons (NULL_TREE,
7669 TYPE_CANONICAL (TREE_VALUE (arg)),
7670 canon_argtypes);
7673 canon_argtypes = nreverse (canon_argtypes);
7674 if (is_void)
7675 canon_argtypes = chainon (canon_argtypes, void_list_node);
7677 /* There is a non-canonical type. */
7678 *any_noncanonical_p = true;
7679 return canon_argtypes;
7682 /* The canonical argument types are the same as ARGTYPES. */
7683 return argtypes;
7686 /* Construct, lay out and return
7687 the type of functions returning type VALUE_TYPE
7688 given arguments of types ARG_TYPES.
7689 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7690 are data type nodes for the arguments of the function.
7691 If such a type has already been constructed, reuse it. */
7693 tree
7694 build_function_type (tree value_type, tree arg_types)
7696 tree t;
7697 hashval_t hashcode = 0;
7698 bool any_structural_p, any_noncanonical_p;
7699 tree canon_argtypes;
7701 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7703 error ("function return type cannot be function");
7704 value_type = integer_type_node;
7707 /* Make a node of the sort we want. */
7708 t = make_node (FUNCTION_TYPE);
7709 TREE_TYPE (t) = value_type;
7710 TYPE_ARG_TYPES (t) = arg_types;
7712 /* If we already have such a type, use the old one. */
7713 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
7714 hashcode = type_hash_list (arg_types, hashcode);
7715 t = type_hash_canon (hashcode, t);
7717 /* Set up the canonical type. */
7718 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7719 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7720 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7721 &any_structural_p,
7722 &any_noncanonical_p);
7723 if (any_structural_p)
7724 SET_TYPE_STRUCTURAL_EQUALITY (t);
7725 else if (any_noncanonical_p)
7726 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7727 canon_argtypes);
7729 if (!COMPLETE_TYPE_P (t))
7730 layout_type (t);
7731 return t;
7734 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
7735 return value if SKIP_RETURN is true. */
7737 static tree
7738 build_function_type_skip_args (tree orig_type, bitmap args_to_skip,
7739 bool skip_return)
7741 tree new_type = NULL;
7742 tree args, new_args = NULL, t;
7743 tree new_reversed;
7744 int i = 0;
7746 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
7747 args = TREE_CHAIN (args), i++)
7748 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
7749 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
7751 new_reversed = nreverse (new_args);
7752 if (args)
7754 if (new_reversed)
7755 TREE_CHAIN (new_args) = void_list_node;
7756 else
7757 new_reversed = void_list_node;
7760 /* Use copy_node to preserve as much as possible from original type
7761 (debug info, attribute lists etc.)
7762 Exception is METHOD_TYPEs must have THIS argument.
7763 When we are asked to remove it, we need to build new FUNCTION_TYPE
7764 instead. */
7765 if (TREE_CODE (orig_type) != METHOD_TYPE
7766 || !args_to_skip
7767 || !bitmap_bit_p (args_to_skip, 0))
7769 new_type = build_distinct_type_copy (orig_type);
7770 TYPE_ARG_TYPES (new_type) = new_reversed;
7772 else
7774 new_type
7775 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
7776 new_reversed));
7777 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
7780 if (skip_return)
7781 TREE_TYPE (new_type) = void_type_node;
7783 /* This is a new type, not a copy of an old type. Need to reassociate
7784 variants. We can handle everything except the main variant lazily. */
7785 t = TYPE_MAIN_VARIANT (orig_type);
7786 if (t != orig_type)
7788 t = build_function_type_skip_args (t, args_to_skip, skip_return);
7789 TYPE_MAIN_VARIANT (new_type) = t;
7790 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
7791 TYPE_NEXT_VARIANT (t) = new_type;
7793 else
7795 TYPE_MAIN_VARIANT (new_type) = new_type;
7796 TYPE_NEXT_VARIANT (new_type) = NULL;
7799 return new_type;
7802 /* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
7803 return value if SKIP_RETURN is true.
7805 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
7806 linked by TREE_CHAIN directly. The caller is responsible for eliminating
7807 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
7809 tree
7810 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip,
7811 bool skip_return)
7813 tree new_decl = copy_node (orig_decl);
7814 tree new_type;
7816 new_type = TREE_TYPE (orig_decl);
7817 if (prototype_p (new_type)
7818 || (skip_return && !VOID_TYPE_P (TREE_TYPE (new_type))))
7819 new_type
7820 = build_function_type_skip_args (new_type, args_to_skip, skip_return);
7821 TREE_TYPE (new_decl) = new_type;
7823 /* For declarations setting DECL_VINDEX (i.e. methods)
7824 we expect first argument to be THIS pointer. */
7825 if (args_to_skip && bitmap_bit_p (args_to_skip, 0))
7826 DECL_VINDEX (new_decl) = NULL_TREE;
7828 /* When signature changes, we need to clear builtin info. */
7829 if (DECL_BUILT_IN (new_decl)
7830 && args_to_skip
7831 && !bitmap_empty_p (args_to_skip))
7833 DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
7834 DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
7836 return new_decl;
7839 /* Build a function type. The RETURN_TYPE is the type returned by the
7840 function. If VAARGS is set, no void_type_node is appended to the
7841 the list. ARGP must be always be terminated be a NULL_TREE. */
7843 static tree
7844 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
7846 tree t, args, last;
7848 t = va_arg (argp, tree);
7849 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
7850 args = tree_cons (NULL_TREE, t, args);
7852 if (vaargs)
7854 last = args;
7855 if (args != NULL_TREE)
7856 args = nreverse (args);
7857 gcc_assert (last != void_list_node);
7859 else if (args == NULL_TREE)
7860 args = void_list_node;
7861 else
7863 last = args;
7864 args = nreverse (args);
7865 TREE_CHAIN (last) = void_list_node;
7867 args = build_function_type (return_type, args);
7869 return args;
7872 /* Build a function type. The RETURN_TYPE is the type returned by the
7873 function. If additional arguments are provided, they are
7874 additional argument types. The list of argument types must always
7875 be terminated by NULL_TREE. */
7877 tree
7878 build_function_type_list (tree return_type, ...)
7880 tree args;
7881 va_list p;
7883 va_start (p, return_type);
7884 args = build_function_type_list_1 (false, return_type, p);
7885 va_end (p);
7886 return args;
7889 /* Build a variable argument function type. The RETURN_TYPE is the
7890 type returned by the function. If additional arguments are provided,
7891 they are additional argument types. The list of argument types must
7892 always be terminated by NULL_TREE. */
7894 tree
7895 build_varargs_function_type_list (tree return_type, ...)
7897 tree args;
7898 va_list p;
7900 va_start (p, return_type);
7901 args = build_function_type_list_1 (true, return_type, p);
7902 va_end (p);
7904 return args;
7907 /* Build a function type. RETURN_TYPE is the type returned by the
7908 function; VAARGS indicates whether the function takes varargs. The
7909 function takes N named arguments, the types of which are provided in
7910 ARG_TYPES. */
7912 static tree
7913 build_function_type_array_1 (bool vaargs, tree return_type, int n,
7914 tree *arg_types)
7916 int i;
7917 tree t = vaargs ? NULL_TREE : void_list_node;
7919 for (i = n - 1; i >= 0; i--)
7920 t = tree_cons (NULL_TREE, arg_types[i], t);
7922 return build_function_type (return_type, t);
7925 /* Build a function type. RETURN_TYPE is the type returned by the
7926 function. The function takes N named arguments, the types of which
7927 are provided in ARG_TYPES. */
7929 tree
7930 build_function_type_array (tree return_type, int n, tree *arg_types)
7932 return build_function_type_array_1 (false, return_type, n, arg_types);
7935 /* Build a variable argument function type. RETURN_TYPE is the type
7936 returned by the function. The function takes N named arguments, the
7937 types of which are provided in ARG_TYPES. */
7939 tree
7940 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
7942 return build_function_type_array_1 (true, return_type, n, arg_types);
7945 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
7946 and ARGTYPES (a TREE_LIST) are the return type and arguments types
7947 for the method. An implicit additional parameter (of type
7948 pointer-to-BASETYPE) is added to the ARGTYPES. */
7950 tree
7951 build_method_type_directly (tree basetype,
7952 tree rettype,
7953 tree argtypes)
7955 tree t;
7956 tree ptype;
7957 int hashcode = 0;
7958 bool any_structural_p, any_noncanonical_p;
7959 tree canon_argtypes;
7961 /* Make a node of the sort we want. */
7962 t = make_node (METHOD_TYPE);
7964 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7965 TREE_TYPE (t) = rettype;
7966 ptype = build_pointer_type (basetype);
7968 /* The actual arglist for this function includes a "hidden" argument
7969 which is "this". Put it into the list of argument types. */
7970 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
7971 TYPE_ARG_TYPES (t) = argtypes;
7973 /* If we already have such a type, use the old one. */
7974 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
7975 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
7976 hashcode = type_hash_list (argtypes, hashcode);
7977 t = type_hash_canon (hashcode, t);
7979 /* Set up the canonical type. */
7980 any_structural_p
7981 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7982 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
7983 any_noncanonical_p
7984 = (TYPE_CANONICAL (basetype) != basetype
7985 || TYPE_CANONICAL (rettype) != rettype);
7986 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
7987 &any_structural_p,
7988 &any_noncanonical_p);
7989 if (any_structural_p)
7990 SET_TYPE_STRUCTURAL_EQUALITY (t);
7991 else if (any_noncanonical_p)
7992 TYPE_CANONICAL (t)
7993 = build_method_type_directly (TYPE_CANONICAL (basetype),
7994 TYPE_CANONICAL (rettype),
7995 canon_argtypes);
7996 if (!COMPLETE_TYPE_P (t))
7997 layout_type (t);
7999 return t;
8002 /* Construct, lay out and return the type of methods belonging to class
8003 BASETYPE and whose arguments and values are described by TYPE.
8004 If that type exists already, reuse it.
8005 TYPE must be a FUNCTION_TYPE node. */
8007 tree
8008 build_method_type (tree basetype, tree type)
8010 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8012 return build_method_type_directly (basetype,
8013 TREE_TYPE (type),
8014 TYPE_ARG_TYPES (type));
8017 /* Construct, lay out and return the type of offsets to a value
8018 of type TYPE, within an object of type BASETYPE.
8019 If a suitable offset type exists already, reuse it. */
8021 tree
8022 build_offset_type (tree basetype, tree type)
8024 tree t;
8025 hashval_t hashcode = 0;
8027 /* Make a node of the sort we want. */
8028 t = make_node (OFFSET_TYPE);
8030 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8031 TREE_TYPE (t) = type;
8033 /* If we already have such a type, use the old one. */
8034 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8035 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
8036 t = type_hash_canon (hashcode, t);
8038 if (!COMPLETE_TYPE_P (t))
8039 layout_type (t);
8041 if (TYPE_CANONICAL (t) == t)
8043 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8044 || TYPE_STRUCTURAL_EQUALITY_P (type))
8045 SET_TYPE_STRUCTURAL_EQUALITY (t);
8046 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8047 || TYPE_CANONICAL (type) != type)
8048 TYPE_CANONICAL (t)
8049 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8050 TYPE_CANONICAL (type));
8053 return t;
8056 /* Create a complex type whose components are COMPONENT_TYPE. */
8058 tree
8059 build_complex_type (tree component_type)
8061 tree t;
8062 hashval_t hashcode;
8064 gcc_assert (INTEGRAL_TYPE_P (component_type)
8065 || SCALAR_FLOAT_TYPE_P (component_type)
8066 || FIXED_POINT_TYPE_P (component_type));
8068 /* Make a node of the sort we want. */
8069 t = make_node (COMPLEX_TYPE);
8071 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8073 /* If we already have such a type, use the old one. */
8074 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
8075 t = type_hash_canon (hashcode, t);
8077 if (!COMPLETE_TYPE_P (t))
8078 layout_type (t);
8080 if (TYPE_CANONICAL (t) == t)
8082 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8083 SET_TYPE_STRUCTURAL_EQUALITY (t);
8084 else if (TYPE_CANONICAL (component_type) != component_type)
8085 TYPE_CANONICAL (t)
8086 = build_complex_type (TYPE_CANONICAL (component_type));
8089 /* We need to create a name, since complex is a fundamental type. */
8090 if (! TYPE_NAME (t))
8092 const char *name;
8093 if (component_type == char_type_node)
8094 name = "complex char";
8095 else if (component_type == signed_char_type_node)
8096 name = "complex signed char";
8097 else if (component_type == unsigned_char_type_node)
8098 name = "complex unsigned char";
8099 else if (component_type == short_integer_type_node)
8100 name = "complex short int";
8101 else if (component_type == short_unsigned_type_node)
8102 name = "complex short unsigned int";
8103 else if (component_type == integer_type_node)
8104 name = "complex int";
8105 else if (component_type == unsigned_type_node)
8106 name = "complex unsigned int";
8107 else if (component_type == long_integer_type_node)
8108 name = "complex long int";
8109 else if (component_type == long_unsigned_type_node)
8110 name = "complex long unsigned int";
8111 else if (component_type == long_long_integer_type_node)
8112 name = "complex long long int";
8113 else if (component_type == long_long_unsigned_type_node)
8114 name = "complex long long unsigned int";
8115 else
8116 name = 0;
8118 if (name != 0)
8119 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8120 get_identifier (name), t);
8123 return build_qualified_type (t, TYPE_QUALS (component_type));
8126 /* If TYPE is a real or complex floating-point type and the target
8127 does not directly support arithmetic on TYPE then return the wider
8128 type to be used for arithmetic on TYPE. Otherwise, return
8129 NULL_TREE. */
8131 tree
8132 excess_precision_type (tree type)
8134 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8136 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8137 switch (TREE_CODE (type))
8139 case REAL_TYPE:
8140 switch (flt_eval_method)
8142 case 1:
8143 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8144 return double_type_node;
8145 break;
8146 case 2:
8147 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8148 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8149 return long_double_type_node;
8150 break;
8151 default:
8152 gcc_unreachable ();
8154 break;
8155 case COMPLEX_TYPE:
8156 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8157 return NULL_TREE;
8158 switch (flt_eval_method)
8160 case 1:
8161 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8162 return complex_double_type_node;
8163 break;
8164 case 2:
8165 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8166 || (TYPE_MODE (TREE_TYPE (type))
8167 == TYPE_MODE (double_type_node)))
8168 return complex_long_double_type_node;
8169 break;
8170 default:
8171 gcc_unreachable ();
8173 break;
8174 default:
8175 break;
8178 return NULL_TREE;
8181 /* Return OP, stripped of any conversions to wider types as much as is safe.
8182 Converting the value back to OP's type makes a value equivalent to OP.
8184 If FOR_TYPE is nonzero, we return a value which, if converted to
8185 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8187 OP must have integer, real or enumeral type. Pointers are not allowed!
8189 There are some cases where the obvious value we could return
8190 would regenerate to OP if converted to OP's type,
8191 but would not extend like OP to wider types.
8192 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8193 For example, if OP is (unsigned short)(signed char)-1,
8194 we avoid returning (signed char)-1 if FOR_TYPE is int,
8195 even though extending that to an unsigned short would regenerate OP,
8196 since the result of extending (signed char)-1 to (int)
8197 is different from (int) OP. */
8199 tree
8200 get_unwidened (tree op, tree for_type)
8202 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8203 tree type = TREE_TYPE (op);
8204 unsigned final_prec
8205 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8206 int uns
8207 = (for_type != 0 && for_type != type
8208 && final_prec > TYPE_PRECISION (type)
8209 && TYPE_UNSIGNED (type));
8210 tree win = op;
8212 while (CONVERT_EXPR_P (op))
8214 int bitschange;
8216 /* TYPE_PRECISION on vector types has different meaning
8217 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8218 so avoid them here. */
8219 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8220 break;
8222 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8223 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8225 /* Truncations are many-one so cannot be removed.
8226 Unless we are later going to truncate down even farther. */
8227 if (bitschange < 0
8228 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8229 break;
8231 /* See what's inside this conversion. If we decide to strip it,
8232 we will set WIN. */
8233 op = TREE_OPERAND (op, 0);
8235 /* If we have not stripped any zero-extensions (uns is 0),
8236 we can strip any kind of extension.
8237 If we have previously stripped a zero-extension,
8238 only zero-extensions can safely be stripped.
8239 Any extension can be stripped if the bits it would produce
8240 are all going to be discarded later by truncating to FOR_TYPE. */
8242 if (bitschange > 0)
8244 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8245 win = op;
8246 /* TYPE_UNSIGNED says whether this is a zero-extension.
8247 Let's avoid computing it if it does not affect WIN
8248 and if UNS will not be needed again. */
8249 if ((uns
8250 || CONVERT_EXPR_P (op))
8251 && TYPE_UNSIGNED (TREE_TYPE (op)))
8253 uns = 1;
8254 win = op;
8259 /* If we finally reach a constant see if it fits in for_type and
8260 in that case convert it. */
8261 if (for_type
8262 && TREE_CODE (win) == INTEGER_CST
8263 && TREE_TYPE (win) != for_type
8264 && int_fits_type_p (win, for_type))
8265 win = fold_convert (for_type, win);
8267 return win;
8270 /* Return OP or a simpler expression for a narrower value
8271 which can be sign-extended or zero-extended to give back OP.
8272 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8273 or 0 if the value should be sign-extended. */
8275 tree
8276 get_narrower (tree op, int *unsignedp_ptr)
8278 int uns = 0;
8279 int first = 1;
8280 tree win = op;
8281 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8283 while (TREE_CODE (op) == NOP_EXPR)
8285 int bitschange
8286 = (TYPE_PRECISION (TREE_TYPE (op))
8287 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8289 /* Truncations are many-one so cannot be removed. */
8290 if (bitschange < 0)
8291 break;
8293 /* See what's inside this conversion. If we decide to strip it,
8294 we will set WIN. */
8296 if (bitschange > 0)
8298 op = TREE_OPERAND (op, 0);
8299 /* An extension: the outermost one can be stripped,
8300 but remember whether it is zero or sign extension. */
8301 if (first)
8302 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8303 /* Otherwise, if a sign extension has been stripped,
8304 only sign extensions can now be stripped;
8305 if a zero extension has been stripped, only zero-extensions. */
8306 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8307 break;
8308 first = 0;
8310 else /* bitschange == 0 */
8312 /* A change in nominal type can always be stripped, but we must
8313 preserve the unsignedness. */
8314 if (first)
8315 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8316 first = 0;
8317 op = TREE_OPERAND (op, 0);
8318 /* Keep trying to narrow, but don't assign op to win if it
8319 would turn an integral type into something else. */
8320 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8321 continue;
8324 win = op;
8327 if (TREE_CODE (op) == COMPONENT_REF
8328 /* Since type_for_size always gives an integer type. */
8329 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8330 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8331 /* Ensure field is laid out already. */
8332 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8333 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
8335 unsigned HOST_WIDE_INT innerprec
8336 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
8337 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8338 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8339 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8341 /* We can get this structure field in a narrower type that fits it,
8342 but the resulting extension to its nominal type (a fullword type)
8343 must satisfy the same conditions as for other extensions.
8345 Do this only for fields that are aligned (not bit-fields),
8346 because when bit-field insns will be used there is no
8347 advantage in doing this. */
8349 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8350 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8351 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8352 && type != 0)
8354 if (first)
8355 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8356 win = fold_convert (type, op);
8360 *unsignedp_ptr = uns;
8361 return win;
8364 /* Returns true if integer constant C has a value that is permissible
8365 for type TYPE (an INTEGER_TYPE). */
8367 bool
8368 int_fits_type_p (const_tree c, const_tree type)
8370 tree type_low_bound, type_high_bound;
8371 bool ok_for_low_bound, ok_for_high_bound, unsc;
8372 double_int dc, dd;
8374 dc = tree_to_double_int (c);
8375 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
8377 retry:
8378 type_low_bound = TYPE_MIN_VALUE (type);
8379 type_high_bound = TYPE_MAX_VALUE (type);
8381 /* If at least one bound of the type is a constant integer, we can check
8382 ourselves and maybe make a decision. If no such decision is possible, but
8383 this type is a subtype, try checking against that. Otherwise, use
8384 double_int_fits_to_tree_p, which checks against the precision.
8386 Compute the status for each possibly constant bound, and return if we see
8387 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8388 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8389 for "constant known to fit". */
8391 /* Check if c >= type_low_bound. */
8392 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8394 dd = tree_to_double_int (type_low_bound);
8395 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
8397 int c_neg = (!unsc && dc.is_negative ());
8398 int t_neg = (unsc && dd.is_negative ());
8400 if (c_neg && !t_neg)
8401 return false;
8402 if ((c_neg || !t_neg) && dc.ult (dd))
8403 return false;
8405 else if (dc.cmp (dd, unsc) < 0)
8406 return false;
8407 ok_for_low_bound = true;
8409 else
8410 ok_for_low_bound = false;
8412 /* Check if c <= type_high_bound. */
8413 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8415 dd = tree_to_double_int (type_high_bound);
8416 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
8418 int c_neg = (!unsc && dc.is_negative ());
8419 int t_neg = (unsc && dd.is_negative ());
8421 if (t_neg && !c_neg)
8422 return false;
8423 if ((t_neg || !c_neg) && dc.ugt (dd))
8424 return false;
8426 else if (dc.cmp (dd, unsc) > 0)
8427 return false;
8428 ok_for_high_bound = true;
8430 else
8431 ok_for_high_bound = false;
8433 /* If the constant fits both bounds, the result is known. */
8434 if (ok_for_low_bound && ok_for_high_bound)
8435 return true;
8437 /* Perform some generic filtering which may allow making a decision
8438 even if the bounds are not constant. First, negative integers
8439 never fit in unsigned types, */
8440 if (TYPE_UNSIGNED (type) && !unsc && dc.is_negative ())
8441 return false;
8443 /* Second, narrower types always fit in wider ones. */
8444 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8445 return true;
8447 /* Third, unsigned integers with top bit set never fit signed types. */
8448 if (! TYPE_UNSIGNED (type) && unsc)
8450 int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
8451 if (prec < HOST_BITS_PER_WIDE_INT)
8453 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
8454 return false;
8456 else if (((((unsigned HOST_WIDE_INT) 1)
8457 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
8458 return false;
8461 /* If we haven't been able to decide at this point, there nothing more we
8462 can check ourselves here. Look at the base type if we have one and it
8463 has the same precision. */
8464 if (TREE_CODE (type) == INTEGER_TYPE
8465 && TREE_TYPE (type) != 0
8466 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8468 type = TREE_TYPE (type);
8469 goto retry;
8472 /* Or to double_int_fits_to_tree_p, if nothing else. */
8473 return double_int_fits_to_tree_p (type, dc);
8476 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8477 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8478 represented (assuming two's-complement arithmetic) within the bit
8479 precision of the type are returned instead. */
8481 void
8482 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8484 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8485 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8486 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
8487 TYPE_UNSIGNED (type));
8488 else
8490 if (TYPE_UNSIGNED (type))
8491 mpz_set_ui (min, 0);
8492 else
8494 double_int mn;
8495 mn = double_int::mask (TYPE_PRECISION (type) - 1);
8496 mn = (mn + double_int_one).sext (TYPE_PRECISION (type));
8497 mpz_set_double_int (min, mn, false);
8501 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8502 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8503 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
8504 TYPE_UNSIGNED (type));
8505 else
8507 if (TYPE_UNSIGNED (type))
8508 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)),
8509 true);
8510 else
8511 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type) - 1),
8512 true);
8516 /* Return true if VAR is an automatic variable defined in function FN. */
8518 bool
8519 auto_var_in_fn_p (const_tree var, const_tree fn)
8521 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8522 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8523 || TREE_CODE (var) == PARM_DECL)
8524 && ! TREE_STATIC (var))
8525 || TREE_CODE (var) == LABEL_DECL
8526 || TREE_CODE (var) == RESULT_DECL));
8529 /* Subprogram of following function. Called by walk_tree.
8531 Return *TP if it is an automatic variable or parameter of the
8532 function passed in as DATA. */
8534 static tree
8535 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8537 tree fn = (tree) data;
8539 if (TYPE_P (*tp))
8540 *walk_subtrees = 0;
8542 else if (DECL_P (*tp)
8543 && auto_var_in_fn_p (*tp, fn))
8544 return *tp;
8546 return NULL_TREE;
8549 /* Returns true if T is, contains, or refers to a type with variable
8550 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8551 arguments, but not the return type. If FN is nonzero, only return
8552 true if a modifier of the type or position of FN is a variable or
8553 parameter inside FN.
8555 This concept is more general than that of C99 'variably modified types':
8556 in C99, a struct type is never variably modified because a VLA may not
8557 appear as a structure member. However, in GNU C code like:
8559 struct S { int i[f()]; };
8561 is valid, and other languages may define similar constructs. */
8563 bool
8564 variably_modified_type_p (tree type, tree fn)
8566 tree t;
8568 /* Test if T is either variable (if FN is zero) or an expression containing
8569 a variable in FN. If TYPE isn't gimplified, return true also if
8570 gimplify_one_sizepos would gimplify the expression into a local
8571 variable. */
8572 #define RETURN_TRUE_IF_VAR(T) \
8573 do { tree _t = (T); \
8574 if (_t != NULL_TREE \
8575 && _t != error_mark_node \
8576 && TREE_CODE (_t) != INTEGER_CST \
8577 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8578 && (!fn \
8579 || (!TYPE_SIZES_GIMPLIFIED (type) \
8580 && !is_gimple_sizepos (_t)) \
8581 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8582 return true; } while (0)
8584 if (type == error_mark_node)
8585 return false;
8587 /* If TYPE itself has variable size, it is variably modified. */
8588 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8589 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8591 switch (TREE_CODE (type))
8593 case POINTER_TYPE:
8594 case REFERENCE_TYPE:
8595 case VECTOR_TYPE:
8596 if (variably_modified_type_p (TREE_TYPE (type), fn))
8597 return true;
8598 break;
8600 case FUNCTION_TYPE:
8601 case METHOD_TYPE:
8602 /* If TYPE is a function type, it is variably modified if the
8603 return type is variably modified. */
8604 if (variably_modified_type_p (TREE_TYPE (type), fn))
8605 return true;
8606 break;
8608 case INTEGER_TYPE:
8609 case REAL_TYPE:
8610 case FIXED_POINT_TYPE:
8611 case ENUMERAL_TYPE:
8612 case BOOLEAN_TYPE:
8613 /* Scalar types are variably modified if their end points
8614 aren't constant. */
8615 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8616 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8617 break;
8619 case RECORD_TYPE:
8620 case UNION_TYPE:
8621 case QUAL_UNION_TYPE:
8622 /* We can't see if any of the fields are variably-modified by the
8623 definition we normally use, since that would produce infinite
8624 recursion via pointers. */
8625 /* This is variably modified if some field's type is. */
8626 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8627 if (TREE_CODE (t) == FIELD_DECL)
8629 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8630 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8631 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8633 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8634 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8636 break;
8638 case ARRAY_TYPE:
8639 /* Do not call ourselves to avoid infinite recursion. This is
8640 variably modified if the element type is. */
8641 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8642 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8643 break;
8645 default:
8646 break;
8649 /* The current language may have other cases to check, but in general,
8650 all other types are not variably modified. */
8651 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8653 #undef RETURN_TRUE_IF_VAR
8656 /* Given a DECL or TYPE, return the scope in which it was declared, or
8657 NULL_TREE if there is no containing scope. */
8659 tree
8660 get_containing_scope (const_tree t)
8662 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8665 /* Return the innermost context enclosing DECL that is
8666 a FUNCTION_DECL, or zero if none. */
8668 tree
8669 decl_function_context (const_tree decl)
8671 tree context;
8673 if (TREE_CODE (decl) == ERROR_MARK)
8674 return 0;
8676 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8677 where we look up the function at runtime. Such functions always take
8678 a first argument of type 'pointer to real context'.
8680 C++ should really be fixed to use DECL_CONTEXT for the real context,
8681 and use something else for the "virtual context". */
8682 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8683 context
8684 = TYPE_MAIN_VARIANT
8685 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8686 else
8687 context = DECL_CONTEXT (decl);
8689 while (context && TREE_CODE (context) != FUNCTION_DECL)
8691 if (TREE_CODE (context) == BLOCK)
8692 context = BLOCK_SUPERCONTEXT (context);
8693 else
8694 context = get_containing_scope (context);
8697 return context;
8700 /* Return the innermost context enclosing DECL that is
8701 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8702 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8704 tree
8705 decl_type_context (const_tree decl)
8707 tree context = DECL_CONTEXT (decl);
8709 while (context)
8710 switch (TREE_CODE (context))
8712 case NAMESPACE_DECL:
8713 case TRANSLATION_UNIT_DECL:
8714 return NULL_TREE;
8716 case RECORD_TYPE:
8717 case UNION_TYPE:
8718 case QUAL_UNION_TYPE:
8719 return context;
8721 case TYPE_DECL:
8722 case FUNCTION_DECL:
8723 context = DECL_CONTEXT (context);
8724 break;
8726 case BLOCK:
8727 context = BLOCK_SUPERCONTEXT (context);
8728 break;
8730 default:
8731 gcc_unreachable ();
8734 return NULL_TREE;
8737 /* CALL is a CALL_EXPR. Return the declaration for the function
8738 called, or NULL_TREE if the called function cannot be
8739 determined. */
8741 tree
8742 get_callee_fndecl (const_tree call)
8744 tree addr;
8746 if (call == error_mark_node)
8747 return error_mark_node;
8749 /* It's invalid to call this function with anything but a
8750 CALL_EXPR. */
8751 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8753 /* The first operand to the CALL is the address of the function
8754 called. */
8755 addr = CALL_EXPR_FN (call);
8757 STRIP_NOPS (addr);
8759 /* If this is a readonly function pointer, extract its initial value. */
8760 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8761 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8762 && DECL_INITIAL (addr))
8763 addr = DECL_INITIAL (addr);
8765 /* If the address is just `&f' for some function `f', then we know
8766 that `f' is being called. */
8767 if (TREE_CODE (addr) == ADDR_EXPR
8768 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8769 return TREE_OPERAND (addr, 0);
8771 /* We couldn't figure out what was being called. */
8772 return NULL_TREE;
8775 /* Print debugging information about tree nodes generated during the compile,
8776 and any language-specific information. */
8778 void
8779 dump_tree_statistics (void)
8781 if (GATHER_STATISTICS)
8783 int i;
8784 int total_nodes, total_bytes;
8785 fprintf (stderr, "Kind Nodes Bytes\n");
8786 fprintf (stderr, "---------------------------------------\n");
8787 total_nodes = total_bytes = 0;
8788 for (i = 0; i < (int) all_kinds; i++)
8790 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8791 tree_node_counts[i], tree_node_sizes[i]);
8792 total_nodes += tree_node_counts[i];
8793 total_bytes += tree_node_sizes[i];
8795 fprintf (stderr, "---------------------------------------\n");
8796 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8797 fprintf (stderr, "---------------------------------------\n");
8798 fprintf (stderr, "Code Nodes\n");
8799 fprintf (stderr, "----------------------------\n");
8800 for (i = 0; i < (int) MAX_TREE_CODES; i++)
8801 fprintf (stderr, "%-20s %7d\n", tree_code_name[i], tree_code_counts[i]);
8802 fprintf (stderr, "----------------------------\n");
8803 ssanames_print_statistics ();
8804 phinodes_print_statistics ();
8806 else
8807 fprintf (stderr, "(No per-node statistics)\n");
8809 print_type_hash_statistics ();
8810 print_debug_expr_statistics ();
8811 print_value_expr_statistics ();
8812 lang_hooks.print_statistics ();
8815 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8817 /* Generate a crc32 of a byte. */
8819 static unsigned
8820 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
8822 unsigned ix;
8824 for (ix = bits; ix--; value <<= 1)
8826 unsigned feedback;
8828 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
8829 chksum <<= 1;
8830 chksum ^= feedback;
8832 return chksum;
8835 /* Generate a crc32 of a 32-bit unsigned. */
8837 unsigned
8838 crc32_unsigned (unsigned chksum, unsigned value)
8840 return crc32_unsigned_bits (chksum, value, 32);
8843 /* Generate a crc32 of a byte. */
8845 unsigned
8846 crc32_byte (unsigned chksum, char byte)
8848 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
8851 /* Generate a crc32 of a string. */
8853 unsigned
8854 crc32_string (unsigned chksum, const char *string)
8858 chksum = crc32_byte (chksum, *string);
8860 while (*string++);
8861 return chksum;
8864 /* P is a string that will be used in a symbol. Mask out any characters
8865 that are not valid in that context. */
8867 void
8868 clean_symbol_name (char *p)
8870 for (; *p; p++)
8871 if (! (ISALNUM (*p)
8872 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
8873 || *p == '$'
8874 #endif
8875 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
8876 || *p == '.'
8877 #endif
8879 *p = '_';
8882 /* Generate a name for a special-purpose function.
8883 The generated name may need to be unique across the whole link.
8884 Changes to this function may also require corresponding changes to
8885 xstrdup_mask_random.
8886 TYPE is some string to identify the purpose of this function to the
8887 linker or collect2; it must start with an uppercase letter,
8888 one of:
8889 I - for constructors
8890 D - for destructors
8891 N - for C++ anonymous namespaces
8892 F - for DWARF unwind frame information. */
8894 tree
8895 get_file_function_name (const char *type)
8897 char *buf;
8898 const char *p;
8899 char *q;
8901 /* If we already have a name we know to be unique, just use that. */
8902 if (first_global_object_name)
8903 p = q = ASTRDUP (first_global_object_name);
8904 /* If the target is handling the constructors/destructors, they
8905 will be local to this file and the name is only necessary for
8906 debugging purposes.
8907 We also assign sub_I and sub_D sufixes to constructors called from
8908 the global static constructors. These are always local. */
8909 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
8910 || (strncmp (type, "sub_", 4) == 0
8911 && (type[4] == 'I' || type[4] == 'D')))
8913 const char *file = main_input_filename;
8914 if (! file)
8915 file = input_filename;
8916 /* Just use the file's basename, because the full pathname
8917 might be quite long. */
8918 p = q = ASTRDUP (lbasename (file));
8920 else
8922 /* Otherwise, the name must be unique across the entire link.
8923 We don't have anything that we know to be unique to this translation
8924 unit, so use what we do have and throw in some randomness. */
8925 unsigned len;
8926 const char *name = weak_global_object_name;
8927 const char *file = main_input_filename;
8929 if (! name)
8930 name = "";
8931 if (! file)
8932 file = input_filename;
8934 len = strlen (file);
8935 q = (char *) alloca (9 + 17 + len + 1);
8936 memcpy (q, file, len + 1);
8938 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
8939 crc32_string (0, name), get_random_seed (false));
8941 p = q;
8944 clean_symbol_name (q);
8945 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
8946 + strlen (type));
8948 /* Set up the name of the file-level functions we may need.
8949 Use a global object (which is already required to be unique over
8950 the program) rather than the file name (which imposes extra
8951 constraints). */
8952 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
8954 return get_identifier (buf);
8957 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
8959 /* Complain that the tree code of NODE does not match the expected 0
8960 terminated list of trailing codes. The trailing code list can be
8961 empty, for a more vague error message. FILE, LINE, and FUNCTION
8962 are of the caller. */
8964 void
8965 tree_check_failed (const_tree node, const char *file,
8966 int line, const char *function, ...)
8968 va_list args;
8969 const char *buffer;
8970 unsigned length = 0;
8971 int code;
8973 va_start (args, function);
8974 while ((code = va_arg (args, int)))
8975 length += 4 + strlen (tree_code_name[code]);
8976 va_end (args);
8977 if (length)
8979 char *tmp;
8980 va_start (args, function);
8981 length += strlen ("expected ");
8982 buffer = tmp = (char *) alloca (length);
8983 length = 0;
8984 while ((code = va_arg (args, int)))
8986 const char *prefix = length ? " or " : "expected ";
8988 strcpy (tmp + length, prefix);
8989 length += strlen (prefix);
8990 strcpy (tmp + length, tree_code_name[code]);
8991 length += strlen (tree_code_name[code]);
8993 va_end (args);
8995 else
8996 buffer = "unexpected node";
8998 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8999 buffer, tree_code_name[TREE_CODE (node)],
9000 function, trim_filename (file), line);
9003 /* Complain that the tree code of NODE does match the expected 0
9004 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9005 the caller. */
9007 void
9008 tree_not_check_failed (const_tree node, const char *file,
9009 int line, const char *function, ...)
9011 va_list args;
9012 char *buffer;
9013 unsigned length = 0;
9014 int code;
9016 va_start (args, function);
9017 while ((code = va_arg (args, int)))
9018 length += 4 + strlen (tree_code_name[code]);
9019 va_end (args);
9020 va_start (args, function);
9021 buffer = (char *) alloca (length);
9022 length = 0;
9023 while ((code = va_arg (args, int)))
9025 if (length)
9027 strcpy (buffer + length, " or ");
9028 length += 4;
9030 strcpy (buffer + length, tree_code_name[code]);
9031 length += strlen (tree_code_name[code]);
9033 va_end (args);
9035 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9036 buffer, tree_code_name[TREE_CODE (node)],
9037 function, trim_filename (file), line);
9040 /* Similar to tree_check_failed, except that we check for a class of tree
9041 code, given in CL. */
9043 void
9044 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9045 const char *file, int line, const char *function)
9047 internal_error
9048 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9049 TREE_CODE_CLASS_STRING (cl),
9050 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9051 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9054 /* Similar to tree_check_failed, except that instead of specifying a
9055 dozen codes, use the knowledge that they're all sequential. */
9057 void
9058 tree_range_check_failed (const_tree node, const char *file, int line,
9059 const char *function, enum tree_code c1,
9060 enum tree_code c2)
9062 char *buffer;
9063 unsigned length = 0;
9064 unsigned int c;
9066 for (c = c1; c <= c2; ++c)
9067 length += 4 + strlen (tree_code_name[c]);
9069 length += strlen ("expected ");
9070 buffer = (char *) alloca (length);
9071 length = 0;
9073 for (c = c1; c <= c2; ++c)
9075 const char *prefix = length ? " or " : "expected ";
9077 strcpy (buffer + length, prefix);
9078 length += strlen (prefix);
9079 strcpy (buffer + length, tree_code_name[c]);
9080 length += strlen (tree_code_name[c]);
9083 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9084 buffer, tree_code_name[TREE_CODE (node)],
9085 function, trim_filename (file), line);
9089 /* Similar to tree_check_failed, except that we check that a tree does
9090 not have the specified code, given in CL. */
9092 void
9093 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9094 const char *file, int line, const char *function)
9096 internal_error
9097 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9098 TREE_CODE_CLASS_STRING (cl),
9099 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9100 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9104 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9106 void
9107 omp_clause_check_failed (const_tree node, const char *file, int line,
9108 const char *function, enum omp_clause_code code)
9110 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9111 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
9112 function, trim_filename (file), line);
9116 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9118 void
9119 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9120 const char *function, enum omp_clause_code c1,
9121 enum omp_clause_code c2)
9123 char *buffer;
9124 unsigned length = 0;
9125 unsigned int c;
9127 for (c = c1; c <= c2; ++c)
9128 length += 4 + strlen (omp_clause_code_name[c]);
9130 length += strlen ("expected ");
9131 buffer = (char *) alloca (length);
9132 length = 0;
9134 for (c = c1; c <= c2; ++c)
9136 const char *prefix = length ? " or " : "expected ";
9138 strcpy (buffer + length, prefix);
9139 length += strlen (prefix);
9140 strcpy (buffer + length, omp_clause_code_name[c]);
9141 length += strlen (omp_clause_code_name[c]);
9144 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9145 buffer, omp_clause_code_name[TREE_CODE (node)],
9146 function, trim_filename (file), line);
9150 #undef DEFTREESTRUCT
9151 #define DEFTREESTRUCT(VAL, NAME) NAME,
9153 static const char *ts_enum_names[] = {
9154 #include "treestruct.def"
9156 #undef DEFTREESTRUCT
9158 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9160 /* Similar to tree_class_check_failed, except that we check for
9161 whether CODE contains the tree structure identified by EN. */
9163 void
9164 tree_contains_struct_check_failed (const_tree node,
9165 const enum tree_node_structure_enum en,
9166 const char *file, int line,
9167 const char *function)
9169 internal_error
9170 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9171 TS_ENUM_NAME(en),
9172 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9176 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9177 (dynamically sized) vector. */
9179 void
9180 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9181 const char *function)
9183 internal_error
9184 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9185 idx + 1, len, function, trim_filename (file), line);
9188 /* Similar to above, except that the check is for the bounds of the operand
9189 vector of an expression node EXP. */
9191 void
9192 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9193 int line, const char *function)
9195 int code = TREE_CODE (exp);
9196 internal_error
9197 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9198 idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
9199 function, trim_filename (file), line);
9202 /* Similar to above, except that the check is for the number of
9203 operands of an OMP_CLAUSE node. */
9205 void
9206 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9207 int line, const char *function)
9209 internal_error
9210 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9211 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9212 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9213 trim_filename (file), line);
9215 #endif /* ENABLE_TREE_CHECKING */
9217 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9218 and mapped to the machine mode MODE. Initialize its fields and build
9219 the information necessary for debugging output. */
9221 static tree
9222 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9224 tree t;
9225 hashval_t hashcode = 0;
9227 t = make_node (VECTOR_TYPE);
9228 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9229 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9230 SET_TYPE_MODE (t, mode);
9232 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9233 SET_TYPE_STRUCTURAL_EQUALITY (t);
9234 else if (TYPE_CANONICAL (innertype) != innertype
9235 || mode != VOIDmode)
9236 TYPE_CANONICAL (t)
9237 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9239 layout_type (t);
9241 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9242 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9243 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9244 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9245 t = type_hash_canon (hashcode, t);
9247 /* We have built a main variant, based on the main variant of the
9248 inner type. Use it to build the variant we return. */
9249 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9250 && TREE_TYPE (t) != innertype)
9251 return build_type_attribute_qual_variant (t,
9252 TYPE_ATTRIBUTES (innertype),
9253 TYPE_QUALS (innertype));
9255 return t;
9258 static tree
9259 make_or_reuse_type (unsigned size, int unsignedp)
9261 if (size == INT_TYPE_SIZE)
9262 return unsignedp ? unsigned_type_node : integer_type_node;
9263 if (size == CHAR_TYPE_SIZE)
9264 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9265 if (size == SHORT_TYPE_SIZE)
9266 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9267 if (size == LONG_TYPE_SIZE)
9268 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9269 if (size == LONG_LONG_TYPE_SIZE)
9270 return (unsignedp ? long_long_unsigned_type_node
9271 : long_long_integer_type_node);
9272 if (size == 128 && int128_integer_type_node)
9273 return (unsignedp ? int128_unsigned_type_node
9274 : int128_integer_type_node);
9276 if (unsignedp)
9277 return make_unsigned_type (size);
9278 else
9279 return make_signed_type (size);
9282 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9284 static tree
9285 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9287 if (satp)
9289 if (size == SHORT_FRACT_TYPE_SIZE)
9290 return unsignedp ? sat_unsigned_short_fract_type_node
9291 : sat_short_fract_type_node;
9292 if (size == FRACT_TYPE_SIZE)
9293 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9294 if (size == LONG_FRACT_TYPE_SIZE)
9295 return unsignedp ? sat_unsigned_long_fract_type_node
9296 : sat_long_fract_type_node;
9297 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9298 return unsignedp ? sat_unsigned_long_long_fract_type_node
9299 : sat_long_long_fract_type_node;
9301 else
9303 if (size == SHORT_FRACT_TYPE_SIZE)
9304 return unsignedp ? unsigned_short_fract_type_node
9305 : short_fract_type_node;
9306 if (size == FRACT_TYPE_SIZE)
9307 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9308 if (size == LONG_FRACT_TYPE_SIZE)
9309 return unsignedp ? unsigned_long_fract_type_node
9310 : long_fract_type_node;
9311 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9312 return unsignedp ? unsigned_long_long_fract_type_node
9313 : long_long_fract_type_node;
9316 return make_fract_type (size, unsignedp, satp);
9319 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9321 static tree
9322 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9324 if (satp)
9326 if (size == SHORT_ACCUM_TYPE_SIZE)
9327 return unsignedp ? sat_unsigned_short_accum_type_node
9328 : sat_short_accum_type_node;
9329 if (size == ACCUM_TYPE_SIZE)
9330 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9331 if (size == LONG_ACCUM_TYPE_SIZE)
9332 return unsignedp ? sat_unsigned_long_accum_type_node
9333 : sat_long_accum_type_node;
9334 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9335 return unsignedp ? sat_unsigned_long_long_accum_type_node
9336 : sat_long_long_accum_type_node;
9338 else
9340 if (size == SHORT_ACCUM_TYPE_SIZE)
9341 return unsignedp ? unsigned_short_accum_type_node
9342 : short_accum_type_node;
9343 if (size == ACCUM_TYPE_SIZE)
9344 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9345 if (size == LONG_ACCUM_TYPE_SIZE)
9346 return unsignedp ? unsigned_long_accum_type_node
9347 : long_accum_type_node;
9348 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9349 return unsignedp ? unsigned_long_long_accum_type_node
9350 : long_long_accum_type_node;
9353 return make_accum_type (size, unsignedp, satp);
9356 /* Create nodes for all integer types (and error_mark_node) using the sizes
9357 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9358 SHORT_DOUBLE specifies whether double should be of the same precision
9359 as float. */
9361 void
9362 build_common_tree_nodes (bool signed_char, bool short_double)
9364 error_mark_node = make_node (ERROR_MARK);
9365 TREE_TYPE (error_mark_node) = error_mark_node;
9367 initialize_sizetypes ();
9369 /* Define both `signed char' and `unsigned char'. */
9370 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9371 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9372 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9373 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9375 /* Define `char', which is like either `signed char' or `unsigned char'
9376 but not the same as either. */
9377 char_type_node
9378 = (signed_char
9379 ? make_signed_type (CHAR_TYPE_SIZE)
9380 : make_unsigned_type (CHAR_TYPE_SIZE));
9381 TYPE_STRING_FLAG (char_type_node) = 1;
9383 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9384 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9385 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9386 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9387 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9388 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9389 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9390 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9391 #if HOST_BITS_PER_WIDE_INT >= 64
9392 /* TODO: This isn't correct, but as logic depends at the moment on
9393 host's instead of target's wide-integer.
9394 If there is a target not supporting TImode, but has an 128-bit
9395 integer-scalar register, this target check needs to be adjusted. */
9396 if (targetm.scalar_mode_supported_p (TImode))
9398 int128_integer_type_node = make_signed_type (128);
9399 int128_unsigned_type_node = make_unsigned_type (128);
9401 #endif
9403 /* Define a boolean type. This type only represents boolean values but
9404 may be larger than char depending on the value of BOOL_TYPE_SIZE.
9405 Front ends which want to override this size (i.e. Java) can redefine
9406 boolean_type_node before calling build_common_tree_nodes_2. */
9407 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9408 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9409 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9410 TYPE_PRECISION (boolean_type_node) = 1;
9412 /* Define what type to use for size_t. */
9413 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9414 size_type_node = unsigned_type_node;
9415 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9416 size_type_node = long_unsigned_type_node;
9417 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9418 size_type_node = long_long_unsigned_type_node;
9419 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9420 size_type_node = short_unsigned_type_node;
9421 else
9422 gcc_unreachable ();
9424 /* Fill in the rest of the sized types. Reuse existing type nodes
9425 when possible. */
9426 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9427 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9428 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9429 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9430 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9432 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9433 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9434 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9435 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9436 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9438 access_public_node = get_identifier ("public");
9439 access_protected_node = get_identifier ("protected");
9440 access_private_node = get_identifier ("private");
9442 /* Define these next since types below may used them. */
9443 integer_zero_node = build_int_cst (integer_type_node, 0);
9444 integer_one_node = build_int_cst (integer_type_node, 1);
9445 integer_three_node = build_int_cst (integer_type_node, 3);
9446 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9448 size_zero_node = size_int (0);
9449 size_one_node = size_int (1);
9450 bitsize_zero_node = bitsize_int (0);
9451 bitsize_one_node = bitsize_int (1);
9452 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9454 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9455 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9457 void_type_node = make_node (VOID_TYPE);
9458 layout_type (void_type_node);
9460 /* We are not going to have real types in C with less than byte alignment,
9461 so we might as well not have any types that claim to have it. */
9462 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9463 TYPE_USER_ALIGN (void_type_node) = 0;
9465 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9466 layout_type (TREE_TYPE (null_pointer_node));
9468 ptr_type_node = build_pointer_type (void_type_node);
9469 const_ptr_type_node
9470 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9471 fileptr_type_node = ptr_type_node;
9473 float_type_node = make_node (REAL_TYPE);
9474 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9475 layout_type (float_type_node);
9477 double_type_node = make_node (REAL_TYPE);
9478 if (short_double)
9479 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9480 else
9481 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9482 layout_type (double_type_node);
9484 long_double_type_node = make_node (REAL_TYPE);
9485 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9486 layout_type (long_double_type_node);
9488 float_ptr_type_node = build_pointer_type (float_type_node);
9489 double_ptr_type_node = build_pointer_type (double_type_node);
9490 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9491 integer_ptr_type_node = build_pointer_type (integer_type_node);
9493 /* Fixed size integer types. */
9494 uint16_type_node = build_nonstandard_integer_type (16, true);
9495 uint32_type_node = build_nonstandard_integer_type (32, true);
9496 uint64_type_node = build_nonstandard_integer_type (64, true);
9498 /* Decimal float types. */
9499 dfloat32_type_node = make_node (REAL_TYPE);
9500 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9501 layout_type (dfloat32_type_node);
9502 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9503 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9505 dfloat64_type_node = make_node (REAL_TYPE);
9506 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9507 layout_type (dfloat64_type_node);
9508 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9509 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9511 dfloat128_type_node = make_node (REAL_TYPE);
9512 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9513 layout_type (dfloat128_type_node);
9514 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9515 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9517 complex_integer_type_node = build_complex_type (integer_type_node);
9518 complex_float_type_node = build_complex_type (float_type_node);
9519 complex_double_type_node = build_complex_type (double_type_node);
9520 complex_long_double_type_node = build_complex_type (long_double_type_node);
9522 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9523 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9524 sat_ ## KIND ## _type_node = \
9525 make_sat_signed_ ## KIND ## _type (SIZE); \
9526 sat_unsigned_ ## KIND ## _type_node = \
9527 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9528 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9529 unsigned_ ## KIND ## _type_node = \
9530 make_unsigned_ ## KIND ## _type (SIZE);
9532 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9533 sat_ ## WIDTH ## KIND ## _type_node = \
9534 make_sat_signed_ ## KIND ## _type (SIZE); \
9535 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9536 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9537 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9538 unsigned_ ## WIDTH ## KIND ## _type_node = \
9539 make_unsigned_ ## KIND ## _type (SIZE);
9541 /* Make fixed-point type nodes based on four different widths. */
9542 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9543 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9544 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9545 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9546 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9548 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9549 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9550 NAME ## _type_node = \
9551 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9552 u ## NAME ## _type_node = \
9553 make_or_reuse_unsigned_ ## KIND ## _type \
9554 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9555 sat_ ## NAME ## _type_node = \
9556 make_or_reuse_sat_signed_ ## KIND ## _type \
9557 (GET_MODE_BITSIZE (MODE ## mode)); \
9558 sat_u ## NAME ## _type_node = \
9559 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9560 (GET_MODE_BITSIZE (U ## MODE ## mode));
9562 /* Fixed-point type and mode nodes. */
9563 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9564 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9565 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9566 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9567 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9568 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9569 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9570 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9571 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9572 MAKE_FIXED_MODE_NODE (accum, da, DA)
9573 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9576 tree t = targetm.build_builtin_va_list ();
9578 /* Many back-ends define record types without setting TYPE_NAME.
9579 If we copied the record type here, we'd keep the original
9580 record type without a name. This breaks name mangling. So,
9581 don't copy record types and let c_common_nodes_and_builtins()
9582 declare the type to be __builtin_va_list. */
9583 if (TREE_CODE (t) != RECORD_TYPE)
9584 t = build_variant_type_copy (t);
9586 va_list_type_node = t;
9590 /* Modify DECL for given flags. */
9591 void
9592 set_call_expr_flags (tree decl, int flags)
9594 if (flags & ECF_NOTHROW)
9595 TREE_NOTHROW (decl) = 1;
9596 if (flags & ECF_CONST)
9597 TREE_READONLY (decl) = 1;
9598 if (flags & ECF_PURE)
9599 DECL_PURE_P (decl) = 1;
9600 if (flags & ECF_LOOPING_CONST_OR_PURE)
9601 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9602 if (flags & ECF_NOVOPS)
9603 DECL_IS_NOVOPS (decl) = 1;
9604 if (flags & ECF_NORETURN)
9605 TREE_THIS_VOLATILE (decl) = 1;
9606 if (flags & ECF_MALLOC)
9607 DECL_IS_MALLOC (decl) = 1;
9608 if (flags & ECF_RETURNS_TWICE)
9609 DECL_IS_RETURNS_TWICE (decl) = 1;
9610 if (flags & ECF_LEAF)
9611 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9612 NULL, DECL_ATTRIBUTES (decl));
9613 if ((flags & ECF_TM_PURE) && flag_tm)
9614 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("transaction_pure"),
9615 NULL, DECL_ATTRIBUTES (decl));
9616 /* Looping const or pure is implied by noreturn.
9617 There is currently no way to declare looping const or looping pure alone. */
9618 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9619 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9623 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9625 static void
9626 local_define_builtin (const char *name, tree type, enum built_in_function code,
9627 const char *library_name, int ecf_flags)
9629 tree decl;
9631 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9632 library_name, NULL_TREE);
9633 set_call_expr_flags (decl, ecf_flags);
9635 set_builtin_decl (code, decl, true);
9638 /* Call this function after instantiating all builtins that the language
9639 front end cares about. This will build the rest of the builtins that
9640 are relied upon by the tree optimizers and the middle-end. */
9642 void
9643 build_common_builtin_nodes (void)
9645 tree tmp, ftype;
9646 int ecf_flags;
9648 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9650 ftype = build_function_type (void_type_node, void_list_node);
9651 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9652 "__builtin_unreachable",
9653 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9654 | ECF_CONST | ECF_LEAF);
9657 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9658 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9660 ftype = build_function_type_list (ptr_type_node,
9661 ptr_type_node, const_ptr_type_node,
9662 size_type_node, NULL_TREE);
9664 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9665 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9666 "memcpy", ECF_NOTHROW | ECF_LEAF);
9667 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9668 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9669 "memmove", ECF_NOTHROW | ECF_LEAF);
9672 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9674 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9675 const_ptr_type_node, size_type_node,
9676 NULL_TREE);
9677 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9678 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9681 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9683 ftype = build_function_type_list (ptr_type_node,
9684 ptr_type_node, integer_type_node,
9685 size_type_node, NULL_TREE);
9686 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9687 "memset", ECF_NOTHROW | ECF_LEAF);
9690 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9692 ftype = build_function_type_list (ptr_type_node,
9693 size_type_node, NULL_TREE);
9694 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9695 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9698 ftype = build_function_type_list (ptr_type_node, size_type_node,
9699 size_type_node, NULL_TREE);
9700 local_define_builtin ("__builtin_alloca_with_align", ftype,
9701 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
9702 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9704 /* If we're checking the stack, `alloca' can throw. */
9705 if (flag_stack_check)
9707 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
9708 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
9711 ftype = build_function_type_list (void_type_node,
9712 ptr_type_node, ptr_type_node,
9713 ptr_type_node, NULL_TREE);
9714 local_define_builtin ("__builtin_init_trampoline", ftype,
9715 BUILT_IN_INIT_TRAMPOLINE,
9716 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9717 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9718 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9719 "__builtin_init_heap_trampoline",
9720 ECF_NOTHROW | ECF_LEAF);
9722 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9723 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9724 BUILT_IN_ADJUST_TRAMPOLINE,
9725 "__builtin_adjust_trampoline",
9726 ECF_CONST | ECF_NOTHROW);
9728 ftype = build_function_type_list (void_type_node,
9729 ptr_type_node, ptr_type_node, NULL_TREE);
9730 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9731 BUILT_IN_NONLOCAL_GOTO,
9732 "__builtin_nonlocal_goto",
9733 ECF_NORETURN | ECF_NOTHROW);
9735 ftype = build_function_type_list (void_type_node,
9736 ptr_type_node, ptr_type_node, NULL_TREE);
9737 local_define_builtin ("__builtin_setjmp_setup", ftype,
9738 BUILT_IN_SETJMP_SETUP,
9739 "__builtin_setjmp_setup", ECF_NOTHROW);
9741 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9742 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
9743 BUILT_IN_SETJMP_DISPATCHER,
9744 "__builtin_setjmp_dispatcher",
9745 ECF_PURE | ECF_NOTHROW);
9747 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9748 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9749 BUILT_IN_SETJMP_RECEIVER,
9750 "__builtin_setjmp_receiver", ECF_NOTHROW);
9752 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9753 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9754 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9756 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9757 local_define_builtin ("__builtin_stack_restore", ftype,
9758 BUILT_IN_STACK_RESTORE,
9759 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9761 /* If there's a possibility that we might use the ARM EABI, build the
9762 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
9763 if (targetm.arm_eabi_unwinder)
9765 ftype = build_function_type_list (void_type_node, NULL_TREE);
9766 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9767 BUILT_IN_CXA_END_CLEANUP,
9768 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9771 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9772 local_define_builtin ("__builtin_unwind_resume", ftype,
9773 BUILT_IN_UNWIND_RESUME,
9774 ((targetm_common.except_unwind_info (&global_options)
9775 == UI_SJLJ)
9776 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9777 ECF_NORETURN);
9779 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
9781 ftype = build_function_type_list (ptr_type_node, integer_type_node,
9782 NULL_TREE);
9783 local_define_builtin ("__builtin_return_address", ftype,
9784 BUILT_IN_RETURN_ADDRESS,
9785 "__builtin_return_address",
9786 ECF_NOTHROW);
9789 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
9790 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9792 ftype = build_function_type_list (void_type_node, ptr_type_node,
9793 ptr_type_node, NULL_TREE);
9794 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
9795 local_define_builtin ("__cyg_profile_func_enter", ftype,
9796 BUILT_IN_PROFILE_FUNC_ENTER,
9797 "__cyg_profile_func_enter", 0);
9798 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9799 local_define_builtin ("__cyg_profile_func_exit", ftype,
9800 BUILT_IN_PROFILE_FUNC_EXIT,
9801 "__cyg_profile_func_exit", 0);
9804 /* The exception object and filter values from the runtime. The argument
9805 must be zero before exception lowering, i.e. from the front end. After
9806 exception lowering, it will be the region number for the exception
9807 landing pad. These functions are PURE instead of CONST to prevent
9808 them from being hoisted past the exception edge that will initialize
9809 its value in the landing pad. */
9810 ftype = build_function_type_list (ptr_type_node,
9811 integer_type_node, NULL_TREE);
9812 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
9813 /* Only use TM_PURE if we we have TM language support. */
9814 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
9815 ecf_flags |= ECF_TM_PURE;
9816 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
9817 "__builtin_eh_pointer", ecf_flags);
9819 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
9820 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
9821 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
9822 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9824 ftype = build_function_type_list (void_type_node,
9825 integer_type_node, integer_type_node,
9826 NULL_TREE);
9827 local_define_builtin ("__builtin_eh_copy_values", ftype,
9828 BUILT_IN_EH_COPY_VALUES,
9829 "__builtin_eh_copy_values", ECF_NOTHROW);
9831 /* Complex multiplication and division. These are handled as builtins
9832 rather than optabs because emit_library_call_value doesn't support
9833 complex. Further, we can do slightly better with folding these
9834 beasties if the real and complex parts of the arguments are separate. */
9836 int mode;
9838 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
9840 char mode_name_buf[4], *q;
9841 const char *p;
9842 enum built_in_function mcode, dcode;
9843 tree type, inner_type;
9844 const char *prefix = "__";
9846 if (targetm.libfunc_gnu_prefix)
9847 prefix = "__gnu_";
9849 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
9850 if (type == NULL)
9851 continue;
9852 inner_type = TREE_TYPE (type);
9854 ftype = build_function_type_list (type, inner_type, inner_type,
9855 inner_type, inner_type, NULL_TREE);
9857 mcode = ((enum built_in_function)
9858 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9859 dcode = ((enum built_in_function)
9860 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9862 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
9863 *q = TOLOWER (*p);
9864 *q = '\0';
9866 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
9867 NULL);
9868 local_define_builtin (built_in_names[mcode], ftype, mcode,
9869 built_in_names[mcode],
9870 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9872 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
9873 NULL);
9874 local_define_builtin (built_in_names[dcode], ftype, dcode,
9875 built_in_names[dcode],
9876 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9881 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
9882 better way.
9884 If we requested a pointer to a vector, build up the pointers that
9885 we stripped off while looking for the inner type. Similarly for
9886 return values from functions.
9888 The argument TYPE is the top of the chain, and BOTTOM is the
9889 new type which we will point to. */
9891 tree
9892 reconstruct_complex_type (tree type, tree bottom)
9894 tree inner, outer;
9896 if (TREE_CODE (type) == POINTER_TYPE)
9898 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9899 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
9900 TYPE_REF_CAN_ALIAS_ALL (type));
9902 else if (TREE_CODE (type) == REFERENCE_TYPE)
9904 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9905 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
9906 TYPE_REF_CAN_ALIAS_ALL (type));
9908 else if (TREE_CODE (type) == ARRAY_TYPE)
9910 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9911 outer = build_array_type (inner, TYPE_DOMAIN (type));
9913 else if (TREE_CODE (type) == FUNCTION_TYPE)
9915 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9916 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
9918 else if (TREE_CODE (type) == METHOD_TYPE)
9920 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9921 /* The build_method_type_directly() routine prepends 'this' to argument list,
9922 so we must compensate by getting rid of it. */
9923 outer
9924 = build_method_type_directly
9925 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
9926 inner,
9927 TREE_CHAIN (TYPE_ARG_TYPES (type)));
9929 else if (TREE_CODE (type) == OFFSET_TYPE)
9931 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9932 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
9934 else
9935 return bottom;
9937 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
9938 TYPE_QUALS (type));
9941 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
9942 the inner type. */
9943 tree
9944 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
9946 int nunits;
9948 switch (GET_MODE_CLASS (mode))
9950 case MODE_VECTOR_INT:
9951 case MODE_VECTOR_FLOAT:
9952 case MODE_VECTOR_FRACT:
9953 case MODE_VECTOR_UFRACT:
9954 case MODE_VECTOR_ACCUM:
9955 case MODE_VECTOR_UACCUM:
9956 nunits = GET_MODE_NUNITS (mode);
9957 break;
9959 case MODE_INT:
9960 /* Check that there are no leftover bits. */
9961 gcc_assert (GET_MODE_BITSIZE (mode)
9962 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
9964 nunits = GET_MODE_BITSIZE (mode)
9965 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
9966 break;
9968 default:
9969 gcc_unreachable ();
9972 return make_vector_type (innertype, nunits, mode);
9975 /* Similarly, but takes the inner type and number of units, which must be
9976 a power of two. */
9978 tree
9979 build_vector_type (tree innertype, int nunits)
9981 return make_vector_type (innertype, nunits, VOIDmode);
9984 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
9986 tree
9987 build_opaque_vector_type (tree innertype, int nunits)
9989 tree t = make_vector_type (innertype, nunits, VOIDmode);
9990 tree cand;
9991 /* We always build the non-opaque variant before the opaque one,
9992 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
9993 cand = TYPE_NEXT_VARIANT (t);
9994 if (cand
9995 && TYPE_VECTOR_OPAQUE (cand)
9996 && check_qualified_type (cand, t, TYPE_QUALS (t)))
9997 return cand;
9998 /* Othewise build a variant type and make sure to queue it after
9999 the non-opaque type. */
10000 cand = build_distinct_type_copy (t);
10001 TYPE_VECTOR_OPAQUE (cand) = true;
10002 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10003 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10004 TYPE_NEXT_VARIANT (t) = cand;
10005 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10006 return cand;
10010 /* Given an initializer INIT, return TRUE if INIT is zero or some
10011 aggregate of zeros. Otherwise return FALSE. */
10012 bool
10013 initializer_zerop (const_tree init)
10015 tree elt;
10017 STRIP_NOPS (init);
10019 switch (TREE_CODE (init))
10021 case INTEGER_CST:
10022 return integer_zerop (init);
10024 case REAL_CST:
10025 /* ??? Note that this is not correct for C4X float formats. There,
10026 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10027 negative exponent. */
10028 return real_zerop (init)
10029 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10031 case FIXED_CST:
10032 return fixed_zerop (init);
10034 case COMPLEX_CST:
10035 return integer_zerop (init)
10036 || (real_zerop (init)
10037 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10038 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10040 case VECTOR_CST:
10042 unsigned i;
10043 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10044 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10045 return false;
10046 return true;
10049 case CONSTRUCTOR:
10051 unsigned HOST_WIDE_INT idx;
10053 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10054 if (!initializer_zerop (elt))
10055 return false;
10056 return true;
10059 case STRING_CST:
10061 int i;
10063 /* We need to loop through all elements to handle cases like
10064 "\0" and "\0foobar". */
10065 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10066 if (TREE_STRING_POINTER (init)[i] != '\0')
10067 return false;
10069 return true;
10072 default:
10073 return false;
10077 /* Build an empty statement at location LOC. */
10079 tree
10080 build_empty_stmt (location_t loc)
10082 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10083 SET_EXPR_LOCATION (t, loc);
10084 return t;
10088 /* Build an OpenMP clause with code CODE. LOC is the location of the
10089 clause. */
10091 tree
10092 build_omp_clause (location_t loc, enum omp_clause_code code)
10094 tree t;
10095 int size, length;
10097 length = omp_clause_num_ops[code];
10098 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10100 record_node_allocation_statistics (OMP_CLAUSE, size);
10102 t = ggc_alloc_tree_node (size);
10103 memset (t, 0, size);
10104 TREE_SET_CODE (t, OMP_CLAUSE);
10105 OMP_CLAUSE_SET_CODE (t, code);
10106 OMP_CLAUSE_LOCATION (t) = loc;
10108 return t;
10111 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10112 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10113 Except for the CODE and operand count field, other storage for the
10114 object is initialized to zeros. */
10116 tree
10117 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10119 tree t;
10120 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10122 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10123 gcc_assert (len >= 1);
10125 record_node_allocation_statistics (code, length);
10127 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10129 TREE_SET_CODE (t, code);
10131 /* Can't use TREE_OPERAND to store the length because if checking is
10132 enabled, it will try to check the length before we store it. :-P */
10133 t->exp.operands[0] = build_int_cst (sizetype, len);
10135 return t;
10138 /* Helper function for build_call_* functions; build a CALL_EXPR with
10139 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10140 the argument slots. */
10142 static tree
10143 build_call_1 (tree return_type, tree fn, int nargs)
10145 tree t;
10147 t = build_vl_exp (CALL_EXPR, nargs + 3);
10148 TREE_TYPE (t) = return_type;
10149 CALL_EXPR_FN (t) = fn;
10150 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10152 return t;
10155 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10156 FN and a null static chain slot. NARGS is the number of call arguments
10157 which are specified as "..." arguments. */
10159 tree
10160 build_call_nary (tree return_type, tree fn, int nargs, ...)
10162 tree ret;
10163 va_list args;
10164 va_start (args, nargs);
10165 ret = build_call_valist (return_type, fn, nargs, args);
10166 va_end (args);
10167 return ret;
10170 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10171 FN and a null static chain slot. NARGS is the number of call arguments
10172 which are specified as a va_list ARGS. */
10174 tree
10175 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10177 tree t;
10178 int i;
10180 t = build_call_1 (return_type, fn, nargs);
10181 for (i = 0; i < nargs; i++)
10182 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10183 process_call_operands (t);
10184 return t;
10187 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10188 FN and a null static chain slot. NARGS is the number of call arguments
10189 which are specified as a tree array ARGS. */
10191 tree
10192 build_call_array_loc (location_t loc, tree return_type, tree fn,
10193 int nargs, const tree *args)
10195 tree t;
10196 int i;
10198 t = build_call_1 (return_type, fn, nargs);
10199 for (i = 0; i < nargs; i++)
10200 CALL_EXPR_ARG (t, i) = args[i];
10201 process_call_operands (t);
10202 SET_EXPR_LOCATION (t, loc);
10203 return t;
10206 /* Like build_call_array, but takes a vec. */
10208 tree
10209 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10211 tree ret, t;
10212 unsigned int ix;
10214 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10215 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10216 CALL_EXPR_ARG (ret, ix) = t;
10217 process_call_operands (ret);
10218 return ret;
10222 /* Returns true if it is possible to prove that the index of
10223 an array access REF (an ARRAY_REF expression) falls into the
10224 array bounds. */
10226 bool
10227 in_array_bounds_p (tree ref)
10229 tree idx = TREE_OPERAND (ref, 1);
10230 tree min, max;
10232 if (TREE_CODE (idx) != INTEGER_CST)
10233 return false;
10235 min = array_ref_low_bound (ref);
10236 max = array_ref_up_bound (ref);
10237 if (!min
10238 || !max
10239 || TREE_CODE (min) != INTEGER_CST
10240 || TREE_CODE (max) != INTEGER_CST)
10241 return false;
10243 if (tree_int_cst_lt (idx, min)
10244 || tree_int_cst_lt (max, idx))
10245 return false;
10247 return true;
10250 /* Returns true if it is possible to prove that the range of
10251 an array access REF (an ARRAY_RANGE_REF expression) falls
10252 into the array bounds. */
10254 bool
10255 range_in_array_bounds_p (tree ref)
10257 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
10258 tree range_min, range_max, min, max;
10260 range_min = TYPE_MIN_VALUE (domain_type);
10261 range_max = TYPE_MAX_VALUE (domain_type);
10262 if (!range_min
10263 || !range_max
10264 || TREE_CODE (range_min) != INTEGER_CST
10265 || TREE_CODE (range_max) != INTEGER_CST)
10266 return false;
10268 min = array_ref_low_bound (ref);
10269 max = array_ref_up_bound (ref);
10270 if (!min
10271 || !max
10272 || TREE_CODE (min) != INTEGER_CST
10273 || TREE_CODE (max) != INTEGER_CST)
10274 return false;
10276 if (tree_int_cst_lt (range_min, min)
10277 || tree_int_cst_lt (max, range_max))
10278 return false;
10280 return true;
10283 /* Return true if T (assumed to be a DECL) must be assigned a memory
10284 location. */
10286 bool
10287 needs_to_live_in_memory (const_tree t)
10289 return (TREE_ADDRESSABLE (t)
10290 || is_global_var (t)
10291 || (TREE_CODE (t) == RESULT_DECL
10292 && !DECL_BY_REFERENCE (t)
10293 && aggregate_value_p (t, current_function_decl)));
10296 /* Return value of a constant X and sign-extend it. */
10298 HOST_WIDE_INT
10299 int_cst_value (const_tree x)
10301 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10302 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10304 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10305 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10306 || TREE_INT_CST_HIGH (x) == -1);
10308 if (bits < HOST_BITS_PER_WIDE_INT)
10310 bool negative = ((val >> (bits - 1)) & 1) != 0;
10311 if (negative)
10312 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10313 else
10314 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10317 return val;
10320 /* Return value of a constant X and sign-extend it. */
10322 HOST_WIDEST_INT
10323 widest_int_cst_value (const_tree x)
10325 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10326 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10328 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10329 gcc_assert (HOST_BITS_PER_WIDEST_INT >= HOST_BITS_PER_DOUBLE_INT);
10330 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
10331 << HOST_BITS_PER_WIDE_INT);
10332 #else
10333 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10334 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10335 || TREE_INT_CST_HIGH (x) == -1);
10336 #endif
10338 if (bits < HOST_BITS_PER_WIDEST_INT)
10340 bool negative = ((val >> (bits - 1)) & 1) != 0;
10341 if (negative)
10342 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10343 else
10344 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10347 return val;
10350 /* If TYPE is an integral or pointer type, return an integer type with
10351 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10352 if TYPE is already an integer type of signedness UNSIGNEDP. */
10354 tree
10355 signed_or_unsigned_type_for (int unsignedp, tree type)
10357 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10358 return type;
10360 if (TREE_CODE (type) == VECTOR_TYPE)
10362 tree inner = TREE_TYPE (type);
10363 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10364 if (!inner2)
10365 return NULL_TREE;
10366 if (inner == inner2)
10367 return type;
10368 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10371 if (!INTEGRAL_TYPE_P (type)
10372 && !POINTER_TYPE_P (type))
10373 return NULL_TREE;
10375 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10378 /* If TYPE is an integral or pointer type, return an integer type with
10379 the same precision which is unsigned, or itself if TYPE is already an
10380 unsigned integer type. */
10382 tree
10383 unsigned_type_for (tree type)
10385 return signed_or_unsigned_type_for (1, type);
10388 /* If TYPE is an integral or pointer type, return an integer type with
10389 the same precision which is signed, or itself if TYPE is already a
10390 signed integer type. */
10392 tree
10393 signed_type_for (tree type)
10395 return signed_or_unsigned_type_for (0, type);
10398 /* If TYPE is a vector type, return a signed integer vector type with the
10399 same width and number of subparts. Otherwise return boolean_type_node. */
10401 tree
10402 truth_type_for (tree type)
10404 if (TREE_CODE (type) == VECTOR_TYPE)
10406 tree elem = lang_hooks.types.type_for_size
10407 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10408 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10410 else
10411 return boolean_type_node;
10414 /* Returns the largest value obtainable by casting something in INNER type to
10415 OUTER type. */
10417 tree
10418 upper_bound_in_type (tree outer, tree inner)
10420 double_int high;
10421 unsigned int det = 0;
10422 unsigned oprec = TYPE_PRECISION (outer);
10423 unsigned iprec = TYPE_PRECISION (inner);
10424 unsigned prec;
10426 /* Compute a unique number for every combination. */
10427 det |= (oprec > iprec) ? 4 : 0;
10428 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10429 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10431 /* Determine the exponent to use. */
10432 switch (det)
10434 case 0:
10435 case 1:
10436 /* oprec <= iprec, outer: signed, inner: don't care. */
10437 prec = oprec - 1;
10438 break;
10439 case 2:
10440 case 3:
10441 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10442 prec = oprec;
10443 break;
10444 case 4:
10445 /* oprec > iprec, outer: signed, inner: signed. */
10446 prec = iprec - 1;
10447 break;
10448 case 5:
10449 /* oprec > iprec, outer: signed, inner: unsigned. */
10450 prec = iprec;
10451 break;
10452 case 6:
10453 /* oprec > iprec, outer: unsigned, inner: signed. */
10454 prec = oprec;
10455 break;
10456 case 7:
10457 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10458 prec = iprec;
10459 break;
10460 default:
10461 gcc_unreachable ();
10464 /* Compute 2^^prec - 1. */
10465 if (prec <= HOST_BITS_PER_WIDE_INT)
10467 high.high = 0;
10468 high.low = ((~(unsigned HOST_WIDE_INT) 0)
10469 >> (HOST_BITS_PER_WIDE_INT - prec));
10471 else
10473 high.high = ((~(unsigned HOST_WIDE_INT) 0)
10474 >> (HOST_BITS_PER_DOUBLE_INT - prec));
10475 high.low = ~(unsigned HOST_WIDE_INT) 0;
10478 return double_int_to_tree (outer, high);
10481 /* Returns the smallest value obtainable by casting something in INNER type to
10482 OUTER type. */
10484 tree
10485 lower_bound_in_type (tree outer, tree inner)
10487 double_int low;
10488 unsigned oprec = TYPE_PRECISION (outer);
10489 unsigned iprec = TYPE_PRECISION (inner);
10491 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10492 and obtain 0. */
10493 if (TYPE_UNSIGNED (outer)
10494 /* If we are widening something of an unsigned type, OUTER type
10495 contains all values of INNER type. In particular, both INNER
10496 and OUTER types have zero in common. */
10497 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10498 low.low = low.high = 0;
10499 else
10501 /* If we are widening a signed type to another signed type, we
10502 want to obtain -2^^(iprec-1). If we are keeping the
10503 precision or narrowing to a signed type, we want to obtain
10504 -2^(oprec-1). */
10505 unsigned prec = oprec > iprec ? iprec : oprec;
10507 if (prec <= HOST_BITS_PER_WIDE_INT)
10509 low.high = ~(unsigned HOST_WIDE_INT) 0;
10510 low.low = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
10512 else
10514 low.high = ((~(unsigned HOST_WIDE_INT) 0)
10515 << (prec - HOST_BITS_PER_WIDE_INT - 1));
10516 low.low = 0;
10520 return double_int_to_tree (outer, low);
10523 /* Return nonzero if two operands that are suitable for PHI nodes are
10524 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10525 SSA_NAME or invariant. Note that this is strictly an optimization.
10526 That is, callers of this function can directly call operand_equal_p
10527 and get the same result, only slower. */
10530 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10532 if (arg0 == arg1)
10533 return 1;
10534 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10535 return 0;
10536 return operand_equal_p (arg0, arg1, 0);
10539 /* Returns number of zeros at the end of binary representation of X.
10541 ??? Use ffs if available? */
10543 tree
10544 num_ending_zeros (const_tree x)
10546 unsigned HOST_WIDE_INT fr, nfr;
10547 unsigned num, abits;
10548 tree type = TREE_TYPE (x);
10550 if (TREE_INT_CST_LOW (x) == 0)
10552 num = HOST_BITS_PER_WIDE_INT;
10553 fr = TREE_INT_CST_HIGH (x);
10555 else
10557 num = 0;
10558 fr = TREE_INT_CST_LOW (x);
10561 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
10563 nfr = fr >> abits;
10564 if (nfr << abits == fr)
10566 num += abits;
10567 fr = nfr;
10571 if (num > TYPE_PRECISION (type))
10572 num = TYPE_PRECISION (type);
10574 return build_int_cst_type (type, num);
10578 #define WALK_SUBTREE(NODE) \
10579 do \
10581 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10582 if (result) \
10583 return result; \
10585 while (0)
10587 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10588 be walked whenever a type is seen in the tree. Rest of operands and return
10589 value are as for walk_tree. */
10591 static tree
10592 walk_type_fields (tree type, walk_tree_fn func, void *data,
10593 struct pointer_set_t *pset, walk_tree_lh lh)
10595 tree result = NULL_TREE;
10597 switch (TREE_CODE (type))
10599 case POINTER_TYPE:
10600 case REFERENCE_TYPE:
10601 /* We have to worry about mutually recursive pointers. These can't
10602 be written in C. They can in Ada. It's pathological, but
10603 there's an ACATS test (c38102a) that checks it. Deal with this
10604 by checking if we're pointing to another pointer, that one
10605 points to another pointer, that one does too, and we have no htab.
10606 If so, get a hash table. We check three levels deep to avoid
10607 the cost of the hash table if we don't need one. */
10608 if (POINTER_TYPE_P (TREE_TYPE (type))
10609 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10610 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10611 && !pset)
10613 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10614 func, data);
10615 if (result)
10616 return result;
10618 break;
10621 /* ... fall through ... */
10623 case COMPLEX_TYPE:
10624 WALK_SUBTREE (TREE_TYPE (type));
10625 break;
10627 case METHOD_TYPE:
10628 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10630 /* Fall through. */
10632 case FUNCTION_TYPE:
10633 WALK_SUBTREE (TREE_TYPE (type));
10635 tree arg;
10637 /* We never want to walk into default arguments. */
10638 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10639 WALK_SUBTREE (TREE_VALUE (arg));
10641 break;
10643 case ARRAY_TYPE:
10644 /* Don't follow this nodes's type if a pointer for fear that
10645 we'll have infinite recursion. If we have a PSET, then we
10646 need not fear. */
10647 if (pset
10648 || (!POINTER_TYPE_P (TREE_TYPE (type))
10649 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10650 WALK_SUBTREE (TREE_TYPE (type));
10651 WALK_SUBTREE (TYPE_DOMAIN (type));
10652 break;
10654 case OFFSET_TYPE:
10655 WALK_SUBTREE (TREE_TYPE (type));
10656 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10657 break;
10659 default:
10660 break;
10663 return NULL_TREE;
10666 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10667 called with the DATA and the address of each sub-tree. If FUNC returns a
10668 non-NULL value, the traversal is stopped, and the value returned by FUNC
10669 is returned. If PSET is non-NULL it is used to record the nodes visited,
10670 and to avoid visiting a node more than once. */
10672 tree
10673 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10674 struct pointer_set_t *pset, walk_tree_lh lh)
10676 enum tree_code code;
10677 int walk_subtrees;
10678 tree result;
10680 #define WALK_SUBTREE_TAIL(NODE) \
10681 do \
10683 tp = & (NODE); \
10684 goto tail_recurse; \
10686 while (0)
10688 tail_recurse:
10689 /* Skip empty subtrees. */
10690 if (!*tp)
10691 return NULL_TREE;
10693 /* Don't walk the same tree twice, if the user has requested
10694 that we avoid doing so. */
10695 if (pset && pointer_set_insert (pset, *tp))
10696 return NULL_TREE;
10698 /* Call the function. */
10699 walk_subtrees = 1;
10700 result = (*func) (tp, &walk_subtrees, data);
10702 /* If we found something, return it. */
10703 if (result)
10704 return result;
10706 code = TREE_CODE (*tp);
10708 /* Even if we didn't, FUNC may have decided that there was nothing
10709 interesting below this point in the tree. */
10710 if (!walk_subtrees)
10712 /* But we still need to check our siblings. */
10713 if (code == TREE_LIST)
10714 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10715 else if (code == OMP_CLAUSE)
10716 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10717 else
10718 return NULL_TREE;
10721 if (lh)
10723 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10724 if (result || !walk_subtrees)
10725 return result;
10728 switch (code)
10730 case ERROR_MARK:
10731 case IDENTIFIER_NODE:
10732 case INTEGER_CST:
10733 case REAL_CST:
10734 case FIXED_CST:
10735 case VECTOR_CST:
10736 case STRING_CST:
10737 case BLOCK:
10738 case PLACEHOLDER_EXPR:
10739 case SSA_NAME:
10740 case FIELD_DECL:
10741 case RESULT_DECL:
10742 /* None of these have subtrees other than those already walked
10743 above. */
10744 break;
10746 case TREE_LIST:
10747 WALK_SUBTREE (TREE_VALUE (*tp));
10748 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10749 break;
10751 case TREE_VEC:
10753 int len = TREE_VEC_LENGTH (*tp);
10755 if (len == 0)
10756 break;
10758 /* Walk all elements but the first. */
10759 while (--len)
10760 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
10762 /* Now walk the first one as a tail call. */
10763 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
10766 case COMPLEX_CST:
10767 WALK_SUBTREE (TREE_REALPART (*tp));
10768 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
10770 case CONSTRUCTOR:
10772 unsigned HOST_WIDE_INT idx;
10773 constructor_elt *ce;
10775 for (idx = 0; vec_safe_iterate(CONSTRUCTOR_ELTS (*tp), idx, &ce); idx++)
10776 WALK_SUBTREE (ce->value);
10778 break;
10780 case SAVE_EXPR:
10781 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
10783 case BIND_EXPR:
10785 tree decl;
10786 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
10788 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
10789 into declarations that are just mentioned, rather than
10790 declared; they don't really belong to this part of the tree.
10791 And, we can see cycles: the initializer for a declaration
10792 can refer to the declaration itself. */
10793 WALK_SUBTREE (DECL_INITIAL (decl));
10794 WALK_SUBTREE (DECL_SIZE (decl));
10795 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
10797 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
10800 case STATEMENT_LIST:
10802 tree_stmt_iterator i;
10803 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
10804 WALK_SUBTREE (*tsi_stmt_ptr (i));
10806 break;
10808 case OMP_CLAUSE:
10809 switch (OMP_CLAUSE_CODE (*tp))
10811 case OMP_CLAUSE_PRIVATE:
10812 case OMP_CLAUSE_SHARED:
10813 case OMP_CLAUSE_FIRSTPRIVATE:
10814 case OMP_CLAUSE_COPYIN:
10815 case OMP_CLAUSE_COPYPRIVATE:
10816 case OMP_CLAUSE_FINAL:
10817 case OMP_CLAUSE_IF:
10818 case OMP_CLAUSE_NUM_THREADS:
10819 case OMP_CLAUSE_SCHEDULE:
10820 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
10821 /* FALLTHRU */
10823 case OMP_CLAUSE_NOWAIT:
10824 case OMP_CLAUSE_ORDERED:
10825 case OMP_CLAUSE_DEFAULT:
10826 case OMP_CLAUSE_UNTIED:
10827 case OMP_CLAUSE_MERGEABLE:
10828 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10830 case OMP_CLAUSE_LASTPRIVATE:
10831 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
10832 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
10833 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10835 case OMP_CLAUSE_COLLAPSE:
10837 int i;
10838 for (i = 0; i < 3; i++)
10839 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10840 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10843 case OMP_CLAUSE_REDUCTION:
10845 int i;
10846 for (i = 0; i < 4; i++)
10847 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10848 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10851 default:
10852 gcc_unreachable ();
10854 break;
10856 case TARGET_EXPR:
10858 int i, len;
10860 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
10861 But, we only want to walk once. */
10862 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
10863 for (i = 0; i < len; ++i)
10864 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10865 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
10868 case DECL_EXPR:
10869 /* If this is a TYPE_DECL, walk into the fields of the type that it's
10870 defining. We only want to walk into these fields of a type in this
10871 case and not in the general case of a mere reference to the type.
10873 The criterion is as follows: if the field can be an expression, it
10874 must be walked only here. This should be in keeping with the fields
10875 that are directly gimplified in gimplify_type_sizes in order for the
10876 mark/copy-if-shared/unmark machinery of the gimplifier to work with
10877 variable-sized types.
10879 Note that DECLs get walked as part of processing the BIND_EXPR. */
10880 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
10882 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
10883 if (TREE_CODE (*type_p) == ERROR_MARK)
10884 return NULL_TREE;
10886 /* Call the function for the type. See if it returns anything or
10887 doesn't want us to continue. If we are to continue, walk both
10888 the normal fields and those for the declaration case. */
10889 result = (*func) (type_p, &walk_subtrees, data);
10890 if (result || !walk_subtrees)
10891 return result;
10893 /* But do not walk a pointed-to type since it may itself need to
10894 be walked in the declaration case if it isn't anonymous. */
10895 if (!POINTER_TYPE_P (*type_p))
10897 result = walk_type_fields (*type_p, func, data, pset, lh);
10898 if (result)
10899 return result;
10902 /* If this is a record type, also walk the fields. */
10903 if (RECORD_OR_UNION_TYPE_P (*type_p))
10905 tree field;
10907 for (field = TYPE_FIELDS (*type_p); field;
10908 field = DECL_CHAIN (field))
10910 /* We'd like to look at the type of the field, but we can
10911 easily get infinite recursion. So assume it's pointed
10912 to elsewhere in the tree. Also, ignore things that
10913 aren't fields. */
10914 if (TREE_CODE (field) != FIELD_DECL)
10915 continue;
10917 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
10918 WALK_SUBTREE (DECL_SIZE (field));
10919 WALK_SUBTREE (DECL_SIZE_UNIT (field));
10920 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
10921 WALK_SUBTREE (DECL_QUALIFIER (field));
10925 /* Same for scalar types. */
10926 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
10927 || TREE_CODE (*type_p) == ENUMERAL_TYPE
10928 || TREE_CODE (*type_p) == INTEGER_TYPE
10929 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
10930 || TREE_CODE (*type_p) == REAL_TYPE)
10932 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
10933 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
10936 WALK_SUBTREE (TYPE_SIZE (*type_p));
10937 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
10939 /* FALLTHRU */
10941 default:
10942 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
10944 int i, len;
10946 /* Walk over all the sub-trees of this operand. */
10947 len = TREE_OPERAND_LENGTH (*tp);
10949 /* Go through the subtrees. We need to do this in forward order so
10950 that the scope of a FOR_EXPR is handled properly. */
10951 if (len)
10953 for (i = 0; i < len - 1; ++i)
10954 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10955 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
10958 /* If this is a type, walk the needed fields in the type. */
10959 else if (TYPE_P (*tp))
10960 return walk_type_fields (*tp, func, data, pset, lh);
10961 break;
10964 /* We didn't find what we were looking for. */
10965 return NULL_TREE;
10967 #undef WALK_SUBTREE_TAIL
10969 #undef WALK_SUBTREE
10971 /* Like walk_tree, but does not walk duplicate nodes more than once. */
10973 tree
10974 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
10975 walk_tree_lh lh)
10977 tree result;
10978 struct pointer_set_t *pset;
10980 pset = pointer_set_create ();
10981 result = walk_tree_1 (tp, func, data, pset, lh);
10982 pointer_set_destroy (pset);
10983 return result;
10987 tree
10988 tree_block (tree t)
10990 char const c = TREE_CODE_CLASS (TREE_CODE (t));
10992 if (IS_EXPR_CODE_CLASS (c))
10993 return LOCATION_BLOCK (t->exp.locus);
10994 gcc_unreachable ();
10995 return NULL;
10998 void
10999 tree_set_block (tree t, tree b)
11001 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11003 if (IS_EXPR_CODE_CLASS (c))
11005 if (b)
11006 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11007 else
11008 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11010 else
11011 gcc_unreachable ();
11014 /* Create a nameless artificial label and put it in the current
11015 function context. The label has a location of LOC. Returns the
11016 newly created label. */
11018 tree
11019 create_artificial_label (location_t loc)
11021 tree lab = build_decl (loc,
11022 LABEL_DECL, NULL_TREE, void_type_node);
11024 DECL_ARTIFICIAL (lab) = 1;
11025 DECL_IGNORED_P (lab) = 1;
11026 DECL_CONTEXT (lab) = current_function_decl;
11027 return lab;
11030 /* Given a tree, try to return a useful variable name that we can use
11031 to prefix a temporary that is being assigned the value of the tree.
11032 I.E. given <temp> = &A, return A. */
11034 const char *
11035 get_name (tree t)
11037 tree stripped_decl;
11039 stripped_decl = t;
11040 STRIP_NOPS (stripped_decl);
11041 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11042 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11043 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11045 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11046 if (!name)
11047 return NULL;
11048 return IDENTIFIER_POINTER (name);
11050 else
11052 switch (TREE_CODE (stripped_decl))
11054 case ADDR_EXPR:
11055 return get_name (TREE_OPERAND (stripped_decl, 0));
11056 default:
11057 return NULL;
11062 /* Return true if TYPE has a variable argument list. */
11064 bool
11065 stdarg_p (const_tree fntype)
11067 function_args_iterator args_iter;
11068 tree n = NULL_TREE, t;
11070 if (!fntype)
11071 return false;
11073 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
11075 n = t;
11078 return n != NULL_TREE && n != void_type_node;
11081 /* Return true if TYPE has a prototype. */
11083 bool
11084 prototype_p (tree fntype)
11086 tree t;
11088 gcc_assert (fntype != NULL_TREE);
11090 t = TYPE_ARG_TYPES (fntype);
11091 return (t != NULL_TREE);
11094 /* If BLOCK is inlined from an __attribute__((__artificial__))
11095 routine, return pointer to location from where it has been
11096 called. */
11097 location_t *
11098 block_nonartificial_location (tree block)
11100 location_t *ret = NULL;
11102 while (block && TREE_CODE (block) == BLOCK
11103 && BLOCK_ABSTRACT_ORIGIN (block))
11105 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11107 while (TREE_CODE (ao) == BLOCK
11108 && BLOCK_ABSTRACT_ORIGIN (ao)
11109 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11110 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11112 if (TREE_CODE (ao) == FUNCTION_DECL)
11114 /* If AO is an artificial inline, point RET to the
11115 call site locus at which it has been inlined and continue
11116 the loop, in case AO's caller is also an artificial
11117 inline. */
11118 if (DECL_DECLARED_INLINE_P (ao)
11119 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11120 ret = &BLOCK_SOURCE_LOCATION (block);
11121 else
11122 break;
11124 else if (TREE_CODE (ao) != BLOCK)
11125 break;
11127 block = BLOCK_SUPERCONTEXT (block);
11129 return ret;
11133 /* If EXP is inlined from an __attribute__((__artificial__))
11134 function, return the location of the original call expression. */
11136 location_t
11137 tree_nonartificial_location (tree exp)
11139 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11141 if (loc)
11142 return *loc;
11143 else
11144 return EXPR_LOCATION (exp);
11148 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11149 nodes. */
11151 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11153 static hashval_t
11154 cl_option_hash_hash (const void *x)
11156 const_tree const t = (const_tree) x;
11157 const char *p;
11158 size_t i;
11159 size_t len = 0;
11160 hashval_t hash = 0;
11162 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11164 p = (const char *)TREE_OPTIMIZATION (t);
11165 len = sizeof (struct cl_optimization);
11168 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11170 p = (const char *)TREE_TARGET_OPTION (t);
11171 len = sizeof (struct cl_target_option);
11174 else
11175 gcc_unreachable ();
11177 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11178 something else. */
11179 for (i = 0; i < len; i++)
11180 if (p[i])
11181 hash = (hash << 4) ^ ((i << 2) | p[i]);
11183 return hash;
11186 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11187 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11188 same. */
11190 static int
11191 cl_option_hash_eq (const void *x, const void *y)
11193 const_tree const xt = (const_tree) x;
11194 const_tree const yt = (const_tree) y;
11195 const char *xp;
11196 const char *yp;
11197 size_t len;
11199 if (TREE_CODE (xt) != TREE_CODE (yt))
11200 return 0;
11202 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11204 xp = (const char *)TREE_OPTIMIZATION (xt);
11205 yp = (const char *)TREE_OPTIMIZATION (yt);
11206 len = sizeof (struct cl_optimization);
11209 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11211 xp = (const char *)TREE_TARGET_OPTION (xt);
11212 yp = (const char *)TREE_TARGET_OPTION (yt);
11213 len = sizeof (struct cl_target_option);
11216 else
11217 gcc_unreachable ();
11219 return (memcmp (xp, yp, len) == 0);
11222 /* Build an OPTIMIZATION_NODE based on the current options. */
11224 tree
11225 build_optimization_node (void)
11227 tree t;
11228 void **slot;
11230 /* Use the cache of optimization nodes. */
11232 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11233 &global_options);
11235 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11236 t = (tree) *slot;
11237 if (!t)
11239 /* Insert this one into the hash table. */
11240 t = cl_optimization_node;
11241 *slot = t;
11243 /* Make a new node for next time round. */
11244 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11247 return t;
11250 /* Build a TARGET_OPTION_NODE based on the current options. */
11252 tree
11253 build_target_option_node (void)
11255 tree t;
11256 void **slot;
11258 /* Use the cache of optimization nodes. */
11260 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11261 &global_options);
11263 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11264 t = (tree) *slot;
11265 if (!t)
11267 /* Insert this one into the hash table. */
11268 t = cl_target_option_node;
11269 *slot = t;
11271 /* Make a new node for next time round. */
11272 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11275 return t;
11278 /* Determine the "ultimate origin" of a block. The block may be an inlined
11279 instance of an inlined instance of a block which is local to an inline
11280 function, so we have to trace all of the way back through the origin chain
11281 to find out what sort of node actually served as the original seed for the
11282 given block. */
11284 tree
11285 block_ultimate_origin (const_tree block)
11287 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11289 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
11290 nodes in the function to point to themselves; ignore that if
11291 we're trying to output the abstract instance of this function. */
11292 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11293 return NULL_TREE;
11295 if (immediate_origin == NULL_TREE)
11296 return NULL_TREE;
11297 else
11299 tree ret_val;
11300 tree lookahead = immediate_origin;
11304 ret_val = lookahead;
11305 lookahead = (TREE_CODE (ret_val) == BLOCK
11306 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11308 while (lookahead != NULL && lookahead != ret_val);
11310 /* The block's abstract origin chain may not be the *ultimate* origin of
11311 the block. It could lead to a DECL that has an abstract origin set.
11312 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11313 will give us if it has one). Note that DECL's abstract origins are
11314 supposed to be the most distant ancestor (or so decl_ultimate_origin
11315 claims), so we don't need to loop following the DECL origins. */
11316 if (DECL_P (ret_val))
11317 return DECL_ORIGIN (ret_val);
11319 return ret_val;
11323 /* Return true if T1 and T2 are equivalent lists. */
11325 bool
11326 list_equal_p (const_tree t1, const_tree t2)
11328 for (; t1 && t2; t1 = TREE_CHAIN (t1) , t2 = TREE_CHAIN (t2))
11329 if (TREE_VALUE (t1) != TREE_VALUE (t2))
11330 return false;
11331 return !t1 && !t2;
11334 /* Return true iff conversion in EXP generates no instruction. Mark
11335 it inline so that we fully inline into the stripping functions even
11336 though we have two uses of this function. */
11338 static inline bool
11339 tree_nop_conversion (const_tree exp)
11341 tree outer_type, inner_type;
11343 if (!CONVERT_EXPR_P (exp)
11344 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11345 return false;
11346 if (TREE_OPERAND (exp, 0) == error_mark_node)
11347 return false;
11349 outer_type = TREE_TYPE (exp);
11350 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11352 if (!inner_type)
11353 return false;
11355 /* Use precision rather then machine mode when we can, which gives
11356 the correct answer even for submode (bit-field) types. */
11357 if ((INTEGRAL_TYPE_P (outer_type)
11358 || POINTER_TYPE_P (outer_type)
11359 || TREE_CODE (outer_type) == OFFSET_TYPE)
11360 && (INTEGRAL_TYPE_P (inner_type)
11361 || POINTER_TYPE_P (inner_type)
11362 || TREE_CODE (inner_type) == OFFSET_TYPE))
11363 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11365 /* Otherwise fall back on comparing machine modes (e.g. for
11366 aggregate types, floats). */
11367 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11370 /* Return true iff conversion in EXP generates no instruction. Don't
11371 consider conversions changing the signedness. */
11373 static bool
11374 tree_sign_nop_conversion (const_tree exp)
11376 tree outer_type, inner_type;
11378 if (!tree_nop_conversion (exp))
11379 return false;
11381 outer_type = TREE_TYPE (exp);
11382 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11384 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11385 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11388 /* Strip conversions from EXP according to tree_nop_conversion and
11389 return the resulting expression. */
11391 tree
11392 tree_strip_nop_conversions (tree exp)
11394 while (tree_nop_conversion (exp))
11395 exp = TREE_OPERAND (exp, 0);
11396 return exp;
11399 /* Strip conversions from EXP according to tree_sign_nop_conversion
11400 and return the resulting expression. */
11402 tree
11403 tree_strip_sign_nop_conversions (tree exp)
11405 while (tree_sign_nop_conversion (exp))
11406 exp = TREE_OPERAND (exp, 0);
11407 return exp;
11410 /* Avoid any floating point extensions from EXP. */
11411 tree
11412 strip_float_extensions (tree exp)
11414 tree sub, expt, subt;
11416 /* For floating point constant look up the narrowest type that can hold
11417 it properly and handle it like (type)(narrowest_type)constant.
11418 This way we can optimize for instance a=a*2.0 where "a" is float
11419 but 2.0 is double constant. */
11420 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11422 REAL_VALUE_TYPE orig;
11423 tree type = NULL;
11425 orig = TREE_REAL_CST (exp);
11426 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11427 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11428 type = float_type_node;
11429 else if (TYPE_PRECISION (TREE_TYPE (exp))
11430 > TYPE_PRECISION (double_type_node)
11431 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11432 type = double_type_node;
11433 if (type)
11434 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11437 if (!CONVERT_EXPR_P (exp))
11438 return exp;
11440 sub = TREE_OPERAND (exp, 0);
11441 subt = TREE_TYPE (sub);
11442 expt = TREE_TYPE (exp);
11444 if (!FLOAT_TYPE_P (subt))
11445 return exp;
11447 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11448 return exp;
11450 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11451 return exp;
11453 return strip_float_extensions (sub);
11456 /* Strip out all handled components that produce invariant
11457 offsets. */
11459 const_tree
11460 strip_invariant_refs (const_tree op)
11462 while (handled_component_p (op))
11464 switch (TREE_CODE (op))
11466 case ARRAY_REF:
11467 case ARRAY_RANGE_REF:
11468 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11469 || TREE_OPERAND (op, 2) != NULL_TREE
11470 || TREE_OPERAND (op, 3) != NULL_TREE)
11471 return NULL;
11472 break;
11474 case COMPONENT_REF:
11475 if (TREE_OPERAND (op, 2) != NULL_TREE)
11476 return NULL;
11477 break;
11479 default:;
11481 op = TREE_OPERAND (op, 0);
11484 return op;
11487 static GTY(()) tree gcc_eh_personality_decl;
11489 /* Return the GCC personality function decl. */
11491 tree
11492 lhd_gcc_personality (void)
11494 if (!gcc_eh_personality_decl)
11495 gcc_eh_personality_decl = build_personality_function ("gcc");
11496 return gcc_eh_personality_decl;
11499 /* Try to find a base info of BINFO that would have its field decl at offset
11500 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11501 found, return, otherwise return NULL_TREE. */
11503 tree
11504 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11506 tree type = BINFO_TYPE (binfo);
11508 while (true)
11510 HOST_WIDE_INT pos, size;
11511 tree fld;
11512 int i;
11514 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (expected_type))
11515 return binfo;
11516 if (offset < 0)
11517 return NULL_TREE;
11519 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11521 if (TREE_CODE (fld) != FIELD_DECL)
11522 continue;
11524 pos = int_bit_position (fld);
11525 size = tree_low_cst (DECL_SIZE (fld), 1);
11526 if (pos <= offset && (pos + size) > offset)
11527 break;
11529 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11530 return NULL_TREE;
11532 if (!DECL_ARTIFICIAL (fld))
11534 binfo = TYPE_BINFO (TREE_TYPE (fld));
11535 if (!binfo)
11536 return NULL_TREE;
11538 /* Offset 0 indicates the primary base, whose vtable contents are
11539 represented in the binfo for the derived class. */
11540 else if (offset != 0)
11542 tree base_binfo, found_binfo = NULL_TREE;
11543 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11544 if (TREE_TYPE (base_binfo) == TREE_TYPE (fld))
11546 found_binfo = base_binfo;
11547 break;
11549 if (!found_binfo)
11550 return NULL_TREE;
11551 binfo = found_binfo;
11554 type = TREE_TYPE (fld);
11555 offset -= pos;
11559 /* Returns true if X is a typedef decl. */
11561 bool
11562 is_typedef_decl (tree x)
11564 return (x && TREE_CODE (x) == TYPE_DECL
11565 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
11568 /* Returns true iff TYPE is a type variant created for a typedef. */
11570 bool
11571 typedef_variant_p (tree type)
11573 return is_typedef_decl (TYPE_NAME (type));
11576 /* Warn about a use of an identifier which was marked deprecated. */
11577 void
11578 warn_deprecated_use (tree node, tree attr)
11580 const char *msg;
11582 if (node == 0 || !warn_deprecated_decl)
11583 return;
11585 if (!attr)
11587 if (DECL_P (node))
11588 attr = DECL_ATTRIBUTES (node);
11589 else if (TYPE_P (node))
11591 tree decl = TYPE_STUB_DECL (node);
11592 if (decl)
11593 attr = lookup_attribute ("deprecated",
11594 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
11598 if (attr)
11599 attr = lookup_attribute ("deprecated", attr);
11601 if (attr)
11602 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
11603 else
11604 msg = NULL;
11606 if (DECL_P (node))
11608 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
11609 if (msg)
11610 warning (OPT_Wdeprecated_declarations,
11611 "%qD is deprecated (declared at %s:%d): %s",
11612 node, xloc.file, xloc.line, msg);
11613 else
11614 warning (OPT_Wdeprecated_declarations,
11615 "%qD is deprecated (declared at %s:%d)",
11616 node, xloc.file, xloc.line);
11618 else if (TYPE_P (node))
11620 tree what = NULL_TREE;
11621 tree decl = TYPE_STUB_DECL (node);
11623 if (TYPE_NAME (node))
11625 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
11626 what = TYPE_NAME (node);
11627 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
11628 && DECL_NAME (TYPE_NAME (node)))
11629 what = DECL_NAME (TYPE_NAME (node));
11632 if (decl)
11634 expanded_location xloc
11635 = expand_location (DECL_SOURCE_LOCATION (decl));
11636 if (what)
11638 if (msg)
11639 warning (OPT_Wdeprecated_declarations,
11640 "%qE is deprecated (declared at %s:%d): %s",
11641 what, xloc.file, xloc.line, msg);
11642 else
11643 warning (OPT_Wdeprecated_declarations,
11644 "%qE is deprecated (declared at %s:%d)", what,
11645 xloc.file, xloc.line);
11647 else
11649 if (msg)
11650 warning (OPT_Wdeprecated_declarations,
11651 "type is deprecated (declared at %s:%d): %s",
11652 xloc.file, xloc.line, msg);
11653 else
11654 warning (OPT_Wdeprecated_declarations,
11655 "type is deprecated (declared at %s:%d)",
11656 xloc.file, xloc.line);
11659 else
11661 if (what)
11663 if (msg)
11664 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
11665 what, msg);
11666 else
11667 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
11669 else
11671 if (msg)
11672 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
11673 msg);
11674 else
11675 warning (OPT_Wdeprecated_declarations, "type is deprecated");
11681 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
11682 somewhere in it. */
11684 bool
11685 contains_bitfld_component_ref_p (const_tree ref)
11687 while (handled_component_p (ref))
11689 if (TREE_CODE (ref) == COMPONENT_REF
11690 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
11691 return true;
11692 ref = TREE_OPERAND (ref, 0);
11695 return false;
11698 #include "gt-tree.h"